New test.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / TemplateControlTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.TemplateControlTest.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 #if NET_2_0
31
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 NUnit.Framework;
44 using MonoTests.stand_alone.WebHarness;
45 using System.Threading;
46
47
48 namespace MonoTests.System.Web.UI.WebControls
49 {
50         
51         class PokerTemplateControl:TemplateControl
52         {
53                 public PokerTemplateControl ()
54                 {
55                         TrackViewState ();
56                 }
57
58                 public bool DoSupportAutoEvents
59                 {
60                         get { return base.SupportAutoEvents; }
61                 }
62
63                 protected override void Construct ()
64                 {
65                         TemplateControlTest.eventchecker = true;
66                         base.Construct ();
67                 }
68
69                 
70                 public void DoOnAbortTransaction (EventArgs e)
71                 {
72                         base.OnAbortTransaction (e);
73                 }
74
75                 public void DoOnCommitTransaction (EventArgs e)
76                 {
77                         base.OnCommitTransaction (e);
78                 }
79
80                 public void DoOnError (EventArgs e)
81                 {
82                         base.OnError (e);
83                 }
84
85                 public object DoEval (string str)
86                 {
87                         return base.Eval (str);
88                 }
89         }
90
91
92         [TestFixture]
93         public class TemplateControlTest
94         {
95                 public static bool eventchecker;
96                 public string message = "My message text";
97
98                 [TestFixtureSetUp]
99                 public void GridViewInit ()
100                 {
101 #if DOT_NET
102                         WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.TemplateUserControl.ascx", "TemplateUserControl.ascx");
103 #else
104                         WebTest.CopyResource (GetType (), "TemplateUserControl.ascx", "TemplateUserControl.ascx");
105 #endif
106                 }
107
108                 [SetUp]
109                 public void SetupTestCase ()
110                 {
111                         Thread.Sleep (100);
112                 }
113
114                 [Test]
115                 public void TemplateControl_DefaultProperty ()
116                 {
117                         PokerTemplateControl t = new PokerTemplateControl ();
118                         Assert.AreEqual (true, t.EnableTheming, "EnableTheming");
119                         Assert.AreEqual (true, t.DoSupportAutoEvents, "SupportAutoEvents");
120                 }
121
122                 [Test]
123                 [Category ("NotWorking")]
124                 public void TemplateControl_DefaultPropertyNotWorking ()
125                 {
126                         PokerTemplateControl t = new PokerTemplateControl ();
127                         //Does not have definition
128                         //Assert.AreEqual (null, t.AppRelativeVirtualPath, "AppRelativeVirtualPath");
129                 }
130
131                 [Test]
132                 [Category ("NunitWeb")]
133                 public void TemplateControl_LoadControl ()
134                 {
135                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadControlTest));
136                         string html = t.Run ();
137                         if (html.IndexOf ("TemplateUserControl") < 0)
138                                 Assert.Fail ("LoadControl failed");
139                 }
140
141                 public static void LoadControlTest (Page p)
142                 {
143                         PokerTemplateControl t = new PokerTemplateControl ();
144                         p.Form.Controls.Add (t.LoadControl ("TemplateUserControl.ascx"));
145                 }
146
147                 [Test]
148                 [Category ("NunitWeb")]
149                 public void TemplateControl_LoadTemplate ()
150                 {
151                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadTemplateTest));
152                         string html = t.Run ();
153                         if (html.IndexOf ("TemplateUserControl") < 0)
154                                 Assert.Fail ("LoadTemplate failed");
155                 }
156
157                 public static void LoadTemplateTest (Page p)
158                 {
159                         PokerTemplateControl t = new PokerTemplateControl ();
160                         ITemplate tmp = t.LoadTemplate ("TemplateUserControl.ascx");
161                         tmp.InstantiateIn (p.Form);
162                 }
163
164                 [Test]
165                 [Category ("NotWorking")]
166                 [Category ("NunitWeb")]
167                 public void TemplateControl_ParseControl ()
168                 {
169                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ParseControlTest));
170                         string html = t.Run ();
171                         if (html.IndexOf ("<span id=\"lb\">test</span>") < 0)
172                                 Assert.Fail ("ParseControl failed");
173                 }
174
175                 public static void ParseControlTest (Page p)
176                 {
177                         PokerTemplateControl t = new PokerTemplateControl ();
178                         //Does not have definition , must be uncommented
179                         //t.AppRelativeVirtualPath = "~\\";
180                         Control c = t.ParseControl ("<asp:label id='lb' runat='server' text='test' />");
181                         p.Controls.Add(c);
182                 }
183
184                 [Test]
185                 public void TemplateControl_ReadStringResource ()
186                 {
187                         // p.s. MSDN
188                         // The ReadStringResource method is not intended for use from within your code
189                 }
190
191                 [Test]
192                 [Category ("NotWorking")]
193                 [Category ("NunitWeb")]
194                 public void TemplateControl_TestDeviceFilter ()
195                 {
196                         //Have no definition to TestDeviceFilter
197                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (TestDeviceFilter));
198                         string html = t.Run ();
199                         
200                 }
201
202                 public static void TestDeviceFilter (Page p)
203                 {
204                         //Have no definition to TestDeviceFilter
205                         // bool res = p.TestDeviceFilter("test");
206                         // Assert.AreEqual (false, res, "TestDeviceFilter#1");
207                         //Have no definition to TestDeviceFilter
208                         // res = p.TestDeviceFilter ("IE");
209                         // Assert.AreEqual (true, res, "TestDeviceFilter#2");
210                 }
211
212                 [Test]
213                 public void TemplateControl_Construct ()
214                 {
215                         eventchecker = false;   
216                         PokerTemplateControl t = new PokerTemplateControl ();
217                         Assert.AreEqual (true, eventchecker, "Construct Failed");
218                 }
219
220                 [Test]
221                 [Category ("NunitWeb")]
222                 [Category ("NotWorking")]
223                 public void TemplateControl_Eval ()
224                 {
225                         // In this test aspx page used as template control
226 #if DOT_NET
227                         WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.EvalTest.aspx", "EvalTest.aspx");
228 #else
229                         WebTest.CopyResource (GetType (), "EvalTest.aspx", "EvalTest.aspx");
230 #endif
231                         WebTest t = new WebTest ("EvalTest.aspx");
232                         PageDelegates pd = new PageDelegates ();
233                         pd.PreRender = _templatePreRender;
234                         t.Invoker = new PageInvoker (pd);
235                         t.Run ();
236                         string html = t.Run ();
237                         if (html.IndexOf ("My databind test") < 0)
238                                 Assert.Fail ("Eval not done fail");
239                 }
240
241                 public static void _templatePreRender (Page p)
242                 {
243                         Repeater rep = p.FindControl ("Repeater1") as Repeater;
244                         if (rep == null)
245                                 Assert.Fail ("Aspx page not creation failed");
246                         Assert.AreEqual (1, rep.Items.Count, "Data items bounding failed");
247                 }
248
249                 [Test]
250                 public void TemplateControl_XPath_XPathSelect ()
251                 {
252                         //These two method are tested on XmlDataSourceTest.cs
253                 }
254
255                 [Test]
256                 public void TemplateControl_CreateResourceBasedLiteralControl ()
257                 {
258                         // The CreateResourceBasedLiteralControl method is not intended 
259                         // for use from within your code. 
260                 }
261
262                 [Test]
263                 public void TemplateControl_SetStringResourcePointer ()
264                 {
265                         // The SetStringResourcePointer method is not intended 
266                         // for use from within your code. 
267                 }
268                 
269                 [Test]
270                 public void TemplateControl_WriteUTF8ResourceString ()
271                 {
272                         //This method supports the .NET Framework infrastructure and is not intended to be used directly from your code. 
273                         //Writes a resource string to an HtmlTextWriter control. 
274                         //The WriteUTF8ResourceString method is used by generated classes and is not intended for use from within your code. 
275                 }
276
277                 // Events 
278                 bool abortTransaction;
279                 bool commitTransaction;
280                 bool error;
281
282                 [Test]
283                 public void TemplateControl_AbortTransaction ()
284                 {
285                         PokerTemplateControl t = new PokerTemplateControl ();
286                         t.AbortTransaction += new EventHandler (t_AbortTransaction);
287                         Assert.AreEqual (false, abortTransaction, "Before transaction aborted");
288                         t.DoOnAbortTransaction (new EventArgs ());
289                         Assert.AreEqual (true, abortTransaction, "After transaction aborted");
290                 }
291
292                 void t_AbortTransaction (object sender, EventArgs e)
293                 {
294                         abortTransaction = true;
295                 }
296
297                 [Test]
298                 public void TemplateControl_CommitTransaction ()
299                 {
300                         PokerTemplateControl t = new PokerTemplateControl ();
301                         t.CommitTransaction += new EventHandler (t_CommitTransaction);
302                         Assert.AreEqual (false, commitTransaction, "Before transaction Commited");
303                         t.DoOnCommitTransaction (new EventArgs ());
304                         Assert.AreEqual (true, commitTransaction, "After transaction Commited");
305                 }
306
307                 void t_CommitTransaction (object sender, EventArgs e)
308                 {
309                         commitTransaction = true;
310                 }
311
312                 [Test]
313                 public void TemplateControl_Error ()
314                 {
315                         PokerTemplateControl t = new PokerTemplateControl ();
316                         t.Error += new EventHandler (t_Error);
317                         Assert.AreEqual (false, error, "Before error");
318                         t.DoOnError (new EventArgs ());
319                         Assert.AreEqual (true, error, "After error");
320                 }
321
322                 void t_Error (object sender, EventArgs e)
323                 {
324                         error = true;
325                 }
326
327
328                 [Test]
329                 [Category ("NotWorking")]
330                 [ExpectedException (typeof (InvalidOperationException))]
331                 public void TemplateControl_EvalException ()
332                 {
333                         PokerTemplateControl t = new PokerTemplateControl ();
334                         t.DoEval (null);
335                 }
336                 
337                 [Test]
338                 [ExpectedException (typeof (ArgumentNullException))]
339                 public void TemplateControl_LoadControlException()
340                 {
341                         PokerTemplateControl t = new PokerTemplateControl ();
342                         t.LoadControl (null);
343                 }
344
345                 [Test]
346                 [Category ("NotWorking")]
347                 [ExpectedException(typeof(ArgumentNullException))]
348                 public void TemplateControl_AppRelativeVirtualPathException1 ()
349                 {
350                         PokerTemplateControl t = new PokerTemplateControl ();
351                         //Does not have definition
352                         //t.AppRelativeVirtualPath = null;
353                 }
354
355                 [Test]
356                 [Category ("NotWorking")]
357                 [ExpectedException (typeof (ArgumentException))]
358                 public void TemplateControl_AppRelativeVirtualPathException2 ()
359                 {
360                         PokerTemplateControl t = new PokerTemplateControl ();
361                         //Does not have definition
362                         //t.AppRelativeVirtualPath = "fake";
363                 }
364
365                 [TestFixtureTearDown]
366                 public void TearDown ()
367                 {
368                         WebTest.Unload ();
369                 }
370         }
371 }
372 #endif