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