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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
//
// HtmlSelectTest.cs
// - Unit tests for System.Web.UI.HtmlControls.HtmlSelect
//
// Author:
// Dick Porter <dick@ximian.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.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using NUnit.Framework;
using MonoTests.stand_alone.WebHarness;
namespace MonoTests.System.Web.UI.HtmlControls {
public class TestHtmlSelect : HtmlSelect {
public TestHtmlSelect ()
{
}
public StateBag GetViewState ()
{
return (ViewState);
}
public HtmlTextWriter GetWriter ()
{
StringWriter text = new StringWriter ();
HtmlTextWriter writer = new HtmlTextWriter (text);
base.RenderAttributes (writer);
return (writer);
}
public int[] GetIndices ()
{
return (SelectedIndices);
}
public void Clear ()
{
ClearSelection ();
}
public void SetIndices (int[] indices)
{
Select (indices);
}
public string Render ()
{
StringWriter sw = new StringWriter ();
sw.NewLine = "\n";
HtmlTextWriter writer = new HtmlTextWriter (sw);
base.Render (writer);
return writer.InnerWriter.ToString ();
}
}
[TestFixture]
public class HtmlSelectTest {
[Test]
public void DefaultProperties ()
{
HtmlSelect sel = new HtmlSelect ();
Assert.AreEqual (0, sel.Attributes.Count, "Attributes.Count");
Assert.AreEqual (String.Empty, sel.DataMember, "DataMember");
Assert.AreEqual (null, sel.DataSource, "DataSource");
Assert.AreEqual (String.Empty, sel.DataTextField, "DataTextField");
Assert.AreEqual (String.Empty, sel.DataValueField, "DataValueField");
Assert.IsTrue (sel.Items.GetType() == typeof (ListItemCollection), "Items");
Assert.AreEqual (false, sel.Multiple, "Multiple");
Assert.AreEqual (0, sel.SelectedIndex, "SelectedIndex");
Assert.AreEqual (-1, sel.Size, "Size");
Assert.AreEqual (String.Empty, sel.Value, "Value");
Assert.AreEqual ("select", sel.TagName, "TagName");
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void InnerHtmlGet ()
{
HtmlSelect sel = new HtmlSelect ();
Assert.IsNotNull (sel.InnerHtml);
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void InnerTextGet ()
{
HtmlSelect sel = new HtmlSelect ();
sel.InnerText = null;
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void InnerHtmlSet ()
{
HtmlSelect sel = new HtmlSelect ();
sel.InnerHtml = null;
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void InnerTextSet ()
{
HtmlSelect sel = new HtmlSelect ();
Assert.IsNotNull (sel.InnerText);
}
[Test]
public void NullProperties ()
{
TestHtmlSelect sel = new TestHtmlSelect ();
sel.DataMember = null;
Assert.AreEqual (String.Empty, sel.DataMember, "DataMember");
sel.DataSource = null;
Assert.AreEqual (null, sel.DataSource, "DataSource");
sel.DataTextField = null;
Assert.AreEqual (String.Empty, sel.DataTextField, "DataTextField");
sel.DataValueField = null;
Assert.AreEqual (String.Empty, sel.DataValueField, "DataValueField");
sel.Multiple = false;
Assert.AreEqual (false, sel.Multiple, "Multiple");
sel.SelectedIndex = -1;
Assert.AreEqual (0, sel.SelectedIndex, "SelectedIndex");
sel.Size = -1;
Assert.AreEqual (-1, sel.Size, "Size");
sel.Value = null;
Assert.AreEqual (String.Empty, sel.Value, "Value");
Assert.AreEqual (0, sel.Attributes.Count, "Attributes.Count");
StateBag sb = sel.GetViewState ();
Assert.AreEqual (0, sb.Count, "ViewState Count");
}
[Test]
public void SourceType ()
{
HtmlSelect sel = new HtmlSelect ();
int[] source = new int[] {1,2,3,4};
sel.DataSource = source;
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void BadSourceType ()
{
HtmlSelect sel = new HtmlSelect ();
object source = new object ();
sel.DataSource = source;
}
[Test]
public void ViewStateCount ()
{
TestHtmlSelect sel = new TestHtmlSelect ();
int[] source = new int[] {1,2,3,4,5,6,7,8};
sel.DataMember = "*1*";
Assert.AreEqual ("*1*", sel.DataMember, "DataMember");
sel.DataSource = source;
Assert.AreEqual (source, sel.DataSource, "DataSource");
sel.DataTextField = "*3*";
Assert.AreEqual ("*3*", sel.DataTextField, "DataTextField");
sel.DataValueField = "*4*";
Assert.AreEqual ("*4*", sel.DataValueField, "DataValueField");
sel.Multiple = true;
Assert.AreEqual (true, sel.Multiple, "Multiple");
sel.SelectedIndex = 6;
/* This doesn't stick */
Assert.AreEqual (-1, sel.SelectedIndex, "SelectedIndex");
sel.Size = 7;
Assert.AreEqual (7, sel.Size, "Size");
sel.Value = "*8*";
/* Neither does this */
Assert.AreEqual (String.Empty, sel.Value, "Value");
Assert.AreEqual (5, sel.Attributes.Count, "Attributes.Count");
/*
foreach (string key in sel.Attributes.Keys) {
Console.WriteLine ("attr key {0} is {1}", key, sel.Attributes[key]);
}
*/
StateBag sb = sel.GetViewState ();
Assert.AreEqual (5, sb.Count, "ViewState Count");
/*
foreach (string key in sb.Keys) {
Console.WriteLine ("vs key {0} is {1}", key, sb[key]);
}
*/
}
[Test]
public void InternalDetails ()
{
/* Test the undocumented but visible
* properties and methods SelectedIndices,
* ClearSelection () and Select ()
*/
TestHtmlSelect sel = new TestHtmlSelect ();
ListItemCollection items;
items = sel.Items;
ListItem item1 = new ListItem ("text1", "value1");
items.Add (item1);
ListItem item2 = new ListItem ("text2", "value2");
item2.Selected = true;
items.Add (item2);
ListItem item3 = new ListItem ("text3", "value3");
items.Add (item3);
ListItem item4 = new ListItem ("text4", "value4");
item4.Selected = true;
items.Add (item4);
ListItem item5 = new ListItem ("text5", "value5");
item5.Selected = true;
items.Add (item5);
ListItem item6 = new ListItem ("text6", "value6");
items.Add (item6);
int[] indices = sel.GetIndices ();
Assert.AreEqual (1, sel.SelectedIndex, "SelectedIndex");
Assert.AreEqual (3, indices.Length, "SelectIndices Length");
Assert.AreEqual (1, indices[0], "SelectIndices 0");
Assert.AreEqual (3, indices[1], "SelectIndices 1");
Assert.AreEqual (4, indices[2], "SelectIndices 2");
Assert.IsFalse (item1.Selected, "Item1");
Assert.IsTrue (item2.Selected, "Item2");
Assert.IsFalse (item3.Selected, "Item3");
Assert.IsTrue (item4.Selected, "Item4");
Assert.IsTrue (item5.Selected, "Item5");
Assert.IsFalse (item6.Selected, "Item6");
sel.Clear ();
Assert.IsFalse (item1.Selected, "Item1 after clear but before SelectedIndices and SelectedIndex");
indices = sel.GetIndices ();
Assert.IsFalse (item1.Selected, "Item1 after clear but between SelectedIndices and SelectedIndex");
Assert.AreEqual (0, sel.SelectedIndex, "SelectedIndex after clear");
Assert.AreEqual (0, indices.Length, "SelectIndices Length after clear");
/* NB: !multiple and size <= 1, therefore
* there must be one selected after
* SelectedIndex is called
*/
Assert.IsTrue (item1.Selected, "Item1 after clear");
Assert.IsFalse (item2.Selected, "Item2 after clear");
Assert.IsFalse (item3.Selected, "Item3 after clear");
Assert.IsFalse (item4.Selected, "Item4 after clear");
Assert.IsFalse (item5.Selected, "Item5 after clear");
Assert.IsFalse (item6.Selected, "Item6 after clear");
int[] new_indices = new int[]{2, 4, 5};
sel.SetIndices (new_indices);
indices = sel.GetIndices ();
Assert.AreEqual (2, sel.SelectedIndex, "SelectedIndex after set");
Assert.AreEqual (3, indices.Length, "SelectIndices Length after set");
Assert.AreEqual (2, indices[0], "SelectIndices 0 after set");
Assert.AreEqual (4, indices[1], "SelectIndices 1 after set");
Assert.AreEqual (5, indices[2], "SelectIndices 2 after set");
Assert.IsFalse (item1.Selected, "Item1 after set");
Assert.IsFalse (item2.Selected, "Item2 after set");
Assert.IsTrue (item3.Selected, "Item3 after set");
Assert.IsFalse (item4.Selected, "Item4 after set");
Assert.IsTrue (item5.Selected, "Item5 after set");
Assert.IsTrue (item6.Selected, "Item6 after set");
new_indices = new int[]{2};
sel.SetIndices (new_indices);
indices = sel.GetIndices ();
Assert.AreEqual (2, sel.SelectedIndex, "SelectedIndex after short set");
Assert.AreEqual (1, indices.Length, "SelectIndices Length after short set");
Assert.AreEqual (2, indices[0], "SelectIndices 0 after short set");
Assert.IsFalse (item1.Selected, "Item1 after short set");
Assert.IsFalse (item2.Selected, "Item2 after short set");
Assert.IsTrue (item3.Selected, "Item3 after short set");
Assert.IsFalse (item4.Selected, "Item4 after short set");
Assert.IsFalse (item5.Selected, "Item5 after short set");
Assert.IsFalse (item6.Selected, "Item6 after short set");
new_indices = new int[]{-2, -1, 2, 4, 5, 6, 7, 8, 9, 10};
sel.SetIndices (new_indices);
indices = sel.GetIndices ();
Assert.AreEqual (2, sel.SelectedIndex, "SelectedIndex after long set");
Assert.AreEqual (3, indices.Length, "SelectIndices Length after long set");
Assert.AreEqual (2, indices[0], "SelectIndices 0 after long set");
Assert.AreEqual (4, indices[1], "SelectIndices 1 after long set");
Assert.AreEqual (5, indices[2], "SelectIndices 2 after long set");
Assert.IsFalse (item1.Selected, "Item1 after long set");
Assert.IsFalse (item2.Selected, "Item2 after long set");
Assert.IsTrue (item3.Selected, "Item3 after long set");
Assert.IsFalse (item4.Selected, "Item4 after long set");
Assert.IsTrue (item5.Selected, "Item5 after long set");
Assert.IsTrue (item6.Selected, "Item6 after long set");
}
[Test]
public void Multiple ()
{
HtmlSelect sel = new HtmlSelect ();
sel.Multiple = true;
Assert.AreEqual (-1, sel.SelectedIndex, "SelectedIndex");
}
[Test]
public void Big ()
{
HtmlSelect sel = new HtmlSelect ();
sel.Size = 5;
Assert.AreEqual (-1, sel.SelectedIndex, "SelectedIndex");
}
[Test]
public void OneRowIndividual ()
{
HtmlSelect sel = new HtmlSelect ();
Assert.AreEqual (0, sel.SelectedIndex, "SelectedIndex");
}
[Test]
public void RenderAttributes ()
{
TestHtmlSelect sel = new TestHtmlSelect ();
object source = new object ();
sel.DataMember = "*1*";
//sel.DataSource = source;
sel.DataTextField = "*3*";
sel.DataValueField = "*4*";
sel.Multiple = true;
sel.Name = "*6*";
sel.SelectedIndex = 7;
sel.Size = 8;
sel.Value = "*9*";
Assert.AreEqual (5, sel.Attributes.Count, "Attributes.Count");
HtmlTextWriter writer = sel.GetWriter ();
Assert.AreEqual (" name multiple=\"multiple\" size=\"8\"", writer.InnerWriter.ToString ());
}
DataSet GetExampleData ()
{
DataSet ds = new DataSet ();
ds.ReadXml (new StringReader (@"
<DataSet>
<Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
<Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92' />
<Stocks Company='Google' Symbol='GOOG' Price='291.60' />
</DataSet>
"));
return ds;
}
[Test]
public void DataBind1 ()
{
TestHtmlSelect s = new TestHtmlSelect ();
s.DataSource = GetExampleData ();
s.DataTextField = "Symbol";
s.DataBind ();
string exp = @"<select name>
<option value=""NOVL"">NOVL</option>
<option value=""MSFT"">MSFT</option>
<option value=""GOOG"">GOOG</option>
</select>";
HtmlDiff.AssertAreEqual (exp, s.Render (), "DataBind1");
}
DataSet GetExampleDataWithEmpty ()
{
DataSet ds = new DataSet ();
ds.ReadXml (new StringReader (@"
<DataSet>
<Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
<Stocks Company='' Symbol='MSFT' Price='25.92' />
<Stocks Company='Google' Symbol='GOOG' Price='291.60' />
</DataSet>
"));
return ds;
}
[Test]
public void DataBind2 ()
{
TestHtmlSelect s = new TestHtmlSelect ();
s.DataSource = GetExampleDataWithEmpty ();
s.DataTextField = "Company";
s.DataValueField = "Symbol";
s.DataBind ();
string exp = @"<select name>
<option value=""NOVL"">Novell Inc.</option>
<option value=""MSFT""></option>
<option value=""GOOG"">Google</option>
</select>";
HtmlDiff.AssertAreEqual (exp, s.Render (), "DataBind2");
}
[Test]
public void DataBind3 ()
{
TestHtmlSelect s = new TestHtmlSelect ();
s.DataSource = new string [] { "A", "B", "C" };
s.DataBind ();
string exp = @"<select name>
<option value=""A"">A</option>
<option value=""B"">B</option>
<option value=""C"">C</option>
</select>";
HtmlDiff.AssertAreEqual (exp, s.Render (), "DataBind3");
}
[Test]
public void DataBindDoubleCall ()
{
TestHtmlSelect s = new TestHtmlSelect ();
s.DataSource = new string [] { "A", "B", "C" };
s.DataBind ();
s.DataBind ();
string exp = @"<select name>
<option value=""A"">A</option>
<option value=""B"">B</option>
<option value=""C"">C</option>
</select>";
HtmlDiff.AssertAreEqual (exp, s.Render (), "DataBindDoubleCall");
}
[Test]
public void HtmlEncodeValues ()
{
TestHtmlSelect s = new TestHtmlSelect ();
s.DataSource = new string [] { "&", "<" };
s.DataBind ();
string exp = @"<select name>
<option value=""&"">&</option>
<option value=""<""><</option>
</select>";
HtmlDiff.AssertAreEqual (exp, s.Render (), "HtmlEncodeValues");
}
}
}
|