TemplateControlTest.cs 10.0 KB

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