ComboBoxTest.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. //
  2. // ComboBoxTest.cs: Test cases for ComboBox.
  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) 2005 Novell, Inc. (http://www.novell.com)
  24. //
  25. // Authors:
  26. // Ritvik Mayank <[email protected]>
  27. // Jordi Mas i Hernandez <[email protected]>
  28. using System;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Drawing;
  32. using System.Globalization;
  33. using System.Reflection;
  34. using System.Threading;
  35. using System.Windows.Forms;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.Windows.Forms
  38. {
  39. [TestFixture]
  40. public class ComboBoxTest
  41. {
  42. private CultureInfo _originalCulture;
  43. [SetUp]
  44. public void SetUp ()
  45. {
  46. _originalCulture = Thread.CurrentThread.CurrentCulture;
  47. }
  48. [TearDown]
  49. public void TearDown ()
  50. {
  51. Thread.CurrentThread.CurrentCulture = _originalCulture;
  52. }
  53. [Test]
  54. public void ComboBoxPropertyTest ()
  55. {
  56. ComboBox mycmbbox = new ComboBox ();
  57. Assert.AreEqual (DrawMode.Normal, mycmbbox.DrawMode, "#1");
  58. Assert.AreEqual (ComboBoxStyle.DropDown, mycmbbox.DropDownStyle, "#2");
  59. Assert.AreEqual (false, mycmbbox.DroppedDown, "#4");
  60. Assert.AreEqual (true, mycmbbox.IntegralHeight, "#5");
  61. Assert.AreEqual (0, mycmbbox.Items.Count, "#6");
  62. //Assert.AreEqual (15, mycmbbox.ItemHeight, "#7"); // Note: Item height depends on the current font.
  63. Assert.AreEqual (8, mycmbbox.MaxDropDownItems, "#8");
  64. Assert.AreEqual (0, mycmbbox.MaxLength, "#9");
  65. //Assert.AreEqual (20, mycmbbox.PreferredHeight, "#10");
  66. // Note: Item height depends on the current font.
  67. Assert.AreEqual (-1, mycmbbox.SelectedIndex, "#11");
  68. Assert.AreEqual (null, mycmbbox.SelectedItem, "#12");
  69. Assert.AreEqual ("", mycmbbox.SelectedText, "#13");
  70. Assert.AreEqual (0, mycmbbox.SelectionLength, "#14");
  71. Assert.AreEqual (0, mycmbbox.SelectionStart, "#15");
  72. Assert.AreEqual (false, mycmbbox.Sorted, "#16");
  73. Assert.AreEqual ("", mycmbbox.Text, "#17");
  74. #if NET_2_0
  75. Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#18");
  76. Assert.AreEqual (AutoCompleteMode.None, mycmbbox.AutoCompleteMode, "#19");
  77. Assert.AreEqual (AutoCompleteSource.None, mycmbbox.AutoCompleteSource, "#20");
  78. mycmbbox.AutoCompleteCustomSource = null;
  79. Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#21");
  80. Assert.AreEqual (ImageLayout.Tile, mycmbbox.BackgroundImageLayout, "#22");
  81. Assert.AreEqual (null, mycmbbox.DataSource, "#23");
  82. Assert.AreEqual (106, mycmbbox.DropDownHeight, "#24");
  83. Assert.AreEqual (FlatStyle.Standard, mycmbbox.FlatStyle, "#25");
  84. Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MaximumSize.ToString (), "#26");
  85. Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MinimumSize.ToString (), "#27");
  86. Assert.AreEqual ("{Left=0,Top=0,Right=0,Bottom=0}", mycmbbox.Padding.ToString (), "#28");
  87. #endif
  88. }
  89. #if NET_2_0
  90. [Test]
  91. public void ResetTextTest ()
  92. {
  93. ComboBox cmbbox = new ComboBox ();
  94. Assert.AreEqual ("", cmbbox.Text, "#01");
  95. cmbbox.Text = "abc";
  96. Assert.AreEqual ("abc", cmbbox.Text, "#02");
  97. cmbbox.ResetText ();
  98. Assert.AreEqual ("", cmbbox.Text, "#03");
  99. }
  100. [Test]
  101. public void BackgroundImageLayoutTest ()
  102. {
  103. ComboBox cmbbox = new ComboBox ();
  104. cmbbox.BackgroundImageLayout = ImageLayout.Stretch;
  105. Assert.AreEqual (ImageLayout.Stretch, cmbbox.BackgroundImageLayout, "#01");
  106. }
  107. [Test]
  108. public void DropDownHeightTest ()
  109. {
  110. ComboBox cmbbox = new ComboBox ();
  111. cmbbox.DropDownHeight = 225;
  112. Assert.AreEqual (225, cmbbox.DropDownHeight, "#01");
  113. cmbbox.DropDownHeight = 1;
  114. Assert.AreEqual (1, cmbbox.DropDownHeight, "#02");
  115. }
  116. [Test]
  117. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  118. public void DropDownHeightExceptionTest1 ()
  119. {
  120. ComboBox cmbbox = new ComboBox ();
  121. cmbbox.DropDownHeight = -225;
  122. }
  123. [Test]
  124. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  125. public void DropDownHeightExceptionTest2 ()
  126. {
  127. ComboBox cmbbox = new ComboBox ();
  128. cmbbox.DropDownHeight = 0;
  129. }
  130. [Test]
  131. public void FlatStyleTest ()
  132. {
  133. ComboBox cmbbox = new ComboBox ();
  134. cmbbox.FlatStyle = FlatStyle.Popup;
  135. Assert.AreEqual (FlatStyle.Popup, cmbbox.FlatStyle, "#01");
  136. }
  137. [Test]
  138. [ExpectedException (typeof (InvalidEnumArgumentException))]
  139. public void FlatStyleExceptionTest ()
  140. {
  141. ComboBox cmbbox = new ComboBox ();
  142. cmbbox.FlatStyle = (FlatStyle) (-123);
  143. }
  144. [Test]
  145. public void MaximumSizeTest ()
  146. {
  147. ComboBox cmbbox = new ComboBox ();
  148. cmbbox.MaximumSize = new Size (25, 25);
  149. Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MaximumSize.ToString (), "#01");
  150. cmbbox.MaximumSize = new Size (50, 75);
  151. Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MaximumSize.ToString (), "#02");
  152. }
  153. [Test]
  154. public void MinumumSizeTest ()
  155. {
  156. ComboBox cmbbox = new ComboBox ();
  157. cmbbox.MinimumSize = new Size (25, 25);
  158. Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MinimumSize.ToString (), "#1");
  159. cmbbox.MinimumSize = new Size (50, 75);
  160. Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MinimumSize.ToString (), "#2");
  161. }
  162. [Test]
  163. public void PaddingTest ()
  164. {
  165. ComboBox cmbbox = new ComboBox ();
  166. cmbbox.Padding = new Padding (21);
  167. Assert.AreEqual ("{Left=21,Top=21,Right=21,Bottom=21}", cmbbox.Padding.ToString (), "#01");
  168. }
  169. #endif
  170. [Test]
  171. public void BeginEndUpdateTest ()
  172. {
  173. Form myform = new Form ();
  174. myform.ShowInTaskbar = false;
  175. myform.Visible = true;
  176. ComboBox cmbbox = new ComboBox ();
  177. cmbbox.Items.Add ("A");
  178. cmbbox.Visible = true;
  179. myform.Controls.Add (cmbbox);
  180. cmbbox.BeginUpdate ();
  181. for (int x = 1 ; x < 5000 ; x++) {
  182. cmbbox.Items.Add ("Item " + x.ToString ());
  183. }
  184. cmbbox.EndUpdate ();
  185. myform.Dispose ();
  186. }
  187. [Test]
  188. public void FindString ()
  189. {
  190. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  191. ComboBox cmbbox = new ComboBox ();
  192. Assert.AreEqual (-1, cmbbox.FindString ("Hello"), "#A1");
  193. Assert.AreEqual (-1, cmbbox.FindString (string.Empty), "#A2");
  194. Assert.AreEqual (-1, cmbbox.FindString (null), "#A3");
  195. Assert.AreEqual (-1, cmbbox.FindString ("Hola", -5), "#A4");
  196. Assert.AreEqual (-1, cmbbox.FindString ("Hola", 40), "#A5");
  197. cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
  198. Assert.AreEqual (2, cmbbox.FindString ("I"), "#B1");
  199. Assert.AreEqual (0, cmbbox.FindString ("in"), "#B2");
  200. Assert.AreEqual (1, cmbbox.FindString ("BAD"), "#B3");
  201. Assert.AreEqual (1, cmbbox.FindString ("Bad"), "#B4");
  202. Assert.AreEqual (1, cmbbox.FindString ("b"), "#B5");
  203. Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#B6");
  204. Assert.AreEqual (-1, cmbbox.FindString (null), "#B7");
  205. Assert.AreEqual (3, cmbbox.FindString ("b", 2), "#C1");
  206. Assert.AreEqual (5, cmbbox.FindString ("I", 3), "#C2");
  207. Assert.AreEqual (4, cmbbox.FindString ("B", 3), "#C3");
  208. Assert.AreEqual (1, cmbbox.FindString ("B", 4), "#C4");
  209. Assert.AreEqual (5, cmbbox.FindString ("I", 4), "#C5");
  210. Assert.AreEqual (4, cmbbox.FindString ("BA", 3), "#C6");
  211. Assert.AreEqual (0, cmbbox.FindString ("i", -1), "#C7");
  212. Assert.AreEqual (3, cmbbox.FindString (string.Empty, 2), "#C8");
  213. Assert.AreEqual (-1, cmbbox.FindString (null, 1), "#C9");
  214. cmbbox.Items.Add (string.Empty);
  215. Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#D1");
  216. Assert.AreEqual (-1, cmbbox.FindString (null), "#D2");
  217. Assert.AreEqual (4, cmbbox.FindString (string.Empty, 3), "#E1");
  218. Assert.AreEqual (-1, cmbbox.FindString (null, 99), "#E2");
  219. Assert.AreEqual (-1, cmbbox.FindString (null, -5), "#E3");
  220. }
  221. [Test]
  222. public void FindString_StartIndex_ItemCount ()
  223. {
  224. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  225. ComboBox cmbbox = new ComboBox ();
  226. cmbbox.Items.AddRange (new object [] { "BA", "BB" });
  227. #if NET_2_0
  228. Assert.AreEqual (0, cmbbox.FindString ("b", 1));
  229. #else
  230. try {
  231. cmbbox.FindString ("b", 1);
  232. Assert.Fail ("#1");
  233. } catch (ArgumentOutOfRangeException ex) {
  234. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  235. Assert.IsNull (ex.InnerException, "#3");
  236. Assert.IsNotNull (ex.Message, "#4");
  237. Assert.IsNotNull (ex.ParamName, "#5");
  238. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  239. }
  240. #endif
  241. }
  242. [Test]
  243. public void FindString_StartIndex_Min ()
  244. {
  245. ComboBox cmbbox = new ComboBox ();
  246. cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
  247. try {
  248. cmbbox.FindString ("Hola", -2);
  249. Assert.Fail ("#1");
  250. } catch (ArgumentOutOfRangeException ex) {
  251. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  252. Assert.IsNull (ex.InnerException, "#3");
  253. Assert.IsNotNull (ex.Message, "#4");
  254. Assert.IsNotNull (ex.ParamName, "#5");
  255. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  256. }
  257. }
  258. [Test]
  259. public void FindString_StartIndex_Max ()
  260. {
  261. ComboBox cmbbox = new ComboBox ();
  262. cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
  263. try {
  264. cmbbox.FindString ("Hola", 4);
  265. Assert.Fail ("#1");
  266. } catch (ArgumentOutOfRangeException ex) {
  267. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  268. Assert.IsNull (ex.InnerException, "#3");
  269. Assert.IsNotNull (ex.Message, "#4");
  270. Assert.IsNotNull (ex.ParamName, "#5");
  271. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  272. }
  273. }
  274. [Test]
  275. public void FindStringExact ()
  276. {
  277. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  278. ComboBox cmbbox = new ComboBox ();
  279. Assert.AreEqual (-1, cmbbox.FindStringExact ("Hello"), "#A1");
  280. Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#A2");
  281. Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#A3");
  282. Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", -5), "#A4");
  283. Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", 40), "#A5");
  284. cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
  285. Assert.AreEqual (2, cmbbox.FindStringExact ("IN"), "#B1");
  286. Assert.AreEqual (0, cmbbox.FindStringExact ("in"), "#B2");
  287. Assert.AreEqual (3, cmbbox.FindStringExact ("BAD"), "#B3");
  288. Assert.AreEqual (3, cmbbox.FindStringExact ("bad"), "#B4");
  289. Assert.AreEqual (-1, cmbbox.FindStringExact ("B"), "#B5");
  290. Assert.AreEqual (-1, cmbbox.FindStringExact ("NonExistant"), "#B6");
  291. Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#B7");
  292. Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#B8");
  293. Assert.AreEqual (2, cmbbox.FindStringExact ("In", 1), "#C1");
  294. Assert.AreEqual (5, cmbbox.FindStringExact ("In", 2), "#C2");
  295. Assert.AreEqual (4, cmbbox.FindStringExact ("BaD", 3), "#C3");
  296. Assert.AreEqual (3, cmbbox.FindStringExact ("bad", -1), "#C4");
  297. Assert.AreEqual (5, cmbbox.FindStringExact ("In", 4), "#C5");
  298. Assert.AreEqual (3, cmbbox.FindStringExact ("bad", 4), "#C6");
  299. Assert.AreEqual (-1, cmbbox.FindStringExact ("B", 4), "#C7");
  300. Assert.AreEqual (-1, cmbbox.FindStringExact ("BADNOT", 0), "#C8");
  301. Assert.AreEqual (-1, cmbbox.FindStringExact ("i", -1), "#C9");
  302. Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty, 2), "#C10");
  303. Assert.AreEqual (-1, cmbbox.FindStringExact (null, 1), "#C11");
  304. cmbbox.Items.Add (string.Empty);
  305. Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty), "#D1");
  306. Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#D2");
  307. Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty, 3), "#E1");
  308. Assert.AreEqual (-1, cmbbox.FindStringExact (null, 99), "#E2");
  309. Assert.AreEqual (-1, cmbbox.FindStringExact (null, -5), "#E3");
  310. }
  311. [Test]
  312. public void FindStringExact_StartIndex_ItemCount ()
  313. {
  314. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  315. ComboBox cmbbox = new ComboBox ();
  316. cmbbox.Items.AddRange (new object [] { "AB", "BA", "AB", "BA" });
  317. #if NET_2_0
  318. Assert.AreEqual (1, cmbbox.FindStringExact ("BA", 3));
  319. #else
  320. try {
  321. cmbbox.FindString ("BA", 3);
  322. Assert.Fail ("#1");
  323. } catch (ArgumentOutOfRangeException ex) {
  324. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  325. Assert.IsNull (ex.InnerException, "#3");
  326. Assert.IsNotNull (ex.Message, "#4");
  327. Assert.IsNotNull (ex.ParamName, "#5");
  328. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  329. }
  330. #endif
  331. }
  332. [Test]
  333. public void FindStringExact_StartIndex_Min ()
  334. {
  335. ComboBox cmbbox = new ComboBox ();
  336. cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
  337. try {
  338. cmbbox.FindStringExact ("Hola", -2);
  339. Assert.Fail ("#1");
  340. } catch (ArgumentOutOfRangeException ex) {
  341. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  342. Assert.IsNull (ex.InnerException, "#3");
  343. Assert.IsNotNull (ex.Message, "#4");
  344. Assert.IsNotNull (ex.ParamName, "#5");
  345. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  346. }
  347. }
  348. [Test]
  349. public void FindStringExact_StartIndex_Max ()
  350. {
  351. ComboBox cmbbox = new ComboBox ();
  352. cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
  353. try {
  354. cmbbox.FindStringExact ("Hola", 4);
  355. Assert.Fail ("#1");
  356. } catch (ArgumentOutOfRangeException ex) {
  357. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  358. Assert.IsNull (ex.InnerException, "#3");
  359. Assert.IsNotNull (ex.Message, "#4");
  360. Assert.IsNotNull (ex.ParamName, "#5");
  361. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  362. }
  363. }
  364. [Test]
  365. public void GetItemHeightTest ()
  366. {
  367. ComboBox cmbbox = new ComboBox ();
  368. cmbbox.Items.Add ("ABC");
  369. cmbbox.Items.Add ("BCD");
  370. cmbbox.Items.Add ("DEF");
  371. int x = -1;
  372. x = cmbbox.GetItemHeight (x);
  373. Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
  374. }
  375. //
  376. // Exceptions
  377. //
  378. [Test]
  379. [ExpectedException (typeof (InvalidEnumArgumentException))]
  380. public void DropDownStyleException ()
  381. {
  382. ComboBox cmbbox = new ComboBox ();
  383. cmbbox.DropDownStyle = (ComboBoxStyle) 10;
  384. }
  385. [Test]
  386. [ExpectedException (typeof (InvalidEnumArgumentException))]
  387. public void DrawModeException ()
  388. {
  389. ComboBox cmbbox = new ComboBox ();
  390. cmbbox.DrawMode = (DrawMode) 10;
  391. }
  392. [Test]
  393. public void DropDownWidth ()
  394. {
  395. ComboBox cmbbox = new ComboBox ();
  396. Assert.AreEqual (121, cmbbox.DropDownWidth, "#A1");
  397. cmbbox.DropDownWidth = 1;
  398. Assert.AreEqual (1, cmbbox.DropDownWidth, "#A2");
  399. try {
  400. cmbbox.DropDownWidth = 0;
  401. Assert.Fail ("#B1");
  402. #if NET_2_0
  403. } catch (ArgumentOutOfRangeException ex) {
  404. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  405. Assert.IsNotNull (ex.Message, "#B3");
  406. Assert.IsNotNull (ex.ParamName, "#B4");
  407. Assert.AreEqual ("DropDownWidth", ex.ParamName, "#B5");
  408. Assert.IsNull (ex.InnerException, "#B6");
  409. }
  410. #else
  411. } catch (ArgumentException ex) {
  412. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  413. Assert.IsNotNull (ex.Message, "#B3");
  414. Assert.IsNull (ex.ParamName, "#B4");
  415. Assert.IsNull (ex.InnerException, "#B5");
  416. }
  417. #endif
  418. }
  419. [Test]
  420. public void ItemHeight ()
  421. {
  422. ComboBox cmbbox = new ComboBox ();
  423. Assert.IsTrue (cmbbox.ItemHeight >= 1, "#A1");
  424. cmbbox.ItemHeight = 1;
  425. Assert.AreEqual (1, cmbbox.ItemHeight, "#A2");
  426. try {
  427. cmbbox.ItemHeight = 0;
  428. Assert.Fail ("#B1");
  429. #if NET_2_0
  430. } catch (ArgumentOutOfRangeException ex) {
  431. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  432. Assert.IsNotNull (ex.Message, "#B3");
  433. Assert.IsNotNull (ex.ParamName, "#B4");
  434. Assert.AreEqual ("ItemHeight", ex.ParamName, "#B5");
  435. Assert.IsNull (ex.InnerException, "#B6");
  436. }
  437. #else
  438. } catch (ArgumentException ex) {
  439. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  440. Assert.IsNotNull (ex.Message, "#B3");
  441. Assert.IsNull (ex.ParamName, "#B4");
  442. Assert.IsNull (ex.InnerException, "#B5");
  443. }
  444. #endif
  445. }
  446. [Test]
  447. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  448. public void SelectedIndexException ()
  449. {
  450. ComboBox cmbbox = new ComboBox ();
  451. cmbbox.SelectedIndex = -2;
  452. }
  453. //
  454. // Events
  455. //
  456. private bool eventFired;
  457. private DrawItemEventArgs drawItemsArgs;
  458. private void DrawItemEventH (object sender, DrawItemEventArgs e)
  459. {
  460. eventFired = true;
  461. drawItemsArgs = e;
  462. }
  463. private void GenericHandler (object sender, EventArgs e)
  464. {
  465. eventFired = true;
  466. }
  467. [Ignore ("Bugs in X11 prevent this test to run properly")]
  468. public void DrawItemEventTest ()
  469. {
  470. eventFired = false;
  471. drawItemsArgs = null;
  472. Form myform = new Form ();
  473. myform.ShowInTaskbar = false;
  474. ComboBox cmbbox = new ComboBox ();
  475. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  476. cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
  477. cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
  478. myform.Controls.Add (cmbbox);
  479. cmbbox.Items.AddRange(new object[] {"Item1"});
  480. myform.Visible = true;
  481. cmbbox.Visible = true;
  482. cmbbox.Refresh ();
  483. Assert.AreEqual (true, eventFired, "DW1");
  484. Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
  485. myform.Dispose ();
  486. }
  487. [Test]
  488. public void DropDownStyleEventTest ()
  489. {
  490. eventFired = false;
  491. ComboBox cmbbox = new ComboBox ();
  492. cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
  493. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  494. Assert.AreEqual (true, eventFired, "DI1");
  495. }
  496. [Test]
  497. public void SelectedIndexTest ()
  498. {
  499. eventFired = false;
  500. ComboBox cmbbox = new ComboBox ();
  501. cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
  502. cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
  503. cmbbox.SelectedIndex = 1;
  504. Assert.AreEqual (true, eventFired, "SI1");
  505. }
  506. [Test]
  507. public void SelectionWithAdd()
  508. {
  509. ComboBox cb = new ComboBox();
  510. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  511. cb.Items.Add("Item 1");
  512. cb.Items.Add("Item 3");
  513. cb.SelectedIndex = 1;
  514. eventFired = false;
  515. cb.Items.Add("Item 4");
  516. Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
  517. Assert.AreEqual(false, eventFired, "SWA2");
  518. cb.Sorted = true;
  519. cb.SelectedIndex = 1;
  520. eventFired = false;
  521. cb.Items.Add("Item 5");
  522. Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
  523. Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
  524. Assert.AreEqual(false, eventFired, "SWA5");
  525. cb.SelectedIndex = 1;
  526. eventFired = false;
  527. cb.Items.Add("Item 2");
  528. Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
  529. Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
  530. Assert.AreEqual(false, eventFired, "SWA8");
  531. }
  532. [Test]
  533. public void SelectionWithInsert()
  534. {
  535. ComboBox cb = new ComboBox();
  536. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  537. cb.Items.Add("Item 1");
  538. cb.SelectedIndex = 0;
  539. eventFired = false;
  540. cb.Items.Insert(0, "Item 2");
  541. Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
  542. Assert.AreEqual(false, eventFired, "SWI2");
  543. }
  544. [Test]
  545. public void SelectionWithClear()
  546. {
  547. ComboBox cb = new ComboBox();
  548. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  549. cb.Items.Add("Item 1");
  550. cb.SelectedIndex = 0;
  551. eventFired = false;
  552. cb.Items.Clear();
  553. Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
  554. Assert.AreEqual(false, eventFired, "SWC2");
  555. }
  556. [Test]
  557. public void SortedTest()
  558. {
  559. ComboBox mycb = new ComboBox();
  560. Assert.AreEqual(false, mycb.Sorted, "#1");
  561. mycb.Items.Add("Item 2");
  562. mycb.Items.Add("Item 1");
  563. Assert.AreEqual("Item 2", mycb.Items[0], "#2");
  564. Assert.AreEqual("Item 1", mycb.Items[1], "#3");
  565. mycb.Sorted = true;
  566. Assert.AreEqual(true, mycb.Sorted, "#4");
  567. Assert.AreEqual("Item 1", mycb.Items[0], "#5");
  568. Assert.AreEqual("Item 2", mycb.Items[1], "#6");
  569. mycb.Sorted = false;
  570. Assert.AreEqual(false, mycb.Sorted, "#7");
  571. Assert.AreEqual("Item 1", mycb.Items[0], "#8");
  572. Assert.AreEqual("Item 2", mycb.Items[1], "#9");
  573. }
  574. [Test]
  575. public void SortedAddTest()
  576. {
  577. ComboBox mycb = new ComboBox();
  578. mycb.Items.Add("Item 2");
  579. mycb.Items.Add("Item 1");
  580. mycb.Sorted = true;
  581. Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
  582. Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
  583. }
  584. [Test]
  585. public void SortedInsertTest()
  586. {
  587. ComboBox mycb = new ComboBox();
  588. mycb.Items.Add("Item 2");
  589. mycb.Items.Add("Item 1");
  590. mycb.Sorted = true;
  591. mycb.Items.Insert (0, "Item 3");
  592. Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
  593. Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
  594. Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
  595. }
  596. [Test]
  597. public void SortedSelectionInteractions()
  598. {
  599. ComboBox cb = new ComboBox();
  600. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  601. cb.Items.Add("Item 1");
  602. cb.Items.Add("Item 2");
  603. cb.Items.Add("Item 3");
  604. cb.SelectedIndex = 1;
  605. eventFired = false;
  606. cb.Sorted = true;
  607. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
  608. Assert.AreEqual(true, eventFired, "#SSI2");
  609. cb.SelectedIndex = 1;
  610. eventFired = false;
  611. cb.Sorted = true;
  612. Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
  613. Assert.AreEqual(false, eventFired, "#SSI4");
  614. cb.SelectedIndex = 1;
  615. eventFired = false;
  616. cb.Sorted = false;
  617. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
  618. Assert.AreEqual(true, eventFired, "#SSI6");
  619. }
  620. [Test]
  621. public void Text_DropDown ()
  622. {
  623. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  624. ComboBox cmbbox = new ComboBox ();
  625. Assert.IsNotNull (cmbbox.Text, "#A1");
  626. Assert.AreEqual (string.Empty, cmbbox.Text, "#A2");
  627. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#A3");
  628. cmbbox.Items.Add ("Another");
  629. cmbbox.Items.Add ("Bad");
  630. cmbbox.Items.Add ("IN");
  631. cmbbox.Items.Add ("Combobox");
  632. cmbbox.Items.Add ("BAD");
  633. cmbbox.Items.Add ("iN");
  634. cmbbox.Items.Add ("Bad");
  635. Assert.IsNotNull (cmbbox.Text, "#B1");
  636. Assert.AreEqual (string.Empty, cmbbox.Text, "#B2");
  637. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#B3");
  638. cmbbox.SelectedIndex = 3;
  639. Assert.IsNotNull (cmbbox.Text, "#C1");
  640. Assert.AreEqual ("Combobox", cmbbox.Text, "#C2");
  641. Assert.AreEqual (3, cmbbox.SelectedIndex, "#C3");
  642. cmbbox.Text = string.Empty;
  643. Assert.IsNotNull (cmbbox.Text, "#D1");
  644. Assert.AreEqual (string.Empty, cmbbox.Text, "#D2");
  645. Assert.AreEqual (3, cmbbox.SelectedIndex, "#D3");
  646. cmbbox.SelectedIndex = 1;
  647. Assert.IsNotNull (cmbbox.Text, "#E1");
  648. Assert.AreEqual ("Bad", cmbbox.Text, "#E2");
  649. Assert.AreEqual (1, cmbbox.SelectedIndex, "#E3");
  650. cmbbox.Text = null;
  651. Assert.IsNotNull (cmbbox.Text, "#F1");
  652. Assert.AreEqual (string.Empty, cmbbox.Text, "#F2");
  653. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#F3");
  654. cmbbox.SelectedIndex = 0;
  655. cmbbox.Text = "Q";
  656. Assert.IsNotNull (cmbbox.Text, "#G1");
  657. Assert.AreEqual ("Q", cmbbox.Text, "#G2");
  658. Assert.AreEqual (0, cmbbox.SelectedIndex, "#G3");
  659. cmbbox.Text = "B";
  660. Assert.IsNotNull (cmbbox.Text, "#H1");
  661. Assert.AreEqual ("B", cmbbox.Text, "#H2");
  662. Assert.AreEqual (0, cmbbox.SelectedIndex, "#H3");
  663. cmbbox.Text = "BAD";
  664. Assert.IsNotNull (cmbbox.Text, "#I1");
  665. Assert.AreEqual ("BAD", cmbbox.Text, "#I2");
  666. Assert.AreEqual (4, cmbbox.SelectedIndex, "#I3");
  667. cmbbox.Text = "BAD";
  668. Assert.IsNotNull (cmbbox.Text, "#J1");
  669. Assert.AreEqual ("BAD", cmbbox.Text, "#J2");
  670. Assert.AreEqual (4, cmbbox.SelectedIndex, "#J3");
  671. cmbbox.Text = "baD";
  672. Assert.IsNotNull (cmbbox.Text, "#K1");
  673. Assert.AreEqual ("Bad", cmbbox.Text, "#K2");
  674. Assert.AreEqual (1, cmbbox.SelectedIndex, "#K3");
  675. cmbbox.SelectedIndex = -1;
  676. cmbbox.Text = "E";
  677. Assert.IsNotNull (cmbbox.Text, "#L1");
  678. Assert.AreEqual ("E", cmbbox.Text, "#L2");
  679. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#L3");
  680. cmbbox.Text = "iN";
  681. Assert.IsNotNull (cmbbox.Text, "#M1");
  682. Assert.AreEqual ("iN", cmbbox.Text, "#M2");
  683. Assert.AreEqual (5, cmbbox.SelectedIndex, "#M3");
  684. cmbbox.Text = "In";
  685. Assert.IsNotNull (cmbbox.Text, "#N1");
  686. Assert.AreEqual ("IN", cmbbox.Text, "#N2");
  687. Assert.AreEqual (2, cmbbox.SelectedIndex, "#N3");
  688. cmbbox.Text = "Badd";
  689. Assert.IsNotNull (cmbbox.Text, "#O1");
  690. Assert.AreEqual ("Badd", cmbbox.Text, "#O2");
  691. Assert.AreEqual (2, cmbbox.SelectedIndex, "#O3");
  692. cmbbox.SelectedIndex = -1;
  693. Assert.IsNotNull (cmbbox.Text, "#P1");
  694. Assert.AreEqual (string.Empty, cmbbox.Text, "#P2");
  695. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#P3");
  696. cmbbox.Text = "Something";
  697. Assert.IsNotNull (cmbbox.Text, "#Q1");
  698. Assert.AreEqual ("Something", cmbbox.Text, "#Q2");
  699. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#Q3");
  700. cmbbox.SelectedIndex = -1;
  701. Assert.IsNotNull (cmbbox.Text, "#R1");
  702. Assert.AreEqual ("Something", cmbbox.Text, "#R2");
  703. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#R3");
  704. cmbbox.Text = null;
  705. Assert.IsNotNull (cmbbox.Text, "#S1");
  706. Assert.AreEqual (string.Empty, cmbbox.Text, "#S2");
  707. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#S3");
  708. }
  709. }
  710. [TestFixture]
  711. public class ComboBoxObjectCollectionTest
  712. {
  713. [Test]
  714. public void ComboBoxObjectCollectionPropertyTest ()
  715. {
  716. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  717. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  718. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  719. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  720. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  721. }
  722. [Test]
  723. public void AddTest ()
  724. {
  725. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  726. col.Add ("Item1");
  727. col.Add ("Item2");
  728. Assert.AreEqual (2, col.Count, "#C1");
  729. }
  730. [Test]
  731. public void Add_Null ()
  732. {
  733. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  734. try {
  735. col.Add (null);
  736. Assert.Fail ("#1");
  737. } catch (ArgumentNullException ex) {
  738. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  739. Assert.IsNull (ex.InnerException, "#3");
  740. Assert.IsNotNull (ex.Message, "#4");
  741. Assert.IsNotNull (ex.ParamName, "#5");
  742. Assert.AreEqual ("item", ex.ParamName, "#6");
  743. }
  744. }
  745. [Test]
  746. public void AddRange_Null ()
  747. {
  748. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  749. try {
  750. col.AddRange (null);
  751. Assert.Fail ("#1");
  752. } catch (ArgumentNullException ex) {
  753. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  754. Assert.IsNull (ex.InnerException, "#3");
  755. Assert.IsNotNull (ex.Message, "#4");
  756. Assert.IsNotNull (ex.ParamName, "#5");
  757. Assert.AreEqual ("items", ex.ParamName, "#6");
  758. }
  759. }
  760. [Test]
  761. public void ClearTest ()
  762. {
  763. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  764. col.Add ("Item1");
  765. col.Add ("Item2");
  766. col.Clear ();
  767. Assert.AreEqual (0, col.Count, "#D1");
  768. }
  769. [Test]
  770. public void ContainsTest ()
  771. {
  772. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  773. object obj = "Item1";
  774. col.Add (obj);
  775. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  776. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  777. }
  778. [Test]
  779. public void Contains_Null ()
  780. {
  781. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  782. try {
  783. col.Contains (null);
  784. Assert.Fail ("#1");
  785. } catch (ArgumentNullException ex) {
  786. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  787. Assert.IsNull (ex.InnerException, "#3");
  788. Assert.IsNotNull (ex.Message, "#4");
  789. Assert.IsNotNull (ex.ParamName, "#5");
  790. #if NET_2_0
  791. Assert.AreEqual ("value", ex.ParamName, "#6");
  792. #endif
  793. }
  794. }
  795. [Test]
  796. public void IndexOfTest ()
  797. {
  798. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  799. col.Add ("Item1");
  800. col.Add ("Item2");
  801. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  802. }
  803. [Test]
  804. public void IndexOf_Null ()
  805. {
  806. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  807. try {
  808. col.IndexOf (null);
  809. Assert.Fail ("#1");
  810. } catch (ArgumentNullException ex) {
  811. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  812. Assert.IsNull (ex.InnerException, "#3");
  813. Assert.IsNotNull (ex.Message, "#4");
  814. Assert.IsNotNull (ex.ParamName, "#5");
  815. #if NET_2_0
  816. Assert.AreEqual ("value", ex.ParamName, "#6");
  817. #endif
  818. }
  819. }
  820. [Test]
  821. public void Insert_Null ()
  822. {
  823. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  824. col.Add ("Item1");
  825. try {
  826. col.Insert (0, null);
  827. Assert.Fail ("#1");
  828. } catch (ArgumentNullException ex) {
  829. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  830. Assert.IsNull (ex.InnerException, "#3");
  831. Assert.IsNotNull (ex.Message, "#4");
  832. Assert.IsNotNull (ex.ParamName, "#5");
  833. Assert.AreEqual ("item", ex.ParamName, "#6");
  834. }
  835. }
  836. [Test]
  837. public void RemoveTest ()
  838. {
  839. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  840. col.Add ("Item1");
  841. col.Add ("Item2");
  842. col.Remove ("Item1");
  843. Assert.AreEqual (1, col.Count, "#1");
  844. col.Remove (null);
  845. Assert.AreEqual (1, col.Count, "#2");
  846. }
  847. [Test]
  848. public void RemoveAtTest ()
  849. {
  850. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  851. col.Add ("Item1");
  852. col.Add ("Item2");
  853. col.RemoveAt (0);
  854. Assert.AreEqual (1, col.Count, "#H1");
  855. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  856. }
  857. [Test]
  858. public void Indexer_Null ()
  859. {
  860. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  861. col.Add ("Item1");
  862. try {
  863. col [0] = null;
  864. Assert.Fail ("#1");
  865. } catch (ArgumentNullException ex) {
  866. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  867. Assert.IsNull (ex.InnerException, "#3");
  868. Assert.IsNotNull (ex.Message, "#4");
  869. Assert.IsNotNull (ex.ParamName, "#5");
  870. Assert.AreEqual ("value", ex.ParamName, "#6");
  871. }
  872. }
  873. }
  874. }