GridColumnStylesCollectionTest.cs 6.1 KB

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