ListControlTest.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.Collections;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. namespace MonoTests.System.Web.UI.WebControls {
  39. public class ListControlPoker : ListControl {
  40. public ListControlPoker ()
  41. {
  42. }
  43. public void TrackState ()
  44. {
  45. TrackViewState ();
  46. }
  47. public object SaveState ()
  48. {
  49. return SaveViewState ();
  50. }
  51. public void LoadState (object state)
  52. {
  53. LoadViewState (state);
  54. }
  55. public object ViewStateValue (string name)
  56. {
  57. return ViewState [name];
  58. }
  59. public string Render ()
  60. {
  61. StringWriter sw = new StringWriter ();
  62. sw.NewLine = "\n";
  63. HtmlTextWriter writer = new HtmlTextWriter (sw);
  64. base.Render (writer);
  65. return writer.InnerWriter.ToString ();
  66. }
  67. }
  68. [TestFixture]
  69. public class ListControlTest {
  70. private Hashtable changed = new Hashtable ();
  71. [Test]
  72. public void DefaultProperties ()
  73. {
  74. ListControlPoker p = new ListControlPoker ();
  75. Assert.AreEqual (p.AutoPostBack, false, "A1");
  76. Assert.AreEqual (p.DataMember, String.Empty, "A2");
  77. Assert.AreEqual (p.DataSource, null, "A3");
  78. Assert.AreEqual (p.DataTextField, String.Empty, "A4");
  79. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A5");
  80. Assert.AreEqual (p.DataValueField, String.Empty, "A6");
  81. Assert.AreEqual (p.Items.Count, 0, "A7");
  82. Assert.AreEqual (p.SelectedIndex, -1,"A8");
  83. Assert.AreEqual (p.SelectedItem, null, "A9");
  84. Assert.AreEqual (p.SelectedValue, String.Empty, "A10");
  85. }
  86. [Test]
  87. public void CleanProperties ()
  88. {
  89. ListControlPoker p = new ListControlPoker ();
  90. p.AutoPostBack = true;
  91. Assert.AreEqual (p.AutoPostBack, true, "A2");
  92. p.DataMember = "DataMember";
  93. Assert.AreEqual (p.DataMember, "DataMember", "A3");
  94. p.DataSource = "DataSource";
  95. Assert.AreEqual (p.DataSource, "DataSource", "A4");
  96. p.DataTextField = "DataTextField";
  97. Assert.AreEqual (p.DataTextField, "DataTextField", "A5");
  98. p.DataTextFormatString = "DataTextFormatString";
  99. Assert.AreEqual (p.DataTextFormatString, "DataTextFormatString", "A6");
  100. p.DataValueField = "DataValueField";
  101. Assert.AreEqual (p.DataValueField, "DataValueField", "A7");
  102. p.SelectedIndex = 10;
  103. Assert.AreEqual (p.SelectedIndex, -1, "A8");
  104. p.SelectedValue = "SelectedValue";
  105. Assert.AreEqual (p.SelectedValue, String.Empty, "A9");
  106. }
  107. [Test]
  108. public void NullProperties ()
  109. {
  110. ListControlPoker p = new ListControlPoker ();
  111. p.DataMember = null;
  112. Assert.AreEqual (p.DataMember, String.Empty, "A1");
  113. p.DataSource = null;
  114. Assert.AreEqual (p.DataSource, null, "A2");
  115. p.DataTextField = null;
  116. Assert.AreEqual (p.DataTextField, String.Empty, "A3");
  117. p.DataTextFormatString = null;
  118. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A4");
  119. p.DataValueField = null;
  120. Assert.AreEqual (p.DataValueField, String.Empty, "A5");
  121. p.SelectedValue = null;
  122. Assert.AreEqual (p.SelectedValue, String.Empty, "A6");
  123. }
  124. [Test]
  125. public void ClearSelection ()
  126. {
  127. ListControlPoker p = new ListControlPoker ();
  128. ListItem foo = new ListItem ("foo");
  129. ListItem bar = new ListItem ("bar");
  130. BeginIndexChanged (p);
  131. p.Items.Add (foo);
  132. p.Items.Add (bar);
  133. p.SelectedIndex = 1;
  134. // sanity for the real test
  135. Assert.AreEqual (p.Items.Count, 2, "A1");
  136. Assert.AreEqual (p.SelectedIndex, 1, "A2");
  137. Assert.AreEqual (p.SelectedItem, bar, "A3");
  138. Assert.AreEqual (p.SelectedValue, bar.Value, "A4");
  139. p.ClearSelection ();
  140. Assert.AreEqual (p.SelectedIndex, -1, "A5");
  141. Assert.AreEqual (p.SelectedItem, null, "A6");
  142. Assert.AreEqual (p.SelectedValue, String.Empty, "A7");
  143. Assert.IsFalse (EndIndexChanged (p), "A8");
  144. // make sure we are still sane
  145. Assert.AreEqual (p.Items.Count, 2, "A9");
  146. }
  147. [Test]
  148. // Tests Save/Load/Track ViewState
  149. public void ViewState ()
  150. {
  151. ListControlPoker a = new ListControlPoker ();
  152. ListControlPoker b = new ListControlPoker ();
  153. a.TrackState ();
  154. BeginIndexChanged (a);
  155. BeginIndexChanged (b);
  156. a.Items.Add ("a");
  157. a.Items.Add ("b");
  158. a.Items.Add ("c");
  159. a.SelectedIndex = 2;
  160. object state = a.SaveState ();
  161. b.LoadState (state);
  162. #if NET_2_0
  163. Assert.AreEqual (b.SelectedIndex, -1, "A1");
  164. #else
  165. Assert.AreEqual (b.SelectedIndex, 2, "A1");
  166. #endif
  167. Assert.AreEqual (b.Items.Count, 3, "A2");
  168. Assert.AreEqual (b.Items [0].Value, "a", "A3");
  169. Assert.AreEqual (b.Items [1].Value, "b", "A4");
  170. Assert.AreEqual (b.Items [2].Value, "c", "A5");
  171. Assert.IsFalse (EndIndexChanged (a), "A6");
  172. Assert.IsFalse (EndIndexChanged (b), "A7");
  173. }
  174. [Test]
  175. public void ViewStateContents ()
  176. {
  177. ListControlPoker p = new ListControlPoker ();
  178. p.TrackState ();
  179. // So the selected index can be set
  180. p.Items.Add ("one");
  181. p.Items.Add ("two");
  182. p.AutoPostBack = false;
  183. p.DataMember = "DataMember";
  184. p.DataSource = "DataSource";
  185. p.DataTextField = "DataTextField";
  186. p.DataTextFormatString = "DataTextFormatString";
  187. p.DataValueField = "DataValueField";
  188. p.SelectedIndex = 1;
  189. Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
  190. Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
  191. Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
  192. Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
  193. Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
  194. "DataTextFormatString", "A5");
  195. Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
  196. // None of these are saved
  197. Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A7");
  198. Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
  199. Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A8");
  200. }
  201. [Test]
  202. public void SelectedIndex ()
  203. {
  204. ListControlPoker p = new ListControlPoker ();
  205. p.Items.Add ("one");
  206. p.Items.Add ("two");
  207. p.Items.Add ("three");
  208. p.Items.Add ("four");
  209. p.Items [2].Selected = true;
  210. p.Items [1].Selected = true;
  211. Assert.AreEqual (p.SelectedIndex, 1, "A1");
  212. p.ClearSelection ();
  213. p.Items [3].Selected = true;
  214. Assert.AreEqual (p.SelectedIndex, 3, "A2");
  215. p.SelectedIndex = 1;
  216. Assert.AreEqual (p.SelectedIndex, 1, "A3");
  217. Assert.IsFalse (p.Items [3].Selected, "A4");
  218. }
  219. [Test]
  220. public void Render ()
  221. {
  222. ListControlPoker p = new ListControlPoker ();
  223. string s = p.Render ();
  224. string expected = "<select>\n\n</select>";
  225. Assert.AreEqual (s, expected, "A1");
  226. }
  227. [Test]
  228. #if !NET_2_0
  229. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  230. #endif
  231. public void ItemsTooHigh ()
  232. {
  233. ListControlPoker l = new ListControlPoker ();
  234. l.Items.Add ("foo");
  235. l.SelectedIndex = 1;
  236. }
  237. [Test]
  238. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  239. public void ItemsTooLow ()
  240. {
  241. ListControlPoker l = new ListControlPoker ();
  242. l.Items.Add ("foo");
  243. l.SelectedIndex = -2;
  244. }
  245. [Test]
  246. public void ItemsOk ()
  247. {
  248. ListControlPoker l = new ListControlPoker ();
  249. l.Items.Add ("foo");
  250. l.SelectedIndex = 0;
  251. l.SelectedIndex = -1;
  252. }
  253. private void BeginIndexChanged (ListControl l)
  254. {
  255. l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
  256. }
  257. private bool EndIndexChanged (ListControl l)
  258. {
  259. bool res = changed [l] != null;
  260. changed [l] = null;
  261. return res;
  262. }
  263. private void IndexChangedHandler (object sender, EventArgs e)
  264. {
  265. changed [sender] = new object ();
  266. }
  267. }
  268. }