GridTableStylesCollectionTest.cs 6.4 KB

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