ComboBoxTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // ComboBoxTests.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) 2006 Matt Hargett
  24. //
  25. // Authors:
  26. // Matt Hargett <[email protected]>
  27. //
  28. using System;
  29. using System.Windows.Forms;
  30. using NUnit.Framework;
  31. namespace MonoTests.System.Windows.Forms
  32. {
  33. [TestFixture]
  34. public class ComboBoxTests
  35. {
  36. ComboBox comboBox;
  37. bool textChanged, layoutUpdated;
  38. [SetUp]
  39. public void SetUp ()
  40. {
  41. comboBox = new ComboBox ();
  42. textChanged = false;
  43. layoutUpdated = false;
  44. comboBox.TextChanged += new EventHandler (textChangedEventHandler);
  45. comboBox.Layout += new LayoutEventHandler (layoutEventHandler);
  46. }
  47. private void textChangedEventHandler (object sender, EventArgs e)
  48. {
  49. textChanged = true;
  50. }
  51. private void layoutEventHandler (object sender, LayoutEventArgs e)
  52. {
  53. layoutUpdated = true;
  54. }
  55. [Test]
  56. public void InitialPropertyValues ()
  57. {
  58. Assert.AreEqual (String.Empty, comboBox.Text);
  59. Assert.AreEqual (-1, comboBox.SelectedIndex);
  60. Assert.IsNull (comboBox.SelectedItem);
  61. Assert.AreEqual (121, comboBox.Size.Width);
  62. //Note: it is environment dependent
  63. //Assert.AreEqual(20, comboBox.Size.Height);
  64. Assert.IsFalse (textChanged);
  65. Assert.IsFalse (layoutUpdated);
  66. }
  67. [Test]
  68. public void SetNegativeOneSelectedIndex ()
  69. {
  70. comboBox.SelectedIndex = -1;
  71. Assert.AreEqual (String.Empty, comboBox.Text);
  72. Assert.IsFalse (textChanged);
  73. }
  74. [Test]
  75. public void SetDifferentText ()
  76. {
  77. comboBox.Text = "foooooooooooooooooooooooooo";
  78. Assert.IsTrue (textChanged);
  79. Assert.IsFalse (layoutUpdated);
  80. }
  81. [Test]
  82. public void SetSameText ()
  83. {
  84. comboBox.Text = String.Empty;
  85. Assert.IsFalse (textChanged);
  86. Assert.IsFalse (layoutUpdated);
  87. }
  88. [Test] // bug #79812
  89. public void Add_Item_NonString ()
  90. {
  91. comboBox.Sorted = true;
  92. comboBox.Items.Add (new Person ("B"));
  93. comboBox.Items.Add (new Person ("A"));
  94. comboBox.Items.Add (new Person ("C"));
  95. Assert.AreEqual (string.Empty, comboBox.Text, "#1");
  96. comboBox.SelectedIndex = 0;
  97. Assert.AreEqual ("A", comboBox.Text, "#2");
  98. comboBox.SelectedIndex = 2;
  99. Assert.AreEqual ("C", comboBox.Text, "#3");
  100. }
  101. [Test]
  102. [Category ("NotWorking")]
  103. public void SelectedText ()
  104. {
  105. Form form = new Form ();
  106. form.ShowInTaskbar = false;
  107. form.Visible = false;
  108. form.Controls.Add (comboBox);
  109. comboBox.Items.Add ("Bar");
  110. comboBox.Items.Add ("Foo");
  111. Assert.AreEqual (-1, comboBox.SelectedIndex, "#A1");
  112. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#A2");
  113. comboBox.SelectedIndex = 0;
  114. Assert.AreEqual (0, comboBox.SelectedIndex, "#B1");
  115. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#B2");
  116. form.Show ();
  117. Assert.AreEqual (0, comboBox.SelectedIndex, "#C1");
  118. Assert.AreEqual ("Bar", comboBox.SelectedText, "#C2");
  119. comboBox.SelectedIndex = 1;
  120. Assert.AreEqual (1, comboBox.SelectedIndex, "#D1");
  121. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#D2");
  122. comboBox.SelectedText = "Ba";
  123. Assert.AreEqual (-1, comboBox.SelectedIndex, "#E1");
  124. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#E2");
  125. comboBox.SelectedText = "Bar";
  126. Assert.AreEqual (-1, comboBox.SelectedIndex, "#F1");
  127. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#F2");
  128. comboBox.SelectedText = "doesnotexist";
  129. Assert.AreEqual (-1, comboBox.SelectedIndex, "#G1");
  130. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#G2");
  131. comboBox.SelectedIndex = 0;
  132. Assert.AreEqual (0, comboBox.SelectedIndex, "#H1");
  133. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#H2");
  134. comboBox.SelectedText = "Foo";
  135. Assert.AreEqual (-1, comboBox.SelectedIndex, "#I1");
  136. Assert.AreEqual (string.Empty, comboBox.SelectedText, "#I2");
  137. }
  138. public class Person
  139. {
  140. private readonly string _name;
  141. public Person (string name)
  142. {
  143. _name = name;
  144. }
  145. public string Name
  146. {
  147. get
  148. {
  149. return _name;
  150. }
  151. }
  152. public override string ToString ()
  153. {
  154. return Name;
  155. }
  156. }
  157. }
  158. }