[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / DropDownListTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.DropDownList.cs 
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31
32 using System;
33 using System.Collections;
34 using System.Drawing;
35 using System.IO;
36 using System.Data;
37 using System.Globalization;
38 using System.Web;
39 using System.Web.UI;
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;
45
46 namespace MonoTests.System.Web.UI.WebControls
47 {
48         [TestFixture]   
49         public class DropDownListTest {
50                 public class NamingContainer : WebControl, INamingContainer {
51
52                 }
53
54                 public class DropDownListTestClass : DropDownList {
55
56                         public DropDownListTestClass ()
57                                 : base () {
58                         }
59
60                         public StateBag StateBag {
61                                 get { return base.ViewState; }
62                         }
63
64                         public string Render () {
65                                 HtmlTextWriter  writer;
66
67                                 writer = DropDownListTest.GetWriter();
68                                 base.Render (writer);
69                                 return writer.InnerWriter.ToString ();
70                         }
71
72                         public bool IsTrackingVS () {
73                                 return IsTrackingViewState;
74                         }
75
76                         public void SetTrackingVS () {
77                                 TrackViewState ();
78                         }
79
80                         public object Save() {
81                                 return base.SaveViewState();
82                         }
83
84                         public void Load(object o) {
85                                 base.LoadViewState(o);
86                         }
87
88                         public new void RenderContents(HtmlTextWriter writer) {
89                                 base.RenderContents(writer);
90                         }
91
92                         public new void CreateControlCollection() {
93                                 base.CreateControlCollection();
94                         }
95
96                         public new void AddAttributesToRender(HtmlTextWriter writer) {
97                                 base.AddAttributesToRender(writer);
98                         }
99
100                         public string[] KeyValuePairs() {
101                                 IEnumerator     e;
102                                 string[]        result;
103                                 int             item;
104
105                                 e = ViewState.GetEnumerator();
106                                 result = new string[ViewState.Keys.Count];
107                                 item = 0;
108
109                                 while (e.MoveNext()) {
110                                         DictionaryEntry d;
111                                         StateItem       si;
112
113                                         d = (DictionaryEntry)e.Current;
114                                         si = (StateItem)d.Value;
115
116                                         if (si.Value is String[]) {
117                                                 string[] values;
118
119                                                 values = (string[]) si.Value;
120                                                 result[item] = d.Key.ToString() + "=";
121                                                 if (values.Length > 0) {
122                                                         result[item] += values[0];
123
124                                                         for (int i = 1; i < values.Length; i++) {
125                                                                 result[item] += ", " + values[i];
126                                                         }
127                                                 }
128                                         } else {
129                                                 result[item] =  d.Key.ToString() + "=" + si.Value;
130                                         }
131                                         item++;
132                                 }
133
134                                 return result;
135                         }
136                 }
137
138                 private static HtmlTextWriter GetWriter () {
139                         StringWriter sw = new StringWriter ();
140                         sw.NewLine = "\n";
141                         return new HtmlTextWriter (sw);
142                 }
143
144                 private bool IsEqual(object[] a1, object[] a2, string assertion) {
145                         int     matches;
146                         bool[]  notfound;       
147
148                         if (a1.Length != a2.Length) {
149                                 if (assertion != null) {
150                                         Assert.Fail(assertion + "( different length )");
151                                 }
152                                 return false;
153                         }
154
155                         matches = 0;
156                         notfound = new bool[a1.Length];
157
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])) {
161                                                 matches++;
162                                                 break;
163                                         }
164                                 }
165                                 if ((assertion != null) && (matches != i+1)) {
166                                         Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
167                                 }
168                         }
169
170                         return matches == a1.Length;
171                 }
172
173
174                 public class DS : ObjectDataSource
175                 {
176                         public static List<string> GetList ()
177                         {
178                                 List<string> list = new List<string> ();
179                                 list.Add ("Norway");
180                                 list.Add ("Sweden");
181                                 list.Add ("France");
182                                 list.Add ("Italy");
183                                 list.Add ("Israel");
184                                 list.Add ("Russia");
185                                 return list;
186                         }
187
188                         public void DoRaiseDataSourceChangedEvent (EventArgs e)
189                         {
190                                 RaiseDataSourceChangedEvent (e);
191                         }
192                 }
193
194                 [Test]
195                 [Category ("NunitWeb")]
196                 public void DropDownList_DataSourceChangedEvent ()
197                 {
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);
203                         t.Run ();
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 = "";
209                         t.Request = fr;
210                         t.Run ();
211                         if (t.UserData == null)
212                                 Assert.Fail ("DataSourceChangedEvent#1");
213                         Assert.AreEqual ("Data_rebounded", t.UserData.ToString (), "DataSourceChangedEvent#2");
214                 }
215
216                 #region DropDownList_DataSourceChangedEvent
217                 public static void DropDownList_Init (Page p)
218                 {
219                         DropDownListTestClass dl = new DropDownListTestClass ();
220                         DS data = new DS ();
221                         p.Controls.Add (dl);
222                         p.Controls.Add (data);
223                         data.TypeName = typeof (DS).AssemblyQualifiedName;
224                         data.SelectMethod = "GetList";
225                         data.ID = "Data";
226                         dl.DataBinding += new EventHandler (data_DataBinding);
227                         dl.DataSourceID = "Data";
228                 }
229
230                 public static void DropDownList_Load (Page p)
231                 {
232                         if (p.IsPostBack) {
233                                 DS data = (DS) p.FindControl ("Data");
234                                 if (data == null)
235                                         Assert.Fail ("Data soource control not created#1");
236                                 data.DoRaiseDataSourceChangedEvent (new EventArgs ());
237                         }
238                 }
239
240                 public static void data_DataBinding (object sender, EventArgs e)
241                 {
242                         if (((WebControl) sender).Page.IsPostBack)
243                                 WebTest.CurrentTest.UserData = "Data_rebounded";
244                 }
245                 #endregion
246
247                 [Test]
248                 public void DropDownList_Defaults ()
249                 {
250                         DropDownListTestClass d = new DropDownListTestClass ();
251
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");
259                 }
260
261                 [Test]
262                 public void DropDownListBasic () {
263                         DropDownListTestClass d = new DropDownListTestClass ();
264
265                         Assert.AreEqual ("<select>\n\n</select>", d.Render (), "B1");
266                         d.ID = "blah";
267                         Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\n</select>", d.Render(), "B2");
268
269                         Assert.AreEqual(false, d.IsTrackingVS(), "B3");
270                         d.SetTrackingVS();
271                         Assert.AreEqual(true, d.IsTrackingVS(), "B4");
272
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");
277                         d.SelectedIndex = 1;
278
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");
280                 }
281
282                 [Test]
283                 public void DropDownListProperties () {
284                         DropDownListTestClass   d;
285
286                         d = new DropDownListTestClass ();
287
288                         Assert.AreEqual(Color.Empty, d.BorderColor, "P1");
289                         d.BorderColor = Color.Red;
290                         Assert.AreEqual(Color.Red, d.BorderColor, "P2");
291
292                         Assert.AreEqual(BorderStyle.NotSet, d.BorderStyle, "P3");
293                         d.BorderStyle = BorderStyle.Dotted;
294                         Assert.AreEqual(BorderStyle.Dotted, d.BorderStyle, "P4");
295
296                         Assert.AreEqual(Unit.Empty, d.BorderWidth, "P5");
297                         d.BorderWidth = new Unit(1);
298                         Assert.AreEqual("1px", d.BorderWidth.ToString(), "P6");
299
300                         Assert.AreEqual(-1, d.SelectedIndex, "P7");
301                         d.Items.Add(new ListItem("text1", "value1"));
302                         d.Items.Add(new ListItem("text2", "value2"));
303                         d.SelectedIndex = 1;
304                         Assert.AreEqual(1, d.SelectedIndex, "P8");
305
306                         Assert.AreEqual(string.Empty, d.ToolTip, "P9");
307                         d.ToolTip = "blah";
308                         Assert.AreEqual ("blah", d.ToolTip, "P10");
309                 }
310
311                 [Test]
312                 [ExpectedException(typeof(HttpException))]
313                 public void DropDownListDoubleSelectCheck () {
314                         DropDownListTestClass   d;
315
316                         d = new DropDownListTestClass ();
317                         d.Items.Add(new ListItem("text1", "value1"));
318                         d.Items.Add(new ListItem("text2", "value2"));
319                         d.SelectedIndex = 1;
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");
324                 }
325
326                 [Test]
327                 [ExpectedException(typeof(ArgumentOutOfRangeException))]
328                 public void DropDownListBorderStyleCheck () {
329                         DropDownListTestClass   d;
330
331                         d = new DropDownListTestClass ();
332                         d.BorderStyle = (BorderStyle)Int32.MinValue;
333                 }
334
335                 [Test]
336                 public void DropDownListSelectedCheck () {
337                         DropDownListTestClass   d;
338
339                         d = new DropDownListTestClass ();
340                         d.SelectedIndex = 2;    // No exception thrown!!!
341                         Assert.AreEqual(-1, d.SelectedIndex, "S1");
342                 }
343
344                 [Test]
345 #if ONLY_1_1
346                 [ExpectedException(typeof(NullReferenceException))]
347 #endif
348                 public void DropDownNullWriterTest () {
349                         DropDownListTestClass   d;
350
351                         d = new DropDownListTestClass ();
352                         d.AddAttributesToRender(null);
353                 }
354
355                 [Test]
356                 public void DropDownListNull () {
357                         DropDownListTestClass   d;
358
359                         d = new DropDownListTestClass ();
360
361                         // We want to surve the next two calls
362                         d.RenderContents(null);
363                 }
364
365 #if not
366                 [Test]
367                 public void HtmlWriter () {
368                         HtmlTextWriter  writer;
369
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");
378                 }
379 #endif
380
381                 [Test]
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");
391
392                         container.ID = "naming";
393                         s = child.Render ();
394                         Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N3a");
395                         Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N3b");
396
397                         child.ID = "fooid";
398                         s = child.Render ();
399                         Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N4a");
400                         Assert.IsTrue (s.EndsWith ("fooid\" id=\"naming_fooid\">\n\n</select>"), "N4b");
401                 }
402
403                 [Test]
404                 public void InitialSelectionMade ()
405                 {
406                         DropDownList ddl = new DropDownList ();
407                         ddl.Items.Add ("a");
408                         ddl.Items.Add ("b");
409
410                         Assert.IsNotNull (ddl.SelectedItem, "need a selected item");
411                         Assert.AreEqual ("a", ddl.SelectedItem.Text);
412                 }
413
414                 DataSet GetExampleData ()
415                 {
416                         DataSet ds = new DataSet ();
417                         ds.ReadXml (new StringReader (@"
418 <DataSet>
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' />
422 </DataSet>
423 "));
424                         return ds;
425                 }
426                 
427                 
428                 [Test]
429                 public void TestValueFieldAndTextFormat ()
430                 {
431                         DropDownListTestClass ddl = new DropDownListTestClass ();
432                         ddl.DataSource = GetExampleData ();
433                         ddl.DataValueField = "Company";
434                         ddl.DataTextFormatString = "This shouldn't show up = {0}";
435                         ddl.DataBind ();
436
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>
441
442 </select>";
443
444             HtmlDiff.AssertAreEqual(exp, ddl.Render(), "TestValueFieldAndTextFormat");
445                 }
446
447                 [Test]
448 #if ONLY_1_1
449         [Category("NotWorking")]
450 #endif
451                 public void HtmlEncodeItem ()
452                 {
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 ("&lt;hola>") != -1, "#02");
458                 }
459
460         class VerifyMultiSelectDropDownList : DropDownList
461         {
462             public new virtual void VerifyMultiSelect()
463             {
464                 base.VerifyMultiSelect();
465             }
466         }
467
468         [Test]
469         [ExpectedException(typeof(HttpException))]
470         public void VerifyMultiSelectTest()
471         {
472             VerifyMultiSelectDropDownList list = new VerifyMultiSelectDropDownList();
473             list.VerifyMultiSelect();
474         }
475         
476         
477
478         }
479 }