2006-12-19 Daniel Nauck <dna@mono-project.de>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / GridColumnStylesCollectionTest.cs
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Jordi Mas i Hernandez <jordi@ximian.com>
24 //
25 //
26
27 using System;
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Drawing;
31 using System.Windows.Forms;
32 using System.Xml;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Windows.Forms
36 {
37         [TestFixture]
38         class GridColumnStylesCollectionTest
39         {
40                 private bool eventhandled;
41                 private object Element;
42                 private CollectionChangeAction Action;
43                 private int times;
44
45
46                 [Test]
47                 public void TestDefaultValues ()
48                 {
49                         DataGridTableStyle ts = new DataGridTableStyle ();
50                         GridColumnStylesCollection sc = ts.GridColumnStyles;
51
52                         Assert.AreEqual (false, sc.IsSynchronized, "IsSynchronized property");
53                         Assert.AreEqual (0, sc.Count, "Count");
54                         Assert.AreEqual (sc, sc.SyncRoot, "SyncRoot property");
55                         Assert.AreEqual (false, ((IList)sc).IsFixedSize, "IsFixedSize property");
56                         Assert.AreEqual (false, sc.IsReadOnly, "IsReadOnly property");
57                 }
58
59                 [Test]
60                 public void TestAdd ()
61                 {
62                         DataGridTableStyle ts = new DataGridTableStyle ();
63                         GridColumnStylesCollection sc = ts.GridColumnStyles;                    
64                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
65
66                         // Add single
67                         ResetEventData ();
68                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
69                         col1.MappingName = "Column1";
70                         sc.Add (col1);
71                         Assert.AreEqual (true, eventhandled);
72                         Assert.AreEqual (col1, Element);
73                         Assert.AreEqual (CollectionChangeAction.Add, Action);
74
75                         // Add multiple
76                         ResetEventData ();
77                         DataGridTextBoxColumn elem1 = new DataGridTextBoxColumn ();
78                         DataGridTextBoxColumn elem2 = new DataGridTextBoxColumn ();
79                         sc.AddRange (new DataGridTextBoxColumn [] {elem1, elem2});
80                         Assert.AreEqual (true, eventhandled, "A1");
81                         Assert.AreEqual (CollectionChangeAction.Add, Action, "A2");
82                         Assert.AreEqual (elem2, Element, "A3");
83                         
84                 }
85
86                 [Test]
87                 public void TestAddRange ()
88                 {
89                         DataGridTableStyle ts = new DataGridTableStyle ();
90                         GridColumnStylesCollection sc = ts.GridColumnStyles;                    
91                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
92
93                         ResetEventData ();
94                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
95                         col1.MappingName = "Column1";
96
97                         DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
98                         col2.MappingName = "Column2";
99                         sc.AddRange (new DataGridColumnStyle[] {col1, col2});
100
101                         Assert.AreEqual (true, eventhandled, "A1");
102                         Assert.AreEqual (col2, Element, "A2");
103                         Assert.AreEqual (CollectionChangeAction.Add, Action, "A3");
104                         Assert.AreEqual (2, times, "A4");
105                 }
106
107                 [Test]
108                 public void TestRemove ()
109                 {
110                         DataGridTableStyle ts = new DataGridTableStyle ();
111                         GridColumnStylesCollection sc = ts.GridColumnStyles;                    
112                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
113
114                         // Add single                   
115                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
116                         col1.MappingName = "Column1";
117                         sc.Add (col1);
118                         
119                         DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
120                         col2.MappingName = "Column2";
121                         sc.Add (col2);
122                         
123                         DataGridTextBoxColumn col3 = new DataGridTextBoxColumn ();
124                         col3.MappingName = "Column3";
125                         sc.Add (col3);
126
127                         ResetEventData ();
128                         sc.Remove (col2);
129                         Assert.AreEqual (true, eventhandled, "A1");
130                         Assert.AreEqual (col2, Element, "A2");
131                         Assert.AreEqual (CollectionChangeAction.Remove, Action, "A3");
132                         Assert.AreEqual (2, sc.Count, "A4");
133
134                         ResetEventData ();
135                         sc.RemoveAt (0);
136                         Assert.AreEqual (true, eventhandled, "A5");
137                         Assert.AreEqual (col1, Element, "A6");
138                         Assert.AreEqual (CollectionChangeAction.Remove, Action, "A6");
139                         Assert.AreEqual (1, sc.Count, "A7");
140
141                         ResetEventData ();
142                         sc.Clear ();
143                         Assert.AreEqual (null, Element, "A8");
144                         Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A9");
145
146                 }
147
148                 [Test]
149                 public void TestIndexContains ()
150                 {
151                         DataGridTableStyle ts = new DataGridTableStyle ();
152                         GridColumnStylesCollection sc = ts.GridColumnStyles;                    
153                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
154
155                         // Add single                   
156                         DataGridTextBoxColumn col1 = new DataGridTextBoxColumn ();
157                         col1.MappingName = "Column1";
158                         sc.Add (col1);
159                         
160                         DataGridTextBoxColumn col2 = new DataGridTextBoxColumn ();
161                         col2.MappingName = "Column2";
162                         sc.Add (col2);
163                         
164                         DataGridTextBoxColumn col3 = new DataGridTextBoxColumn ();
165                         col3.MappingName = "Column3";
166                         sc.Add (col3);
167
168                         ResetEventData ();
169                         IList ilist = (IList) sc;
170                         Assert.AreEqual (1, ilist.IndexOf (col2), "A1");
171                         Assert.AreEqual (false, sc.Contains ("nothing"), "A2");
172                         Assert.AreEqual (true, sc.Contains (col3), "A3");
173                 }
174
175                 private void ResetEventData ()
176                 {
177                         times = 0;
178                         eventhandled = false;
179                         Element = null;
180                         Action = (CollectionChangeAction) 0;
181                 }
182
183                 private void OnEventHandler (object sender, EventArgs e)
184                 {
185                         eventhandled = true;
186                 }
187
188                 private void OnCollectionEventHandler (object sender, CollectionChangeEventArgs e)
189                 {                       
190                         eventhandled = true;
191                         Element = e.Element;
192                         Action = e.Action;
193                         times++;
194                 }
195
196                 
197         }
198
199 }