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