2 // Tests for System.Web.UI.WebControls.MasterPageTest.cs
5 // Yoni Klein (yonik@mainsoft.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
33 using System.Collections.Generic;
37 using System.Web.UI.WebControls;
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;
47 namespace MonoTests.System.Web.UI.WebControls
49 public class PokerMasterPage : MasterPage
51 public PokerMasterPage ()
55 public StateBag StateBag
57 get { return base.ViewState; }
59 public new IDictionary ContentTemplates ()
61 return base.ContentTemplates;
63 public new void AddContentTemplate (string templateName, ITemplate template)
65 base.AddContentTemplate (templateName, template);
67 public string MasterMethod ()
69 return "FromMasterMethod";
74 public class MasterPageTest
76 class MyTemplate : ITemplate
78 public const string MyText = "|MyTemplate.InstantiateIn called|";
80 public void InstantiateIn (Control container)
82 container.Controls.Add (new LiteralControl (MyText));
86 class MyContentTemplate : Content, ITemplate
88 public const string MyText = "|MyContentTemplate.InstantiateIn called|";
90 public void InstantiateIn (Control container)
92 container.Controls.Add (new LiteralControl (MyText));
96 public void CopyTestResources ()
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");
105 public void SetupTestCase ()
111 public void MasterPage_DefaultProperties ()
113 PokerMasterPage pmp = new PokerMasterPage ();
114 Assert.AreEqual (null, pmp.Master, "Master Property");
115 Assert.AreEqual (null, pmp.MasterPageFile, "MasterPageFile Property");
119 [Category ("NotWorking")]
120 public void MasterPage_DefaultPropertiesNotWorking ()
122 PokerMasterPage pmp = new PokerMasterPage ();
123 IDictionary i = pmp.ContentTemplates ();
124 Assert.AreEqual (null, i, "ContentTemplates");
128 [Category ("NunitWeb")]
129 public void MasterPage_Render ()
131 Render_Helper (StandardUrl.PAGE_WITH_MASTER);
136 [Category ("NunitWeb")]
137 public void MasterPageDerived_Render ()
139 Render_Helper (StandardUrl.PAGE_WITH_DERIVED_MASTER);
144 [Category ("NunitWeb")]
145 [ExpectedException (typeof(HttpException))]
146 public void MasterPage_ContentPlaceHolder_Not_Found ()
148 Render_Helper (StandardUrl.PAGE_WITH_MASTER_INVALID_PLACE_HOLDER);
151 public void Render_Helper(string url)
153 WebTest t = new WebTest (PageInvoker.CreateOnLoad (_RenderDefault));
155 string PageRenderHtml = t.Run ();
158 if (PageRenderHtml.IndexOf ("Page main text") < 0) {
159 Assert.Fail ("Master#2");
162 Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master main text"), "Master#3");
165 if (PageRenderHtml.IndexOf ("Page dynamic text") < 0) {
166 Assert.Fail ("Master#5");
169 if (PageRenderHtml.IndexOf ("My master page footer") < 0) {
170 Assert.Fail ("Master#6, result: "+PageRenderHtml);
173 if (PageRenderHtml.IndexOf ("Master page content text") < 0) {
174 Assert.Fail ("Master#7");
177 if (url == StandardUrl.PAGE_WITH_DERIVED_MASTER) {
178 if (PageRenderHtml.IndexOf ("Derived header text") < 0) {
179 Assert.Fail ("Master#8");
182 if (PageRenderHtml.IndexOf ("Derived master page text ") < 0) {
183 Assert.Fail ("Master#9");
186 if (PageRenderHtml.IndexOf ("Master header text") < 0) {
187 Assert.Fail ("Master#10");
191 Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master header text"), "Master#1");
192 Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master dynamic text"), "Master#4");
197 [Category ("NunitWeb")]
198 [Category ("NotWorking")]
199 public void MasterType_VirtualPath ()
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");
208 [Category ("NunitWeb")]
209 public void MasterType_TypeName ()
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");
217 public void InstantiateInContentPlaceHolder ()
219 var mp = new MasterPage ();
220 ITemplate template = new MyTemplate ();
222 AssertExtensions.Throws<NullReferenceException> (() => {
223 mp.InstantiateInContentPlaceHolder (null, template);
226 Control container = new Control ();
227 AssertExtensions.Throws<NullReferenceException> (() => {
228 mp.InstantiateInContentPlaceHolder (container, null);
231 // TODO: why does it throw? Unchecked 'as' type cast?
232 AssertExtensions.Throws<NullReferenceException> (() => {
233 mp.InstantiateInContentPlaceHolder (container, template);
236 // TODO: Still throws a NREX, probably needs a full web request context, as it works below in the
237 // InstantiateInContentPlaceHolder_WithPage test
239 //template = new MyContentTemplate ();
240 //mp.InstantiateInContentPlaceHolder (container, template);
244 public void InstantiateInContentPlaceHolder_WithPage ()
246 WebTest t = new WebTest ("MyPageWithDerivedMaster.aspx");
247 var pd = new PageDelegates ();
248 pd.Load = InstantiateInContentPlaceHolder_WithPage_Load;
249 t.Invoker = new PageInvoker (pd);
253 public static void InstantiateInContentPlaceHolder_WithPage_Load (Page p)
255 MasterPage mp = p.Master;
256 Assert.IsNotNull (mp, "#A0");
258 ITemplate template = new MyTemplate ();
260 AssertExtensions.Throws<NullReferenceException> (() => {
261 mp.InstantiateInContentPlaceHolder (null, template);
264 Control container = new Control ();
265 AssertExtensions.Throws<NullReferenceException> (() => {
266 mp.InstantiateInContentPlaceHolder (container, null);
269 mp.InstantiateInContentPlaceHolder (container, template);
270 Assert.IsTrue (HasLiteralWithText (container, MyTemplate.MyText), "#B1-1");
272 template = new MyContentTemplate ();
273 mp.InstantiateInContentPlaceHolder (container, template);
274 Assert.IsTrue (HasLiteralWithText (container, MyContentTemplate.MyText), "#B1-2");
277 static bool HasLiteralWithText (Control container, string text)
279 if (container == null || container.Controls.Count == 0)
283 foreach (Control c in container.Controls) {
284 ctl = c as LiteralControl;
288 if (String.Compare (ctl.Text, text, StringComparison.Ordinal) == 0)
295 public static void _RenderDefault (Page p)
297 p.Form.Controls.Add(new LiteralControl("Page dynamic text"));
301 [ExpectedException (typeof(HttpException))]
302 public void MasterPage_AddContentTemplate ()
304 PokerMasterPage pmp = new PokerMasterPage();
306 pmp.AddContentTemplate ("myTemplate", it);
307 pmp.AddContentTemplate ("myTemplate", it);
310 [TestFixtureTearDown]
311 public void TearDown ()