ListControlTest.cs 9.4 KB

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