RadioButtonListTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //
  2. // Tests for System.Web.UI.WebControls.RadioButtonList.cs
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernandez ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Web.UI.WebControls;
  31. using NUnit.Framework;
  32. using System;
  33. using System.Collections.Specialized;
  34. using System.IO;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Globalization;
  38. using MonoTests.SystemWeb.Framework;
  39. using MonoTests.stand_alone.WebHarness;
  40. using System.Drawing;
  41. using System.Collections;
  42. namespace MonoTests.System.Web.UI.WebControls {
  43. [TestFixture]
  44. public class RadioButtonListTest
  45. {
  46. #region help_classes
  47. public class TestRadioButtonList : RadioButtonList {
  48. public StateBag StateBag {
  49. get { return base.ViewState; }
  50. }
  51. public string Render ()
  52. {
  53. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  54. base.Render (writer);
  55. return writer.InnerWriter.ToString ();
  56. }
  57. }
  58. #if NET_2_0
  59. class PokerRadioButtonList : RadioButtonList
  60. {
  61. public StateBag StateBag
  62. {
  63. get { return base.ViewState; }
  64. }
  65. public string Render ()
  66. {
  67. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  68. base.Render (writer);
  69. return writer.InnerWriter.ToString ();
  70. }
  71. protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
  72. {
  73. Style s = new Style ();
  74. s.BackColor = Color.Red;
  75. s.BorderStyle = BorderStyle.Solid;
  76. WebTest.CurrentTest.UserData = "GetItemStyle";
  77. return s;
  78. }
  79. public new bool HasFooter
  80. {
  81. get
  82. {
  83. return base.HasFooter;
  84. }
  85. }
  86. public new bool HasHeader
  87. {
  88. get
  89. {
  90. return base.HasHeader;
  91. }
  92. }
  93. public new bool HasSeparators
  94. {
  95. get
  96. {
  97. return base.HasSeparators;
  98. }
  99. }
  100. public new int RepeatedItemCount
  101. {
  102. get
  103. {
  104. return base.RepeatedItemCount;
  105. }
  106. }
  107. protected override void RaisePostDataChangedEvent ()
  108. {
  109. base.RaisePostDataChangedEvent ();
  110. }
  111. public void DoRaisePostDataChangedEvent ()
  112. {
  113. base.RaisePostDataChangedEvent ();
  114. }
  115. public new virtual void VerifyMultiSelect()
  116. {
  117. base.VerifyMultiSelect();
  118. }
  119. }
  120. #endif
  121. #endregion
  122. [Test]
  123. public void RadioButtonList_Constructor ()
  124. {
  125. TestRadioButtonList r = new TestRadioButtonList ();
  126. Assert.AreEqual (-1, r.CellPadding, "A1");
  127. Assert.AreEqual (-1, r.CellSpacing, "A2");
  128. Assert.AreEqual (0, r.RepeatColumns, "A3");
  129. Assert.AreEqual (RepeatDirection.Vertical, r.RepeatDirection, "A4");
  130. Assert.AreEqual (RepeatLayout.Table, r.RepeatLayout, "A5");
  131. Assert.AreEqual (TextAlign.Right, r.TextAlign, "A6");
  132. Assert.AreEqual (false, ((IRepeatInfoUser)r).HasFooter, "A7");
  133. Assert.AreEqual (false, ((IRepeatInfoUser)r).HasHeader, "A8");
  134. Assert.AreEqual (false, ((IRepeatInfoUser)r).HasSeparators, "A9");
  135. Assert.AreEqual (0, ((IRepeatInfoUser)r).RepeatedItemCount, "A10");
  136. }
  137. [Test]
  138. public void CellPaddingProperties ()
  139. {
  140. TestRadioButtonList r = new TestRadioButtonList ();
  141. r.CellPadding = 5;
  142. Assert.AreEqual (5, r.CellPadding, "setting");
  143. string s = r.Render ();
  144. #if NET_2_0
  145. // FIXME: missing some info to start rendering ?
  146. Assert.AreEqual (String.Empty, s, "htmloutput");
  147. #else
  148. Assert.IsTrue (s.ToLower ().IndexOf ("cellpadding=\"5\"") != -1, "htmloutput");
  149. #endif
  150. }
  151. [Test]
  152. public void CellSpacingProperties ()
  153. {
  154. TestRadioButtonList r = new TestRadioButtonList ();
  155. r.CellSpacing = 5;
  156. Assert.AreEqual (5, r.CellSpacing, "setting");
  157. string s = r.Render ();
  158. #if NET_2_0
  159. // FIXME: missing some info to start rendering ?
  160. Assert.AreEqual (String.Empty, s, "htmloutput");
  161. #else
  162. Assert.IsTrue (s.ToLower ().IndexOf ("cellspacing=\"5\"") != -1, "htmloutput");
  163. #endif
  164. }
  165. #if NET_2_0
  166. [Test]
  167. [Category ("NunitWeb")]
  168. public void Render ()
  169. {
  170. string RenderedPageHtml = new WebTest (PageInvoker.CreateOnLoad (Render_Load)).Run ();
  171. string RenderedControlHtml = HtmlDiff.GetControlFromPageHtml (RenderedPageHtml);
  172. string OriginControlHtml = @"<table id=""ctl01"" border=""0"">
  173. <tr>
  174. <td><input id=""ctl01_0"" type=""radio"" name=""ctl01"" value=""value1"" /><label for=""ctl01_0"">text2</label></td>
  175. </tr>
  176. </table>";
  177. HtmlDiff.AssertAreEqual (OriginControlHtml, RenderedControlHtml, "Render");
  178. }
  179. public static void Render_Load (Page p)
  180. {
  181. LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
  182. LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
  183. TestRadioButtonList c = new TestRadioButtonList ();
  184. p.Form.Controls.Add (lcb);
  185. p.Form.Controls.Add (c);
  186. p.Form.Controls.Add (lce);
  187. c.Items.Add (new ListItem ("text2", "value1"));
  188. }
  189. #else
  190. [Test]
  191. public void Render ()
  192. {
  193. TestRadioButtonList c = new TestRadioButtonList ();
  194. c.Items.Add (new ListItem ("text2", "value1"));
  195. string s = c.Render ();
  196. Assert.IsTrue (s.ToLower ().IndexOf (" type=\"radio\"") != -1, "type");
  197. Assert.IsTrue (s.ToLower ().IndexOf ("value1") != -1, "value");
  198. Assert.IsTrue (s.ToLower ().IndexOf ("text2") != -1, "text");
  199. }
  200. #endif
  201. // Exceptions
  202. [Test]
  203. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  204. public void RepeatColumnsException ()
  205. {
  206. TestRadioButtonList r = new TestRadioButtonList ();
  207. r.RepeatColumns = -1;
  208. }
  209. [Test]
  210. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  211. public void RepeatDirectionException ()
  212. {
  213. TestRadioButtonList r = new TestRadioButtonList ();
  214. r.RepeatDirection = (RepeatDirection) 4;
  215. }
  216. [Test]
  217. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  218. public void RepeatLayoutException ()
  219. {
  220. TestRadioButtonList r = new TestRadioButtonList ();
  221. r.RepeatLayout = (RepeatLayout) 3;
  222. }
  223. bool event_called;
  224. void OnSelected (object sender, EventArgs args)
  225. {
  226. event_called = true;
  227. }
  228. [Test]
  229. public void LoadAndRaise1 ()
  230. {
  231. RadioButtonList rbl = new RadioButtonList ();
  232. rbl.Items.Add (new ListItem ("Uno", "1"));
  233. rbl.Items.Add (new ListItem ("Dos", "2"));
  234. rbl.Items.Add (new ListItem ("Tres", "3"));
  235. rbl.SelectedIndex = 2;
  236. NameValueCollection nvc = new NameValueCollection ();
  237. nvc ["XXX"] = "3";
  238. IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
  239. #if NET_2_0
  240. Assert.IsFalse (handler.LoadPostData ("XXX", nvc), "#01");
  241. #else
  242. Assert.IsTrue (handler.LoadPostData ("XXX", nvc), "#01");
  243. #endif
  244. rbl.SelectedIndexChanged += new EventHandler (OnSelected);
  245. event_called = false;
  246. handler.RaisePostDataChangedEvent ();
  247. #if NET_2_0
  248. Assert.IsTrue (event_called, "#02");
  249. #else
  250. // Not called. Value is the same as the selected previously
  251. Assert.IsFalse (event_called, "#02");
  252. #endif
  253. Assert.AreEqual ("3", rbl.SelectedValue, "#03");
  254. }
  255. [Test]
  256. public void LoadAndRaise2 ()
  257. {
  258. RadioButtonList rbl = new RadioButtonList ();
  259. rbl.Items.Add (new ListItem ("Uno", "1"));
  260. rbl.Items.Add (new ListItem ("Dos", "2"));
  261. rbl.Items.Add (new ListItem ("Tres", "3"));
  262. rbl.SelectedIndex = 2;
  263. NameValueCollection nvc = new NameValueCollection ();
  264. nvc ["XXX"] = "2";
  265. IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
  266. Assert.AreEqual (true, handler.LoadPostData ("XXX", nvc), "#01");
  267. rbl.SelectedIndexChanged += new EventHandler (OnSelected);
  268. event_called = false;
  269. handler.RaisePostDataChangedEvent ();
  270. Assert.AreEqual (true, event_called, "#02");
  271. Assert.AreEqual ("2", rbl.SelectedValue, "#03");
  272. }
  273. [Test]
  274. public void LoadAndRaise3 ()
  275. {
  276. RadioButtonList rbl = new RadioButtonList ();
  277. rbl.Items.Add (new ListItem ("Uno", "1"));
  278. rbl.Items.Add (new ListItem ("Dos", "2"));
  279. rbl.Items.Add (new ListItem ("Tres", "3"));
  280. rbl.SelectedIndex = 2;
  281. NameValueCollection nvc = new NameValueCollection ();
  282. nvc ["XXX"] = "blah";
  283. IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
  284. Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
  285. }
  286. #if NET_2_0
  287. [Test]
  288. [ExpectedException(typeof(HttpException))]
  289. public void VerifyMultiSelectTest()
  290. {
  291. PokerRadioButtonList list = new PokerRadioButtonList();
  292. list.VerifyMultiSelect();
  293. }
  294. [Test]
  295. public void Defaults ()
  296. {
  297. PokerRadioButtonList r = new PokerRadioButtonList ();
  298. Assert.AreEqual (0, r.RepeatedItemCount, "RepeatedItemCount");
  299. Assert.AreEqual (false, r.HasFooter, "HasFooter");
  300. Assert.AreEqual (false, r.HasHeader, "HasHeader");
  301. Assert.AreEqual (false, r.HasSeparators, "HasSeparators");
  302. }
  303. [Test]
  304. [Category ("NunitWeb")]
  305. public void GetItemStyle ()
  306. {
  307. WebTest t = new WebTest (PageInvoker.CreateOnLoad (GetItemStyle_Load));
  308. string html = t.Run ();
  309. string ctrl = HtmlDiff.GetControlFromPageHtml (html);
  310. if (ctrl == string.Empty)
  311. Assert.Fail ("RadioButtonList not created fail");
  312. Assert.AreEqual ("GetItemStyle", (string) t.UserData, "GetItemStyle not done");
  313. if (ctrl.IndexOf ("<td style=\"background-color:Red;border-style:Solid;\">") == -1)
  314. Assert.Fail ("RadioButtonList style not rendered");
  315. }
  316. public static void GetItemStyle_Load (Page p)
  317. {
  318. PokerRadioButtonList rbl = new PokerRadioButtonList ();
  319. rbl.Items.Add (new ListItem ("Uno", "1"));
  320. rbl.Items.Add (new ListItem ("Dos", "2"));
  321. rbl.Items.Add (new ListItem ("Tres", "3"));
  322. p.Form.Controls.Add (new LiteralControl (HtmlDiff.BEGIN_TAG));
  323. p.Form.Controls.Add (rbl);
  324. p.Form.Controls.Add (new LiteralControl (HtmlDiff.END_TAG));
  325. }
  326. [Test]
  327. public void RaisePostDataChangedEvent ()
  328. {
  329. PokerRadioButtonList r = new PokerRadioButtonList ();
  330. r.SelectedIndexChanged += new EventHandler (r_SelectedIndexChanged);
  331. Assert.AreEqual (false, eventSelectedIndexChanged, "Before");
  332. r.DoRaisePostDataChangedEvent ();
  333. Assert.AreEqual (true, eventSelectedIndexChanged, "After");
  334. }
  335. bool eventSelectedIndexChanged;
  336. void r_SelectedIndexChanged (object sender, EventArgs e)
  337. {
  338. eventSelectedIndexChanged = true;
  339. }
  340. [Test]
  341. [Category ("NunitWeb")]
  342. public void RaisePostDataChangedEvent_PostBack ()
  343. {
  344. WebTest t = new WebTest (PageInvoker.CreateOnInit (RaisePostDataChangedEvent_Init));
  345. string html = t.Run ();
  346. FormRequest fr = new FormRequest (t.Response, "form1");
  347. fr.Controls.Add ("__EVENTTARGET");
  348. fr.Controls.Add ("__EVENTARGUMENT");
  349. fr.Controls.Add ("RadioButtonList1");
  350. fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
  351. fr.Controls["__EVENTARGUMENT"].Value = "";
  352. fr.Controls["RadioButtonList1"].Value = "test";
  353. t.Request = fr;
  354. t.Run ();
  355. if (t.UserData == null)
  356. Assert.Fail ("RaisePostDataChangedEvent Failed#1");
  357. Assert.AreEqual ("SelectedIndexChanged", (string) t.UserData, "RaisePostDataChangedEvent Failed#2");
  358. }
  359. public static void RaisePostDataChangedEvent_Init (Page p)
  360. {
  361. TestRadioButtonList r = new TestRadioButtonList ();
  362. r.ID = "RadioButtonList1";
  363. r.Items.Add (new ListItem ("test", "test"));
  364. r.SelectedIndexChanged += new EventHandler (event_SelectedIndexChanged);
  365. p.Form.Controls.Add (r);
  366. }
  367. public static void event_SelectedIndexChanged (object sender, EventArgs e)
  368. {
  369. WebTest.CurrentTest.UserData = "SelectedIndexChanged";
  370. }
  371. #region help_class
  372. class Poker : RadioButtonList
  373. {
  374. protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
  375. {
  376. if (WebTest.CurrentTest.UserData == null) {
  377. ArrayList list = new ArrayList ();
  378. list.Add ("LoadPostData");
  379. WebTest.CurrentTest.UserData = list;
  380. }
  381. else {
  382. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  383. if (list == null)
  384. throw new NullReferenceException ();
  385. list.Add ("LoadPostData");
  386. WebTest.CurrentTest.UserData = list;
  387. }
  388. return base.LoadPostData (postDataKey, postCollection);
  389. }
  390. protected override void OnLoad (EventArgs e)
  391. {
  392. if (WebTest.CurrentTest.UserData == null) {
  393. ArrayList list = new ArrayList ();
  394. list.Add ("ControlLoad");
  395. WebTest.CurrentTest.UserData = list;
  396. }
  397. else {
  398. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  399. if (list == null)
  400. throw new NullReferenceException ();
  401. list.Add ("ControlLoad");
  402. WebTest.CurrentTest.UserData = list;
  403. }
  404. base.OnLoad (e);
  405. }
  406. }
  407. #endregion
  408. [Test]
  409. [Category ("NunitWeb")]
  410. public void LoadPostData () //Just flow and not implementation detail
  411. {
  412. WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
  413. string html = t.Run ();
  414. FormRequest fr = new FormRequest (t.Response, "form1");
  415. fr.Controls.Add ("__EVENTTARGET");
  416. fr.Controls.Add ("__EVENTARGUMENT");
  417. fr.Controls.Add ("RadioButtonList1");
  418. fr.Controls["__EVENTTARGET"].Value = "RadioButtonList1";
  419. fr.Controls["__EVENTARGUMENT"].Value = "";
  420. fr.Controls["RadioButtonList1"].Value = "test";
  421. t.Request = fr;
  422. t.Run ();
  423. ArrayList eventlist = t.UserData as ArrayList;
  424. if (eventlist == null)
  425. Assert.Fail ("User data does not been created fail");
  426. Assert.AreEqual ("ControlLoad", eventlist[0], "Live Cycle Flow #1");
  427. Assert.AreEqual ("PageLoad", eventlist[1], "Live Cycle Flow #2");
  428. Assert.AreEqual ("ControlLoad", eventlist[2], "Live Cycle Flow #3");
  429. Assert.AreEqual ("LoadPostData", eventlist[3], "Live Cycle Flow #4");
  430. }
  431. public static void LoadPostData_Load (Page p)
  432. {
  433. Poker b = new Poker ();
  434. b.ID = "RadioButtonList1";
  435. b.Items.Add (new ListItem ("test", "test"));
  436. p.Form.Controls.Add (b);
  437. if (p.IsPostBack) {
  438. if (WebTest.CurrentTest.UserData == null) {
  439. ArrayList list = new ArrayList ();
  440. list.Add ("PageLoad");
  441. WebTest.CurrentTest.UserData = list;
  442. }
  443. else {
  444. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  445. if (list == null)
  446. throw new NullReferenceException ();
  447. list.Add ("PageLoad");
  448. WebTest.CurrentTest.UserData = list;
  449. }
  450. }
  451. }
  452. [Test]
  453. public void RepeatedItemCount ()
  454. {
  455. PokerRadioButtonList r = new PokerRadioButtonList ();
  456. Assert.AreEqual (0, r.RepeatedItemCount, "RepeatedItemCount#1");
  457. r.Items.Add (new ListItem ("Uno", "1"));
  458. Assert.AreEqual (1, r.RepeatedItemCount, "RepeatedItemCount#2");
  459. r.Items.Add (new ListItem ("Dos", "2"));
  460. Assert.AreEqual (2, r.RepeatedItemCount, "RepeatedItemCount#3");
  461. r.Items.Remove (r.Items[1]);
  462. Assert.AreEqual (1, r.RepeatedItemCount, "RepeatedItemCount#4");
  463. }
  464. [TestFixtureTearDown]
  465. public void TearDown ()
  466. {
  467. WebTest.Unload ();
  468. }
  469. #endif
  470. }
  471. }