GridTableStylesCollectionTest.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. 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 TestAdd ()
  55. {
  56. DataGrid grid = new DataGrid ();
  57. GridTableStylesCollection sc = grid.TableStyles;
  58. sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
  59. // Add single
  60. ResetEventData ();
  61. DataGridTableStyle ts = new DataGridTableStyle ();
  62. ts.MappingName = "Table1";
  63. sc.Add (ts);
  64. Assert.AreEqual (true, eventhandled, "A1");
  65. Assert.AreEqual (ts, Element, "A2");
  66. Assert.AreEqual (CollectionChangeAction.Add, Action, "A3");
  67. // Add multiple
  68. ResetEventData ();
  69. sc.AddRange (new DataGridTableStyle [] {new DataGridTableStyle (), new DataGridTableStyle ()});
  70. Assert.AreEqual (true, eventhandled, "A4");
  71. Assert.AreEqual (null, Element, "A5");
  72. Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A6");
  73. }
  74. [Test]
  75. public void TestAddRange ()
  76. {
  77. DataGrid grid = new DataGrid ();
  78. GridTableStylesCollection sc = grid.TableStyles;
  79. sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
  80. ResetEventData ();
  81. DataGridTableStyle ts1 = new DataGridTableStyle ();
  82. ts1.MappingName = "Table1";
  83. DataGridTableStyle ts2 = new DataGridTableStyle ();
  84. ts2.MappingName = "Table2";
  85. sc.AddRange (new DataGridTableStyle[] {ts1, ts2});
  86. Assert.AreEqual (true, eventhandled, "A1");
  87. Assert.AreEqual (null, Element, "A2");
  88. Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A3");
  89. Assert.AreEqual (1, times, "A4");
  90. }
  91. [Test]
  92. public void TestRemove ()
  93. {
  94. DataGrid grid = new DataGrid ();
  95. GridTableStylesCollection sc = grid.TableStyles;
  96. sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
  97. // Add single
  98. DataGridTableStyle ts1 = new DataGridTableStyle ();
  99. ts1.MappingName = "Table1";
  100. sc.Add (ts1);
  101. DataGridTableStyle ts2 = new DataGridTableStyle ();
  102. ts2.MappingName = "Table2";
  103. sc.Add (ts2);
  104. DataGridTableStyle ts3 = new DataGridTableStyle ();
  105. ts3.MappingName = "Table3";
  106. sc.Add (ts3);
  107. ResetEventData ();
  108. sc.Remove (ts2);
  109. Assert.AreEqual (true, eventhandled, "A1");
  110. Assert.AreEqual (ts2, Element, "A2");
  111. Assert.AreEqual (CollectionChangeAction.Remove, Action, "A3");
  112. Assert.AreEqual (2, sc.Count, "A4");
  113. ResetEventData ();
  114. sc.RemoveAt (0);
  115. Assert.AreEqual (true, eventhandled, "A5");
  116. Assert.AreEqual (ts1, Element, "A6");
  117. Assert.AreEqual (CollectionChangeAction.Remove, Action, "A7");
  118. Assert.AreEqual (1, sc.Count, "A8");
  119. ResetEventData ();
  120. sc.Clear ();
  121. Assert.AreEqual (null, Element, "A9");
  122. Assert.AreEqual (CollectionChangeAction.Refresh, Action, "A10");
  123. }
  124. [Test]
  125. public void TestIndexContains ()
  126. {
  127. DataGrid grid = new DataGrid ();
  128. GridTableStylesCollection sc = grid.TableStyles;
  129. sc.CollectionChanged += new CollectionChangeEventHandler (OnCollectionEventHandler);
  130. // Add single
  131. DataGridTableStyle ts1 = new DataGridTableStyle ();
  132. ts1.MappingName = "Table1";
  133. sc.Add (ts1);
  134. DataGridTableStyle ts2 = new DataGridTableStyle ();
  135. ts2.MappingName = "Table2";
  136. sc.Add (ts2);
  137. DataGridTableStyle ts3 = new DataGridTableStyle ();
  138. ts3.MappingName = "Table3";
  139. sc.Add (ts3);
  140. ResetEventData ();
  141. IList ilist = (IList) sc;
  142. Assert.AreEqual (1, ilist.IndexOf (ts2), "A1");
  143. Assert.AreEqual (false, sc.Contains ("nothing"), "A2");
  144. Assert.AreEqual (true, sc.Contains (ts3), "A3");
  145. }
  146. private void ResetEventData ()
  147. {
  148. times = 0;
  149. eventhandled = false;
  150. Element = null;
  151. Action = (CollectionChangeAction) 0;
  152. }
  153. private void OnEventHandler (object sender, EventArgs e)
  154. {
  155. eventhandled = true;
  156. }
  157. private void OnCollectionEventHandler (object sender, CollectionChangeEventArgs e)
  158. {
  159. times++;
  160. eventhandled = true;
  161. Element = e.Element;
  162. Action = e.Action;
  163. }
  164. }
  165. }