AutoCompleteStringCollectionTest.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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
  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. public 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. }
  55. private void AutoColChanged(object sender, CollectionChangeEventArgs e)
  56. {
  57. switch (e.Action)
  58. {
  59. case CollectionChangeAction.Add:
  60. add_event++;
  61. value_event = (string)e.Element;
  62. break;
  63. case CollectionChangeAction.Refresh:
  64. refresh_event++;
  65. value_event = (string)e.Element;
  66. break;
  67. case CollectionChangeAction.Remove:
  68. remove_event++;
  69. value_event = (string)e.Element;
  70. break;
  71. }
  72. }
  73. [Test]
  74. public void DefaultProperties ()
  75. {
  76. Assert.AreEqual (false, autoCol.IsReadOnly, "#A1");
  77. Assert.AreEqual (false, ((IList)autoCol).IsFixedSize, "#A2");
  78. Assert.AreEqual (false, autoCol.IsSynchronized, "#A3");
  79. Assert.AreEqual (autoCol, autoCol.SyncRoot, "#A4");
  80. Assert.AreEqual (0, autoCol.Count, "#A5");
  81. }
  82. [Test]
  83. public void AddTest ()
  84. {
  85. int item1 = autoCol.Add ("Item1");
  86. Assert.AreEqual (1, add_event, "#B1");
  87. Assert.AreEqual ("Item1", value_event, "#B2");
  88. int item2 = autoCol.Add ("Item2");
  89. Assert.AreEqual (2, add_event, "#B3");
  90. Assert.AreEqual ("Item2", value_event, "#B4");
  91. Assert.AreEqual (2, autoCol.Count, "#B5");
  92. Assert.AreEqual ("Item1", autoCol[item1], "#B6");
  93. Assert.AreEqual ("Item2", autoCol[item2], "#B7");
  94. }
  95. [Test]
  96. public void ClearTest ()
  97. {
  98. autoCol.Add ("Item1");
  99. autoCol.Add ("Item2");
  100. autoCol.Clear ();
  101. Assert.AreEqual (1, refresh_event, "#C1");
  102. Assert.AreEqual (null, value_event, "#C2");
  103. Assert.AreEqual (0, autoCol.Count, "#C3");
  104. }
  105. [Test]
  106. public void ContainsTest ()
  107. {
  108. autoCol.Add ("Item1");
  109. Assert.AreEqual (true, autoCol.Contains("Item1"), "#D1");
  110. Assert.AreEqual (false, autoCol.Contains("Item2"), "#D2");
  111. }
  112. [Test]
  113. public void IndexOfTest ()
  114. {
  115. autoCol.Add ("Item1");
  116. autoCol.Add ("Item2");
  117. Assert.AreEqual (1, autoCol.IndexOf("Item2"), "#E1");
  118. }
  119. [Test]
  120. public void RemoveTest ()
  121. {
  122. autoCol.Add ("Item1");
  123. autoCol.Add ("Item2");
  124. autoCol.Remove ("Item1");
  125. Assert.AreEqual(1, remove_event, "#F1");
  126. Assert.AreEqual("Item1", value_event, "#F2");
  127. Assert.AreEqual (1, autoCol.Count, "#F3");
  128. Assert.AreEqual ("Item2", autoCol[0], "#F4");
  129. }
  130. [Test]
  131. public void RemoveAtTest ()
  132. {
  133. autoCol.Add ("Item1");
  134. autoCol.Add ("Item2");
  135. autoCol.RemoveAt (0);
  136. Assert.AreEqual(1, remove_event, "#G1");
  137. Assert.AreEqual("Item1", value_event, "#G2");
  138. Assert.AreEqual (1, autoCol.Count, "#G3");
  139. Assert.AreEqual (true, autoCol.Contains("Item2"), "#G4");
  140. }
  141. [Test]
  142. public void AddRangeTest ()
  143. {
  144. string[] values = new string[] { "Item1", "Item2", "Item3", "Item4" };
  145. autoCol.Add ("Item5");
  146. autoCol.AddRange (values);
  147. Assert.AreEqual(1, refresh_event, "#E1");
  148. Assert.AreEqual(null, value_event, "#E2");
  149. Assert.AreEqual (5, autoCol.Count, "#E3");
  150. Assert.AreEqual (true, autoCol.Contains("Item1"), "#E4");
  151. Assert.AreEqual (true, autoCol.Contains("Item2"), "#E5");
  152. Assert.AreEqual (true, autoCol.Contains("Item3"), "#E6");
  153. Assert.AreEqual (true, autoCol.Contains("Item4"), "#E7");
  154. Assert.AreEqual (true, autoCol.Contains("Item5"), "#E8");
  155. }
  156. [Test]
  157. [ExpectedException (typeof (ArgumentNullException))]
  158. public void AddRangeNullTest ()
  159. {
  160. string[] values = null;
  161. autoCol.AddRange (values);
  162. }
  163. [Test]
  164. public void AddTest_Junk ()
  165. {
  166. autoCol.Add (null);
  167. Assert.AreEqual (1, autoCol.Count, "#F1");
  168. autoCol.Add (null);
  169. Assert.AreEqual (2, autoCol.Count, "#F2");
  170. Assert.AreEqual (true, autoCol.Contains(null), "#F3");
  171. Assert.AreEqual (0, autoCol.IndexOf(null), "#F4");
  172. autoCol.Remove (null);
  173. Assert.AreEqual (1, autoCol.Count, "#F5");
  174. autoCol[0] = "Item1";
  175. autoCol[0] = null;
  176. Assert.AreEqual (null, autoCol[0], "#F6");
  177. }
  178. [Test]
  179. public void IndexerTest()
  180. {
  181. autoCol.Add("Item1");
  182. autoCol[0] = "NewItem1";
  183. Assert.AreEqual(1, remove_event, "#G1");
  184. Assert.AreEqual(2, add_event, "#G2");
  185. }
  186. }
  187. }
  188. #endif