2009-07-17 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / DataGridViewRowHeaderTest.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Rolf Bjarne Kvinge  (RKvinge@novell.com)
24 //
25
26
27 #if NET_2_0
28
29 using NUnit.Framework;
30 using System;
31 using System.Drawing;
32 using System.Windows.Forms;
33 using System.ComponentModel;
34 using System.Collections;
35
36 namespace MonoTests.System.Windows.Forms
37 {
38         [TestFixture]
39         public class DataGridViewRowHeaderTest : TestHelper
40         {
41
42                 [Test]
43                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
44                 public void GetClipboardContentTestException ()
45                 {
46                         DataGridViewRowHeaderClipboardCell cell = new DataGridViewRowHeaderClipboardCell ();
47                         
48                         using (DataGridView dgv = new DataGridView ()) {
49                                 DataGridViewColumn col = new DataGridViewColumn ();
50                                 col.CellTemplate = new DataGridViewTextBoxCell ();
51                                 dgv.Columns.Add (col);
52                                 DataGridViewRow row = new DataGridViewRow ();
53                                 row.HeaderCell = cell;
54                                 dgv.Rows.Add (row);
55                                 dgv.Rows [0].SetValues ("abc");
56                                 dgv.Rows [0].Cells [0].Selected = true;
57
58                                 cell = dgv.Rows [0].HeaderCell as DataGridViewRowHeaderClipboardCell;
59                                 cell.GetClipboardContentPublic (-1, false, false, false, false, "Text");
60
61                         }
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
66                 public void GetClipboardContentTestException2 ()
67                 {
68                         DataGridViewRowHeaderClipboardCell cell = new DataGridViewRowHeaderClipboardCell ();
69
70                         using (DataGridView dgv = new DataGridView ()) {
71                                 DataGridViewColumn col = new DataGridViewColumn ();
72                                 col.CellTemplate = new DataGridViewTextBoxCell ();
73                                 dgv.Columns.Add (col);
74                                 DataGridViewRow row = new DataGridViewRow ();
75                                 row.HeaderCell = cell;
76                                 dgv.Rows.Add (row);
77                                 dgv.Rows [0].SetValues ("abc");
78                                 dgv.Rows [0].Cells [0].Selected = true;
79
80                                 cell = dgv.Rows [0].HeaderCell as DataGridViewRowHeaderClipboardCell;
81                                 cell.GetClipboardContentPublic (2, false, false, false, false, "Text");
82
83                         }
84                 }
85                 
86                 
87                 public class DataGridViewRowHeaderClipboardCell : DataGridViewRowHeaderCell
88                 {
89                         public DataGridViewRowHeaderClipboardCell ()
90                         {
91                         }
92
93                         public object GetClipboardContentPublic (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format)
94                         {
95                                 return GetClipboardContent (rowIndex, firstCell, lastCell, inFirstRow, inLastRow, format);
96                         }
97
98                         public override Type FormattedValueType
99                         {
100                                 get
101                                 {
102                                         return typeof (string);
103                                 }
104                         }
105                 }
106
107                 /* font dependent
108                 [Test]
109                 public void MethodGetContentBounds ()
110                 {
111                         BaseCell c = new BaseCell ();
112                         Assert.AreEqual (Rectangle.Empty, c.GetContentBounds (c.RowIndex), "A1");
113                         c.Value = "hello there";
114                         
115                         DataGridView dgv = new DataGridView ();
116                         dgv.Columns.Add ("hi", "there");
117
118                         DataGridViewRow row = new DataGridViewRow ();
119                         row.HeaderCell = c;
120                         dgv.Rows.Add (row);
121
122                         Assert.AreEqual (new Size (41, 22), dgv.Rows[0].HeaderCell.Size, "A1-1");
123                         Assert.AreEqual (new Rectangle (24, 2, 11, 18), dgv.Rows[0].HeaderCell.GetContentBounds (dgv.Rows[0].HeaderCell.RowIndex), "A2");
124
125                         dgv.Rows[0].HeaderCell.Value = "whoa whoa whoa whoa";
126                         Assert.AreEqual (new Rectangle (24, 2, 11, 18), dgv.Rows[0].HeaderCell.GetContentBounds (dgv.Rows[0].HeaderCell.RowIndex), "A3");
127                 }
128                 */
129                 
130                 [Test]
131                 public void MethodGetErrorIconBounds ()
132                 {
133                         Bitmap b = new Bitmap (1, 1);
134                         Graphics g = Graphics.FromImage (b);
135
136                         BaseCell c = new BaseCell ();
137                         c.ErrorText = "Yo!";
138                         Assert.AreEqual (Rectangle.Empty, c.PublicGetErrorIconBounds (g, c.Style, c.RowIndex), "A1");
139
140                         DataGridView dgv = new DataGridView ();
141                         dgv.Columns.Add ("hi", "there");
142
143                         DataGridViewRow row = new DataGridViewRow ();
144                         row.HeaderCell = c;
145                         dgv.Rows.Add (row);
146
147                         Assert.AreEqual (Rectangle.Empty, (dgv.Rows[0].HeaderCell as BaseCell).PublicGetErrorIconBounds (g, dgv.Rows[0].HeaderCell.InheritedStyle, dgv.Rows[0].HeaderCell.RowIndex), "A2");
148
149                         dgv.Rows[0].ErrorText = "Danger!";
150                         Assert.AreEqual (new Rectangle (24, 5, 12, 11), (dgv.Rows[0].HeaderCell as BaseCell).PublicGetErrorIconBounds (g, dgv.Rows[0].HeaderCell.InheritedStyle, dgv.Rows[0].HeaderCell.RowIndex), "A3");
151                         Assert.AreEqual ("Danger!", (dgv.Rows[0].HeaderCell as BaseCell).PublicGetErrorText (dgv.Rows[0].HeaderCell.RowIndex), "A4");
152
153                         g.Dispose ();
154                         b.Dispose ();
155                 }
156                 
157                 [Test]
158                 public void MethodGetInheritedContextMenuStrip ()
159                 {
160                         BaseCell c = new BaseCell ();
161                         Assert.AreEqual (null, c.GetInheritedContextMenuStrip (c.RowIndex), "A1");
162
163                         DataGridView dgv = new DataGridView ();
164                         dgv.Columns.Add ("hi", "there");
165
166                         DataGridViewRow row = new DataGridViewRow ();
167                         row.HeaderCell = c;
168                         dgv.Rows.Add (row);
169
170                         Assert.AreEqual (null, dgv.Rows[0].HeaderCell.GetInheritedContextMenuStrip (dgv.Rows[0].HeaderCell.RowIndex), "A2");
171
172                         ContextMenuStrip cms1 = new ContextMenuStrip ();
173                         cms1.Items.Add ("Moose");
174                         dgv.ContextMenuStrip = cms1;
175
176                         Assert.AreSame (cms1, dgv.Rows[0].HeaderCell.GetInheritedContextMenuStrip (dgv.Rows[0].HeaderCell.RowIndex), "A3");
177                         
178                         ContextMenuStrip cms2 = new ContextMenuStrip ();
179                         cms2.Items.Add ("Moose");
180
181                         dgv.Rows[0].ContextMenuStrip = cms2;
182                         Assert.AreSame (cms1, dgv.Rows[0].HeaderCell.GetInheritedContextMenuStrip (dgv.Rows[0].HeaderCell.RowIndex), "A4");
183
184                         dgv.Rows[0].HeaderCell.ContextMenuStrip = cms2;
185                         Assert.AreSame (cms2, dgv.Rows[0].HeaderCell.GetInheritedContextMenuStrip (dgv.Rows[0].HeaderCell.RowIndex), "A5");
186                 }
187
188                 /* font dependent
189                 [Test]
190                 public void PreferredSize ()
191                 {
192                         BaseCell c = new BaseCell ();
193                         Assert.AreEqual (new Size (-1, -1), c.PreferredSize, "A1");
194
195                         DataGridView dgv = new DataGridView ();
196                         dgv.Columns.Add ("hi", "there");
197
198                         DataGridViewRow row = new DataGridViewRow ();
199                         row.HeaderCell = c;
200                         dgv.Rows.Add (row);
201
202                         Assert.AreEqual (new Size (39, 17), dgv.Rows[0].HeaderCell.PreferredSize, "A2");
203
204                         dgv.Rows[0].HeaderCell.Value = "bob";
205                         Assert.AreEqual (new Size (73, 17), dgv.Rows[0].HeaderCell.PreferredSize, "A3");
206
207                         dgv.Rows[0].HeaderCell.Value = "roasted quail";
208                         Assert.AreEqual (new Size (115, 17), dgv.Rows[0].HeaderCell.PreferredSize, "A4");
209                 }
210                 */
211                 
212                 private class BaseCell : DataGridViewRowHeaderCell
213                 {
214                         public Rectangle PublicGetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
215                         { return GetContentBounds (graphics, cellStyle, rowIndex); }
216                         public Rectangle PublicGetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
217                         { return GetErrorIconBounds (graphics, cellStyle, rowIndex); }
218                         public string PublicGetErrorText (int rowIndex)
219                         { return GetErrorText (rowIndex); }
220                         public Rectangle PublicBorderWidths (DataGridViewAdvancedBorderStyle advancedBorderStyle)
221                         { return BorderWidths (advancedBorderStyle); }
222                 }
223         }
224 }
225 #endif