2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls.Adapters / MenuAdapterTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.Adapters.MenuAdapter
3 //
4 // Author:
5 //      Dean Brettle (dean@brettle.com)
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30 using NUnit.Framework;
31 using System;
32 using System.Collections;
33 using System.Drawing;
34 using System.IO;
35 using System.Globalization;
36 using System.Web;
37 using System.Web.UI;
38 using System.Web.UI.WebControls;
39 using System.Web.UI.WebControls.Adapters;
40 using System.Web.Configuration;
41 using MonoTests.SystemWeb.Framework;
42
43
44 namespace MonoTests.System.Web.UI.WebControls.Adapters
45 {
46         [TestFixture]
47         public class MenuAdapterTest
48         {
49                 MyMenu c;
50                 MyMenuAdapter a;
51                 StringWriter sw;
52                 HtmlTextWriter w;
53                 Page p;
54                 EventArgs e;
55
56                 [SetUp]
57                 public void SetUp ()
58                 {
59                         p = new Page ();
60                         c = new MyMenu ();
61                         a = new MyMenuAdapter (c);
62                         p.Controls.Add(c);
63                         sw = new StringWriter ();
64                         w = new HtmlTextWriter (sw);
65                         e = new EventArgs();
66                 }
67                 
68                 [Test]
69                 public void OnInit ()
70                 {
71                         a.OnInit (e);
72                         Assert.IsTrue (p.RequiresControlState (c), "OnInit #1");
73                         Assert.AreEqual (e, c.on_init_arg, "OnInit #2");
74                 }
75
76                 [Test]
77                 public void OnPreRender ()
78                 {
79                         a.OnPreRender (e);
80                         Assert.AreEqual (e, c.on_pre_render_arg, "OnPreRender #1");
81                 }
82
83                 [Test]
84                 public void RaisePostBackEvent ()
85                 {
86                         ((IPostBackEventHandler)a).RaisePostBackEvent ("eventArg");
87                         Assert.AreEqual ("eventArg", c.raise_post_back_event_arg, "RaisePostBackEvent #1");
88                 }
89                 
90                 [Test]
91                 public void RenderBeginTag ()
92                 {
93                         a.RenderBeginTag (w);
94                         Assert.AreEqual ("RenderBeginTag\n", sw.ToString (), "RenderBeginTag #1");
95                 }
96
97                 [Test]
98                 public void RenderContentsTag ()
99                 {
100                         a.RenderContents (w);
101                         Assert.AreEqual ("RenderContents\n", sw.ToString (), "RenderContents #1");
102                 }
103
104                 [Test]
105                 public void RenderEndTag ()
106                 {
107                         a.RenderEndTag (w);
108                         Assert.AreEqual ("RenderEndTag\n", sw.ToString (), "RenderEndTag #1");
109                 }
110
111                 [Test]
112                 public void RenderItem ()
113                 {
114                         MenuItem item = new MenuItem("menu item text");
115
116                         // This has to stay to work around event validation errors. If it's removed,
117                         // then RenderItem will eventually attempt to register for event validation,
118                         // which can only be done during the Render phase.
119                         item.NavigateUrl = "http://google.com/";
120                         a.RenderItem (w, item, 0);
121                         Assert.IsTrue (sw.ToString ().IndexOf("menu item text") != -1, "RenderItem #1");
122                 }
123
124                 [Test]
125                 public void Control ()
126                 {
127                         Assert.AreEqual (c, a.Control, "Control #1");
128                 }
129                 
130
131
132 #region Support classes
133                 
134                 class MyMenu : Menu
135                 {
136                 
137                         internal EventArgs on_init_arg;
138                         protected internal override void OnInit (EventArgs e)
139                         {
140                                 on_init_arg = e;
141                                 base.OnInit (e);
142                         }
143                         
144                         internal EventArgs on_pre_render_arg;
145                         protected internal override void OnPreRender (EventArgs e)
146                         {
147                                 on_pre_render_arg = e;
148                                 base.OnPreRender (e);
149                         }
150                         
151                         internal string raise_post_back_event_arg;
152                         protected internal override void RaisePostBackEvent (string eventArgument)
153                         {
154                                 raise_post_back_event_arg = eventArgument;
155                         }
156
157                         public override void RenderBeginTag (HtmlTextWriter w)
158                         {
159                                 w.WriteLine("RenderBeginTag");
160                         }
161
162                         protected internal override void RenderContents (HtmlTextWriter w)
163                         {
164                                 w.WriteLine("RenderContents");
165                         }
166
167                         public override void RenderEndTag (HtmlTextWriter w)
168                         {
169                                 w.WriteLine("RenderEndTag");
170                         }
171
172                 }
173
174                 class MyMenuAdapter : MenuAdapter
175                 {
176                         internal MyMenuAdapter (Menu c) : base (c)
177                         {
178                         }
179                                                                         
180                         new internal void RenderBeginTag (HtmlTextWriter w)
181                         {
182                                 base.RenderBeginTag (w);
183                         }
184
185                         new internal void RenderContents (HtmlTextWriter w)
186                         {
187                                 base.RenderContents (w);
188                         }
189
190                         new internal void RenderEndTag (HtmlTextWriter w)
191                         {
192                                 base.RenderEndTag (w);
193                         }
194
195                         new internal Menu Control {
196                                 get { return base.Control; }
197                         }
198                 }
199                 
200 #endregion
201         }
202 }
203 #endif