[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / ListControlTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.ListBoxTest.cs
3 //
4 // Author:
5 //      Jackson Harper (jackson@ximian.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.IO;
34 using System.Collections;
35 using System.Globalization;
36 using System.Web;
37 using System.Web.UI;
38 using System.Web.UI.WebControls;
39 using MonoTests.stand_alone.WebHarness;
40 using MonoTests.SystemWeb.Framework;
41
42 namespace MonoTests.System.Web.UI.WebControls 
43 {
44
45         public class ListControlPoker : ListControl {
46
47                 public ListControlPoker ()
48                 {
49                 }
50
51                 public void TrackState ()
52                 {
53                         TrackViewState ();
54                 }
55
56                 public object SaveState ()
57                 {
58                         return SaveViewState ();
59                 }
60
61                 public void LoadState (object state)
62                 {
63                         LoadViewState (state);
64                 }
65
66                 public object ViewStateValue (string name)
67                 {
68                         return ViewState [name];
69                 }
70
71                 public string Render ()
72                 {
73                         StringWriter sw = new StringWriter ();
74                         sw.NewLine = "\n";
75                         HtmlTextWriter writer = new HtmlTextWriter (sw);
76                         base.Render (writer);
77                         return writer.InnerWriter.ToString ();
78                 }
79
80                 public bool GetIsTrackingViewState ()
81                 {
82                         return IsTrackingViewState;
83                 }
84                 public HtmlTextWriterTag GetTagKey ()
85                 {
86                         return TagKey;
87                 }
88
89                 public new void OnSelectedIndexChanged (EventArgs e)
90                 {
91                         base.OnSelectedIndexChanged (e);
92                 }
93
94                 public new void OnTextChanged (EventArgs e)
95                 {
96                         base.OnTextChanged (e);
97                 }
98
99                 public new void VerifyMultiSelect ()
100                 {
101                         base.VerifyMultiSelect ();
102                 }
103
104         }
105
106         [TestFixture]
107         public class ListControlTest
108         {
109                 #region help_class_for_addattributetorender
110                 class Poker : ListControl
111                 {
112                         public void TrackState ()
113                         {
114                                 TrackViewState ();
115                         }
116
117                         public object SaveState ()
118                         {
119                                 return SaveViewState ();
120                         }
121
122                         public void LoadState (object state)
123                         {
124                                 LoadViewState (state);
125                         }
126
127                         public object ViewStateValue (string name)
128                         {
129                                 return ViewState[name];
130                         }
131
132
133                         public string Render ()
134                         {
135                                 StringWriter sw = new StringWriter ();
136                                 sw.NewLine = "\n";
137                                 HtmlTextWriter writer = new HtmlTextWriter (sw);
138                                 base.Render (writer);
139                                 return writer.InnerWriter.ToString ();
140                         }
141                         ArrayList Databound ()
142                         {
143                                 ArrayList list = new ArrayList ();
144                                 list.Add (1);
145                                 list.Add (2);
146                                 list.Add (3);
147                                 return list;
148                         }
149
150                         public HtmlTextWriterTag GetTagKey ()
151                         {
152                                 return TagKey;
153                         }
154
155                         protected override void AddAttributesToRender (HtmlTextWriter writer)
156                         {
157                                 writer.AddAttribute (HtmlTextWriterAttribute.Type, "MyType");
158                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, "MyName");
159                                 writer.AddAttribute (HtmlTextWriterAttribute.Value, "MyValue");
160                                 base.AddAttributesToRender (writer);
161                         }
162
163                         protected internal override void PerformDataBinding (IEnumerable dataSource)
164                         {
165                                 base.PerformDataBinding (Databound());
166                         }
167
168                         protected override void PerformSelect ()
169                         {
170                                 base.PerformSelect ();
171                                 SelectedIndex = 0;
172                         }
173
174                         public new void SetPostDataSelection(int selectedItem)
175                         {
176                                 base.SetPostDataSelection(selectedItem);
177                         }
178                 }
179                 #endregion
180                 private Hashtable changed = new Hashtable ();
181
182                 [TestFixtureSetUp]
183                 public void SetUp ()
184                 {
185                         WebTest.CopyResource (GetType (), "ListControlPage.aspx", "ListControlPage.aspx");
186                 }
187
188                 [Test]
189                 public void DefaultProperties ()
190                 {
191                         ListControlPoker p = new ListControlPoker ();
192
193                         Assert.AreEqual (p.AutoPostBack, false, "A1");
194                         Assert.AreEqual (p.DataMember, String.Empty, "A2");
195                         Assert.AreEqual (p.DataSource, null, "A3");
196                         Assert.AreEqual (p.DataTextField, String.Empty, "A4");
197                         Assert.AreEqual (p.DataTextFormatString, String.Empty, "A5");
198                         Assert.AreEqual (p.DataValueField, String.Empty, "A6");
199                         Assert.AreEqual (p.Items.Count, 0, "A7");
200                         Assert.AreEqual (p.SelectedIndex, -1,"A8");
201                         Assert.AreEqual (p.SelectedItem, null, "A9");
202                         Assert.AreEqual (p.SelectedValue, String.Empty, "A10");
203                         Assert.IsFalse (p.AppendDataBoundItems, "A11");
204                         Assert.AreEqual (p.Text, "", "A12");
205                         Assert.AreEqual (p.GetTagKey(), HtmlTextWriterTag.Select, "A13");
206                         Assert.AreEqual (false, p.AppendDataBoundItems, "A14");
207                         Assert.AreEqual (false, p.CausesValidation, "A15");
208                 }
209
210                 [Test]
211                 public void AddAttributesToRender ()
212                 {
213                         Poker p = new Poker ();
214                         ListItem foo = new ListItem ("foo");
215                         ListItem bar = new ListItem ("bar");
216                         p.Items.Add (foo);
217                         p.Items.Add (bar);
218 #region orig
219                         string orig = "<select type=\"MyType\" name=\"MyName\" value=\"MyValue\">\n\t<option value=\"foo\">foo</option>\n\t<option value=\"bar\">bar</option>\n\n</select>";
220 #endregion
221                         string html =  p.Render ();
222                         HtmlDiff.AssertAreEqual(orig,html,"AddAttributesToRender failed");
223                 }
224
225                 [Test]
226                 public void AppendDataBoundItems ()
227                 {
228                         ListControlPoker p = new ListControlPoker ();
229                         ListItem foo = new ListItem ("foo");
230                         ListItem bar = new ListItem ("bar");
231                         p.Items.Add (foo);
232                         p.Items.Add (bar);
233                         Assert.AreEqual (2, p.Items.Count, "Before databound");
234                         p.AppendDataBoundItems = true; // Not to clear list before databind
235                         p.DataSource = Databound ();
236                         p.DataBind ();
237                         Assert.AreEqual (5, p.Items.Count, "After databound");
238                         p.AppendDataBoundItems = false; // To clear list before databind
239                         p.DataBind ();
240                         Assert.AreEqual (3, p.Items.Count, "Clear list and databound");
241                 }
242
243                 ArrayList Databound ()
244                 {
245                         ArrayList list = new ArrayList ();
246                         list.Add (1);
247                         list.Add (2);
248                         list.Add (3);
249                         return list;
250                 }
251
252                 [Test]
253                 [Category ("NunitWeb")]
254                 public void CausesValidation_ValidationGroup ()
255                 {
256                         WebTest t = new WebTest ("ListControlPage.aspx");
257                         string str = t.Run ();
258                         FormRequest fr = new FormRequest (t.Response, "form1");
259                         fr.Controls.Add ("__EVENTTARGET");
260                         fr.Controls.Add ("__EVENTARGUMENT");
261                         fr.Controls.Add ("ListBox1");
262                         fr.Controls["__EVENTTARGET"].Value = "ListBox1";
263                         fr.Controls["__EVENTARGUMENT"].Value = "";
264                         fr.Controls["ListBox1"].Value = "2";
265                         t.Request = fr;
266                         string html = t.Run ();
267
268                         if (html.IndexOf ("Validate_validation_group") == -1)
269                                 Assert.Fail ("Validate not created");
270                         if (html.IndexOf ("MyValidationGroup") == -1)
271                                 Assert.Fail ("Wrong validation group");
272                 }
273
274                 [Test]
275                 public void OnTextChanged ()
276                 {
277                         ListControlPoker p = new ListControlPoker ();
278                         p.TextChanged += new EventHandler (p_TextChanged);
279                         Assert.AreEqual (false, eventTextChanged, "Before");
280                         p.OnTextChanged (new EventArgs ());
281                         Assert.AreEqual (true, eventTextChanged, "After");
282                 }
283
284                 bool eventTextChanged;
285                 void p_TextChanged (object sender, EventArgs e)
286                 {
287                         eventTextChanged = true;
288                 }
289
290                 [Test]
291                 public void PerformDataBind_PerformSelect ()
292                 {
293                         Poker p = new Poker ();
294                         p.DataBind ();
295                         string html = p.Render ();
296 #region #1
297                         string orig = "<select type=\"MyType\" name=\"MyName\" value=\"MyValue\">\n\t<option selected=\"selected\" value=\"1\">1</option>\n\t<option value=\"2\">2</option>\n\t<option value=\"3\">3</option>\n\n</select>";
298 #endregion
299                         HtmlDiff.AssertAreEqual (orig, html, "PerformDataBind");
300                 }
301
302                 [Test]
303                 [Category("NotWorking")] //Not implemented
304                 public void SetPostDataSelection ()
305                 {
306                         Poker p = new Poker ();
307                         ListItem foo = new ListItem ("foo");
308                         ListItem bar = new ListItem ("bar");
309                         p.Items.Add (foo);
310                         p.Items.Add (bar);
311                         p.SetPostDataSelection (1);
312                         Assert.AreEqual (1, p.SelectedIndex, "SetPostDataSelection");
313                 }
314
315                 [Test]
316                 public void Text ()
317                 {
318                         Poker p = new Poker ();
319                         ListItem foo = new ListItem ("foo");
320                         ListItem bar = new ListItem ("bar");
321                         p.Items.Add (foo);
322                         p.Items.Add (bar);
323                         Assert.AreEqual (string.Empty, p.Text, "Text#1");
324                         p.SelectedIndex = 1;
325                         Assert.AreEqual ("bar", p.Text, "Text#2");
326                 }
327
328                 #region HelpListForMultiple
329                 class PokerL : ListBox
330                 {
331                         public PokerL ()
332                         {
333                                 InitializeMyListBox ();
334                         }
335                         private void InitializeMyListBox ()
336                         {
337                                 // Add items to the ListBox.
338                                 Items.Add ("A");
339                                 Items.Add ("C");
340                                 Items.Add ("E");
341                                 Items.Add ("F");
342                                 Items.Add ("G");
343                                 Items.Add ("D");
344                                 Items.Add ("B");
345
346                                 // Set the SelectionMode to select multiple items.
347                                 SelectionMode = ListSelectionMode.Multiple;
348
349                                 Items[0].Selected = true;
350                                 Items[2].Selected = true;
351                         }
352
353                         public void TrackState ()
354                         {
355                                 TrackViewState ();
356                         }
357
358                         public object SaveState ()
359                         {
360                                 return SaveViewState ();
361                         }
362
363                         public void LoadState (object state)
364                         {
365                                 LoadViewState (state);
366                         }
367
368                         public object ViewStateValue (string name)
369                         {
370                                 return ViewState[name];
371                         }
372
373                         public string Render ()
374                         {
375                                 StringWriter sw = new StringWriter ();
376                                 sw.NewLine = "\n";
377                                 HtmlTextWriter writer = new HtmlTextWriter (sw);
378                                 base.Render (writer);
379                                 return writer.InnerWriter.ToString ();
380                         }
381                 }
382                 #endregion
383                 [Test]
384                 public void Multiple ()
385                 {
386                         PokerL p = new PokerL ();
387                         Assert.AreEqual (true, p.Items[0].Selected, "MultipleSelect#1");
388                         Assert.AreEqual (true, p.Items[2].Selected, "MultipleSelect#2");
389                         string html = p.Render ();
390                         #region origin
391                         string orig = "<select size=\"4\" multiple=\"multiple\">\n\t<option selected=\"selected\" value=\"A\">A</option>\n\t<option value=\"C\">C</option>\n\t<option selected=\"selected\" value=\"E\">E</option>\n\t<option value=\"F\">F</option>\n\t<option value=\"G\">G</option>\n\t<option value=\"D\">D</option>\n\t<option value=\"B\">B</option>\n\n</select>";
392                         #endregion
393                         HtmlDiff.AssertAreEqual (orig, html, "MultipleSelect#3");
394                 }
395
396                 [Test]
397                 public void CleanProperties ()
398                 {
399                         ListControlPoker p = new ListControlPoker ();
400
401                         p.AutoPostBack = true;
402                         Assert.AreEqual (p.AutoPostBack, true, "A2");
403
404                         p.DataMember = "DataMember";
405                         Assert.AreEqual (p.DataMember, "DataMember", "A3");
406
407                         p.DataSource = "DataSource";
408                         Assert.AreEqual (p.DataSource, "DataSource", "A4");
409
410                         p.DataTextField = "DataTextField";
411                         Assert.AreEqual (p.DataTextField, "DataTextField", "A5");
412
413                         p.DataTextFormatString = "DataTextFormatString";
414                         Assert.AreEqual (p.DataTextFormatString, "DataTextFormatString", "A6");
415
416                         p.DataValueField = "DataValueField";
417                         Assert.AreEqual (p.DataValueField, "DataValueField", "A7");
418
419                         p.SelectedIndex = 10;
420                         Assert.AreEqual (p.SelectedIndex, -1, "A8");
421
422                         p.SelectedValue = "SelectedValue";
423                         Assert.AreEqual (p.SelectedValue, String.Empty, "A9");
424                 }
425
426                 [Test]
427                 public void NullProperties ()
428                 {
429                         ListControlPoker p = new ListControlPoker ();
430
431                         p.DataMember = null;
432                         Assert.AreEqual (p.DataMember, String.Empty, "A1");
433
434                         p.DataSource = null;
435                         Assert.AreEqual (p.DataSource, null, "A2");
436
437                         p.DataTextField = null;
438                         Assert.AreEqual (p.DataTextField, String.Empty, "A3");
439
440                         p.DataTextFormatString = null;
441                         Assert.AreEqual (p.DataTextFormatString, String.Empty, "A4");
442
443                         p.DataValueField = null;
444                         Assert.AreEqual (p.DataValueField, String.Empty, "A5");
445
446                         p.SelectedValue = null;
447                         Assert.AreEqual (p.SelectedValue, String.Empty, "A6");
448                 }
449
450                 [Test]
451                 public void ClearSelection ()
452                 {
453                         ListControlPoker p = new ListControlPoker ();
454
455                         ListItem foo = new ListItem ("foo");
456                         ListItem bar = new ListItem ("bar");
457
458                         BeginIndexChanged (p);
459
460                         p.Items.Add (foo);
461                         p.Items.Add (bar);
462                         p.SelectedIndex = 1;
463
464                         // sanity for the real test
465                         Assert.AreEqual (p.Items.Count, 2, "A1");
466                         Assert.AreEqual (p.SelectedIndex, 1, "A2");
467                         Assert.AreEqual (p.SelectedItem, bar, "A3");
468                         Assert.AreEqual (p.SelectedValue, bar.Value, "A4");
469                         
470                         p.ClearSelection ();
471
472                         Assert.AreEqual (p.SelectedIndex, -1, "A5");
473                         Assert.AreEqual (p.SelectedItem, null, "A6");
474                         Assert.AreEqual (p.SelectedValue, String.Empty, "A7");
475                         Assert.IsFalse (EndIndexChanged (p), "A8");
476
477                         // make sure we are still sane
478                         Assert.AreEqual (p.Items.Count, 2, "A9");
479                 }
480
481                 [Test]
482                 // Tests Save/Load ControlState
483                 public void ControlState ()
484                 {
485                         ListControlPoker a = new ListControlPoker ();
486                         ListControlPoker b = new ListControlPoker ();
487
488                         a.TrackState ();
489
490                         a.Items.Add ("a");
491                         a.Items.Add ("b");
492                         a.Items.Add ("c");
493                         a.SelectedIndex = 2;
494
495                         b.Items.Add ("a");
496                         b.Items.Add ("b");
497                         b.Items.Add ("c");
498
499                         Assert.AreEqual (-1, b.SelectedIndex, "A1");
500                 }
501
502                 [Test]
503                 // Tests Save/Load/Track ViewState
504                 public void ViewState ()
505                 {
506                         ListControlPoker a = new ListControlPoker ();
507                         ListControlPoker b = new ListControlPoker ();
508
509                         a.TrackState ();
510
511                         BeginIndexChanged (a);
512                         BeginIndexChanged (b);
513
514                         a.Items.Add ("a");
515                         a.Items.Add ("b");
516                         a.Items.Add ("c");
517                         a.SelectedIndex = 2;
518
519                         object state = a.SaveState ();
520                         b.LoadState (state);
521
522                         Assert.AreEqual (2, b.SelectedIndex, "A1");
523                         Assert.AreEqual (b.Items.Count, 3, "A2");
524
525                         Assert.AreEqual (b.Items [0].Value, "a", "A3");
526                         Assert.AreEqual (b.Items [1].Value, "b", "A4");
527                         Assert.AreEqual (b.Items [2].Value, "c", "A5");
528
529                         Assert.IsFalse (EndIndexChanged (a), "A6");
530                         Assert.IsFalse (EndIndexChanged (b), "A7");
531                 }
532
533                 [Test]
534                 // Tests Save/Load/Track ViewState
535                 public void ViewStateIsNeeded ()
536                 {
537                         ListControlPoker a = new ListControlPoker ();
538                         IStateManager sm = a.Items as IStateManager;
539                         
540                         Assert.IsFalse (a.GetIsTrackingViewState (), "#A1-1");
541                         Assert.IsFalse (sm.IsTrackingViewState, "#A1-2");
542                         object state = a.SaveState ();
543                         Assert.IsNotNull (state, "#A1-3");
544
545                         a.Items.Add ("a");
546                         a.Items.Add ("b");
547                         a.Items.Add ("c");
548
549                         Assert.IsFalse (a.GetIsTrackingViewState (), "#A2-1");
550                         Assert.IsFalse (sm.IsTrackingViewState, "#A2-2");
551
552                         state = a.SaveState ();
553                         Assert.IsNotNull (state, "#A3");
554                 }
555
556                 [Test]
557                 public void ViewStateContents ()
558                 {
559                         ListControlPoker p = new ListControlPoker ();
560
561                         p.TrackState ();
562
563                         // So the selected index can be set
564                         p.Items.Add ("one");
565                         p.Items.Add ("two");
566
567                         p.AutoPostBack = false;
568                         p.DataMember = "DataMember";
569                         p.DataSource = "DataSource";
570                         p.DataTextField = "DataTextField";
571                         p.DataTextFormatString = "DataTextFormatString";
572                         p.DataValueField = "DataValueField";
573                         p.SelectedIndex = 1;
574                         p.AppendDataBoundItems = true;
575                         p.Text = "Text";
576
577                         Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
578                         Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
579
580                         Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
581                         Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
582                         Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
583                                         "DataTextFormatString", "A5");
584                         Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
585
586                         Assert.AreEqual (p.ViewStateValue ("AppendDataBoundItems"), true, "A7");
587
588                         // None of these are saved
589                         Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A8");
590                         Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
591                         Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A10");
592                         Assert.AreEqual (p.ViewStateValue ("Text"), null, "A11");
593
594                 }
595
596                 [Test]
597                 public void SelectedIndex ()
598                 {
599                         ListControlPoker p = new ListControlPoker ();
600
601                         p.Items.Add ("one");
602                         p.Items.Add ("two");
603                         p.Items.Add ("three");
604                         p.Items.Add ("four");
605
606                         p.Items [2].Selected = true;
607                         p.Items [1].Selected = true;
608
609                         Assert.AreEqual (p.SelectedIndex, 1, "A1");
610
611                         p.ClearSelection ();
612                         p.Items [3].Selected = true;
613
614                         Assert.AreEqual (p.SelectedIndex, 3, "A2");
615
616                         p.SelectedIndex = 1;
617                         Assert.AreEqual (p.SelectedIndex, 1, "A3");
618                         Assert.IsFalse (p.Items [3].Selected, "A4");
619                 }
620
621                 [Test]
622                 public void Render ()
623                 {
624                         ListControlPoker p = new ListControlPoker ();
625
626                         string s = p.Render ();
627                         string expected = "<select>\n\n</select>";
628                         Assert.AreEqual (s, expected, "A1");
629                 }
630
631                 [Test]
632                 public void ItemsTooHigh ()
633                 {
634                         ListControlPoker l = new ListControlPoker ();
635
636                         l.Items.Add ("foo");
637                         l.SelectedIndex = 1;
638                         Assert.AreEqual (-1, l.SelectedIndex, "#01");
639                 }
640
641                 [Test]
642                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
643                 public void ItemsTooLow ()
644                 {
645                         ListControlPoker l = new ListControlPoker ();
646
647                         l.Items.Add ("foo");
648                         l.SelectedIndex = -2;
649                 }
650
651                 [Test]
652                 public void ItemsOk ()
653                 {
654                         ListControlPoker l = new ListControlPoker ();
655
656                         l.Items.Add ("foo");
657                         l.SelectedIndex = 0;
658                         l.SelectedIndex = -1;
659                 }
660
661                 [Test]
662                 public void DataBinding1 ()
663                 {
664                         ListControlPoker p = new ListControlPoker ();
665                         ArrayList list = new ArrayList ();
666                         list.Add (1);
667                         list.Add (2);
668                         list.Add (3);
669                         p.DataSource = list;
670                         p.SelectedIndex = 2;
671                         Assert.AreEqual (-1, p.SelectedIndex, "#01");
672                         p.DataBind ();
673                         Assert.AreEqual (2, p.SelectedIndex, "#02");
674                         Assert.AreEqual (3.ToString (), p.SelectedValue, "#03");
675                 }
676
677                 [Test]
678                 [ExpectedException (typeof (ArgumentException))] // The SelectedIndex and SelectedValue are mutually exclusive 
679                 public void DataBinding2 ()
680                 {
681                         ListControlPoker p = new ListControlPoker ();
682                         ArrayList list = new ArrayList ();
683                         list.Add (1);
684                         list.Add (2);
685                         list.Add (3);
686                         p.DataSource = list;
687                         p.SelectedIndex = 0;
688                         p.SelectedValue = "3";
689                         p.DataBind ();
690                 }
691
692                 [Test]
693                 public void DataBinding3 ()
694                 {
695                         ListControlPoker p = new ListControlPoker ();
696                         ArrayList list = new ArrayList ();
697                         list.Add (1);
698                         list.Add (2);
699                         list.Add (3);
700                         p.DataSource = list;
701                         // If Index and Value are used, everything's ok if they are selecting
702                         // the same thing.
703                         p.SelectedIndex = 2;
704                         p.SelectedValue = "3";
705                         Assert.AreEqual ("", p.SelectedValue, "#01");
706                         p.DataBind ();
707                         Assert.AreEqual ("3", p.SelectedValue, "#02");
708                 }
709
710                 [Test]
711                 [ExpectedException (typeof (NullReferenceException))]
712                 public void DataBinding4 ()
713                 {
714                         ListControlPoker p = new ListControlPoker ();
715                         ArrayList list = new ArrayList ();
716                         list.Add (1);
717                         list.Add (null);
718                         list.Add (3);
719                         p.DataSource = list;
720                         p.DataBind ();
721                 }
722
723                 [Test]
724                 public void DataBinding6 () {
725                         ListControlPoker p = new ListControlPoker ();
726                         ArrayList list = new ArrayList ();
727                         list.Add (1);
728                         list.Add (2);
729                         list.Add (3);
730                         p.DataSource = list;
731                         p.DataBind ();
732                         Assert.AreEqual (3, p.Items.Count, "#01");
733                         p.DataSource = null;
734                         p.DataBind ();
735                         Assert.AreEqual (3, p.Items.Count, "#01");
736                 }
737
738                 [Test]
739                 public void DataBinding7 () {
740                         ListControlPoker p = new ListControlPoker ();
741                         ArrayList list = new ArrayList ();
742                         list.Add (1);
743                         list.Add (2);
744                         list.Add (3);
745                         p.DataSource = list;
746                         p.DataBind ();
747
748                         p.SelectedValue = "3";
749                         Assert.AreEqual (2, p.SelectedIndex, "#01");
750                         
751                         p.DataBind ();
752                         Assert.AreEqual ("3", p.SelectedValue, "#02");
753                         Assert.AreEqual (2, p.SelectedIndex, "#03");
754                 }
755
756                 [Test]
757                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
758                 public void DataBinding8 () {
759                         ListControlPoker p = new ListControlPoker ();
760                         ArrayList list = new ArrayList ();
761                         list.Add (1);
762                         list.Add (2);
763                         list.Add (3);
764                         p.DataSource = list;
765                         p.DataBind ();
766
767                         p.SelectedValue = "3";
768                         Assert.AreEqual (2, p.SelectedIndex, "#01");
769
770                         list = new ArrayList ();
771                         list.Add (4);
772                         list.Add (5);
773                         list.Add (6);
774                         p.DataSource = list;
775                         p.DataBind ();
776                         Assert.AreEqual ("3", p.SelectedValue, "#01");
777                         Assert.AreEqual (2, p.SelectedIndex, "#01");
778                 }
779
780                 [Test]
781                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
782                 public void DataBinding9 () {
783                         ListControlPoker p = new ListControlPoker ();
784                         ArrayList list = new ArrayList ();
785                         list.Add (1);
786                         list.Add (2);
787                         list.Add (3);
788                         p.DataSource = list;
789                         p.SelectedValue = "5";
790                         p.DataBind ();
791                 }
792
793                 [Test]
794                 public void DataBinding1a () {
795                         ListControlPoker p = new ListControlPoker ();
796                         ArrayList list = new ArrayList ();
797                         list.Add (1);
798                         list.Add (2);
799                         list.Add (3);
800                         p.DataSource = list;
801                         p.SelectedIndex = 4;
802                         Assert.AreEqual (-1, p.SelectedIndex, "#01");
803                         p.DataBind ();
804                         Assert.AreEqual (-1, p.SelectedIndex, "#02");
805                         Assert.AreEqual ("", p.SelectedValue, "#03");
806                 }
807                 
808                 [Test]
809                 public void DataBinding10 () {
810                         ListControlPoker p = new ListControlPoker ();
811                         ArrayList list = new ArrayList ();
812                         list.Add (1);
813                         list.Add (2);
814                         list.Add (3);
815                         p.DataSource = list;
816                         p.DataBind ();
817
818                         p.SelectedValue = "5";
819                         Assert.AreEqual ("", p.SelectedValue, "#01");
820                         
821                         p.SelectedIndex = 4;
822                         Assert.AreEqual (-1, p.SelectedIndex, "#02");
823                 }
824
825                 [Test]
826                 public void DataBinding11 () {
827                         ListControlPoker p = new ListControlPoker ();
828                         ArrayList list = new ArrayList ();
829                         list.Add (1);
830                         list.Add (2);
831                         list.Add (3);
832                         p.DataSource = list;
833                         p.SelectedValue = "3";
834                         p.DataBind ();
835
836                         p.SelectedValue = "5";
837                         Assert.AreEqual ("3", p.SelectedValue, "#01");
838
839                         p.SelectedIndex = 4;
840                         Assert.AreEqual (2, p.SelectedIndex, "#02");
841
842                         p.Items.Clear ();
843                         Assert.AreEqual ("", p.SelectedValue, "#03");
844                         Assert.AreEqual (-1, p.SelectedIndex, "#04");
845
846                         p.Items.Add (new ListItem ("1"));
847                         p.Items.Add (new ListItem ("2"));
848                         p.Items.Add (new ListItem ("3"));
849                         p.Items.Add (new ListItem ("4"));
850                         p.Items.Add (new ListItem ("5"));
851                         Assert.AreEqual ("", p.SelectedValue, "#05");
852                         Assert.AreEqual (-1, p.SelectedIndex, "#06");
853                         
854                         list = new ArrayList ();
855                         list.Add (1);
856                         list.Add (2);
857                         list.Add (3);
858                         list.Add (4);
859                         list.Add (5);
860                         p.DataSource = list;
861                         p.DataBind ();
862                         Assert.AreEqual ("5", p.SelectedValue, "#07");
863                         Assert.AreEqual (4, p.SelectedIndex, "#08");
864                 }
865
866                 class Data 
867                 {
868                         string name;
869                         object val;
870
871                         public Data (string name, object val)
872                         {
873                                 this.name = name;
874                                 this.val = val;
875                         }
876
877                         public string Name {
878                                 get { return name; }
879                         }
880
881                         public object Value {
882                                 get { return val; }
883                         }
884                 }
885
886                 [Test]
887                 public void DataBinding5 ()
888                 {
889                         ListControlPoker p = new ListControlPoker ();
890                         p.DataTextField = "Name";
891                         p.DataValueField = "Value";
892                         ArrayList list = new ArrayList ();
893                         list.Add (new Data ("uno", 1));
894                         list.Add (new Data ("dos", 2));
895                         list.Add (new Data ("tres", 3));
896                         p.DataSource = list;
897                         p.SelectedIndex = 2;
898                         p.DataBind ();
899                         Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
900                         Assert.AreEqual ("tres", p.SelectedItem.Text, "#01");
901                 }
902
903                 [Test]
904                 public void DataBindingFormat1 ()
905                 {
906                         ListControlPoker p = new ListControlPoker ();
907                         ArrayList list = new ArrayList ();
908                         list.Add (1);
909                         list.Add (2);
910                         list.Add (3);
911                         p.DataSource = list;
912                         p.DataTextFormatString = "{0:00}";
913                         p.SelectedIndex = 2;
914                         p.DataBind ();
915                         Assert.AreEqual ("3", p.SelectedValue, "#01");
916                 }
917
918                 [Test]
919                 public void DataBindingFormat2 ()
920                 {
921                         ListControlPoker p = new ListControlPoker ();
922                         ArrayList list = new ArrayList ();
923                         list.Add (1);
924                         list.Add (2);
925                         list.Add (3);
926                         p.DataSource = list;
927                         p.DataTextFormatString = "{0:00}";
928                         p.SelectedIndex = 2;
929                         p.DataBind ();
930                         Assert.IsNotNull (p.SelectedItem, "#00");
931                         Assert.AreEqual ("03", p.SelectedItem.Text, "#01");
932                 }
933
934                 [Test]
935                 [ExpectedException (typeof (NullReferenceException))]
936                 public void DataBindingFormat3 ()
937                 {
938                         ListControlPoker p = new ListControlPoker ();
939                         ArrayList list = new ArrayList ();
940                         list.Add (1);
941                         list.Add (null);
942                         list.Add (3);
943                         p.DataSource = list;
944                         p.DataTextFormatString = "{0:00}";
945                         p.SelectedIndex = 2;
946                         p.DataBind ();
947                 }
948
949                 private void BeginIndexChanged (ListControl l)
950                 {
951                         l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
952                 }
953
954                 private bool EndIndexChanged (ListControl l)
955                 {
956                         bool res = changed [l] != null;
957                         changed [l] = null;
958                         return res;
959                 }
960
961                 private void IndexChangedHandler (object sender, EventArgs e)
962                 {
963                         changed [sender] = new object ();
964                 }
965
966                 [Test]
967                 public void ValueFound1 ()
968                 {
969                         ListControlPoker p = new ListControlPoker ();
970                         p.Items.Add ("one");
971                         p.SelectedValue = "one";
972                 }
973
974                 [Test]
975                 public void ValueNotFound1 ()
976                 {
977                         ListControlPoker p = new ListControlPoker ();
978                         p.Items.Add ("one");
979                         p.SelectedValue = "dos";
980             Assert.AreEqual("", p.SelectedValue, "SelectedValue");
981             Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
982             Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
983                 }
984
985                 [Test]
986                 public void ValueNotFound2 ()
987                 {
988                         ListControlPoker p = new ListControlPoker ();
989                         p.SelectedValue = "dos";
990             Assert.AreEqual("", p.SelectedValue, "SelectedValue");
991             Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
992             Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
993                 }
994                 [Test]
995                 [ExpectedException (typeof (HttpException))]
996                 public void VerifyMultiSelect_Exception ()
997                 {
998                         ListControlPoker p = new ListControlPoker ();
999                         p.VerifyMultiSelect ();
1000                 }
1001
1002                 [Test]
1003                 public void DataBinding_SelectedValue () {
1004                         ListControlPoker p = new ListControlPoker ();
1005                         p.SelectedValue = "b";
1006
1007                         p.Items.Add (new ListItem ("a", "a"));
1008                         p.Items.Add (new ListItem ("b", "b"));
1009                         p.Items.Add (new ListItem ("c", "c"));
1010
1011                         Assert.IsFalse (p.Items [1].Selected, "SelectedIndex");
1012                         p.DataBind ();
1013                         Assert.IsTrue (p.Items [1].Selected, "SelectedIndex");
1014                 }
1015
1016                 [Test]
1017                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1018                 public void DataBinding_SelectedValue_Exception () {
1019                         ListControlPoker p = new ListControlPoker ();
1020                         p.SelectedValue = "AAA";
1021
1022                         p.Items.Add (new ListItem ("a", "a"));
1023                         p.Items.Add (new ListItem ("b", "b"));
1024                         p.Items.Add (new ListItem ("c", "c"));
1025
1026                         Assert.IsFalse (p.Items [1].Selected, "SelectedIndex");
1027                         p.DataBind ();
1028                         Assert.IsTrue (p.Items [1].Selected, "SelectedIndex");
1029                 }
1030
1031                 [TestFixtureTearDown]
1032                 public void TearDown ()
1033                 {
1034                         WebTest.Unload ();
1035                 }
1036
1037         }
1038 }
1039
1040
1041
1042
1043