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
|
//
// Tests for System.Web.UI.WebControls.DropDownList.cs
//
// Author:
// Peter Dennis Bartok (pbartok@novell.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.Collections;
using System.Drawing;
using System.IO;
using System.Data;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NUnit.Framework;
using MonoTests.stand_alone.WebHarness;
using System.Collections.Generic;
using MonoTests.SystemWeb.Framework;
namespace MonoTests.System.Web.UI.WebControls
{
[TestFixture]
public class DropDownListTest {
public class NamingContainer : WebControl, INamingContainer {
}
public class DropDownListTestClass : DropDownList {
public DropDownListTestClass ()
: base () {
}
public StateBag StateBag {
get { return base.ViewState; }
}
public string Render () {
HtmlTextWriter writer;
writer = DropDownListTest.GetWriter();
base.Render (writer);
return writer.InnerWriter.ToString ();
}
public bool IsTrackingVS () {
return IsTrackingViewState;
}
public void SetTrackingVS () {
TrackViewState ();
}
public object Save() {
return base.SaveViewState();
}
public void Load(object o) {
base.LoadViewState(o);
}
public new void RenderContents(HtmlTextWriter writer) {
base.RenderContents(writer);
}
public new void CreateControlCollection() {
base.CreateControlCollection();
}
public new void AddAttributesToRender(HtmlTextWriter writer) {
base.AddAttributesToRender(writer);
}
public string[] KeyValuePairs() {
IEnumerator e;
string[] result;
int item;
e = ViewState.GetEnumerator();
result = new string[ViewState.Keys.Count];
item = 0;
while (e.MoveNext()) {
DictionaryEntry d;
StateItem si;
d = (DictionaryEntry)e.Current;
si = (StateItem)d.Value;
if (si.Value is String[]) {
string[] values;
values = (string[]) si.Value;
result[item] = d.Key.ToString() + "=";
if (values.Length > 0) {
result[item] += values[0];
for (int i = 1; i < values.Length; i++) {
result[item] += ", " + values[i];
}
}
} else {
result[item] = d.Key.ToString() + "=" + si.Value;
}
item++;
}
return result;
}
}
private static HtmlTextWriter GetWriter () {
StringWriter sw = new StringWriter ();
sw.NewLine = "\n";
return new HtmlTextWriter (sw);
}
private bool IsEqual(object[] a1, object[] a2, string assertion) {
int matches;
bool[] notfound;
if (a1.Length != a2.Length) {
if (assertion != null) {
Assert.Fail(assertion + "( different length )");
}
return false;
}
matches = 0;
notfound = new bool[a1.Length];
for (int i = 0; i < a1.Length; i++) {
for (int j = 0; j < a2.Length; j++) {
if (a1[i].Equals(a2[j])) {
matches++;
break;
}
}
if ((assertion != null) && (matches != i+1)) {
Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
}
}
return matches == a1.Length;
}
public class DS : ObjectDataSource
{
public static List<string> GetList ()
{
List<string> list = new List<string> ();
list.Add ("Norway");
list.Add ("Sweden");
list.Add ("France");
list.Add ("Italy");
list.Add ("Israel");
list.Add ("Russia");
return list;
}
public void DoRaiseDataSourceChangedEvent (EventArgs e)
{
RaiseDataSourceChangedEvent (e);
}
}
[Test]
[Category ("NunitWeb")]
public void DropDownList_DataSourceChangedEvent ()
{
WebTest t = new WebTest ();
PageDelegates pd = new PageDelegates ();
pd.Load = DropDownList_Init;
pd.PreRenderComplete = DropDownList_Load;
t.Invoker = new PageInvoker (pd);
t.Run ();
FormRequest fr = new FormRequest (t.Response, "form1");
fr.Controls.Add ("__EVENTTARGET");
fr.Controls.Add ("__EVENTARGUMENT");
fr.Controls["__EVENTTARGET"].Value = "";
fr.Controls["__EVENTARGUMENT"].Value = "";
t.Request = fr;
t.Run ();
if (t.UserData == null)
Assert.Fail ("DataSourceChangedEvent#1");
Assert.AreEqual ("Data_rebounded", t.UserData.ToString (), "DataSourceChangedEvent#2");
}
#region DropDownList_DataSourceChangedEvent
public static void DropDownList_Init (Page p)
{
DropDownListTestClass dl = new DropDownListTestClass ();
DS data = new DS ();
p.Controls.Add (dl);
p.Controls.Add (data);
data.TypeName = typeof (DS).AssemblyQualifiedName;
data.SelectMethod = "GetList";
data.ID = "Data";
dl.DataBinding += new EventHandler (data_DataBinding);
dl.DataSourceID = "Data";
}
public static void DropDownList_Load (Page p)
{
if (p.IsPostBack) {
DS data = (DS) p.FindControl ("Data");
if (data == null)
Assert.Fail ("Data soource control not created#1");
data.DoRaiseDataSourceChangedEvent (new EventArgs ());
}
}
public static void data_DataBinding (object sender, EventArgs e)
{
if (((WebControl) sender).Page.IsPostBack)
WebTest.CurrentTest.UserData = "Data_rebounded";
}
#endregion
[Test]
public void DropDownList_Defaults ()
{
DropDownListTestClass d = new DropDownListTestClass ();
Assert.AreEqual (Color.Empty, d.BackColor, "D1");
Assert.AreEqual (Color.Empty, d.BorderColor, "D2");
Assert.AreEqual (BorderStyle.NotSet, d.BorderStyle, "D3");
Assert.AreEqual (Unit.Empty, d.BorderWidth, "D4");
Assert.AreEqual (-1, d.SelectedIndex, "D5");
Assert.AreEqual (string.Empty, d.ToolTip, "D6");
Assert.AreEqual (0, d.Items.Count, "D7");
}
[Test]
public void DropDownListBasic () {
DropDownListTestClass d = new DropDownListTestClass ();
Assert.AreEqual ("<select>\n\n</select>", d.Render (), "B1");
d.ID = "blah";
Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\n</select>", d.Render(), "B2");
Assert.AreEqual(false, d.IsTrackingVS(), "B3");
d.SetTrackingVS();
Assert.AreEqual(true, d.IsTrackingVS(), "B4");
d.Items.Add(new ListItem("text1", "value1"));
Assert.AreEqual(1, d.Items.Count, "B5");
d.Items.Add(new ListItem("text2", "value2"));
Assert.AreEqual(2, d.Items.Count, "B6");
d.SelectedIndex = 1;
Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\t<option value=\"value1\">text1</option>\n\t<option selected=\"selected\" value=\"value2\">text2</option>\n\n</select>", d.Render(), "B7");
}
[Test]
public void DropDownListProperties () {
DropDownListTestClass d;
d = new DropDownListTestClass ();
Assert.AreEqual(Color.Empty, d.BorderColor, "P1");
d.BorderColor = Color.Red;
Assert.AreEqual(Color.Red, d.BorderColor, "P2");
Assert.AreEqual(BorderStyle.NotSet, d.BorderStyle, "P3");
d.BorderStyle = BorderStyle.Dotted;
Assert.AreEqual(BorderStyle.Dotted, d.BorderStyle, "P4");
Assert.AreEqual(Unit.Empty, d.BorderWidth, "P5");
d.BorderWidth = new Unit(1);
Assert.AreEqual("1px", d.BorderWidth.ToString(), "P6");
Assert.AreEqual(-1, d.SelectedIndex, "P7");
d.Items.Add(new ListItem("text1", "value1"));
d.Items.Add(new ListItem("text2", "value2"));
d.SelectedIndex = 1;
Assert.AreEqual(1, d.SelectedIndex, "P8");
Assert.AreEqual(string.Empty, d.ToolTip, "P9");
d.ToolTip = "blah";
Assert.AreEqual ("blah", d.ToolTip, "P10");
}
[Test]
[ExpectedException(typeof(HttpException))]
public void DropDownListDoubleSelectCheck () {
DropDownListTestClass d;
d = new DropDownListTestClass ();
d.Items.Add(new ListItem("text1", "value1"));
d.Items.Add(new ListItem("text2", "value2"));
d.SelectedIndex = 1;
Assert.AreEqual(1, d.SelectedIndex, "DS1");
d.Items[0].Selected = true;
d.Items[1].Selected = true;
Assert.AreEqual("<select name>\n\t<option selected=\"selected\" value=\"value1\">text1</option>\n\t<option selected=\"selected\" value=\"value2\">text2</option>\n\n</select>", d.Render(), "DS1");
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void DropDownListBorderStyleCheck () {
DropDownListTestClass d;
d = new DropDownListTestClass ();
d.BorderStyle = (BorderStyle)Int32.MinValue;
}
[Test]
public void DropDownListSelectedCheck () {
DropDownListTestClass d;
d = new DropDownListTestClass ();
d.SelectedIndex = 2; // No exception thrown!!!
Assert.AreEqual(-1, d.SelectedIndex, "S1");
}
[Test]
public void DropDownNullWriterTest () {
DropDownListTestClass d;
d = new DropDownListTestClass ();
d.AddAttributesToRender(null);
}
[Test]
public void DropDownListNull () {
DropDownListTestClass d;
d = new DropDownListTestClass ();
// We want to surve the next two calls
d.RenderContents(null);
}
#if not
[Test]
public void HtmlWriter () {
HtmlTextWriter writer;
writer = DropDownListTest.GetWriter();
writer.RenderBeginTag(HtmlTextWriterTag.Select);
writer.AddAttribute(HtmlTextWriterAttribute.Value, "MeValue", true);
writer.RenderBeginTag(HtmlTextWriterTag.Option);
writer.Write("MeText");
writer.RenderEndTag();
writer.RenderEndTag();
Assert.AreEqual("<select>\n\t<option value=\"MeValue\">\n\t\tMeText\n\t</option>\n</select>", writer.InnerWriter.ToString(), "H1");
}
#endif
[Test]
public void DropDownNamingTest () {
NamingContainer container = new NamingContainer ();
DropDownListTestClass child = new DropDownListTestClass ();
Assert.AreEqual ("<select>\n\n</select>", child.Render (), "N1");
container.Controls.Add (child);
// don't assume the generated id
string s = child.Render ();
Assert.IsTrue (s.StartsWith ("<select name=\""), "N2a");
Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N2b");
container.ID = "naming";
s = child.Render ();
Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N3a");
Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N3b");
child.ID = "fooid";
s = child.Render ();
Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N4a");
Assert.IsTrue (s.EndsWith ("fooid\" id=\"naming_fooid\">\n\n</select>"), "N4b");
}
[Test]
public void InitialSelectionMade ()
{
DropDownList ddl = new DropDownList ();
ddl.Items.Add ("a");
ddl.Items.Add ("b");
Assert.IsNotNull (ddl.SelectedItem, "need a selected item");
Assert.AreEqual ("a", ddl.SelectedItem.Text);
}
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 TestValueFieldAndTextFormat ()
{
DropDownListTestClass ddl = new DropDownListTestClass ();
ddl.DataSource = GetExampleData ();
ddl.DataValueField = "Company";
ddl.DataTextFormatString = "This shouldn't show up = {0}";
ddl.DataBind ();
string exp = @"<select>
<option value=""Novell Inc."">Novell Inc.</option>
<option value=""Microsoft Corp."">Microsoft Corp.</option>
<option value=""Google"">Google</option>
</select>";
HtmlDiff.AssertAreEqual(exp, ddl.Render(), "TestValueFieldAndTextFormat");
}
[Test]
public void HtmlEncodeItem ()
{
DropDownListTestClass d = new DropDownListTestClass ();
d.Items.Add(new ListItem ("text1", "<hola>"));
string html = d.Render();
Assert.IsTrue (html.IndexOf ("<hola>") == -1, "#01");
Assert.IsTrue (html.IndexOf ("<hola>") != -1, "#02");
}
class VerifyMultiSelectDropDownList : DropDownList
{
public new virtual void VerifyMultiSelect()
{
base.VerifyMultiSelect();
}
}
[Test]
[ExpectedException(typeof(HttpException))]
public void VerifyMultiSelectTest()
{
VerifyMultiSelectDropDownList list = new VerifyMultiSelectDropDownList();
list.VerifyMultiSelect();
}
}
}
|