ListControlTest.cs 9.7 KB

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