[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / MultiViewTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.View.cs
3 //
4 // Author:
5 //      Yoni Klein (yonik@mainsoft.com)
6 //
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using NUnit.Framework;
31 using System;
32 using System.IO;
33 using System.Globalization;
34 using System.Web;
35 using System.Web.UI;
36 using System.Web.UI.WebControls;
37 using MonoTests.SystemWeb.Framework;
38 using MonoTests.stand_alone.WebHarness;
39
40
41 namespace MonoTests.System.Web.UI.WebControls
42 {
43
44
45         class PokerMultiView : MultiView
46         {
47                 public PokerMultiView ()
48                 {
49                         TrackViewState ();
50                 }
51
52                 public object SaveState ()
53                 {
54                         return SaveControlState ();
55                 }
56
57                 public void LoadState (object o)
58                 {
59                         LoadControlState (o);
60                 }
61
62                 public StateBag StateBag
63                 {
64                         get { return base.ViewState; }
65                 }
66
67                 public string Render ()
68                 {
69                         StringWriter sw = new StringWriter ();
70                         HtmlTextWriter tw = new HtmlTextWriter (sw);
71                         Render (tw);
72                         return sw.ToString ();
73                 }
74
75                 public void DoAddParsedSubObject (object e)
76                 {
77                         AddParsedSubObject (e);
78                 }
79
80                 public void DoOnActiveViewChanged (EventArgs e)
81                 {
82                         base.OnActiveViewChanged (e);
83                 }
84
85                 public void DoBubbleEvent (object source, EventArgs e)
86                 {
87                         OnBubbleEvent (source, e);
88                 }
89
90                 public void AddViewCtrl (View v)
91                 {
92                         this.Controls.Add (v);
93                 }
94         }
95
96         [TestFixture]
97         public class MultiViewTest
98         {
99
100                 [SetUp]
101                 public void SetUp ()
102                 {
103                         WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
104                 }
105
106                 [Test]
107                 public void MultiView_DefaultProperties ()
108                 {
109                         PokerMultiView pmw = new PokerMultiView ();
110                         Assert.AreEqual (0, pmw.StateBag.Count, "ViewState.Count");
111                         Assert.AreEqual (-1, pmw.ActiveViewIndex, "ActiveViewIndex");
112                         Assert.AreEqual (0, pmw.Views.Count, "DefaultZeroViews");
113                         Assert.AreEqual (PokerMultiView.NextViewCommandName, "NextView", "DefaultNextViewCommandName");
114                         Assert.AreEqual (PokerMultiView.PreviousViewCommandName, "PrevView", "DefaultPrevViewCommandName");
115                         Assert.AreEqual (PokerMultiView.SwitchViewByIDCommandName, "SwitchViewByID", "SwitchViewByIDCommandName");
116                         Assert.AreEqual (PokerMultiView.SwitchViewByIndexCommandName, "SwitchViewByIndex", "SwitchViewByIndexCommandName");
117                 }
118
119                 [Test]
120                 public void MultiView_NotWorkingDefaultProperties ()
121                 {
122                         PokerMultiView pmw = new PokerMultiView ();
123                         Assert.IsTrue (pmw.EnableTheming, "EnableTheming");
124
125                 }
126
127                 [Test]
128                 public void MultiView_AddViews ()
129                 {
130                         PokerMultiView pmv = new PokerMultiView ();
131                         View v1 = new View ();
132                         pmv.Controls.Add (v1);
133                         Assert.AreEqual (1, pmv.Views.Count, "ViewsCount");
134                         Assert.AreEqual (-1, pmv.ActiveViewIndex, "ActiveViewIndex");
135                 }
136
137
138
139                 [Test]
140                 public void MultiView_ActiveIndex ()
141                 {
142                         PokerMultiView pmv = new PokerMultiView ();
143                         View myView = new View ();
144                         Assert.AreEqual (-1, pmv.ActiveViewIndex, "ActiveViewIndexDefault");
145                         pmv.ActiveViewIndex = 0;
146                         Assert.AreEqual (0, pmv.ActiveViewIndex, "ActiveViewIndexChange");
147                         pmv.Controls.Remove (myView);
148                         Assert.AreEqual (0, pmv.Controls.Count, "ControlsCount");
149                         Assert.AreEqual (0, pmv.ActiveViewIndex, "ActiveViewIndexRemove");
150                 }
151
152
153
154                 [Test]
155                 public void MultiView_SetActiveView ()
156                 {
157                         PokerMultiView pmw = new PokerMultiView ();
158                         PokerView pv1 = new PokerView ();
159                         pmw.Controls.Add (pv1);
160                         pmw.SetActiveView (pv1);
161                         Assert.AreEqual (pv1, pmw.GetActiveView (), "GetActiveView");
162                         Assert.AreEqual (1, pmw.Controls.Count, "MultiViewControlsCount");
163
164                 }
165
166                 [Test]
167                 public void MultiView_RemoveViewControlEvent ()
168                 {
169                         PokerMultiView pmv = new PokerMultiView ();
170                         View pv1 = new View ();
171                         View pv2 = new View ();
172                         View pv3 = new View ();
173                         pmv.Controls.Add (pv1);
174                         pmv.Controls.Add (pv2);
175                         pmv.Controls.Add (pv3);
176                         pmv.SetActiveView (pv1);
177                         Assert.AreEqual (0, pmv.ActiveViewIndex, "MultiViewActiveView");
178                         Assert.AreEqual (3, pmv.Controls.Count, "MultiViewControlsCount1");
179                         pmv.Controls.Remove (pv1);
180                         Assert.AreEqual (2, pmv.Controls.Count, "MultiViewControlsCount2");
181                         // Protected method MultiView RemovedControl has changed active view to next 
182                         Assert.AreSame (pv2, pmv.GetActiveView (), "EventRemovedControl");
183
184                 }
185
186                 [Test]
187                 public void MultiView_AddParsedSubObject ()
188                 {
189                         PokerMultiView pmv = new PokerMultiView ();
190                         View v1 = new View ();
191                         pmv.DoAddParsedSubObject (v1);
192                         Assert.AreEqual (1, pmv.Controls.Count, "AddParsedSubObjectSuccssed");
193                 }
194
195
196                 [Test]
197                 public void MultiView_CreateControlCollection ()
198                 {
199                         PokerMultiView pmv = new PokerMultiView ();
200                         Assert.IsNotNull (pmv.Views, "CreatingViewCollection");
201                 }
202
203
204                 [Test]
205                 public void MultiView_Render ()
206                 {
207                         PokerMultiView b = new PokerMultiView ();
208                         string html = b.Render ();
209                         Assert.AreEqual (b.Render (), string.Empty, "DefaultRender");
210                 }
211
212                 [Test]
213                 public void MultiView_ButtonRender ()
214                 {
215                         PokerMultiView m = new PokerMultiView ();
216                         PokerView v = new PokerView ();
217                         Button b = new Button ();
218                         b.ID = "test";
219                         v.Controls.Add (b);
220                         m.Controls.Add (v);
221                         m.SetActiveView (v);
222                         string html = m.Render ();
223                         Assert.AreEqual ("<input type=\"submit\" name=\"test\" value=\"\" id=\"test\" />", html, "ButtonRender");
224                 }
225
226                 [Test]
227                 public void MultiView_SomeViewsButtonRender ()
228                 {
229                         PokerMultiView m = new PokerMultiView ();
230                         View v = new View ();
231                         View v1 = new View ();
232                         Button b = new Button ();
233                         Button b1 = new Button ();
234                         b1.ID = "test1";
235                         b.ID = "test";
236                         v.Controls.Add (b);
237                         v1.Controls.Add (b1);
238                         m.Controls.Add (v);
239                         m.Controls.Add (v1);
240                         m.SetActiveView (v);
241                         Assert.AreEqual (m.Render (), "<input type=\"submit\" name=\"test\" value=\"\" id=\"test\" />", "ViewWithButtonRender");
242                         m.SetActiveView (v1);
243                         Assert.AreEqual (m.Render (), "<input type=\"submit\" name=\"test1\" value=\"\" id=\"test1\" />", "ChangeViewButtonRender");
244
245                 }
246
247                 [Test]
248                 public void MultiView_ControlState ()
249                 {
250                         PokerMultiView pmv = new PokerMultiView ();
251                         View v1 = new View ();
252                         View v2 = new View ();
253                         View v3 = new View ();
254                         pmv.AddViewCtrl (v1);
255                         pmv.AddViewCtrl (v2);
256                         pmv.AddViewCtrl (v3);
257                         pmv.SetActiveView (v1);
258                         Assert.AreEqual (v1, pmv.GetActiveView (), "BeforeLoadState");
259                         object state = pmv.SaveState ();
260                         pmv.SetActiveView (v2);
261                         Assert.AreEqual (1, pmv.ActiveViewIndex, "AftreSetActiveViewChanged");
262                         pmv.LoadState (state);
263                         Assert.AreEqual (0, pmv.ActiveViewIndex, "AftreLoadState");
264
265                 }
266
267
268                 // Events Stuff
269
270                 private bool OnActiveChanged;
271
272                 private void OnActiveViewChangedHandler (object sender, EventArgs e)
273                 {
274                         OnActiveChanged = true;
275                 }
276
277                 private void ResetEvents ()
278                 {
279                         OnActiveChanged = false;
280                 }
281
282                 [Test]
283                 public void MultiView_Events ()
284                 {
285                         PokerMultiView pmv = new PokerMultiView ();
286                         pmv.ActiveViewChanged += new EventHandler (OnActiveViewChangedHandler);
287                         Assert.AreEqual (false, OnActiveChanged, "OnActiveChanged");
288                         pmv.DoOnActiveViewChanged (new EventArgs ());
289                         Assert.AreEqual (true, OnActiveChanged, "AfterOnActiveChanged");
290
291                 }
292
293                 [Test]
294                 public void MultiView_OnBubbleEvent ()
295                 {
296                         Page myPage = new Page ();
297                         PokerMultiView pmv = new PokerMultiView ();
298                         View v1 = new View ();
299                         View v2 = new View ();
300                         pmv.Controls.Add (v1);
301                         pmv.Controls.Add (v2);
302                         pmv.ActiveViewIndex = 0;
303                         // Command NextView
304                         CommandEventArgs ceaNext = new CommandEventArgs ("NextView", null);
305                         pmv.DoBubbleEvent (this, ceaNext);
306                         Assert.AreEqual (1, pmv.ActiveViewIndex, "BubbleEventNext ");
307                         // Command PrevView
308                         CommandEventArgs ceaPrev = new CommandEventArgs ("PrevView", null);
309                         pmv.DoBubbleEvent (this, ceaPrev);
310                         Assert.AreEqual (0, pmv.ActiveViewIndex, "BubbleEventPrev");
311                         // Command SwitchViewByIndex
312                         CommandEventArgs ceaSwitch = new CommandEventArgs ("SwitchViewByIndex", "1");
313                         pmv.DoBubbleEvent (this, ceaSwitch);
314                         Assert.AreEqual (1, pmv.ActiveViewIndex, "BubbleSwitchViewByIndex");
315                         // Command SwitchViewByID
316                         v1.ID = "v1";
317                         myPage.Controls.Add (pmv);    // FindControl inherited from control & Page must exist
318                         CommandEventArgs ceaSwitchViewByID = new CommandEventArgs ("SwitchViewByID", "v1");
319                         pmv.DoBubbleEvent (this, ceaSwitchViewByID);
320                         Assert.AreEqual (0, pmv.ActiveViewIndex, "SwitchViewByID");
321                 }
322
323                 [Test]
324                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
325                 public void MultiView_IndexOutRange ()
326                 {
327                         PokerMultiView pmw = new PokerMultiView ();
328                         View pv1 = new View ();
329                         pmw.Controls.Add (pv1);
330                         pmw.SetActiveView (pv1);
331                         pmw.ActiveViewIndex = 7;
332
333                 }
334
335                 [Test]
336                 public void MultiView_AddParsedSubObjectExeption2 ()
337                 {
338                         PokerMultiView pmv = new PokerMultiView ();
339                         LiteralControl l1 = new LiteralControl ("literal");
340                         pmv.DoAddParsedSubObject (l1);
341                 }
342
343                 //PostBack Events
344                 [Test]
345                 [Category ("NunitWeb")]
346                 public void MultiView_Events_Base_PostBack ()
347                 {
348                         WebTest t = new WebTest ("NoEventValidation.aspx");
349                         t.Invoker = PageInvoker.CreateOnLoad (new PageDelegate (EventsTest));
350                         string html = t.Run ();
351                         if (html.IndexOf ("View_1_is_active") < 0)
352                                 Assert.Fail ("MultiView_Events#1 Failed");
353
354                         FormRequest fr = new FormRequest (t.Response, "form1");
355                         fr.Controls.Add ("bt");
356                         fr.Controls["bt"].Value = "Button";
357                         t.Request = fr;
358                         html = t.Run ();
359
360                         if (html.IndexOf ("ActiveViewChangedFired") < 0) {
361                                 Assert.Fail ("MultiView_Events#3 Failed");
362                         }
363
364                         if (html.IndexOf ("View_2_is_active") < 0)
365                                 Assert.Fail ("MultiView_Events#4 Failed");
366                 }
367
368                 #region base_events
369                 public static void EventsTest (Page p)
370                 {
371
372                         MultiView MultiView1 = new MultiView ();
373                         MultiView1.ID = "MultiView1";
374                         View view_1 = new View ();
375                         view_1.ID = "view_1";
376                         View view_2 = new View ();
377                         view_2.ID = "view_2";
378                         Button bt = new Button ();
379                         bt.ID = "bt";
380                         
381
382                         view_1.Controls.Add (bt);
383                         view_1.Controls.Add (new LiteralControl ("View_1_is_active"));
384                         view_2.Controls.Add (new LiteralControl ("View_2_is_active"));
385                         MultiView1.Views.Add (view_1);
386                         MultiView1.Views.Add (view_2);
387                         MultiView1.ActiveViewIndex = 0;
388                         MultiView1.ActiveViewChanged += new EventHandler (MultiView1_ActiveViewChanged);
389                         p.Controls.Add (MultiView1);
390
391                         if (p.IsPostBack) {
392                                 MultiView1.ActiveViewIndex = 1;
393                         }
394                 }
395                 #endregion
396
397                 [Test]
398                 [Category ("NunitWeb")]
399                 public void MultiView_Events_NextView_PostBack ()
400                 {
401                         WebTest t = new WebTest ("NoEventValidation.aspx");
402                         t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (EventsTest_1));
403                         string html = t.Run ();
404                         if (html.IndexOf ("View_1_is_active") < 0)
405                                 Assert.Fail ("MultiView_Events#1 Failed");
406
407                         FormRequest fr = new FormRequest (t.Response, "form1");
408                         fr.Controls.Add ("bt");
409                         fr.Controls["bt"].Value = "Button";
410                         t.Request = fr;
411                         html = t.Run ();
412
413                         if (html.IndexOf ("ActiveViewChangedFired") < 0) {
414                                 Assert.Fail ("MultiView_Events#3 Failed");
415                         }
416
417                         if (html.IndexOf ("View_2_is_active") < 0)
418                                 Assert.Fail ("MultiView_Events#4 Failed");
419                 }
420
421                 #region NextView_PostBack
422                 public static void EventsTest_1 (Page p)
423                 {
424                         MultiView MultiView1 = new MultiView ();
425                         MultiView1.ID = "MultiView1";
426                         View view_1 = new View ();
427                         view_1.ID = "view_1";
428                         View view_2 = new View ();
429                         view_2.ID = "view_2";
430                         Button bt = new Button ();
431                         bt.ID = "bt";
432                         bt.CommandName = "NextView";
433                         view_1.Controls.Add (bt);
434                         view_1.Controls.Add (new LiteralControl ("View_1_is_active"));
435                         view_2.Controls.Add (new LiteralControl ("View_2_is_active"));
436                         MultiView1.Views.Add (view_1);
437                         MultiView1.Views.Add (view_2);
438                         MultiView1.ActiveViewIndex = 0;
439                         MultiView1.ActiveViewChanged += new EventHandler (MultiView1_ActiveViewChanged);
440                         p.Controls.Add (MultiView1);
441                 }
442
443                 #endregion
444
445                 [Test]
446                 [Category ("NunitWeb")]
447                 public void MultiView_Events_PrevView_PostBack ()
448                 {
449                         WebTest t = new WebTest ("NoEventValidation.aspx");
450                         t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (EventsTest_2));
451                         string html = t.Run ();
452                         if (html.IndexOf ("View_2_is_active") < 0)
453                                 Assert.Fail ("MultiView_Events#1 Failed");
454
455                         FormRequest fr = new FormRequest (t.Response, "form1");
456                         fr.Controls.Add ("bt");
457                         fr.Controls["bt"].Value = "Button";
458                         t.Request = fr;
459                         html = t.Run ();
460
461                         if (html.IndexOf ("ActiveViewChangedFired") < 0) {
462                                 Assert.Fail ("MultiView_Events#3 Failed");
463                         }
464
465                         if (html.IndexOf ("View_1_is_active") < 0)
466                                 Assert.Fail ("MultiView_Events#4 Failed");
467                 }
468
469                 #region PrevView_PostBack
470                 public static void EventsTest_2 (Page p)
471                 {
472                         MultiView MultiView1 = new MultiView ();
473                         MultiView1.ID = "MultiView1";
474                         View view_1 = new View ();
475                         view_1.ID = "view_1";
476                         View view_2 = new View ();
477                         view_2.ID = "view_2";
478                         Button bt = new Button ();
479                         bt.ID = "bt";
480                         bt.CommandName = "PrevView";
481                         view_1.Controls.Add (bt);
482                         view_1.Controls.Add (new LiteralControl ("View_1_is_active"));
483                         view_2.Controls.Add (new LiteralControl ("View_2_is_active"));
484                         MultiView1.Views.Add (view_1);
485                         MultiView1.Views.Add (view_2);
486                         MultiView1.ActiveViewIndex = 1;
487                         MultiView1.ActiveViewChanged += new EventHandler (MultiView1_ActiveViewChanged);
488                         p.Controls.Add (MultiView1);
489                 }
490                 #endregion
491
492                 [Test]
493                 [Category ("NunitWeb")]
494                 public void MultiView_Events_SwitchViewByID_PostBack ()
495                 {
496                         WebTest t = new WebTest ("NoEventValidation.aspx");
497                         t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (EventsTest_3));
498                         string html = t.Run ();
499                         if (html.IndexOf ("View_1_is_active") < 0)
500                                 Assert.Fail ("MultiView_Events#1 Failed");
501
502                         FormRequest fr = new FormRequest (t.Response, "form1");
503                         fr.Controls.Add ("bt");
504                         fr.Controls["bt"].Value = "Button";
505                         t.Request = fr;
506                         html = t.Run ();
507
508                         if (html.IndexOf ("ActiveViewChangedFired") < 0) {
509                                 Assert.Fail ("MultiView_Events#3 Failed");
510                         }
511
512                         if (html.IndexOf ("View_2_is_active") < 0)
513                                 Assert.Fail ("MultiView_Events#4 Failed");
514                 }
515
516                 #region ByID_PostBack
517                 public static void EventsTest_3 (Page p)
518                 {
519                         MultiView MultiView1 = new MultiView ();
520                         MultiView1.ID = "MultiView1";
521                         View view_1 = new View ();
522                         view_1.ID = "view_1";
523                         View view_2 = new View ();
524                         view_2.ID = "view_2";
525                         Button bt = new Button ();
526                         bt.ID = "bt";
527                         bt.CommandName = "SwitchViewByID";
528                         bt.CommandArgument = "view_2";
529                         view_1.Controls.Add (bt);
530                         view_1.Controls.Add (new LiteralControl ("View_1_is_active"));
531                         view_2.Controls.Add (new LiteralControl ("View_2_is_active"));
532                         MultiView1.Views.Add (view_1);
533                         MultiView1.Views.Add (view_2);
534                         MultiView1.ActiveViewIndex = 0;
535                         MultiView1.ActiveViewChanged += new EventHandler (MultiView1_ActiveViewChanged);
536                         p.Controls.Add (MultiView1);
537                 }
538                 #endregion
539
540                 [Test]
541                 [Category ("NunitWeb")]
542                 public void MultiView_Events_SwitchViewByIndex_PostBack ()
543                 {
544                         WebTest t = new WebTest ("NoEventValidation.aspx");
545                         t.Invoker = PageInvoker.CreateOnInit (new PageDelegate (EventsTest_4));
546                         string html = t.Run ();
547                         if (html.IndexOf ("View_1_is_active") < 0)
548                                 Assert.Fail ("MultiView_Events#1 Failed");
549
550                         FormRequest fr = new FormRequest (t.Response, "form1");
551                         fr.Controls.Add ("bt");
552                         fr.Controls["bt"].Value = "Button";
553                         t.Request = fr;
554                         html = t.Run ();
555
556                         if (html.IndexOf ("ActiveViewChangedFired") < 0) {
557                                 Assert.Fail ("MultiView_Events#3 Failed");
558                         }
559
560                         if (html.IndexOf ("View_2_is_active") < 0)
561                                 Assert.Fail ("MultiView_Events#4 Failed");
562                 }
563
564                 #region SwitchViewByIndex_PostBack
565                 public static void EventsTest_4 (Page p)
566                 {
567                         MultiView MultiView1 = new MultiView ();
568                         MultiView1.ID = "MultiView1";
569                         View view_1 = new View ();
570                         view_1.ID = "view_1";
571                         View view_2 = new View ();
572                         view_2.ID = "view_2";
573                         Button bt = new Button ();
574                         bt.ID = "bt";
575                         bt.CommandName = "SwitchViewByIndex";
576                         bt.CommandArgument = "1";
577                         view_1.Controls.Add (bt);
578                         view_1.Controls.Add (new LiteralControl ("View_1_is_active"));
579                         view_2.Controls.Add (new LiteralControl ("View_2_is_active"));
580                         MultiView1.Views.Add (view_1);
581                         MultiView1.Views.Add (view_2);
582                         MultiView1.ActiveViewIndex = 0;
583                         MultiView1.ActiveViewChanged += new EventHandler (MultiView1_ActiveViewChanged);
584                         p.Controls.Add (MultiView1);
585                 }
586                 #endregion
587
588                 #region help_event_handler
589                 public static void MultiView1_ActiveViewChanged (object sender, EventArgs e)
590                 {
591                         MultiView mv = sender as MultiView;
592                         if (mv == null)
593                                 Assert.Fail ("MultiView_Events#2 Failed");
594                         mv.Page.Controls.Add (new LiteralControl ("ActiveViewChangedFired"));
595                 }
596                 #endregion
597
598                 [TestFixtureTearDown]
599                 public void TearDown ()
600                 {
601                         WebTest.Unload ();
602                 }
603         }
604 }