ComboBoxTest.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. // Copyright (c) 2006 Matt Hargett
  25. //
  26. // Authors:
  27. // Ritvik Mayank <[email protected]>
  28. // Jordi Mas i Hernandez <[email protected]>
  29. // Matt Hargett <[email protected]>
  30. using System;
  31. using System.Data;
  32. using System.IO;
  33. using System.Collections;
  34. using System.ComponentModel;
  35. using System.Drawing;
  36. using System.Globalization;
  37. using System.Reflection;
  38. using System.Threading;
  39. using System.Windows.Forms;
  40. using NUnit.Framework;
  41. using CategoryAttribute=NUnit.Framework.CategoryAttribute;
  42. namespace MonoTests.System.Windows.Forms
  43. {
  44. [TestFixture]
  45. public class ComboBoxTest : TestHelper
  46. {
  47. private CultureInfo _originalCulture;
  48. [SetUp]
  49. protected override void SetUp ()
  50. {
  51. _originalCulture = Thread.CurrentThread.CurrentCulture;
  52. base.SetUp ();
  53. }
  54. [TearDown]
  55. protected override void TearDown ()
  56. {
  57. Thread.CurrentThread.CurrentCulture = _originalCulture;
  58. base.TearDown ();
  59. }
  60. [Test] // bug #331144
  61. public void DropDownStyle ()
  62. {
  63. ComboBox comboBox = new ComboBox ();
  64. comboBox.Items.Add ("abc");
  65. Form form = new Form ();
  66. form.Controls.Add (comboBox);
  67. form.Show ();
  68. Assert.AreEqual (ComboBoxStyle.DropDown, comboBox.DropDownStyle, "#1");
  69. comboBox.DropDownStyle = ComboBoxStyle.Simple;
  70. Assert.AreEqual (ComboBoxStyle.Simple, comboBox.DropDownStyle, "#2");
  71. comboBox.DropDownStyle = ComboBoxStyle.DropDown;
  72. Assert.AreEqual (ComboBoxStyle.DropDown, comboBox.DropDownStyle, "#3");
  73. form.Dispose ();
  74. }
  75. [Test] // bug 81610
  76. [Category ("NotWorking")]
  77. public void InitialBoundValue ()
  78. {
  79. ComboBox comboBox1;
  80. using (Form f = new Form ()) {
  81. f.ShowInTaskbar = false;
  82. DataTable t = new DataTable ();
  83. t.Columns.Add ("displaymember");
  84. t.Columns.Add ("valuemember");
  85. t.Rows.Add (new object [] {"lower", "a"});
  86. t.Rows.Add (new object [] {"upper", "A"});
  87. // This test seems to prove that the SelectedItem is updated when:
  88. // - We have a parent
  89. // - We're setting either DisplayMember, ValueMember or DataSource.
  90. comboBox1 = new ComboBox ();
  91. comboBox1.DisplayMember = "displaymember";
  92. comboBox1.ValueMember = "valuemember";
  93. comboBox1.DataSource = t;
  94. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  95. f.Controls.Add (comboBox1);
  96. Assert.AreEqual (string.Empty, comboBox1.Text, "#??");
  97. comboBox1 = new ComboBox ();
  98. f.Controls.AddRange (new Control [] { comboBox1 });
  99. comboBox1.DisplayMember = "displaymember";
  100. Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
  101. comboBox1.ValueMember = "valuemember";
  102. Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
  103. comboBox1.DataSource = t;
  104. Assert.AreEqual ("lower", comboBox1.Text, "#A02");
  105. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  106. Assert.AreEqual ("lower", comboBox1.Text, "#A03");
  107. comboBox1 = new ComboBox ();
  108. comboBox1.DisplayMember = "displaymember";
  109. comboBox1.ValueMember = "valuemember";
  110. comboBox1.DataSource = t;
  111. Assert.AreEqual (string.Empty, comboBox1.Text, "#A01");
  112. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  113. Assert.AreEqual (string.Empty, comboBox1.Text, "#A04");
  114. f.Controls.AddRange (new Control [] { comboBox1 });
  115. Assert.AreEqual (string.Empty, comboBox1.Text, "#A04");
  116. comboBox1 = new ComboBox ();
  117. f.Controls.AddRange (new Control [] { comboBox1 });
  118. comboBox1.DisplayMember = "displaymember";
  119. comboBox1.ValueMember = "valuemember";
  120. Assert.AreEqual (string.Empty, comboBox1.Text, "#A02");
  121. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  122. Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
  123. comboBox1.DataSource = t;
  124. Assert.AreEqual ("lower", comboBox1.Text, "#A03");
  125. comboBox1 = new ComboBox ();
  126. f.Controls.AddRange (new Control [] { comboBox1 });
  127. comboBox1.DataSource = t;
  128. Assert.AreEqual ("System.Data.DataRowView", comboBox1.Text, "#A03");
  129. comboBox1.DisplayMember = "displaymember";
  130. Assert.AreEqual ("lower", comboBox1.Text, "#A03");
  131. comboBox1.ValueMember = "valuemember";
  132. Assert.AreEqual ("lower", comboBox1.Text, "#A02");
  133. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  134. Assert.AreEqual ("lower", comboBox1.Text, "#A02-1");
  135. comboBox1 = new ComboBox ();
  136. comboBox1.DisplayMember = "displaymember";
  137. comboBox1.ValueMember = "valuemember";
  138. Assert.AreEqual ("", comboBox1.Text, "#A02");
  139. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  140. Assert.AreEqual (string.Empty, comboBox1.Text, "#A02-1");
  141. comboBox1.DataSource = t;
  142. Assert.AreEqual (string.Empty, comboBox1.Text, "#A03");
  143. f.Controls.AddRange (new Control [] { comboBox1 });
  144. Assert.AreEqual (string.Empty, comboBox1.Text, "#A03");
  145. comboBox1 = new ComboBox ();
  146. comboBox1.DataSource = t;
  147. Assert.AreEqual ("", comboBox1.Text, "#A03");
  148. comboBox1.DisplayMember = "displaymember";
  149. Assert.AreEqual ("", comboBox1.Text, "#A03");
  150. comboBox1.ValueMember = "valuemember";
  151. Assert.AreEqual ("", comboBox1.Text, "#A02");
  152. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  153. Assert.AreEqual ("", comboBox1.Text, "#A02-1");
  154. f.Controls.AddRange (new Control [] { comboBox1 });
  155. Assert.AreEqual ("", comboBox1.Text, "#A02-1");
  156. comboBox1 = new ComboBox ();
  157. f.Controls.AddRange (new Control [] { comboBox1 });
  158. comboBox1.DisplayMember = "displaymember";
  159. Assert.AreEqual ("", comboBox1.Text, "#A03");
  160. comboBox1.ValueMember = "valuemember";
  161. Assert.AreEqual ("", comboBox1.Text, "#A03");
  162. comboBox1.DataSource = t;
  163. Assert.AreEqual ("lower", comboBox1.Text, "#A03");
  164. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  165. Assert.AreEqual ("lower", comboBox1.Text, "#A03");
  166. comboBox1 = new ComboBox ();
  167. f.Controls.AddRange (new Control [] { comboBox1 });
  168. comboBox1.DisplayMember = "displaymember";
  169. Assert.AreEqual ("", comboBox1.Text, "#A03");
  170. comboBox1.ValueMember = "valuemember";
  171. Assert.AreEqual ("", comboBox1.Text, "#A03");
  172. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  173. Assert.AreEqual ("", comboBox1.Text, "#A03");
  174. comboBox1.DataSource = t;
  175. Assert.AreEqual ("lower", comboBox1.Text, "#A03");
  176. comboBox1 = new ComboBox ();
  177. f.Controls.AddRange (new Control [] { comboBox1 });
  178. comboBox1.ValueMember = "valuemember";
  179. Assert.AreEqual ("", comboBox1.Text, "#A03");
  180. comboBox1.DataSource = t;
  181. Assert.AreEqual ("a", comboBox1.Text, "#A03");
  182. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  183. Assert.AreEqual ("a", comboBox1.Text, "#A03");
  184. comboBox1.DisplayMember = "displaymember";
  185. Assert.AreEqual ("lower", comboBox1.Text, "#A03");
  186. comboBox1 = new ComboBox ();
  187. comboBox1.DataSource = t;
  188. Assert.AreEqual ("", comboBox1.Text, "#A03");
  189. comboBox1.DisplayMember = "displaymember";
  190. Assert.AreEqual ("", comboBox1.Text, "#A03");
  191. comboBox1.ValueMember = "valuemember";
  192. Assert.AreEqual ("", comboBox1.Text, "#A03");
  193. f.Controls.AddRange (new Control [] { comboBox1 });
  194. Assert.AreEqual ("lower", comboBox1.Text, "#A03");
  195. InitialBoundValue_dummy e = new InitialBoundValue_dummy ();
  196. e.controlsrc = "c";
  197. comboBox1.DataBindings.Add ("SelectedValue", e, "controlsrc");
  198. Assert.AreEqual ("", comboBox1.Text, "#A03");
  199. f.Show ();
  200. Assert.AreEqual ("", comboBox1.Text, "#A03");
  201. // The real failing test
  202. comboBox1 = new ComboBox ();
  203. comboBox1.DisplayMember = "displaymember";
  204. Assert.AreEqual ("", comboBox1.Text, "#A03");
  205. comboBox1.ValueMember = "valuemember";
  206. Assert.AreEqual ("", comboBox1.Text, "#A03");
  207. comboBox1.DataSource = t;
  208. Assert.AreEqual ("", comboBox1.Text, "#A03");
  209. comboBox1.DataBindings.Add ("SelectedValue", new InitialBoundValue_dummy (), "controlsrc");
  210. Assert.AreEqual ("", comboBox1.Text, "#A03");
  211. f.Controls.AddRange (new Control [] { comboBox1 });
  212. Assert.AreEqual ("", comboBox1.Text, "#A03");
  213. f.Show ();
  214. Assert.AreEqual ("", comboBox1.Text, "#A03");
  215. Assert.AreEqual ("", comboBox1.Text, "#bug");
  216. string table =
  217. @"<?xml version=""1.0"" standalone=""yes""?>
  218. <DOK>
  219. <DOK>
  220. <klient>287</klient>
  221. </DOK>
  222. </DOK>
  223. ";
  224. string lookup =
  225. @"<?xml version=""1.0"" standalone=""yes""?>
  226. <klient>
  227. <klient>
  228. <nimi>FAILED</nimi>
  229. <kood>316</kood>
  230. </klient>
  231. <klient>
  232. <nimi>SUCCESS</nimi>
  233. <kood>287</kood>
  234. </klient>
  235. </klient>";
  236. using (Form frm = new Form ()) {
  237. frm.ShowInTaskbar = false;
  238. DataSet dsTable = new DataSet ();
  239. dsTable.ReadXml (new StringReader (table));
  240. DataSet dsLookup = new DataSet ();
  241. dsLookup.ReadXml (new StringReader (lookup));
  242. ComboBox cb = new ComboBox ();
  243. cb.DataSource = dsLookup.Tables [0];
  244. cb.DisplayMember = "nimi";
  245. cb.ValueMember = "kood";
  246. InitialBoundValue_dummy d = new InitialBoundValue_dummy();
  247. d.controlsrc = "287";
  248. cb.DataBindings.Add ("SelectedValue", d, "controlsrc");
  249. frm.Controls.Add (cb);
  250. Assert.AreEqual ("", cb.Text, "#01");
  251. frm.Show ();
  252. Assert.AreEqual ("SUCCESS", cb.Text, "#02");
  253. }
  254. }
  255. }
  256. class InitialBoundValue_dummy
  257. {
  258. string t;
  259. public string controlsrc
  260. {
  261. get { return t; }
  262. set { t = value; }
  263. }
  264. }
  265. [Test]
  266. public void ContextMenuTest ()
  267. {
  268. ComboBox cmb = new ComboBox ();
  269. ContextMenu cm = new ContextMenu ();
  270. Assert.IsNull (cmb.ContextMenu, "#1");
  271. cmb.ContextMenu = cm;
  272. Assert.AreSame (cmb.ContextMenu, cm, "#2");
  273. cmb.DropDownStyle = ComboBoxStyle.DropDown;
  274. Assert.AreSame (cmb.ContextMenu, cm, "#3");
  275. cmb.DropDownStyle = ComboBoxStyle.DropDownList;
  276. Assert.AreSame (cmb.ContextMenu, cm, "#4");
  277. cmb.DropDownStyle = ComboBoxStyle.Simple;
  278. Assert.AreSame (cmb.ContextMenu, cm, "#5");
  279. }
  280. [Test] // bug 80794
  281. public void DataBindingTest ()
  282. {
  283. string table =
  284. @"<?xml version=""1.0"" standalone=""yes""?>
  285. <DOK>
  286. <DOK>
  287. <klient>287</klient>
  288. </DOK>
  289. </DOK>
  290. ";
  291. string lookup =
  292. @"<?xml version=""1.0"" standalone=""yes""?>
  293. <klient>
  294. <klient>
  295. <nimi>FAILED</nimi>
  296. <kood>316</kood>
  297. </klient>
  298. <klient>
  299. <nimi>SUCCESS</nimi>
  300. <kood>287</kood>
  301. </klient>
  302. </klient>";
  303. using (Form frm = new Form ()) {
  304. frm.ShowInTaskbar = false;
  305. DataSet dsTable = new DataSet ();
  306. dsTable.ReadXml (new StringReader (table));
  307. DataSet dsLookup = new DataSet ();
  308. dsLookup.ReadXml (new StringReader (lookup));
  309. ComboBox cb = new ComboBox ();
  310. cb.DataSource = dsLookup.Tables [0];
  311. cb.DisplayMember = "nimi";
  312. cb.ValueMember = "kood";
  313. cb.DataBindings.Add ("SelectedValue", dsTable.Tables [0], "klient");
  314. frm.Controls.Add (cb);
  315. Assert.AreEqual ("", cb.Text, "#01");
  316. frm.Show ();
  317. Assert.AreEqual ("SUCCESS", cb.Text, "#02");
  318. }
  319. }
  320. [Test] // bug #82069
  321. public void DataBindingTest2 ()
  322. {
  323. ComboBox cmb = new ComboBox ();
  324. cmb.Items.Add (new MockItem ("Foo", 9));
  325. cmb.Items.Add (new MockItem ("Bar", 6));
  326. cmb.ValueMember = "Value";
  327. cmb.DisplayMember = "Text";
  328. Form form = new Form ();
  329. form.Controls.Add (cmb);
  330. form.Show ();
  331. cmb.SelectedIndex = 0;
  332. Assert.AreEqual ("Foo", cmb.Text, "#1");
  333. cmb.SelectedIndex = 1;
  334. Assert.AreEqual ("Bar", cmb.Text, "#2");
  335. form.Dispose ();
  336. }
  337. [Test]
  338. public void ComboBoxPropertyTest ()
  339. {
  340. ComboBox mycmbbox = new ComboBox ();
  341. Assert.AreEqual (DrawMode.Normal, mycmbbox.DrawMode, "#1");
  342. Assert.AreEqual (ComboBoxStyle.DropDown, mycmbbox.DropDownStyle, "#2");
  343. Assert.AreEqual (false, mycmbbox.DroppedDown, "#4");
  344. Assert.AreEqual (true, mycmbbox.IntegralHeight, "#5");
  345. Assert.AreEqual (0, mycmbbox.Items.Count, "#6");
  346. //Assert.AreEqual (15, mycmbbox.ItemHeight, "#7"); // Note: Item height depends on the current font.
  347. Assert.AreEqual (8, mycmbbox.MaxDropDownItems, "#8");
  348. Assert.AreEqual (0, mycmbbox.MaxLength, "#9");
  349. //Assert.AreEqual (20, mycmbbox.PreferredHeight, "#10");
  350. // Note: Item height depends on the current font.
  351. Assert.AreEqual (-1, mycmbbox.SelectedIndex, "#11");
  352. Assert.AreEqual (null, mycmbbox.SelectedItem, "#12");
  353. Assert.AreEqual ("", mycmbbox.SelectedText, "#13");
  354. Assert.AreEqual (0, mycmbbox.SelectionLength, "#14");
  355. Assert.AreEqual (0, mycmbbox.SelectionStart, "#15");
  356. Assert.AreEqual (false, mycmbbox.Sorted, "#16");
  357. Assert.AreEqual ("", mycmbbox.Text, "#17");
  358. #if NET_2_0
  359. Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#18");
  360. Assert.AreEqual (AutoCompleteMode.None, mycmbbox.AutoCompleteMode, "#19");
  361. Assert.AreEqual (AutoCompleteSource.None, mycmbbox.AutoCompleteSource, "#20");
  362. mycmbbox.AutoCompleteCustomSource = null;
  363. Assert.AreEqual (true, mycmbbox.AutoCompleteCustomSource != null, "#21");
  364. Assert.AreEqual (ImageLayout.Tile, mycmbbox.BackgroundImageLayout, "#22");
  365. Assert.AreEqual (null, mycmbbox.DataSource, "#23");
  366. Assert.AreEqual (106, mycmbbox.DropDownHeight, "#24");
  367. Assert.AreEqual (FlatStyle.Standard, mycmbbox.FlatStyle, "#25");
  368. Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MaximumSize.ToString (), "#26");
  369. Assert.AreEqual ("{Width=0, Height=0}", mycmbbox.MinimumSize.ToString (), "#27");
  370. Assert.AreEqual ("{Left=0,Top=0,Right=0,Bottom=0}", mycmbbox.Padding.ToString (), "#28");
  371. #endif
  372. }
  373. [Test]
  374. public void PreferredHeight ()
  375. {
  376. // PreferredHeight is not tied to ItemHeight like we had it
  377. ComboBox cb = new ComboBox ();
  378. int h = cb.PreferredHeight;
  379. cb.ItemHeight = 10;
  380. Assert.AreEqual (h, cb.PreferredHeight, "A2");
  381. cb.ItemHeight = 30;
  382. Assert.AreEqual (h, cb.PreferredHeight, "A3");
  383. }
  384. #if NET_2_0
  385. [Test]
  386. public void ResetTextTest ()
  387. {
  388. ComboBox cmbbox = new ComboBox ();
  389. Assert.AreEqual ("", cmbbox.Text, "#01");
  390. cmbbox.Text = "abc";
  391. Assert.AreEqual ("abc", cmbbox.Text, "#02");
  392. cmbbox.ResetText ();
  393. Assert.AreEqual ("", cmbbox.Text, "#03");
  394. }
  395. [Test]
  396. public void BackgroundImageLayoutTest ()
  397. {
  398. ComboBox cmbbox = new ComboBox ();
  399. cmbbox.BackgroundImageLayout = ImageLayout.Stretch;
  400. Assert.AreEqual (ImageLayout.Stretch, cmbbox.BackgroundImageLayout, "#01");
  401. }
  402. [Test]
  403. public void DropDownHeightTest ()
  404. {
  405. ComboBox cmbbox = new ComboBox ();
  406. cmbbox.DropDownHeight = 225;
  407. Assert.AreEqual (225, cmbbox.DropDownHeight, "#01");
  408. cmbbox.DropDownHeight = 1;
  409. Assert.AreEqual (1, cmbbox.DropDownHeight, "#02");
  410. }
  411. [Test]
  412. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  413. public void DropDownHeightExceptionTest1 ()
  414. {
  415. ComboBox cmbbox = new ComboBox ();
  416. cmbbox.DropDownHeight = -225;
  417. }
  418. [Test]
  419. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  420. public void DropDownHeightExceptionTest2 ()
  421. {
  422. ComboBox cmbbox = new ComboBox ();
  423. cmbbox.DropDownHeight = 0;
  424. }
  425. [Test]
  426. public void FlatStyleTest ()
  427. {
  428. ComboBox cmbbox = new ComboBox ();
  429. cmbbox.FlatStyle = FlatStyle.Popup;
  430. Assert.AreEqual (FlatStyle.Popup, cmbbox.FlatStyle, "#01");
  431. }
  432. [Test]
  433. [ExpectedException (typeof (InvalidEnumArgumentException))]
  434. public void FlatStyleExceptionTest ()
  435. {
  436. ComboBox cmbbox = new ComboBox ();
  437. cmbbox.FlatStyle = (FlatStyle) (-123);
  438. }
  439. [Test]
  440. public void MaximumSizeTest ()
  441. {
  442. ComboBox cmbbox = new ComboBox ();
  443. cmbbox.MaximumSize = new Size (25, 25);
  444. Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MaximumSize.ToString (), "#01");
  445. cmbbox.MaximumSize = new Size (50, 75);
  446. Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MaximumSize.ToString (), "#02");
  447. }
  448. [Test]
  449. public void MinumumSizeTest ()
  450. {
  451. ComboBox cmbbox = new ComboBox ();
  452. cmbbox.MinimumSize = new Size (25, 25);
  453. Assert.AreEqual ("{Width=25, Height=0}", cmbbox.MinimumSize.ToString (), "#1");
  454. cmbbox.MinimumSize = new Size (50, 75);
  455. Assert.AreEqual ("{Width=50, Height=0}", cmbbox.MinimumSize.ToString (), "#2");
  456. }
  457. [Test]
  458. public void PaddingTest ()
  459. {
  460. ComboBox cmbbox = new ComboBox ();
  461. cmbbox.Padding = new Padding (21);
  462. Assert.AreEqual ("{Left=21,Top=21,Right=21,Bottom=21}", cmbbox.Padding.ToString (), "#01");
  463. }
  464. #endif
  465. [Test]
  466. public void BeginEndUpdateTest ()
  467. {
  468. Form myform = new Form ();
  469. myform.ShowInTaskbar = false;
  470. myform.Visible = true;
  471. ComboBox cmbbox = new ComboBox ();
  472. cmbbox.Items.Add ("A");
  473. cmbbox.Visible = true;
  474. myform.Controls.Add (cmbbox);
  475. cmbbox.BeginUpdate ();
  476. for (int x = 1 ; x < 5000 ; x++) {
  477. cmbbox.Items.Add ("Item " + x.ToString ());
  478. }
  479. cmbbox.EndUpdate ();
  480. myform.Dispose ();
  481. }
  482. [Test]
  483. public void FindString ()
  484. {
  485. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  486. ComboBox cmbbox = new ComboBox ();
  487. Assert.AreEqual (-1, cmbbox.FindString ("Hello"), "#A1");
  488. Assert.AreEqual (-1, cmbbox.FindString (string.Empty), "#A2");
  489. Assert.AreEqual (-1, cmbbox.FindString (null), "#A3");
  490. Assert.AreEqual (-1, cmbbox.FindString ("Hola", -5), "#A4");
  491. Assert.AreEqual (-1, cmbbox.FindString ("Hola", 40), "#A5");
  492. cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
  493. Assert.AreEqual (2, cmbbox.FindString ("I"), "#B1");
  494. Assert.AreEqual (0, cmbbox.FindString ("in"), "#B2");
  495. Assert.AreEqual (1, cmbbox.FindString ("BAD"), "#B3");
  496. Assert.AreEqual (1, cmbbox.FindString ("Bad"), "#B4");
  497. Assert.AreEqual (1, cmbbox.FindString ("b"), "#B5");
  498. Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#B6");
  499. Assert.AreEqual (-1, cmbbox.FindString (null), "#B7");
  500. Assert.AreEqual (3, cmbbox.FindString ("b", 2), "#C1");
  501. Assert.AreEqual (5, cmbbox.FindString ("I", 3), "#C2");
  502. Assert.AreEqual (4, cmbbox.FindString ("B", 3), "#C3");
  503. Assert.AreEqual (1, cmbbox.FindString ("B", 4), "#C4");
  504. Assert.AreEqual (5, cmbbox.FindString ("I", 4), "#C5");
  505. Assert.AreEqual (4, cmbbox.FindString ("BA", 3), "#C6");
  506. Assert.AreEqual (0, cmbbox.FindString ("i", -1), "#C7");
  507. Assert.AreEqual (3, cmbbox.FindString (string.Empty, 2), "#C8");
  508. Assert.AreEqual (-1, cmbbox.FindString (null, 1), "#C9");
  509. cmbbox.Items.Add (string.Empty);
  510. Assert.AreEqual (0, cmbbox.FindString (string.Empty), "#D1");
  511. Assert.AreEqual (-1, cmbbox.FindString (null), "#D2");
  512. Assert.AreEqual (4, cmbbox.FindString (string.Empty, 3), "#E1");
  513. Assert.AreEqual (-1, cmbbox.FindString (null, 99), "#E2");
  514. Assert.AreEqual (-1, cmbbox.FindString (null, -5), "#E3");
  515. }
  516. [Test]
  517. public void FindString_StartIndex_ItemCount ()
  518. {
  519. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  520. ComboBox cmbbox = new ComboBox ();
  521. cmbbox.Items.AddRange (new object [] { "BA", "BB" });
  522. #if NET_2_0
  523. Assert.AreEqual (0, cmbbox.FindString ("b", 1));
  524. #else
  525. try {
  526. cmbbox.FindString ("b", 1);
  527. Assert.Fail ("#1");
  528. } catch (ArgumentOutOfRangeException ex) {
  529. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  530. Assert.IsNull (ex.InnerException, "#3");
  531. Assert.IsNotNull (ex.Message, "#4");
  532. Assert.IsNotNull (ex.ParamName, "#5");
  533. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  534. }
  535. #endif
  536. }
  537. [Test]
  538. public void FindString_StartIndex_Min ()
  539. {
  540. ComboBox cmbbox = new ComboBox ();
  541. cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
  542. try {
  543. cmbbox.FindString ("Hola", -2);
  544. Assert.Fail ("#1");
  545. } catch (ArgumentOutOfRangeException ex) {
  546. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  547. Assert.IsNull (ex.InnerException, "#3");
  548. Assert.IsNotNull (ex.Message, "#4");
  549. Assert.IsNotNull (ex.ParamName, "#5");
  550. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  551. }
  552. }
  553. [Test]
  554. public void FindString_StartIndex_Max ()
  555. {
  556. ComboBox cmbbox = new ComboBox ();
  557. cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
  558. try {
  559. cmbbox.FindString ("Hola", 4);
  560. Assert.Fail ("#1");
  561. } catch (ArgumentOutOfRangeException ex) {
  562. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  563. Assert.IsNull (ex.InnerException, "#3");
  564. Assert.IsNotNull (ex.Message, "#4");
  565. Assert.IsNotNull (ex.ParamName, "#5");
  566. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  567. }
  568. }
  569. [Test]
  570. public void FindStringExact ()
  571. {
  572. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  573. ComboBox cmbbox = new ComboBox ();
  574. Assert.AreEqual (-1, cmbbox.FindStringExact ("Hello"), "#A1");
  575. Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#A2");
  576. Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#A3");
  577. Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", -5), "#A4");
  578. Assert.AreEqual (-1, cmbbox.FindStringExact ("Hola", 40), "#A5");
  579. cmbbox.Items.AddRange (new object [] { "in", "BADTest", "IN", "BAD", "Bad", "In" });
  580. Assert.AreEqual (2, cmbbox.FindStringExact ("IN"), "#B1");
  581. Assert.AreEqual (0, cmbbox.FindStringExact ("in"), "#B2");
  582. Assert.AreEqual (3, cmbbox.FindStringExact ("BAD"), "#B3");
  583. Assert.AreEqual (3, cmbbox.FindStringExact ("bad"), "#B4");
  584. Assert.AreEqual (-1, cmbbox.FindStringExact ("B"), "#B5");
  585. Assert.AreEqual (-1, cmbbox.FindStringExact ("NonExistant"), "#B6");
  586. Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty), "#B7");
  587. Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#B8");
  588. Assert.AreEqual (2, cmbbox.FindStringExact ("In", 1), "#C1");
  589. Assert.AreEqual (5, cmbbox.FindStringExact ("In", 2), "#C2");
  590. Assert.AreEqual (4, cmbbox.FindStringExact ("BaD", 3), "#C3");
  591. Assert.AreEqual (3, cmbbox.FindStringExact ("bad", -1), "#C4");
  592. Assert.AreEqual (5, cmbbox.FindStringExact ("In", 4), "#C5");
  593. Assert.AreEqual (3, cmbbox.FindStringExact ("bad", 4), "#C6");
  594. Assert.AreEqual (-1, cmbbox.FindStringExact ("B", 4), "#C7");
  595. Assert.AreEqual (-1, cmbbox.FindStringExact ("BADNOT", 0), "#C8");
  596. Assert.AreEqual (-1, cmbbox.FindStringExact ("i", -1), "#C9");
  597. Assert.AreEqual (-1, cmbbox.FindStringExact (string.Empty, 2), "#C10");
  598. Assert.AreEqual (-1, cmbbox.FindStringExact (null, 1), "#C11");
  599. cmbbox.Items.Add (string.Empty);
  600. Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty), "#D1");
  601. Assert.AreEqual (-1, cmbbox.FindStringExact (null), "#D2");
  602. Assert.AreEqual (6, cmbbox.FindStringExact (string.Empty, 3), "#E1");
  603. Assert.AreEqual (-1, cmbbox.FindStringExact (null, 99), "#E2");
  604. Assert.AreEqual (-1, cmbbox.FindStringExact (null, -5), "#E3");
  605. }
  606. [Test]
  607. public void FindStringExact_StartIndex_ItemCount ()
  608. {
  609. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  610. ComboBox cmbbox = new ComboBox ();
  611. cmbbox.Items.AddRange (new object [] { "AB", "BA", "AB", "BA" });
  612. #if NET_2_0
  613. Assert.AreEqual (1, cmbbox.FindStringExact ("BA", 3));
  614. #else
  615. try {
  616. cmbbox.FindString ("BA", 3);
  617. Assert.Fail ("#1");
  618. } catch (ArgumentOutOfRangeException ex) {
  619. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  620. Assert.IsNull (ex.InnerException, "#3");
  621. Assert.IsNotNull (ex.Message, "#4");
  622. Assert.IsNotNull (ex.ParamName, "#5");
  623. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  624. }
  625. #endif
  626. }
  627. [Test]
  628. public void FindStringExact_StartIndex_Min ()
  629. {
  630. ComboBox cmbbox = new ComboBox ();
  631. cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
  632. try {
  633. cmbbox.FindStringExact ("Hola", -2);
  634. Assert.Fail ("#1");
  635. } catch (ArgumentOutOfRangeException ex) {
  636. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  637. Assert.IsNull (ex.InnerException, "#3");
  638. Assert.IsNotNull (ex.Message, "#4");
  639. Assert.IsNotNull (ex.ParamName, "#5");
  640. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  641. }
  642. }
  643. [Test]
  644. public void FindStringExact_StartIndex_Max ()
  645. {
  646. ComboBox cmbbox = new ComboBox ();
  647. cmbbox.Items.AddRange (new object [] { "ACBD", "ABDC", "ACBD", "ABCD" });
  648. try {
  649. cmbbox.FindStringExact ("Hola", 4);
  650. Assert.Fail ("#1");
  651. } catch (ArgumentOutOfRangeException ex) {
  652. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  653. Assert.IsNull (ex.InnerException, "#3");
  654. Assert.IsNotNull (ex.Message, "#4");
  655. Assert.IsNotNull (ex.ParamName, "#5");
  656. Assert.AreEqual ("startIndex", ex.ParamName, "#6");
  657. }
  658. }
  659. [Test]
  660. public void GetItemHeightTest ()
  661. {
  662. ComboBox cmbbox = new ComboBox ();
  663. cmbbox.Items.Add ("ABC");
  664. cmbbox.Items.Add ("BCD");
  665. cmbbox.Items.Add ("DEF");
  666. int x = -1;
  667. x = cmbbox.GetItemHeight (x);
  668. Assert.IsTrue (cmbbox.ItemHeight > 0, "#21");
  669. }
  670. //
  671. // Exceptions
  672. //
  673. [Test]
  674. [ExpectedException (typeof (InvalidEnumArgumentException))]
  675. public void DropDownStyleException ()
  676. {
  677. ComboBox cmbbox = new ComboBox ();
  678. cmbbox.DropDownStyle = (ComboBoxStyle) 10;
  679. }
  680. [Test]
  681. [ExpectedException (typeof (InvalidEnumArgumentException))]
  682. public void DrawModeException ()
  683. {
  684. ComboBox cmbbox = new ComboBox ();
  685. cmbbox.DrawMode = (DrawMode) 10;
  686. }
  687. [Test]
  688. public void DropDownWidth ()
  689. {
  690. ComboBox cmbbox = new ComboBox ();
  691. Assert.AreEqual (121, cmbbox.DropDownWidth, "#A1");
  692. cmbbox.DropDownWidth = 1;
  693. Assert.AreEqual (1, cmbbox.DropDownWidth, "#A2");
  694. try {
  695. cmbbox.DropDownWidth = 0;
  696. Assert.Fail ("#B1");
  697. }
  698. #if NET_2_0
  699. catch (ArgumentOutOfRangeException ex) {
  700. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  701. Assert.IsNotNull (ex.Message, "#B3");
  702. Assert.IsNotNull (ex.ParamName, "#B4");
  703. Assert.AreEqual ("DropDownWidth", ex.ParamName, "#B5");
  704. Assert.IsNull (ex.InnerException, "#B6");
  705. }
  706. #else
  707. catch (ArgumentException ex) {
  708. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  709. Assert.IsNotNull (ex.Message, "#B3");
  710. Assert.IsNull (ex.ParamName, "#B4");
  711. Assert.IsNull (ex.InnerException, "#B5");
  712. }
  713. #endif
  714. }
  715. [Test]
  716. public void ItemHeight ()
  717. {
  718. ComboBox cmbbox = new ComboBox ();
  719. Assert.IsTrue (cmbbox.ItemHeight >= 1, "#A1");
  720. cmbbox.ItemHeight = 1;
  721. Assert.AreEqual (1, cmbbox.ItemHeight, "#A2");
  722. try {
  723. cmbbox.ItemHeight = 0;
  724. Assert.Fail ("#B1");
  725. }
  726. #if NET_2_0
  727. catch (ArgumentOutOfRangeException ex) {
  728. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  729. Assert.IsNotNull (ex.Message, "#B3");
  730. Assert.IsNotNull (ex.ParamName, "#B4");
  731. Assert.AreEqual ("ItemHeight", ex.ParamName, "#B5");
  732. Assert.IsNull (ex.InnerException, "#B6");
  733. }
  734. #else
  735. catch (ArgumentException ex) {
  736. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  737. Assert.IsNotNull (ex.Message, "#B3");
  738. Assert.IsNull (ex.ParamName, "#B4");
  739. Assert.IsNull (ex.InnerException, "#B5");
  740. }
  741. #endif
  742. }
  743. [Test]
  744. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  745. public void SelectedIndexException ()
  746. {
  747. ComboBox cmbbox = new ComboBox ();
  748. cmbbox.SelectedIndex = -2;
  749. }
  750. //
  751. // Events
  752. //
  753. private bool eventFired;
  754. private DrawItemEventArgs drawItemsArgs;
  755. private void DrawItemEventH (object sender, DrawItemEventArgs e)
  756. {
  757. eventFired = true;
  758. drawItemsArgs = e;
  759. }
  760. private void GenericHandler (object sender, EventArgs e)
  761. {
  762. eventFired = true;
  763. }
  764. [Ignore ("Bugs in X11 prevent this test to run properly")]
  765. public void DrawItemEventTest ()
  766. {
  767. eventFired = false;
  768. drawItemsArgs = null;
  769. Form myform = new Form ();
  770. myform.ShowInTaskbar = false;
  771. ComboBox cmbbox = new ComboBox ();
  772. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  773. cmbbox.DrawMode = DrawMode.OwnerDrawFixed;
  774. cmbbox.DrawItem += new DrawItemEventHandler (DrawItemEventH);
  775. myform.Controls.Add (cmbbox);
  776. cmbbox.Items.AddRange(new object[] {"Item1"});
  777. myform.Visible = true;
  778. cmbbox.Visible = true;
  779. cmbbox.Refresh ();
  780. Assert.AreEqual (true, eventFired, "DW1");
  781. Assert.AreEqual (0, drawItemsArgs.Index, "DW2");
  782. myform.Dispose ();
  783. }
  784. [Test]
  785. public void DropDownStyleEventTest ()
  786. {
  787. eventFired = false;
  788. ComboBox cmbbox = new ComboBox ();
  789. cmbbox.DropDownStyleChanged += new EventHandler (GenericHandler);
  790. cmbbox.DropDownStyle = ComboBoxStyle.Simple;
  791. Assert.AreEqual (true, eventFired, "DI1");
  792. }
  793. [Test]
  794. public void SelectedIndexTest ()
  795. {
  796. eventFired = false;
  797. ComboBox cmbbox = new ComboBox ();
  798. cmbbox.Items.AddRange(new object[] {"Item1", "Item2"});
  799. cmbbox.SelectedIndexChanged += new EventHandler (GenericHandler);
  800. cmbbox.SelectedIndex = 1;
  801. Assert.AreEqual (true, eventFired, "SI1");
  802. }
  803. [Test]
  804. public void SelectionWithAdd()
  805. {
  806. ComboBox cb = new ComboBox();
  807. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  808. cb.Items.Add("Item 1");
  809. cb.Items.Add("Item 3");
  810. cb.SelectedIndex = 1;
  811. eventFired = false;
  812. cb.Items.Add("Item 4");
  813. Assert.AreEqual(1, cb.SelectedIndex, "SWA1");
  814. Assert.AreEqual(false, eventFired, "SWA2");
  815. cb.Sorted = true;
  816. cb.SelectedIndex = 1;
  817. eventFired = false;
  818. cb.Items.Add("Item 5");
  819. Assert.AreEqual(1, cb.SelectedIndex, "SWA3");
  820. Assert.AreEqual("Item 3", cb.SelectedItem, "SWA4");
  821. Assert.AreEqual(false, eventFired, "SWA5");
  822. cb.SelectedIndex = 1;
  823. eventFired = false;
  824. cb.Items.Add("Item 2");
  825. Assert.AreEqual(1, cb.SelectedIndex, "SWA6");
  826. Assert.AreEqual("Item 2", cb.SelectedItem, "SWA7");
  827. Assert.AreEqual(false, eventFired, "SWA8");
  828. }
  829. [Test]
  830. public void SelectionWithInsert()
  831. {
  832. ComboBox cb = new ComboBox();
  833. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  834. cb.Items.Add("Item 1");
  835. cb.SelectedIndex = 0;
  836. eventFired = false;
  837. cb.Items.Insert(0, "Item 2");
  838. Assert.AreEqual(0, cb.SelectedIndex, "SWI1");
  839. Assert.AreEqual(false, eventFired, "SWI2");
  840. }
  841. [Test]
  842. public void SelectionWithClear()
  843. {
  844. ComboBox cb = new ComboBox();
  845. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  846. cb.Items.Add("Item 1");
  847. cb.SelectedIndex = 0;
  848. eventFired = false;
  849. cb.Items.Clear();
  850. Assert.AreEqual(-1, cb.SelectedIndex, "SWC1");
  851. Assert.AreEqual(false, eventFired, "SWC2");
  852. }
  853. // Bug #333750 - DisplayMember assignation
  854. // when ComboBox already has a BindingContext BUT
  855. // handle hasn't been created yet.
  856. [Test]
  857. public void SelectedTextWithBindingTest ()
  858. {
  859. Form form = new Form ();
  860. form.ShowInTaskbar = false;
  861. ComboBox cb = new ComboBox ();
  862. cb.Parent = form;
  863. MockItem [] items = new MockItem [] {
  864. new MockItem ("A", 0),
  865. new MockItem ("B", 1),
  866. new MockItem ("C", 2)
  867. };
  868. cb.DataSource = items;
  869. cb.DisplayMember = "Text";
  870. form.Show ();
  871. Assert.AreEqual ("A", cb.SelectedText, "#A1");
  872. form.Dispose ();
  873. }
  874. [Test]
  875. public void SortedTest()
  876. {
  877. ComboBox mycb = new ComboBox();
  878. Assert.AreEqual(false, mycb.Sorted, "#1");
  879. mycb.Items.Add("Item 2");
  880. mycb.Items.Add("Item 1");
  881. Assert.AreEqual("Item 2", mycb.Items[0], "#2");
  882. Assert.AreEqual("Item 1", mycb.Items[1], "#3");
  883. mycb.Sorted = true;
  884. Assert.AreEqual(true, mycb.Sorted, "#4");
  885. Assert.AreEqual("Item 1", mycb.Items[0], "#5");
  886. Assert.AreEqual("Item 2", mycb.Items[1], "#6");
  887. mycb.Sorted = false;
  888. Assert.AreEqual(false, mycb.Sorted, "#7");
  889. Assert.AreEqual("Item 1", mycb.Items[0], "#8");
  890. Assert.AreEqual("Item 2", mycb.Items[1], "#9");
  891. }
  892. [Test]
  893. public void SortedAddTest()
  894. {
  895. ComboBox mycb = new ComboBox();
  896. mycb.Items.Add("Item 2");
  897. mycb.Items.Add("Item 1");
  898. mycb.Sorted = true;
  899. Assert.AreEqual("Item 1", mycb.Items[0], "#I1");
  900. Assert.AreEqual("Item 2", mycb.Items[1], "#I2");
  901. }
  902. [Test]
  903. public void SortedInsertTest()
  904. {
  905. ComboBox mycb = new ComboBox();
  906. mycb.Items.Add("Item 2");
  907. mycb.Items.Add("Item 1");
  908. mycb.Sorted = true;
  909. mycb.Items.Insert (0, "Item 3");
  910. Assert.AreEqual("Item 1", mycb.Items[0], "#J1");
  911. Assert.AreEqual("Item 2", mycb.Items[1], "#J2");
  912. Assert.AreEqual("Item 3", mycb.Items[2], "#J3");
  913. }
  914. [Test]
  915. public void SortedSelectionInteractions()
  916. {
  917. ComboBox cb = new ComboBox();
  918. cb.SelectedIndexChanged += new EventHandler(GenericHandler);
  919. cb.Items.Add("Item 1");
  920. cb.Items.Add("Item 2");
  921. cb.Items.Add("Item 3");
  922. cb.SelectedIndex = 1;
  923. eventFired = false;
  924. cb.Sorted = true;
  925. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI1");
  926. Assert.AreEqual(true, eventFired, "#SSI2");
  927. cb.SelectedIndex = 1;
  928. eventFired = false;
  929. cb.Sorted = true;
  930. Assert.AreEqual(1, cb.SelectedIndex, "#SSI3");
  931. Assert.AreEqual(false, eventFired, "#SSI4");
  932. cb.SelectedIndex = 1;
  933. eventFired = false;
  934. cb.Sorted = false;
  935. Assert.AreEqual(-1, cb.SelectedIndex, "#SSI5");
  936. Assert.AreEqual(true, eventFired, "#SSI6");
  937. }
  938. [Test]
  939. public void Text_DropDown ()
  940. {
  941. Thread.CurrentThread.CurrentCulture = new CultureInfo ("tr-TR");
  942. ComboBox cmbbox = new ComboBox ();
  943. Assert.IsNotNull (cmbbox.Text, "#A1");
  944. Assert.AreEqual (string.Empty, cmbbox.Text, "#A2");
  945. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#A3");
  946. cmbbox.Items.Add ("Another");
  947. cmbbox.Items.Add ("Bad");
  948. cmbbox.Items.Add ("IN");
  949. cmbbox.Items.Add ("Combobox");
  950. cmbbox.Items.Add ("BAD");
  951. cmbbox.Items.Add ("iN");
  952. cmbbox.Items.Add ("Bad");
  953. Assert.IsNotNull (cmbbox.Text, "#B1");
  954. Assert.AreEqual (string.Empty, cmbbox.Text, "#B2");
  955. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#B3");
  956. cmbbox.SelectedIndex = 3;
  957. Assert.IsNotNull (cmbbox.Text, "#C1");
  958. Assert.AreEqual ("Combobox", cmbbox.Text, "#C2");
  959. Assert.AreEqual (3, cmbbox.SelectedIndex, "#C3");
  960. cmbbox.Text = string.Empty;
  961. Assert.IsNotNull (cmbbox.Text, "#D1");
  962. Assert.AreEqual (string.Empty, cmbbox.Text, "#D2");
  963. Assert.AreEqual (3, cmbbox.SelectedIndex, "#D3");
  964. cmbbox.SelectedIndex = 1;
  965. Assert.IsNotNull (cmbbox.Text, "#E1");
  966. Assert.AreEqual ("Bad", cmbbox.Text, "#E2");
  967. Assert.AreEqual (1, cmbbox.SelectedIndex, "#E3");
  968. cmbbox.Text = null;
  969. Assert.IsNotNull (cmbbox.Text, "#F1");
  970. Assert.AreEqual (string.Empty, cmbbox.Text, "#F2");
  971. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#F3");
  972. cmbbox.SelectedIndex = 0;
  973. cmbbox.Text = "Q";
  974. Assert.IsNotNull (cmbbox.Text, "#G1");
  975. Assert.AreEqual ("Q", cmbbox.Text, "#G2");
  976. Assert.AreEqual (0, cmbbox.SelectedIndex, "#G3");
  977. cmbbox.Text = "B";
  978. Assert.IsNotNull (cmbbox.Text, "#H1");
  979. Assert.AreEqual ("B", cmbbox.Text, "#H2");
  980. Assert.AreEqual (0, cmbbox.SelectedIndex, "#H3");
  981. cmbbox.Text = "BAD";
  982. Assert.IsNotNull (cmbbox.Text, "#I1");
  983. Assert.AreEqual ("BAD", cmbbox.Text, "#I2");
  984. Assert.AreEqual (4, cmbbox.SelectedIndex, "#I3");
  985. cmbbox.Text = "BAD";
  986. Assert.IsNotNull (cmbbox.Text, "#J1");
  987. Assert.AreEqual ("BAD", cmbbox.Text, "#J2");
  988. Assert.AreEqual (4, cmbbox.SelectedIndex, "#J3");
  989. cmbbox.Text = "baD";
  990. Assert.IsNotNull (cmbbox.Text, "#K1");
  991. Assert.AreEqual ("Bad", cmbbox.Text, "#K2");
  992. Assert.AreEqual (1, cmbbox.SelectedIndex, "#K3");
  993. cmbbox.SelectedIndex = -1;
  994. cmbbox.Text = "E";
  995. Assert.IsNotNull (cmbbox.Text, "#L1");
  996. Assert.AreEqual ("E", cmbbox.Text, "#L2");
  997. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#L3");
  998. cmbbox.Text = "iN";
  999. Assert.IsNotNull (cmbbox.Text, "#M1");
  1000. Assert.AreEqual ("iN", cmbbox.Text, "#M2");
  1001. Assert.AreEqual (5, cmbbox.SelectedIndex, "#M3");
  1002. cmbbox.Text = "In";
  1003. Assert.IsNotNull (cmbbox.Text, "#N1");
  1004. Assert.AreEqual ("IN", cmbbox.Text, "#N2");
  1005. Assert.AreEqual (2, cmbbox.SelectedIndex, "#N3");
  1006. cmbbox.Text = "Badd";
  1007. Assert.IsNotNull (cmbbox.Text, "#O1");
  1008. Assert.AreEqual ("Badd", cmbbox.Text, "#O2");
  1009. Assert.AreEqual (2, cmbbox.SelectedIndex, "#O3");
  1010. cmbbox.SelectedIndex = -1;
  1011. Assert.IsNotNull (cmbbox.Text, "#P1");
  1012. Assert.AreEqual (string.Empty, cmbbox.Text, "#P2");
  1013. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#P3");
  1014. cmbbox.Text = "Something";
  1015. Assert.IsNotNull (cmbbox.Text, "#Q1");
  1016. Assert.AreEqual ("Something", cmbbox.Text, "#Q2");
  1017. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#Q3");
  1018. cmbbox.SelectedIndex = -1;
  1019. Assert.IsNotNull (cmbbox.Text, "#R1");
  1020. Assert.AreEqual ("Something", cmbbox.Text, "#R2");
  1021. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#R3");
  1022. cmbbox.Text = null;
  1023. Assert.IsNotNull (cmbbox.Text, "#S1");
  1024. Assert.AreEqual (string.Empty, cmbbox.Text, "#S2");
  1025. Assert.AreEqual (-1, cmbbox.SelectedIndex, "#S3");
  1026. }
  1027. [Test] // bug 360862
  1028. public void SizeChangesAtCreateHandle ()
  1029. {
  1030. ComboBox cb = new ComboBox ();
  1031. cb.Font = new Font ("Arial", 24f);
  1032. int original = cb.Height;
  1033. IntPtr h = cb.Handle;
  1034. Assert.IsTrue (cb.Height > original, string.Format ("ComboBox height ({0}) should be bigger than original ({1})", cb.Height, original));
  1035. }
  1036. [Test]
  1037. public void Bug424270 ()
  1038. {
  1039. ComboBox cb = new ComboBox ();
  1040. cb.Items.Add ("ab");
  1041. cb.SelectedIndex = 0;
  1042. Assert.AreEqual (0, cb.SelectedIndex, "A1");
  1043. Assert.AreEqual ("ab", cb.SelectedItem, "A2");
  1044. cb.SelectedItem = null;
  1045. Assert.AreEqual (-1, cb.SelectedIndex, "A3");
  1046. Assert.AreEqual (null, cb.SelectedItem, "A4");
  1047. }
  1048. #if NET_2_0
  1049. [Test]
  1050. public void BehaviorAutoSize ()
  1051. {
  1052. if (TestHelper.RunningOnUnix)
  1053. Assert.Ignore ("Depends on font measurements, corresponds to windows");
  1054. Form f = new Form ();
  1055. f.ShowInTaskbar = false;
  1056. f.Show ();
  1057. Image i = new Bitmap (20, 20);
  1058. String s = "My test string";
  1059. CheckBox b = new CheckBox ();
  1060. Size s_size = TextRenderer.MeasureText (s, b.Font);
  1061. b.UseCompatibleTextRendering = false;
  1062. b.Size = new Size (5, 5);
  1063. b.AutoSize = true;
  1064. b.Text = s;
  1065. f.Controls.Add (b);
  1066. // Text only
  1067. b.TextImageRelation = TextImageRelation.Overlay;
  1068. Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A1");
  1069. b.TextImageRelation = TextImageRelation.ImageAboveText;
  1070. b.Size = new Size (5, 5);
  1071. Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A2");
  1072. b.TextImageRelation = TextImageRelation.ImageBeforeText;
  1073. b.Size = new Size (5, 5);
  1074. Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A3");
  1075. b.TextImageRelation = TextImageRelation.TextAboveImage;
  1076. b.Size = new Size (5, 5);
  1077. Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A4");
  1078. b.TextImageRelation = TextImageRelation.TextBeforeImage;
  1079. b.Size = new Size (5, 5);
  1080. Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + 4), b.Size, "A5");
  1081. // Text and Image
  1082. b.Image = i;
  1083. b.TextImageRelation = TextImageRelation.Overlay;
  1084. b.Size = new Size (5, 5);
  1085. Assert.AreEqual (new Size (s_size.Width + 19, i.Height), b.Size, "A6");
  1086. b.TextImageRelation = TextImageRelation.ImageAboveText;
  1087. b.Size = new Size (5, 5);
  1088. Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A7");
  1089. b.TextImageRelation = TextImageRelation.ImageBeforeText;
  1090. b.Size = new Size (5, 5);
  1091. Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A8");
  1092. b.TextImageRelation = TextImageRelation.TextAboveImage;
  1093. b.Size = new Size (5, 5);
  1094. Assert.AreEqual (new Size (s_size.Width + 19, s_size.Height + i.Height + 4), b.Size, "A9");
  1095. b.TextImageRelation = TextImageRelation.TextBeforeImage;
  1096. b.Size = new Size (5, 5);
  1097. Assert.AreEqual (new Size (s_size.Width + i.Width + 19, i.Height), b.Size, "A10");
  1098. // Image only
  1099. b.Text = string.Empty;
  1100. b.TextImageRelation = TextImageRelation.Overlay;
  1101. b.Size = new Size (5, 5);
  1102. Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A11");
  1103. b.TextImageRelation = TextImageRelation.ImageAboveText;
  1104. b.Size = new Size (5, 5);
  1105. Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A12");
  1106. b.TextImageRelation = TextImageRelation.ImageBeforeText;
  1107. b.Size = new Size (5, 5);
  1108. Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A13");
  1109. b.TextImageRelation = TextImageRelation.TextAboveImage;
  1110. b.Size = new Size (5, 5);
  1111. Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A14");
  1112. b.TextImageRelation = TextImageRelation.TextBeforeImage;
  1113. b.Size = new Size (5, 5);
  1114. Assert.AreEqual (new Size (i.Height + 15, i.Height), b.Size, "A15");
  1115. // Neither
  1116. b.Image = null;
  1117. b.TextImageRelation = TextImageRelation.Overlay;
  1118. b.Size = new Size (5, 5);
  1119. Assert.AreEqual (new Size (15, 14), b.Size, "A16");
  1120. b.TextImageRelation = TextImageRelation.ImageAboveText;
  1121. b.Size = new Size (5, 5);
  1122. Assert.AreEqual (new Size (15, 14), b.Size, "A17");
  1123. b.TextImageRelation = TextImageRelation.ImageBeforeText;
  1124. b.Size = new Size (5, 5);
  1125. Assert.AreEqual (new Size (15, 14), b.Size, "A18");
  1126. b.TextImageRelation = TextImageRelation.TextAboveImage;
  1127. b.Size = new Size (5, 5);
  1128. Assert.AreEqual (new Size (15, 14), b.Size, "A19");
  1129. b.TextImageRelation = TextImageRelation.TextBeforeImage;
  1130. b.Size = new Size (5, 5);
  1131. Assert.AreEqual (new Size (15, 14), b.Size, "A20");
  1132. // Padding
  1133. b.Padding = new Padding (5, 10, 15, 20);
  1134. Assert.AreEqual (new Size (15 + b.Padding.Horizontal, 14 + b.Padding.Vertical), b.Size, "A21");
  1135. f.Dispose ();
  1136. }
  1137. [Test]
  1138. public void MethodScaleControl ()
  1139. {
  1140. Form f = new Form ();
  1141. f.ShowInTaskbar = false;
  1142. f.Show ();
  1143. PublicComboBox gb = new PublicComboBox ();
  1144. gb.Location = new Point (5, 10);
  1145. f.Controls.Add (gb);
  1146. Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A1");
  1147. gb.PublicScaleControl (new SizeF (2.0f, 2.0f), BoundsSpecified.All);
  1148. Assert.AreEqual (new Rectangle (10, 20, 238, gb.PreferredHeight), gb.Bounds, "A2");
  1149. gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Location);
  1150. Assert.AreEqual (new Rectangle (5, 10, 238, gb.PreferredHeight), gb.Bounds, "A3");
  1151. gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Size);
  1152. Assert.AreEqual (new Rectangle (5, 10, 121, gb.PreferredHeight), gb.Bounds, "A4");
  1153. gb.PublicScaleControl (new SizeF (3.5f, 3.5f), BoundsSpecified.Size);
  1154. Assert.AreEqual (new Rectangle (5, 10, 414, gb.PreferredHeight), gb.Bounds, "A5");
  1155. gb.PublicScaleControl (new SizeF (2.5f, 2.5f), BoundsSpecified.Size);
  1156. Assert.AreEqual (new Rectangle (5, 10, 1029, gb.PreferredHeight), gb.Bounds, "A6");
  1157. gb.PublicScaleControl (new SizeF (.2f, .2f), BoundsSpecified.Size);
  1158. Assert.AreEqual (new Rectangle (5, 10, 209, gb.PreferredHeight), gb.Bounds, "A7");
  1159. f.Dispose ();
  1160. }
  1161. private class PublicComboBox : ComboBox
  1162. {
  1163. public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
  1164. {
  1165. base.ScaleControl (factor, specified);
  1166. }
  1167. }
  1168. #endif
  1169. private struct ComboVal
  1170. {
  1171. public string text;
  1172. public ComboVal (string t)
  1173. {
  1174. text = t;
  1175. }
  1176. public override string ToString ()
  1177. {
  1178. return text;
  1179. }
  1180. }
  1181. [Test]
  1182. public void SortTest ()
  1183. {
  1184. // Test sorting of objects with no IComparer
  1185. // should not crash
  1186. ComboBox cmb = new ComboBox ();
  1187. cmb.Items.Add (new ComboVal ("B"));
  1188. cmb.Items.Add (new ComboVal ("A"));
  1189. cmb.Sorted = true;
  1190. }
  1191. }
  1192. [TestFixture]
  1193. public class ComboBoxObjectCollectionTest : TestHelper
  1194. {
  1195. [Test]
  1196. public void ComboBoxObjectCollectionPropertyTest ()
  1197. {
  1198. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1199. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  1200. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  1201. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  1202. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  1203. }
  1204. [Test]
  1205. public void AddTest ()
  1206. {
  1207. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1208. col.Add ("Item1");
  1209. col.Add ("Item2");
  1210. Assert.AreEqual (2, col.Count, "#C1");
  1211. }
  1212. [Test]
  1213. public void Add_Null ()
  1214. {
  1215. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1216. try {
  1217. col.Add (null);
  1218. Assert.Fail ("#1");
  1219. } catch (ArgumentNullException ex) {
  1220. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1221. Assert.IsNull (ex.InnerException, "#3");
  1222. Assert.IsNotNull (ex.Message, "#4");
  1223. Assert.IsNotNull (ex.ParamName, "#5");
  1224. Assert.AreEqual ("item", ex.ParamName, "#6");
  1225. }
  1226. }
  1227. [Test]
  1228. public void AddRange_Null ()
  1229. {
  1230. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1231. try {
  1232. col.AddRange (null);
  1233. Assert.Fail ("#1");
  1234. } catch (ArgumentNullException ex) {
  1235. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1236. Assert.IsNull (ex.InnerException, "#3");
  1237. Assert.IsNotNull (ex.Message, "#4");
  1238. Assert.IsNotNull (ex.ParamName, "#5");
  1239. Assert.AreEqual ("items", ex.ParamName, "#6");
  1240. }
  1241. }
  1242. [Test]
  1243. public void ClearTest ()
  1244. {
  1245. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1246. col.Add ("Item1");
  1247. col.Add ("Item2");
  1248. col.Clear ();
  1249. Assert.AreEqual (0, col.Count, "#D1");
  1250. }
  1251. [Test]
  1252. public void ContainsTest ()
  1253. {
  1254. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1255. object obj = "Item1";
  1256. col.Add (obj);
  1257. Assert.AreEqual (true, col.Contains ("Item1"), "#E1");
  1258. Assert.AreEqual (false, col.Contains ("Item2"), "#E2");
  1259. }
  1260. [Test]
  1261. public void Contains_Null ()
  1262. {
  1263. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1264. try {
  1265. col.Contains (null);
  1266. Assert.Fail ("#1");
  1267. } catch (ArgumentNullException ex) {
  1268. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1269. Assert.IsNull (ex.InnerException, "#3");
  1270. Assert.IsNotNull (ex.Message, "#4");
  1271. Assert.IsNotNull (ex.ParamName, "#5");
  1272. #if NET_2_0
  1273. Assert.AreEqual ("value", ex.ParamName, "#6");
  1274. #endif
  1275. }
  1276. }
  1277. [Test]
  1278. public void IndexOfTest ()
  1279. {
  1280. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1281. col.Add ("Item1");
  1282. col.Add ("Item2");
  1283. Assert.AreEqual (1, col.IndexOf ("Item2"), "#F1");
  1284. }
  1285. [Test]
  1286. public void IndexOf_Null ()
  1287. {
  1288. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1289. try {
  1290. col.IndexOf (null);
  1291. Assert.Fail ("#1");
  1292. } catch (ArgumentNullException ex) {
  1293. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1294. Assert.IsNull (ex.InnerException, "#3");
  1295. Assert.IsNotNull (ex.Message, "#4");
  1296. Assert.IsNotNull (ex.ParamName, "#5");
  1297. #if NET_2_0
  1298. Assert.AreEqual ("value", ex.ParamName, "#6");
  1299. #endif
  1300. }
  1301. }
  1302. [Test]
  1303. public void Insert_Null ()
  1304. {
  1305. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1306. col.Add ("Item1");
  1307. try {
  1308. col.Insert (0, null);
  1309. Assert.Fail ("#1");
  1310. } catch (ArgumentNullException ex) {
  1311. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1312. Assert.IsNull (ex.InnerException, "#3");
  1313. Assert.IsNotNull (ex.Message, "#4");
  1314. Assert.IsNotNull (ex.ParamName, "#5");
  1315. Assert.AreEqual ("item", ex.ParamName, "#6");
  1316. }
  1317. }
  1318. [Test]
  1319. public void RemoveTest ()
  1320. {
  1321. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1322. col.Add ("Item1");
  1323. col.Add ("Item2");
  1324. col.Remove ("Item1");
  1325. Assert.AreEqual (1, col.Count, "#1");
  1326. col.Remove (null);
  1327. Assert.AreEqual (1, col.Count, "#2");
  1328. }
  1329. [Test]
  1330. public void RemoveAtTest ()
  1331. {
  1332. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1333. col.Add ("Item1");
  1334. col.Add ("Item2");
  1335. col.RemoveAt (0);
  1336. Assert.AreEqual (1, col.Count, "#H1");
  1337. Assert.AreEqual (true, col.Contains ("Item2"), "#H1");
  1338. }
  1339. [Test]
  1340. public void Indexer_Null ()
  1341. {
  1342. ComboBox.ObjectCollection col = new ComboBox.ObjectCollection (new ComboBox ());
  1343. col.Add ("Item1");
  1344. try {
  1345. col [0] = null;
  1346. Assert.Fail ("#1");
  1347. } catch (ArgumentNullException ex) {
  1348. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1349. Assert.IsNull (ex.InnerException, "#3");
  1350. Assert.IsNotNull (ex.Message, "#4");
  1351. Assert.IsNotNull (ex.ParamName, "#5");
  1352. Assert.AreEqual ("value", ex.ParamName, "#6");
  1353. }
  1354. }
  1355. }
  1356. [TestFixture]
  1357. public class ComboBoxTests : TestHelper
  1358. {
  1359. ComboBox comboBox;
  1360. bool textChanged, layoutUpdated;
  1361. [SetUp]
  1362. protected override void SetUp () {
  1363. comboBox = new ComboBox ();
  1364. textChanged = false;
  1365. layoutUpdated = false;
  1366. comboBox.TextChanged += new EventHandler (textChangedEventHandler);
  1367. comboBox.Layout += new LayoutEventHandler (layoutEventHandler);
  1368. base.SetUp ();
  1369. }
  1370. private void textChangedEventHandler (object sender, EventArgs e)
  1371. {
  1372. textChanged = true;
  1373. }
  1374. private void layoutEventHandler (object sender, LayoutEventArgs e)
  1375. {
  1376. layoutUpdated = true;
  1377. }
  1378. [Test]
  1379. public void InitialPropertyValues ()
  1380. {
  1381. Assert.AreEqual (String.Empty, comboBox.Text);
  1382. Assert.AreEqual (-1, comboBox.SelectedIndex);
  1383. Assert.IsNull (comboBox.SelectedItem);
  1384. Assert.AreEqual (121, comboBox.Size.Width);
  1385. //Note: it is environment dependent
  1386. //Assert.AreEqual(20, comboBox.Size.Height);
  1387. Assert.IsFalse (textChanged);
  1388. Assert.IsFalse (layoutUpdated);
  1389. }
  1390. [Test]
  1391. public void SetNegativeOneSelectedIndex ()
  1392. {
  1393. comboBox.SelectedIndex = -1;
  1394. Assert.AreEqual (String.Empty, comboBox.Text);
  1395. Assert.IsFalse (textChanged);
  1396. }
  1397. [Test]
  1398. public void SetDifferentText ()
  1399. {
  1400. comboBox.Text = "foooooooooooooooooooooooooo";
  1401. Assert.IsTrue (textChanged);
  1402. Assert.IsFalse (layoutUpdated);
  1403. }
  1404. [Test]
  1405. public void SetSameText ()
  1406. {
  1407. comboBox.Text = String.Empty;
  1408. Assert.IsFalse (textChanged);
  1409. Assert.IsFalse (layoutUpdated);
  1410. }
  1411. [Test] // bug #79812
  1412. public void Add_Item_NonString ()
  1413. {
  1414. comboBox.Sorted = true;
  1415. comboBox.Items.Add (new Person ("B"));
  1416. comboBox.Items.Add (new Person ("A"));
  1417. comboBox.Items.Add (new Person ("C"));
  1418. Assert.AreEqual (string.Empty, comboBox.Text, "#1");
  1419. comboBox.SelectedIndex = 0;
  1420. Assert.AreEqual ("A", comboBox.Text, "#2");
  1421. comboBox.SelectedIndex = 2;
  1422. Assert.AreEqual ("C", comboBox.Text, "#3");
  1423. }
  1424. [Test]
  1425. [Category ("NotWorking")]
  1426. public void SelectedText ()
  1427. {
  1428. Form form = new Form ();
  1429. form.ShowInTaskbar = false;
  1430. form.Visible = false;
  1431. form.Controls.Add (comboBox);
  1432. comboBox.Items.Add ("Bar");
  1433. comboBox.Items.Add ("Foo");
  1434. Assert.AreEqual (-1, comboBox.SelectedIndex, "#A1");
  1435. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#A2");
  1436. comboBox.SelectedIndex = 0;
  1437. Assert.AreEqual (0, comboBox.SelectedIndex, "#B1");
  1438. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#B2");
  1439. form.Show ();
  1440. Assert.AreEqual (0, comboBox.SelectedIndex, "#C1");
  1441. Assert.AreEqual ("Bar", comboBox.SelectedText, "#C2");
  1442. comboBox.SelectedIndex = 1;
  1443. Assert.AreEqual (1, comboBox.SelectedIndex, "#D1");
  1444. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#D2");
  1445. comboBox.SelectedText = "Ba";
  1446. Assert.AreEqual (-1, comboBox.SelectedIndex, "#E1");
  1447. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#E2");
  1448. comboBox.SelectedText = "Bar";
  1449. Assert.AreEqual (-1, comboBox.SelectedIndex, "#F1");
  1450. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#F2");
  1451. comboBox.SelectedText = "doesnotexist";
  1452. Assert.AreEqual (-1, comboBox.SelectedIndex, "#G1");
  1453. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#G2");
  1454. comboBox.SelectedIndex = 0;
  1455. Assert.AreEqual (0, comboBox.SelectedIndex, "#H1");
  1456. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#H2");
  1457. comboBox.SelectedText = "Foo";
  1458. Assert.AreEqual (-1, comboBox.SelectedIndex, "#I1");
  1459. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#I2");
  1460. }
  1461. [Test]
  1462. public void SortedSelectedItem ()
  1463. {
  1464. using (Form form = new Form ()) {
  1465. ComboBox box = new ComboBox ();
  1466. box.Sorted = true;
  1467. form.Controls.Add (box);
  1468. form.ShowInTaskbar = false;
  1469. form.Show ();
  1470. box.Items.Add ("English");
  1471. box.SelectedItem = "English";
  1472. box.Items.Add ("Danish");
  1473. Assert.AreEqual ("English", box.SelectedItem, "A1");
  1474. box.Items.Add ("French");
  1475. Assert.AreEqual ("English", box.SelectedItem, "A2");
  1476. box.Items.Add ("Brazilian Portuguese");
  1477. box.Items.Add ("Arabic");
  1478. Assert.AreEqual ("English", box.SelectedItem, "A3");
  1479. }
  1480. }
  1481. public class Person
  1482. {
  1483. private readonly string _name;
  1484. public Person (string name)
  1485. {
  1486. _name = name;
  1487. }
  1488. public string Name
  1489. {
  1490. get
  1491. {
  1492. return _name;
  1493. }
  1494. }
  1495. public override string ToString ()
  1496. {
  1497. return Name;
  1498. }
  1499. }
  1500. }
  1501. }