RadioButtonTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // RadioButtonTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.RadioButton
  4. //
  5. // Author:
  6. // Dick Porter <[email protected]>
  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 System;
  30. using System.IO;
  31. using System.Web;
  32. using System.Web.UI;
  33. using System.Web.UI.WebControls;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Web.UI.WebControls {
  36. public class TestRadioButton : RadioButton {
  37. public StateBag StateBag {
  38. get { return base.ViewState; }
  39. }
  40. public string Render ()
  41. {
  42. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  43. base.Render (writer);
  44. return writer.InnerWriter.ToString ();
  45. }
  46. }
  47. [TestFixture]
  48. public class RadioButtonTest {
  49. [Test]
  50. public void DefaultProperties ()
  51. {
  52. TestRadioButton r = new TestRadioButton ();
  53. Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count");
  54. Assert.IsFalse (r.AutoPostBack, "AutoPostBack");
  55. Assert.IsFalse (r.Checked, "Checked");
  56. Assert.AreEqual (String.Empty, r.Text, "Text");
  57. Assert.AreEqual (TextAlign.Right, r.TextAlign, "TextAlign");
  58. Assert.AreEqual (String.Empty, r.GroupName, "GroupName");
  59. Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count-2");
  60. }
  61. [Test]
  62. public void NullProperties ()
  63. {
  64. TestRadioButton r = new TestRadioButton ();
  65. r.Text = null;
  66. Assert.AreEqual (String.Empty, r.Text, "Text");
  67. r.TextAlign = TextAlign.Right;
  68. Assert.AreEqual (TextAlign.Right, r.TextAlign, "TextAlign");
  69. r.AutoPostBack = true;
  70. Assert.IsTrue (r.AutoPostBack, "AutoPostBack");
  71. r.Checked = true;
  72. Assert.IsTrue (r.Checked, "Checked");
  73. r.GroupName = null;
  74. Assert.AreEqual (String.Empty, r.GroupName, "GroupName");
  75. Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count");
  76. Assert.AreEqual (3, r.StateBag.Count, "ViewState.Count-1");
  77. }
  78. [Test]
  79. public void CleanProperties ()
  80. {
  81. TestRadioButton r = new TestRadioButton ();
  82. r.Text = "text";
  83. Assert.AreEqual ("text", r.Text, "Text");
  84. r.AutoPostBack = true;
  85. r.TextAlign = TextAlign.Left;
  86. r.Checked = true;
  87. r.GroupName = "groupname";
  88. Assert.AreEqual ("groupname", r.GroupName, "GroupName");
  89. Assert.AreEqual (5, r.StateBag.Count, "ViewState.Count");
  90. Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count");
  91. r.Text = null;
  92. r.AutoPostBack = false;
  93. r.TextAlign = TextAlign.Right;
  94. r.Checked = false;
  95. r.GroupName = null;
  96. // If Text is null it is removed from the
  97. // ViewState. Ditto GroupName
  98. Assert.AreEqual (3, r.StateBag.Count, "ViewState.Count-2");
  99. Assert.AreEqual (TextAlign.Right, r.StateBag["TextAlign"], "TextAlign");
  100. Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count-2");
  101. }
  102. [Test]
  103. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  104. public void TextAlign_Invalid ()
  105. {
  106. RadioButton r = new RadioButton ();
  107. r.TextAlign = (TextAlign)Int32.MinValue;
  108. }
  109. [Test]
  110. public void TextAlign_Values ()
  111. {
  112. RadioButton r = new RadioButton ();
  113. foreach (TextAlign ta in Enum.GetValues (typeof (TextAlign))) {
  114. r.TextAlign = ta;
  115. }
  116. }
  117. /* Segfaults on ms runtime */
  118. [Test]
  119. [Category ("NotDotNet")]
  120. public void Render ()
  121. {
  122. TestRadioButton r = new TestRadioButton ();
  123. string s = r.Render ();
  124. Assert.IsTrue (s.IndexOf (" type=\"radio\"") > 0, "type");
  125. r.Text = "label text";
  126. s = r.Render ();
  127. Assert.IsTrue (s.IndexOf (">label text</label>") > 0, "text");
  128. r.TextAlign = TextAlign.Left;
  129. s = r.Render ();
  130. Assert.IsTrue (s.IndexOf (">label text</label><input") > 0, "text left");
  131. r.TextAlign = TextAlign.Right;
  132. s = r.Render ();
  133. Assert.IsTrue (s.IndexOf ("/><label for") > 0, "text right");
  134. }
  135. }
  136. }