summaryrefslogtreecommitdiff
path: root/mcs/class/System.Web/Test/System.Web.UI.WebControls/MasterPageTest.cs
blob: dfba12c6b6cf8b224a9de99f9bf879dd1da134ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
// Tests for System.Web.UI.WebControls.MasterPageTest.cs
//
// Author:
//	Yoni Klein (yonik@mainsoft.com)
//
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#if NET_2_0

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using MyWebControl = System.Web.UI.WebControls;
using System.Collections;
using MonoTests.SystemWeb.Framework;
using MonoTests.stand_alone.WebHarness;
using System.Threading;
using MonoTests.Common;

namespace MonoTests.System.Web.UI.WebControls
{
	public class PokerMasterPage : MasterPage
	{
		public PokerMasterPage ()
		{
			TrackViewState ();
		}
		public StateBag StateBag
		{
			get { return base.ViewState; }
		}
		public new IDictionary ContentTemplates ()
		{
			return base.ContentTemplates;
		}
		public new void AddContentTemplate (string templateName, ITemplate template)
		{
			base.AddContentTemplate (templateName, template);
		}
		public string MasterMethod ()
		{
			return "FromMasterMethod";
		}
	}

	[TestFixture]
	public class MasterPageTest
	{
#if NET_4_0
		class MyTemplate : ITemplate
		{
			public const string MyText = "|MyTemplate.InstantiateIn called|";

			public void InstantiateIn (Control container)
			{
				container.Controls.Add (new LiteralControl (MyText));
			}
		}

		class MyContentTemplate : Content, ITemplate
		{
			public const string MyText = "|MyContentTemplate.InstantiateIn called|";

			public void InstantiateIn (Control container)
			{
				container.Controls.Add (new LiteralControl (MyText));
			}
		}
#endif
		[TestFixtureSetUp]
		public void CopyTestResources ()
		{
			WebTest.CopyResource (GetType (), "MasterTypeTest1.aspx", "MasterTypeTest1.aspx");
			WebTest.CopyResource (GetType (), "MasterTypeTest2.aspx", "MasterTypeTest2.aspx");
			WebTest.CopyResource (GetType (), "MyDerived.master", "MyDerived.master");
			WebTest.CopyResource (GetType (), "MyPageWithDerivedMaster.aspx", "MyPageWithDerivedMaster.aspx");
		}

		[SetUp]
		public void SetupTestCase ()
		{
			Thread.Sleep (100);
		}

		[Test]
		public void MasterPage_DefaultProperties ()
		{
			PokerMasterPage pmp = new PokerMasterPage ();
			Assert.AreEqual (null, pmp.Master, "Master Property");
			Assert.AreEqual (null, pmp.MasterPageFile, "MasterPageFile Property");
		}

		[Test]
		[Category ("NotWorking")]
		public void MasterPage_DefaultPropertiesNotWorking ()
		{
			PokerMasterPage pmp = new PokerMasterPage ();
			IDictionary i = pmp.ContentTemplates ();
			Assert.AreEqual (null, i, "ContentTemplates");
		}

		[Test]
		[Category ("NunitWeb")]
		public void MasterPage_Render ()
		{
			Render_Helper (StandardUrl.PAGE_WITH_MASTER);
		}


		[Test]
		[Category ("NunitWeb")]
		public void MasterPageDerived_Render ()
		{
			Render_Helper (StandardUrl.PAGE_WITH_DERIVED_MASTER);
		}

		// Bug #325114
		[Test]
		[Category ("NunitWeb")]
		[ExpectedException (typeof(HttpException))]
		public void MasterPage_ContentPlaceHolder_Not_Found ()
		{
			Render_Helper (StandardUrl.PAGE_WITH_MASTER_INVALID_PLACE_HOLDER);
		}
		
		public void Render_Helper(string url)
		{
			WebTest t = new WebTest (PageInvoker.CreateOnLoad (_RenderDefault));
			t.Request.Url = url;
			string PageRenderHtml = t.Run ();
			
			
			if (PageRenderHtml.IndexOf ("Page main text") < 0) {
			        Assert.Fail ("Master#2");
			}
			
			Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master main text"), "Master#3");
			

			if (PageRenderHtml.IndexOf ("Page dynamic text") < 0) {
				Assert.Fail ("Master#5");
			}

			if (PageRenderHtml.IndexOf ("My master page footer") < 0) {
			        Assert.Fail ("Master#6, result: "+PageRenderHtml);
			}

			if (PageRenderHtml.IndexOf ("Master page content text") < 0) {
				Assert.Fail ("Master#7");
			}

			if (url == StandardUrl.PAGE_WITH_DERIVED_MASTER) {
				if (PageRenderHtml.IndexOf ("Derived header text") < 0) {
					Assert.Fail ("Master#8");
				}

				if (PageRenderHtml.IndexOf ("Derived master page text ") < 0) {
					Assert.Fail ("Master#9");
				}

				if (PageRenderHtml.IndexOf ("Master header text") < 0) {
					Assert.Fail ("Master#10");
				}
			}
			else {
				Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master header text"), "Master#1");
				Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master dynamic text"), "Master#4");
			}
		}

