[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ToolStripComboBoxTest.cs
1 //
2 // ToolStripComboBoxTests.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Text;
31 using NUnit.Framework;
32 using System.Drawing;
33 using System.Windows.Forms;
34
35 namespace MonoTests.System.Windows.Forms
36 {
37         [TestFixture]
38         public class ToolStripComboBoxTests : TestHelper
39         {
40                 [Test]
41                 public void Constructor ()
42                 {
43                         ToolStripComboBox tsi = new ToolStripComboBox ();
44
45                         //Assert.AreEqual ("System.Windows.Forms.AutoCompleteStringCollection", tsi.AutoCompleteCustomSource.GetType ().ToString (), "A1");
46                         //Assert.AreEqual (AutoCompleteMode.None, tsi.AutoCompleteMode, "A2");
47                         //Assert.AreEqual (AutoCompleteSource.None, tsi.AutoCompleteSource, "A3");
48                         Assert.AreEqual ("System.Windows.Forms.ToolStripComboBox+ToolStripComboBoxControl", tsi.ComboBox.GetType ().ToString (), "A4");
49                         //Assert.AreEqual (106, tsi.DropDownHeight, "A5");
50                         Assert.AreEqual (ComboBoxStyle.DropDown, tsi.DropDownStyle, "A6");
51                         Assert.AreEqual (121, tsi.DropDownWidth, "A7");
52                         Assert.AreEqual (false, tsi.DroppedDown, "A8");
53                         Assert.AreEqual (FlatStyle.Popup, tsi.FlatStyle, "A9");
54                         Assert.AreEqual (true, tsi.IntegralHeight, "A10");
55                         Assert.AreEqual ("System.Windows.Forms.ComboBox+ObjectCollection", tsi.Items.ToString (), "A11");
56                         Assert.AreEqual (8, tsi.MaxDropDownItems, "A12");
57                         Assert.AreEqual (0, tsi.MaxLength, "A13");
58                         Assert.AreEqual (-1, tsi.SelectedIndex, "A14");
59                         Assert.AreEqual (null, tsi.SelectedItem, "A15");
60                         Assert.AreEqual (string.Empty, tsi.SelectedText, "A16");
61                         Assert.AreEqual (0, tsi.SelectionLength, "A17");
62                         Assert.AreEqual (0, tsi.SelectionStart, "A18");
63                         Assert.AreEqual (false, tsi.Sorted, "A19");
64
65                         tsi = new ToolStripComboBox ("Bob");
66                         Assert.AreEqual ("Bob", tsi.Name, "A20");
67                         Assert.AreEqual (string.Empty, tsi.Control.Name, "A21");
68                 }
69         
70                 [Test]
71                 [ExpectedException (typeof (NotSupportedException))]
72                 public void ConstructorNSE ()
73                 {
74                         new ToolStripComboBox (new ComboBox ());
75                 }
76                 
77                 [Test]
78                 public void ProtectedProperties ()
79                 {
80                         ExposeProtectedProperties epp = new ExposeProtectedProperties ();
81
82                         Assert.AreEqual (new Padding (1, 0, 1, 0), epp.DefaultMargin, "C1");
83                         Assert.AreEqual (new Size (100, 22), epp.DefaultSize, "C2");
84                 }
85
86                 //[Test]
87                 //public void PropertyAutoCompleteCustomSource ()
88                 //{
89                 //        ToolStripComboBox tsi = new ToolStripComboBox ();
90                 //        EventWatcher ew = new EventWatcher (tsi);
91
92                 //        AutoCompleteStringCollection acsc = new AutoCompleteStringCollection ();
93                 //        acsc.AddRange (new string[] { "Apple", "Banana" });
94
95                 //        tsi.AutoCompleteCustomSource = acsc;
96                 //        Assert.AreSame (acsc, tsi.AutoCompleteCustomSource, "B1");
97                 //        Assert.AreEqual (string.Empty, ew.ToString (), "B2");
98
99                 //        ew.Clear ();
100                 //        tsi.AutoCompleteCustomSource = acsc;
101                 //        Assert.AreEqual (string.Empty, ew.ToString (), "B3");
102                 //}
103
104                 [Test]
105                 public void PropertyDropDownHeight ()
106                 {
107                         ToolStripComboBox tsi = new ToolStripComboBox ();
108                         EventWatcher ew = new EventWatcher (tsi);
109
110                         tsi.DropDownHeight = 42;
111                         Assert.AreEqual (42, tsi.DropDownHeight, "B1");
112                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
113
114                         ew.Clear ();
115                         tsi.DropDownHeight = 42;
116                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
117                 }
118
119                 [Test]
120                 public void PropertyDropDownStyle ()
121                 {
122                         ToolStripComboBox tsi = new ToolStripComboBox ();
123                         EventWatcher ew = new EventWatcher (tsi);
124
125                         tsi.DropDownStyle = ComboBoxStyle.Simple;
126                         Assert.AreEqual (ComboBoxStyle.Simple, tsi.DropDownStyle, "B1");
127                         Assert.AreEqual ("DropDownStyleChanged", ew.ToString (), "B2");
128
129                         ew.Clear ();
130                         tsi.DropDownStyle = ComboBoxStyle.Simple;
131                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
132                 }
133
134                 [Test]
135                 public void PropertyDropDownWidth ()
136                 {
137                         ToolStripComboBox tsi = new ToolStripComboBox ();
138                         EventWatcher ew = new EventWatcher (tsi);
139
140                         tsi.DropDownWidth = 42;
141                         Assert.AreEqual (42, tsi.DropDownWidth, "B1");
142                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
143
144                         ew.Clear ();
145                         tsi.DropDownWidth = 42;
146                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
147                 }
148
149                 [Test]
150                 public void PropertyDroppedDown ()
151                 {
152                         ToolStripComboBox tsi = new ToolStripComboBox ();
153                         EventWatcher ew = new EventWatcher (tsi);
154
155                         tsi.DroppedDown = true;
156                         Assert.AreEqual (true, tsi.DroppedDown, "B1");
157                         Assert.AreEqual ("DropDown", ew.ToString (), "B2");
158
159                         ew.Clear ();
160                         tsi.DroppedDown = true;
161                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
162                 }
163
164                 [Test]
165                 public void PropertyFlatStyle ()
166                 {
167                         ToolStripComboBox tsi = new ToolStripComboBox ();
168                         EventWatcher ew = new EventWatcher (tsi);
169
170                         tsi.FlatStyle = FlatStyle.System;
171                         Assert.AreEqual (FlatStyle.System, tsi.FlatStyle, "B1");
172                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
173
174                         ew.Clear ();
175                         tsi.FlatStyle = FlatStyle.System;
176                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
177                 }
178
179                 [Test]
180                 public void PropertyIntegralHeight ()
181                 {
182                         ToolStripComboBox tsi = new ToolStripComboBox ();
183                         EventWatcher ew = new EventWatcher (tsi);
184
185                         tsi.IntegralHeight = false;
186                         Assert.AreEqual (false, tsi.IntegralHeight, "B1");
187                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
188
189                         ew.Clear ();
190                         tsi.IntegralHeight = false;
191                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
192                 }
193
194                 [Test]
195                 public void PropertyMaxDropDownItems ()
196                 {
197                         ToolStripComboBox tsi = new ToolStripComboBox ();
198                         EventWatcher ew = new EventWatcher (tsi);
199
200                         tsi.MaxDropDownItems = 12;
201                         Assert.AreEqual (12, tsi.MaxDropDownItems, "B1");
202                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
203
204                         ew.Clear ();
205                         tsi.MaxDropDownItems = 12;
206                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
207                 }
208
209                 [Test]
210                 public void PropertyMaxLength ()
211                 {
212                         ToolStripComboBox tsi = new ToolStripComboBox ();
213                         EventWatcher ew = new EventWatcher (tsi);
214
215                         tsi.MaxLength = 42;
216                         Assert.AreEqual (42, tsi.MaxLength, "B1");
217                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
218
219                         ew.Clear ();
220                         tsi.MaxLength = 42;
221                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
222                 }
223
224                 [Test]
225                 public void PropertySelectedIndex ()
226                 {
227                         ToolStripComboBox tsi = new ToolStripComboBox ();
228                         EventWatcher ew = new EventWatcher (tsi);
229
230                         tsi.Items.Add ("A");
231                         tsi.Items.Add ("B");
232
233                         tsi.SelectedIndex = 1;
234                         Assert.AreEqual (1, tsi.SelectedIndex, "B1");
235                         Assert.AreEqual ("SelectedIndexChanged", ew.ToString (), "B2");
236
237                         ew.Clear ();
238                         tsi.SelectedIndex = 1;
239                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
240                 }
241
242                 [Test]
243                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
244                 public void PropertySelectedIndexAOORE ()
245                 {
246                         ToolStripComboBox tsi = new ToolStripComboBox ();
247
248                         tsi.SelectedIndex = 42;
249                 }
250
251                 [Test]
252                 public void PropertySelectedItem ()
253                 {
254                         ToolStripComboBox tsi = new ToolStripComboBox ();
255                         EventWatcher ew = new EventWatcher (tsi);
256
257                         tsi.Items.Add ("A");
258                         tsi.Items.Add ("B");
259                         
260                         tsi.SelectedItem = "B";
261                         Assert.AreEqual ("B", tsi.SelectedItem, "B1");
262                         Assert.AreEqual ("SelectedIndexChanged", ew.ToString (), "B2");
263
264                         ew.Clear ();
265                         tsi.SelectedItem = "B";
266                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
267                 }
268
269                 [Test]
270                 public void PropertySelectedItem2 ()
271                 {
272                         ToolStripComboBox tsi = new ToolStripComboBox ();
273                         EventWatcher ew = new EventWatcher (tsi);
274
275                         tsi.SelectedItem = "B";
276                         Assert.AreEqual (null, tsi.SelectedItem, "B1");
277                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
278
279                         ew.Clear ();
280                         tsi.SelectedItem = "B";
281                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
282                 }
283
284                 [Test]
285                 [Ignore ("Need TextUpdate event implemented in 2.0 ComboBox")]
286                 public void PropertySelectedText ()
287                 {
288                         ToolStripComboBox tsi = new ToolStripComboBox ();
289                         EventWatcher ew = new EventWatcher (tsi);
290
291                         tsi.SelectedText = "Hi";
292                         Assert.AreEqual (string.Empty, tsi.SelectedText, "B1");
293                         Assert.AreEqual ("TextUpdate", ew.ToString (), "B2");
294
295                         ew.Clear ();
296                         tsi.SelectedText = string.Empty;
297                         Assert.AreEqual ("TextUpdate", ew.ToString (), "B3");
298                 }
299
300                 [Test]
301                 public void PropertySelectionLength ()
302                 {
303                         ToolStripComboBox tsi = new ToolStripComboBox ();
304                         EventWatcher ew = new EventWatcher (tsi);
305
306                         tsi.SelectionLength = 42;
307                         Assert.AreEqual (0, tsi.SelectionLength, "B1");
308                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
309
310                         ew.Clear ();
311                         tsi.SelectionLength = 42;
312                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
313                 }
314
315                 [Test]
316                 public void PropertySelectionStart ()
317                 {
318                         ToolStripComboBox tsi = new ToolStripComboBox ();
319                         EventWatcher ew = new EventWatcher (tsi);
320
321                         tsi.SelectionStart = 42;
322                         Assert.AreEqual (0, tsi.SelectionStart, "B1");
323                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
324
325                         ew.Clear ();
326                         tsi.SelectionStart = 42;
327                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
328                 }
329
330                 [Test]
331                 public void PropertySorted ()
332                 {
333                         ToolStripComboBox tsi = new ToolStripComboBox ();
334                         EventWatcher ew = new EventWatcher (tsi);
335
336                         tsi.Sorted = true;
337                         Assert.AreEqual (true, tsi.Sorted, "B1");
338                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
339
340                         ew.Clear ();
341                         tsi.Sorted = true;
342                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
343                 }
344
345                 private class EventWatcher
346                 {
347                         private string events = string.Empty;
348                         
349                         public EventWatcher (ToolStripComboBox tsi)
350                         {
351                                 tsi.DropDown += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDown;"); });
352                                 tsi.DropDownClosed += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDownClosed;"); });
353                                 tsi.DropDownStyleChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDownStyleChanged;"); });
354                                 tsi.SelectedIndexChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("SelectedIndexChanged;"); });
355                                 tsi.TextUpdate += new EventHandler (delegate (Object obj, EventArgs e) { events += ("TextUpdate;"); });
356                         }
357
358                         public override string ToString ()
359                         {
360                                 return events.TrimEnd (';');
361                         }
362                         
363                         public void Clear ()
364                         {
365                                 events = string.Empty;
366                         }
367                 }
368                 
369                 private class ExposeProtectedProperties : ToolStripComboBox
370                 {
371                         public ExposeProtectedProperties () : base () {}
372
373                         public new Padding DefaultMargin { get { return base.DefaultMargin; } }
374                         public new Size DefaultSize { get { return base.DefaultSize; } }
375                 }
376         }
377 }