ThemeTest.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 NunitWeb;
  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. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "Test1.Resources.Theme1.skin", "App_Themes/Theme1/Theme1.skin");
  55. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "Test1.Resources.PageWithStyleSheet.aspx", "PageWithStyleSheet.aspx");
  56. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "Test1.Resources.PageWithTheme.aspx", "PageWithTheme.aspx");
  57. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "Test1.Resources.RunTimeSetTheme.aspx", "RunTimeSetTheme.aspx");
  58. #else
  59. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "Theme1.skin", "App_Themes/Theme1/Theme1.skin");
  60. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "PageWithStyleSheet.aspx", "PageWithStyleSheet.aspx");
  61. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "PageWithTheme.aspx", "PageWithTheme.aspx");
  62. Helper.Instance.CopyResource (Assembly.GetExecutingAssembly (), "RunTimeSetTheme.aspx", "RunTimeSetTheme.aspx");
  63. #endif
  64. }
  65. [SetUp]
  66. public void SetupTestCase ()
  67. {
  68. Thread.Sleep (100);
  69. }
  70. //Run on page with theme
  71. [Test]
  72. [Category ("NunitWeb")]
  73. public void Theme_TestLabelTheme ()
  74. {
  75. Helper.Instance.RunUrl ("PageWithTheme.aspx", RenderLabelTest);
  76. }
  77. public static void RenderLabelTest (HttpContext c, Page p, object param)
  78. {
  79. Assert.AreEqual (Color.Black,((MyWebControl.Label) p.FindControl ("Label")).BackColor, "Default Theme#1");
  80. Assert.AreEqual (Color.Red, ((MyWebControl.Label) p.FindControl ("LabelRed")).BackColor, "Red Skin Theme#2");
  81. Assert.AreEqual (Color.Yellow, ((MyWebControl.Label) p.FindControl ("LabelYellow")).BackColor, "Yellow Skin Theme#3");
  82. Assert.AreEqual (Color.Black, ((MyWebControl.Label) p.FindControl ("LabelOverride")).BackColor, "Override Skin Theme#4");
  83. }
  84. [Test]
  85. [Category ("NunitWeb")]
  86. public void Theme_TestImageTheme ()
  87. {
  88. Helper.Instance.RunUrl ("PageWithTheme.aspx", RenderImageTest);
  89. }
  90. public static void RenderImageTest (HttpContext c, Page p, object param)
  91. {
  92. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("Image")).ImageUrl.IndexOf ("myimageurl") >= 0, "Default Theme#1");
  93. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageRed")).ImageUrl.IndexOf ("myredimageurl") >= 0, "RedImage Theme#2");
  94. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageYellow")).ImageUrl.IndexOf ("myyellowimageurl") >= 0, "YellowImage Theme#3");
  95. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageOverride")).ImageUrl.IndexOf ("myimageurl") >= 0, "OverrideImage Theme#3");
  96. }
  97. // Run on page with StyleSheet
  98. [Test]
  99. [Category ("NunitWeb")]
  100. public void Theme_TestLabelStyleSheet ()
  101. {
  102. Helper.Instance.RunUrl ("PageWithStyleSheet.aspx", StyleSheetRenderLabelTest);
  103. }
  104. public static void StyleSheetRenderLabelTest (HttpContext c, Page p, object param)
  105. {
  106. Assert.AreEqual (Color.Black, ((MyWebControl.Label) p.FindControl ("Label")).BackColor, "Default Theme#1");
  107. Assert.AreEqual (Color.Red, ((MyWebControl.Label) p.FindControl ("LabelRed")).BackColor, "Red Skin Theme#2");
  108. Assert.AreEqual (Color.Yellow, ((MyWebControl.Label) p.FindControl ("LabelYellow")).BackColor, "Yellow Skin Theme#3");
  109. Assert.AreEqual (Color.White, ((MyWebControl.Label) p.FindControl ("LabelOverride")).BackColor, "Override Skin Theme#4");
  110. }
  111. [Test]
  112. [Category ("NunitWeb")]
  113. public void Theme_TestImageStyleSheet ()
  114. {
  115. Helper.Instance.RunUrl ("PageWithStyleSheet.aspx", StyleSheetRenderImageTest);
  116. }
  117. public static void StyleSheetRenderImageTest (HttpContext c, Page p, object param)
  118. {
  119. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("Image")).ImageUrl.IndexOf ("myimageurl") >= 0, "Default Theme#1");
  120. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageRed")).ImageUrl.IndexOf ("myredimageurl") >= 0, "RedImage Theme#2");
  121. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageYellow")).ImageUrl.IndexOf ("myyellowimageurl") >= 0, "YellowImage Theme#3");
  122. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageOverride")).ImageUrl.IndexOf ("overridedurl") >= 0, "OverrideImage Theme#3");
  123. }
  124. [Test]
  125. [Category ("NunitWeb")]
  126. public void Theme_TestRuntimeSetTheme ()
  127. {
  128. PageDelegates p = new PageDelegates ();
  129. p.PreInit = RuntimeSetThemePreInit;
  130. p.Load = RuntimeSetThemeLoad;
  131. Helper.Instance.RunUrlDelegates ("RunTimeSetTheme.aspx", p);
  132. }
  133. public static void RuntimeSetThemePreInit (HttpContext c, Page p, object param)
  134. {
  135. p.Theme = "Theme1";
  136. }
  137. public static void RuntimeSetThemeLoad (HttpContext c, Page p, object param)
  138. {
  139. Assert.AreEqual (Color.Black, ((MyWebControl.Label) p.FindControl ("Label")).BackColor, "Default Theme#1");
  140. Assert.AreEqual (Color.Red, ((MyWebControl.Label) p.FindControl ("LabelRed")).BackColor, "Red Skin Theme#2");
  141. Assert.AreEqual (Color.Yellow, ((MyWebControl.Label) p.FindControl ("LabelYellow")).BackColor, "Yellow Skin Theme#3");
  142. Assert.AreEqual (Color.Black, ((MyWebControl.Label) p.FindControl ("LabelOverride")).BackColor, "Override Skin Theme#4");
  143. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("Image")).ImageUrl.IndexOf ("myimageurl") >= 0, "Default Theme#1");
  144. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageRed")).ImageUrl.IndexOf ("myredimageurl") >= 0, "RedImage Theme#2");
  145. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageYellow")).ImageUrl.IndexOf ("myyellowimageurl") >= 0, "YellowImage Theme#3");
  146. Assert.IsTrue (((MyWebControl.Image) p.FindControl ("ImageOverride")).ImageUrl.IndexOf ("myimageurl") >= 0, "OverrideImage Theme#3");
  147. }
  148. [Test]
  149. [Category ("NunitWeb")]
  150. public void Theme_TestThemeNotExistExeption()
  151. {
  152. string page = Helper.Instance.RunInPagePreInit (TestThemeNotExistException);
  153. Assert.IsTrue (page.IndexOf("System.Web.HttpException") >= 0, "System.Web.HttpException was expected");
  154. }
  155. public static void TestThemeNotExistException (HttpContext c, Page p, object param)
  156. {
  157. p.Theme = "NotExistTheme";
  158. }
  159. // Delegate running on Page Load , only before PreInit possible set Theme on running time !
  160. [Test]
  161. [Category ("NunitWeb")]
  162. //Use Assert.Fail to print the actual result
  163. //[ExpectedException (typeof (InvalidOperationException))]
  164. [Category ("NotWorking")]
  165. public void Theme_SetThemeException ()
  166. {
  167. try {
  168. string res=Helper.Instance.RunInPagePreInit (SetThemeExeption);
  169. Assert.Fail ("InvalidOperationException was expected. Result: "+res);
  170. }
  171. catch (InvalidOperationException e) {
  172. //swallow the expected exception
  173. }
  174. }
  175. public static void SetThemeExeption (HttpContext c, Page p, object param)
  176. {
  177. p.Theme = "InvalidTheme1";
  178. }
  179. [TestFixtureTearDown]
  180. public void TearDown ()
  181. {
  182. Thread.Sleep (100);
  183. Helper.Unload ();
  184. Thread.Sleep (100);
  185. }
  186. }
  187. }
  188. #endif