9c3fab54c6c0d95350a3777d7716b945570fd4f2
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / ListBoxTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.ListBoxTest.cs
3 //
4 // Author:
5 //      Jackson Harper (jackson@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8
9 //
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using NUnit.Framework;
33 using System;
34 using System.IO;
35 using System.Drawing;
36 using System.Collections.Specialized;
37 using System.Globalization;
38 using System.Web;
39 using System.Web.UI;
40 using System.Web.UI.WebControls;
41 using System.Data;
42 using MonoTests.stand_alone.WebHarness;
43
44 namespace MonoTests.System.Web.UI.WebControls
45 {
46         class ListBoxPoker : ListBox {
47
48                 public ListBoxPoker ()
49                 {
50                         TrackViewState ();
51                 }
52
53                 public bool LoadPD (string key, NameValueCollection values)
54                 {
55                         return ((IPostBackDataHandler) this).LoadPostData (key, values);
56                 }
57
58                 public object SaveState ()
59                 {
60                         return SaveViewState ();
61                 }
62
63                 public void LoadState (object o)
64                 {
65                         LoadViewState (o);
66                 }
67
68                 public StateBag _ViewState {
69                         get { return ViewState; }
70                 }
71
72                                         
73                 public string Render ()
74                 {
75                         StringWriter sw = new StringWriter ();
76                         sw.NewLine = "\n";
77                         HtmlTextWriter writer = new HtmlTextWriter (sw);
78                         base.Render (writer);
79                         return writer.InnerWriter.ToString ();
80                 }
81 #if NET_2_0
82         public new virtual void VerifyMultiSelect()
83         {
84             base.VerifyMultiSelect();
85         }
86 #endif
87         }
88
89         [TestFixture]   
90         public class ListBoxTest {
91
92                 [Test]
93                 public void Defaults ()
94                 {
95                         ListBox lb = new ListBox ();
96
97                         Assert.AreEqual (lb.BorderColor, Color.Empty, "A1");
98                         Assert.AreEqual (lb.BorderStyle, BorderStyle.NotSet, "A2");
99                         Assert.AreEqual (lb.BorderWidth, Unit.Empty, "A3");
100                         Assert.AreEqual (lb.Rows, 4, "A4");
101                         Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Single, "A5");
102                         Assert.AreEqual (lb.ToolTip, String.Empty, "A6");
103                 }
104
105                 [Test]
106                 public void SetProps ()
107                 {
108                         ListBox lb = new ListBox ();
109
110                         lb.BorderColor = Color.Black;
111                         Assert.AreEqual (lb.BorderColor, Color.Black, "A1");
112
113                         lb.BorderStyle = BorderStyle.Dashed;
114                         Assert.AreEqual (lb.BorderStyle, BorderStyle.Dashed, "A2");
115
116                         lb.BorderWidth = 0;
117                         Assert.AreEqual (lb.BorderWidth, (Unit) 0, "A3");
118
119                         lb.BorderWidth = 15;
120                         Assert.AreEqual (lb.BorderWidth, (Unit) 15, "A3");
121
122                         lb.Rows = 1;
123                         Assert.AreEqual (lb.Rows, 1, "A4");
124
125                         lb.SelectionMode = ListSelectionMode.Multiple;
126                         Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Multiple, "A6");
127
128                         lb.ToolTip = "foo";
129 #if NET_2_0
130                         Assert.AreEqual (lb.ToolTip, "foo", "A7");
131 #else
132                         Assert.AreEqual (lb.ToolTip, String.Empty, "A7"); // Always empty in 1.x
133 #endif
134                 }
135
136                 [Test]
137         [Category ("NotWorking")]
138 #if !NET_2_0
139                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
140 #endif
141                 public void RowsTooHigh ()
142                 {
143                         ListBox lb = new ListBox ();
144                         lb.Rows = 2001;
145                 }
146
147                 [Test]
148                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
149                 public void RowsTooLow ()
150                 {
151                         ListBox lb = new ListBox ();
152                         lb.Rows = 0;
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
157                 public void BadSelectionMode ()
158                 {
159                         ListBox lb = new ListBox ();
160                         lb.SelectionMode = (ListSelectionMode) 500;
161                 }
162
163                 [Test]
164                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
165                 public void BadBorderStyle ()
166                 {
167                         ListBox lb = new ListBox ();
168                         lb.BorderStyle = (BorderStyle) 500;
169                 }
170
171                 [Test]
172                 public void ViewState ()
173                 {
174                         ListBoxPoker p = new ListBoxPoker ();
175
176                         p.BorderColor = Color.Red;
177                         Assert.AreEqual (p._ViewState ["BorderColor"],
178                                         Color.Red, "A1");
179
180                         p.BorderStyle = BorderStyle.Double;
181                         Assert.AreEqual (p._ViewState ["BorderStyle"],
182                                         BorderStyle.Double, "A2");
183
184                         p.BorderWidth = 25;
185                         Assert.AreEqual (p._ViewState ["BorderWidth"],
186                                         (Unit) 25, "A3");
187
188                         p.SelectionMode = ListSelectionMode.Multiple;
189                         Assert.AreEqual (p._ViewState ["SelectionMode"],
190                                         ListSelectionMode.Multiple, "A4");
191                 }
192
193                 [Test]
194                 public void Render1 ()
195                 {
196                         ListBoxPoker l = new ListBoxPoker ();
197                         for (int i = 0; i < 3; i ++)
198                                 l.Items.Add (i.ToString ());
199
200                         l.SelectedIndex = l.Items.Count - 1;
201 #if NET_2_0
202                         string exp = @"<select size=""4"">
203         <option value=""0"">0</option>
204         <option value=""1"">1</option>
205         <option selected=""selected"" value=""2"">2</option>
206
207 </select>";
208 #else
209                         string exp = @"<select name size=""4"">
210         <option value=""0"">0</option>
211         <option value=""1"">1</option>
212         <option selected=""selected"" value=""2"">2</option>
213
214 </select>";
215 #endif
216                         HtmlDiff.AssertAreEqual (exp, l.Render (), "Render1");
217                 }
218
219                 DataSet GetExampleData ()
220                 {
221                         DataSet ds = new DataSet ();
222                         ds.ReadXml (new StringReader (@"
223 <DataSet>
224         <Stocks Company='Novell Inc.'     Symbol='NOVL' Price='6.14'   />
225         <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92'  />
226         <Stocks Company='Google'          Symbol='GOOG' Price='291.60' />
227 </DataSet>
228 "));
229                         return ds;
230                 }
231                 
232                 [Test]
233                 public void DoubleDataBind ()
234                 {
235                         ListBoxPoker l = new ListBoxPoker ();
236                         l.DataSource = GetExampleData ();
237                         l.DataTextField = "Company";
238                         l.DataBind ();
239                         l.DataBind ();
240 #if NET_2_0
241                         string exp = @"<select size=""4"">
242         <option value=""Novell Inc."">Novell Inc.</option>
243         <option value=""Microsoft Corp."">Microsoft Corp.</option>
244         <option value=""Google"">Google</option>
245
246 </select>";
247 #else
248                         string exp = @"<select name size=""4"">
249         <option value=""Novell Inc."">Novell Inc.</option>
250         <option value=""Microsoft Corp."">Microsoft Corp.</option>
251         <option value=""Google"">Google</option>
252
253 </select>";
254 #endif
255                         HtmlDiff.AssertAreEqual (exp, l.Render (), "DoubleDataBind");
256                 }
257
258                 class MyNC : Control, INamingContainer {
259                 }
260
261                 [Test]
262                 [Category ("NotDotNet")]
263         [Category ("NotWorking")]
264                 public void NameIsUniqueID ()
265                 {
266                         ListBoxPoker list = new ListBoxPoker ();
267                         Page page = new Page ();
268                         page.ID = "pg";
269                         Control ctrl = new MyNC ();
270                         ctrl.ID = "ctrl";
271                         page.Controls.Add (ctrl);
272                         ctrl.Controls.Add (list);
273                         string str = list.Render();
274                         Assert.IsTrue (-1 != list.Render ().IndexOf (':'), "unique");
275                 }
276
277                 [Test]
278                 public void HtmlEncodedText ()
279                 {
280                         ListBoxPoker list = new ListBoxPoker ();
281                         // The att. value is encoded by the writer, but the text is encoded in ListBox.
282                         list.Items.Add (new ListItem ("\"hola", "\"adios"));
283                         string output = list.Render ();
284                         Assert.IsTrue (-1 != output.IndexOf ("&quot;hola"), "#01");
285                         Assert.IsTrue (-1 != output.IndexOf ("&quot;adios"), "#02");
286                 }
287
288                 [Test]
289                 public void SelectSingle1 ()
290                 {
291                         ListBoxPoker list = new ListBoxPoker ();
292                         list.Items.Add (new ListItem ("1", "first"));
293                         list.Items.Add (new ListItem ("2", "second"));
294                         list.SelectedIndex = 0;
295                         NameValueCollection coll = new NameValueCollection ();
296                         coll.Add ("2", "second");
297                         Assert.IsTrue (list.LoadPD ("2", coll), "#00");
298                         Assert.IsFalse (list.Items [0].Selected, "#01");
299                         Assert.IsTrue (list.Items [1].Selected, "#02");
300                         Assert.AreEqual (1, list.SelectedIndex, "#03");
301                 }
302
303                 [Test]
304                 public void SelectSingle2 ()
305                 {
306                         ListBoxPoker list = new ListBoxPoker ();
307                         list.Items.Add (new ListItem ("1", "first"));
308                         list.Items.Add (new ListItem ("2", "second"));
309                         list.SelectedIndex = 0;
310                         NameValueCollection coll = new NameValueCollection ();
311                         coll.Add ("willnotbefound", "second");
312                         Assert.IsTrue (list.LoadPD ("2", coll), "#00");
313                         Assert.IsFalse (list.Items [0].Selected, "#01");
314                         Assert.IsFalse (list.Items [1].Selected, "#02");
315                         Assert.AreEqual (-1, list.SelectedIndex, "#03");
316                 }
317
318                 [Test]
319                 public void SelectMultiple1 ()
320                 {
321                         ListBoxPoker list = new ListBoxPoker ();
322                         list.SelectionMode = ListSelectionMode.Multiple;
323                         list.Items.Add (new ListItem ("1", "first"));
324                         list.Items.Add (new ListItem ("2", "second"));
325                         list.SelectedIndex = 0;
326                         NameValueCollection coll = new NameValueCollection ();
327                         coll.Add ("2", "second");
328                         Assert.IsTrue (list.LoadPD ("2", coll), "#00");
329                         Assert.IsFalse (list.Items [0].Selected, "#01");
330                         Assert.IsTrue (list.Items [1].Selected, "#02");
331                         Assert.AreEqual (1, list.SelectedIndex, "#03");
332                 }
333
334                 [Test]
335                 public void SelectMultiple2 ()
336                 {
337                         ListBoxPoker list = new ListBoxPoker ();
338                         list.SelectionMode = ListSelectionMode.Multiple;
339                         list.Items.Add (new ListItem ("1", "first"));
340                         list.Items.Add (new ListItem ("2", "second"));
341                         list.Items.Add (new ListItem ("3", "third"));
342                         list.Items.Add (new ListItem ("4", "forth"));
343                         NameValueCollection coll = new NameValueCollection ();
344                         coll.Add ("key", "second");
345                         coll.Add ("key", "forth");
346                         Assert.IsTrue (list.LoadPD ("key", coll), "#00");
347                         Assert.IsFalse (list.Items [0].Selected, "#01");
348                         Assert.IsTrue (list.Items [1].Selected, "#02");
349                         Assert.IsFalse (list.Items [2].Selected, "#03");
350                         Assert.IsTrue (list.Items [3].Selected, "#04");
351
352                         Assert.IsFalse (list.LoadPD ("key", coll), "#05");
353                         Assert.IsFalse (list.Items [0].Selected, "#06");
354                         Assert.IsTrue (list.Items [1].Selected, "#07");
355                         Assert.IsFalse (list.Items [2].Selected, "#08");
356                         Assert.IsTrue (list.Items [3].Selected, "#09");
357
358                         coll.Clear ();
359                         coll.Add ("key", "first");
360                         coll.Add ("key", "third");
361                         Assert.IsTrue (list.LoadPD ("key", coll), "#10");
362                         Assert.IsTrue (list.Items [0].Selected, "#11");
363                         Assert.IsFalse (list.Items [1].Selected, "#12");
364                         Assert.IsTrue (list.Items [2].Selected, "#13");
365                         Assert.IsFalse (list.Items [3].Selected, "#14");
366
367                 }
368 #if NET_2_0
369         [Test]
370 #if TARGET_JVM
371         [Ignore ("TD #7164")]
372 #endif
373         public void VerifyMultiSelectPositive()
374         {
375             ListBoxPoker list = new ListBoxPoker();
376             list.SelectionMode = ListSelectionMode.Multiple;
377             list.VerifyMultiSelect();
378         }
379
380         [Test]
381         [ExpectedException(typeof(HttpException))]
382         public void VerifyMultiSelectNegative()
383         {
384             ListBoxPoker list = new ListBoxPoker();
385             list.SelectionMode = ListSelectionMode.Single;
386             list.VerifyMultiSelect();
387         }
388 #endif
389         }
390 }
391
392