MasterPageTest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Tests for System.Web.UI.WebControls.MasterPageTest.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 NUnit.Framework;
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Text;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36. using System.IO;
  37. using System.Drawing;
  38. using MyWebControl = System.Web.UI.WebControls;
  39. using System.Collections;
  40. using MonoTests.SystemWeb.Framework;
  41. using MonoTests.stand_alone.WebHarness;
  42. using System.Threading;
  43. namespace MonoTests.System.Web.UI.WebControls
  44. {
  45. public class PokerMasterPage : MasterPage
  46. {
  47. public PokerMasterPage ()
  48. {
  49. TrackViewState ();
  50. }
  51. public StateBag StateBag
  52. {
  53. get { return base.ViewState; }
  54. }
  55. public new IDictionary ContentTemplates ()
  56. {
  57. return base.ContentTemplates;
  58. }
  59. public new void AddContentTemplate (string templateName, ITemplate template)
  60. {
  61. base.AddContentTemplate (templateName, template);
  62. }
  63. public string MasterMethod ()
  64. {
  65. return "FromMasterMethod";
  66. }
  67. }
  68. [TestFixture]
  69. public class MasterPageTest
  70. {
  71. public const string PAGE_WITH_MASTER = "MyPageWithMaster.aspx";
  72. public const string PAGE_WITH_DERIVED_MASTER = "MyPageWithDerivedMaster.aspx";
  73. [TestFixtureSetUp]
  74. public void CopyTestResources ()
  75. {
  76. #if DOT_NET
  77. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.MasterTypeTest1.aspx", "MasterTypeTest1.aspx");
  78. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.MasterTypeTest2.aspx", "MasterTypeTest2.aspx");
  79. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.MyDerived.master", "MyDerived.master");
  80. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.MyPageWithDerivedMaster.aspx", "MyPageWithDerivedMaster.aspx");
  81. #else
  82. WebTest.CopyResource (GetType (), "MasterTypeTest1.aspx", "MasterTypeTest1.aspx");
  83. WebTest.CopyResource (GetType (), "MasterTypeTest2.aspx", "MasterTypeTest2.aspx");
  84. WebTest.CopyResource (GetType (), "MyDerived.master", "MyDerived.master");
  85. WebTest.CopyResource (GetType (), "MyPageWithDerivedMaster.aspx", "MyPageWithDerivedMaster.aspx");
  86. #endif
  87. }
  88. [SetUp]
  89. public void SetupTestCase ()
  90. {
  91. Thread.Sleep (100);
  92. }
  93. [Test]
  94. public void MasterPage_DefaultProperties ()
  95. {
  96. PokerMasterPage pmp = new PokerMasterPage ();
  97. Assert.AreEqual (null, pmp.Master, "Master Property");
  98. Assert.AreEqual (null, pmp.MasterPageFile, "MasterPageFile Property");
  99. }
  100. [Test]
  101. [Category ("NotWorking")]
  102. public void MasterPage_DefaultPropertiesNotWorking ()
  103. {
  104. PokerMasterPage pmp = new PokerMasterPage ();
  105. IDictionary i = pmp.ContentTemplates ();
  106. Assert.AreEqual (null, i, "ContentTemplates");
  107. }
  108. [Test]
  109. [Category ("NunitWeb")]
  110. public void MasterPage_Render ()
  111. {
  112. Render_Helper (StandardUrl.PAGE_WITH_MASTER);
  113. }
  114. [Test]
  115. [Category ("NunitWeb")]
  116. public void MasterPageDerived_Render ()
  117. {
  118. Render_Helper (StandardUrl.PAGE_WITH_DERIVED_MASTER);
  119. }
  120. public void Render_Helper(string url)
  121. {
  122. WebTest t = new WebTest (PageInvoker.CreateOnLoad (_RenderDefault));
  123. t.Request.Url = url;
  124. string PageRenderHtml = t.Run ();
  125. if (PageRenderHtml.IndexOf ("Page main text") < 0) {
  126. Assert.Fail ("Master#2");
  127. }
  128. Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master main text"), "Master#3");
  129. if (PageRenderHtml.IndexOf ("Page dynamic text") < 0) {
  130. Assert.Fail ("Master#5");
  131. }
  132. if (PageRenderHtml.IndexOf ("My master page footer") < 0) {
  133. Assert.Fail ("Master#6, result: "+PageRenderHtml);
  134. }
  135. if (PageRenderHtml.IndexOf ("Master page content text") < 0) {
  136. Assert.Fail ("Master#7");
  137. }
  138. if (url == PAGE_WITH_DERIVED_MASTER) {
  139. if (PageRenderHtml.IndexOf ("Derived header text") < 0) {
  140. Assert.Fail ("Master#8");
  141. }
  142. if (PageRenderHtml.IndexOf ("Derived master page text ") < 0) {
  143. Assert.Fail ("Master#9");
  144. }
  145. if (PageRenderHtml.IndexOf ("Master header text") < 0) {
  146. Assert.Fail ("Master#10");
  147. }
  148. }
  149. else {
  150. Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master header text"), "Master#1");
  151. Assert.AreEqual (-1, PageRenderHtml.IndexOf ("Master dynamic text"), "Master#4");
  152. }
  153. }
  154. [Test]
  155. [Category ("NunitWeb")]
  156. [Category ("NotWorking")]
  157. public void MasterType_VirtualPath ()
  158. {
  159. WebTest t = new WebTest ("MasterTypeTest1.aspx");
  160. string PageRenderHtml = t.Run ();
  161. if (PageRenderHtml.IndexOf ("MasterTypeMethod") < 0)
  162. Assert.Fail ("MasterType VirtualPath test failed");
  163. }
  164. [Test]
  165. [Category ("NunitWeb")]
  166. public void MasterType_TypeName ()
  167. {
  168. WebTest t = new WebTest ("MasterTypeTest2.aspx");
  169. string PageRenderHtml = t.Run ();
  170. if (PageRenderHtml.IndexOf ("FromMasterMethod") < 0)
  171. Assert.Fail ("MasterType TypeName test failed");
  172. }
  173. public static void _RenderDefault (Page p)
  174. {
  175. p.Form.Controls.Add(new LiteralControl("Page dynamic text"));
  176. }
  177. [Test]
  178. [ExpectedException (typeof(HttpException))]
  179. public void MasterPage_AddContentTemplate ()
  180. {
  181. PokerMasterPage pmp = new PokerMasterPage();
  182. ITemplate it = null;
  183. pmp.AddContentTemplate ("myTemplate", it);
  184. pmp.AddContentTemplate ("myTemplate", it);
  185. }
  186. [TestFixtureTearDown]
  187. public void TearDown ()
  188. {
  189. WebTest.Unload ();
  190. }
  191. }
  192. }
  193. #endif