2005-07-05 Jordi Mas i Hernandez <jordi@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / GridTableStylesCollectionTest.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.Data;
29 using System.Collections;
30 using System.ComponentModel;
31 using System.Drawing;
32 using System.Windows.Forms;
33 using System.Xml;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Windows.Forms
37 {
38         [TestFixture]
39         class GridTableStylesCollectionTests : Assertion
40         {
41                 private bool eventhandled;
42                 private object Element;
43                 private CollectionChangeAction Action;
44
45                 [TearDown]
46                 public void Clean() {}
47
48                 [SetUp]
49                 public void GetReady ()
50                 {
51                 }
52
53                 [Test]
54                 public void TestDefaultValues ()
55                 {
56                         DataGrid grid = new DataGrid ();
57                         GridTableStylesCollection sc = grid.TableStyles;
58
59                         AssertEquals ("IsSynchronized property", false, sc.IsSynchronized);
60                         AssertEquals ("SyncRoot property", false, sc.IsSynchronized);
61                         AssertEquals ("IsReadOnly  property", false, sc.IsSynchronized);
62                 }
63
64                 [Test]
65                 public void TestAdd ()
66                 {                       
67                         DataGrid grid = new DataGrid ();
68                         GridTableStylesCollection sc = grid.TableStyles;
69                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
70
71                         // Add single
72                         ResetEventData ();
73                         DataGridTableStyle ts = new DataGridTableStyle ();
74                         ts.MappingName = "Table1";
75                         sc.Add (ts);
76                         AssertEquals (true, eventhandled);
77                         AssertEquals (ts, Element);
78                         AssertEquals (CollectionChangeAction.Add, Action);
79
80                         // Add multiple
81                         ResetEventData ();
82                         sc.AddRange (new DataGridTableStyle [] {new DataGridTableStyle (), new DataGridTableStyle ()});
83                         AssertEquals (true, eventhandled);
84                         AssertEquals (null, Element);
85                         AssertEquals (CollectionChangeAction.Refresh, Action);
86                 }
87
88                 [Test]
89                 public void TestRemove ()
90                 {
91                         DataGrid grid = new DataGrid ();
92                         GridTableStylesCollection sc = grid.TableStyles;
93                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
94
95                         // Add single
96                         DataGridTableStyle ts1 = new DataGridTableStyle ();
97                         ts1.MappingName = "Table1";
98                         sc.Add (ts1);
99
100                         DataGridTableStyle ts2 = new DataGridTableStyle ();
101                         ts2.MappingName = "Table2";
102                         sc.Add (ts2);
103
104                         DataGridTableStyle ts3 = new DataGridTableStyle ();
105                         ts3.MappingName = "Table3";
106                         sc.Add (ts3);
107
108                         ResetEventData ();
109                         sc.Remove (ts2);
110                         AssertEquals (true, eventhandled);
111                         AssertEquals (ts2, Element);
112                         AssertEquals (CollectionChangeAction.Remove, Action);
113                         AssertEquals (2, sc.Count);
114
115                         ResetEventData ();
116                         sc.RemoveAt (0);
117                         AssertEquals (true, eventhandled);
118                         AssertEquals (ts1, Element);
119                         AssertEquals (CollectionChangeAction.Remove, Action);
120                         AssertEquals (1, sc.Count);
121
122                         ResetEventData ();
123                         sc.Clear ();
124                         AssertEquals (null, Element);
125                         AssertEquals (CollectionChangeAction.Refresh, Action);
126
127                 }
128
129                 [Test]
130                 public void TestIndexContains ()
131                 {
132                         DataGrid grid = new DataGrid ();
133                         GridTableStylesCollection sc = grid.TableStyles;
134                         sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
135
136                         // Add single
137                         DataGridTableStyle ts1 = new DataGridTableStyle ();
138                         ts1.MappingName = "Table1";
139                         sc.Add (ts1);
140
141                         DataGridTableStyle ts2 = new DataGridTableStyle ();
142                         ts2.MappingName = "Table2";
143                         sc.Add (ts2);
144
145                         DataGridTableStyle ts3 = new DataGridTableStyle ();
146                         ts3.MappingName = "Table3";
147                         sc.Add (ts3);
148
149                         ResetEventData ();
150                         IList ilist = (IList) sc;
151                         AssertEquals (1, ilist.IndexOf (ts2));
152                         AssertEquals (false, sc.Contains ("nothing"));
153                         AssertEquals (true, sc.Contains (ts3));
154                 }
155
156                 private void ResetEventData ()
157                 {
158                         eventhandled = false;
159                         Element = null;
160                         Action = (CollectionChangeAction) 0;
161                 }
162
163                 private void OnEventHandler (object sender, EventArgs e)\r
164                 {\r
165                         eventhandled = true;\r
166                 }
167
168                 private void OnCollectionEventHandler (object sender, CollectionChangeEventArgs e)\r
169                 {\r
170                         eventhandled = true;
171                         Element = e.Element;
172                         Action = e.Action;                      \r
173                 }
174         }
175 }