HtmlAnchorTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. }
  50. [TestFixture]
  51. public class HtmlAnchorTest {
  52. [Test]
  53. public void DefaultProperties ()
  54. {
  55. HtmlAnchor a = new HtmlAnchor ();
  56. Assert.AreEqual (0, a.Attributes.Count, "Attributes.Count");
  57. Assert.AreEqual (String.Empty, a.HRef, "HRef");
  58. Assert.AreEqual (String.Empty, a.Name, "Name");
  59. Assert.AreEqual (String.Empty, a.Target, "Target");
  60. Assert.AreEqual (String.Empty, a.Title, "Title");
  61. Assert.AreEqual ("a", a.TagName, "TagName");
  62. }
  63. [Test]
  64. public void NullProperties ()
  65. {
  66. HtmlAnchor a = new HtmlAnchor ();
  67. a.HRef = null;
  68. Assert.AreEqual (String.Empty, a.HRef, "HRef");
  69. a.Name = null;
  70. Assert.AreEqual (String.Empty, a.Name, "Name");
  71. a.Target = null;
  72. Assert.AreEqual (String.Empty, a.Target, "Target");
  73. a.Title = null;
  74. Assert.AreEqual (String.Empty, a.Title, "Title");
  75. Assert.AreEqual (0, a.Attributes.Count, "Attributes.Count");
  76. }
  77. [Test]
  78. public void Target ()
  79. {
  80. HtmlAnchor a = new HtmlAnchor ();
  81. // specials (various casing)
  82. a.Target = "_blank";
  83. Assert.AreEqual ("_blank", a.Target, "_blank");
  84. a.Target = "_parent";
  85. Assert.AreEqual ("_parent", a.Target, "_parent");
  86. a.Target = "_SELF";
  87. Assert.AreEqual ("_SELF", a.Target, "_SELF");
  88. a.Target = "_ToP";
  89. Assert.AreEqual ("_ToP", a.Target, "_ToP");
  90. // alpha
  91. a.Target = "a";
  92. Assert.AreEqual ("a", a.Target, "a");
  93. a.Target = "blank";
  94. Assert.AreEqual ("blank", a.Target, "blank");
  95. a.Target = "Z";
  96. Assert.AreEqual ("Z", a.Target, "Z");
  97. }
  98. [Test]
  99. public void Target_Invalid ()
  100. {
  101. HtmlAnchor a = new HtmlAnchor ();
  102. // non alpha
  103. a.Target = "9a";
  104. Assert.AreEqual ("9a", a.Target, "9a");
  105. // non special
  106. a.Target = "_mono";
  107. Assert.AreEqual ("_mono", a.Target, "_mono");
  108. }
  109. [Test]
  110. public void HRef ()
  111. {
  112. TestHtmlAnchor a = new TestHtmlAnchor ();
  113. a.HRef = "~/otherfile.txt";
  114. Assert.AreEqual ("~/otherfile.txt", a.HRef, "HRef");
  115. // resolve doesn't apply on the property
  116. }
  117. [Test]
  118. public void RenderAttributes ()
  119. {
  120. TestHtmlAnchor a = new TestHtmlAnchor ();
  121. a.HRef = "*1*";
  122. a.Name = "*2*";
  123. a.Target = "*3*";
  124. a.Title = "*4*";
  125. Assert.AreEqual (4, a.Attributes.Count, "Attributes.Count/4");
  126. HtmlTextWriter writer = a.GetWriter ();
  127. Assert.AreEqual (" href=\"*1*\" name=\"*2*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "attributes");
  128. // HRef is missing, from the attributes collection, after rendering
  129. Assert.AreEqual (3, a.Attributes.Count, "Attributes.Count/3");
  130. Assert.AreEqual (String.Empty, a.HRef, "HRef");
  131. // but href is still rendered
  132. Assert.AreEqual (" href=\"*1*\" name=\"*2*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "HRef is back");
  133. }
  134. private bool serverClick;
  135. private void ServerClick (object sender, EventArgs e)
  136. {
  137. serverClick = true;
  138. }
  139. [Test]
  140. #if NET_2_0
  141. [ExpectedException (typeof (NullReferenceException))] // since 2.0 RC :-(
  142. #endif
  143. public void IPostBackEventHandler_RaisePostBackEvent ()
  144. {
  145. TestHtmlAnchor a = new TestHtmlAnchor ();
  146. a.ServerClick += new EventHandler (ServerClick);
  147. IPostBackEventHandler pbeh = (a as IPostBackEventHandler);
  148. serverClick = false;
  149. pbeh.RaisePostBackEvent ("mono");
  150. Assert.IsTrue (serverClick, "ServerClick");
  151. }
  152. [Test]
  153. public void AbsoluteHRef ()
  154. {
  155. TestHtmlAnchor a = new TestHtmlAnchor ();
  156. a.HRef = "http://127.0.0.1/";
  157. HtmlTextWriter writer = a.GetWriter ();
  158. Assert.AreEqual (" href=\"http://127.0.0.1/\"", writer.InnerWriter.ToString (), "#01");
  159. }
  160. #if NET_2_0
  161. [Test]
  162. [ExpectedException (typeof (NullReferenceException))] // since 2.0 RC :-(
  163. public void RaisePostBackEvent ()
  164. {
  165. TestHtmlAnchor a = new TestHtmlAnchor ();
  166. a.ServerClick += new EventHandler (ServerClick);
  167. serverClick = false;
  168. a.Raise ();
  169. Assert.IsTrue (serverClick, "ServerClick");
  170. }
  171. #endif
  172. }
  173. }