PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ListBoxTest.cs

https://bitbucket.org/danipen/mono
C# | 1661 lines | 1374 code | 207 blank | 80 comment | 7 complexity | 16c0c0a3fefc697d3661736d2d35182e MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  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 <mritvik@novell.com>
  27. // Jordi Mas i Hernandez <jordi@ximian.com>
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.ComponentModel;
  32. using System.Drawing;
  33. using System.Reflection;
  34. using System.Windows.Forms;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Windows.Forms
  37. {
  38. [TestFixture]
  39. public class ListBoxTest : TestHelper
  40. {
  41. ListBox listBox;
  42. Form form;
  43. [SetUp]
  44. protected override void SetUp ()
  45. {
  46. listBox = new ListBox();
  47. form = new Form();
  48. form.ShowInTaskbar = false;
  49. base.SetUp ();
  50. }
  51. [TearDown]
  52. protected override void TearDown ()
  53. {
  54. form.Dispose ();
  55. base.TearDown ();
  56. }
  57. [Test] // bug #465422
  58. public void RemoveLast ()
  59. {
  60. listBox.Items.Clear ();
  61. for (int i = 0; i < 3; i++)
  62. listBox.Items.Add (i.ToString ());
  63. // need to create control to actually test the invalidation
  64. listBox.CreateControl ();
  65. // select last - then remove an item that is *not* the last,
  66. // so basically the selection is invalidated implicitly
  67. listBox.SelectedIndex = 2;
  68. listBox.Items.RemoveAt (1);
  69. Assert.AreEqual (1, listBox.SelectedIndex, "#A1");
  70. listBox.SelectedIndex = 0;
  71. Assert.AreEqual (0, listBox.SelectedIndex, "#B1");
  72. //
  73. // MultiSelection
  74. //
  75. listBox.ClearSelected ();
  76. listBox.Items.Clear ();
  77. for (int i = 0; i < 3; i++)
  78. listBox.Items.Add (i.ToString ());
  79. listBox.SelectionMode = SelectionMode.MultiSimple;
  80. listBox.SetSelected (0, true);
  81. listBox.SetSelected (2, true);
  82. Assert.AreEqual (true, listBox.GetSelected (0), "#C1");
  83. Assert.AreEqual (false, listBox.GetSelected (1), "#C2");
  84. Assert.AreEqual (true, listBox.GetSelected (2), "#C3");
  85. listBox.Items.RemoveAt (2);
  86. Assert.AreEqual (true, listBox.GetSelected (0), "#D1");
  87. Assert.AreEqual (false, listBox.GetSelected (1), "#D2");
  88. Assert.AreEqual (1, listBox.SelectedIndices.Count, "#D3");
  89. }
  90. [Test]
  91. public void ListBoxPropertyTest ()
  92. {
  93. Assert.AreEqual (0, listBox.ColumnWidth, "#1");
  94. Assert.AreEqual (DrawMode.Normal, listBox.DrawMode, "#2");
  95. Assert.AreEqual (0, listBox.HorizontalExtent, "#3");
  96. Assert.AreEqual (false, listBox.HorizontalScrollbar, "#4");
  97. Assert.AreEqual (true, listBox.IntegralHeight, "#5");
  98. //Assert.AreEqual (13, listBox.ItemHeight, "#6"); // Note: Item height depends on the current font.
  99. listBox.Items.Add ("a");
  100. listBox.Items.Add ("b");
  101. listBox.Items.Add ("c");
  102. Assert.AreEqual (3, listBox.Items.Count, "#7");
  103. Assert.AreEqual (false, listBox.MultiColumn, "#8");
  104. //Assert.AreEqual (46, listBox.PreferredHeight, "#9"); // Note: Item height depends on the current font.
  105. //Assert.AreEqual (RightToLeft.No , listBox.RightToLeft, "#10"); // Depends on Windows version
  106. Assert.AreEqual (false, listBox.ScrollAlwaysVisible, "#11");
  107. Assert.AreEqual (-1, listBox.SelectedIndex, "#12");
  108. listBox.SetSelected (2,true);
  109. Assert.AreEqual (2, listBox.SelectedIndices[0], "#13");
  110. Assert.AreEqual ("c", listBox.SelectedItem, "#14");
  111. Assert.AreEqual ("c", listBox.SelectedItems[0], "#15");
  112. Assert.AreEqual (SelectionMode.One, listBox.SelectionMode, "#16");
  113. listBox.SetSelected (2,false);
  114. Assert.AreEqual (false, listBox.Sorted, "#17");
  115. Assert.AreEqual ("", listBox.Text, "#18");
  116. Assert.AreEqual (0, listBox.TopIndex, "#19");
  117. Assert.AreEqual (true, listBox.UseTabStops, "#20");
  118. }
  119. [Test]
  120. public void BeginEndUpdateTest ()
  121. {
  122. form.Visible = true;
  123. listBox.Items.Add ("A");
  124. listBox.Visible = true;
  125. form.Controls.Add (listBox);
  126. listBox.BeginUpdate ();
  127. for (int x = 1; x < 5000; x++)
  128. {
  129. listBox.Items.Add ("Item " + x.ToString ());
  130. }
  131. listBox.EndUpdate ();
  132. listBox.SetSelected (1, true);
  133. listBox.SetSelected (3, true);
  134. Assert.AreEqual (true, listBox.SelectedItems.Contains ("Item 3"), "#21");
  135. }
  136. [Test]
  137. public void ClearSelectedTest ()
  138. {
  139. form.Visible = true;
  140. listBox.Items.Add ("A");
  141. listBox.Visible = true;
  142. form.Controls.Add (listBox);
  143. listBox.SetSelected (0, true);
  144. Assert.AreEqual ("A", listBox.SelectedItems [0].ToString (),"#22");
  145. listBox.ClearSelected ();
  146. Assert.AreEqual (0, listBox.SelectedItems.Count,"#23");
  147. }
  148. [Test] // bug #80620
  149. [NUnit.Framework.Category ("NotWorking")]
  150. public void ClientRectangle_Borders ()
  151. {
  152. // This test is invalid because createcontrol forces .net to resize
  153. // the listbox using integralheight, which defaults to true. This
  154. // will only hold for most font sizes.
  155. listBox.CreateControl ();
  156. Assert.AreEqual (listBox.ClientRectangle, new ListBox ().ClientRectangle);
  157. }
  158. [Ignore ("It depends on user system settings")]
  159. public void GetItemHeightTest ()
  160. {
  161. listBox.Visible = true;
  162. form.Controls.Add (listBox);
  163. listBox.Items.Add ("A");
  164. Assert.AreEqual (13, listBox.GetItemHeight (0) , "#28");
  165. }
  166. [Ignore ("It depends on user system settings")]
  167. public void GetItemRectangleTest ()
  168. {
  169. form.Visible = true;
  170. listBox.Visible = true;
  171. form.Controls.Add (listBox);
  172. listBox.Items.Add ("A");
  173. Assert.AreEqual (new Rectangle(0,0,116,13), listBox.GetItemRectangle (0), "#29");
  174. }
  175. [Test]
  176. public void GetSelectedTest ()
  177. {
  178. listBox.Items.Add ("A");
  179. listBox.Items.Add ("B");
  180. listBox.Items.Add ("C");
  181. listBox.Items.Add ("D");
  182. listBox.SelectionMode = SelectionMode.MultiSimple;
  183. listBox.Sorted = true;
  184. listBox.SetSelected (0,true);
  185. listBox.SetSelected (2,true);
  186. listBox.TopIndex=0;
  187. Assert.AreEqual (true, listBox.GetSelected (0), "#30");
  188. listBox.SetSelected (2,false);
  189. Assert.AreEqual (false, listBox.GetSelected (2), "#31");
  190. }
  191. [Test]
  192. public void IndexFromPointTest ()
  193. {
  194. listBox.Items.Add ("A");
  195. Point pt = new Point (100,100);
  196. listBox.IndexFromPoint (pt);
  197. Assert.AreEqual (-1, listBox.IndexFromPoint (100,100), "#32");
  198. }
  199. [Test]
  200. public void FindStringTest ()
  201. {
  202. listBox.FindString ("Hola", -5); // No exception, it's empty
  203. int x = listBox.FindString ("Hello");
  204. Assert.AreEqual (-1, x, "#19");
  205. listBox.Items.AddRange(new object[] {"ACBD", "ABDC", "ACBD", "ABCD"});
  206. String myString = "ABC";
  207. x = listBox.FindString (myString);
  208. Assert.AreEqual (3, x, "#191");
  209. x = listBox.FindString (string.Empty);
  210. Assert.AreEqual (0, x, "#192");
  211. x = listBox.FindString ("NonExistant");
  212. Assert.AreEqual (-1, x, "#193");
  213. x = listBox.FindString ("A", -1);
  214. Assert.AreEqual (0, x, "#194");
  215. x = listBox.FindString ("A", 0);
  216. Assert.AreEqual (1, x, "#195");
  217. x = listBox.FindString ("A", listBox.Items.Count - 1);
  218. Assert.AreEqual (0, x, "#196");
  219. x = listBox.FindString ("a", listBox.Items.Count - 1);
  220. Assert.AreEqual (0, x, "#197");
  221. }
  222. [Test]
  223. public void FindStringExactTest ()
  224. {
  225. listBox.FindStringExact ("Hola", -5); // No exception, it's empty
  226. int x = listBox.FindStringExact ("Hello");
  227. Assert.AreEqual (-1, x, "#20");
  228. listBox.Items.AddRange (new object[] {"ABCD","ABC","ABDC"});
  229. String myString = "ABC";
  230. x = listBox.FindStringExact (myString);
  231. Assert.AreEqual (1, x, "#201");
  232. x = listBox.FindStringExact (string.Empty);
  233. Assert.AreEqual (-1, x, "#202");
  234. x = listBox.FindStringExact ("NonExistant");
  235. Assert.AreEqual (-1, x, "#203");
  236. x = listBox.FindStringExact ("ABCD", -1);
  237. Assert.AreEqual (0, x, "#204");
  238. x = listBox.FindStringExact ("ABC", 0);
  239. Assert.AreEqual (1, x, "#205");
  240. x = listBox.FindStringExact ("ABC", listBox.Items.Count - 1);
  241. Assert.AreEqual (1, x, "#206");
  242. x = listBox.FindStringExact ("abcd", listBox.Items.Count - 1);
  243. Assert.AreEqual (0, x, "#207");
  244. }
  245. #if NET_2_0
  246. [Test]
  247. public void AllowSelection ()
  248. {
  249. MockListBox lb = new MockListBox ();
  250. lb.SelectionMode = SelectionMode.None;
  251. Assert.IsFalse (lb.allow_selection, "#1");
  252. lb.SelectionMode = SelectionMode.One;
  253. Assert.IsTrue (lb.allow_selection, "#2");
  254. }
  255. #endif
  256. //
  257. // Exceptions
  258. //
  259. [Test]
  260. [ExpectedException (typeof (InvalidEnumArgumentException))]
  261. public void BorderStyleException ()
  262. {
  263. listBox.BorderStyle = (BorderStyle) 10;
  264. }
  265. [Test]
  266. [ExpectedException (typeof (ArgumentException))]
  267. public void ColumnWidthException ()
  268. {
  269. listBox.ColumnWidth = -1;
  270. }
  271. [Test]
  272. [ExpectedException (typeof (InvalidEnumArgumentException))]
  273. public void DrawModeException ()
  274. {
  275. listBox.DrawMode = (DrawMode) 10;
  276. }
  277. [Test]
  278. [ExpectedException (typeof (ArgumentException))]
  279. public void DrawModeAndMultiColumnException ()
  280. {
  281. listBox.MultiColumn = true;
  282. listBox.DrawMode = DrawMode.OwnerDrawVariable;
  283. }
  284. [Test]
  285. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  286. public void ItemHeightException ()
  287. {
  288. listBox.ItemHeight = 256;
  289. }
  290. [Test] // bug #80696
  291. public void SelectedIndex_Created ()
  292. {
  293. Form form = new Form ();
  294. ListBox listBox = new ListBox ();
  295. listBox.Items.Add ("A");
  296. listBox.Items.Add ("B");
  297. form.Controls.Add (listBox);
  298. form.Show ();
  299. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  300. listBox.SelectedIndex = 0;
  301. Assert.AreEqual (0, listBox.SelectedIndex, "#2");
  302. listBox.SelectedIndex = -1;
  303. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  304. listBox.SelectedIndex = 1;
  305. Assert.AreEqual (1, listBox.SelectedIndex, "#4");
  306. form.Close ();
  307. }
  308. [Test] // bug #80753
  309. public void SelectedIndex_NotCreated ()
  310. {
  311. ListBox listBox = new ListBox ();
  312. listBox.Items.Add ("A");
  313. listBox.Items.Add ("B");
  314. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  315. listBox.SelectedIndex = 0;
  316. Assert.AreEqual (0, listBox.SelectedIndex, "#2");
  317. listBox.SelectedIndex = -1;
  318. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  319. listBox.SelectedIndex = 1;
  320. Assert.AreEqual (1, listBox.SelectedIndex, "#4");
  321. }
  322. #if NET_2_0
  323. [Test]
  324. public void SelectedIndex_Removed ()
  325. {
  326. ListBox listBox = new ListBox ();
  327. listBox.Items.Add ("A");
  328. listBox.Items.Add ("B");
  329. listBox.Items.Add ("C");
  330. Assert.AreEqual (-1, listBox.SelectedIndex, "#1");
  331. listBox.SelectedIndex = 2;
  332. Assert.AreEqual (2, listBox.SelectedIndex, "#2");
  333. listBox.Items.RemoveAt (2);
  334. Assert.AreEqual (-1, listBox.SelectedIndex, "#3");
  335. listBox.SelectedIndex = 0;
  336. Assert.AreEqual (0, listBox.SelectedIndex, "#4");
  337. listBox.Items.RemoveAt (0);
  338. Assert.AreEqual (-1, listBox.SelectedIndex, "#5");
  339. }
  340. // This should also apply to MultiSimple selection mode
  341. [Test]
  342. public void Selection_MultiExtended ()
  343. {
  344. listBox.Items.Add ("A");
  345. listBox.Items.Add ("B");
  346. listBox.Items.Add ("C");
  347. listBox.Items.Add ("D");
  348. listBox.SelectionMode = SelectionMode.MultiExtended;
  349. //
  350. // First part: test the order of SelectedItems as well
  351. // as SelectedIndex when more than one item is selected
  352. //
  353. listBox.SelectedItems.Add ("D");
  354. listBox.SelectedItems.Add ("B");
  355. Assert.AreEqual (1, listBox.SelectedIndex, "#A1");
  356. Assert.AreEqual (2, listBox.SelectedItems.Count, "#A2");
  357. Assert.AreEqual ("B", listBox.SelectedItems [0], "#A3");
  358. Assert.AreEqual ("D", listBox.SelectedItems [1], "#A4");
  359. listBox.SelectedItems.Add ("C");
  360. Assert.AreEqual (1, listBox.SelectedIndex, "#B1");
  361. Assert.AreEqual (3, listBox.SelectedItems.Count, "#B2");
  362. Assert.AreEqual ("B", listBox.SelectedItems [0], "#B3");
  363. Assert.AreEqual ("C", listBox.SelectedItems [1], "#B4");
  364. Assert.AreEqual ("D", listBox.SelectedItems [2], "#B5");
  365. listBox.SelectedItems.Add ("A");
  366. Assert.AreEqual (0, listBox.SelectedIndex, "#C1");
  367. Assert.AreEqual (4, listBox.SelectedItems.Count, "#C2");
  368. Assert.AreEqual ("A", listBox.SelectedItems [0], "#C3");
  369. Assert.AreEqual ("B", listBox.SelectedItems [1], "#C4");
  370. Assert.AreEqual ("C", listBox.SelectedItems [2], "#C5");
  371. Assert.AreEqual ("D", listBox.SelectedItems [3], "#C6");
  372. //
  373. // Second part: how does SelectedIndex setter work related
  374. // to SelectedItems
  375. //
  376. listBox.SelectedIndex = -1;
  377. Assert.AreEqual (-1, listBox.SelectedIndex, "#D1");
  378. Assert.AreEqual (0, listBox.SelectedItems.Count, "#D2");
  379. listBox.SelectedIndex = 3; // "D"
  380. Assert.AreEqual (3, listBox.SelectedIndex, "#E1");
  381. Assert.AreEqual (1, listBox.SelectedItems.Count, "#E2");
  382. Assert.AreEqual ("D", listBox.SelectedItems [0], "#E3");
  383. listBox.SelectedItems.Add ("B"); // index = 1
  384. Assert.AreEqual (1, listBox.SelectedIndex, "#F1");
  385. Assert.AreEqual (2, listBox.SelectedItems.Count, "#E3");
  386. Assert.AreEqual ("B", listBox.SelectedItems [0], "#E4");
  387. Assert.AreEqual ("D", listBox.SelectedItems [1], "#E5");
  388. listBox.SelectedIndex = 2;
  389. Assert.AreEqual (1, listBox.SelectedIndex, "#G1");
  390. Assert.AreEqual (3, listBox.SelectedItems.Count, "#G2");
  391. Assert.AreEqual ("B", listBox.SelectedItems [0], "#G3");
  392. Assert.AreEqual ("C", listBox.SelectedItems [1], "#G4");
  393. Assert.AreEqual ("D", listBox.SelectedItems [2], "#G5");
  394. listBox.SelectedIndex = 1; // already selected
  395. Assert.AreEqual (1, listBox.SelectedIndex, "#H1");
  396. Assert.AreEqual (3, listBox.SelectedItems.Count, "#H2");
  397. // NOTE: It seems that passing -1 does not affect the collection
  398. // in anyway (other wrong values generate an exception, however)
  399. listBox.SelectedIndices.Add (-1);
  400. Assert.AreEqual (3, listBox.SelectedItems.Count, "#J1");
  401. }
  402. [Test]
  403. public void Selection_One ()
  404. {
  405. listBox.Items.Add ("A");
  406. listBox.Items.Add ("B");
  407. listBox.Items.Add ("C");
  408. listBox.SelectionMode = SelectionMode.One;
  409. listBox.SelectedItems.Add ("B");
  410. Assert.AreEqual (1, listBox.SelectedIndex, "#A1");
  411. Assert.AreEqual (1, listBox.SelectedItems.Count, "#A2");
  412. Assert.AreEqual ("B", listBox.SelectedItems [0], "#A3");
  413. listBox.SelectedIndex = 2;
  414. Assert.AreEqual (2, listBox.SelectedIndex, "#B1");
  415. Assert.AreEqual (1, listBox.SelectedItems.Count, "#B2");
  416. Assert.AreEqual ("C", listBox.SelectedItems [0], "#B3");
  417. listBox.SelectedItems.Add ("A");
  418. Assert.AreEqual (0, listBox.SelectedIndex, "#C1");
  419. Assert.AreEqual (1, listBox.SelectedItems.Count, "#C2");
  420. Assert.AreEqual ("A", listBox.SelectedItems [0], "#C3");
  421. }
  422. [Test]
  423. public void Selection_None ()
  424. {
  425. listBox.Items.Add ("A");
  426. listBox.Items.Add ("B");
  427. listBox.SelectionMode = SelectionMode.None;
  428. try {
  429. listBox.SelectedIndex = 0;
  430. Assert.Fail ("#A");
  431. } catch (ArgumentException) {
  432. }
  433. try {
  434. listBox.SelectedIndices.Add (0);
  435. Assert.Fail ("#B");
  436. } catch (InvalidOperationException e) {
  437. Console.WriteLine (e.Message);
  438. }
  439. try {
  440. listBox.SelectedItems.Add ("A");
  441. Assert.Fail ("#C");
  442. } catch (ArgumentException) {
  443. }
  444. }
  445. #endif
  446. [Test]
  447. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  448. public void SelectedIndexException ()
  449. {
  450. listBox.SelectedIndex = -2;
  451. }
  452. [Test]
  453. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  454. public void SelectedIndexException2 ()
  455. {
  456. listBox.SelectedIndex = listBox.Items.Count;
  457. }
  458. [Test]
  459. [ExpectedException (typeof (ArgumentException))]
  460. public void SelectedIndexModeNoneException ()
  461. {
  462. listBox.SelectionMode = SelectionMode.None;
  463. listBox.SelectedIndex = -1;
  464. }
  465. [Test]
  466. [ExpectedException (typeof (InvalidEnumArgumentException))]
  467. public void SelectionModeException ()
  468. {
  469. listBox.SelectionMode = (SelectionMode) 10;
  470. }
  471. [Test]
  472. public void SelectedValueNull()
  473. {
  474. listBox.Items.Clear ();
  475. listBox.Items.Add ("A");
  476. listBox.Items.Add ("B");
  477. listBox.Items.Add ("C");
  478. listBox.Items.Add ("D");
  479. listBox.SelectedIndex = 2;
  480. listBox.SelectedValue = null;
  481. Assert.AreEqual (listBox.SelectedIndex, 2);
  482. }
  483. [Test]
  484. public void SelectedValueEmptyString()
  485. {
  486. listBox.Items.Clear ();
  487. listBox.Items.Add ("A");
  488. listBox.Items.Add ("B");
  489. listBox.Items.Add ("C");
  490. listBox.Items.Add ("D");
  491. listBox.SelectedIndex = 2;
  492. listBox.SelectedValue = null;
  493. Assert.AreEqual (listBox.SelectedIndex, 2);
  494. }
  495. [Test]
  496. public void SetItemsCore ()
  497. {
  498. MockListBox l = new MockListBox ();
  499. l.InvokeSetItemsCore (new object [] { "A", "B", "C" });
  500. Assert.AreEqual (3, l.Items.Count, "#1");
  501. Assert.AreEqual ("A", l.Items [0], "#2");
  502. Assert.AreEqual ("B", l.Items [1], "#3");
  503. Assert.AreEqual ("C", l.Items [2], "#4");
  504. }
  505. [Test]
  506. public void SetItemsCore_Item_Null ()
  507. {
  508. MockListBox l = new MockListBox ();
  509. try {
  510. l.InvokeSetItemsCore (new object [] { "A", null, "B" });
  511. Assert.Fail ("#1");
  512. } catch (ArgumentNullException ex) {
  513. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  514. Assert.IsNull (ex.InnerException, "#3");
  515. Assert.IsNotNull (ex.Message, "#4");
  516. Assert.AreEqual ("item", ex.ParamName, "#5");
  517. }
  518. #if NET_2_0
  519. Assert.AreEqual (1, l.Items.Count, "#6");
  520. Assert.AreEqual ("A", l.Items [0], "#7");
  521. #else
  522. Assert.AreEqual (0, l.Items.Count, "#6");
  523. #endif
  524. }
  525. [Test]
  526. public void SetItemsCore_Value_Null ()
  527. {
  528. MockListBox l = new MockListBox ();
  529. try {
  530. l.InvokeSetItemsCore ((IList) null);
  531. Assert.Fail ("#1");
  532. } catch (ArgumentNullException ex) {
  533. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  534. Assert.IsNull (ex.InnerException, "#3");
  535. Assert.IsNotNull (ex.Message, "#4");
  536. Assert.AreEqual ("items", ex.ParamName, "#5");
  537. }
  538. }
  539. [Test] // Bug #80466
  540. public void ListBoxHeight ()
  541. {
  542. ListBox l = new ListBox ();
  543. for (int h = 0; h < 100; h++) {
  544. l.Height = h;
  545. if (l.Height != h)
  546. Assert.Fail ("Set ListBox height of {0}, got back {1}. Should be the same.", h, l.Height);
  547. }
  548. }
  549. #if NET_2_0
  550. [Test]
  551. public void GetScaledBoundsTest ()
  552. {
  553. ScaleListBox c = new ScaleListBox ();
  554. Rectangle r = new Rectangle (100, 200, 300, 400);
  555. Assert.AreEqual (new Rectangle (200, 100, 596, 50), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.All), "A1");
  556. Assert.AreEqual (new Rectangle (200, 100, 300, 96), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Location), "A2");
  557. Assert.AreEqual (new Rectangle (100, 200, 596, 50), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Size), "A3");
  558. Assert.AreEqual (new Rectangle (100, 200, 300, 50), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.Height), "A4");
  559. Assert.AreEqual (new Rectangle (200, 200, 300, 96), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.X), "A5");
  560. Assert.AreEqual (new Rectangle (100, 200, 300, 96), c.PublicGetScaledBounds (r, new SizeF (2f, .5f), BoundsSpecified.None), "A6");
  561. Assert.AreEqual (new Rectangle (100, 200, 300, 96), c.PublicGetScaledBounds (r, new SizeF (1f, 1f), BoundsSpecified.All), "A6-2");
  562. Assert.AreEqual (new Rectangle (200, 400, 596, 188), c.PublicGetScaledBounds (r, new SizeF (2f, 2f), BoundsSpecified.All), "A7");
  563. Assert.AreEqual (new Rectangle (300, 600, 892, 280), c.PublicGetScaledBounds (r, new SizeF (3f, 3f), BoundsSpecified.All), "A8");
  564. Assert.AreEqual (new Rectangle (400, 800, 1188, 372), c.PublicGetScaledBounds (r, new SizeF (4f, 4f), BoundsSpecified.All), "A9");
  565. Assert.AreEqual (new Rectangle (50, 100, 152, 50), c.PublicGetScaledBounds (r, new SizeF (.5f, .5f), BoundsSpecified.All), "A10");
  566. }
  567. [Test]
  568. [NUnit.Framework.Category ("NotWorking")]
  569. public void MethodScaleControl ()
  570. {
  571. Form f = new Form ();
  572. f.ShowInTaskbar = false;
  573. f.Show ();
  574. ScaleListBox gb = new ScaleListBox ();
  575. gb.Location = new Point (5, 10);
  576. f.Controls.Add (gb);
  577. Assert.AreEqual (new Rectangle (5, 10, 120, 95), gb.Bounds, "A1");
  578. gb.PublicScaleControl (new SizeF (2.0f, 2.0f), BoundsSpecified.All);
  579. Assert.AreEqual (new Rectangle (10, 20, 236, 186), gb.Bounds, "A2");
  580. gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Location);
  581. Assert.AreEqual (new Rectangle (5, 10, 236, 186), gb.Bounds, "A3");
  582. gb.PublicScaleControl (new SizeF (.5f, .5f), BoundsSpecified.Size);
  583. Assert.AreEqual (new Rectangle (5, 10, 120, 95), gb.Bounds, "A4");
  584. gb.PublicScaleControl (new SizeF (3.5f, 3.5f), BoundsSpecified.Size);
  585. Assert.AreEqual (new Rectangle (5, 10, 410, 316), gb.Bounds, "A5");
  586. gb.PublicScaleControl (new SizeF (2.5f, 2.5f), BoundsSpecified.Size);
  587. Assert.AreEqual (new Rectangle (5, 10, 1019, 797), gb.Bounds, "A6");
  588. gb.PublicScaleControl (new SizeF (.2f, .2f), BoundsSpecified.Size);
  589. Assert.AreEqual (new Rectangle (5, 10, 207, 160), gb.Bounds, "A7");
  590. f.Dispose ();
  591. }
  592. private class ScaleListBox : ListBox
  593. {
  594. public Rectangle PublicGetScaledBounds (Rectangle bounds, SizeF factor, BoundsSpecified specified)
  595. {
  596. return base.GetScaledBounds (bounds, factor, specified);
  597. }
  598. public void PublicScaleControl (SizeF factor, BoundsSpecified specified)
  599. {
  600. base.ScaleControl (factor, specified);
  601. }
  602. }
  603. #endif
  604. [Test]
  605. public void MethodIsInputChar ()
  606. {
  607. // Basically, show that this method always returns true
  608. InputCharControl m = new InputCharControl ();
  609. bool result = true;
  610. for (int i = 0; i < 256; i++)
  611. result &= m.PublicIsInputChar ((char)i);
  612. Assert.AreEqual (true, result, "I1");
  613. }
  614. private class InputCharControl : ListBox
  615. {
  616. public bool PublicIsInputChar (char charCode)
  617. {
  618. return base.IsInputChar (charCode);
  619. }
  620. }
  621. [Test]
  622. public void HeightAndIntegralHeight ()
  623. {
  624. ListBox a = new ListBox();
  625. Size defaultSize = new Size(120, 96);
  626. Assert.AreEqual (defaultSize, a.Size, "A1");
  627. a.CreateControl();
  628. Assert.AreEqual (0, (a.ClientSize.Height % a.ItemHeight), "A2");
  629. a.IntegralHeight = false;
  630. Assert.AreEqual (a.Size, defaultSize, "A3");
  631. a.IntegralHeight = true;
  632. Assert.AreEqual (0, (a.ClientSize.Height % a.ItemHeight), "A4");
  633. Size clientSizeI = new Size(200, a.ItemHeight * 5);
  634. Size clientSize = clientSizeI + new Size(0, a.ItemHeight / 2);
  635. Size borderSize = new Size(a.Width - a.ClientSize.Width, a.Height - a.ClientSize.Height);
  636. Size totalSizeI = clientSizeI + borderSize;
  637. Size totalSize = clientSize + borderSize;
  638. a = new ListBox();
  639. a.ClientSize = clientSize;
  640. Assert.AreEqual (clientSize, a.ClientSize, "A5");
  641. Assert.AreEqual (totalSize, a.Size, "A6");
  642. a.IntegralHeight = false;
  643. a.IntegralHeight = true;
  644. Assert.AreEqual (clientSize, a.ClientSize, "A7");
  645. a.CreateControl();
  646. Assert.AreEqual (clientSizeI, a.ClientSize, "A8");
  647. Assert.AreEqual (totalSizeI, a.Size, "A9");
  648. a.IntegralHeight = false;
  649. Assert.AreEqual (clientSize, a.ClientSize, "A10");
  650. a.IntegralHeight = true;
  651. Assert.AreEqual (totalSizeI, a.Size, "A11");
  652. a = new ListBox();
  653. a.CreateControl();
  654. a.Size = totalSize;
  655. Assert.AreEqual (totalSizeI, a.Size, "A12");
  656. Assert.AreEqual (clientSizeI, a.ClientSize, "A13");
  657. a.IntegralHeight = false;
  658. Assert.AreEqual (totalSize, a.Size, "A14");
  659. Assert.AreEqual (clientSize, a.ClientSize, "A15");
  660. a = new ListBox();
  661. a.IntegralHeight = false;
  662. Assert.AreEqual (defaultSize, a.Size, "A16");
  663. a.CreateControl();
  664. Assert.AreEqual (defaultSize, a.Size, "A17");
  665. a = new ListBox();
  666. a.ClientSize = clientSize;
  667. a.IntegralHeight = false;
  668. Assert.AreEqual (clientSize, a.ClientSize, "A18");
  669. a.CreateControl();
  670. Assert.AreEqual (clientSize, a.ClientSize, "A19");
  671. }
  672. [Test]
  673. public void PropertyTopIndex ()
  674. {
  675. Form f = new Form ();
  676. f.ShowInTaskbar = false;
  677. f.Show ();
  678. ListBox l = new ListBox ();
  679. l.Height = 100;
  680. f.Controls.Add (l);
  681. l.Items.AddRange (new string[] { "A", "B", "C"});
  682. Assert.AreEqual (0, l.TopIndex, "A1");
  683. l.TopIndex = 2;
  684. Assert.AreEqual (0, l.TopIndex, "A2");
  685. l.Items.AddRange (new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" });
  686. Assert.AreEqual (0, l.TopIndex, "A3");
  687. l.TopIndex = 2;
  688. Assert.AreEqual (2, l.TopIndex, "A4");
  689. // There aren't items enough for 12 to be the top index, but
  690. // the actual value is font height dependent.
  691. l.TopIndex = 12;
  692. Assert.IsTrue (l.TopIndex < 12, "A5");
  693. f.Close ();
  694. f.Dispose ();
  695. }
  696. //
  697. // Events
  698. //
  699. //private bool eventFired;
  700. //private void GenericHandler (object sender, EventArgs e)
  701. //{
  702. // eventFired = true;
  703. //}
  704. public class MockListBox : ListBox
  705. {
  706. #if NET_2_0
  707. public bool allow_selection {
  708. get { return base.AllowSelection; }
  709. }
  710. #endif
  711. public void InvokeSetItemsCore (IList value)
  712. {
  713. base.SetItemsCore (value);
  714. }
  715. }
  716. [Test]
  717. public void AddToSorted ()
  718. {
  719. AddItems (true, CheckedListBoxTest.Items, CheckedListBoxTest.ExpectedAddPositionsSorted);
  720. }
  721. [Test]
  722. public void AddToUnsorted ()
  723. {
  724. AddItems (false, CheckedListBoxTest.Items, CheckedListBoxTest.ExpectedAddPositionsUnsorted);
  725. }
  726. void AddItems (bool sorted, object[] items, int[] expectedAddPositions)
  727. {
  728. ListBox clb = new ListBox ();
  729. clb.Sorted = sorted;
  730. ArrayList addedAtList = new ArrayList ();
  731. foreach (object cur in items)
  732. {
  733. int idx = clb.Items.Add (cur);
  734. addedAtList.Add (idx);
  735. }
  736. Assert.AreEqual ((Array)expectedAddPositions, (Array)addedAtList.ToArray (typeof (int)), "addedAtList");
  737. }
  738. [Test]
  739. public void SelectedIndexUpdated () // Xamarin bug 4921
  740. {
  741. using (Form f = new Form ()) {
  742. f.ShowInTaskbar = false;
  743. ListBox l = new ListBox ();
  744. l.Sorted = true;
  745. f.Controls.Add (l);
  746. l.Items.Add ("B");
  747. l.SelectedIndex = 0;
  748. Assert.AreEqual (0, l.SelectedIndex);
  749. l.Items.Add ("A");
  750. Assert.AreEqual (1, l.SelectedIndex);
  751. }
  752. }
  753. [Test]
  754. public void SelectedIndexUpdated_MultiSelect () // Xamarin bug 4921
  755. {
  756. using (Form f = new Form ()) {
  757. f.ShowInTaskbar = false;
  758. ListBox l = new ListBox ();
  759. l.Sorted = true;
  760. l.SelectionMode = SelectionMode.MultiSimple;
  761. f.Controls.Add (l);
  762. l.Items.Add ("B");
  763. l.Items.Add ("C");
  764. l.SelectedIndex = 0;
  765. l.SelectedIndex = 1;
  766. Assert.AreEqual (2, l.SelectedIndices.Count);
  767. Assert.AreEqual (0, l.SelectedIndices [0]);
  768. Assert.AreEqual (1, l.SelectedIndices [1]);
  769. Assert.AreEqual (2, l.SelectedItems.Count);
  770. Assert.AreEqual ("B", l.SelectedItems [0]);
  771. Assert.AreEqual ("C", l.SelectedItems [1]);
  772. l.Items.Add ("A");
  773. Assert.AreEqual (2, l.SelectedIndices.Count);
  774. Assert.AreEqual (1, l.SelectedIndices[0]);
  775. Assert.AreEqual (2, l.SelectedIndices[1]);
  776. Assert.AreEqual (2, l.SelectedItems.Count);
  777. Assert.AreEqual ("B", l.SelectedItems [0]);
  778. Assert.AreEqual ("C", l.SelectedItems [1]);
  779. }
  780. }
  781. }
  782. [TestFixture]
  783. public class ListBoxObjectCollectionTest : TestHelper
  784. {
  785. ListBox.ObjectCollection col;
  786. [SetUp]
  787. protected override void SetUp ()
  788. {
  789. col = new ListBox.ObjectCollection (new ListBox ());
  790. }
  791. [Test]
  792. public void DefaultProperties ()
  793. {
  794. Assert.AreEqual (false, col.IsReadOnly, "#B1");
  795. Assert.AreEqual (false, ((ICollection)col).IsSynchronized, "#B2");
  796. Assert.AreEqual (col, ((ICollection)col).SyncRoot, "#B3");
  797. Assert.AreEqual (false, ((IList)col).IsFixedSize, "#B4");
  798. Assert.AreEqual (0, col.Count);
  799. }
  800. [Test]
  801. public void Add ()
  802. {
  803. col.Add ("Item1");
  804. col.Add ("Item2");
  805. Assert.AreEqual (2, col.Count, "#1");
  806. Assert.AreEqual ("Item1", col [0], "#2");
  807. Assert.AreEqual ("Item2", col [1], "#3");
  808. }
  809. [Test]
  810. public void Add_Item_Null ()
  811. {
  812. try {
  813. col.Add (null);
  814. Assert.Fail ("#1");
  815. } catch (ArgumentNullException ex) {
  816. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  817. Assert.IsNull (ex.InnerException, "#3");
  818. Assert.IsNotNull (ex.Message, "#4");
  819. Assert.AreEqual ("item", ex.ParamName, "#5");
  820. }
  821. }
  822. [Test] // AddRange (Object [])
  823. public void AddRange1_Items_Null ()
  824. {
  825. try {
  826. col.AddRange ((object []) null);
  827. Assert.Fail ("#1");
  828. } catch (ArgumentNullException ex) {
  829. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  830. Assert.IsNull (ex.InnerException, "#3");
  831. Assert.IsNotNull (ex.Message, "#4");
  832. Assert.AreEqual ("items", ex.ParamName, "#5");
  833. }
  834. }
  835. [Test] // AddRange (ListBox.ObjectCollection)
  836. public void AddRange2_Value_Null ()
  837. {
  838. try {
  839. col.AddRange ((ListBox.ObjectCollection) null);
  840. Assert.Fail ("#1");
  841. } catch (ArgumentNullException ex) {
  842. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  843. Assert.IsNull (ex.InnerException, "#3");
  844. Assert.IsNotNull (ex.Message, "#4");
  845. Assert.AreEqual ("items", ex.ParamName, "#5");
  846. }
  847. }
  848. [Test]
  849. public void Clear ()
  850. {
  851. col.Add ("Item1");
  852. col.Add ("Item2");
  853. col.Clear ();
  854. Assert.AreEqual (0, col.Count, "#D1");
  855. }
  856. [Test]
  857. public void Contains ()
  858. {
  859. object obj = "Item1";
  860. col.Add (obj);
  861. Assert.IsTrue (col.Contains ("Item1"), "#1");
  862. Assert.IsFalse (col.Contains ("Item2"), "#2");
  863. }
  864. [Test]
  865. public void Contains_Value_Null ()
  866. {
  867. try {
  868. col.Contains (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. #if NET_2_0
  875. Assert.AreEqual ("value", ex.ParamName, "#5");
  876. #else
  877. Assert.IsNotNull (ex.ParamName, "#5");
  878. #endif
  879. }
  880. }
  881. [Test]
  882. public void Indexer_Value_Null ()
  883. {
  884. col.Add ("Item1");
  885. try {
  886. col [0] = null;
  887. Assert.Fail ("#1");
  888. } catch (ArgumentNullException ex) {
  889. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  890. Assert.IsNull (ex.InnerException, "#3");
  891. Assert.IsNotNull (ex.Message, "#4");
  892. Assert.AreEqual ("value", ex.ParamName, "#5");
  893. }
  894. }
  895. [Test]
  896. public void IndexOf ()
  897. {
  898. col.Add ("Item1");
  899. col.Add ("Item2");
  900. Assert.AreEqual (1, col.IndexOf ("Item2"), "#1");
  901. Assert.AreEqual (0, col.IndexOf ("Item1"), "#2");
  902. Assert.AreEqual (-1, col.IndexOf ("Item3"), "#3");
  903. }
  904. [Test]
  905. public void IndexOf_Value_Null ()
  906. {
  907. try {
  908. col.IndexOf (null);
  909. Assert.Fail ("#1");
  910. } catch (ArgumentNullException ex) {
  911. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  912. Assert.IsNull (ex.InnerException, "#3");
  913. Assert.IsNotNull (ex.Message, "#4");
  914. #if NET_2_0
  915. Assert.AreEqual ("value", ex.ParamName, "#5");
  916. #else
  917. Assert.IsNotNull (ex.ParamName, "#5");
  918. #endif
  919. }
  920. }
  921. [Test]
  922. #if NET_2_0
  923. [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363285
  924. #endif
  925. public void Insert_Item_Null ()
  926. {
  927. col.Add ("Item1");
  928. try {
  929. col.Insert (0, null);
  930. Assert.Fail ("#1");
  931. } catch (ArgumentNullException ex) {
  932. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  933. Assert.IsNull (ex.InnerException, "#3");
  934. Assert.IsNotNull (ex.Message, "#4");
  935. Assert.AreEqual ("item", ex.ParamName, "#5");
  936. }
  937. }
  938. [Test]
  939. public void Remove ()
  940. {
  941. col.Add ("Item1");
  942. col.Add ("Item2");
  943. col.Remove ("Item1");
  944. Assert.AreEqual (1, col.Count, "#A1");
  945. Assert.AreEqual (-1, col.IndexOf ("Item1"), "#A2");
  946. Assert.AreEqual (0, col.IndexOf ("Item2"), "#A3");
  947. col.Remove (null);
  948. Assert.AreEqual (1, col.Count, "#B1");
  949. Assert.AreEqual (-1, col.IndexOf ("Item1"), "#B2");
  950. Assert.AreEqual (0, col.IndexOf ("Item2"), "#B3");
  951. col.Remove ("Item3");
  952. Assert.AreEqual (1, col.Count, "#C1");
  953. Assert.AreEqual (-1, col.IndexOf ("Item1"), "#C2");
  954. Assert.AreEqual (0, col.IndexOf ("Item2"), "#C3");
  955. col.Remove ("Item2");
  956. Assert.AreEqual (0, col.Count, "#D1");
  957. Assert.AreEqual (-1, col.IndexOf ("Item1"), "#D2");
  958. Assert.AreEqual (-1, col.IndexOf ("Item2"), "#D3");
  959. }
  960. [Test]
  961. public void RemoveAt ()
  962. {
  963. col.Add ("Item1");
  964. col.Add ("Item2");
  965. col.RemoveAt (0);
  966. Assert.AreEqual (1, col.Count, "#1");
  967. Assert.AreEqual (-1, col.IndexOf ("Item1"), "#2");
  968. Assert.AreEqual (0, col.IndexOf ("Item2"), "#3");
  969. }
  970. }
  971. #if NET_2_0
  972. [TestFixture]
  973. public class ListBoxIntegerCollectionTest : TestHelper
  974. {
  975. ListBox.IntegerCollection col;
  976. ListBox listBox;
  977. [SetUp]
  978. protected override void SetUp ()
  979. {
  980. listBox = new ListBox ();
  981. col = new ListBox.IntegerCollection (listBox);
  982. }
  983. [Test]
  984. public void Add ()
  985. {
  986. col.Add (5);
  987. Assert.AreEqual (1, col.Count, "#1");
  988. col.Add (7);
  989. Assert.AreEqual (2, col.Count, "#2");
  990. col.Add (5);
  991. Assert.AreEqual (2, col.Count, "#3");
  992. col.Add (3);
  993. Assert.AreEqual (3, col.Count, "#4");
  994. }
  995. [Test] // AddRange (Int32 [])
  996. public void AddRange1 ()
  997. {
  998. col.Add (5);
  999. col.Add (3);
  1000. col.AddRange (new int [] { 3, 7, 9, 5, 4 });
  1001. Assert.AreEqual (5, col.Count, "#1");
  1002. Assert.AreEqual (3, col [0], "#2");
  1003. Assert.AreEqual (4, col [1], "#3");
  1004. Assert.AreEqual (5, col [2], "#4");
  1005. Assert.AreEqual (7, col [3], "#5");
  1006. Assert.AreEqual (9, col [4], "#6");
  1007. }
  1008. [Test] // AddRange (Int32 [])
  1009. public void AddRange1_Items_Null ()
  1010. {
  1011. try {
  1012. col.AddRange ((int []) null);
  1013. Assert.Fail ("#1");
  1014. } catch (ArgumentNullException ex) {
  1015. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1016. Assert.IsNull (ex.InnerException, "#3");
  1017. Assert.IsNotNull (ex.Message, "#4");
  1018. Assert.AreEqual ("items", ex.ParamName, "#5");
  1019. }
  1020. }
  1021. [Test] // AddRange (ListBox.IntegerCollection)
  1022. public void AddRange2 ()
  1023. {
  1024. ListBox.IntegerCollection ints = new ListBox.IntegerCollection (
  1025. listBox);
  1026. ints.Add (3);
  1027. ints.Add (1);
  1028. ints.Add (-5);
  1029. ints.Add (4);
  1030. ints.Add (2);
  1031. col.Add (5);
  1032. col.Add (3);
  1033. col.Add (12);
  1034. col.AddRange (ints);
  1035. Assert.AreEqual (7, col.Count, "#1");
  1036. Assert.AreEqual (-5, col [0], "#2");
  1037. Assert.AreEqual (1, col [1], "#3");
  1038. Assert.AreEqual (2, col [2], "#4");
  1039. Assert.AreEqual (3, col [3], "#5");
  1040. Assert.AreEqual (4, col [4], "#6");
  1041. Assert.AreEqual (5, col [5], "#7");
  1042. }
  1043. [Test] // AddRange (ListBox.IntegerCollection)
  1044. public void AddRange2_Items_Null ()
  1045. {
  1046. try {
  1047. col.AddRange ((ListBox.IntegerCollection) null);
  1048. Assert.Fail ("#1");
  1049. } catch (ArgumentNullException ex) {
  1050. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1051. Assert.IsNull (ex.InnerException, "#3");
  1052. Assert.IsNotNull (ex.Message, "#4");
  1053. Assert.AreEqual ("items", ex.ParamName, "#5");
  1054. }
  1055. }
  1056. [Test]
  1057. [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363278
  1058. public void Clear ()
  1059. {
  1060. col.Add (5);
  1061. col.Add (3);
  1062. col.Clear ();
  1063. Assert.AreEqual (0, col.Count, "#1");
  1064. Assert.AreEqual (-1, col.IndexOf (5), "#2");
  1065. Assert.AreEqual (-1, col.IndexOf (3), "#3");
  1066. }
  1067. [Test]
  1068. public void Contains ()
  1069. {
  1070. col.Add (5);
  1071. col.Add (7);
  1072. Assert.IsTrue (col.Contains (5), "#1");
  1073. Assert.IsFalse (col.Contains (3), "#2");
  1074. Assert.IsTrue (col.Contains (7), "#3");
  1075. Assert.IsFalse (col.Contains (-5), "#4");
  1076. }
  1077. [Test]
  1078. public void CopyTo ()
  1079. {
  1080. int [] copy = new int [5] { 9, 4, 6, 2, 8 };
  1081. col.Add (3);
  1082. col.Add (7);
  1083. col.Add (5);
  1084. col.CopyTo (copy, 1);
  1085. Assert.AreEqual (9, copy [0], "#1");
  1086. Assert.AreEqual (3, copy [1], "#2");
  1087. Assert.AreEqual (5, copy [2], "#3");
  1088. Assert.AreEqual (7, copy [3], "#4");
  1089. Assert.AreEqual (8, copy [4], "#5");
  1090. }
  1091. [Test]
  1092. public void CopyTo_Destination_Invalid ()
  1093. {
  1094. string [] copy = new string [3] { "A", "B", "C" };
  1095. col.CopyTo (copy, 1);
  1096. col.Add (3);
  1097. try {
  1098. col.CopyTo (copy, 1);
  1099. Assert.Fail ("#1");
  1100. } catch (InvalidCastException) {
  1101. }
  1102. }
  1103. [Test]
  1104. public void CopyTo_Destination_Null ()
  1105. {
  1106. col.CopyTo ((Array) null, 1);
  1107. col.Add (3);
  1108. try {
  1109. col.CopyTo ((Array) null, 1);
  1110. Assert.Fail ("#1");
  1111. } catch (NullReferenceException) {
  1112. }
  1113. }
  1114. [Test]
  1115. public void CopyTo_Index_Negative ()
  1116. {
  1117. int [] copy = new int [5] { 9, 4, 6, 2, 8 };
  1118. col.CopyTo (copy, -5);
  1119. col.Add (3);
  1120. try {
  1121. col.CopyTo (copy, -5);
  1122. Assert.Fail ("#1");
  1123. } catch (IndexOutOfRangeException ex) {
  1124. // Index was outside the bounds of the array
  1125. Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
  1126. Assert.IsNull (ex.InnerException, "#3");
  1127. Assert.IsNotNull (ex.Message, "#4");
  1128. }
  1129. }
  1130. [Test]
  1131. public void Count ()
  1132. {
  1133. Assert.AreEqual (0, col.Count, "#1");
  1134. col.Add (5);
  1135. Assert.AreEqual (1, col.Count, "#2");
  1136. col.Add (7);
  1137. Assert.AreEqual (2, col.Count, "#3");
  1138. col.Remove (7);
  1139. Assert.AreEqual (1, col.Count, "#4");
  1140. }
  1141. [Test]
  1142. public void Indexer ()
  1143. {
  1144. col.Add (5);
  1145. col.Add (7);
  1146. Assert.AreEqual (7, col [1], "#1");
  1147. Assert.AreEqual (5, col [0], "#2");
  1148. col [0] = 3;
  1149. Assert.AreEqual (7, col [1], "#3");
  1150. Assert.AreEqual (3, col [0], "#4");
  1151. }
  1152. [Test]
  1153. public void IndexOf ()
  1154. {
  1155. col.Add (5);
  1156. col.Add (7);
  1157. Assert.AreEqual (0, col.IndexOf (5), "#1");
  1158. Assert.AreEqual (-1, col.IndexOf (3), "#2");
  1159. Assert.AreEqual (1, col.IndexOf (7), "#3");
  1160. Assert.AreEqual (-1, col.IndexOf (-5), "#4");
  1161. }
  1162. [Test]
  1163. public void Remove ()
  1164. {
  1165. col.Add (5);
  1166. col.Add (3);
  1167. col.Remove (5);
  1168. col.Remove (7);
  1169. Assert.AreEqual (1, col.Count, "#1");
  1170. Assert.AreEqual (3, col [0], "#2");
  1171. col.Remove (3);
  1172. Assert.AreEqual (0, col.Count, "#3");
  1173. col.Remove (3);
  1174. // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363280
  1175. //Assert.AreEqual (0, col.Count, "#4");
  1176. }
  1177. [Test]
  1178. public void RemoveAt ()
  1179. {
  1180. col.Add (5);
  1181. col.Add (3);
  1182. col.Add (7);
  1183. col.RemoveAt (1);
  1184. Assert.AreEqual (2, col.Count, "#A1");
  1185. Assert.AreEqual (3, col [0], "#A2");
  1186. Assert.AreEqual (7, col [1], "#A3");
  1187. col.RemoveAt (0);
  1188. Assert.AreEqual (1, col.Count, "#B1");
  1189. Assert.AreEqual (7, col [0], "#B2");
  1190. col.RemoveAt (0);
  1191. Assert.AreEqual (0, col.Count, "#C1");
  1192. Assert.AreEqual (-1, col.IndexOf (5), "#C2");
  1193. Assert.AreEqual (-1, col.IndexOf (3), "#C3");
  1194. // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363280
  1195. //Assert.AreEqual (-1, col.IndexOf (7), "#C4");
  1196. }
  1197. [Test]
  1198. [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
  1199. public void RemoveAt_Index_Negative ()
  1200. {
  1201. col.Add (5);
  1202. try {
  1203. col.RemoveAt (-1);
  1204. Assert.Fail ("#1");
  1205. } catch (IndexOutOfRangeException ex) {
  1206. // Index was outside the bounds of the array
  1207. Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
  1208. Assert.IsNull (ex.InnerException, "#3");
  1209. Assert.IsNotNull (ex.Message, "#4");
  1210. }
  1211. Assert.AreEqual (1, col.Count, "#5");
  1212. }
  1213. [Test]
  1214. [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
  1215. public void RemoveAt_Index_Overflow ()
  1216. {
  1217. col.Add (5);
  1218. try {
  1219. col.RemoveAt (1);
  1220. Assert.Fail ("#1");
  1221. } catch (ArgumentOutOfRangeException ex) {
  1222. // Index was outside the bounds of the array
  1223. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  1224. Assert.IsNull (ex.InnerException, "#3");
  1225. Assert.IsNotNull (ex.Message, "#4");
  1226. Assert.AreEqual ("index", ex.ParamName, "#5");
  1227. }
  1228. Assert.AreEqual (1, col.Count, "#6");
  1229. }
  1230. [Test]
  1231. public void ICollection_IsSynchronized ()
  1232. {
  1233. ICollection collection = (ICollection) col;
  1234. Assert.IsTrue (collection.IsSynchronized);
  1235. }
  1236. [Test]
  1237. public void ICollection_SyncRoot ()
  1238. {
  1239. ICollection collection = (ICollection) col;
  1240. Assert.AreSame (collection, collection.SyncRoot);
  1241. }
  1242. [Test]
  1243. public void IList_Add ()
  1244. {
  1245. IList list = (IList) col;
  1246. list.Add (5);
  1247. Assert.AreEqual (1, list.Count, "#1");
  1248. list.Add (7);
  1249. Assert.AreEqual (2, list.Count, "#2");
  1250. list.Add (5);
  1251. Assert.AreEqual (2, list.Count, "#3");
  1252. list.Add (3);
  1253. Assert.AreEqual (3, list.Count, "#4");
  1254. }
  1255. [Test]
  1256. public void IList_Add_Item_Invalid ()
  1257. {
  1258. IList list = (IList) col;
  1259. try {
  1260. list.Add (null);
  1261. Assert.Fail ("#A1");
  1262. } catch (ArgumentException ex) {
  1263. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  1264. Assert.IsNull (ex.InnerException, "#A3");
  1265. Assert.AreEqual ("item", ex.Message, "#A4");
  1266. Assert.IsNull (ex.ParamName, "#A5");
  1267. }
  1268. try {
  1269. list.Add ("x");
  1270. Assert.Fail ("#B1");
  1271. } catch (ArgumentException ex) {
  1272. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  1273. Assert.IsNull (ex.InnerException, "#B3");
  1274. Assert.AreEqual ("item", ex.Message, "#B4");
  1275. Assert.IsNull (ex.ParamName, "#B5");
  1276. }
  1277. }
  1278. [Test]
  1279. public void IList_Clear ()
  1280. {
  1281. IList list = (IList) col;
  1282. list.Add (5);
  1283. list.Add (7);
  1284. list.Clear ();
  1285. Assert.AreEqual (0, list.Count);
  1286. }
  1287. [Test]
  1288. public void IList_Contains ()
  1289. {
  1290. IList list = (IList) col;
  1291. list.Add (5);
  1292. list.Add (7);
  1293. Assert.IsTrue (list.Contains (5), "#1");
  1294. Assert.IsFalse (list.Contains (3), "#2");
  1295. Assert.IsTrue (list.Contains (7), "#3");
  1296. Assert.IsFalse (list.Contains (null), "#4");
  1297. Assert.IsFalse (list.Contains ("x"), "#5");
  1298. }
  1299. [Test]
  1300. public void IList_Indexer ()
  1301. {
  1302. IList list = (IList) col;
  1303. list.Add (5);
  1304. list.Add (7);
  1305. Assert.AreEqual (7, list [1], "#1");
  1306. Assert.AreEqual (5, list [0], "#2");
  1307. list [0] = 3;
  1308. Assert.AreEqual (7, list [1], "#3");
  1309. Assert.AreEqual (3, list [0], "#4");
  1310. }
  1311. [Test]
  1312. public void IList_IndexOf ()
  1313. {
  1314. IList list = (IList) col;
  1315. list.Add (5);
  1316. list.Add (7);
  1317. Assert.AreEqual (0, list.IndexOf (5), "#1");
  1318. Assert.AreEqual (-1, list.IndexOf (3), "#2");
  1319. Assert.AreEqual (1, list.IndexOf (7), "#3");
  1320. Assert.AreEqual (-1, list.IndexOf (null), "#4");
  1321. Assert.AreEqual (-1, list.IndexOf ("x"), "#5");
  1322. }
  1323. [Test]
  1324. public void IList_Insert ()
  1325. {
  1326. IList list = (IList) col;
  1327. list.Add (5);
  1328. try {
  1329. list.Insert (0, 7);
  1330. Assert.Fail ("#A1");
  1331. } catch (NotSupportedException ex) {
  1332. // ListBox.IntegerCollection is sorted, and
  1333. // items cannot be inserted into it
  1334. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
  1335. Assert.IsNull (ex.InnerException, "#A3");
  1336. Assert.IsNotNull (ex.Message, "#A4");
  1337. }
  1338. try {
  1339. list.Insert (-5, null);
  1340. Assert.Fail ("#B1");
  1341. } catch (NotSupportedException ex) {
  1342. // ListBox.IntegerCollection is sorted, and
  1343. // items cannot be inserted into it
  1344. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
  1345. Assert.IsNull (ex.InnerException, "#B3");
  1346. Assert.IsNotNull (ex.Message, "#B4");
  1347. }
  1348. }
  1349. [Test]
  1350. public void IList_IsFixedSize ()
  1351. {
  1352. IList list = (IList) col;
  1353. Assert.IsFalse (list.IsFixedSize);
  1354. }
  1355. [Test]
  1356. public void IList_IsReadOnly ()
  1357. {
  1358. IList list = (IList) col;
  1359. Assert.IsFalse (list.IsReadOnly);
  1360. }
  1361. [Test]
  1362. public void IList_Remove ()
  1363. {
  1364. IList list = (IList) col;
  1365. list.Add (5);
  1366. list.Add (3);
  1367. list.Remove (5);
  1368. list.Remove (7);
  1369. list.Remove (int.MinValue);
  1370. list.Remove (int.MaxValue);
  1371. Assert.AreEqual (1, list.Count, "#1");
  1372. Assert.AreEqual (3, list [0], "#2");
  1373. list.Remove (3);
  1374. Assert.AreEqual (0, list.Count, "#3");
  1375. }
  1376. [Test]
  1377. public void IList_Remove_Value_Invalid ()
  1378. {
  1379. IList list = (IList) col;
  1380. list.Add (5);
  1381. try {
  1382. list.Remove ("x");
  1383. Assert.Fail ("#A1");
  1384. } catch (ArgumentException ex) {
  1385. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  1386. Assert.IsNull (ex.InnerException, "#A3");
  1387. Assert.AreEqual ("value", ex.Message, "#A4");
  1388. Assert.IsNull (ex.ParamName, "#A5");
  1389. }
  1390. try {
  1391. list.Remove (null);
  1392. Assert.Fail ("#B1");
  1393. } catch (ArgumentException ex) {
  1394. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  1395. Assert.IsNull (ex.InnerException, "#B3");
  1396. Assert.AreEqual ("value", ex.Message, "#B4");
  1397. Assert.IsNull (ex.ParamName, "#B5");
  1398. }
  1399. }
  1400. [Test]
  1401. public void IList_RemoveAt ()
  1402. {
  1403. IList list = (IList) col;
  1404. list.Add (5);
  1405. list.Add (3);
  1406. list.Add (7);
  1407. list.RemoveAt (1);
  1408. Assert.AreEqual (2, list.Count, "#A1");
  1409. Assert.AreEqual (3, list [0], "#A2");
  1410. Assert.AreEqual (7, list [1], "#A3");
  1411. list.RemoveAt (0);
  1412. Assert.AreEqual (1, list.Count, "#B1");
  1413. Assert.AreEqual (7, list [0], "#B2");
  1414. list.RemoveAt (0);
  1415. Assert.AreEqual (0, list.Count, "#C");
  1416. }
  1417. [Test]
  1418. public void IList_RemoveAt_Index_Negative ()
  1419. {
  1420. IList list = (IList) col;
  1421. list.Add (5);
  1422. try {
  1423. list.RemoveAt (-1);
  1424. Assert.Fail ("#1");
  1425. } catch (IndexOutOfRangeException ex) {
  1426. // Index was outside the bounds of the array
  1427. Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
  1428. Assert.IsNull (ex.InnerException, "#3");
  1429. Assert.IsNotNull (ex.Message, "#4");
  1430. }
  1431. // // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
  1432. //Assert.AreEqual (1, list.Count, "#5");
  1433. }
  1434. [Test]
  1435. [NUnit.Framework.Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=363276
  1436. public void IList_RemoveAt_Index_Overflow ()
  1437. {
  1438. IList list = (IList) col;
  1439. list.Add (5);
  1440. try {
  1441. list.RemoveAt (1);
  1442. Assert.Fail ("#1");
  1443. } catch (ArgumentOutOfRangeException ex) {
  1444. // Index was outside the bounds of the array
  1445. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  1446. Assert.IsNull (ex.InnerException, "#3");
  1447. Assert.IsNotNull (ex.Message, "#4");
  1448. Assert.AreEqual ("index", ex.ParamName, "#5");
  1449. }
  1450. Assert.AreEqual (1, list.Count, "#6");
  1451. }
  1452. }
  1453. #endif
  1454. }