2 // Tests for System.Web.UI.WebControls.ListBoxTest.cs
5 // Jackson Harper (jackson@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using NUnit.Framework;
36 using System.Collections.Specialized;
37 using System.Globalization;
40 using System.Web.UI.WebControls;
42 using MonoTests.stand_alone.WebHarness;
44 namespace MonoTests.System.Web.UI.WebControls
46 class ListBoxPoker : ListBox {
48 public ListBoxPoker ()
53 public bool LoadPD (string key, NameValueCollection values)
55 return ((IPostBackDataHandler) this).LoadPostData (key, values);
58 public object SaveState ()
60 return SaveViewState ();
63 public void LoadState (object o)
68 public StateBag _ViewState {
69 get { return ViewState; }
73 public string Render ()
75 StringWriter sw = new StringWriter ();
77 HtmlTextWriter writer = new HtmlTextWriter (sw);
79 return writer.InnerWriter.ToString ();
82 public new virtual void VerifyMultiSelect()
84 base.VerifyMultiSelect();
90 public class ListBoxTest {
93 public void Defaults ()
95 ListBox lb = new ListBox ();
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");
106 public void SetProps ()
108 ListBox lb = new ListBox ();
110 lb.BorderColor = Color.Black;
111 Assert.AreEqual (lb.BorderColor, Color.Black, "A1");
113 lb.BorderStyle = BorderStyle.Dashed;
114 Assert.AreEqual (lb.BorderStyle, BorderStyle.Dashed, "A2");
117 Assert.AreEqual (lb.BorderWidth, (Unit) 0, "A3");
120 Assert.AreEqual (lb.BorderWidth, (Unit) 15, "A3");
123 Assert.AreEqual (lb.Rows, 1, "A4");
125 lb.SelectionMode = ListSelectionMode.Multiple;
126 Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Multiple, "A6");
130 Assert.AreEqual (lb.ToolTip, "foo", "A7");
132 Assert.AreEqual (lb.ToolTip, String.Empty, "A7"); // Always empty in 1.x
137 [Category ("NotWorking")]
139 [ExpectedException (typeof (ArgumentOutOfRangeException))]
141 public void RowsTooHigh ()
143 ListBox lb = new ListBox ();
148 [ExpectedException (typeof (ArgumentOutOfRangeException))]
149 public void RowsTooLow ()
151 ListBox lb = new ListBox ();
156 [ExpectedException (typeof (ArgumentOutOfRangeException))]
157 public void BadSelectionMode ()
159 ListBox lb = new ListBox ();
160 lb.SelectionMode = (ListSelectionMode) 500;
164 [ExpectedException (typeof (ArgumentOutOfRangeException))]
165 public void BadBorderStyle ()
167 ListBox lb = new ListBox ();
168 lb.BorderStyle = (BorderStyle) 500;
172 public void ViewState ()
174 ListBoxPoker p = new ListBoxPoker ();
176 p.BorderColor = Color.Red;
177 Assert.AreEqual (p._ViewState ["BorderColor"],
180 p.BorderStyle = BorderStyle.Double;
181 Assert.AreEqual (p._ViewState ["BorderStyle"],
182 BorderStyle.Double, "A2");
185 Assert.AreEqual (p._ViewState ["BorderWidth"],
188 p.SelectionMode = ListSelectionMode.Multiple;
189 Assert.AreEqual (p._ViewState ["SelectionMode"],
190 ListSelectionMode.Multiple, "A4");
194 public void Render1 ()
196 ListBoxPoker l = new ListBoxPoker ();
197 for (int i = 0; i < 3; i ++)
198 l.Items.Add (i.ToString ());
200 l.SelectedIndex = l.Items.Count - 1;
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>
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>
216 HtmlDiff.AssertAreEqual (exp, l.Render (), "Render1");
219 DataSet GetExampleData ()
221 DataSet ds = new DataSet ();
222 ds.ReadXml (new StringReader (@"
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' />
233 public void DoubleDataBind ()
235 ListBoxPoker l = new ListBoxPoker ();
236 l.DataSource = GetExampleData ();
237 l.DataTextField = "Company";
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>
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>
255 HtmlDiff.AssertAreEqual (exp, l.Render (), "DoubleDataBind");
258 class MyNC : Control, INamingContainer {
262 [Category ("NotDotNet")]
263 [Category ("NotWorking")]
264 public void NameIsUniqueID ()
266 ListBoxPoker list = new ListBoxPoker ();
267 Page page = new Page ();
269 Control ctrl = new MyNC ();
271 page.Controls.Add (ctrl);
272 ctrl.Controls.Add (list);
273 string str = list.Render();
274 Assert.IsTrue (-1 != list.Render ().IndexOf (':'), "unique");
278 public void HtmlEncodedText ()
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 (""hola"), "#01");
285 Assert.IsTrue (-1 != output.IndexOf (""adios"), "#02");
289 public void SelectSingle1 ()
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");
304 public void SelectSingle2 ()
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");
319 public void SelectMultiple1 ()
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");
335 public void SelectMultiple2 ()
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");
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");
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");
371 [Ignore ("TD #7164")]
373 public void VerifyMultiSelectPositive()
375 ListBoxPoker list = new ListBoxPoker();
376 list.SelectionMode = ListSelectionMode.Multiple;
377 list.VerifyMultiSelect();
381 [ExpectedException(typeof(HttpException))]
382 public void VerifyMultiSelectNegative()
384 ListBoxPoker list = new ListBoxPoker();
385 list.SelectionMode = ListSelectionMode.Single;
386 list.VerifyMultiSelect();