ComboBoxTest.cs 31 KB

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