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