bb97516436660db62002bfb0b36ee5209277622e
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / MasterPageTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.MasterPageTest.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
30
31 using NUnit.Framework;
32 using System;
33 using System.Collections.Generic;
34 using System.Text;
35 using System.Web;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38 using System.IO;
39 using System.Drawing;
40 using MyWebControl = System.Web.UI.WebControls;
41 using System.Collections;
42 using MonoTests.SystemWeb.Framework;
43 using MonoTests.stand_alone.WebHarness;
44 using System.Threading;
45 using MonoTests.Common;
46
47 namespace MonoTests.System.Web.UI.WebControls
48 {
49         public class PokerMasterPage : MasterPage
50         {
51                 public PokerMasterPage ()
52                 {
53                         TrackViewState ();
54                 }
55                 public StateBag StateBag
56                 {
57                         get { return base.ViewState; }
58                 }
59                 public new IDictionary ContentTemplates ()
60                 {
61                         return base.ContentTemplates;
62                 }
63                 public new void AddContentTemplate (string templateName, ITemplate template)
64                 {
65                         base.AddContentTemplate (templateName, template);
66                 }
67                 public string MasterMethod ()
68                 {
69                         return "FromMasterMethod";
70                 }
71         }
72
73         [TestFixture]
74         public class MasterPageTest
75         {
76 #if NET_4_0
77                 class MyTemplate : ITemplate
78                 {
79                         public const string MyText = "|MyTemplate.InstantiateIn called|";
80
81                         public void InstantiateIn (Control container)
82                         {
83                                 container.Controls.Add (new LiteralControl (MyText));
84                         }
85                 }
86
87                 class MyContentTemplate : Content, ITemplate
88                 {
89                         public const string MyText = "|MyContentTemplate.InstantiateIn called|";
90
91                         public void InstantiateIn (Control container)
92                         {
93                                 container.Controls.Add (new LiteralControl (MyText));
94                         }
95                 }
96 #endif
97                 [TestFixtureSetUp]
98                 public void CopyTestResources ()
99                 {
100                         WebTest.CopyResource (GetType (), "MasterTypeTest1.aspx", "MasterTypeTest1.aspx");
101                         WebTest.CopyResource (GetType (), "MasterTypeTest2.aspx", "MasterTypeTest2.aspx");
102                         WebTest.CopyResource (GetType (), "MyDerived.master", "MyDerived.master");
103                         WebTest.CopyResource (GetType (), "MyPageWithDerivedMaster.aspx", "MyPageWithDerivedMaster.aspx");
104                 }
105
106                 [SetUp]
107                 public void SetupTestCase ()
108                 {
109                         Thread.Sleep (100);
110                 }
111
112                 [Test]
113                 public void MasterPage_DefaultProperties ()
114                 {
115                         PokerMasterPage pmp = new PokerMasterPage ();
116                         Assert.AreEqual (null, pmp.Master, "Master Property");
117                         Assert.AreEqual (null, pmp.MasterPageFile, "MasterPageFile Property");
118                 }
119
120                 [Test]
121                 [Category ("NotWorking")]
122                 public void MasterPage_DefaultPropertiesNotWorking ()
123                 {
124                         PokerMasterPage pmp = new PokerMasterPage ();
125                         IDictionary i = pmp.ContentTemplates ();
126                         Assert.AreEqual (null, i, "ContentTemplates");
127                 }
128
129                 [Test]
130                 [Category ("NunitWeb")]
131                 public void MasterPage_Render ()
132                 {
133                         Render_Helper (StandardUrl.PAGE_WITH_MASTER);
134                 }
135
136
137                 [Test]
138                 [Category ("NunitWeb")]
139                 public void MasterPageDerived_Render ()
140                 {
141                         Render_Helper (StandardUrl.PAGE_WITH_DERIVED_MASTER);
142                 }
143
144                 // Bug #325114
145                 [Test]
146                 [Category ("NunitWeb")]
147                 [ExpectedException (typeof(HttpException))]
148                 public void MasterPage_ContentPlaceHolder_Not_Found ()
149                 {
150                         Render_Helper (StandardUrl.PAGE_WITH_MASTER_INVALID_PLACE_HOLDER);
151                 }
152                 
153                 public void Render_Helper(string url)
154                 {
155                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (_RenderDefault));
156                         t.Request.Url = url;
157                         string PageRenderHtml = t.Run ();
158                         
159                         
160                         if (PageRenderHtml.IndexOf ("Page main text") < 0) {
161                                 Assert.Fail ("Master#2");
162                         }
163                         
164                         Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master main text"), "Master#3");
165                         
166
167                         if (PageRenderHtml.IndexOf ("Page dynamic text") < 0) {
168                                 Assert.Fail ("Master#5");
169                         }
170
171                         if (PageRenderHtml.IndexOf ("My master page footer") < 0) {
172                                 Assert.Fail ("Master#6, result: "+PageRenderHtml);
173                         }
174
175                         if (PageRenderHtml.IndexOf ("Master page content text") < 0) {
176                                 Assert.Fail ("Master#7");
177                         }
178
179                         if (url == StandardUrl.PAGE_WITH_DERIVED_MASTER) {
180                                 if (PageRenderHtml.IndexOf ("Derived header text") < 0) {
181                                         Assert.Fail ("Master#8");
182                                 }
183
184                                 if (PageRenderHtml.IndexOf ("Derived master page text ") < 0) {
185                                         Assert.Fail ("Master#9");
186                                 }
187
188                                 if (PageRenderHtml.IndexOf ("Master header text") < 0) {
189                                         Assert.Fail ("Master#10");
190                                 }
191                         }
192                         else {
193                                 Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master header text"), "Master#1");
194                                 Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master dynamic text"), "Master#4");
195                         }
196                 }
197
198                 [Test]
199                 [Category ("NunitWeb")]
200                 [Category ("NotWorking")]
201                 public void MasterType_VirtualPath ()
202                 {
203                         WebTest t = new WebTest ("MasterTypeTest1.aspx");
204                         string PageRenderHtml = t.Run ();
205                         if (PageRenderHtml.IndexOf ("MasterTypeMethod") < 0)
206                                 Assert.Fail ("MasterType VirtualPath test failed");
207                 }
208
209                 [Test]
210                 [Category ("NunitWeb")]
211                 public void MasterType_TypeName ()
212                 {
213                         WebTest t = new WebTest ("MasterTypeTest2.aspx");
214                         string PageRenderHtml = t.Run ();
215                         if (PageRenderHtml.IndexOf ("FromMasterMethod") < 0)
216                                 Assert.Fail ("MasterType TypeName test failed");
217                 }
218 #if NET_4_0
219                 [Test]
220                 public void InstantiateInContentPlaceHolder ()
221                 {
222                         var mp = new MasterPage ();
223                         ITemplate template = new MyTemplate ();
224
225                         AssertExtensions.Throws<NullReferenceException> (() => {
226                                 mp.InstantiateInContentPlaceHolder (null, template);
227                         }, "#A1-1");
228
229                         Control container = new Control ();
230                         AssertExtensions.Throws<NullReferenceException> (() => {
231                                 mp.InstantiateInContentPlaceHolder (container, null);
232                         }, "#A1-2");
233 #if DOTNET
234                         // TODO: why does it throw? Unchecked 'as' type cast?
235                         AssertExtensions.Throws<NullReferenceException> (() => {
236                                 mp.InstantiateInContentPlaceHolder (container, template);
237                         }, "#B1-1");
238 #endif
239                         // TODO: Still throws a NREX, probably needs a full web request context, as it works below in the
240                         // InstantiateInContentPlaceHolder_WithPage test
241                         //
242                         //template = new MyContentTemplate ();
243                         //mp.InstantiateInContentPlaceHolder (container, template);
244                 }
245
246                 [Test]
247                 public void InstantiateInContentPlaceHolder_WithPage ()
248                 {
249                         WebTest t = new WebTest ("MyPageWithDerivedMaster.aspx");
250                         var pd = new PageDelegates ();
251                         pd.Load = InstantiateInContentPlaceHolder_WithPage_Load;
252                         t.Invoker = new PageInvoker (pd);
253                         t.Run ();
254                 }
255
256                 public static void InstantiateInContentPlaceHolder_WithPage_Load (Page p)
257                 {
258                         MasterPage mp = p.Master;
259                         Assert.IsNotNull (mp, "#A0");
260
261                         ITemplate template = new MyTemplate ();
262
263                         AssertExtensions.Throws<NullReferenceException> (() => {
264                                 mp.InstantiateInContentPlaceHolder (null, template);
265                         }, "#A1-1");
266
267                         Control container = new Control ();
268                         AssertExtensions.Throws<NullReferenceException> (() => {
269                                 mp.InstantiateInContentPlaceHolder (container, null);
270                         }, "#A1-2");
271
272                         mp.InstantiateInContentPlaceHolder (container, template);
273                         Assert.IsTrue (HasLiteralWithText (container, MyTemplate.MyText), "#B1-1");
274
275                         template = new MyContentTemplate ();
276                         mp.InstantiateInContentPlaceHolder (container, template);
277                         Assert.IsTrue (HasLiteralWithText (container, MyContentTemplate.MyText), "#B1-2");
278                 }
279
280                 static bool HasLiteralWithText (Control container, string text)
281                 {
282                         if (container == null || container.Controls.Count == 0)
283                                 return false;
284
285                         LiteralControl ctl;
286                         foreach (Control c in container.Controls) {
287                                 ctl = c as LiteralControl;
288                                 if (ctl == null)
289                                         continue;
290
291                                 if (String.Compare (ctl.Text, text, StringComparison.Ordinal) == 0)
292                                         return true;
293                         }
294
295                         return false;
296                 }
297
298 #endif
299                 public static void _RenderDefault (Page p)
300                 {
301                         p.Form.Controls.Add(new LiteralControl("Page dynamic text"));
302                 }
303
304                 [Test]
305                 [ExpectedException (typeof(HttpException))]
306                 public void MasterPage_AddContentTemplate ()
307                 {
308                         PokerMasterPage pmp = new PokerMasterPage();
309                         ITemplate it = null;
310                         pmp.AddContentTemplate ("myTemplate", it);
311                         pmp.AddContentTemplate ("myTemplate", it);
312                 }
313                 
314                 [TestFixtureTearDown]
315                 public void TearDown ()
316                 {
317                         WebTest.Unload ();
318                 }
319         }
320 }