RadioButtonListTest.cs 6.2 KB

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