ListViewGroupCollectionTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // ListViewGroupCollectionTest.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. // Carlos Alberto Cortez <[email protected]>
  28. #if NET_2_0
  29. using System;
  30. using System.Windows.Forms;
  31. using System.Drawing;
  32. using System.Reflection;
  33. using System.Collections;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Windows.Forms
  36. {
  37. [TestFixture]
  38. public class ListViewGroupCollectionTest : TestHelper
  39. {
  40. ListViewGroupCollection grpCol = null;
  41. ListView lv = null;
  42. [SetUp]
  43. protected override void SetUp () {
  44. lv = new ListView ();
  45. grpCol = lv.Groups;
  46. base.SetUp ();
  47. }
  48. [Test]
  49. public void DefaultProperties ()
  50. {
  51. Assert.AreEqual (false, ((IList)grpCol).IsReadOnly, "#A1");
  52. Assert.AreEqual (false, ((IList)grpCol).IsFixedSize, "#A2");
  53. Assert.AreEqual (true, ((ICollection)grpCol).IsSynchronized, "#A3");
  54. Assert.AreEqual (grpCol, ((ICollection)grpCol).SyncRoot, "#A4");
  55. Assert.AreEqual (0, grpCol.Count, "#A5");
  56. }
  57. [Test]
  58. public void AddTest ()
  59. {
  60. ListViewGroup group1 = new ListViewGroup ("Item1");
  61. ListViewGroup group2 = new ListViewGroup ("Item2");
  62. grpCol.Add (group1);
  63. grpCol.Add (group2);
  64. Assert.AreEqual (2, grpCol.Count, "#B1");
  65. Assert.AreEqual (lv, group1.ListView, "#B2");
  66. Assert.AreEqual (lv, group2.ListView, "#B2");
  67. }
  68. [Test]
  69. public void ClearTest ()
  70. {
  71. ListViewGroup group1 = new ListViewGroup ("Item1");
  72. ListViewGroup group2 = new ListViewGroup ("Item2");
  73. grpCol.Add (group1);
  74. grpCol.Add (group2);
  75. grpCol.Clear ();
  76. Assert.AreEqual (0, grpCol.Count, "#C1");
  77. Assert.AreEqual (null, group1.ListView, "#C2");
  78. Assert.AreEqual (null, group2.ListView, "#C3");
  79. }
  80. [Test]
  81. public void ContainsTest ()
  82. {
  83. ListViewGroup obj = new ListViewGroup ("Item1");
  84. ListViewGroup obj2 = new ListViewGroup ("Item2");
  85. grpCol.Add (obj);
  86. Assert.AreEqual (true, grpCol.Contains (obj), "#D1");
  87. Assert.AreEqual (false, grpCol.Contains (obj2), "#D2");
  88. }
  89. [Test]
  90. public void IndexOfTest ()
  91. {
  92. ListViewGroup obj = new ListViewGroup ("Item1");
  93. ListViewGroup obj2 = new ListViewGroup ("Item2");
  94. grpCol.Add (obj);
  95. grpCol.Add (obj2);
  96. Assert.AreEqual (1, grpCol.IndexOf (obj2), "#E1");
  97. }
  98. [Test]
  99. public void RemoveTest ()
  100. {
  101. ListViewGroup obj = new ListViewGroup ("Item1");
  102. ListViewGroup obj2 = new ListViewGroup ("Item2");
  103. grpCol.Add (obj);
  104. grpCol.Add (obj2);
  105. grpCol.Remove (obj);
  106. Assert.AreEqual (1, grpCol.Count, "#F1");
  107. Assert.AreEqual (null, obj.ListView, "#F2");
  108. Assert.AreEqual (lv, obj2.ListView, "#F3");
  109. }
  110. [Test]
  111. public void RemoveAtTest ()
  112. {
  113. ListViewGroup obj = new ListViewGroup ("Item1");
  114. ListViewGroup obj2 = new ListViewGroup ("Item2");
  115. grpCol.Add (obj);
  116. grpCol.Add (obj2);
  117. grpCol.RemoveAt (0);
  118. Assert.AreEqual (1, grpCol.Count, "#G1");
  119. Assert.AreEqual (true, grpCol.Contains (obj2), "#G2");
  120. Assert.AreEqual (null, obj.ListView, "#G3");
  121. Assert.AreEqual (lv, obj2.ListView, "#G4");
  122. }
  123. [Test]
  124. public void IndexerTest ()
  125. {
  126. ListViewGroup group1 = new ListViewGroup ("Item1");
  127. grpCol.Add (group1);
  128. Assert.AreEqual (group1, grpCol [0], "#A1");
  129. Assert.AreEqual (lv, group1.ListView, "#A2");
  130. Assert.AreEqual (1, grpCol.Count, "#A3");
  131. grpCol [0] = null;
  132. Assert.AreEqual (null, grpCol [0], "#A4");
  133. Assert.AreEqual (1, grpCol.Count, "#A5");
  134. ListViewGroup group2 = new ListViewGroup ("Item2");
  135. grpCol [0] = group2;
  136. Assert.AreEqual (group2, grpCol [0], "#A6");
  137. Assert.AreEqual (null, group2.ListView, "#A7");
  138. Assert.AreEqual (1, grpCol.Count, "#A8");
  139. }
  140. [Test]
  141. public void IndexerNullTest ()
  142. {
  143. ListViewGroup group1 = new ListViewGroup ("Item1");
  144. grpCol.Add (group1);
  145. grpCol [0] = null;
  146. Assert.AreEqual (null, grpCol [0], "#A1");
  147. Assert.AreEqual (1, grpCol.Count, "#A2");
  148. }
  149. /* There's an inconsistency between other collections using
  150. * Key methods and the impl of this collection */
  151. [Test]
  152. public void IndexerKeyTest ()
  153. {
  154. ListViewGroup group1 = new ListViewGroup ("Item1");
  155. ListViewGroup group2 = new ListViewGroup ("Item2");
  156. ListViewGroup group3 = new ListViewGroup ("Item3");
  157. grpCol.Add (group1);
  158. grpCol.Add (group2);
  159. grpCol.Add (group3);
  160. group1.Name = String.Empty;
  161. group2.Name = "A";
  162. group3.Name = "A";
  163. Assert.AreEqual (group1, grpCol [String.Empty], "#A1"); /* Inconsistent */
  164. Assert.AreEqual (null, grpCol [null], "#A2");
  165. Assert.AreEqual (group2, grpCol ["A"], "#A3");
  166. Assert.AreEqual (null, grpCol ["a"], "#A4"); /* Inconsistent, again */
  167. ListViewGroup group4 = new ListViewGroup ("Item4");
  168. group4.Name = "A";
  169. grpCol [String.Empty] = group4;
  170. Assert.AreEqual (group4, grpCol [0], "#A5"); /* First position */
  171. Assert.AreEqual (group4, grpCol ["A"], "#A6");
  172. Assert.AreEqual (null, group4.ListView, "#A7");
  173. grpCol ["A"] = null;
  174. Assert.AreEqual (null, grpCol [0], "#A8");
  175. }
  176. [Test]
  177. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  178. public void IndexerOutOfRangeTest ()
  179. {
  180. grpCol.Add (new ListViewGroup ("Item1"));
  181. grpCol[10] = null;
  182. }
  183. [Test]
  184. public void IndexerOutOfRangeTest2()
  185. { //.NET 2.0 don't throw a exception here
  186. grpCol.Add (new ListViewGroup ("Item1"));
  187. grpCol["TestItemThatDoesNotExist"] = null;
  188. Assert.IsNotNull (grpCol [0], "#A1");
  189. }
  190. }
  191. }
  192. #endif