Unit test for bug #821.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / AutoCompleteStringCollectionTest.cs
1 //
2 //  AutoCompleteStringCollectionTest.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 Daniel Nauck
24 //
25 // Author:
26 //      Daniel Nauck    (dna(at)mono-project(dot)de)
27
28 #if NET_2_0
29
30 using System;
31 using System.Windows.Forms;
32 using System.Drawing;
33 using System.Reflection;
34 using System.Collections;
35 using System.ComponentModel;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Windows.Forms
39 {
40         [TestFixture]
41         public class AutoCompleteStringCollectionTest : TestHelper
42         {
43                 AutoCompleteStringCollection autoCol = null;
44                 private int add_event = 0;
45                 private int refresh_event = 0;
46                 private int remove_event = 0;
47                 private string value_event = null;
48
49                 [SetUp]
50                 protected override void SetUp ()
51                 {
52                         autoCol = new AutoCompleteStringCollection ();
53                         autoCol.CollectionChanged += new CollectionChangeEventHandler (AutoColChanged);
54                         add_event = 0;
55                         refresh_event = 0;
56                         remove_event = 0;
57                         value_event = "unknown item";
58                         base.SetUp ();
59                 }
60
61                 private void AutoColChanged(object sender, CollectionChangeEventArgs e)
62                 {
63                         switch (e.Action)
64                         {
65                                 case CollectionChangeAction.Add:
66                                         add_event++;
67                                         value_event = (string)e.Element;
68                                         break;
69                                 case CollectionChangeAction.Refresh:
70                                         refresh_event++;
71                                         value_event = (string)e.Element;
72                                         break;
73                                 case CollectionChangeAction.Remove:
74                                         remove_event++;
75                                         value_event = (string)e.Element;
76                                         break;
77                         }
78                 }
79
80                 [Test]
81                 public void DefaultProperties ()
82                 {
83                         Assert.AreEqual (false, autoCol.IsReadOnly, "#A1");
84                         Assert.AreEqual (false, ((IList)autoCol).IsFixedSize, "#A2");
85                         Assert.AreEqual (false, autoCol.IsSynchronized, "#A3");
86                         Assert.AreEqual (autoCol, autoCol.SyncRoot, "#A4");
87                         Assert.AreEqual (0, autoCol.Count, "#A5");
88                 }
89         
90                 [Test]
91                 public void AddTest ()
92                 {
93                         int item1 = autoCol.Add ("Item1");
94                         Assert.AreEqual (1, add_event, "#B1");
95                         Assert.AreEqual ("Item1", value_event, "#B2");
96                         int item2 = autoCol.Add ("Item2");
97                         Assert.AreEqual (2, add_event, "#B3");
98                         Assert.AreEqual ("Item2", value_event, "#B4");
99
100                         Assert.AreEqual (2, autoCol.Count, "#B5");
101                         Assert.AreEqual ("Item1", autoCol[item1], "#B6");
102                         Assert.AreEqual ("Item2", autoCol[item2], "#B7");
103                 }
104         
105                 [Test]
106                 public void ClearTest ()
107                 {
108                         autoCol.Add ("Item1");
109                         autoCol.Add ("Item2");
110                         autoCol.Clear ();
111                         Assert.AreEqual (1, refresh_event, "#C1");
112                         Assert.AreEqual (null, value_event, "#C2");
113                         Assert.AreEqual (0, autoCol.Count, "#C3");
114                 }
115         
116                 [Test]
117                 public void ContainsTest ()
118                 {
119                         autoCol.Add ("Item1");
120                         Assert.AreEqual (true, autoCol.Contains("Item1"), "#D1");
121                         Assert.AreEqual (false, autoCol.Contains("Item2"), "#D2");
122                 }
123         
124                 [Test]
125                 public void IndexOfTest ()
126                 {
127                         autoCol.Add ("Item1");
128                         autoCol.Add ("Item2");
129                         Assert.AreEqual (1, autoCol.IndexOf("Item2"), "#E1");
130                 }
131         
132                 [Test]
133                 public void RemoveTest ()
134                 {
135                         autoCol.Add ("Item1");
136                         autoCol.Add ("Item2");
137                         autoCol.Remove ("Item1");
138                         Assert.AreEqual(1, remove_event, "#F1");
139                         Assert.AreEqual("Item1", value_event, "#F2");
140                         Assert.AreEqual (1, autoCol.Count, "#F3");
141                         Assert.AreEqual ("Item2", autoCol[0], "#F4");
142                 }
143         
144                 [Test]
145                 public void RemoveAtTest ()
146                 {
147                         autoCol.Add ("Item1");
148                         autoCol.Add ("Item2");
149                         autoCol.RemoveAt (0);
150                         Assert.AreEqual(1, remove_event, "#G1");
151                         Assert.AreEqual("Item1", value_event, "#G2");
152                         Assert.AreEqual (1, autoCol.Count, "#G3");
153                         Assert.AreEqual (true, autoCol.Contains("Item2"), "#G4");
154                 }
155         
156                 [Test]
157                 public void AddRangeTest ()
158                 {
159                         string[] values = new string[] { "Item1", "Item2", "Item3", "Item4" };
160                         autoCol.Add ("Item5");
161                         autoCol.AddRange (values);
162                         Assert.AreEqual(1, refresh_event, "#E1");
163                         Assert.AreEqual(null, value_event, "#E2");
164                         Assert.AreEqual (5, autoCol.Count, "#E3");
165                         Assert.AreEqual (true, autoCol.Contains("Item1"), "#E4");
166                         Assert.AreEqual (true, autoCol.Contains("Item2"), "#E5");
167                         Assert.AreEqual (true, autoCol.Contains("Item3"), "#E6");
168                         Assert.AreEqual (true, autoCol.Contains("Item4"), "#E7");
169                         Assert.AreEqual (true, autoCol.Contains("Item5"), "#E8");
170                 }
171
172                 [Test]
173                 [ExpectedException (typeof (ArgumentNullException))]
174                 public void AddRangeNullTest ()
175                 {
176                         string[] values = null;
177                         autoCol.AddRange (values);
178                 }
179
180                 [Test]
181                 public void AddTest_Junk ()
182                 {
183                         autoCol.Add (null);
184                         Assert.AreEqual (1, autoCol.Count, "#F1");
185                         autoCol.Add (null);
186                         Assert.AreEqual (2, autoCol.Count, "#F2");
187                         Assert.AreEqual (true, autoCol.Contains(null), "#F3");
188                         Assert.AreEqual (0, autoCol.IndexOf(null), "#F4");
189                         autoCol.Remove (null);
190                         Assert.AreEqual (1, autoCol.Count, "#F5");
191                         autoCol[0] = "Item1";
192                         autoCol[0] = null;
193                         Assert.AreEqual (null, autoCol[0], "#F6");
194                 }
195
196                 [Test]
197                 public void IndexerTest()
198                 {
199                         autoCol.Add("Item1");
200                         autoCol[0] = "NewItem1";
201
202                         Assert.AreEqual(1, remove_event, "#G1");
203                         Assert.AreEqual(2, add_event, "#G2");
204                 }
205         }
206 }
207 #endif