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.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
23 // Rolf Bjarne Kvinge (RKvinge@novell.com)
29 using NUnit.Framework;
32 using System.Windows.Forms;
33 using System.ComponentModel;
34 using System.Collections;
36 namespace MonoTests.System.Windows.Forms
39 public class DataGridViewRowHeaderTest : TestHelper
43 [ExpectedException (typeof (ArgumentOutOfRangeException))]
44 public void GetClipboardContentTestException ()
46 DataGridViewRowHeaderClipboardCell cell = new DataGridViewRowHeaderClipboardCell ();
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;
55 dgv.Rows [0].SetValues ("abc");
56 dgv.Rows [0].Cells [0].Selected = true;
58 cell = dgv.Rows [0].HeaderCell as DataGridViewRowHeaderClipboardCell;
59 cell.GetClipboardContentPublic (-1, false, false, false, false, "Text");
65 [ExpectedException (typeof (ArgumentOutOfRangeException))]
66 public void GetClipboardContentTestException2 ()
68 DataGridViewRowHeaderClipboardCell cell = new DataGridViewRowHeaderClipboardCell ();
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;
77 dgv.Rows [0].SetValues ("abc");
78 dgv.Rows [0].Cells [0].Selected = true;
80 cell = dgv.Rows [0].HeaderCell as DataGridViewRowHeaderClipboardCell;
81 cell.GetClipboardContentPublic (2, false, false, false, false, "Text");
87 public class DataGridViewRowHeaderClipboardCell : DataGridViewRowHeaderCell
89 public DataGridViewRowHeaderClipboardCell ()
93 public object GetClipboardContentPublic (int rowIndex, bool firstCell, bool lastCell, bool inFirstRow, bool inLastRow, string format)
95 return GetClipboardContent (rowIndex, firstCell, lastCell, inFirstRow, inLastRow, format);
98 public override Type FormattedValueType
102 return typeof (string);
109 public void MethodGetContentBounds ()
111 BaseCell c = new BaseCell ();
112 Assert.AreEqual (Rectangle.Empty, c.GetContentBounds (c.RowIndex), "A1");
113 c.Value = "hello there";
115 DataGridView dgv = new DataGridView ();
116 dgv.Columns.Add ("hi", "there");
118 DataGridViewRow row = new DataGridViewRow ();
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");
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");
131 public void MethodGetErrorIconBounds ()
133 Bitmap b = new Bitmap (1, 1);
134 Graphics g = Graphics.FromImage (b);
136 BaseCell c = new BaseCell ();
138 Assert.AreEqual (Rectangle.Empty, c.PublicGetErrorIconBounds (g, c.Style, c.RowIndex), "A1");
140 DataGridView dgv = new DataGridView ();
141 dgv.Columns.Add ("hi", "there");
143 DataGridViewRow row = new DataGridViewRow ();
147 Assert.AreEqual (Rectangle.Empty, (dgv.Rows[0].HeaderCell as BaseCell).PublicGetErrorIconBounds (g, dgv.Rows[0].HeaderCell.InheritedStyle, dgv.Rows[0].HeaderCell.RowIndex), "A2");
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");
158 public void MethodGetInheritedContextMenuStrip ()
160 BaseCell c = new BaseCell ();
161 Assert.AreEqual (null, c.GetInheritedContextMenuStrip (c.RowIndex), "A1");
163 DataGridView dgv = new DataGridView ();
164 dgv.Columns.Add ("hi", "there");
166 DataGridViewRow row = new DataGridViewRow ();
170 Assert.AreEqual (null, dgv.Rows[0].HeaderCell.GetInheritedContextMenuStrip (dgv.Rows[0].HeaderCell.RowIndex), "A2");
172 ContextMenuStrip cms1 = new ContextMenuStrip ();
173 cms1.Items.Add ("Moose");
174 dgv.ContextMenuStrip = cms1;
176 Assert.AreSame (cms1, dgv.Rows[0].HeaderCell.GetInheritedContextMenuStrip (dgv.Rows[0].HeaderCell.RowIndex), "A3");
178 ContextMenuStrip cms2 = new ContextMenuStrip ();
179 cms2.Items.Add ("Moose");
181 dgv.Rows[0].ContextMenuStrip = cms2;
182 Assert.AreSame (cms1, dgv.Rows[0].HeaderCell.GetInheritedContextMenuStrip (dgv.Rows[0].HeaderCell.RowIndex), "A4");
184 dgv.Rows[0].HeaderCell.ContextMenuStrip = cms2;
185 Assert.AreSame (cms2, dgv.Rows[0].HeaderCell.GetInheritedContextMenuStrip (dgv.Rows[0].HeaderCell.RowIndex), "A5");
190 public void PreferredSize ()
192 BaseCell c = new BaseCell ();
193 Assert.AreEqual (new Size (-1, -1), c.PreferredSize, "A1");
195 DataGridView dgv = new DataGridView ();
196 dgv.Columns.Add ("hi", "there");
198 DataGridViewRow row = new DataGridViewRow ();
202 Assert.AreEqual (new Size (39, 17), dgv.Rows[0].HeaderCell.PreferredSize, "A2");
204 dgv.Rows[0].HeaderCell.Value = "bob";
205 Assert.AreEqual (new Size (73, 17), dgv.Rows[0].HeaderCell.PreferredSize, "A3");
207 dgv.Rows[0].HeaderCell.Value = "roasted quail";
208 Assert.AreEqual (new Size (115, 17), dgv.Rows[0].HeaderCell.PreferredSize, "A4");
212 private class BaseCell : DataGridViewRowHeaderCell
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); }