ListBoxTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // Tests for System.Web.UI.WebControls.ListBoxTest.cs
  3. //
  4. // Author:
  5. // Jackson Harper ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Drawing;
  33. using System.Globalization;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. using System.Data;
  38. namespace MonoTests.System.Web.UI.WebControls
  39. {
  40. class ListBoxPoker : ListBox {
  41. public ListBoxPoker ()
  42. {
  43. TrackViewState ();
  44. }
  45. public object SaveState ()
  46. {
  47. return SaveViewState ();
  48. }
  49. public void LoadState (object o)
  50. {
  51. LoadViewState (o);
  52. }
  53. public StateBag _ViewState {
  54. get { return ViewState; }
  55. }
  56. public string Render ()
  57. {
  58. StringWriter sw = new StringWriter ();
  59. sw.NewLine = "\n";
  60. HtmlTextWriter writer = new HtmlTextWriter (sw);
  61. base.Render (writer);
  62. return writer.InnerWriter.ToString ();
  63. }
  64. }
  65. [TestFixture]
  66. public class ListBoxTest {
  67. [Test]
  68. public void Defaults ()
  69. {
  70. ListBox lb = new ListBox ();
  71. Assert.AreEqual (lb.BorderColor, Color.Empty, "A1");
  72. Assert.AreEqual (lb.BorderStyle, BorderStyle.NotSet, "A2");
  73. Assert.AreEqual (lb.BorderWidth, Unit.Empty, "A3");
  74. Assert.AreEqual (lb.Rows, 4, "A4");
  75. Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Single, "A5");
  76. Assert.AreEqual (lb.ToolTip, String.Empty, "A6");
  77. }
  78. [Test]
  79. public void SetProps ()
  80. {
  81. ListBox lb = new ListBox ();
  82. lb.BorderColor = Color.Black;
  83. Assert.AreEqual (lb.BorderColor, Color.Black, "A1");
  84. lb.BorderStyle = BorderStyle.Dashed;
  85. Assert.AreEqual (lb.BorderStyle, BorderStyle.Dashed, "A2");
  86. lb.BorderWidth = 0;
  87. Assert.AreEqual (lb.BorderWidth, (Unit) 0, "A3");
  88. lb.BorderWidth = 15;
  89. Assert.AreEqual (lb.BorderWidth, (Unit) 15, "A3");
  90. lb.Rows = 1;
  91. Assert.AreEqual (lb.Rows, 1, "A4");
  92. lb.SelectionMode = ListSelectionMode.Multiple;
  93. Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Multiple, "A6");
  94. lb.ToolTip = "foo";
  95. #if NET_2_0
  96. Assert.AreEqual (lb.ToolTip, "foo", "A7");
  97. #else
  98. Assert.AreEqual (lb.ToolTip, String.Empty, "A7"); // Always empty in 1.x
  99. #endif
  100. }
  101. [Test]
  102. #if !NET_2_0
  103. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  104. #endif
  105. public void RowsTooHigh ()
  106. {
  107. ListBox lb = new ListBox ();
  108. lb.Rows = 2001;
  109. }
  110. [Test]
  111. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  112. public void RowsTooLow ()
  113. {
  114. ListBox lb = new ListBox ();
  115. lb.Rows = 0;
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  119. public void BadSelectionMode ()
  120. {
  121. ListBox lb = new ListBox ();
  122. lb.SelectionMode = (ListSelectionMode) 500;
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  126. public void BadBorderStyle ()
  127. {
  128. ListBox lb = new ListBox ();
  129. lb.BorderStyle = (BorderStyle) 500;
  130. }
  131. [Test]
  132. public void ViewState ()
  133. {
  134. ListBoxPoker p = new ListBoxPoker ();
  135. p.BorderColor = Color.Red;
  136. Assert.AreEqual (p._ViewState ["BorderColor"],
  137. Color.Red, "A1");
  138. p.BorderStyle = BorderStyle.Double;
  139. Assert.AreEqual (p._ViewState ["BorderStyle"],
  140. BorderStyle.Double, "A2");
  141. p.BorderWidth = 25;
  142. Assert.AreEqual (p._ViewState ["BorderWidth"],
  143. (Unit) 25, "A3");
  144. p.SelectionMode = ListSelectionMode.Multiple;
  145. Assert.AreEqual (p._ViewState ["SelectionMode"],
  146. ListSelectionMode.Multiple, "A4");
  147. }
  148. [Test]
  149. public void Render1 ()
  150. {
  151. ListBoxPoker l = new ListBoxPoker ();
  152. for (int i = 0; i < 3; i ++)
  153. l.Items.Add (i.ToString ());
  154. l.SelectedIndex = l.Items.Count - 1;
  155. #if NET_2_0
  156. string exp = @"<select size=""4"">
  157. <option value=""0"">0</option>
  158. <option value=""1"">1</option>
  159. <option selected=""selected"" value=""2"">2</option>
  160. </select>";
  161. #else
  162. string exp = @"<select name size=""4"">
  163. <option value=""0"">0</option>
  164. <option value=""1"">1</option>
  165. <option selected=""selected"" value=""2"">2</option>
  166. </select>";
  167. #endif
  168. Assert.AreEqual (exp, l.Render ());
  169. }
  170. DataSet GetExampleData ()
  171. {
  172. DataSet ds = new DataSet ();
  173. ds.ReadXml (new StringReader (@"
  174. <DataSet>
  175. <Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
  176. <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92' />
  177. <Stocks Company='Google' Symbol='GOOG' Price='291.60' />
  178. </DataSet>
  179. "));
  180. return ds;
  181. }
  182. [Test]
  183. public void DoubleDataBind ()
  184. {
  185. ListBoxPoker l = new ListBoxPoker ();
  186. l.DataSource = GetExampleData ();
  187. l.DataTextField = "Company";
  188. l.DataBind ();
  189. l.DataBind ();
  190. #if NET_2_0
  191. string exp = @"<select size=""4"">
  192. <option value=""Novell Inc."">Novell Inc.</option>
  193. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  194. <option value=""Google"">Google</option>
  195. </select>";
  196. #else
  197. string exp = @"<select name size=""4"">
  198. <option value=""Novell Inc."">Novell Inc.</option>
  199. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  200. <option value=""Google"">Google</option>
  201. </select>";
  202. #endif
  203. Assert.AreEqual (exp, l.Render ());
  204. }
  205. }
  206. }