PageRenderTime 60ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/OLV Remake/Tests/TestCheckBoxes.cs

#
C# | 295 lines | 237 code | 41 blank | 17 comment | 16 complexity | adffc211cdc04ff5422f28a150259aa1 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * [File purpose]
  3. * Author: Phillip Piper
  4. * Date: 10/25/2008 11:06 PM
  5. *
  6. * CHANGE LOG:
  7. * when who what
  8. * 10/25/2008 JPP Initial Version
  9. */
  10. using System.Windows.Forms;
  11. using NUnit.Framework;
  12. namespace BrightIdeasSoftware.Tests
  13. {
  14. [TestFixture]
  15. public class TestOlvCheckBoxes
  16. {
  17. [SetUp]
  18. public void InitEachTest() {
  19. if (!this.olv.CheckBoxes)
  20. this.olv.CheckBoxes = true;
  21. this.olv.SetObjects(PersonDb.All);
  22. this.olv.CheckedObjects = null;
  23. this.olv.TriStateCheckBoxes = false;
  24. }
  25. [TearDown]
  26. public void TearDownEachTest() {
  27. this.olv.CheckStateGetter = null;
  28. this.olv.CheckStatePutter = null;
  29. }
  30. [Test]
  31. public void TestCheckObject() {
  32. Person p = PersonDb.All[1];
  33. this.olv.CheckObject(p);
  34. Assert.AreEqual(p, this.olv.CheckedObject);
  35. Assert.IsTrue(this.olv.IsChecked(p));
  36. Assert.IsFalse(this.olv.IsCheckedIndeterminate(p));
  37. Assert.AreEqual(CheckState.Checked, this.olv.ModelToItem(p).CheckState);
  38. }
  39. [Test]
  40. public void TestCheckIndeterminateObject() {
  41. Person p = PersonDb.All[1];
  42. this.olv.CheckIndeterminateObject(p);
  43. Assert.IsTrue(this.olv.IsCheckedIndeterminate(p));
  44. Assert.IsFalse(this.olv.IsChecked(p));
  45. Assert.AreEqual(CheckState.Indeterminate, this.olv.ModelToItem(p).CheckState);
  46. }
  47. [Test]
  48. public void TestUncheckObject() {
  49. Person p = PersonDb.All[1];
  50. this.olv.CheckedObjects = PersonDb.All;
  51. this.olv.UncheckObject(p);
  52. Assert.IsFalse(this.olv.IsChecked(p));
  53. Assert.IsFalse(this.olv.IsCheckedIndeterminate(p));
  54. Assert.AreEqual(CheckState.Unchecked, this.olv.ModelToItem(p).CheckState);
  55. }
  56. [Test]
  57. public void TestToggleCheckObject() {
  58. this.olv.ToggleCheckObject(PersonDb.All[1]);
  59. Assert.IsTrue(this.olv.IsChecked(PersonDb.All[1]));
  60. this.olv.ToggleCheckObject(PersonDb.All[1]);
  61. Assert.IsFalse(this.olv.IsChecked(PersonDb.All[1]));
  62. }
  63. [Test]
  64. public void TestCheckedObject() {
  65. this.olv.CheckedObject = PersonDb.All[1];
  66. Assert.AreEqual(PersonDb.All[1], this.olv.CheckedObject);
  67. }
  68. [Test]
  69. public void TestCheckedObjects() {
  70. this.olv.CheckedObjects = PersonDb.All;
  71. // We really want to check that the collections contain the same members, but
  72. // there is no easy way to do that
  73. Assert.AreEqual(PersonDb.All.Count, this.olv.CheckedObjects.Count);
  74. }
  75. [Test]
  76. public void TestCheckStateGetter() {
  77. this.olv.TriStateCheckBoxes = true;
  78. this.olv.CheckStateGetter = delegate(object x) {
  79. int idx = PersonDb.All.IndexOf((Person)x);
  80. switch (idx) {
  81. case 0:
  82. return CheckState.Checked;
  83. case 1:
  84. return CheckState.Unchecked;
  85. default:
  86. return CheckState.Indeterminate;
  87. }
  88. };
  89. this.olv.SetObjects(PersonDb.All);
  90. Assert.IsTrue(this.olv.IsChecked(PersonDb.All[0]));
  91. Assert.IsTrue(this.olv.ModelToItem(PersonDb.All[0]).CheckState == CheckState.Checked);
  92. Assert.IsFalse(this.olv.IsChecked(PersonDb.All[1]));
  93. Assert.IsTrue(this.olv.ModelToItem(PersonDb.All[1]).CheckState == CheckState.Unchecked);
  94. for (int i = 2; i < PersonDb.All.Count; i++) {
  95. Assert.IsTrue(this.olv.IsCheckedIndeterminate(PersonDb.All[i]));
  96. Assert.IsTrue(this.olv.ModelToItem(PersonDb.All[i]).CheckState == CheckState.Indeterminate);
  97. }
  98. }
  99. [Test]
  100. public void TestBooleanCheckStateGetter() {
  101. this.olv.BooleanCheckStateGetter = delegate(object x) { return x == PersonDb.All[1]; };
  102. this.olv.SetObjects(PersonDb.All);
  103. Assert.IsTrue(this.olv.IsChecked(PersonDb.All[1]));
  104. Assert.IsFalse(this.olv.IsChecked(PersonDb.All[0]));
  105. this.olv.BooleanCheckStateGetter = delegate(object x) { return x != PersonDb.All[0]; };
  106. this.olv.SetObjects(PersonDb.All);
  107. foreach (Person x in PersonDb.All) {
  108. if (x == PersonDb.All[0])
  109. Assert.IsFalse(this.olv.IsChecked(x));
  110. else
  111. Assert.IsTrue(this.olv.IsChecked(x));
  112. }
  113. }
  114. [Test]
  115. public void TestCheckStatePutter() {
  116. this.olv.CheckStatePutter = delegate(object x, CheckState state) {
  117. Person p = (Person)x;
  118. switch (state) {
  119. case CheckState.Checked:
  120. p.IsActive = true;
  121. break;
  122. case CheckState.Unchecked:
  123. p.IsActive = false;
  124. break;
  125. case CheckState.Indeterminate:
  126. p.IsActive = null;
  127. break;
  128. }
  129. return state;
  130. };
  131. this.olv.CheckObject(PersonDb.All[0]);
  132. Assert.IsTrue(PersonDb.All[0].IsActive == true);
  133. this.olv.UncheckObject(PersonDb.All[0]);
  134. Assert.IsTrue(PersonDb.All[0].IsActive == false);
  135. this.olv.CheckIndeterminateObject(PersonDb.All[0]);
  136. Assert.IsFalse(PersonDb.All[0].IsActive.HasValue);
  137. }
  138. [Test]
  139. public void TestCheckStatePutterWithDifferentReturnValue() {
  140. this.olv.CheckStatePutter = delegate(object x, CheckState state) {
  141. Person p = (Person)x;
  142. // Pretend first person cannot be activated
  143. if (p == PersonDb.All[0]) {
  144. p.IsActive = false;
  145. return CheckState.Unchecked;
  146. }
  147. switch (state) {
  148. case CheckState.Checked:
  149. p.IsActive = true;
  150. break;
  151. case CheckState.Unchecked:
  152. p.IsActive = false;
  153. break;
  154. case CheckState.Indeterminate:
  155. p.IsActive = null;
  156. break;
  157. }
  158. return state;
  159. };
  160. this.olv.CheckObject(PersonDb.All[0]);
  161. Assert.IsFalse(this.olv.IsChecked(PersonDb.All[0]));
  162. Assert.AreEqual(CheckState.Unchecked, this.olv.ModelToItem(PersonDb.All[1]).CheckState);
  163. }
  164. [Test]
  165. public void TestItemCheckEvent() {
  166. this.olv.TriStateCheckBoxes = true;
  167. try {
  168. this.olv.ItemCheck += new ItemCheckEventHandler(olv_ItemCheck);
  169. Person p = PersonDb.All[0];
  170. this.olv.CheckObject(p);
  171. Assert.AreEqual(p, this.personInEvent);
  172. Assert.AreEqual(CheckState.Checked, this.stateInEvent);
  173. p = PersonDb.All[1];
  174. this.olv.CheckObject(p);
  175. Assert.AreEqual(p, this.personInEvent);
  176. Assert.AreEqual(CheckState.Checked, this.stateInEvent);
  177. this.olv.UncheckObject(p);
  178. Assert.AreEqual(p, this.personInEvent);
  179. Assert.AreEqual(CheckState.Unchecked, this.stateInEvent);
  180. this.olv.CheckIndeterminateObject(p);
  181. Assert.AreEqual(p, this.personInEvent);
  182. Assert.AreEqual(CheckState.Indeterminate, this.stateInEvent);
  183. }
  184. finally {
  185. this.olv.ItemCheck -= new ItemCheckEventHandler(olv_ItemCheck);
  186. }
  187. }
  188. Person personInEvent;
  189. CheckState stateInEvent;
  190. void olv_ItemCheck(object sender, ItemCheckEventArgs e) {
  191. this.personInEvent = (Person)this.olv.GetItem(e.Index).RowObject;
  192. this.stateInEvent = e.NewValue;
  193. }
  194. [Test]
  195. public void TestSpaceBar() {
  196. // For some reason, SendKeys doesn't work here
  197. //this.olv.Select(); // Make the listview have the keyboard focus
  198. //this.olv.SelectAll();
  199. //SendKeys.SendWait(" ");
  200. //Assert.AreEqual(this.olv.GetItemCount(), this.olv.CheckedObjects.Count);
  201. }
  202. [Test]
  203. public void TestCheckedAspectName() {
  204. foreach (Person p in PersonDb.All)
  205. p.IsActive = false;
  206. this.olv.CheckedAspectName = "IsActive";
  207. this.olv.SetObjects(PersonDb.All);
  208. Assert.IsEmpty(this.olv.CheckedObjects);
  209. foreach (Person p in PersonDb.All)
  210. p.IsActive = true;
  211. this.olv.SetObjects(PersonDb.All);
  212. Assert.AreEqual(PersonDb.All.Count, this.olv.CheckedObjects.Count);
  213. this.olv.CheckedAspectName = null;
  214. }
  215. [Test]
  216. public void TestSubItemCheckBox() {
  217. PersonDb.All[0].IsActive = true;
  218. PersonDb.All[1].IsActive = false;
  219. OLVColumn column = this.olv.GetColumn(1);
  220. string oldAspectName = column.AspectName;
  221. column.AspectName = "IsActive";
  222. column.CheckBoxes = true;
  223. this.olv.SetObjects(PersonDb.All);
  224. Assert.IsTrue(this.olv.IsSubItemChecked(PersonDb.All[0], column));
  225. Assert.IsFalse(this.olv.IsSubItemChecked(PersonDb.All[1], column));
  226. this.olv.ToggleSubItemCheckBox(PersonDb.All[0], column);
  227. this.olv.ToggleSubItemCheckBox(PersonDb.All[1], column);
  228. Assert.IsFalse(this.olv.IsSubItemChecked(PersonDb.All[0], column));
  229. Assert.IsTrue(this.olv.IsSubItemChecked(PersonDb.All[1], column));
  230. this.olv.GetColumn(1).AspectName = oldAspectName;
  231. }
  232. [TestFixtureSetUp]
  233. public void Init() {
  234. this.olv = MyGlobals.mainForm.objectListView1;
  235. }
  236. protected ObjectListView olv;
  237. }
  238. [TestFixture]
  239. public class TestFastOlvCheckBoxes : TestOlvCheckBoxes
  240. {
  241. [TestFixtureSetUp]
  242. new public void Init() {
  243. this.olv = MyGlobals.mainForm.fastObjectListView1;
  244. }
  245. }
  246. [TestFixture]
  247. public class TestTreeListViewCheckBoxes : TestOlvCheckBoxes
  248. {
  249. [TestFixtureSetUp]
  250. new public void Init() {
  251. this.olv = MyGlobals.mainForm.treeListView1;
  252. }
  253. }
  254. }