RadioButtonListTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. namespace MonoTests.System.Web.UI.WebControls {
  39. [TestFixture]
  40. public class RadioButtonListTest {
  41. public class TestRadioButtonList : RadioButtonList {
  42. public StateBag StateBag {
  43. get { return base.ViewState; }
  44. }
  45. public string Render ()
  46. {
  47. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  48. base.Render (writer);
  49. return writer.InnerWriter.ToString ();
  50. }
  51. }
  52. [Test]
  53. public void RadioButtonList_Constructor ()
  54. {
  55. TestRadioButtonList r = new TestRadioButtonList ();
  56. Assert.AreEqual (-1, r.CellPadding, "A1");
  57. Assert.AreEqual (-1, r.CellSpacing, "A2");
  58. Assert.AreEqual (0, r.RepeatColumns, "A3");
  59. Assert.AreEqual (RepeatDirection.Vertical, r.RepeatDirection, "A4");
  60. Assert.AreEqual (RepeatLayout.Table, r.RepeatLayout, "A5");
  61. Assert.AreEqual (TextAlign.Right, r.TextAlign, "A6");
  62. Assert.AreEqual (false, ((IRepeatInfoUser)r).HasFooter, "A7");
  63. Assert.AreEqual (false, ((IRepeatInfoUser)r).HasHeader, "A8");
  64. Assert.AreEqual (false, ((IRepeatInfoUser)r).HasSeparators, "A9");
  65. Assert.AreEqual (0, ((IRepeatInfoUser)r).RepeatedItemCount, "A10");
  66. }
  67. [Test]
  68. public void CellPaddingProperties ()
  69. {
  70. TestRadioButtonList r = new TestRadioButtonList ();
  71. r.CellPadding = 5;
  72. Assert.AreEqual (5, r.CellPadding, "setting");
  73. string s = r.Render ();
  74. #if NET_2_0
  75. // FIXME: missing some info to start rendering ?
  76. Assert.AreEqual (String.Empty, s, "htmloutput");
  77. #else
  78. Assert.IsTrue (s.ToLower ().IndexOf ("cellpadding=\"5\"") != -1, "htmloutput");
  79. #endif
  80. }
  81. [Test]
  82. public void CellSpacingProperties ()
  83. {
  84. TestRadioButtonList r = new TestRadioButtonList ();
  85. r.CellSpacing = 5;
  86. Assert.AreEqual (5, r.CellSpacing, "setting");
  87. string s = r.Render ();
  88. #if NET_2_0
  89. // FIXME: missing some info to start rendering ?
  90. Assert.AreEqual (String.Empty, s, "htmloutput");
  91. #else
  92. Assert.IsTrue (s.ToLower ().IndexOf ("cellspacing=\"5\"") != -1, "htmloutput");
  93. #endif
  94. }
  95. [Test]
  96. #if NET_2_0
  97. // FIXME: missing some info to start rendering ?
  98. [ExpectedException (typeof (NullReferenceException))]
  99. #endif
  100. public void Render ()
  101. {
  102. TestRadioButtonList c = new TestRadioButtonList ();
  103. c.Items.Add (new ListItem ("text2", "value1"));
  104. string s = c.Render ();
  105. Assert.IsTrue (s.ToLower ().IndexOf (" type=\"radio\"") != -1, "type");
  106. Assert.IsTrue (s.ToLower ().IndexOf ("value1") != -1, "value");
  107. Assert.IsTrue (s.ToLower ().IndexOf ("text2") != -1, "text");
  108. }
  109. // Exceptions
  110. [Test]
  111. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  112. public void RepeatColumnsException ()
  113. {
  114. TestRadioButtonList r = new TestRadioButtonList ();
  115. r.RepeatColumns = -1;
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  119. public void RepeatDirectionException ()
  120. {
  121. TestRadioButtonList r = new TestRadioButtonList ();
  122. r.RepeatDirection = (RepeatDirection) 4;
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  126. public void RepeatLayoutException ()
  127. {
  128. TestRadioButtonList r = new TestRadioButtonList ();
  129. r.RepeatLayout = (RepeatLayout) 3;
  130. }
  131. bool event_called;
  132. void OnSelected (object sender, EventArgs args)
  133. {
  134. event_called = true;
  135. }
  136. [Test]
  137. public void LoadAndRaise1 ()
  138. {
  139. RadioButtonList rbl = new RadioButtonList ();
  140. rbl.Items.Add (new ListItem ("Uno", "1"));
  141. rbl.Items.Add (new ListItem ("Dos", "2"));
  142. rbl.Items.Add (new ListItem ("Tres", "3"));
  143. rbl.SelectedIndex = 2;
  144. NameValueCollection nvc = new NameValueCollection ();
  145. nvc ["XXX"] = "3";
  146. IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
  147. #if NET_2_0
  148. Assert.IsFalse (handler.LoadPostData ("XXX", nvc), "#01");
  149. #else
  150. Assert.IsTrue (handler.LoadPostData ("XXX", nvc), "#01");
  151. #endif
  152. rbl.SelectedIndexChanged += new EventHandler (OnSelected);
  153. event_called = false;
  154. handler.RaisePostDataChangedEvent ();
  155. #if NET_2_0
  156. Assert.IsTrue (event_called, "#02");
  157. #else
  158. // Not called. Value is the same as the selected previously
  159. Assert.IsFalse (event_called, "#02");
  160. #endif
  161. Assert.AreEqual ("3", rbl.SelectedValue, "#03");
  162. }
  163. [Test]
  164. public void LoadAndRaise2 ()
  165. {
  166. RadioButtonList rbl = new RadioButtonList ();
  167. rbl.Items.Add (new ListItem ("Uno", "1"));
  168. rbl.Items.Add (new ListItem ("Dos", "2"));
  169. rbl.Items.Add (new ListItem ("Tres", "3"));
  170. rbl.SelectedIndex = 2;
  171. NameValueCollection nvc = new NameValueCollection ();
  172. nvc ["XXX"] = "2";
  173. IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
  174. Assert.AreEqual (true, handler.LoadPostData ("XXX", nvc), "#01");
  175. rbl.SelectedIndexChanged += new EventHandler (OnSelected);
  176. event_called = false;
  177. handler.RaisePostDataChangedEvent ();
  178. Assert.AreEqual (true, event_called, "#02");
  179. Assert.AreEqual ("2", rbl.SelectedValue, "#03");
  180. }
  181. [Test]
  182. public void LoadAndRaise3 ()
  183. {
  184. RadioButtonList rbl = new RadioButtonList ();
  185. rbl.Items.Add (new ListItem ("Uno", "1"));
  186. rbl.Items.Add (new ListItem ("Dos", "2"));
  187. rbl.Items.Add (new ListItem ("Tres", "3"));
  188. rbl.SelectedIndex = 2;
  189. NameValueCollection nvc = new NameValueCollection ();
  190. nvc ["XXX"] = "blah";
  191. IPostBackDataHandler handler = (IPostBackDataHandler) rbl;
  192. Assert.AreEqual (false, handler.LoadPostData ("XXX", nvc), "#01");
  193. }
  194. }
  195. }