2 // Tests for System.Web.UI.WebControls.View.cs
5 // Yoni Klein (yonik@mainsoft.com)
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
33 using System.Globalization;
36 using System.Web.UI.WebControls;
37 using MonoTests.SystemWeb.Framework;
38 using MonoTests.stand_alone.WebHarness;
41 namespace MonoTests.System.Web.UI.WebControls
45 class PokerMultiView : MultiView
47 public PokerMultiView ()
52 public object SaveState ()
54 return SaveControlState ();
57 public void LoadState (object o)
62 public StateBag StateBag
64 get { return base.ViewState; }
67 public string Render ()
69 StringWriter sw = new StringWriter ();
70 HtmlTextWriter tw = new HtmlTextWriter (sw);
72 return sw.ToString ();
75 public void DoAddParsedSubObject (object e)
77 AddParsedSubObject (e);
80 public void DoOnActiveViewChanged (EventArgs e)
82 base.OnActiveViewChanged (e);
85 public void DoBubbleEvent (object source, EventArgs e)
87 OnBubbleEvent (source, e);
90 public void AddViewCtrl (View v)
92 this.Controls.Add (v);
97 public class MultiViewTest
103 WebTest.CopyResource (GetType (), "NoEventValidation.aspx", "NoEventValidation.aspx");
107 public void MultiView_DefaultProperties ()
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");
120 public void MultiView_NotWorkingDefaultProperties ()
122 PokerMultiView pmw = new PokerMultiView ();
123 Assert.IsTrue (pmw.EnableTheming, "EnableTheming");
128 public void MultiView_AddViews ()
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");
140 public void MultiView_ActiveIndex ()
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");
155 public void MultiView_SetActiveView ()
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");
167 public void MultiView_RemoveViewControlEvent ()
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");
187 public void MultiView_AddParsedSubObject ()
189 PokerMultiView pmv = new PokerMultiView ();
190 View v1 = new View ();
191 pmv.DoAddParsedSubObject (v1);
192 Assert.AreEqual (1, pmv.Controls.Count, "AddParsedSubObjectSuccssed");
197 public void MultiView_CreateControlCollection ()
199 PokerMultiView pmv = new PokerMultiView ();
200 Assert.IsNotNull (pmv.Views, "CreatingViewCollection");
205 public void MultiView_Render ()
207 PokerMultiView b = new PokerMultiView ();
208 string html = b.Render ();
209 Assert.AreEqual (b.Render (), string.Empty, "DefaultRender");
213 public void MultiView_ButtonRender ()
215 PokerMultiView m = new PokerMultiView ();
216 PokerView v = new PokerView ();
217 Button b = new Button ();
222 string html = m.Render ();
223 Assert.AreEqual ("<input type=\"submit\" name=\"test\" value=\"\" id=\"test\" />", html, "ButtonRender");
227 public void MultiView_SomeViewsButtonRender ()
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 ();
237 v1.Controls.Add (b1);
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");
248 public void MultiView_ControlState ()
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");
270 private bool OnActiveChanged;
272 private void OnActiveViewChangedHandler (object sender, EventArgs e)
274 OnActiveChanged = true;
277 private void ResetEvents ()
279 OnActiveChanged = false;
283 public void MultiView_Events ()
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");
294 public void MultiView_OnBubbleEvent ()
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;
304 CommandEventArgs ceaNext = new CommandEventArgs ("NextView", null);
305 pmv.DoBubbleEvent (this, ceaNext);
306 Assert.AreEqual (1, pmv.ActiveViewIndex, "BubbleEventNext ");
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
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");
324 [ExpectedException (typeof (ArgumentOutOfRangeException))]
325 public void MultiView_IndexOutRange ()
327 PokerMultiView pmw = new PokerMultiView ();
328 View pv1 = new View ();
329 pmw.Controls.Add (pv1);
330 pmw.SetActiveView (pv1);
331 pmw.ActiveViewIndex = 7;
336 public void MultiView_AddParsedSubObjectExeption2 ()
338 PokerMultiView pmv = new PokerMultiView ();
339 LiteralControl l1 = new LiteralControl ("literal");
340 pmv.DoAddParsedSubObject (l1);
345 [Category ("NunitWeb")]
346 public void MultiView_Events_Base_PostBack ()
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");
354 FormRequest fr = new FormRequest (t.Response, "form1");
355 fr.Controls.Add ("bt");
356 fr.Controls["bt"].Value = "Button";
360 if (html.IndexOf ("ActiveViewChangedFired") < 0) {
361 Assert.Fail ("MultiView_Events#3 Failed");
364 if (html.IndexOf ("View_2_is_active") < 0)
365 Assert.Fail ("MultiView_Events#4 Failed");
369 public static void EventsTest (Page p)
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 ();
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);
392 MultiView1.ActiveViewIndex = 1;
398 [Category ("NunitWeb")]
399 public void MultiView_Events_NextView_PostBack ()
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");
407 FormRequest fr = new FormRequest (t.Response, "form1");
408 fr.Controls.Add ("bt");
409 fr.Controls["bt"].Value = "Button";
413 if (html.IndexOf ("ActiveViewChangedFired") < 0) {
414 Assert.Fail ("MultiView_Events#3 Failed");
417 if (html.IndexOf ("View_2_is_active") < 0)
418 Assert.Fail ("MultiView_Events#4 Failed");
421 #region NextView_PostBack
422 public static void EventsTest_1 (Page p)
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 ();
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);
446 [Category ("NunitWeb")]
447 public void MultiView_Events_PrevView_PostBack ()
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");
455 FormRequest fr = new FormRequest (t.Response, "form1");
456 fr.Controls.Add ("bt");
457 fr.Controls["bt"].Value = "Button";
461 if (html.IndexOf ("ActiveViewChangedFired") < 0) {
462 Assert.Fail ("MultiView_Events#3 Failed");
465 if (html.IndexOf ("View_1_is_active") < 0)
466 Assert.Fail ("MultiView_Events#4 Failed");
469 #region PrevView_PostBack
470 public static void EventsTest_2 (Page p)
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 ();
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);
493 [Category ("NunitWeb")]
494 public void MultiView_Events_SwitchViewByID_PostBack ()
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");
502 FormRequest fr = new FormRequest (t.Response, "form1");
503 fr.Controls.Add ("bt");
504 fr.Controls["bt"].Value = "Button";
508 if (html.IndexOf ("ActiveViewChangedFired") < 0) {
509 Assert.Fail ("MultiView_Events#3 Failed");
512 if (html.IndexOf ("View_2_is_active") < 0)
513 Assert.Fail ("MultiView_Events#4 Failed");
516 #region ByID_PostBack
517 public static void EventsTest_3 (Page p)
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 ();
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);
541 [Category ("NunitWeb")]
542 public void MultiView_Events_SwitchViewByIndex_PostBack ()
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");
550 FormRequest fr = new FormRequest (t.Response, "form1");
551 fr.Controls.Add ("bt");
552 fr.Controls["bt"].Value = "Button";
556 if (html.IndexOf ("ActiveViewChangedFired") < 0) {
557 Assert.Fail ("MultiView_Events#3 Failed");
560 if (html.IndexOf ("View_2_is_active") < 0)
561 Assert.Fail ("MultiView_Events#4 Failed");
564 #region SwitchViewByIndex_PostBack
565 public static void EventsTest_4 (Page p)
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 ();
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);
588 #region help_event_handler
589 public static void MultiView1_ActiveViewChanged (object sender, EventArgs e)
591 MultiView mv = sender as MultiView;
593 Assert.Fail ("MultiView_Events#2 Failed");
594 mv.Page.Controls.Add (new LiteralControl ("ActiveViewChangedFired"));
598 [TestFixtureTearDown]
599 public void TearDown ()