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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
//
// CheckBoxTest.cs
// - Unit tests for System.Web.UI.WebControls.CheckBox
//
// 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 System.Drawing;
using System.Collections.Specialized;
using NUnit.Framework;
using MonoTests.stand_alone.WebHarness;
using MonoTests.SystemWeb.Framework;
using System.Collections;
namespace MonoTests.System.Web.UI.WebControls {
public class TestCheckBox : CheckBox {
public StateBag StateBag {
get { return base.ViewState; }
}
public string Render ()
{
HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
base.Render (writer);
return writer.InnerWriter.ToString ();
}
public void TrackState ()
{
TrackViewState ();
}
public object SaveState ()
{
return SaveViewState ();
}
public void LoadState (object o)
{
LoadViewState (o);
}
public void LoadPostbackData (string value)
{
NameValueCollection nvc = new NameValueCollection ();
if (value != null)
nvc.Add ("mykey", value);
((IPostBackDataHandler) this).LoadPostData ("mykey", nvc);
}
}
public class PokerCheckBox : CheckBox
{
private bool checker;
public bool Checker
{
get { return checker; }
}
public void Reset ()
{
checker = false;
}
public string Render ()
{
HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
base.Render (writer);
return writer.InnerWriter.ToString ();
}
override protected void AddAttributesToRender (HtmlTextWriter writer)
{
Reset ();
base.AddAttributesToRender (writer);
writer.AddAttribute ("Attribute", "AttributeValue");
writer.AddStyleAttribute ("StyleAttribute", "StyleAttValue");
checker = true;
}
public new void RaisePostDataChangedEvent ()
{
base.RaisePostDataChangedEvent ();
}
protected override bool LoadPostData (string postDataKey, 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 internal override void OnLoad (EventArgs e)
{
if (this.Page.IsPostBack) {
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);
}
}
[TestFixture]
public class CheckBoxTest {
[Test]
public void DefaultProperties ()
{
TestCheckBox c = new TestCheckBox ();
Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
Assert.IsFalse (c.AutoPostBack, "AutoPostBack");
Assert.IsFalse (c.Checked, "Checked");
Assert.AreEqual (String.Empty, c.Text, "Text");
Assert.AreEqual (TextAlign.Right, c.TextAlign, "TextAlign");
Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count-2");
Assert.IsFalse (c.CausesValidation, "CausesValidation");
Assert.AreEqual (String.Empty, c.ValidationGroup, "ValidationGroup");
}
[Test]
public void InputAttributesTest ()
{
TestCheckBox c = new TestCheckBox ();
c.InputAttributes.Add ("Atribute", "Test");
string html = c.Render ();
HtmlDiff.AssertAreEqual ("<input type=\"checkbox\" Atribute=\"Test\" />", html, "Input Attribute fail");
}
[Test]
public void InputAttributesTest2 ()
{
TestCheckBox c = new TestCheckBox ();
c.InputAttributes.Add ("value", "value1");
c.Text = "Title";
c.Checked = true;
string origHtml = "<input id=\"\" type=\"checkbox\" checked=\"checked\" value=\"value1\" /><label for=\"\">Title</label>";
string html = c.Render ();
HtmlDiff.AssertAreEqual (origHtml, html, "#A1");
}
[Test]
public void LabelAttributesTest_1 ()
{
TestCheckBox c = new TestCheckBox ();
c.ID = "Check";
c.Text = "CheckBoxText";
c.LabelAttributes.Add ("Atribute", "Test");
string html = c.Render ();
HtmlDiff.AssertAreEqual ("<input id=\"Check\" type=\"checkbox\" name=\"Check\" /><label for=\"Check\" Atribute=\"Test\">CheckBoxText</label>", html, "Label Attributes fail#1");
}
[Test]
public void LabelAttributesTest_2 ()
{
TestCheckBox c = new TestCheckBox ();
c.LabelAttributes.Add ("Atribute", "Test");
string html = c.Render ();
HtmlDiff.AssertAreEqual ("<input type=\"checkbox\" />", html, "Label Attributes fail#2");
}
[Test]
public void AddAttributesToRenderTest_1 ()
{
PokerCheckBox c = new PokerCheckBox ();
c.Text = "CheckBoxText";
string html = c.Render ();
Assert.AreEqual (true, c.Checker, "AddAttributesToRender dosn't called fail");
HtmlDiff.AssertAreEqual ("<input Attribute=\"AttributeValue\" type=\"checkbox\" style=\"StyleAttribute:StyleAttValue;\" /><label for>CheckBoxText</label>", html, "Add Attributes To Render fail#1");
}
[Test]
public void AddAttributesToRenderTest_2 ()
{
PokerCheckBox c = new PokerCheckBox ();
string html = c.Render ();
Assert.AreEqual (true, c.Checker, "AddAttributesToRender dosn't called fail");
HtmlDiff.AssertAreEqual ("<input Attribute=\"AttributeValue\" type=\"checkbox\" style=\"StyleAttribute:StyleAttValue;\" />", html, "Add Attributes To Render fail#2");
}
[Test]
public void NullProperties ()
{
TestCheckBox c = new TestCheckBox ();
c.Text = null;
Assert.AreEqual (String.Empty, c.Text, "Text");
c.TextAlign = TextAlign.Right;
Assert.AreEqual (TextAlign.Right, c.TextAlign, "TextAlign");
c.AutoPostBack = true;
Assert.IsTrue (c.AutoPostBack, "AutoPostBack");
c.Checked = true;
Assert.IsTrue (c.Checked, "Checked");
Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
Assert.AreEqual (3, c.StateBag.Count, "ViewState.Count-1");
}
[Test]
public void CleanProperties ()
{
TestCheckBox c = new TestCheckBox ();
c.Text = "text";
Assert.AreEqual ("text", c.Text, "Text");
c.AutoPostBack = true;
c.TextAlign = TextAlign.Left;
c.Checked = true;
Assert.AreEqual (4, c.StateBag.Count, "ViewState.Count");
Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
c.Text = null;
c.AutoPostBack = false;
c.TextAlign = TextAlign.Right;
c.Checked = false;
// If Text is null it is removed from the ViewState
Assert.AreEqual (3, c.StateBag.Count, "ViewState.Count-2");
// This was failing on mono, because the
// viewstate is holding an int not an enum.
// (it passes on ms)
Assert.AreEqual (TextAlign.Right, c.StateBag["TextAlign"], "TextAlign");
Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count-2");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TextAlign_Invalid ()
{
CheckBox c = new CheckBox ();
c.TextAlign = (TextAlign)Int32.MinValue;
}
[Test]
public void TextAlign_Values ()
{
CheckBox c = new CheckBox ();
foreach (TextAlign ta in Enum.GetValues (typeof (TextAlign))) {
c.TextAlign = ta;
}
}
[Test]
public void Render ()
{
TestCheckBox c = new TestCheckBox ();
c.ID = "ID";
c.Text = "Text";
c.TextAlign = TextAlign.Left;
Assert.AreEqual (@"<label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" />", c.Render (), "R#1");
c.TextAlign = TextAlign.Right;
Assert.AreEqual (@"<input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label>", c.Render (), "R#2");
c.Attributes ["style"] = "color:red;";
c.TextAlign = TextAlign.Left;
Assert.AreEqual (@"<span style=""color:red;""><label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" /></span>",
c.Render (), "R#3");
c.TextAlign = TextAlign.Right;
Assert.AreEqual (@"<span style=""color:red;""><input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label></span>",
c.Render (), "R#4");
c.Attributes ["style"] = null;
c.ForeColor = Color.Red;
c.TextAlign = TextAlign.Left;
Assert.AreEqual (@"<span style=""color:Red;""><label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" /></span>",
c.Render (), "R#4");
c.TextAlign = TextAlign.Right;
Assert.AreEqual (@"<span style=""color:Red;""><input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label></span>",
c.Render (), "R#6");
}
//
// Code like
// if (value == null)
// ViewState.Remove ("Text");
// else
// ViewState ["Text"] = value
// is wrong. We need to store when we are tracking viewstate.
//
// Statebag takes care of this behavior, so the code is not needed.
//
[Test]
public void CheckboxViewstateTextNull ()
{
TestCheckBox c = new TestCheckBox ();
c.Text = "text";
c.TrackState ();
c.Text = null;
Assert.AreEqual ("", c.Text);
object o = c.SaveState ();
c = new TestCheckBox ();
c.Text = "text";
c.TrackState ();
c.LoadState (o);
Assert.AreEqual ("", c.Text);
}
[Test]
public void CheckboxViewstateValidation ()
{
// for some reason, MS doesn't save the validation
// properties to the viewstate for the Checkbox
// control. why not?
TestCheckBox o = new TestCheckBox ();
o.ValidationGroup = "VG1";
o.CausesValidation = true;
o.TrackState ();
Assert.AreEqual ("VG1", o.ValidationGroup, "V1");
Assert.IsTrue (o.CausesValidation, "V2");
object state = o.SaveState ();
TestCheckBox copy = new TestCheckBox ();
copy.LoadState (state);
Assert.AreEqual ("", copy.ValidationGroup, "V3");
Assert.IsFalse (copy.CausesValidation, "V4");
}
[Test]
public void RaisePostDataChangedEvent ()
{
PokerCheckBox o = new PokerCheckBox ();
o.CheckedChanged += new EventHandler (o_CheckedChanged);
Assert.AreEqual (false, eventCheckedChanged, "RaisePostDataChangedEven#1");
o.RaisePostDataChangedEvent ();
Assert.AreEqual (true, eventCheckedChanged, "RaisePostDataChangedEven#2");
}
bool eventCheckedChanged;
void o_CheckedChanged (object sender, EventArgs e)
{
eventCheckedChanged = true;
}
[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["__EVENTTARGET"].Value = "pb";
fr.Controls["__EVENTARGUMENT"].Value = "";
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 ("PageLoad", eventlist[0], "Live Cycle Flow #1");
Assert.AreEqual ("ControlLoad", eventlist[1], "Live Cycle Flow #2");
Assert.AreEqual ("LoadPostData", eventlist[2], "Live Cycle Flow #3");
}
public static void LoadPostData_Load (Page p)
{
PokerCheckBox b = new PokerCheckBox ();
b.AutoPostBack = true;
b.ID = "pb";
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 ();
}
}
}
|