da62d2d479e40c1fc7a2fd9a47a9f879f76f2a33
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / DropDownListTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.Style.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 using NUnit.Framework;
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
42 namespace MonoTests.System.Web.UI.WebControls
43 {
44         [TestFixture]   
45         public class DropDownListTest {
46                 public class NamingContainer : WebControl, INamingContainer {
47
48                 }
49
50                 public class DropDownListTestClass : DropDownList {
51
52                         public DropDownListTestClass ()
53                                 : base () {
54                         }
55
56                         public StateBag StateBag {
57                                 get { return base.ViewState; }
58                         }
59
60                         public string Render () {
61                                 HtmlTextWriter  writer;
62
63                                 writer = DropDownListTest.GetWriter();
64                                 base.Render (writer);
65                                 return writer.InnerWriter.ToString ();
66                         }
67
68                         public bool IsTrackingVS () {
69                                 return IsTrackingViewState;
70                         }
71
72                         public void SetTrackingVS () {
73                                 TrackViewState ();
74                         }
75
76                         public object Save() {
77                                 return base.SaveViewState();
78                         }
79
80                         public void Load(object o) {
81                                 base.LoadViewState(o);
82                         }
83
84                         public new void RenderContents(HtmlTextWriter writer) {
85                                 base.RenderContents(writer);
86                         }
87
88                         public new void CreateControlCollection() {
89                                 base.CreateControlCollection();
90                         }
91
92                         public new void AddAttributesToRender(HtmlTextWriter writer) {
93                                 base.AddAttributesToRender(writer);
94                         }
95
96                         public string[] KeyValuePairs() {
97                                 IEnumerator     e;
98                                 string[]        result;
99                                 int             item;
100
101                                 e = ViewState.GetEnumerator();
102                                 result = new string[ViewState.Keys.Count];
103                                 item = 0;
104
105                                 while (e.MoveNext()) {
106                                         DictionaryEntry d;
107                                         StateItem       si;
108
109                                         d = (DictionaryEntry)e.Current;
110                                         si = (StateItem)d.Value;
111
112                                         if (si.Value is String[]) {
113                                                 string[] values;
114
115                                                 values = (string[]) si.Value;
116                                                 result[item] = d.Key.ToString() + "=";
117                                                 if (values.Length > 0) {
118                                                         result[item] += values[0];
119
120                                                         for (int i = 1; i < values.Length; i++) {
121                                                                 result[item] += ", " + values[i];
122                                                         }
123                                                 }
124                                         } else {
125                                                 result[item] =  d.Key.ToString() + "=" + si.Value;
126                                         }
127                                         item++;
128                                 }
129
130                                 return result;
131                         }
132                 }
133
134                 private static HtmlTextWriter GetWriter () {
135                         StringWriter sw = new StringWriter ();
136                         sw.NewLine = "\n";
137                         return new HtmlTextWriter (sw);
138                 }
139
140                 private bool IsEqual(object[] a1, object[] a2, string assertion) {
141                         int     matches;
142                         bool[]  notfound;       
143
144                         if (a1.Length != a2.Length) {
145                                 if (assertion != null) {
146                                         Assert.Fail(assertion + "( different length )");
147                                 }
148                                 return false;
149                         }
150
151                         matches = 0;
152                         notfound = new bool[a1.Length];
153
154                         for (int i = 0; i < a1.Length; i++) {
155                                 for (int j = 0; j < a2.Length; j++) {
156                                         if (a1[i].Equals(a2[j])) {
157                                                 matches++;
158                                                 break;
159                                         }
160                                 }
161                                 if ((assertion != null) && (matches != i+1)) {
162                                         Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
163                                 }
164                         }
165
166                         return matches == a1.Length;
167                 }
168
169                 [Test]
170                 public void DropDownList_Defaults ()
171                 {
172                         DropDownListTestClass d = new DropDownListTestClass ();
173
174                         Assert.AreEqual (Color.Empty, d.BackColor, "D1");
175                         Assert.AreEqual (Color.Empty, d.BorderColor, "D2");
176                         Assert.AreEqual (BorderStyle.NotSet, d.BorderStyle, "D3");
177                         Assert.AreEqual (Unit.Empty, d.BorderWidth, "D4");
178                         Assert.AreEqual (-1, d.SelectedIndex, "D5");
179                         Assert.AreEqual (string.Empty, d.ToolTip, "D6");
180                         Assert.AreEqual (0, d.Items.Count, "D7");
181                 }
182
183                 [Test]
184                 public void DropDownListBasic () {
185                         DropDownListTestClass d = new DropDownListTestClass ();
186 #if NET_2_0
187                         Assert.AreEqual ("<select>\n\n</select>", d.Render (), "B1");
188 #else
189                         Assert.AreEqual("<select name>\n\n</select>", d.Render(), "B1");
190 #endif
191                         d.ID = "blah";
192                         Assert.AreEqual("<select name=\"blah\" id=\"blah\">\n\n</select>", d.Render(), "B2");
193
194                         Assert.AreEqual(false, d.IsTrackingVS(), "B3");
195                         d.SetTrackingVS();
196                         Assert.AreEqual(true, d.IsTrackingVS(), "B4");
197
198                         d.Items.Add(new ListItem("text1", "value1"));
199                         Assert.AreEqual(1, d.Items.Count, "B5");
200                         d.Items.Add(new ListItem("text2", "value2"));
201                         Assert.AreEqual(2, d.Items.Count, "B6");
202                         d.SelectedIndex = 1;
203
204                         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");
205                 }
206
207                 [Test]
208                 public void DropDownListProperties () {
209                         DropDownListTestClass   d;
210
211                         d = new DropDownListTestClass ();
212
213                         Assert.AreEqual(Color.Empty, d.BorderColor, "P1");
214                         d.BorderColor = Color.Red;
215                         Assert.AreEqual(Color.Red, d.BorderColor, "P2");
216
217                         Assert.AreEqual(BorderStyle.NotSet, d.BorderStyle, "P3");
218                         d.BorderStyle = BorderStyle.Dotted;
219                         Assert.AreEqual(BorderStyle.Dotted, d.BorderStyle, "P4");
220
221                         Assert.AreEqual(Unit.Empty, d.BorderWidth, "P5");
222                         d.BorderWidth = new Unit(1);
223                         Assert.AreEqual("1px", d.BorderWidth.ToString(), "P6");
224
225                         Assert.AreEqual(-1, d.SelectedIndex, "P7");
226                         d.Items.Add(new ListItem("text1", "value1"));
227                         d.Items.Add(new ListItem("text2", "value2"));
228                         d.SelectedIndex = 1;
229                         Assert.AreEqual(1, d.SelectedIndex, "P8");
230
231                         Assert.AreEqual(string.Empty, d.ToolTip, "P9");
232                         d.ToolTip = "blah";
233 #if NET_2_0
234                         Assert.AreEqual ("blah", d.ToolTip, "P10");
235 #else
236                         Assert.AreEqual(string.Empty, d.ToolTip, "P10");
237 #endif
238                 }
239
240                 [Test]
241                 [ExpectedException(typeof(HttpException))]
242                 public void DropDownListDoubleSelectCheck () {
243                         DropDownListTestClass   d;
244
245                         d = new DropDownListTestClass ();
246                         d.Items.Add(new ListItem("text1", "value1"));
247                         d.Items.Add(new ListItem("text2", "value2"));
248                         d.SelectedIndex = 1;
249                         Assert.AreEqual(1, d.SelectedIndex, "DS1");
250                         d.Items[0].Selected = true;
251                         d.Items[1].Selected = true;
252                         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");
253                 }
254
255                 [Test]
256                 [ExpectedException(typeof(ArgumentOutOfRangeException))]
257                 public void DropDownListBorderStyleCheck () {
258                         DropDownListTestClass   d;
259
260                         d = new DropDownListTestClass ();
261                         d.BorderStyle = (BorderStyle)Int32.MinValue;
262                 }
263
264                 [Test]
265                 public void DropDownListSelectedCheck () {
266                         DropDownListTestClass   d;
267
268                         d = new DropDownListTestClass ();
269                         d.SelectedIndex = 2;    // No exception thrown!!!
270                         Assert.AreEqual(-1, d.SelectedIndex, "S1");
271                 }
272
273                 [Test]
274 #if ONLY_1_1
275                 [ExpectedException(typeof(NullReferenceException))]
276 #endif
277                 public void DropDownNullWriterTest () {
278                         DropDownListTestClass   d;
279
280                         d = new DropDownListTestClass ();
281                         d.AddAttributesToRender(null);
282                 }
283
284                 [Test]
285                 public void DropDownListNull () {
286                         DropDownListTestClass   d;
287
288                         d = new DropDownListTestClass ();
289
290                         // We want to surve the next two calls
291                         d.RenderContents(null);
292                 }
293
294 #if not
295                 [Test]
296                 public void HtmlWriter () {
297                         HtmlTextWriter  writer;
298
299                         writer = DropDownListTest.GetWriter();
300                         writer.RenderBeginTag(HtmlTextWriterTag.Select);
301                         writer.AddAttribute(HtmlTextWriterAttribute.Value, "MeValue", true);
302                         writer.RenderBeginTag(HtmlTextWriterTag.Option);
303                         writer.Write("MeText");
304                         writer.RenderEndTag();
305                         writer.RenderEndTag();
306                         Assert.AreEqual("<select>\n\t<option value=\"MeValue\">\n\t\tMeText\n\t</option>\n</select>", writer.InnerWriter.ToString(), "H1");
307                 }
308 #endif
309
310                 [Test]
311                 public void DropDownNamingTest () {
312                         NamingContainer container = new NamingContainer ();
313                         DropDownListTestClass child = new DropDownListTestClass ();
314 #if NET_2_0
315                         Assert.AreEqual ("<select>\n\n</select>", child.Render (), "N1");
316 #else
317                         Assert.AreEqual ("<select name>\n\n</select>", child.Render (), "N1");
318 #endif
319                         container.Controls.Add (child);
320                         // don't assume the generated id
321                         string s = child.Render ();
322                         Assert.IsTrue (s.StartsWith ("<select name=\""), "N2a");
323                         Assert.IsTrue (s.EndsWith ("\">\n\n</select>"),  "N2b");
324
325                         container.ID = "naming";
326                         s = child.Render ();
327                         Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N3a");
328                         Assert.IsTrue (s.EndsWith ("\">\n\n</select>"), "N3b");
329
330                         child.ID = "fooid";
331                         s = child.Render ();
332                         Assert.IsTrue (s.StartsWith ("<select name=\"naming"), "N4a");
333                         Assert.IsTrue (s.EndsWith ("fooid\" id=\"naming_fooid\">\n\n</select>"), "N4b");
334                 }
335
336                 [Test]
337                 public void InitialSelectionMade ()
338                 {
339                         DropDownList ddl = new DropDownList ();
340                         ddl.Items.Add ("a");
341                         ddl.Items.Add ("b");
342
343                         Assert.IsNotNull (ddl.SelectedItem, "need a selected item");
344                         Assert.AreEqual ("a", ddl.SelectedItem.Text);
345                 }
346
347                 DataSet GetExampleData ()
348                 {
349                         DataSet ds = new DataSet ();
350                         ds.ReadXml (new StringReader (@"
351 <DataSet>
352         <Stocks Company='Novell Inc.'     Symbol='NOVL' Price='6.14'   />
353         <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92'  />
354         <Stocks Company='Google'          Symbol='GOOG' Price='291.60' />
355 </DataSet>
356 "));
357                         return ds;
358                 }
359                 
360                 
361                 [Test]
362                 public void TestValueFieldAndTextFormat ()
363                 {
364                         DropDownListTestClass ddl = new DropDownListTestClass ();
365                         ddl.DataSource = GetExampleData ();
366                         ddl.DataValueField = "Company";
367                         ddl.DataTextFormatString = "This shouldn't show up = {0}";
368                         ddl.DataBind ();
369 #if NET_2_0
370                         string exp = @"<select>
371         <option value=""Novell Inc."">Novell Inc.</option>
372         <option value=""Microsoft Corp."">Microsoft Corp.</option>
373         <option value=""Google"">Google</option>
374
375 </select>";
376 #else
377                         string exp = @"<select name>
378         <option value=""Novell Inc."">Novell Inc.</option>
379         <option value=""Microsoft Corp."">Microsoft Corp.</option>
380         <option value=""Google"">Google</option>
381
382 </select>";
383 #endif
384                         Assert.AreEqual (exp, ddl.Render ());
385                 }
386         }
387 }