2007-01-30 Adar Wesley <adarw@mainsofot.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / LabelTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.Label.cs 
3 //
4 // Author:
5 //      Miguel de Icaza (miguel@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Globalization;
35 using System.Web;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38
39 namespace MonoTests.System.Web.UI.WebControls
40 {
41         [TestFixture]   
42         public class LabelTest {        
43                 class Poker : Label {
44                         
45                         public new void AddParsedSubObject (object o)
46                         {
47                                 base.AddParsedSubObject (o);
48                         }
49
50                         public void TrackState () 
51                         {
52                                 TrackViewState ();
53                         }
54                         
55                         public object SaveState ()
56                         {
57                                 return SaveViewState ();
58                         }
59                         
60                         public void LoadState (object o)
61                         {
62                                 LoadViewState (o);
63                         }
64                         
65                         public string Render ()
66                         {
67                                 StringWriter sw = new StringWriter ();
68                                 sw.NewLine = "\n";
69                                 HtmlTextWriter writer = new HtmlTextWriter (sw);
70                                 base.Render (writer);
71                                 return writer.InnerWriter.ToString ();
72                         }                       
73                 }
74                 
75                 [Test]
76                 public void Label_ViewState ()
77                 {
78                         Poker p = new Poker ();
79                         p.TrackState ();
80
81                         Assert.AreEqual (p.Text, "", "A1");
82                         p.Text = "Hello";
83                         Assert.AreEqual (p.Text, "Hello", "A2");
84
85                         object state = p.SaveState ();
86
87                         Poker copy = new Poker ();
88                         copy.TrackState ();
89                         copy.LoadState (state);
90                         Assert.AreEqual (copy.Text, "Hello", "A3");
91                 }
92
93                 [Test]
94                 public void Label_Render ()
95                 {
96                         Poker l = new Poker ();
97                         l.Text = "Hello";
98                         Assert.AreEqual ("<span>Hello</span>", l.Render (), "R1");
99                 }
100
101                 Poker MakeNested ()
102                 {
103                         Poker p = new Poker ();
104                         Label ll = new Label ();
105                         ll.Text = ", World";
106                         p.AddParsedSubObject (new LiteralControl ("Hello"));
107                         p.AddParsedSubObject (ll);
108                         return p;
109                 }
110                 
111                 
112                 [Test]
113                 public void ChildControl ()
114                 {
115                         Poker l = MakeNested ();
116                         Assert.AreEqual ("<span>Hello<span>, World</span></span>", l.Render ());
117                         Assert.AreEqual ("", l.Text);
118                         l.Text = "Hello";
119                         Assert.AreEqual ("<span>Hello</span>", l.Render ());
120                         Assert.AreEqual ("Hello", l.Text);
121                         Assert.IsFalse (l.HasControls ());
122                 }
123
124                 [Test]
125                 public void ChildControlViewstate ()
126                 {
127                         Poker l = MakeNested ();
128                         l.TrackState ();
129                         l.Text = "Hello";
130
131                         object o = l.SaveState ();
132                         l = MakeNested ();
133                         l.TrackState ();
134                         l.LoadState (o);
135                         
136                         Assert.AreEqual ("<span>Hello</span>", l.Render ());
137                         Assert.AreEqual ("Hello", l.Text);
138                         Assert.IsFalse (l.HasControls ());
139                 }
140
141                 [Test]
142                 public void AssocControlId ()
143                 {
144                         Page p = new Page ();
145                         Poker l = new Poker ();
146                         TextBox t = new TextBox ();
147                         t.ID = "mytxtbox";
148
149                         p.Controls.Add (l);
150                         p.Controls.Add (t);
151                         
152                         l.Text = "Hello";
153                         l.AssociatedControlID = "mytxtbox";
154                         Assert.AreEqual (@"<label for=""mytxtbox"">Hello</label>", l.Render ());                        
155                 }               
156         }
157 }
158
159