ToolStripComboBoxTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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
  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. [Ignore ("Need DropDownHeight implemented in 2.0 ComboBox")]
  96. public void PropertyDropDownHeight ()
  97. {
  98. ToolStripComboBox tsi = new ToolStripComboBox ();
  99. EventWatcher ew = new EventWatcher (tsi);
  100. tsi.DropDownHeight = 42;
  101. Assert.AreEqual (42, tsi.DropDownHeight, "B1");
  102. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  103. ew.Clear ();
  104. tsi.DropDownHeight = 42;
  105. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  106. }
  107. [Test]
  108. public void PropertyDropDownStyle ()
  109. {
  110. ToolStripComboBox tsi = new ToolStripComboBox ();
  111. EventWatcher ew = new EventWatcher (tsi);
  112. tsi.DropDownStyle = ComboBoxStyle.Simple;
  113. Assert.AreEqual (ComboBoxStyle.Simple, tsi.DropDownStyle, "B1");
  114. Assert.AreEqual ("DropDownStyleChanged", ew.ToString (), "B2");
  115. ew.Clear ();
  116. tsi.DropDownStyle = ComboBoxStyle.Simple;
  117. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  118. }
  119. [Test]
  120. public void PropertyDropDownWidth ()
  121. {
  122. ToolStripComboBox tsi = new ToolStripComboBox ();
  123. EventWatcher ew = new EventWatcher (tsi);
  124. tsi.DropDownWidth = 42;
  125. Assert.AreEqual (42, tsi.DropDownWidth, "B1");
  126. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  127. ew.Clear ();
  128. tsi.DropDownWidth = 42;
  129. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  130. }
  131. [Test]
  132. public void PropertyDroppedDown ()
  133. {
  134. ToolStripComboBox tsi = new ToolStripComboBox ();
  135. EventWatcher ew = new EventWatcher (tsi);
  136. tsi.DroppedDown = true;
  137. Assert.AreEqual (true, tsi.DroppedDown, "B1");
  138. Assert.AreEqual ("DropDown", ew.ToString (), "B2");
  139. ew.Clear ();
  140. tsi.DroppedDown = true;
  141. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  142. }
  143. //[Test]
  144. //public void PropertyFlatStyle ()
  145. //{
  146. // ToolStripComboBox tsi = new ToolStripComboBox ();
  147. // EventWatcher ew = new EventWatcher (tsi);
  148. // tsi.FlatStyle = FlatStyle.System;
  149. // Assert.AreEqual (FlatStyle.System, tsi.FlatStyle, "B1");
  150. // Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  151. // ew.Clear ();
  152. // tsi.FlatStyle = FlatStyle.System;
  153. // Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  154. //}
  155. [Test]
  156. public void PropertyIntegralHeight ()
  157. {
  158. ToolStripComboBox tsi = new ToolStripComboBox ();
  159. EventWatcher ew = new EventWatcher (tsi);
  160. tsi.IntegralHeight = false;
  161. Assert.AreEqual (false, tsi.IntegralHeight, "B1");
  162. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  163. ew.Clear ();
  164. tsi.IntegralHeight = false;
  165. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  166. }
  167. [Test]
  168. public void PropertyMaxDropDownItems ()
  169. {
  170. ToolStripComboBox tsi = new ToolStripComboBox ();
  171. EventWatcher ew = new EventWatcher (tsi);
  172. tsi.MaxDropDownItems = 12;
  173. Assert.AreEqual (12, tsi.MaxDropDownItems, "B1");
  174. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  175. ew.Clear ();
  176. tsi.MaxDropDownItems = 12;
  177. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  178. }
  179. [Test]
  180. public void PropertyMaxLength ()
  181. {
  182. ToolStripComboBox tsi = new ToolStripComboBox ();
  183. EventWatcher ew = new EventWatcher (tsi);
  184. tsi.MaxLength = 42;
  185. Assert.AreEqual (42, tsi.MaxLength, "B1");
  186. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  187. ew.Clear ();
  188. tsi.MaxLength = 42;
  189. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  190. }
  191. [Test]
  192. public void PropertySelectedIndex ()
  193. {
  194. ToolStripComboBox tsi = new ToolStripComboBox ();
  195. EventWatcher ew = new EventWatcher (tsi);
  196. tsi.Items.Add ("A");
  197. tsi.Items.Add ("B");
  198. tsi.SelectedIndex = 1;
  199. Assert.AreEqual (1, tsi.SelectedIndex, "B1");
  200. Assert.AreEqual ("SelectedIndexChanged", ew.ToString (), "B2");
  201. ew.Clear ();
  202. tsi.SelectedIndex = 1;
  203. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  204. }
  205. [Test]
  206. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  207. public void PropertySelectedIndexAOORE ()
  208. {
  209. ToolStripComboBox tsi = new ToolStripComboBox ();
  210. tsi.SelectedIndex = 42;
  211. }
  212. [Test]
  213. public void PropertySelectedItem ()
  214. {
  215. ToolStripComboBox tsi = new ToolStripComboBox ();
  216. EventWatcher ew = new EventWatcher (tsi);
  217. tsi.Items.Add ("A");
  218. tsi.Items.Add ("B");
  219. tsi.SelectedItem = "B";
  220. Assert.AreEqual ("B", tsi.SelectedItem, "B1");
  221. Assert.AreEqual ("SelectedIndexChanged", ew.ToString (), "B2");
  222. ew.Clear ();
  223. tsi.SelectedItem = "B";
  224. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  225. }
  226. [Test]
  227. public void PropertySelectedItem2 ()
  228. {
  229. ToolStripComboBox tsi = new ToolStripComboBox ();
  230. EventWatcher ew = new EventWatcher (tsi);
  231. tsi.SelectedItem = "B";
  232. Assert.AreEqual (null, tsi.SelectedItem, "B1");
  233. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  234. ew.Clear ();
  235. tsi.SelectedItem = "B";
  236. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  237. }
  238. [Test]
  239. [Ignore ("Need TextUpdate event implemented in 2.0 ComboBox")]
  240. public void PropertySelectedText ()
  241. {
  242. ToolStripComboBox tsi = new ToolStripComboBox ();
  243. EventWatcher ew = new EventWatcher (tsi);
  244. tsi.SelectedText = "Hi";
  245. Assert.AreEqual (string.Empty, tsi.SelectedText, "B1");
  246. Assert.AreEqual ("TextUpdate", ew.ToString (), "B2");
  247. ew.Clear ();
  248. tsi.SelectedText = string.Empty;
  249. Assert.AreEqual ("TextUpdate", ew.ToString (), "B3");
  250. }
  251. [Test]
  252. public void PropertySelectionLength ()
  253. {
  254. ToolStripComboBox tsi = new ToolStripComboBox ();
  255. EventWatcher ew = new EventWatcher (tsi);
  256. tsi.SelectionLength = 42;
  257. Assert.AreEqual (0, tsi.SelectionLength, "B1");
  258. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  259. ew.Clear ();
  260. tsi.SelectionLength = 42;
  261. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  262. }
  263. [Test]
  264. public void PropertySelectionStart ()
  265. {
  266. ToolStripComboBox tsi = new ToolStripComboBox ();
  267. EventWatcher ew = new EventWatcher (tsi);
  268. tsi.SelectionStart = 42;
  269. Assert.AreEqual (0, tsi.SelectionStart, "B1");
  270. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  271. ew.Clear ();
  272. tsi.SelectionStart = 42;
  273. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  274. }
  275. [Test]
  276. public void PropertySorted ()
  277. {
  278. ToolStripComboBox tsi = new ToolStripComboBox ();
  279. EventWatcher ew = new EventWatcher (tsi);
  280. tsi.Sorted = true;
  281. Assert.AreEqual (true, tsi.Sorted, "B1");
  282. Assert.AreEqual (string.Empty, ew.ToString (), "B2");
  283. ew.Clear ();
  284. tsi.Sorted = true;
  285. Assert.AreEqual (string.Empty, ew.ToString (), "B3");
  286. }
  287. private class EventWatcher
  288. {
  289. private string events = string.Empty;
  290. public EventWatcher (ToolStripComboBox tsi)
  291. {
  292. tsi.DropDown += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDown;"); });
  293. tsi.DropDownClosed += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDownClosed;"); });
  294. tsi.DropDownStyleChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("DropDownStyleChanged;"); });
  295. tsi.SelectedIndexChanged += new EventHandler (delegate (Object obj, EventArgs e) { events += ("SelectedIndexChanged;"); });
  296. tsi.TextUpdate += new EventHandler (delegate (Object obj, EventArgs e) { events += ("TextUpdate;"); });
  297. }
  298. public override string ToString ()
  299. {
  300. return events.TrimEnd (';');
  301. }
  302. public void Clear ()
  303. {
  304. events = string.Empty;
  305. }
  306. }
  307. private class ExposeProtectedProperties : ToolStripComboBox
  308. {
  309. public ExposeProtectedProperties () : base () {}
  310. public new Padding DefaultMargin { get { return base.DefaultMargin; } }
  311. public new Size DefaultSize { get { return base.DefaultSize; } }
  312. }
  313. }
  314. }
  315. #endif