[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / DataGridViewCellCollectionTest.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 // Authors:
21 //      Gert Driesen <drieseng@users.sourceforge.net>
22 // 
23 // Copyright (c) 2007 Gert Driesen
24
25
26 using System;
27 using System.Data;
28 using System.Windows.Forms;
29
30 using NUnit.Framework;
31
32 namespace MonoTests.System.Windows.Forms
33 {
34         [TestFixture]
35         public class DataGridViewCellCollectionTest : TestHelper
36         {
37                 private DataGridView _dataGridView;
38
39                 [SetUp]
40                 protected override void SetUp () {
41                         DataTable dt = new DataTable ();
42                         dt.Columns.Add ("Date", typeof (DateTime));
43                         dt.Columns.Add ("Registered", typeof (bool));
44                         dt.Columns.Add ("Event", typeof (string));
45
46                         DataRow row = dt.NewRow ();
47                         row ["Date"] = new DateTime (2007, 2, 3);
48                         row ["Event"] = "one";
49                         row ["Registered"] = false;
50                         dt.Rows.Add (row);
51
52                         row = dt.NewRow ();
53                         row ["Date"] = new DateTime (2008, 3, 4);
54                         row ["Event"] = "two";
55                         row ["Registered"] = true;
56                         dt.Rows.Add (row);
57
58                         _dataGridView = new DataGridView ();
59                         _dataGridView.DataSource = dt;
60                         base.SetUp ();
61                 }
62
63                 [Test]
64                 [Category ("NotWorking")]
65                 public void Indexer_ColumnName ()
66                 {
67                         Form form = new Form ();
68                         form.ShowInTaskbar = false;
69                         form.Controls.Add (_dataGridView);
70                         form.Show ();
71
72                         DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
73
74                         DataGridViewCell dateCell = cells ["Date"];
75                         Assert.IsNotNull (dateCell, "#A1");
76                         Assert.IsNotNull (dateCell.OwningColumn, "#A2");
77                         Assert.AreEqual ("Date", dateCell.OwningColumn.Name, "#A3");
78                         Assert.IsNotNull (dateCell.Value, "#A4");
79                         Assert.AreEqual (new DateTime (2007, 2, 3), dateCell.Value, "#A5");
80
81                         DataGridViewCell eventCell = cells ["eVeNT"];
82                         Assert.IsNotNull (eventCell, "#B1");
83                         Assert.IsNotNull (eventCell.OwningColumn, "#B2");
84                         Assert.AreEqual ("Event", eventCell.OwningColumn.Name, "#B3");
85                         Assert.IsNotNull (eventCell.Value, "#B4");
86                         Assert.AreEqual ("one", eventCell.Value, "#B5");
87
88                         DataGridViewCell registeredCell = cells ["Registered"];
89                         Assert.IsNotNull (registeredCell, "#C1");
90                         Assert.IsNotNull (registeredCell.OwningColumn, "#C2");
91                         Assert.AreEqual ("Registered", registeredCell.OwningColumn.Name, "#C3");
92                         Assert.IsNotNull (registeredCell.Value, "#C4");
93                         Assert.AreEqual (false, registeredCell.Value, "#C5");
94
95                         form.Dispose ();
96                 }
97
98                 [Test]
99                 public void Indexer_ColumnName_NotFound ()
100                 {
101                         Form form = new Form ();
102                         form.ShowInTaskbar = false;
103                         form.Controls.Add (_dataGridView);
104                         form.Show ();
105
106                         DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
107
108                         try {
109                                 DataGridViewCell cell = cells ["DoesNotExist"];
110                                 Assert.Fail ("#A1: " + cell);
111                         } catch (ArgumentException ex) {
112                                 // Column named DoesNotExist cannot be found
113                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
114                                 Assert.IsNull (ex.InnerException, "#A3");
115                                 Assert.IsNotNull (ex.Message, "#A4");
116                                 Assert.IsTrue (ex.Message.IndexOf ("DoesNotExist") != -1, "#A5");
117                                 Assert.IsNotNull (ex.ParamName, "#A6");
118                                 Assert.AreEqual ("columnName", ex.ParamName, "#A7");
119                         }
120
121                         try {
122                                 DataGridViewCell cell = cells [string.Empty];
123                                 Assert.Fail ("#B1: " + cell);
124                         } catch (ArgumentException ex) {
125                                 // Column named DoesNotExist cannot be found
126                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
127                                 Assert.IsNull (ex.InnerException, "#B3");
128                                 Assert.IsNotNull (ex.Message, "#B4");
129                                 Assert.IsTrue (ex.Message.IndexOf ("  ") != -1, "#B5");
130                                 Assert.IsNotNull (ex.ParamName, "#B6");
131                                 Assert.AreEqual ("columnName", ex.ParamName, "#B7");
132                         }
133
134                         form.Dispose ();
135                 }
136
137                 [Test]
138                 public void Indexer_ColumnName_Null ()
139                 {
140                         Form form = new Form ();
141                         form.ShowInTaskbar = false;
142                         form.Controls.Add (_dataGridView);
143                         form.Show ();
144
145                         DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
146
147                         try {
148                                 DataGridViewCell cell = cells [(string) null];
149                                 Assert.Fail ("#A1: " + cell);
150                         } catch (ArgumentNullException ex) {
151                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
152                                 Assert.IsNull (ex.InnerException, "#A3");
153                                 Assert.IsNotNull (ex.Message, "#A4");
154                                 Assert.IsNotNull (ex.ParamName, "#A5");
155                                 Assert.AreEqual ("columnName", ex.ParamName, "#A6");
156                         }
157
158                         try {
159                                 cells [(string) null] = cells [0];
160                                 Assert.Fail ("#B1");
161                         } catch (ArgumentNullException ex) {
162                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
163                                 Assert.IsNull (ex.InnerException, "#B3");
164                                 Assert.IsNotNull (ex.Message, "#B4");
165                                 Assert.IsNotNull (ex.ParamName, "#B5");
166                                 Assert.AreEqual ("columnName", ex.ParamName, "#B6");
167                         }
168
169                         form.Dispose ();
170                 }
171
172                 [Test]
173                 [Category ("NotWorking")]
174                 public void Indexer_Index ()
175                 {
176                         Form form = new Form ();
177                         form.ShowInTaskbar = false;
178                         form.Controls.Add (_dataGridView);
179                         form.Show ();
180
181                         DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
182
183                         DataGridViewCell dateCell = cells [0];
184                         Assert.IsNotNull (dateCell, "#A1");
185                         Assert.IsNotNull (dateCell.OwningColumn, "#A2");
186                         Assert.AreEqual ("Date", dateCell.OwningColumn.Name, "#A3");
187                         Assert.IsNotNull (dateCell.Value, "#A4");
188                         Assert.AreEqual (new DateTime (2007, 2, 3), dateCell.Value, "#A5");
189
190                         DataGridViewCell eventCell = cells [2];
191                         Assert.IsNotNull (eventCell, "#B1");
192                         Assert.IsNotNull (eventCell.OwningColumn, "#B2");
193                         Assert.AreEqual ("Event", eventCell.OwningColumn.Name, "#B3");
194                         Assert.IsNotNull (eventCell.Value, "#B4");
195                         Assert.AreEqual ("one", eventCell.Value, "#B5");
196
197                         DataGridViewCell registeredCell = cells [1];
198                         Assert.IsNotNull (registeredCell, "#C1");
199                         Assert.IsNotNull (registeredCell.OwningColumn, "#C2");
200                         Assert.AreEqual ("Registered", registeredCell.OwningColumn.Name, "#C3");
201                         Assert.IsNotNull (registeredCell.Value, "#C4");
202                         Assert.AreEqual (false, registeredCell.Value, "#C5");
203
204                         form.Dispose ();
205                 }
206
207                 [Test]
208                 public void Indexer_Index_Negative ()
209                 {
210                         Form form = new Form ();
211                         form.ShowInTaskbar = false;
212                         form.Controls.Add (_dataGridView);
213                         form.Show ();
214
215                         DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
216
217                         try {
218                                 DataGridViewCell cell = cells [-1];
219                                 Assert.Fail ("#A1:" + cell);
220                         } catch (ArgumentOutOfRangeException ex) {
221                                 // Index was out of range. Must be non-negative
222                                 // and less than the size of the collection
223                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
224                                 Assert.IsNull (ex.InnerException, "#A3");
225                                 Assert.IsNotNull (ex.Message, "#A4");
226                                 Assert.IsNotNull (ex.ParamName, "#A5");
227                                 Assert.AreEqual ("index", ex.ParamName, "#A6");
228                         }
229
230                         try {
231                                 cells [-1] = new MockDataGridViewCell ();
232                                 Assert.Fail ("#B1");
233                         } catch (ArgumentOutOfRangeException ex) {
234                                 // Index was out of range. Must be non-negative
235                                 // and less than the size of the collection
236                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
237                                 Assert.IsNull (ex.InnerException, "#B3");
238                                 Assert.IsNotNull (ex.Message, "#B4");
239                                 Assert.IsNotNull (ex.ParamName, "#B5");
240                                 Assert.AreEqual ("index", ex.ParamName, "#B6");
241                         }
242                 }
243
244                 [Test]
245                 public void Indexer_Index_Overflow ()
246                 {
247                         DataGridViewRow row = new DataGridViewRow ();
248                         DataGridViewCellCollection cells = row.Cells;
249                         try {
250                                 DataGridViewCell cell = cells [0];
251                                 Assert.Fail ("#1:" + cell);
252                         } catch (ArgumentOutOfRangeException ex) {
253                                 // Index was out of range. Must be non-negative
254                                 // and less than the size of the collection
255                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
256                                 Assert.IsNull (ex.InnerException, "#3");
257                                 Assert.IsNotNull (ex.Message, "#4");
258                                 Assert.IsNotNull (ex.ParamName, "#5");
259                                 Assert.AreEqual ("index", ex.ParamName, "#6");
260                         }
261                 }
262
263                 [Test]
264                 public void Indexer_Value_Null ()
265                 {
266                         Form form = new Form ();
267                         form.ShowInTaskbar = false;
268                         form.Controls.Add (_dataGridView);
269                         form.Show ();
270
271                         DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
272
273                         try {
274                                 cells ["Date"] = null;
275                                 Assert.Fail ("#A1");
276                         } catch (ArgumentNullException ex) {
277                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
278                                 Assert.IsNull (ex.InnerException, "#A3");
279                                 Assert.IsNotNull (ex.Message, "#A4");
280                                 Assert.IsNotNull (ex.ParamName, "#A5");
281                                 Assert.AreEqual ("value", ex.ParamName, "#A6");
282                         }
283
284                         try {
285                                 cells [0] = null;
286                                 Assert.Fail ("#B1");
287                         } catch (ArgumentNullException ex) {
288                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
289                                 Assert.IsNull (ex.InnerException, "#B3");
290                                 Assert.IsNotNull (ex.Message, "#B4");
291                                 Assert.IsNotNull (ex.ParamName, "#B5");
292                                 Assert.AreEqual ("value", ex.ParamName, "#B6");
293                         }
294
295                         form.Dispose ();
296                 }
297
298                 class MockDataGridViewCell : DataGridViewCell
299                 {
300                 }
301         }
302 }
303