2007-05-10 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ToolStripItemCollectionTest.cs
1 //
2 // ToolStripItemCollectionTest.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) 2007 Gert Driesen
24 //
25 // Authors:
26 //      Gert Driesen (drieseng@users.sourceforge.net)
27 //
28
29 #if NET_2_0
30 using System;
31 using System.Collections.Generic;
32 using System.Windows.Forms;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Windows.Forms
37 {
38         [TestFixture]
39         public class ToolStripItemCollectionTests
40         {
41                 private List<ToolStripItem> itemsAdded;
42                 private List<ToolStripItem> itemsRemoved;
43
44                 [SetUp]
45                 public void SetUp ()
46                 {
47                         itemsAdded = new List<ToolStripItem> ();
48                         itemsRemoved = new List<ToolStripItem> ();
49                 }
50
51                 [Test]
52                 public void Constructor ()
53                 {
54                         ToolStrip toolStrip = CreateToolStrip ();
55                         ToolStripItemCollection items = null;
56
57                         items = new ToolStripItemCollection (toolStrip, new ToolStripItem [0]);
58                         Assert.AreEqual (0, items.Count, "#A1");
59                         Assert.IsFalse (items.IsReadOnly, "#A2");
60                         Assert.AreEqual (0, itemsAdded.Count, "#A3");
61
62                         MockToolStripButton buttonA = new MockToolStripButton ("A");
63                         MockToolStripButton buttonB = new MockToolStripButton ("B");
64                         items = new ToolStripItemCollection (toolStrip, new ToolStripItem [] {
65                                 buttonA, buttonB });
66                         Assert.AreEqual (2, items.Count, "#B1");
67                         Assert.IsFalse (items.IsReadOnly, "#B2");
68                         Assert.AreEqual (0, itemsAdded.Count, "#B3");
69                         Assert.AreSame (buttonA, items [0], "#B4");
70                         Assert.AreSame (buttonB, items [1], "#B5");
71                         Assert.IsNull (buttonA.Owner, "#B6");
72                         Assert.IsNull (buttonA.ParentToolStrip, "#B7");
73                         Assert.IsNull (buttonB.Owner, "#B8");
74                         Assert.IsNull (buttonB.ParentToolStrip, "#B9");
75
76                         // null item
77                         try {
78                                 new ToolStripItemCollection (toolStrip, new ToolStripItem [] {
79                                         buttonA, null, buttonB });
80                                 Assert.Fail ("#C1");
81                         } catch (ArgumentNullException ex) {
82                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
83                                 Assert.IsNull (ex.InnerException, "#C3");
84                                 Assert.IsNotNull (ex.Message, "#C4");
85                                 Assert.AreEqual ("value", ex.ParamName, "#C5");
86                         }
87                 }
88
89                 [Test]
90                 public void Constructor_Owner_Null ()
91                 {
92                         try {
93                                 new ToolStripItemCollection ((ToolStrip) null, new ToolStripItem [0]);
94                                 Assert.Fail ("#1");
95                         } catch (ArgumentNullException ex) {
96                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
97                                 Assert.IsNull (ex.InnerException, "#3");
98                                 Assert.IsNotNull (ex.Message, "#4");
99                                 Assert.AreEqual ("owner", ex.ParamName, "#5");
100                         }
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentNullException))]
105                 public void ConstructorANE ()
106                 {
107                         new ToolStripItemCollection (new ToolStrip (), (ToolStripItem[])null);
108                 }
109                 
110                 [Test]
111                 public void Constructor_Items_Null ()
112                 {
113                         try {
114                                 new ToolStripItemCollection (new ToolStrip (), (ToolStripItem []) null);
115                                 Assert.Fail ("#1");
116                         } catch (ArgumentNullException ex) {
117                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
118                                 Assert.IsNull (ex.InnerException, "#3");
119                                 Assert.IsNotNull (ex.Message, "#4");
120                                 Assert.AreEqual ("toolStripItems", ex.ParamName, "#5");
121                         }
122                 }
123
124                 [Test]
125                 public void Insert_Owned ()
126                 {
127                         ToolStrip toolStrip = CreateToolStrip ();
128                         ToolStripItemCollection items = toolStrip.Items;
129
130                         MockToolStripButton buttonA = new MockToolStripButton ("A");
131                         items.Insert (0, buttonA);
132                         Assert.AreEqual (1, items.Count, "#A1");
133                         Assert.AreEqual (1, itemsAdded.Count, "#A2");
134                         Assert.AreSame (buttonA, items [0], "#A3");
135                         Assert.AreSame (toolStrip, buttonA.Owner, "#A4");
136                         Assert.IsNull (buttonA.ParentToolStrip, "#A5");
137
138                         MockToolStripButton buttonB = new MockToolStripButton ("B");
139                         items.Insert (0, buttonB);
140                         Assert.AreEqual (2, items.Count, "#B1");
141                         Assert.AreEqual (2, itemsAdded.Count, "#B2");
142                         Assert.AreSame (buttonB, items [0], "#B3");
143                         Assert.AreSame (buttonA, items [1], "#B4");
144                         Assert.AreSame (toolStrip, buttonB.Owner, "#B5");
145                         Assert.IsNull (buttonB.ParentToolStrip, "#B6");
146
147                         MockToolStripButton buttonC = new MockToolStripButton ("C");
148                         items.Insert (1, buttonC);
149                         Assert.AreEqual (3, items.Count, "#C1");
150                         Assert.AreEqual (3, itemsAdded.Count, "#C2");
151                         Assert.AreSame (buttonB, items [0], "#C3");
152                         Assert.AreSame (buttonC, items [1], "#C4");
153                         Assert.AreSame (buttonA, items [2], "#C5");
154                         Assert.AreSame (toolStrip, buttonC.Owner, "#C6");
155                         Assert.IsNull (buttonC.ParentToolStrip, "#C7");
156                 }
157
158                 [Test]
159                 public void Insert_Owned_CreateControl ()
160                 {
161                         ToolStrip toolStrip = CreateToolStrip ();
162                         toolStrip.CreateControl ();
163                         ToolStripItemCollection items = toolStrip.Items;
164
165                         MockToolStripButton buttonA = new MockToolStripButton ("A");
166                         items.Insert (0, buttonA);
167                         Assert.AreEqual (1, items.Count, "#A1");
168                         Assert.AreEqual (1, itemsAdded.Count, "#A2");
169                         Assert.AreSame (buttonA, items[0], "#A3");
170                         Assert.AreSame (toolStrip, buttonA.Owner, "#A4");
171                         Assert.IsNotNull (buttonA.ParentToolStrip, "#A5");
172
173                         MockToolStripButton buttonB = new MockToolStripButton ("B");
174                         items.Insert (0, buttonB);
175                         Assert.AreEqual (2, items.Count, "#B1");
176                         Assert.AreEqual (2, itemsAdded.Count, "#B2");
177                         Assert.AreSame (buttonB, items[0], "#B3");
178                         Assert.AreSame (buttonA, items[1], "#B4");
179                         Assert.AreSame (toolStrip, buttonB.Owner, "#B5");
180                         Assert.IsNotNull (buttonB.ParentToolStrip, "#B6");
181
182                         MockToolStripButton buttonC = new MockToolStripButton ("C");
183                         items.Insert (1, buttonC);
184                         Assert.AreEqual (3, items.Count, "#C1");
185                         Assert.AreEqual (3, itemsAdded.Count, "#C2");
186                         Assert.AreSame (buttonB, items[0], "#C3");
187                         Assert.AreSame (buttonC, items[1], "#C4");
188                         Assert.AreSame (buttonA, items[2], "#C5");
189                         Assert.AreSame (toolStrip, buttonC.Owner, "#C6");
190                         Assert.IsNotNull (buttonC.ParentToolStrip, "#C7");
191                 }
192
193                 [Test]
194                 public void Insert_StandAlone ()
195                 {
196                         ToolStrip toolStrip = CreateToolStrip ();
197                         ToolStripItemCollection items = new ToolStripItemCollection (
198                                 toolStrip, new ToolStripItem [0]);
199
200                         MockToolStripButton buttonA = new MockToolStripButton ("A");
201                         items.Insert (0, buttonA);
202                         Assert.AreEqual (1, items.Count, "#A1");
203                         Assert.AreEqual (0, itemsAdded.Count, "#A2");
204                         Assert.AreSame (buttonA, items [0], "#A3");
205                         Assert.IsNull (buttonA.Owner, "#A4");
206                         Assert.IsNull (buttonA.ParentToolStrip, "#A5");
207
208                         MockToolStripButton buttonB = new MockToolStripButton ("B");
209                         items.Insert (0, buttonB);
210                         Assert.AreEqual (2, items.Count, "#B1");
211                         Assert.AreEqual (0, itemsAdded.Count, "#B2");
212                         Assert.AreSame (buttonB, items [0], "#B3");
213                         Assert.AreSame (buttonA, items [1], "#B4");
214                         Assert.IsNull (buttonB.Owner, "#B5");
215                         Assert.IsNull (buttonB.ParentToolStrip, "#B6");
216
217                         MockToolStripButton buttonC = new MockToolStripButton ("C");
218                         items.Insert (1, buttonC);
219                         Assert.AreEqual (3, items.Count, "#C1");
220                         Assert.AreEqual (0, itemsAdded.Count, "#C2");
221                         Assert.AreSame (buttonB, items [0], "#C3");
222                         Assert.AreSame (buttonC, items [1], "#C4");
223                         Assert.AreSame (buttonA, items [2], "#C5");
224                         Assert.IsNull (buttonC.Owner, "#C6");
225                         Assert.IsNull (buttonC.ParentToolStrip, "#C7");
226                 }
227
228                 [Test]
229                 public void Insert_Index_OutOfRange ()
230                 {
231                         ToolStrip toolStrip = CreateToolStrip ();
232                         ToolStripItemCollection items = new ToolStripItemCollection (
233                                 toolStrip, new ToolStripItem [0]);
234
235                         try {
236                                 items.Insert (-1, new ToolStripButton ());
237                                 Assert.Fail ("#A1");
238                         } catch (ArgumentOutOfRangeException ex) {
239                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
240                                 Assert.IsNull (ex.InnerException, "#A3");
241                                 Assert.IsNotNull (ex.Message, "#A4");
242                                 Assert.AreEqual ("index", ex.ParamName, "#A5");
243                         }
244
245                         try {
246                                 items.Insert (1, new ToolStripButton ());
247                                 Assert.Fail ("#B1");
248                         } catch (ArgumentOutOfRangeException ex) {
249                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
250                                 Assert.IsNull (ex.InnerException, "#B3");
251                                 Assert.IsNotNull (ex.Message, "#B4");
252                                 Assert.AreEqual ("index", ex.ParamName, "#B5");
253                         }
254                 }
255
256                 [Test]
257                 public void Insert_Item_Null ()
258                 {
259                         ToolStrip toolStrip = CreateToolStrip ();
260                         ToolStripItemCollection items = new ToolStripItemCollection (
261                                 toolStrip, new ToolStripItem [0]);
262                         try {
263                                 items.Insert (0, (ToolStripItem) null);
264                                 Assert.Fail ("#1");
265                         } catch (ArgumentNullException ex) {
266                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
267                                 Assert.IsNull (ex.InnerException, "#3");
268                                 Assert.IsNotNull (ex.Message, "#4");
269                                 Assert.AreEqual ("value", ex.ParamName, "#5");
270                         }
271                 }
272
273                 [Test]
274                 public void Remove_Owned ()
275                 {
276                         ToolStrip toolStrip = CreateToolStrip ();
277                         ToolStripItemCollection items = toolStrip.Items;
278
279                         MockToolStripButton buttonA = new MockToolStripButton ("A");
280                         MockToolStripButton buttonB = new MockToolStripButton ("B");
281                         MockToolStripButton buttonC = new MockToolStripButton ("B");
282                         items.Insert (0, buttonA);
283                         items.Insert (0, buttonB);
284
285                         items.Remove (buttonB);
286                         Assert.AreEqual (1, items.Count, "#A1");
287                         Assert.AreEqual (1, itemsRemoved.Count, "#A2");
288                         Assert.AreSame (buttonA, items [0], "#A3");
289                         Assert.AreSame (buttonB, itemsRemoved [0], "#A4");
290                         Assert.IsNull (buttonB.Owner, "#A5");
291                         Assert.IsNull (buttonB.ParentToolStrip, "#A6");
292
293                         // remove null item
294                         items.Remove ((ToolStripItem) null);
295                         Assert.AreEqual (1, items.Count, "#B1");
296                         Assert.AreEqual (2, itemsRemoved.Count, "#B2");
297                         Assert.AreSame (buttonA, items [0], "#B3");
298                         Assert.IsNull (itemsRemoved [1], "#B4");
299
300                         // remove item not owner by toolstrip
301                         items.Remove (buttonC);
302                         Assert.AreEqual (1, items.Count, "#C1");
303                         Assert.AreEqual (3, itemsRemoved.Count, "#C2");
304                         Assert.AreSame (buttonA, items [0], "#C3");
305                         Assert.AreSame(buttonC, itemsRemoved [2], "#C4");
306                         Assert.IsNull (buttonC.Owner, "#C5");
307                         Assert.IsNull (buttonC.ParentToolStrip, "#C6");
308
309                         items.Remove (buttonA);
310                         Assert.AreEqual (0, items.Count, "#D1");
311                         Assert.AreEqual (4, itemsRemoved.Count, "#D2");
312                         Assert.AreSame(buttonA, itemsRemoved [3], "#D3");
313                         Assert.IsNull (buttonC.Owner, "#D4");
314                         Assert.IsNull (buttonC.ParentToolStrip, "#D5");
315
316                         // remove item which is no longer in the collection
317                         items.Remove (buttonA);
318                         Assert.AreEqual (0, items.Count, "#E1");
319                         Assert.AreEqual (5, itemsRemoved.Count, "#E2");
320                         Assert.AreSame(buttonA, itemsRemoved [4], "#E3");
321
322                         // remove item owned by other toolstrip
323                         ToolStrip otherToolStrip = new ToolStrip ();
324                         MockToolStripButton buttonD = new MockToolStripButton ("B");
325                         otherToolStrip.Items.Add (buttonD);
326                         Assert.AreSame (otherToolStrip, buttonD.Owner, "#F1");
327                         Assert.IsNull (buttonD.ParentToolStrip, "#F2");
328                         items.Remove (buttonD);
329                         Assert.AreEqual (0, items.Count, "#F3");
330                         Assert.AreEqual (6, itemsRemoved.Count, "#F4");
331                         Assert.IsNull (buttonD.Owner, "#F5");
332                         Assert.IsNull (buttonD.ParentToolStrip, "#F6");
333                 }
334
335                 [Test]
336                 public void Remove_StandAlone ()
337                 {
338                         ToolStrip toolStrip = CreateToolStrip ();
339                         ToolStripItemCollection items = new ToolStripItemCollection (
340                                 toolStrip, new ToolStripItem [0]);
341
342                         MockToolStripButton buttonA = new MockToolStripButton ("A");
343                         MockToolStripButton buttonB = new MockToolStripButton ("B");
344                         MockToolStripButton buttonC = new MockToolStripButton ("B");
345                         items.Insert (0, buttonA);
346                         items.Insert (0, buttonB);
347
348                         items.Remove (buttonB);
349                         Assert.AreEqual (1, items.Count, "#A1");
350                         Assert.AreEqual (0, itemsRemoved.Count, "#A2");
351                         Assert.AreSame (buttonA, items [0], "#A3");
352
353                         items.Remove ((ToolStripItem) null);
354                         Assert.AreEqual (1, items.Count, "#B1");
355                         Assert.AreEqual (0, itemsRemoved.Count, "#B2");
356                         Assert.AreSame (buttonA, items [0], "#B3");
357
358                         items.Remove (buttonC);
359                         Assert.AreEqual (1, items.Count, "#C1");
360                         Assert.AreEqual (0, itemsRemoved.Count, "#C2");
361                         Assert.AreSame (buttonA, items [0], "#C3");
362
363                         items.Remove (buttonA);
364                         Assert.AreEqual (0, items.Count, "#D1");
365                         Assert.AreEqual (0, itemsRemoved.Count, "#D2");
366
367                         items.Remove (buttonA);
368                         Assert.AreEqual (0, items.Count, "#E1");
369                         Assert.AreEqual (0, itemsRemoved.Count, "#E2");
370
371                         // remove item owned by other toolstrip
372                         ToolStrip otherToolStrip = new ToolStrip ();
373                         MockToolStripButton buttonD = new MockToolStripButton ("B");
374                         otherToolStrip.Items.Add (buttonD);
375                         Assert.AreSame (otherToolStrip, buttonD.Owner, "#F1");
376                         Assert.IsNull (buttonD.ParentToolStrip, "#F2");
377                         items.Remove (buttonD);
378                         Assert.AreEqual (0, items.Count, "#F3");
379                         Assert.AreEqual (0, itemsRemoved.Count, "#F4");
380                         Assert.AreSame (otherToolStrip, buttonD.Owner, "#F5");
381                         Assert.IsNull (buttonD.ParentToolStrip, "#F6");
382                 }
383
384                 void ToolStrip_ItemAdded (object sender, ToolStripItemEventArgs e)
385                 {
386                         itemsAdded.Add (e.Item);
387                 }
388
389                 void ToolStrip_ItemRemoved (object sender, ToolStripItemEventArgs e)
390                 {
391                         itemsRemoved.Add (e.Item);
392                 }
393
394                 ToolStrip CreateToolStrip ()
395                 {
396                         ToolStrip toolStrip = new ToolStrip ();
397                         toolStrip.ItemAdded += ToolStrip_ItemAdded;
398                         toolStrip.ItemRemoved += ToolStrip_ItemRemoved;
399                         return toolStrip;
400                 }
401
402                 class MockToolStripButton : ToolStripButton
403                 {
404                         public MockToolStripButton (string text) : base (text)
405                         {
406                         }
407
408                         public ToolStrip ParentToolStrip {
409                                 get { return base.Parent; }
410                                 set { base.Parent = value; }
411                         }
412                 }
413         }
414 }
415 #endif