ToolStripComboBoxTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //
  2. // ToolStripComboBoxTests.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 Jonathan Pobst
  24. //
  25. // Authors:
  26. // Jonathan Pobst ([email protected])
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using NUnit.Framework;
  33. using System.Drawing;
  34. using System.Windows.Forms;
  35. namespace MonoTests.System.Windows.Forms
  36. {
  37. [TestFixture]
  38. public class ToolStripComboBoxTests : TestHelper
  39. {
  40. [Test]
  41. public void Constructor ()
  42. {
  43. ToolStripComboBox tsi = new ToolStripComboBox ();
  44. //Assert.AreEqual ("System.Windows.Forms.AutoCompleteStringCollection", tsi.AutoCompleteCustomSource.GetType ().ToString (), "A1");
  45. //Assert.AreEqual (AutoCompleteMode.None, tsi.AutoCompleteMode, "A2");
  46. //Assert.AreEqual (AutoCompleteSource.None, tsi.AutoCompleteSource, "A3");
  47. Assert.AreEqual ("System.Windows.Forms.ToolStripComboBox+ToolStripComboBoxControl", tsi.ComboBox.GetType ().ToString (), "A4");
  48. //Assert.AreEqual (106, tsi.DropDownHeight, "A5");
  49. Assert.AreEqual (ComboBoxStyle.DropDown, tsi.DropDownStyle, "A6");
  50. Assert.AreEqual (121, tsi.DropDownWidth, "A7");
  51. Assert.AreEqual (false, tsi.DroppedDown, "A8");
  52. Assert.AreEqual (FlatStyle.Popup, tsi.FlatStyle, "A9");
  53. Assert.AreEqual (true, tsi.IntegralHeight, "A10");
  54. Assert.AreEqual ("System.Windows.Forms.ComboBox+ObjectCollection", tsi.Items.ToString (), "A11");
  55. Assert.AreEqual (8, tsi.MaxDropDownItems, "A12");
  56. Assert.AreEqual (0, tsi.MaxLength, "A13");
  57. Assert.AreEqual (-1, tsi.SelectedIndex, "A14");
  58. Assert.AreEqual (null, tsi.SelectedItem, "A15");
  59. Assert.AreEqual (string.Empty, tsi.SelectedText, "A16");
  60. Assert.AreEqual (0, tsi.SelectionLength, "A17");
  61. Assert.AreEqual (0, tsi.SelectionStart, "A18");
  62. Assert.AreEqual (false, tsi.Sorted, "A19");
  63. tsi = new ToolStripComboBox ("Bob");
  64. Assert.AreEqual ("Bob", tsi.Name, "A20");
  65. Assert.AreEqual (string.Empty, tsi.Control.Name, "A21");
  66. }
  67. [Test]
  68. [ExpectedException (typeof (NotSupportedException))]
  69. public void ConstructorNSE ()
  70. {
  71. new ToolStripComboBox (new ComboBox ());
  72. }
  73. [Test]
  74. public void ProtectedProperties ()
  75. {
  76. ExposeProtectedProperties epp = new ExposeProtectedProperties ();
  77. Assert.AreEqual (new Padding (1, 0, 1, 0), epp.DefaultMargin, "C1");
  78. Assert.AreEqual (new Size (100, 22), epp.DefaultSize, "C2");
  79. }
  80. //[Test]
  81. //public void PropertyAutoCompleteCustomSource ()
  82. //{
  83. // ToolStripComboBox tsi = new ToolStripComboBox ();
  84. // EventWatcher ew = new EventWatcher (tsi);
  85. // AutoCompleteStringCollection acsc = new AutoCompleteStringCollection ();
  86. // acsc.AddRange (new string[] { "Apple", "Banana" });
  87. // tsi.AutoCompleteCustomSource = acsc;
  88. // Assert.AreSame (acsc, tsi.AutoCompleteCustomSource, "B1");
  89. // Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  90. // ew.Clear ();
  91. // tsi.AutoCompleteCustomSource = acsc;
  92. // Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  93. //}
  94. [Test]
  95. public void PropertyDropDownHeight ()
  96. {
  97. ToolStripComboBox tsi = new ToolStripComboBox ();
  98. EventWatcher ew = new EventWatcher (tsi);
  99. tsi.DropDownHeight = 42;
  100. Assert.AreEqual (42, tsi.DropDownHeight, "B1");
  101. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  102. ew.Clear ();
  103. tsi.DropDownHeight = 42;
  104. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  105. }
  106. [Test]
  107. public void PropertyDropDownStyle ()
  108. {
  109. ToolStripComboBox tsi = new ToolStripComboBox ();
  110. EventWatcher ew = new EventWatcher (tsi);
  111. tsi.DropDownStyle = ComboBoxStyle.Simple;
  112. Assert.AreEqual (ComboBoxStyle.Simple, tsi.DropDownStyle, "B1");
  113. Assert.AreEqual ("DropDownStyleChanged", ew.ToString (), "B2");
  114. ew.Clear ();
  115. tsi.DropDownStyle = ComboBoxStyle.Simple;
  116. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  117. }
  118. [Test]
  119. public void PropertyDropDownWidth ()
  120. {
  121. ToolStripComboBox tsi = new ToolStripComboBox ();
  122. EventWatcher ew = new EventWatcher (tsi);
  123. tsi.DropDownWidth = 42;
  124. Assert.AreEqual (42, tsi.DropDownWidth, "B1");
  125. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  126. ew.Clear ();
  127. tsi.DropDownWidth = 42;
  128. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  129. }
  130. [Test]
  131. public void PropertyDroppedDown ()
  132. {
  133. ToolStripComboBox tsi = new ToolStripComboBox ();
  134. EventWatcher ew = new EventWatcher (tsi);
  135. tsi.DroppedDown = true;
  136. Assert.AreEqual (true, tsi.DroppedDown, "B1");
  137. Assert.AreEqual ("DropDown", ew.ToString (), "B2");
  138. ew.Clear ();
  139. tsi.DroppedDown = true;
  140. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  141. }
  142. [Test]
  143. public void PropertyFlatStyle ()
  144. {
  145. ToolStripComboBox tsi = new ToolStripComboBox ();
  146. EventWatcher ew = new EventWatcher (tsi);
  147. tsi.FlatStyle = FlatStyle.System;
  148. Assert.AreEqual (FlatStyle.System, tsi.FlatStyle, "B1");
  149. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  150. ew.Clear ();
  151. tsi.FlatStyle = FlatStyle.System;
  152. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  153. }
  154. [Test]
  155. public void PropertyIntegralHeight ()
  156. {
  157. ToolStripComboBox tsi = new ToolStripComboBox ();
  158. EventWatcher ew = new EventWatcher (tsi);
  159. tsi.IntegralHeight = false;
  160. Assert.AreEqual (false, tsi.IntegralHeight, "B1");
  161. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  162. ew.Clear ();
  163. tsi.IntegralHeight = false;
  164. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  165. }
  166. [Test]
  167. public void PropertyMaxDropDownItems ()
  168. {
  169. ToolStripComboBox tsi = new ToolStripComboBox ();
  170. EventWatcher ew = new EventWatcher (tsi);
  171. tsi.MaxDropDownItems = 12;
  172. Assert.AreEqual (12, tsi.MaxDropDownItems, "B1");
  173. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  174. ew.Clear ();
  175. tsi.MaxDropDownItems = 12;
  176. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  177. }
  178. [Test]
  179. public void PropertyMaxLength ()
  180. {
  181. ToolStripComboBox tsi = new ToolStripComboBox ();
  182. EventWatcher ew = new EventWatcher (tsi);
  183. tsi.MaxLength = 42;
  184. Assert.AreEqual (42, tsi.MaxLength, "B1");
  185. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  186. ew.Clear ();
  187. tsi.MaxLength = 42;
  188. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  189. }
  190. [Test]
  191. public void PropertySelectedIndex ()
  192. {
  193. ToolStripComboBox tsi = new ToolStripComboBox ();
  194. EventWatcher ew = new EventWatcher (tsi);
  195. tsi.Items.Add ("A");
  196. tsi.Items.Add ("B");
  197. tsi.SelectedIndex = 1;
  198. Assert.AreEqual (1, tsi.SelectedIndex, "B1");
  199. Assert.AreEqual ("SelectedIndexChanged", ew.ToString (), "B2");
  200. ew.Clear ();
  201. tsi.SelectedIndex = 1;
  202. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  203. }
  204. [Test]
  205. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  206. public void PropertySelectedIndexAOORE ()
  207. {
  208. ToolStripComboBox tsi = new ToolStripComboBox ();
  209. tsi.SelectedIndex = 42;
  210. }
  211. [Test]
  212. public void PropertySelectedItem ()
  213. {
  214. ToolStripComboBox tsi = new ToolStripComboBox ();
  215. EventWatcher ew = new EventWatcher (tsi);
  216. tsi.Items.Add ("A");
  217. tsi.Items.Add ("B");
  218. tsi.SelectedItem = "B";
  219. Assert.AreEqual ("B", tsi.SelectedItem, "B1");
  220. Assert.AreEqual ("SelectedIndexChanged", ew.ToString (), "B2");
  221. ew.Clear ();
  222. tsi.SelectedItem = "B";
  223. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  224. }
  225. [Test]
  226. public void PropertySelectedItem2 ()
  227. {
  228. ToolStripComboBox tsi = new ToolStripComboBox ();
  229. EventWatcher ew = new EventWatcher (tsi);
  230. tsi.SelectedItem = "B";
  231. Assert.AreEqual (null, tsi.SelectedItem, "B1");
  232. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  233. ew.Clear ();
  234. tsi.SelectedItem = "B";
  235. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  236. }
  237. [Test]
  238. [Ignore ("Need TextUpdate event implemented in 2.0 ComboBox")]
  239. public void PropertySelectedText ()
  240. {
  241. ToolStripComboBox tsi = new ToolStripComboBox ();
  242. EventWatcher ew = new EventWatcher (tsi);
  243. tsi.SelectedText = "Hi";
  244. Assert.AreEqual (string.Empty, tsi.SelectedText, "B1");
  245. Assert.AreEqual ("TextUpdate", ew.ToString (), "B2");
  246. ew.Clear ();
  247. tsi.SelectedText = string.Empty;
  248. Assert.AreEqual ("TextUpdate", ew.ToString (), "B3");
  249. }
  250. [Test]
  251. public void PropertySelectionLength ()
  252. {
  253. ToolStripComboBox tsi = new ToolStripComboBox ();
  254. EventWatcher ew = new EventWatcher (tsi);
  255. tsi.SelectionLength = 42;
  256. Assert.AreEqual (0, tsi.SelectionLength, "B1");
  257. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  258. ew.Clear ();
  259. tsi.SelectionLength = 42;
  260. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  261. }
  262. [Test]
  263. public void PropertySelectionStart ()
  264. {
  265. ToolStripComboBox tsi = new ToolStripComboBox ();
  266. EventWatcher ew = new EventWatcher (tsi);
  267. tsi.SelectionStart = 42;
  268. Assert.AreEqual (0, tsi.SelectionStart, "B1");
  269. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  270. ew.Clear ();
  271. tsi.SelectionStart = 42;
  272. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  273. }
  274. [Test]
  275. public void PropertySorted ()
  276. {
  277. ToolStripComboBox tsi = new ToolStripComboBox ();
  278. EventWatcher ew = new EventWatcher (tsi);
  279. tsi.Sorted = true;
  280. Assert.AreEqual (true, tsi.Sorted, "B1");
  281. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  282. ew.Clear ();
  283. tsi.Sorted = true;
  284. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  285. }
  286. private class EventWatcher
  287. {
  288. private string events = string.Empty;
  289. public EventWatcher (ToolStripComboBox tsi)
  290. {
  291. tsi.DropDown += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDown;"); });
  292. tsi.DropDownClosed += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDownClosed;"); });
  293. tsi.DropDownStyleChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDownStyleChanged;"); });
  294. tsi.SelectedIndexChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("SelectedIndexChanged;"); });
  295. tsi.TextUpdate += new EventHandler (delegate (Object obj, EventArgs e) { events += ("TextUpdate;"); });
  296. }
  297. public override string ToString ()
  298. {
  299. return events.TrimEnd (';');
  300. }
  301. public void Clear ()
  302. {
  303. events = string.Empty;
  304. }
  305. }
  306. private class ExposeProtectedProperties : ToolStripComboBox
  307. {
  308. public ExposeProtectedProperties () : base () {}
  309. public new Padding DefaultMargin { get { return base.DefaultMargin; } }
  310. public new Size DefaultSize { get { return base.DefaultSize; } }
  311. }
  312. }
  313. }
  314. #endif