LabelTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Tests for System.Web.UI.WebControls.Label.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([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. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Globalization;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36. namespace MonoTests.System.Web.UI.WebControls
  37. {
  38. [TestFixture]
  39. public class LabelTest {
  40. class Poker : Label {
  41. public new void AddParsedSubObject (object o)
  42. {
  43. base.AddParsedSubObject (o);
  44. }
  45. public void TrackState ()
  46. {
  47. TrackViewState ();
  48. }
  49. public object SaveState ()
  50. {
  51. return SaveViewState ();
  52. }
  53. public void LoadState (object o)
  54. {
  55. LoadViewState (o);
  56. }
  57. public string Render ()
  58. {
  59. StringWriter sw = new StringWriter ();
  60. sw.NewLine = "\n";
  61. HtmlTextWriter writer = new HtmlTextWriter (sw);
  62. base.Render (writer);
  63. return writer.InnerWriter.ToString ();
  64. }
  65. }
  66. [Test]
  67. public void Label_ViewState ()
  68. {
  69. Poker p = new Poker ();
  70. p.TrackState ();
  71. Assert.AreEqual (p.Text, "", "A1");
  72. p.Text = "Hello";
  73. Assert.AreEqual (p.Text, "Hello", "A2");
  74. object state = p.SaveState ();
  75. Poker copy = new Poker ();
  76. copy.TrackState ();
  77. copy.LoadState (state);
  78. Assert.AreEqual (copy.Text, "Hello", "A3");
  79. }
  80. [Test]
  81. public void Label_Render ()
  82. {
  83. Poker l = new Poker ();
  84. l.Text = "Hello";
  85. Assert.AreEqual ("<span>Hello</span>", l.Render (), "R1");
  86. }
  87. Poker MakeNested ()
  88. {
  89. Poker p = new Poker ();
  90. Label ll = new Label ();
  91. ll.Text = ", World";
  92. p.AddParsedSubObject (new LiteralControl ("Hello"));
  93. p.AddParsedSubObject (ll);
  94. return p;
  95. }
  96. [Test]
  97. public void ChildControl ()
  98. {
  99. Poker l = MakeNested ();
  100. Assert.AreEqual ("<span>Hello<span>, World</span></span>", l.Render ());
  101. Assert.AreEqual ("", l.Text);
  102. l.Text = "Hello";
  103. Assert.AreEqual ("<span>Hello</span>", l.Render ());
  104. Assert.AreEqual ("Hello", l.Text);
  105. Assert.IsFalse (l.HasControls ());
  106. }
  107. [Test]
  108. public void ChildControlViewstate ()
  109. {
  110. Poker l = MakeNested ();
  111. l.TrackState ();
  112. l.Text = "Hello";
  113. object o = l.SaveState ();
  114. l = MakeNested ();
  115. l.TrackState ();
  116. l.LoadState (o);
  117. Assert.AreEqual ("<span>Hello</span>", l.Render ());
  118. Assert.AreEqual ("Hello", l.Text);
  119. Assert.IsFalse (l.HasControls ());
  120. }
  121. [Test]
  122. public void AssocControlId ()
  123. {
  124. Page p = new Page ();
  125. Poker l = new Poker ();
  126. TextBox t = new TextBox ();
  127. t.ID = "mytxtbox";
  128. p.Controls.Add (l);
  129. p.Controls.Add (t);
  130. l.Text = "Hello";
  131. l.AssociatedControlID = "mytxtbox";
  132. Assert.AreEqual (@"<label for=""mytxtbox"">Hello</label>", l.Render ());
  133. }
  134. }
  135. }