GridTableStylesCollectionTest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // Fails due to:
  70. // "The TableStyles collection already has a TableStyle with this mapping name"
  71. // There is a TODO in DataGrid about allowing TableStyles to have the same mapping name.
  72. [Test]
  73. [NUnit.Framework.Category ("NotWorking")]
  74. public void TestAdd ()
  75. {
  76. DataGrid grid = new DataGrid ();
  77. GridTableStylesCollection sc = grid.TableStyles;
  78. sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
  79. // Add single
  80. ResetEventData ();
  81. DataGridTableStyle ts = new DataGridTableStyle ();
  82. ts.MappingName = "Table1";
  83. sc.Add (ts);
  84. Assert.AreEqual (true, eventhandled, "A1");
  85. Assert.AreEqual (ts, Element, "A2");
  86. Assert.AreEqual (CollectionChangeAction.Add, Action, "A3");
  87. // Add multiple
  88. ResetEventData ();
  89. sc.AddRange (new DataGridTableStyle [] {new DataGridTableStyle (), new DataGridTableStyle ()});
  90. Assert.AreEqual (true, eventhandled, "A4");
  91. Assert.AreEqual (null, Element, "A5");
  92. Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A6");
  93. }
  94. [Test]
  95. public void TestAddRange ()
  96. {
  97. DataGrid grid = new DataGrid ();
  98. GridTableStylesCollection sc = grid.TableStyles;
  99. sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
  100. ResetEventData ();
  101. DataGridTableStyle ts1 = new DataGridTableStyle ();
  102. ts1.MappingName = "Table1";
  103. DataGridTableStyle ts2 = new DataGridTableStyle ();
  104. ts2.MappingName = "Table2";
  105. sc.AddRange (new DataGridTableStyle[] {ts1, ts2});
  106. Assert.AreEqual (true, eventhandled, "A1");
  107. Assert.AreEqual (null, Element, "A2");
  108. Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A3");
  109. Assert.AreEqual (1, times, "A4");
  110. }
  111. [Test]
  112. public void TestRemove ()
  113. {
  114. DataGrid grid = new DataGrid ();
  115. GridTableStylesCollection sc = grid.TableStyles;
  116. sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
  117. // Add single
  118. DataGridTableStyle ts1 = new DataGridTableStyle ();
  119. ts1.MappingName = "Table1";
  120. sc.Add (ts1);
  121. DataGridTableStyle ts2 = new DataGridTableStyle ();
  122. ts2.MappingName = "Table2";
  123. sc.Add (ts2);
  124. DataGridTableStyle ts3 = new DataGridTableStyle ();
  125. ts3.MappingName = "Table3";
  126. sc.Add (ts3);
  127. ResetEventData ();
  128. sc.Remove (ts2);
  129. Assert.AreEqual (true, eventhandled, "A1");
  130. Assert.AreEqual (ts2, Element, "A2");
  131. Assert.AreEqual (CollectionChangeAction.Remove, Action, "A3");
  132. Assert.AreEqual (2, sc.Count, "A4");
  133. ResetEventData ();
  134. sc.RemoveAt (0);
  135. Assert.AreEqual (true, eventhandled, "A5");
  136. Assert.AreEqual (ts1, Element, "A6");
  137. Assert.AreEqual (CollectionChangeAction.Remove, Action, "A7");
  138. Assert.AreEqual (1, sc.Count, "A8");
  139. ResetEventData ();
  140. sc.Clear ();
  141. Assert.AreEqual (null, Element, "A9");
  142. Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A10");
  143. }
  144. [Test]
  145. public void TestIndexContains ()
  146. {
  147. DataGrid grid = new DataGrid ();
  148. GridTableStylesCollection sc = grid.TableStyles;
  149. sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
  150. // Add single
  151. DataGridTableStyle ts1 = new DataGridTableStyle ();
  152. ts1.MappingName = "Table1";
  153. sc.Add (ts1);
  154. DataGridTableStyle ts2 = new DataGridTableStyle ();
  155. ts2.MappingName = "Table2";
  156. sc.Add (ts2);
  157. DataGridTableStyle ts3 = new DataGridTableStyle ();
  158. ts3.MappingName = "Table3";
  159. sc.Add (ts3);
  160. ResetEventData ();
  161. IList ilist = (IList) sc;
  162. Assert.AreEqual (1, ilist.IndexOf (ts2), "A1");
  163. Assert.AreEqual (false, sc.Contains ("nothing"), "A2");
  164. Assert.AreEqual (true, sc.Contains (ts3), "A3");
  165. }
  166. private void ResetEventData ()
  167. {
  168. times = 0;
  169. eventhandled = false;
  170. Element = null;
  171. Action = (CollectionChangeAction) 0;
  172. }
  173. //private void OnEventHandler (object sender, EventArgs e)
  174. //{
  175. // eventhandled = true;
  176. //}
  177. private void OnCollectionEventHandler (object sender, CollectionChangeEventArgs e)
  178. {
  179. times++;
  180. eventhandled = true;
  181. Element = e.Element;
  182. Action = e.Action;
  183. }
  184. }
  185. }