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 ("NotDotNet")]  // Must be removed after adding AppRelativeVirtualPath property
167                 [Category ("NunitWeb")]
168                 public void TemplateControl_ParseControl ()
169                 {
170                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (ParseControlTest));
171                         string html = t.Run ();
172                         if (html.IndexOf ("<span id=\"lb\">test</span>") < 0)
173                                 Assert.Fail ("ParseControl failed");
174                 }
175
176                 public static void ParseControlTest (Page p)
177                 {
178                         PokerTemplateControl t = new PokerTemplateControl ();
179                         //Does not have definition , must be uncommented
180                         //t.AppRelativeVirtualPath = "~\\";
181                         Control c = t.ParseControl ("<asp:label id='lb' runat='server' text='test' />");
182                         p.Controls.Add(c);
183                 }
184
185                 [Test]
186                 public void TemplateControl_ReadStringResource ()
187                 {
188                         // p.s. MSDN
189                         // The ReadStringResource method is not intended for use from within your code
190                 }
191
192                 [Test]
193                 [Category ("NotWorking")]
194                 [Category ("NunitWeb")]
195                 public void TemplateControl_TestDeviceFilter ()
196                 {
197                         //Have no definition to TestDeviceFilter
198                         WebTest t = new WebTest (PageInvoker.CreateOnLoad (TestDeviceFilter));
199                         string html = t.Run ();
200                         
201                 }
202
203                 public static void TestDeviceFilter (Page p)
204                 {
205                         //Have no definition to TestDeviceFilter
206                         // bool res = p.TestDeviceFilter("test");
207                         // Assert.AreEqual (false, res, "TestDeviceFilter#1");
208                         //Have no definition to TestDeviceFilter
209                         // res = p.TestDeviceFilter ("IE");
210                         // Assert.AreEqual (true, res, "TestDeviceFilter#2");
211                 }
212
213                 [Test]
214                 public void TemplateControl_Construct ()
215                 {
216                         eventchecker = false;   
217                         PokerTemplateControl t = new PokerTemplateControl ();
218                         Assert.AreEqual (true, eventchecker, "Construct Failed");
219                 }
220
221                 [Test]
222                 [Category ("NunitWeb")]
223                 [Category ("NotWorking")]
224                 public void TemplateControl_Eval ()
225                 {
226                         // In this test aspx page used as template control
227 #if DOT_NET
228                         WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.EvalTest.aspx", "EvalTest.aspx");
229 #else
230                         WebTest.CopyResource (GetType (), "EvalTest.aspx", "EvalTest.aspx");
231 #endif
232                         WebTest t = new WebTest ("EvalTest.aspx");
233                         PageDelegates pd = new PageDelegates ();
234                         pd.PreRender = _templatePreRender;
235                         t.Invoker = new PageInvoker (pd);
236                         t.Run ();
237                         string html = t.Run ();
238                         if (html.IndexOf ("My databind test") < 0)
239                                 Assert.Fail ("Eval not done fail");
240                 }
241
242                 public static void _templatePreRender (Page p)
243                 {
244                         Repeater rep = p.FindControl ("Repeater1") as Repeater;
245                         if (rep == null)
246                                 Assert.Fail ("Aspx page not creation failed");
247                         Assert.AreEqual (1, rep.Items.Count, "Data items bounding failed");
248                 }
249
250                 [Test]
251                 public void TemplateControl_XPath_XPathSelect ()
252                 {
253                         //These two method are tested on XmlDataSourceTest.cs
254                 }
255
256                 [Test]
257                 public void TemplateControl_CreateResourceBasedLiteralControl ()
258                 {
259                         // The CreateResourceBasedLiteralControl method is not intended 
260                         // for use from within your code. 
261                 }
262
263                 [Test]
264                 public void TemplateControl_SetStringResourcePointer ()
265                 {
266                         // The SetStringResourcePointer method is not intended 
267                         // for use from within your code. 
268                 }
269                 
270                 [Test]
271                 public void TemplateControl_WriteUTF8ResourceString ()
272                 {
273                         //This method supports the .NET Framework infrastructure and is not intended to be used directly from your code. 
274                         //Writes a resource string to an HtmlTextWriter control. 
275                         //The WriteUTF8ResourceString method is used by generated classes and is not intended for use from within your code. 
276                 }
277
278                 // Events 
279                 bool abortTransaction;
280                 bool commitTransaction;
281                 bool error;
282
283                 [Test]
284                 public void TemplateControl_AbortTransaction ()
285                 {
286                         PokerTemplateControl t = new PokerTemplateControl ();
287                         t.AbortTransaction += new EventHandler (t_AbortTransaction);
288                         Assert.AreEqual (false, abortTransaction, "Before transaction aborted");
289                         t.DoOnAbortTransaction (new EventArgs ());
290                         Assert.AreEqual (true, abortTransaction, "After transaction aborted");
291                 }
292
293                 void t_AbortTransaction (object sender, EventArgs e)
294                 {
295                         abortTransaction = true;
296                 }
297
298                 [Test]
299                 public void TemplateControl_CommitTransaction ()
300                 {
301                         PokerTemplateControl t = new PokerTemplateControl ();
302                         t.CommitTransaction += new EventHandler (t_CommitTransaction);
303                         Assert.AreEqual (false, commitTransaction, "Before transaction Commited");
304                         t.DoOnCommitTransaction (new EventArgs ());
305                         Assert.AreEqual (true, commitTransaction, "After transaction Commited");
306                 }
307
308                 void t_CommitTransaction (object sender, EventArgs e)
309                 {
310                         commitTransaction = true;
311                 }
312
313                 [Test]
314                 public void TemplateControl_Error ()
315                 {
316                         PokerTemplateControl t = new PokerTemplateControl ();
317                         t.Error += new EventHandler (t_Error);
318                         Assert.AreEqual (false, error, "Before error");
319                         t.DoOnError (new EventArgs ());
320                         Assert.AreEqual (true, error, "After error");
321                 }
322
323                 void t_Error (object sender, EventArgs e)
324                 {
325                         error = true;
326                 }
327
328
329                 [Test]
330                 [Category ("NotWorking")]
331                 [ExpectedException (typeof (InvalidOperationException))]
332                 public void TemplateControl_EvalException ()
333                 {
334                         PokerTemplateControl t = new PokerTemplateControl ();
335                         t.DoEval (null);
336                 }
337                 
338                 [Test]
339                 [ExpectedException (typeof (ArgumentNullException))]
340                 public void TemplateControl_LoadControlException()
341                 {
342                         PokerTemplateControl t = new PokerTemplateControl ();
343                         t.LoadControl (null);
344                 }
345
346                 
347                 [Test]
348                 [Category ("NotWorking")]
349                 //[ExpectedException(typeof(ArgumentNullException))]
350                 public void TemplateControl_AppRelativeVirtualPathException1 ()
351                 {
352                         PokerTemplateControl t = new PokerTemplateControl ();
353                         //Does not have definition
354                         //t.AppRelativeVirtualPath = null;
355                 }
356
357                 [Test]
358                 [Category ("NotWorking")]
359                 //[ExpectedException (typeof (ArgumentException))]
360                 public void TemplateControl_AppRelativeVirtualPathException2 ()
361                 {
362                         PokerTemplateControl t = new PokerTemplateControl ();
363                         //Does not have definition
364                         //t.AppRelativeVirtualPath = "fake";
365                 }
366
367                 [TestFixtureTearDown]
368                 public void TearDown ()
369                 {
370                         WebTest.Unload ();
371                 }
372         }
373 }
374 #endif