ThemeTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // Tests for System.Web.UI.WebControls.ThemeTest.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. // Additional resources :
  29. // PageWithStyleSheet.aspx; PageWithStyleSheet.aspx.cs;RunTimeSetTheme.aspx;
  30. // RunTimeSetTheme.aspx.cs; PageWithTheme.aspx; PageWithTheme.aspx.cs; Theme1.skin
  31. #if NET_2_0
  32. using System;
  33. using System.Drawing;
  34. using System.IO;
  35. using System.Configuration;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. using System.Threading;
  40. using MyWebControl = System.Web.UI.WebControls;
  41. using System.Reflection;
  42. using NUnit.Framework;
  43. using MonoTests.SystemWeb.Framework;
  44. namespace MonoTests.System.Web.UI.WebControls
  45. {
  46. [Serializable]
  47. [TestFixture]
  48. public class ThemeTest
  49. {
  50. [TestFixtureSetUp]
  51. public void Set_Up ()
  52. {
  53. #if VISUAL_STUDIO
  54. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.Theme1.skin", "App_Themes/Theme1/Theme1.skin");
  55. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.WizardTest.skin", "App_Themes/Theme1/WizardTest.skin");
  56. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.PageWithStyleSheet.aspx", "PageWithStyleSheet.aspx");
  57. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.PageWithTheme.aspx", "PageWithTheme.aspx");
  58. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.RunTimeSetTheme.aspx", "RunTimeSetTheme.aspx");
  59. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.UrlProperty.aspx", "UrlProperty.aspx");
  60. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.UrlProperty.ascx", "UrlProperty.ascx");
  61. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.UrlProperty.ascx.cs", "UrlProperty.ascx.cs");
  62. #else
  63. WebTest.CopyResource (GetType (), "Theme1.skin", "App_Themes/Theme1/Theme1.skin");
  64. WebTest.CopyResource (GetType (), "WizardTest.skin", "App_Themes/Theme1/WizardTest.skin");
  65. WebTest.CopyResource (GetType (), "PageWithStyleSheet.aspx", "PageWithStyleSheet.aspx");
  66. WebTest.CopyResource (GetType (), "PageWithTheme.aspx", "PageWithTheme.aspx");
  67. WebTest.CopyResource (GetType (), "RunTimeSetTheme.aspx", "RunTimeSetTheme.aspx");
  68. WebTest.CopyResource (GetType (), "UrlProperty.aspx", "UrlProperty.aspx");
  69. WebTest.CopyResource (GetType (), "UrlProperty.ascx", "UrlProperty.ascx");
  70. WebTest.CopyResource (GetType (), "UrlProperty.ascx.cs", "UrlProperty.ascx.cs");
  71. #endif
  72. }
  73. [SetUp]
  74. public void SetupTestCase ()
  75. {
  76. Thread.Sleep (100);
  77. }
  78. //Run on page with theme
  79. [Test]
  80. [Category ("NunitWeb")]
  81. public void Theme_TestLabelTheme ()
  82. {
  83. WebTest t = new WebTest ("PageWithTheme.aspx");
  84. t.Invoker = PageInvoker.CreateOnLoad (RenderLabelTest);
  85. t.Run ();
  86. }
  87. public static void RenderLabelTest (Page p)
  88. {
  89. Assert.AreEqual (Color.Black,((MyWebControl.Label) p.FindControl ("Label")).BackColor, "Default Theme#1");
  90. Assert.AreEqual (Color.Red, ((MyWebControl.Label) p.FindControl ("LabelRed")).BackColor, "Red Skin Theme#2");
  91. Assert.AreEqual (Color.Yellow, ((MyWebControl.Label) p.FindControl ("LabelYellow")).BackColor, "Yellow Skin Theme#3");
  92. Assert.AreEqual (Color.Black, ((MyWebControl.Label) p.FindControl ("LabelOverride")).BackColor, "Override Skin Theme#4");
  93. }
  94. [Test]
  95. [Category ("NunitWeb")]
  96. public void Theme_TestImageTheme ()
  97. {
  98. WebTest t = new WebTest ("PageWithTheme.aspx");
  99. t.Invoker = PageInvoker.CreateOnLoad (RenderImageTest);
  100. t.Run ();
  101. }
  102. public static void RenderImageTest (Page p)
  103. {
  104. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("Image")).ImageUrl.IndexOf ("myimageurl") >= 0, "Default Theme#1");
  105. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageRed")).ImageUrl.IndexOf ("myredimageurl") >= 0, "RedImage Theme#2");
  106. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageYellow")).ImageUrl.IndexOf ("myyellowimageurl") >= 0, "YellowImage Theme#3");
  107. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageOverride")).ImageUrl.IndexOf ("myimageurl") >= 0, "OverrideImage Theme#3");
  108. }
  109. // Run on page with StyleSheet
  110. [Test]
  111. [Category ("NunitWeb")]
  112. public void Theme_TestLabelStyleSheet ()
  113. {
  114. WebTest t = new WebTest ("PageWithStyleSheet.aspx");
  115. t.Invoker = PageInvoker.CreateOnLoad (StyleSheetRenderLabelTest);
  116. t.Run ();
  117. }
  118. public static void StyleSheetRenderLabelTest (Page p)
  119. {
  120. Assert.AreEqual (Color.Black, ((MyWebControl.Label) p.FindControl ("Label")).BackColor, "Default Theme#1");
  121. Assert.AreEqual (Color.Red, ((MyWebControl.Label) p.FindControl ("LabelRed")).BackColor, "Red Skin Theme#2");
  122. Assert.AreEqual (Color.Yellow, ((MyWebControl.Label) p.FindControl ("LabelYellow")).BackColor, "Yellow Skin Theme#3");
  123. Assert.AreEqual (Color.White, ((MyWebControl.Label) p.FindControl ("LabelOverride")).BackColor, "Override Skin Theme#4");
  124. }
  125. [Test]
  126. [Category ("NunitWeb")]
  127. public void Theme_TestImageStyleSheet ()
  128. {
  129. WebTest t = new WebTest ("PageWithStyleSheet.aspx");
  130. t.Invoker = PageInvoker.CreateOnLoad (StyleSheetRenderImageTest);
  131. t.Run ();
  132. }
  133. public static void StyleSheetRenderImageTest (Page p)
  134. {
  135. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("Image")).ImageUrl.IndexOf ("myimageurl") >= 0, "Default Theme#1");
  136. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageRed")).ImageUrl.IndexOf ("myredimageurl") >= 0, "RedImage Theme#2");
  137. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageYellow")).ImageUrl.IndexOf ("myyellowimageurl") >= 0, "YellowImage Theme#3");
  138. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageOverride")).ImageUrl.IndexOf ("overridedurl") >= 0, "OverrideImage Theme#3");
  139. }
  140. [Test]
  141. [Category ("NunitWeb")]
  142. public void Theme_TestRuntimeSetTheme ()
  143. {
  144. PageDelegates p = new PageDelegates ();
  145. p.PreInit = RuntimeSetThemePreInit;
  146. p.Load = RuntimeSetThemeLoad;
  147. WebTest t = new WebTest ("RunTimeSetTheme.aspx");
  148. t.Invoker = new PageInvoker (p);
  149. t.Run ();
  150. }
  151. public static void RuntimeSetThemePreInit (Page p)
  152. {
  153. p.Theme = "Theme1";
  154. }
  155. public static void RuntimeSetThemeLoad (Page p)
  156. {
  157. Assert.AreEqual (Color.Black, ((MyWebControl.Label) p.FindControl ("Label")).BackColor, "Default Theme#1");
  158. Assert.AreEqual (Color.Red, ((MyWebControl.Label) p.FindControl ("LabelRed")).BackColor, "Red Skin Theme#2");
  159. Assert.AreEqual (Color.Yellow, ((MyWebControl.Label) p.FindControl ("LabelYellow")).BackColor, "Yellow Skin Theme#3");
  160. Assert.AreEqual (Color.Black, ((MyWebControl.Label) p.FindControl ("LabelOverride")).BackColor, "Override Skin Theme#4");
  161. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("Image")).ImageUrl.IndexOf ("myimageurl") >= 0, "Default Theme#1");
  162. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageRed")).ImageUrl.IndexOf ("myredimageurl") >= 0, "RedImage Theme#2");
  163. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageYellow")).ImageUrl.IndexOf ("myyellowimageurl") >= 0, "YellowImage Theme#3");
  164. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageOverride")).ImageUrl.IndexOf ("myimageurl") >= 0, "OverrideImage Theme#3");
  165. }
  166. [Test]
  167. [Category ("NunitWeb")]
  168. public void Theme_TestThemeNotExistExeption()
  169. {
  170. string page = new WebTest (PageInvoker.CreateOnPreInit (_ThemeNotExistException)).Run ();
  171. Assert.IsTrue (page.IndexOf("System.Web.HttpException") >= 0, "System.Web.HttpException was expected, actual result: "+page);
  172. }
  173. public static void _ThemeNotExistException (Page p)
  174. {
  175. p.Theme = "NotExistTheme";
  176. }
  177. [Test]
  178. [Category ("NunitWeb")]
  179. public void Theme_SetThemeException ()
  180. {
  181. string page=new WebTest (PageInvoker.CreateOnPreInit (SetThemeExeption)).Run ();
  182. Assert.IsTrue (page.IndexOf("System.Web.HttpException") >= 0, "System.Web.HttpException was expected, actual result: "+page);
  183. }
  184. //// Delegate running on Page Load , only before PreInit possible set Theme on running time !
  185. //[Test]
  186. //[Category ("NunitWeb")]
  187. ////Use Assert.Fail to print the actual result
  188. ////[ExpectedException (typeof (InvalidOperationException))]
  189. //[Category ("NotWorking")]
  190. //public void Theme_SetThemeException ()
  191. //{
  192. // try {
  193. // string res=Helper.Instance.RunInPagePreInit (SetThemeExeption);
  194. // Assert.Fail ("InvalidOperationException was expected. Result: "+res);
  195. // }
  196. // catch (InvalidOperationException e) {
  197. // //swallow the expected exception
  198. // }
  199. //}
  200. public static void SetThemeExeption (Page p)
  201. {
  202. p.Theme = "InvalidTheme1";
  203. }
  204. [Test]
  205. [Category ("NunitWeb")]
  206. public void Theme_EnableTheming ()
  207. {
  208. PageDelegates pd = new PageDelegates ();
  209. pd.PreInit = new PageDelegate (SetTheme1);
  210. pd.Load = new PageDelegate (Theme1Load);
  211. PageInvoker pi = new PageInvoker (pd);
  212. string page = new WebTest (pi).Run ();
  213. Assert.IsTrue (page.IndexOf ("testing") < 0, "Theme_EnableTheming");
  214. }
  215. public static void Theme1Load (Page p)
  216. {
  217. Table t = new Table ();
  218. TableRow tr = new TableRow ();
  219. TableCell cell = new TableCell ();
  220. cell.Controls.Add (new Button ());
  221. tr.Cells.Add (cell);
  222. t.Rows.Add (tr);
  223. t.EnableTheming = false;
  224. p.Form.Controls.Add (t);
  225. }
  226. public static void SetTheme1 (Page p)
  227. {
  228. p.Theme = "Theme1";
  229. }
  230. [Test]
  231. [Category ("NunitWeb")]
  232. public void Theme_EnableThemingChild ()
  233. {
  234. PageDelegates pd = new PageDelegates ();
  235. pd.PreInit = new PageDelegate (SetTheme1);
  236. pd.Load = new PageDelegate (Theme1ChildLoad);
  237. PageInvoker pi = new PageInvoker (pd);
  238. string page = new WebTest (pi).Run ();
  239. Assert.IsTrue (page.IndexOf ("testing") > 0, "Theme_EnableThemingChild");
  240. }
  241. public static void Theme1ChildLoad (Page p)
  242. {
  243. Table t = new Table ();
  244. TableRow tr = new TableRow ();
  245. TableCell cell = new TableCell ();
  246. cell.Controls.Add (new Button ());
  247. tr.Cells.Add (cell);
  248. t.Rows.Add (tr);
  249. t.EnableTheming = false;
  250. cell.EnableTheming = true;
  251. p.Form.Controls.Add (t);
  252. }
  253. [TestFixtureTearDown]
  254. public void TearDown ()
  255. {
  256. Thread.Sleep (100);
  257. WebTest.Unload ();
  258. Thread.Sleep (100);
  259. }
  260. [Test]
  261. [Category("NunitWeb")]
  262. public void UrlPropertyTest ()
  263. {
  264. string res = new WebTest ("UrlProperty.aspx").Run ();
  265. Assert.IsTrue (res.IndexOf ("Property1 = testProp1") != -1,
  266. "Property1 should be assigned as is, actual result: "+res);
  267. Assert.IsTrue (res.IndexOf ("UrlProperty2 = ~/App_Themes/Theme1/testProp2") != -1,
  268. "UrlProperty2 should be assigned including theme subfolder, actual result: "+res);
  269. }
  270. }
  271. }
  272. #endif