WizardStepBaseTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //
  2. // Tests for System.Web.UI.WebControls.WizardStepBaseTest.cs
  3. //
  4. // Author:
  5. // Yoni Klein ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. #if NET_2_0
  29. using System;
  30. using System.Web;
  31. using System.Web.UI;
  32. using System.Web.UI.WebControls;
  33. using NUnit.Framework;
  34. using System.IO;
  35. using MonoTests.stand_alone.WebHarness;
  36. using MonoTests.SystemWeb.Framework;
  37. using System.Collections;
  38. namespace MonoTests.System.Web.UI.WebControls
  39. {
  40. public class PokerWizardStepBase : WizardStepBase
  41. {
  42. // View state Stuff
  43. public PokerWizardStepBase ()
  44. : base ()
  45. {
  46. TrackViewState ();
  47. }
  48. public object SaveState ()
  49. {
  50. return SaveViewState ();
  51. }
  52. public void LoadState (object o)
  53. {
  54. LoadViewState (o);
  55. }
  56. public StateBag StateBag
  57. {
  58. get { return base.ViewState; }
  59. }
  60. public void DoOnLoad (EventArgs e)
  61. {
  62. base.OnLoad (e);
  63. }
  64. public string RenderChildren ()
  65. {
  66. StringWriter sw = new StringWriter ();
  67. sw.NewLine = "\n";
  68. HtmlTextWriter writer = new HtmlTextWriter (sw);
  69. base.RenderChildren (writer);
  70. return writer.InnerWriter.ToString ();
  71. }
  72. }
  73. [TestFixture]
  74. public class WizardStepBaseTest
  75. {
  76. private bool event_checker;
  77. [TestFixtureTearDown]
  78. public void TearDown ()
  79. {
  80. WebTest.Unload ();
  81. }
  82. [Test]
  83. public void WizardStepBase_DefaultProperty ()
  84. {
  85. PokerWizardStepBase step = new PokerWizardStepBase ();
  86. Assert.AreEqual (true, step.AllowReturn, "AllowReturn");
  87. Assert.AreEqual (true, step.EnableTheming, "EnableTheming");
  88. Assert.AreEqual (null, step.ID, "ID");
  89. Assert.AreEqual (WizardStepType.Auto, step.StepType, "StepType");
  90. Assert.AreEqual ("", step.Title, "Title");
  91. Assert.AreEqual (null, step.Wizard, "Wizard");
  92. }
  93. [Test]
  94. public void WizardStepBase_DefaultPropertyNotWorking ()
  95. {
  96. PokerWizardStepBase step = new PokerWizardStepBase ();
  97. Assert.AreEqual (null, step.Name, "Name");
  98. }
  99. [Test]
  100. public void WizardStepBase_AssignProperty ()
  101. {
  102. PokerWizardStepBase step = new PokerWizardStepBase ();
  103. Wizard w = new Wizard ();
  104. Assert.AreEqual (0, step.StateBag.Count, "ViewState.Count");
  105. w.WizardSteps.Add (step);
  106. Assert.AreEqual (w, step.Wizard, "Wizard");
  107. step.EnableTheming = false;
  108. Assert.AreEqual (false, step.EnableTheming, "EnableTheming");
  109. step.ID = "test";
  110. Assert.AreEqual ("test", step.ID, "ID");
  111. step.Title = "test";
  112. Assert.AreEqual ("test", step.Title, "Title");
  113. step.AllowReturn = false;
  114. Assert.AreEqual (false, step.AllowReturn, "AllowReturn");
  115. step.StepType = WizardStepType.Complete;
  116. Assert.AreEqual (WizardStepType.Complete, step.StepType, "StepType");
  117. }
  118. [Test]
  119. public void WizardStepBase_StateBag ()
  120. {
  121. PokerWizardStepBase step = new PokerWizardStepBase ();
  122. Wizard w = new Wizard ();
  123. step.StepType = WizardStepType.Complete;
  124. Assert.AreEqual (WizardStepType.Complete, step.StepType, "StepType");
  125. Assert.AreEqual (1, step.StateBag.Count, "StepTypeStateBag");
  126. step.AllowReturn = false;
  127. Assert.AreEqual (false, step.AllowReturn, "AllowReturn");
  128. Assert.AreEqual (2, step.StateBag.Count, "AllowReturnStateBag");
  129. step.Title = "test";
  130. Assert.AreEqual ("test", step.Title, "Title");
  131. Assert.AreEqual (3, step.StateBag.Count, "Title");
  132. }
  133. [Test]
  134. public void WizardStepBase_LoadViewState ()
  135. {
  136. PokerWizardStepBase step = new PokerWizardStepBase ();
  137. PokerWizardStepBase copy = new PokerWizardStepBase ();
  138. step.AllowReturn = false;
  139. Assert.AreEqual (false, step.AllowReturn, "AllowReturn");
  140. Assert.AreEqual (1, step.StateBag.Count, "AllowReturnStateBag");
  141. step.StepType = WizardStepType.Complete;
  142. Assert.AreEqual (WizardStepType.Complete, step.StepType, "StepType");
  143. Assert.AreEqual (2, step.StateBag.Count, "StepTypeStateBag");
  144. object state = step.SaveState ();
  145. copy.LoadState (state);
  146. Assert.AreEqual (false, copy.AllowReturn, "AllowReturn");
  147. Assert.AreEqual (WizardStepType.Complete, copy.StepType, "StepType");
  148. }
  149. [Test]
  150. public void WizardStepBase_LoadEvent ()
  151. {
  152. Wizard w = new Wizard ();
  153. PokerWizardStepBase step = new PokerWizardStepBase ();
  154. w.WizardSteps.Add (step);
  155. step.Load += new EventHandler (eventchecker);
  156. step.DoOnLoad (new EventArgs ());
  157. eventassert ("OnLoadEvent");
  158. }
  159. [Test]
  160. public void WizardStepBase_RenderChildren ()
  161. {
  162. Wizard w = new Wizard ();
  163. PokerWizardStepBase step = new PokerWizardStepBase ();
  164. LiteralControl lc = new LiteralControl ("test");
  165. step.Controls.Add (lc);
  166. w.WizardSteps.Add (step);
  167. Assert.AreEqual ("test", step.RenderChildren (), "RenderChildren");
  168. }
  169. [Test]
  170. public void WizardStepBase_ID ()
  171. {
  172. Wizard w = new Wizard ();
  173. PokerWizardStepBase step = new PokerWizardStepBase ();
  174. step.ID = "step1";
  175. w.WizardSteps.Add (step);
  176. Assert.AreEqual (step, w.FindControl ("step1"), "Step with ID fail");
  177. }
  178. [Test]
  179. [Category ("NunitWeb")]
  180. public void WizardStepBase_RenderTest ()
  181. {
  182. // This render test include Title property test
  183. string html = new WebTest (PageInvoker.CreateOnLoad (
  184. new PageDelegate (Render_Test))).Run ();
  185. #if NET_4_0
  186. string origin = "<table cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse:collapse;\">\r\n\t<tr>\r\n\t\t<td style=\"height:100%;\"><a href=\"#ctl01_SkipLink\"><img alt=\"Skip Navigation Links.\" height=\"0\" width=\"0\" src=\"/NunitWeb/WebResource.axd?d=8VpphgAbakKUC_J8R6hR0Q2&amp;t=634067491135766272\" style=\"border-width:0px;\" /></a><table id=\"ctl01_SideBarContainer_SideBarList\" cellspacing=\"0\" style=\"border-collapse:collapse;\">\r\n\t\t\t<tr>\r\n\t\t\t\t<td style=\"font-weight:bold;\"><a id=\"ctl01_SideBarContainer_SideBarList_SideBarButton_0\" href=\"javascript:__doPostBack(&#39;ctl01$SideBarContainer$SideBarList$ctl00$SideBarButton&#39;,&#39;&#39;)\">my_title</a></td>\r\n\t\t\t</tr><tr>\r\n\t\t\t\t<td><a id=\"ctl01_SideBarContainer_SideBarList_SideBarButton_1\" href=\"javascript:__doPostBack(&#39;ctl01$SideBarContainer$SideBarList$ctl01$SideBarButton&#39;,&#39;&#39;)\">my_title_2</a></td>\r\n\t\t\t</tr>\r\n\t\t</table><a id=\"ctl01_SkipLink\"></a></td><td style=\"height:100%;\"><table cellspacing=\"0\" cellpadding=\"0\" style=\"height:100%;width:100%;border-collapse:collapse;\">\r\n\t\t\t<tr style=\"height:100%;\">\r\n\t\t\t\t<td>123</td>\r\n\t\t\t</tr><tr>\r\n\t\t\t\t<td align=\"right\"><table cellspacing=\"5\" cellpadding=\"5\">\r\n\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t<td align=\"right\"><input type=\"submit\" name=\"ctl01$StartNavigationTemplateContainerID$StartNextButton\" value=\"Next\" id=\"ctl01_StartNavigationTemplateContainerID_StartNextButton\" /></td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t</table></td>\r\n\t\t\t</tr>\r\n\t\t</table></td>\r\n\t</tr>\r\n</table>";
  187. #else
  188. string origin = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"border-collapse:collapse;\">\r\n\t<tr>\r\n\t\t<td style=\"height:100%;\"><a href=\"#ctl01_SkipLink\"><img alt=\"Skip Navigation Links.\" height=\"0\" width=\"0\" src=\"/NunitWeb/WebResource.axd?d=4RHYfeNnynkXiM59uthjZg2&amp;t=633802729995006876\" style=\"border-width:0px;\" /></a><table id=\"ctl01_SideBarContainer_SideBarList\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;\">\r\n\t\t\t<tr>\r\n\t\t\t\t<td style=\"font-weight:bold;\"><a id=\"ctl01_SideBarContainer_SideBarList_ctl00_SideBarButton\" href=\"javascript:__doPostBack('ctl01$SideBarContainer$SideBarList$ctl00$SideBarButton','')\">my_title</a></td>\r\n\t\t\t</tr><tr>\r\n\t\t\t\t<td><a id=\"ctl01_SideBarContainer_SideBarList_ctl01_SideBarButton\" href=\"javascript:__doPostBack('ctl01$SideBarContainer$SideBarList$ctl01$SideBarButton','')\">my_title_2</a></td>\r\n\t\t\t</tr>\r\n\t\t</table><a id=\"ctl01_SkipLink\"></a></td><td style=\"height:100%;\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"height:100%;width:100%;border-collapse:collapse;\">\r\n\t\t\t<tr style=\"height:100%;\">\r\n\t\t\t\t<td>123</td>\r\n\t\t\t</tr><tr>\r\n\t\t\t\t<td align=\"right\"><table cellspacing=\"5\" cellpadding=\"5\" border=\"0\">\r\n\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t<td align=\"right\"><input type=\"submit\" name=\"ctl01$StartNavigationTemplateContainerID$StartNextButton\" value=\"Next\" id=\"ctl01_StartNavigationTemplateContainerID_StartNextButton\" /></td>\r\n\t\t\t\t\t</tr>\r\n\t\t\t\t</table></td>\r\n\t\t\t</tr>\r\n\t\t</table></td>\r\n\t</tr>\r\n</table>";
  189. #endif
  190. string renderedHtml = HtmlDiff.GetControlFromPageHtml (html);
  191. HtmlDiff.AssertAreEqual (origin, renderedHtml, "BaseRender");
  192. if (html.IndexOf ("my_title") < 0)
  193. Assert.Fail ("WizardStepBase title not rendered");
  194. }
  195. public static void Render_Test (Page p)
  196. {
  197. LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
  198. LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
  199. Wizard w = new Wizard ();
  200. PokerWizardStepBase ws = new PokerWizardStepBase ();
  201. ws.Title = "my_title";
  202. ws.Controls.Add (new LiteralControl ("123"));
  203. ws.StepType = WizardStepType.Start;
  204. PokerWizardStepBase ws2 = new PokerWizardStepBase ();
  205. ws2.Title = "my_title_2";
  206. ws2.Controls.Add (new LiteralControl ("1234567"));
  207. ws2.StepType = WizardStepType.Finish;
  208. w.DisplaySideBar = true;
  209. w.WizardSteps.Add (ws);
  210. w.WizardSteps.Add (ws2);
  211. p.Form.Controls.Add (lcb);
  212. p.Form.Controls.Add (w);
  213. p.Form.Controls.Add (lce);
  214. }
  215. [Test]
  216. [Category ("NunitWeb")]
  217. public void WizardStepBase_PostBackAllowReturnTest ()
  218. {
  219. // This test examine the rendering 2 steps and make postbake
  220. // assigned AllowReturn property
  221. WebTest t = new WebTest ();
  222. PageDelegates pd = new PageDelegates ();
  223. pd.PreInit = _postback;
  224. pd.PreRenderComplete = read_control;
  225. t.Invoker = new PageInvoker (pd);
  226. string result = t.Run ();
  227. if (result.IndexOf ("Start") < 0)
  228. Assert.Fail ("Rendering fault");
  229. ArrayList list = t.UserData as ArrayList;
  230. Assert.IsNotNull (list, "PostBackDataNotCreated");
  231. FormRequest fr = new FormRequest (t.Response, "form1");
  232. fr.Controls.Add ("__EVENTTARGET");
  233. fr.Controls.Add ("__EVENTARGUMENT");
  234. fr.Controls["__EVENTTARGET"].Value = list[1].ToString ();
  235. fr.Controls["__EVENTARGUMENT"].Value = "";
  236. t.Request = fr;
  237. result = t.Run ();
  238. if (result.IndexOf ("StepType") < 0)
  239. Assert.Fail ("MovedToStep1");
  240. if (result.IndexOf ("Previous") > 0) {
  241. Assert.Fail ("Previous button rendered");
  242. }
  243. }
  244. public static void _postback (Page p)
  245. {
  246. p.EnableEventValidation = false;
  247. Wizard w = new Wizard ();
  248. w.ID = "Wizard";
  249. PokerWizardStepBase ws = new PokerWizardStepBase ();
  250. ws.ID = "step";
  251. ws.StepType = WizardStepType.Start;
  252. ws.Controls.Add (new LiteralControl ("StartType"));
  253. ws.AllowReturn = false;
  254. PokerWizardStepBase ws1 = new PokerWizardStepBase ();
  255. ws1.ID = "step1";
  256. ws1.StepType = WizardStepType.Step;
  257. ws1.Controls.Add (new LiteralControl ("StepType"));
  258. w.DisplaySideBar = true;
  259. w.WizardSteps.Add (ws);
  260. w.WizardSteps.Add (ws1);
  261. p.Controls.Add (w);
  262. }
  263. [Test]
  264. [Category ("NunitWeb")]
  265. public void WizardStepBase_Theme ()
  266. {
  267. WebTest.CopyResource (GetType (), "WizardTest.skin", "App_Themes/Theme1/WizardTest.skin");
  268. WebTest t = new WebTest ();
  269. PageDelegates pd = new PageDelegates ();
  270. pd.PreInit = set_properties;
  271. pd.Load = theme;
  272. t.Invoker = new PageInvoker (pd);
  273. string html = t.Run ();
  274. if (html.IndexOf ("testing") < 0) {
  275. Assert.Fail ("WizardStepBase themes not applyed when EnableTheming = true");
  276. }
  277. pd.Load = notheme;
  278. t.Invoker = new PageInvoker (pd);
  279. html = t.Run ();
  280. if (html.IndexOf ("testing") > 0) {
  281. Assert.Fail ("WizardStepBase themes applyed when EnableTheming = false");
  282. }
  283. }
  284. public static void set_properties (Page p)
  285. {
  286. p.Theme = "Theme1";
  287. }
  288. public static void theme (Page p)
  289. {
  290. Wizard w = new Wizard ();
  291. PokerWizardStepBase ws = new PokerWizardStepBase ();
  292. ws.Controls.Add (new Button ());
  293. ws.EnableTheming = true;
  294. ws.SkinID = "WizardTest";
  295. w.WizardSteps.Add (ws);
  296. p.Form.Controls.Add (w);
  297. }
  298. public static void notheme (Page p)
  299. {
  300. Wizard w = new Wizard ();
  301. PokerWizardStepBase ws = new PokerWizardStepBase ();
  302. ws.Controls.Add (new Button ());
  303. ws.EnableTheming = false;
  304. ws.SkinID = "WizardTest";
  305. w.WizardSteps.Add (ws);
  306. p.Form.Controls.Add (w);
  307. }
  308. private void eventchecker (object o, EventArgs e)
  309. {
  310. event_checker = true;
  311. }
  312. private void eventassert (string message)
  313. {
  314. Assert.IsTrue (event_checker, message);
  315. event_checker = false;
  316. }
  317. public static void read_control (Page p)
  318. {
  319. ArrayList list = new ArrayList ();
  320. recurcive_find (list, typeof (LinkButton), p.FindControl ("Wizard"));
  321. WebTest.CurrentTest.UserData = list;
  322. }
  323. public static void recurcive_find (ArrayList list, Type t, Control control)
  324. {
  325. foreach (Control c in control.Controls) {
  326. if (c == null)
  327. continue;
  328. if (t == c.GetType ()) {
  329. list.Add (c.UniqueID);
  330. }
  331. recurcive_find (list, t, c);
  332. }
  333. }
  334. }
  335. }
  336. #endif