AutoCompleteStringCollectionTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // AutoCompleteStringCollectionTest.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Daniel Nauck
  24. //
  25. // Author:
  26. // Daniel Nauck (dna(at)mono-project(dot)de)
  27. #if NET_2_0
  28. using System;
  29. using System.Windows.Forms;
  30. using System.Drawing;
  31. using System.Reflection;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Windows.Forms
  36. {
  37. [TestFixture]
  38. public class AutoCompleteStringCollectionTest : TestHelper
  39. {
  40. AutoCompleteStringCollection autoCol = null;
  41. private int add_event = 0;
  42. private int refresh_event = 0;
  43. private int remove_event = 0;
  44. private string value_event = null;
  45. [SetUp]
  46. protected override void SetUp ()
  47. {
  48. autoCol = new AutoCompleteStringCollection ();
  49. autoCol.CollectionChanged += new CollectionChangeEventHandler (AutoColChanged);
  50. add_event = 0;
  51. refresh_event = 0;
  52. remove_event = 0;
  53. value_event = "unknown item";
  54. base.SetUp ();
  55. }
  56. private void AutoColChanged(object sender, CollectionChangeEventArgs e)
  57. {
  58. switch (e.Action)
  59. {
  60. case CollectionChangeAction.Add:
  61. add_event++;
  62. value_event = (string)e.Element;
  63. break;
  64. case CollectionChangeAction.Refresh:
  65. refresh_event++;
  66. value_event = (string)e.Element;
  67. break;
  68. case CollectionChangeAction.Remove:
  69. remove_event++;
  70. value_event = (string)e.Element;
  71. break;
  72. }
  73. }
  74. [Test]
  75. public void DefaultProperties ()
  76. {
  77. Assert.AreEqual (false, autoCol.IsReadOnly, "#A1");
  78. Assert.AreEqual (false, ((IList)autoCol).IsFixedSize, "#A2");
  79. Assert.AreEqual (false, autoCol.IsSynchronized, "#A3");
  80. Assert.AreEqual (autoCol, autoCol.SyncRoot, "#A4");
  81. Assert.AreEqual (0, autoCol.Count, "#A5");
  82. }
  83. [Test]
  84. public void AddTest ()
  85. {
  86. int item1 = autoCol.Add ("Item1");
  87. Assert.AreEqual (1, add_event, "#B1");
  88. Assert.AreEqual ("Item1", value_event, "#B2");
  89. int item2 = autoCol.Add ("Item2");
  90. Assert.AreEqual (2, add_event, "#B3");
  91. Assert.AreEqual ("Item2", value_event, "#B4");
  92. Assert.AreEqual (2, autoCol.Count, "#B5");
  93. Assert.AreEqual ("Item1", autoCol[item1], "#B6");
  94. Assert.AreEqual ("Item2", autoCol[item2], "#B7");
  95. }
  96. [Test]
  97. public void ClearTest ()
  98. {
  99. autoCol.Add ("Item1");
  100. autoCol.Add ("Item2");
  101. autoCol.Clear ();
  102. Assert.AreEqual (1, refresh_event, "#C1");
  103. Assert.AreEqual (null, value_event, "#C2");
  104. Assert.AreEqual (0, autoCol.Count, "#C3");
  105. }
  106. [Test]
  107. public void ContainsTest ()
  108. {
  109. autoCol.Add ("Item1");
  110. Assert.AreEqual (true, autoCol.Contains("Item1"), "#D1");
  111. Assert.AreEqual (false, autoCol.Contains("Item2"), "#D2");
  112. }
  113. [Test]
  114. public void IndexOfTest ()
  115. {
  116. autoCol.Add ("Item1");
  117. autoCol.Add ("Item2");
  118. Assert.AreEqual (1, autoCol.IndexOf("Item2"), "#E1");
  119. }
  120. [Test]
  121. public void RemoveTest ()
  122. {
  123. autoCol.Add ("Item1");
  124. autoCol.Add ("Item2");
  125. autoCol.Remove ("Item1");
  126. Assert.AreEqual(1, remove_event, "#F1");
  127. Assert.AreEqual("Item1", value_event, "#F2");
  128. Assert.AreEqual (1, autoCol.Count, "#F3");
  129. Assert.AreEqual ("Item2", autoCol[0], "#F4");
  130. }
  131. [Test]
  132. public void RemoveAtTest ()
  133. {
  134. autoCol.Add ("Item1");
  135. autoCol.Add ("Item2");
  136. autoCol.RemoveAt (0);
  137. Assert.AreEqual(1, remove_event, "#G1");
  138. Assert.AreEqual("Item1", value_event, "#G2");
  139. Assert.AreEqual (1, autoCol.Count, "#G3");
  140. Assert.AreEqual (true, autoCol.Contains("Item2"), "#G4");
  141. }
  142. [Test]
  143. public void AddRangeTest ()
  144. {
  145. string[] values = new string[] { "Item1", "Item2", "Item3", "Item4" };
  146. autoCol.Add ("Item5");
  147. autoCol.AddRange (values);
  148. Assert.AreEqual(1, refresh_event, "#E1");
  149. Assert.AreEqual(null, value_event, "#E2");
  150. Assert.AreEqual (5, autoCol.Count, "#E3");
  151. Assert.AreEqual (true, autoCol.Contains("Item1"), "#E4");
  152. Assert.AreEqual (true, autoCol.Contains("Item2"), "#E5");
  153. Assert.AreEqual (true, autoCol.Contains("Item3"), "#E6");
  154. Assert.AreEqual (true, autoCol.Contains("Item4"), "#E7");
  155. Assert.AreEqual (true, autoCol.Contains("Item5"), "#E8");
  156. }
  157. [Test]
  158. [ExpectedException (typeof (ArgumentNullException))]
  159. public void AddRangeNullTest ()
  160. {
  161. string[] values = null;
  162. autoCol.AddRange (values);
  163. }
  164. [Test]
  165. public void AddTest_Junk ()
  166. {
  167. autoCol.Add (null);
  168. Assert.AreEqual (1, autoCol.Count, "#F1");
  169. autoCol.Add (null);
  170. Assert.AreEqual (2, autoCol.Count, "#F2");
  171. Assert.AreEqual (true, autoCol.Contains(null), "#F3");
  172. Assert.AreEqual (0, autoCol.IndexOf(null), "#F4");
  173. autoCol.Remove (null);
  174. Assert.AreEqual (1, autoCol.Count, "#F5");
  175. autoCol[0] = "Item1";
  176. autoCol[0] = null;
  177. Assert.AreEqual (null, autoCol[0], "#F6");
  178. }
  179. [Test]
  180. public void IndexerTest()
  181. {
  182. autoCol.Add("Item1");
  183. autoCol[0] = "NewItem1";
  184. Assert.AreEqual(1, remove_event, "#G1");
  185. Assert.AreEqual(2, add_event, "#G2");
  186. }
  187. }
  188. }
  189. #endif