HtmlAnchorTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // HtmlAnchorTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlAnchor
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  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. using System;
  30. using System.IO;
  31. using System.Web.UI;
  32. using System.Web.UI.HtmlControls;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Web.UI.HtmlControls {
  35. public class TestHtmlAnchor : HtmlAnchor {
  36. public HtmlTextWriter GetWriter ()
  37. {
  38. StringWriter text = new StringWriter ();
  39. HtmlTextWriter writer = new HtmlTextWriter (text);
  40. base.RenderAttributes (writer);
  41. return writer;
  42. }
  43. #if NET_2_0
  44. public void Raise ()
  45. {
  46. base.RaisePostBackEvent ("2.0");
  47. }
  48. #endif
  49. public void TrakState () {
  50. TrackViewState();
  51. }
  52. public object SaveState () {
  53. return SaveViewState ();
  54. }
  55. public void LoadState (object state) {
  56. LoadViewState (state);
  57. }
  58. }
  59. [TestFixture]
  60. public class HtmlAnchorTest {
  61. [Test]
  62. public void DefaultProperties ()
  63. {
  64. HtmlAnchor a = new HtmlAnchor ();
  65. Assert.AreEqual (0, a.Attributes.Count, "Attributes.Count");
  66. Assert.AreEqual (String.Empty, a.HRef, "HRef");
  67. Assert.AreEqual (String.Empty, a.Name, "Name");
  68. Assert.AreEqual (String.Empty, a.Target, "Target");
  69. Assert.AreEqual (String.Empty, a.Title, "Title");
  70. Assert.AreEqual ("a", a.TagName, "TagName");
  71. }
  72. [Test]
  73. public void NullProperties ()
  74. {
  75. HtmlAnchor a = new HtmlAnchor ();
  76. a.HRef = null;
  77. Assert.AreEqual (String.Empty, a.HRef, "HRef");
  78. a.Name = null;
  79. Assert.AreEqual (String.Empty, a.Name, "Name");
  80. a.Target = null;
  81. Assert.AreEqual (String.Empty, a.Target, "Target");
  82. a.Title = null;
  83. Assert.AreEqual (String.Empty, a.Title, "Title");
  84. Assert.AreEqual (0, a.Attributes.Count, "Attributes.Count");
  85. }
  86. [Test]
  87. public void Target ()
  88. {
  89. HtmlAnchor a = new HtmlAnchor ();
  90. // specials (various casing)
  91. a.Target = "_blank";
  92. Assert.AreEqual ("_blank", a.Target, "_blank");
  93. a.Target = "_parent";
  94. Assert.AreEqual ("_parent", a.Target, "_parent");
  95. a.Target = "_SELF";
  96. Assert.AreEqual ("_SELF", a.Target, "_SELF");
  97. a.Target = "_ToP";
  98. Assert.AreEqual ("_ToP", a.Target, "_ToP");
  99. // alpha
  100. a.Target = "a";
  101. Assert.AreEqual ("a", a.Target, "a");
  102. a.Target = "blank";
  103. Assert.AreEqual ("blank", a.Target, "blank");
  104. a.Target = "Z";
  105. Assert.AreEqual ("Z", a.Target, "Z");
  106. }
  107. [Test]
  108. public void Target_Invalid ()
  109. {
  110. HtmlAnchor a = new HtmlAnchor ();
  111. // non alpha
  112. a.Target = "9a";
  113. Assert.AreEqual ("9a", a.Target, "9a");
  114. // non special
  115. a.Target = "_mono";
  116. Assert.AreEqual ("_mono", a.Target, "_mono");
  117. }
  118. [Test]
  119. public void HRef ()
  120. {
  121. TestHtmlAnchor a = new TestHtmlAnchor ();
  122. a.HRef = "~/otherfile.txt";
  123. Assert.AreEqual ("~/otherfile.txt", a.HRef, "HRef");
  124. // resolve doesn't apply on the property
  125. }
  126. [Test]
  127. public void RenderAttributes ()
  128. {
  129. TestHtmlAnchor a = new TestHtmlAnchor ();
  130. a.HRef = "*1*";
  131. a.Name = "*2*";
  132. a.Target = "*3*";
  133. a.Title = "*4*";
  134. Assert.AreEqual (4, a.Attributes.Count, "Attributes.Count/4");
  135. HtmlTextWriter writer = a.GetWriter ();
  136. Assert.AreEqual (" href=\"*1*\" name=\"*2*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "attributes");
  137. // HRef is missing, from the attributes collection, after rendering
  138. Assert.AreEqual (3, a.Attributes.Count, "Attributes.Count/3");
  139. Assert.AreEqual (String.Empty, a.HRef, "HRef");
  140. // but href is still rendered
  141. Assert.AreEqual (" href=\"*1*\" name=\"*2*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "HRef is back");
  142. }
  143. private bool serverClick;
  144. private void ServerClick (object sender, EventArgs e)
  145. {
  146. serverClick = true;
  147. }
  148. [Test]
  149. #if NET_2_0
  150. [ExpectedException (typeof (NullReferenceException))] // since 2.0 RC :-(
  151. #endif
  152. public void IPostBackEventHandler_RaisePostBackEvent ()
  153. {
  154. TestHtmlAnchor a = new TestHtmlAnchor ();
  155. a.ServerClick += new EventHandler (ServerClick);
  156. IPostBackEventHandler pbeh = (a as IPostBackEventHandler);
  157. serverClick = false;
  158. pbeh.RaisePostBackEvent ("mono");
  159. Assert.IsTrue (serverClick, "ServerClick");
  160. }
  161. [Test]
  162. public void AbsoluteHRef ()
  163. {
  164. TestHtmlAnchor a = new TestHtmlAnchor ();
  165. a.HRef = "http://127.0.0.1/";
  166. HtmlTextWriter writer = a.GetWriter ();
  167. Assert.AreEqual (" href=\"http://127.0.0.1/\"", writer.InnerWriter.ToString (), "#01");
  168. }
  169. #if NET_2_0
  170. [Test]
  171. [ExpectedException (typeof (NullReferenceException))] // since 2.0 RC :-(
  172. public void RaisePostBackEvent ()
  173. {
  174. TestHtmlAnchor a = new TestHtmlAnchor ();
  175. a.ServerClick += new EventHandler (ServerClick);
  176. serverClick = false;
  177. a.Raise ();
  178. Assert.IsTrue (serverClick, "ServerClick");
  179. }
  180. [Test]
  181. public void ViewState2 () {
  182. TestHtmlAnchor a = new TestHtmlAnchor ();
  183. a.TrakState ();
  184. a.CausesValidation = false;
  185. a.ValidationGroup = "VG";
  186. object s = a.SaveState ();
  187. TestHtmlAnchor copy = new TestHtmlAnchor ();
  188. copy.LoadState (s);
  189. Assert.AreEqual (false, copy.CausesValidation, "A1");
  190. Assert.AreEqual ("VG", copy.ValidationGroup, "A2");
  191. }
  192. #endif
  193. }
  194. }