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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
21 // Gert Driesen <drieseng@users.sourceforge.net>
23 // Copyright (c) 2007 Gert Driesen
28 using System.Windows.Forms;
30 using NUnit.Framework;
32 namespace MonoTests.System.Windows.Forms
35 public class DataGridViewCellCollectionTest : TestHelper
37 private DataGridView _dataGridView;
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));
46 DataRow row = dt.NewRow ();
47 row ["Date"] = new DateTime (2007, 2, 3);
48 row ["Event"] = "one";
49 row ["Registered"] = false;
53 row ["Date"] = new DateTime (2008, 3, 4);
54 row ["Event"] = "two";
55 row ["Registered"] = true;
58 _dataGridView = new DataGridView ();
59 _dataGridView.DataSource = dt;
64 [Category ("NotWorking")]
65 public void Indexer_ColumnName ()
67 Form form = new Form ();
68 form.ShowInTaskbar = false;
69 form.Controls.Add (_dataGridView);
72 DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
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");
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");
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");
99 public void Indexer_ColumnName_NotFound ()
101 Form form = new Form ();
102 form.ShowInTaskbar = false;
103 form.Controls.Add (_dataGridView);
106 DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
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");
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");
138 public void Indexer_ColumnName_Null ()
140 Form form = new Form ();
141 form.ShowInTaskbar = false;
142 form.Controls.Add (_dataGridView);
145 DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
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");
159 cells [(string) null] = cells [0];
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");
173 [Category ("NotWorking")]
174 public void Indexer_Index ()
176 Form form = new Form ();
177 form.ShowInTaskbar = false;
178 form.Controls.Add (_dataGridView);
181 DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
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");
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");
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");
208 public void Indexer_Index_Negative ()
210 Form form = new Form ();
211 form.ShowInTaskbar = false;
212 form.Controls.Add (_dataGridView);
215 DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
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");
231 cells [-1] = new MockDataGridViewCell ();
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");
245 public void Indexer_Index_Overflow ()
247 DataGridViewRow row = new DataGridViewRow ();
248 DataGridViewCellCollection cells = row.Cells;
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");
264 public void Indexer_Value_Null ()
266 Form form = new Form ();
267 form.ShowInTaskbar = false;
268 form.Controls.Add (_dataGridView);
271 DataGridViewCellCollection cells = _dataGridView.Rows [0].Cells;
274 cells ["Date"] = null;
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");
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");
298 class MockDataGridViewCell : DataGridViewCell