1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
//
// RadioButtonTest.cs
// - Unit tests for System.Web.UI.WebControls.RadioButton
//
// Author:
// Dick Porter <dick@ximian.com>
// Yoni Klain <yonik@mainsoft.com>
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NUnit.Framework;
using MonoTests.SystemWeb.Framework;
using System.Collections;
namespace MonoTests.System.Web.UI.WebControls {
public class TestRadioButton : RadioButton {
public StateBag StateBag {
get { return base.ViewState; }
}
public string Render ()
{
HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
base.Render (writer);
return writer.InnerWriter.ToString ();
}
#if NET_2_0
public new void RaisePostDataChangedEvent ()
{
base.RaisePostDataChangedEvent ();
}
#endif
}
[TestFixture]
public class RadioButtonTest {
[Test]
public void DefaultProperties ()
{
TestRadioButton r = new TestRadioButton ();
Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count");
Assert.IsFalse (r.AutoPostBack, "AutoPostBack");
Assert.IsFalse (r.Checked, "Checked");
Assert.AreEqual (String.Empty, r.Text, "Text");
Assert.AreEqual (TextAlign.Right, r.TextAlign, "TextAlign");
Assert.AreEqual (String.Empty, r.GroupName, "GroupName");
Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count-2");
}
[Test]
public void NullProperties ()
{
TestRadioButton r = new TestRadioButton ();
r.Text = null;
Assert.AreEqual (String.Empty, r.Text, "Text");
r.TextAlign = TextAlign.Right;
Assert.AreEqual (TextAlign.Right, r.TextAlign, "TextAlign");
r.AutoPostBack = true;
Assert.IsTrue (r.AutoPostBack, "AutoPostBack");
r.Checked = true;
Assert.IsTrue (r.Checked, "Checked");
r.GroupName = null;
Assert.AreEqual (String.Empty, r.GroupName, "GroupName");
Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count");
Assert.AreEqual (3, r.StateBag.Count, "ViewState.Count-1");
}
[Test]
public void CleanProperties ()
{
TestRadioButton r = new TestRadioButton ();
r.Text = "text";
Assert.AreEqual ("text", r.Text, "Text");
r.AutoPostBack = true;
r.TextAlign = TextAlign.Left;
r.Checked = true;
r.GroupName = "groupname";
Assert.AreEqual ("groupname", r.GroupName, "GroupName");
Assert.AreEqual (5, r.StateBag.Count, "ViewState.Count");
Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count");
r.Text = null;
r.AutoPostBack = false;
r.TextAlign = TextAlign.Right;
r.Checked = false;
r.GroupName = null;
// If Text is null it is removed from the
// ViewState. Ditto GroupName
Assert.AreEqual (3, r.StateBag.Count, "ViewState.Count-2");
Assert.AreEqual (TextAlign.Right, r.StateBag["TextAlign"], "TextAlign");
Assert.AreEqual (0, r.Attributes.Count, "Attributes.Count-2");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TextAlign_Invalid ()
{
RadioButton r = new RadioButton ();
r.TextAlign = (TextAlign)Int32.MinValue;
}
[Test]
public void TextAlign_Values ()
{
RadioButton r = new RadioButton ();
foreach (TextAlign ta in Enum.GetValues (typeof (TextAlign))) {
r.TextAlign = ta;
}
}
/* Segfaults on ms runtime */
[Test]
public void Render ()
{
TestRadioButton r = new TestRadioButton ();
string s = r.Render ();
Assert.IsTrue (s.IndexOf (" type=\"radio\"") > 0, "type");
r.Text = "label text";
s = r.Render ();
Assert.IsTrue (s.IndexOf (">label text</label>") > 0, "text");
r.TextAlign = TextAlign.Left;
s = r.Render ();
Assert.IsTrue (s.IndexOf (">label text</label><input") > 0, "text left");
r.TextAlign = TextAlign.Right;
s = r.Render ();
Assert.IsTrue (s.IndexOf ("/><label for") > 0, "text right");
}
[Test]
public void NameAttr1 ()
{
TestRadioButton b1 = new TestRadioButton ();
b1.GroupName = "mono";
TestRadioButton b2 = new TestRadioButton ();
b2.GroupName = "mono";
Page p = new Page ();
#if NET_2_0
p.EnableEventValidation = false;
#endif
p.ID = "MyPage";
p.Controls.Add (b1);
p.Controls.Add (b2);
string t1 = b1.Render ();
Assert.IsTrue (t1.IndexOf ("name=\"mono\"") != -1, "#01");
string t2 = b2.Render ();
Assert.IsTrue (t2.IndexOf ("name=\"mono\"") != -1, "#02");
}
[Test]
public void NameAttr2 () {
TestRadioButton b1 = new TestRadioButton ();
b1.ID = "monoId";
Page p = new Page ();
#if NET_2_0
p.EnableEventValidation = false;
#endif
p.ID = "MyPage";
p.Controls.Add (b1);
string t1 = b1.Render ();
Assert.IsTrue (t1.IndexOf ("name=\"monoId\"") != -1, "#01");
}
#if NET_2_0
[Test]
public void RaisePostDataChangedEvent ()
{
TestRadioButton b = new TestRadioButton ();
b.CheckedChanged += new EventHandler (b_CheckedChanged);
Assert.AreEqual (false, eventCheckedChanged, "before");
b.RaisePostDataChangedEvent ();
Assert.AreEqual (true, eventCheckedChanged, "after");
}
bool eventCheckedChanged;
void b_CheckedChanged (object sender, EventArgs e)
{
eventCheckedChanged = true;
}
[Test]
[Category ("NunitWeb")]
public void RaisePostDataChangedEvent_PostBack ()
{
WebTest t = new WebTest (PageInvoker.CreateOnInit (RaisePostDataChangedEvent_Init));
string html = t.Run ();
FormRequest fr = new FormRequest (t.Response, "form1");
fr.Controls.Add ("__EVENTTARGET");
fr.Controls.Add ("__EVENTARGUMENT");
fr.Controls.Add ("RadioButton1");
fr.Controls["__EVENTTARGET"].Value = "RadioButton1";
fr.Controls["__EVENTARGUMENT"].Value = "";
fr.Controls["RadioButton1"].Value = "RadioButton1";
t.Request = fr;
event_CheckedChanged2_flag = false;
html = t.Run ();
if (t.UserData == null)
Assert.Fail ("RaisePostDataChangedEvent Failed#1");
Assert.AreEqual ("CheckedChanged", (string) t.UserData, "RaisePostDataChangedEvent Failed#2");
Assert.IsFalse (event_CheckedChanged2_flag, "RaisePostDataChangedEvent Failed#3");
}
public static void RaisePostDataChangedEvent_Init (Page p)
{
TestRadioButton b = new TestRadioButton ();
b.ID = "RadioButton1";
b.GroupName = "RadioButton1";
b.CheckedChanged+=new EventHandler(event_CheckedChanged);
p.Form.Controls.Add (b);
TestRadioButton b2 = new TestRadioButton ();
b2.ID = "RadioButton2";
b2.GroupName = "RadioButton1";
b2.CheckedChanged += new EventHandler (event_CheckedChanged2);
p.Form.Controls.Add (b2);
if (!p.IsPostBack)
b2.Checked = true;
}
public static void event_CheckedChanged (object sender, EventArgs e)
{
WebTest.CurrentTest.UserData = "CheckedChanged";
}
static bool event_CheckedChanged2_flag;
public static void event_CheckedChanged2 (object sender, EventArgs e)
{
event_CheckedChanged2_flag = true;
}
#region help_class
class Poker : RadioButton
{
protected override bool LoadPostData (string postDataKey, global::System.Collections.Specialized.NameValueCollection postCollection)
{
if (WebTest.CurrentTest.UserData == null) {
ArrayList list = new ArrayList ();
list.Add ("LoadPostData");
WebTest.CurrentTest.UserData = list;
}
else {
ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
if (list == null)
throw new NullReferenceException ();
list.Add ("LoadPostData");
WebTest.CurrentTest.UserData = list;
}
return base.LoadPostData (postDataKey, postCollection);
}
protected override void OnLoad (EventArgs e)
{
if (WebTest.CurrentTest.UserData == null) {
ArrayList list = new ArrayList ();
list.Add ("ControlLoad");
WebTest.CurrentTest.UserData = list;
}
else {
ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
if (list == null)
throw new NullReferenceException ();
list.Add ("ControlLoad");
WebTest.CurrentTest.UserData = list;
}
base.OnLoad (e);
}
}
#endregion
[Test]
[Category ("NunitWeb")]
public void LoadPostData () //Just flow and not implementation detail
{
WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
string html = t.Run ();
FormRequest fr = new FormRequest (t.Response, "form1");
fr.Controls.Add ("__EVENTTARGET");
fr.Controls.Add ("__EVENTARGUMENT");
fr.Controls.Add ("RadioButton1");
fr.Controls["__EVENTTARGET"].Value = "__Page";
fr.Controls["__EVENTARGUMENT"].Value = "";
fr.Controls ["RadioButton1"].Value = "RadioButton1";
t.Request = fr;
t.Run ();
ArrayList eventlist = t.UserData as ArrayList;
if (eventlist == null)
Assert.Fail ("User data does not been created fail");
Assert.AreEqual ("ControlLoad", eventlist[0], "Live Cycle Flow #1");
Assert.AreEqual ("PageLoad", eventlist[1], "Live Cycle Flow #2");
Assert.AreEqual ("ControlLoad", eventlist[2], "Live Cycle Flow #3");
Assert.AreEqual ("LoadPostData", eventlist[3], "Live Cycle Flow #4");
}
public static void LoadPostData_Load (Page p)
{
Poker b = new Poker();
b.ID = "RadioButton1";
p.Form.Controls.Add (b);
if (p.IsPostBack) {
if (WebTest.CurrentTest.UserData == null) {
ArrayList list = new ArrayList ();
list.Add ("PageLoad");
WebTest.CurrentTest.UserData = list;
}
else {
ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
if (list == null)
throw new NullReferenceException ();
list.Add ("PageLoad");
WebTest.CurrentTest.UserData = list;
}
}
}
[TestFixtureTearDown]
public void TearDown ()
{
WebTest.Unload ();
}
#endif
}
}
|