2009-07-17 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ListViewGroupCollectionTest.cs
1 //
2 //  ListViewGroupCollectionTest.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 //      Carlos Alberto Cortez <calberto.cortez@gmail.com>
28
29 #if NET_2_0
30
31 using System;
32 using System.Windows.Forms;
33 using System.Drawing;
34 using System.Reflection;
35 using System.Collections;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Windows.Forms
39 {
40         [TestFixture]
41         public class ListViewGroupCollectionTest : TestHelper
42         {
43                 ListViewGroupCollection grpCol = null;
44                 ListView lv = null;
45
46                 [SetUp]
47                 protected override void SetUp () {
48                         lv = new ListView ();
49                         grpCol = lv.Groups;
50                         base.SetUp ();
51                 }
52
53                 [Test]
54                 public void DefaultProperties ()
55                 {
56                         Assert.AreEqual (false, ((IList)grpCol).IsReadOnly, "#A1");
57                         Assert.AreEqual (false, ((IList)grpCol).IsFixedSize, "#A2");
58                         Assert.AreEqual (true, ((ICollection)grpCol).IsSynchronized, "#A3");
59                         Assert.AreEqual (grpCol, ((ICollection)grpCol).SyncRoot, "#A4");
60                         Assert.AreEqual (0, grpCol.Count, "#A5");
61                 }
62
63                 [Test]
64                 public void AddTest ()
65                 {
66                         ListViewGroup group1 = new ListViewGroup ("Item1");
67                         ListViewGroup group2 = new ListViewGroup ("Item2");
68                         grpCol.Add (group1);
69                         grpCol.Add (group2);
70
71                         Assert.AreEqual (2, grpCol.Count, "#B1");
72                         Assert.AreEqual (lv, group1.ListView, "#B2");
73                         Assert.AreEqual (lv, group2.ListView, "#B2");
74                 }
75
76                 [Test]
77                 public void ClearTest ()
78                 {
79                         ListViewGroup group1 = new ListViewGroup ("Item1");
80                         ListViewGroup group2 = new ListViewGroup ("Item2");
81                         grpCol.Add (group1);
82                         grpCol.Add (group2);
83                         grpCol.Clear ();
84
85                         Assert.AreEqual (0, grpCol.Count, "#C1");
86                         Assert.AreEqual (null, group1.ListView, "#C2");
87                         Assert.AreEqual (null, group2.ListView, "#C3");
88                 }
89
90                 [Test]
91                 public void ContainsTest ()
92                 {
93                         ListViewGroup obj = new ListViewGroup ("Item1");
94                         ListViewGroup obj2 = new ListViewGroup ("Item2");
95                         grpCol.Add (obj);
96                         Assert.AreEqual (true, grpCol.Contains (obj), "#D1");
97                         Assert.AreEqual (false, grpCol.Contains (obj2), "#D2");
98                 }
99
100                 [Test]
101                 public void IndexOfTest ()
102                 {
103                         ListViewGroup obj = new ListViewGroup ("Item1");
104                         ListViewGroup obj2 = new ListViewGroup ("Item2");
105                         grpCol.Add (obj);
106                         grpCol.Add (obj2);
107                         Assert.AreEqual (1, grpCol.IndexOf (obj2), "#E1");
108                 }
109
110                 [Test]
111                 public void RemoveTest ()
112                 {
113                         ListViewGroup obj = new ListViewGroup ("Item1");
114                         ListViewGroup obj2 = new ListViewGroup ("Item2");
115                         grpCol.Add (obj);
116                         grpCol.Add (obj2);
117                         grpCol.Remove (obj);
118                         Assert.AreEqual (1, grpCol.Count, "#F1");
119                         Assert.AreEqual (null, obj.ListView, "#F2");
120                         Assert.AreEqual (lv, obj2.ListView, "#F3");
121                 }
122
123                 [Test]
124                 public void RemoveAtTest ()
125                 {
126                         ListViewGroup obj = new ListViewGroup ("Item1");
127                         ListViewGroup obj2 = new ListViewGroup ("Item2");
128                         grpCol.Add (obj);
129                         grpCol.Add (obj2);
130                         grpCol.RemoveAt (0);
131                         Assert.AreEqual (1, grpCol.Count, "#G1");
132                         Assert.AreEqual (true, grpCol.Contains (obj2), "#G2");
133                         Assert.AreEqual (null, obj.ListView, "#G3");
134                         Assert.AreEqual (lv, obj2.ListView, "#G4");
135                 }
136
137                 [Test]
138                 public void IndexerTest ()
139                 {
140                         ListViewGroup group1 = new ListViewGroup ("Item1");
141
142                         grpCol.Add (group1);
143                         Assert.AreEqual (group1, grpCol [0], "#A1");
144                         Assert.AreEqual (lv, group1.ListView, "#A2");
145                         Assert.AreEqual (1, grpCol.Count, "#A3");
146
147                         grpCol [0] = null;
148                         Assert.AreEqual (null, grpCol [0], "#A4");
149                         Assert.AreEqual (1, grpCol.Count, "#A5");
150
151                         ListViewGroup group2 = new ListViewGroup ("Item2");
152                         grpCol [0] = group2;
153                         Assert.AreEqual (group2, grpCol [0], "#A6");
154                         Assert.AreEqual (null, group2.ListView, "#A7");
155                         Assert.AreEqual (1, grpCol.Count, "#A8");
156                 }
157
158                 [Test]
159                 public void IndexerNullTest ()
160                 {
161                         ListViewGroup group1 = new ListViewGroup ("Item1");
162                         grpCol.Add (group1);
163                         grpCol [0] = null;
164
165                         Assert.AreEqual (null, grpCol [0], "#A1");
166                         Assert.AreEqual (1, grpCol.Count, "#A2");
167                 }
168
169                 /* There's an inconsistency between other collections using
170                  * Key methods and the impl of this collection */
171                 [Test]
172                 public void IndexerKeyTest ()
173                 {
174                         ListViewGroup group1 = new ListViewGroup ("Item1");
175                         ListViewGroup group2 = new ListViewGroup ("Item2");
176                         ListViewGroup group3 = new ListViewGroup ("Item3");
177                         grpCol.Add (group1);
178                         grpCol.Add (group2);
179                         grpCol.Add (group3);
180
181                         group1.Name = String.Empty;
182                         group2.Name = "A";
183                         group3.Name = "A";
184
185                         Assert.AreEqual (group1, grpCol [String.Empty], "#A1"); /* Inconsistent */
186                         Assert.AreEqual (null, grpCol [null], "#A2");
187                         Assert.AreEqual (group2, grpCol ["A"], "#A3");
188                         Assert.AreEqual (null, grpCol ["a"], "#A4"); /* Inconsistent, again */
189
190                         ListViewGroup group4 = new ListViewGroup ("Item4");
191                         group4.Name = "A";
192
193                         grpCol [String.Empty] = group4;
194                         Assert.AreEqual (group4, grpCol [0], "#A5"); /* First position */
195                         Assert.AreEqual (group4, grpCol ["A"], "#A6");
196                         Assert.AreEqual (null, group4.ListView, "#A7");
197
198                         grpCol ["A"] = null;
199                         Assert.AreEqual (null, grpCol [0], "#A8");
200                 }
201
202                 [Test]
203                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
204                 public void IndexerOutOfRangeTest ()
205                 {
206                         grpCol.Add (new ListViewGroup ("Item1"));
207                         grpCol[10] = null;
208                 }
209
210                 [Test]
211                 public void IndexerOutOfRangeTest2()
212                 {   //.NET 2.0 don't throw a exception here
213                         grpCol.Add (new ListViewGroup ("Item1"));
214                         grpCol["TestItemThatDoesNotExist"] = null;
215                         Assert.IsNotNull (grpCol [0], "#A1");
216                 }
217         }
218 }
219 #endif