		[Test]
		[Category ("NunitWeb")]
		[Category ("NotWorking")]
		public void MasterType_VirtualPath ()
		{
			WebTest t = new WebTest ("MasterTypeTest1.aspx");
			string PageRenderHtml = t.Run ();
			if (PageRenderHtml.IndexOf ("MasterTypeMethod") < 0)
				Assert.Fail ("MasterType VirtualPath test failed");
		}

		[Test]
		[Category ("NunitWeb")]
		public void MasterType_TypeName ()
		{
			WebTest t = new WebTest ("MasterTypeTest2.aspx");
			string PageRenderHtml = t.Run ();
			if (PageRenderHtml.IndexOf ("FromMasterMethod") < 0)
				Assert.Fail ("MasterType TypeName test failed");
		}
#if NET_4_0
		[Test]
		public void InstantiateInContentPlaceHolder ()
		{
			var mp = new MasterPage ();
			ITemplate template = new MyTemplate ();

			AssertExtensions.Throws<NullReferenceException> (() => {
				mp.InstantiateInContentPlaceHolder (null, template);
			}, "#A1-1");

			Control container = new Control ();
			AssertExtensions.Throws<NullReferenceException> (() => {
				mp.InstantiateInContentPlaceHolder (container, null);
			}, "#A1-2");
#if DOTNET
			// TODO: why does it throw? Unchecked 'as' type cast?
			AssertExtensions.Throws<NullReferenceException> (() => {
				mp.InstantiateInContentPlaceHolder (container, template);
			}, "#B1-1");
#endif
			// TODO: Still throws a NREX, probably needs a full web request context, as it works below in the
			// InstantiateInContentPlaceHolder_WithPage test
			//
			//template = new MyContentTemplate ();
			//mp.InstantiateInContentPlaceHolder (container, template);
		}

		[Test]
		public void InstantiateInContentPlaceHolder_WithPage ()
		{
			WebTest t = new WebTest ("MyPageWithDerivedMaster.aspx");
			var pd = new PageDelegates ();
			pd.Load = InstantiateInContentPlaceHolder_WithPage_Load;
			t.Invoker = new PageInvoker (pd);
			t.Run ();
		}

		public static void InstantiateInContentPlaceHolder_WithPage_Load (Page p)
		{
			MasterPage mp = p.Master;
			Assert.IsNotNull (mp, "#A0");

			ITemplate template = new MyTemplate ();

			AssertExtensions.Throws<NullReferenceException> (() => {
				mp.InstantiateInContentPlaceHolder (null, template);
			}, "#A1-1");

			Control container = new Control ();
			AssertExtensions.Throws<NullReferenceException> (() => {
				mp.InstantiateInContentPlaceHolder (container, null);
			}, "#A1-2");

			mp.InstantiateInContentPlaceHolder (container, template);
			Assert.IsTrue (HasLiteralWithText (container, MyTemplate.MyText), "#B1-1");

			template = new MyContentTemplate ();
			mp.InstantiateInContentPlaceHolder (container, template);
			Assert.IsTrue (HasLiteralWithText (container, MyContentTemplate.MyText), "#B1-2");
		}

		static bool HasLiteralWithText (Control container, string text)
		{
			if (container == null || container.Controls.Count == 0)
				return false;

			LiteralControl ctl;
			foreach (Control c in container.Controls) {
				ctl = c as LiteralControl;
				if (ctl == null)
					continue;

				if (String.Compare (ctl.Text, text, StringComparison.Ordinal) == 0)
					return true;
			}

			return false;
		}

#endif
		public static void _RenderDefault (Page p)
		{
			p.Form.Controls.Add(new LiteralControl("Page dynamic text"));
		}

		[Test]
	 	[ExpectedException (typeof(HttpException))]
		public void MasterPage_AddContentTemplate ()
		{
			PokerMasterPage pmp = new PokerMasterPage();
			ITemplate it = null;
			pmp.AddContentTemplate ("myTemplate", it);
			pmp.AddContentTemplate ("myTemplate", it);
		}
		
		[TestFixtureTearDown]
		public void TearDown ()
		{
			WebTest.Unload ();
		}
	}
}
#endif