2 // Tests for System.Web.UI.WebControls.DropDownList.cs
5 // Peter Dennis Bartok (pbartok@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
37 using System.Globalization;
40 using System.Web.UI.WebControls;
41 using NUnit.Framework;
42 using MonoTests.stand_alone.WebHarness;
43 using System.Collections.Generic;
44 using MonoTests.SystemWeb.Framework;
46 namespace MonoTests.System.Web.UI.WebControls
49 public class DropDownListTest {
50 public class NamingContainer : WebControl, INamingContainer {
54 public class DropDownListTestClass : DropDownList {
56 public DropDownListTestClass ()
60 public StateBag StateBag {
61 get { return base.ViewState; }
64 public string Render () {
65 HtmlTextWriter writer;
67 writer = DropDownListTest.GetWriter();
69 return writer.InnerWriter.ToString ();
72 public bool IsTrackingVS () {
73 return IsTrackingViewState;
76 public void SetTrackingVS () {
80 public object Save() {
81 return base.SaveViewState();
84 public void Load(object o) {
85 base.LoadViewState(o);
88 public new void RenderContents(HtmlTextWriter writer) {
89 base.RenderContents(writer);
92 public new void CreateControlCollection() {
93 base.CreateControlCollection();
96 public new void AddAttributesToRender(HtmlTextWriter writer) {
97 base.AddAttributesToRender(writer);
100 public string[] KeyValuePairs() {
105 e = ViewState.GetEnumerator();
106 result = new string[ViewState.Keys.Count];
109 while (e.MoveNext()) {
113 d = (DictionaryEntry)e.Current;
114 si = (StateItem)d.Value;
116 if (si.Value is String[]) {
119 values = (string[]) si.Value;
120 result[item] = d.Key.ToString() + "=";
121 if (values.Length > 0) {
122 result[item] += values[0];
124 for (int i = 1; i < values.Length; i++) {
125 result[item] += ", " + values[i];
129 result[item] = d.Key.ToString() + "=" + si.Value;
138 private static HtmlTextWriter GetWriter () {
139 StringWriter sw = new StringWriter ();
141 return new HtmlTextWriter (sw);
144 private bool IsEqual(object[] a1, object[] a2, string assertion) {
148 if (a1.Length != a2.Length) {
149 if (assertion != null) {
150 Assert.Fail(assertion + "( different length )");
156 notfound = new bool[a1.Length];
158 for (int i = 0; i < a1.Length; i++) {
159 for (int j = 0; j < a2.Length; j++) {
160 if (a1[i].Equals(a2[j])) {
165 if ((assertion != null) && (matches != i+1)) {
166 Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
170 return matches == a1.Length;
174 public class DS : ObjectDataSource
176 public static List<string> GetList ()
178 List<string> list = new List<string> ();
188 public void DoRaiseDataSourceChangedEvent (EventArgs e)
190 RaiseDataSourceChangedEvent (e);
195 [Category ("NunitWeb")]
196 public void DropDownList_DataSourceChangedEvent ()
198 WebTest t = new WebTest ();
199 PageDelegates pd = new PageDelegates ();
200 pd.Load = DropDownList_Init;
201 pd.PreRenderComplete = DropDownList_Load;
202 t.Invoker = new PageInvoker (pd);
204 FormRequest fr = new FormRequest (t.Response, "form1");
205 fr.Controls.Add ("__EVENTTARGET");
206 fr.Controls.Add ("__EVENTARGUMENT");
207 fr.Controls["__EVENTTARGET"].Value = "";
208 fr.Controls["__EVENTARGUMENT"].Value = "";
211 if (t.UserData == null)
212 Assert.Fail ("DataSourceChangedEvent#1");
213 Assert.AreEqual ("Data_rebounded", t.UserData.ToString (), "DataSourceChangedEvent#2");
216 #region DropDownList_DataSourceChangedEvent
217 public static void DropDownList_Init (Page p)
219 DropDownListTestClass dl = new DropDownListTestClass ();
222 p.Controls.Add (data);
223 data.TypeName = typeof (DS).AssemblyQualifiedName;
224 data.SelectMethod = "GetList";
226 dl.DataBinding += new EventHandler (data_DataBinding);
227 dl.DataSourceID = "Data";
230 public static void DropDownList_Load (Page p)
233 DS data = (DS) p.FindControl ("Data");
235 Assert.Fail ("Data soource control not created#1");
236 data.DoRaiseDataSourceChangedEvent (new EventArgs ());
240 public static void data_DataBinding (object sender, EventArgs e)
242 if (((WebControl) sender).Page.IsPostBack)
243 WebTest.CurrentTest.UserData = "Data_rebounded";
248 public void DropDownList_Defaults ()
250 DropDownListTestClass d = new DropDownListTestClass ();
252 Assert.AreEqual (Color.Empty, d.BackColor, "D1");
253 Assert.AreEqual (Color.Empty, d.BorderColor, "D2");
254 Assert.AreEqual (BorderStyle.NotSet, d.BorderStyle, "D3");
255 Assert.AreEqual (Unit.Empty, d.BorderWidth, "D4");
256 Assert.AreEqual (-1, d.SelectedIndex, "D5");
257 Assert.AreEqual (string.Empty, d.ToolTip, "D6");
258 Assert.AreEqual (0, d.Items.Count, "D7");
262 public void DropDownListBasic () {
263 DropDownListTestClass d = new DropDownListTestClass ();
265 Assert.AreEqual ("<select>\n\n</select>", d.Render (), "B1");
267 Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\n</select>", d.Render(), "B2");
269 Assert.AreEqual(false, d.IsTrackingVS(), "B3");
271 Assert.AreEqual(true, d.IsTrackingVS(), "B4");
273 d.Items.Add(new ListItem("text1", "value1"));
274 Assert.AreEqual(1, d.Items.Count, "B5");
275 d.Items.Add(new ListItem("text2", "value2"));
276 Assert.AreEqual(2, d.Items.Count, "B6");
279 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");
283 public void DropDownListProperties () {
284 DropDownListTestClass d;
286 d = new DropDownListTestClass ();
288 Assert.AreEqual(Color.Empty, d.BorderColor, "P1");
289 d.BorderColor = Color.Red;
290 Assert.AreEqual(Color.Red, d.BorderColor, "P2");
292 Assert.AreEqual(BorderStyle.NotSet, d.BorderStyle, "P3");
293 d.BorderStyle = BorderStyle.Dotted;
294 Assert.AreEqual(BorderStyle.Dotted, d.BorderStyle, "P4");
296 Assert.AreEqual(Unit.Empty, d.BorderWidth, "P5");
297 d.BorderWidth = new Unit(1);
298 Assert.AreEqual("1px", d.BorderWidth.ToString(), "P6");
300 Assert.AreEqual(-1, d.SelectedIndex, "P7");
301 d.Items.Add(new ListItem("text1", "value1"));
302 d.Items.Add(new ListItem("text2", "value2"));
304 Assert.AreEqual(1, d.SelectedIndex, "P8");
306 Assert.AreEqual(string.Empty, d.ToolTip, "P9");
308 Assert.AreEqual ("blah", d.ToolTip, "P10");
312 [ExpectedException(typeof(HttpException))]
313 public void DropDownListDoubleSelectCheck () {
314 DropDownListTestClass d;
316 d = new DropDownListTestClass ();
317 d.Items.Add(new ListItem("text1", "value1"));
318 d.Items.Add(new ListItem("text2", "value2"));
320 Assert.AreEqual(1, d.SelectedIndex, "DS1");
321 d.Items[0].Selected = true;
322 d.Items[1].Selected = true;
323 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");
327 [ExpectedException(typeof(ArgumentOutOfRangeException))]
328 public void DropDownListBorderStyleCheck () {
329 DropDownListTestClass d;
331 d = new DropDownListTestClass ();
332 d.BorderStyle = (BorderStyle)Int32.MinValue;
336 public void DropDownListSelectedCheck () {
337 DropDownListTestClass d;
339 d = new DropDownListTestClass ();
340 d.SelectedIndex = 2; // No exception thrown!!!
341 Assert.AreEqual(-1, d.SelectedIndex, "S1");
346 [ExpectedException(typeof(NullReferenceException))]
348 public void DropDownNullWriterTest () {
349 DropDownListTestClass d;
351 d = new DropDownListTestClass ();
352 d.AddAttributesToRender(null);
356 public void DropDownListNull () {
357 DropDownListTestClass d;
359 d = new DropDownListTestClass ();
361 // We want to surve the next two calls
362 d.RenderContents(null);
367 public void HtmlWriter () {
368 HtmlTextWriter writer;
370 writer = DropDownListTest.GetWriter();
371 writer.RenderBeginTag(HtmlTextWriterTag.Select);
372 writer.AddAttribute(HtmlTextWriterAttribute.Value, "MeValue", true);
373 writer.RenderBeginTag(HtmlTextWriterTag.Option);
374 writer.Write("MeText");
375 writer.RenderEndTag();
376 writer.RenderEndTag();
377 Assert.AreEqual("<select>\n\t<option value=\"MeValue\">\n\t\tMeText\n\t</option>\n</select>", writer.InnerWriter.ToString(), "H1");
382 public void DropDownNamingTest () {
383 NamingContainer container = new NamingContainer ();
384 DropDownListTestClass child = new DropDownListTestClass ();
385 Assert.AreEqual ("<select>\n\n</select>", child.Render (), "N1");
386 container.Controls.Add (child);
387 // don't assume the generated id
388 string s = child.Render ();
389 Assert.IsTrue (s.StartsWith ("<select name=\""), "N2a");
390 Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N2b");
392 container.ID = "naming";
394 Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N3a");
395 Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N3b");
399 Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N4a");
400 Assert.IsTrue (s.EndsWith ("fooid\" id=\"naming_fooid\">\n\n</select>"), "N4b");
404 public void InitialSelectionMade ()
406 DropDownList ddl = new DropDownList ();
410 Assert.IsNotNull (ddl.SelectedItem, "need a selected item");
411 Assert.AreEqual ("a", ddl.SelectedItem.Text);
414 DataSet GetExampleData ()
416 DataSet ds = new DataSet ();
417 ds.ReadXml (new StringReader (@"
419 <Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
420 <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92' />
421 <Stocks Company='Google' Symbol='GOOG' Price='291.60' />
429 public void TestValueFieldAndTextFormat ()
431 DropDownListTestClass ddl = new DropDownListTestClass ();
432 ddl.DataSource = GetExampleData ();
433 ddl.DataValueField = "Company";
434 ddl.DataTextFormatString = "This shouldn't show up = {0}";
437 string exp = @"<select>
438 <option value=""Novell Inc."">Novell Inc.</option>
439 <option value=""Microsoft Corp."">Microsoft Corp.</option>
440 <option value=""Google"">Google</option>
444 HtmlDiff.AssertAreEqual(exp, ddl.Render(), "TestValueFieldAndTextFormat");
449 [Category("NotWorking")]
451 public void HtmlEncodeItem ()
453 DropDownListTestClass d = new DropDownListTestClass ();
454 d.Items.Add(new ListItem ("text1", "<hola>"));
455 string html = d.Render();
456 Assert.IsTrue (html.IndexOf ("<hola>") == -1, "#01");
457 Assert.IsTrue (html.IndexOf ("<hola>") != -1, "#02");
460 class VerifyMultiSelectDropDownList : DropDownList
462 public new virtual void VerifyMultiSelect()
464 base.VerifyMultiSelect();
469 [ExpectedException(typeof(HttpException))]
470 public void VerifyMultiSelectTest()
472 VerifyMultiSelectDropDownList list = new VerifyMultiSelectDropDownList();
473 list.VerifyMultiSelect();