ListControlTest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #if NET_2_0
  68. public HtmlTextWriterTag GetTagKey ()
  69. {
  70. return TagKey;
  71. }
  72. #endif
  73. }
  74. [TestFixture]
  75. public class ListControlTest {
  76. private Hashtable changed = new Hashtable ();
  77. [Test]
  78. public void DefaultProperties ()
  79. {
  80. ListControlPoker p = new ListControlPoker ();
  81. Assert.AreEqual (p.AutoPostBack, false, "A1");
  82. Assert.AreEqual (p.DataMember, String.Empty, "A2");
  83. Assert.AreEqual (p.DataSource, null, "A3");
  84. Assert.AreEqual (p.DataTextField, String.Empty, "A4");
  85. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A5");
  86. Assert.AreEqual (p.DataValueField, String.Empty, "A6");
  87. Assert.AreEqual (p.Items.Count, 0, "A7");
  88. Assert.AreEqual (p.SelectedIndex, -1,"A8");
  89. Assert.AreEqual (p.SelectedItem, null, "A9");
  90. Assert.AreEqual (p.SelectedValue, String.Empty, "A10");
  91. #if NET_2_0
  92. Assert.IsFalse (p.AppendDataBoundItems, "A11");
  93. Assert.AreEqual (p.Text, "", "A12");
  94. Assert.AreEqual (p.GetTagKey(), HtmlTextWriterTag.Select, "A13");
  95. #endif
  96. }
  97. [Test]
  98. public void CleanProperties ()
  99. {
  100. ListControlPoker p = new ListControlPoker ();
  101. p.AutoPostBack = true;
  102. Assert.AreEqual (p.AutoPostBack, true, "A2");
  103. p.DataMember = "DataMember";
  104. Assert.AreEqual (p.DataMember, "DataMember", "A3");
  105. p.DataSource = "DataSource";
  106. Assert.AreEqual (p.DataSource, "DataSource", "A4");
  107. p.DataTextField = "DataTextField";
  108. Assert.AreEqual (p.DataTextField, "DataTextField", "A5");
  109. p.DataTextFormatString = "DataTextFormatString";
  110. Assert.AreEqual (p.DataTextFormatString, "DataTextFormatString", "A6");
  111. p.DataValueField = "DataValueField";
  112. Assert.AreEqual (p.DataValueField, "DataValueField", "A7");
  113. p.SelectedIndex = 10;
  114. Assert.AreEqual (p.SelectedIndex, -1, "A8");
  115. p.SelectedValue = "SelectedValue";
  116. Assert.AreEqual (p.SelectedValue, String.Empty, "A9");
  117. }
  118. [Test]
  119. public void NullProperties ()
  120. {
  121. ListControlPoker p = new ListControlPoker ();
  122. p.DataMember = null;
  123. Assert.AreEqual (p.DataMember, String.Empty, "A1");
  124. p.DataSource = null;
  125. Assert.AreEqual (p.DataSource, null, "A2");
  126. p.DataTextField = null;
  127. Assert.AreEqual (p.DataTextField, String.Empty, "A3");
  128. p.DataTextFormatString = null;
  129. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A4");
  130. p.DataValueField = null;
  131. Assert.AreEqual (p.DataValueField, String.Empty, "A5");
  132. p.SelectedValue = null;
  133. Assert.AreEqual (p.SelectedValue, String.Empty, "A6");
  134. }
  135. [Test]
  136. public void ClearSelection ()
  137. {
  138. ListControlPoker p = new ListControlPoker ();
  139. ListItem foo = new ListItem ("foo");
  140. ListItem bar = new ListItem ("bar");
  141. BeginIndexChanged (p);
  142. p.Items.Add (foo);
  143. p.Items.Add (bar);
  144. p.SelectedIndex = 1;
  145. // sanity for the real test
  146. Assert.AreEqual (p.Items.Count, 2, "A1");
  147. Assert.AreEqual (p.SelectedIndex, 1, "A2");
  148. Assert.AreEqual (p.SelectedItem, bar, "A3");
  149. Assert.AreEqual (p.SelectedValue, bar.Value, "A4");
  150. p.ClearSelection ();
  151. Assert.AreEqual (p.SelectedIndex, -1, "A5");
  152. Assert.AreEqual (p.SelectedItem, null, "A6");
  153. Assert.AreEqual (p.SelectedValue, String.Empty, "A7");
  154. Assert.IsFalse (EndIndexChanged (p), "A8");
  155. // make sure we are still sane
  156. Assert.AreEqual (p.Items.Count, 2, "A9");
  157. }
  158. [Test]
  159. // Tests Save/Load/Track ViewState
  160. public void ViewState ()
  161. {
  162. ListControlPoker a = new ListControlPoker ();
  163. ListControlPoker b = new ListControlPoker ();
  164. a.TrackState ();
  165. BeginIndexChanged (a);
  166. BeginIndexChanged (b);
  167. a.Items.Add ("a");
  168. a.Items.Add ("b");
  169. a.Items.Add ("c");
  170. a.SelectedIndex = 2;
  171. object state = a.SaveState ();
  172. b.LoadState (state);
  173. #if NET_2_0
  174. Assert.AreEqual (b.SelectedIndex, -1, "A1");
  175. #else
  176. Assert.AreEqual (b.SelectedIndex, 2, "A1");
  177. #endif
  178. Assert.AreEqual (b.Items.Count, 3, "A2");
  179. Assert.AreEqual (b.Items [0].Value, "a", "A3");
  180. Assert.AreEqual (b.Items [1].Value, "b", "A4");
  181. Assert.AreEqual (b.Items [2].Value, "c", "A5");
  182. Assert.IsFalse (EndIndexChanged (a), "A6");
  183. Assert.IsFalse (EndIndexChanged (b), "A7");
  184. }
  185. [Test]
  186. public void ViewStateContents ()
  187. {
  188. ListControlPoker p = new ListControlPoker ();
  189. p.TrackState ();
  190. // So the selected index can be set
  191. p.Items.Add ("one");
  192. p.Items.Add ("two");
  193. p.AutoPostBack = false;
  194. p.DataMember = "DataMember";
  195. p.DataSource = "DataSource";
  196. p.DataTextField = "DataTextField";
  197. p.DataTextFormatString = "DataTextFormatString";
  198. p.DataValueField = "DataValueField";
  199. p.SelectedIndex = 1;
  200. #if NET_2_0
  201. p.AppendDataBoundItems = true;
  202. p.Text = "Text";
  203. #endif
  204. Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
  205. Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
  206. Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
  207. Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
  208. Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
  209. "DataTextFormatString", "A5");
  210. Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
  211. #if NET_2_0
  212. Assert.AreEqual (p.ViewStateValue ("AppendDataBoundItems"), true, "A7");
  213. #endif
  214. // None of these are saved
  215. Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A8");
  216. Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
  217. Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A10");
  218. #if NET_2_0
  219. Assert.AreEqual (p.ViewStateValue ("Text"), null, "A11");
  220. #endif
  221. }
  222. [Test]
  223. public void SelectedIndex ()
  224. {
  225. ListControlPoker p = new ListControlPoker ();
  226. p.Items.Add ("one");
  227. p.Items.Add ("two");
  228. p.Items.Add ("three");
  229. p.Items.Add ("four");
  230. p.Items [2].Selected = true;
  231. p.Items [1].Selected = true;
  232. Assert.AreEqual (p.SelectedIndex, 1, "A1");
  233. p.ClearSelection ();
  234. p.Items [3].Selected = true;
  235. Assert.AreEqual (p.SelectedIndex, 3, "A2");
  236. p.SelectedIndex = 1;
  237. Assert.AreEqual (p.SelectedIndex, 1, "A3");
  238. Assert.IsFalse (p.Items [3].Selected, "A4");
  239. }
  240. [Test]
  241. public void Render ()
  242. {
  243. ListControlPoker p = new ListControlPoker ();
  244. string s = p.Render ();
  245. string expected = "<select>\n\n</select>";
  246. Assert.AreEqual (s, expected, "A1");
  247. }
  248. [Test]
  249. #if !NET_2_0
  250. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  251. #endif
  252. public void ItemsTooHigh ()
  253. {
  254. ListControlPoker l = new ListControlPoker ();
  255. l.Items.Add ("foo");
  256. l.SelectedIndex = 1;
  257. }
  258. [Test]
  259. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  260. public void ItemsTooLow ()
  261. {
  262. ListControlPoker l = new ListControlPoker ();
  263. l.Items.Add ("foo");
  264. l.SelectedIndex = -2;
  265. }
  266. [Test]
  267. public void ItemsOk ()
  268. {
  269. ListControlPoker l = new ListControlPoker ();
  270. l.Items.Add ("foo");
  271. l.SelectedIndex = 0;
  272. l.SelectedIndex = -1;
  273. }
  274. private void BeginIndexChanged (ListControl l)
  275. {
  276. l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
  277. }
  278. private bool EndIndexChanged (ListControl l)
  279. {
  280. bool res = changed [l] != null;
  281. changed [l] = null;
  282. return res;
  283. }
  284. private void IndexChangedHandler (object sender, EventArgs e)
  285. {
  286. changed [sender] = new object ();
  287. }
  288. }
  289. }