BindingNavigatorTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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) 2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Olivier Dufour [email protected]
  24. // Alan McGovern [email protected]
  25. //
  26. #if NET_2_0
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using NUnit.Framework;
  31. using System.Windows.Forms;
  32. using System.ComponentModel;
  33. namespace MonoTests.System.Windows.Forms
  34. {
  35. [TestFixture]
  36. public class BindingNavigatorTest
  37. {
  38. private bool flag = false;
  39. private BindingNavigator navigator;
  40. private void SetFlag(object a, EventArgs e)
  41. {
  42. flag = true;
  43. }
  44. [SetUp]
  45. public void Setup()
  46. {
  47. IntThing test = new IntThing(50);
  48. BindingSource s = new BindingSource();
  49. s.DataSource = test;
  50. navigator = new BindingNavigator(s);
  51. flag = false;
  52. }
  53. [Test]
  54. public void AddNewItemTest()
  55. {
  56. navigator.ItemAdded += new ToolStripItemEventHandler(SetFlag);
  57. navigator.Items.Add("Test Item");
  58. Assert.IsTrue(flag, "#1");
  59. }
  60. [Test]
  61. public void AddStandardItems()
  62. {
  63. BindingNavigator navigator = new BindingNavigator();
  64. navigator.AddStandardItems();
  65. CheckStandardItems(navigator);
  66. }
  67. [Test]
  68. public void BeginInitTest()
  69. {
  70. navigator.RefreshItems += new EventHandler(SetFlag);
  71. navigator.ItemAdded += new ToolStripItemEventHandler(SetFlag);
  72. navigator.Paint += new PaintEventHandler(SetFlag);
  73. navigator.BeginInit();
  74. navigator.Invalidate();
  75. navigator.AddNewItem = new ToolStripButton();
  76. navigator.Refresh();
  77. Assert.IsFalse(flag, "#1");
  78. navigator.EndInit();
  79. Assert.IsTrue(flag, "#2");
  80. }
  81. [Test]
  82. public void BindingSourceTest()
  83. {
  84. navigator.BindingSource.PositionChanged += new EventHandler(SetFlag);
  85. navigator.BindingSource.Position = 5;
  86. Assert.IsTrue(flag, "#1");
  87. Assert.AreEqual("6", navigator.PositionItem.Text, "#2");
  88. }
  89. [Test]
  90. public void Constructor()
  91. {
  92. BindingNavigator navigator = new BindingNavigator(true);
  93. Assert.AreEqual(11, navigator.Items.Count, "count");
  94. CheckStandardItems(navigator);
  95. navigator = new BindingNavigator(false);
  96. Assert.IsTrue(navigator.Items.Count == 0, "#01");
  97. IntThing test = new IntThing(50);
  98. BindingSource s = new BindingSource();
  99. s.DataSource = test;
  100. }
  101. private void CheckStandardItems(BindingNavigator navigator)
  102. {
  103. Assert.IsNotNull(navigator.AddNewItem, "*1");
  104. Assert.IsNotNull(navigator.MoveFirstItem, "*2");
  105. Assert.IsNotNull(navigator.MoveLastItem, "*3");
  106. Assert.IsNotNull(navigator.MoveNextItem, "*4");
  107. Assert.IsNotNull(navigator.MovePreviousItem, "*5");
  108. Assert.IsNotNull(navigator.DeleteItem, "*6");
  109. Assert.IsNotNull(navigator.CountItem, "*7");
  110. Assert.IsNotNull(navigator.PositionItem, "*8");
  111. Assert.IsNotNull(navigator.AddNewItem, "*9");
  112. Assert.IsNotNull(navigator.AddNewItem, "*10");
  113. Assert.IsNull(navigator.BindingSource, "*11");
  114. Assert.IsTrue(navigator.AddNewItem is ToolStripButton, "#1");
  115. Assert.IsTrue(navigator.MoveFirstItem is ToolStripButton, "#2");
  116. Assert.IsTrue(navigator.MoveLastItem is ToolStripButton, "#3");
  117. Assert.IsTrue(navigator.MoveNextItem is ToolStripButton, "#4");
  118. Assert.IsTrue(navigator.MovePreviousItem is ToolStripButton, "#5");
  119. Assert.IsTrue(navigator.DeleteItem is ToolStripButton, "#6");
  120. Assert.IsTrue(navigator.CountItem is ToolStripLabel, "#7");
  121. Assert.IsTrue(navigator.PositionItem is ToolStripTextBox, "#8");
  122. Assert.IsTrue(navigator.AddNewItem is ToolStripButton, "#9");
  123. Assert.IsTrue(navigator.AddNewItem is ToolStripButton, "#10");
  124. Assert.AreEqual("of {0}", navigator.CountItemFormat, "#11");
  125. }
  126. [Test]
  127. public void OnRefreshItems()
  128. {
  129. navigator.RefreshItems += new EventHandler(SetFlag);
  130. navigator.AddNewItem = new ToolStripButton();
  131. Assert.IsTrue(flag, "#1");
  132. }
  133. [Test]
  134. [Ignore("Not working yet")]
  135. public void PositionItemTest()
  136. {
  137. navigator.BindingSource.PositionChanged += new EventHandler(SetFlag);
  138. int position = int.Parse(navigator.PositionItem.Text);
  139. navigator.PositionItem.Text = "aaa";
  140. Assert.IsFalse(flag, "#1");
  141. RefreshNav();
  142. Assert.AreEqual(position.ToString(), navigator.PositionItem.Text, "#2");
  143. navigator.PositionItem.Text = "-1";
  144. RefreshNav();
  145. Assert.IsFalse(flag, "#3");
  146. flag = false;
  147. Assert.AreEqual("1", navigator.PositionItem.Text, "#4");
  148. navigator.PositionItem.Text = "7";
  149. RefreshNav();
  150. Assert.IsFalse(flag, "#5");
  151. Assert.AreEqual("1", navigator.PositionItem.Text, "#6");
  152. Assert.AreEqual(0, navigator.BindingSource.Position, "#7");
  153. }
  154. [Test]
  155. public void RefreshItemsCore()
  156. {
  157. navigator.RefreshItems += new EventHandler(SetFlag);
  158. navigator.AddNewItem = new ToolStripButton();
  159. Assert.IsTrue(flag, "#1");
  160. }
  161. private void RefreshNav()
  162. {
  163. navigator.BeginInit();
  164. navigator.EndInit();
  165. }
  166. [Test]
  167. [Ignore("Not working")]
  168. public void RemoveItemTest()
  169. {
  170. navigator.BindingSource.Position = 5;
  171. navigator.BindingSource.ListChanged += new ListChangedEventHandler(SetFlag);
  172. navigator.BindingSource.Remove(5);
  173. Assert.IsFalse(navigator.BindingSource.Contains(5), "#1");
  174. Assert.AreEqual("6", navigator.PositionItem.Text, "#2");
  175. Assert.AreEqual(6, (navigator.BindingSource.Current), "#3");
  176. }
  177. private class IntThing : BindingList<int>
  178. {
  179. int Number;
  180. public IntThing(int number)
  181. : base()
  182. {
  183. for (int i = 0; i < number; i++)
  184. this.Add(i);
  185. }
  186. protected override bool SupportsSearchingCore
  187. {
  188. get { return true; }
  189. }
  190. protected override int FindCore(PropertyDescriptor prop, object key)
  191. {
  192. return this.Items.IndexOf((int)key);
  193. return -1;
  194. }
  195. }
  196. }
  197. }
  198. #endif