2009-11-16 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / RepeaterTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.Repeater.cs 
3 //
4 // Author:
5 //      Chris Toshok (toshok@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 using MonoTests.SystemWeb.Framework;
39 using System.Collections;
40
41 namespace MonoTests.System.Web.UI.WebControls
42 {
43         [TestFixture]   
44         public class RepeaterTest {     
45                 class Poker : Repeater {
46                         
47                         public void TrackState () 
48                         {
49                                 TrackViewState ();
50                         }
51                         
52                         public object SaveState ()
53                         {
54                                 return SaveViewState ();
55                         }
56                         
57                         public void LoadState (object o)
58                         {
59                                 LoadViewState (o);
60                         }
61
62 #if NET_2_0
63                         public DataSourceSelectArguments GetSelectArguments()
64                         {
65                                 return SelectArguments;
66                         }
67
68                         public DataSourceSelectArguments DoCreateDataSourceSelectArguments ()
69                         {
70                                 return base.CreateDataSourceSelectArguments();
71                         }
72
73                         public new void EnsureDataBound ()
74                         {
75                                 base.EnsureDataBound ();
76                         }
77
78                         public global::System.Collections.IEnumerable DoGetData ()
79                         {
80                                 return base.GetData ();
81                         }
82
83                         public new bool Initialized
84                         {
85                                 get { return base.Initialized; }
86                         }
87
88                         public new bool IsBoundUsingDataSourceID
89                         {
90                                 get { return base.IsBoundUsingDataSourceID; }
91                         }
92
93                         protected override void OnDataPropertyChanged ()
94                         {
95                                 eventChecker = true;
96                                 base.OnDataPropertyChanged ();
97                         }
98
99                         public void DoOnDataSourceViewChanged (object sender, EventArgs e)
100                         {
101                                 base.OnDataSourceViewChanged (sender, e);
102                         }
103
104                         public new bool RequiresDataBinding
105                         {
106                                 get { return base.RequiresDataBinding; }
107                                 set { base.RequiresDataBinding = value; }
108                         }
109
110                         bool eventChecker;
111                         public bool EventChecker
112                         {
113                                 get { return eventChecker; }
114                                 set { throw new NotImplementedException (); }
115                         }
116
117                         public void clearEvents ()
118                         {
119                                 eventChecker = false;
120                         }
121 #endif
122                 }
123
124 #if NET_2_0
125                 [Test]
126                 public void Repeater_DefaultsSelectArguments ()
127                 {
128                         Poker p = new Poker ();
129                         DataSourceSelectArguments args, args2;
130
131                         args = p.GetSelectArguments();
132                         args2 = p.DoCreateDataSourceSelectArguments();
133
134                         Assert.AreEqual (args, args2, "call == prop");
135
136                         Assert.IsNotNull (args, "property null check");
137                         Assert.IsTrue    (args != DataSourceSelectArguments.Empty, "property != Empty check");
138                         Assert.IsTrue    (args.Equals (DataSourceSelectArguments.Empty), "property but they are empty check");
139
140                         Assert.IsNotNull (args2, "method null check");
141                         Assert.IsTrue    (args2 != DataSourceSelectArguments.Empty, "method != Empty check");
142                         Assert.IsTrue    (args2.Equals (DataSourceSelectArguments.Empty), "method but they are empty check");
143
144                         /* check to see whether multiple calls give us different refs */
145                         args = p.DoCreateDataSourceSelectArguments();
146                         
147                         Assert.AreEqual (args, args2, "multiple calls, same ref");
148                         Assert.AreEqual (string.Empty, p.DataSourceID, "DataSourceID");
149                         Assert.AreEqual (false, p.RequiresDataBinding, "RequiresDataBinding");
150                 }
151
152                 [Test]
153                 public void Repeater_Defaults ()
154                 {
155                         Poker p = new Poker ();
156                         Assert.AreEqual (true, p.EnableTheming, "EnableTheming");
157                 }
158
159
160                 
161                 [Test]
162                 [Category("NunitWeb")]
163                 [ExpectedException(typeof(HttpException))]
164                 public void  EnsureDataBound ()
165                 {
166                         WebTest t = new WebTest (PageInvoker.CreateOnInit (EnsureDataBound_Init));
167                         string html = t.Run ();
168                 }
169
170                 public static void EnsureDataBound_Init (Page p)
171                 {
172                         Poker r = new Poker ();
173                         r.DataSourceID = "Fake";
174                         p.Form.Controls.Add (r);
175                         r.EnsureDataBound ();
176                 }
177
178                 [Test]
179                 public void GetData ()
180                 {
181                         Poker p = new Poker ();
182                         p.DataSource = Databound ();
183                         ArrayList data = (ArrayList)p.DoGetData ();
184                         Assert.AreEqual (3, data.Count, "GetData#1");
185                         Assert.AreEqual (1, data[0], "GetData#2");
186                 }
187
188                 #region help_class
189                 static ArrayList Databound ()
190                 {
191                         ArrayList list = new ArrayList ();
192                         list.Add (1);
193                         list.Add (2);
194                         list.Add (3);
195                         return list;
196                 }
197                 #endregion
198
199                 [Test]
200                 [Category ("NunitWeb")]
201                 public void Initialized ()
202                 {
203                         WebTest t = new WebTest ();
204                         PageDelegates pd = new PageDelegates ();
205                         pd.Init = new PageDelegate (Initialized_Init);
206                         pd.Load = new PageDelegate (Initialized_Load);
207                         t.Invoker = new PageInvoker (pd);
208                         string html = t.Run ();
209                 }
210
211                 public static void Initialized_Init (Page p)
212                 {
213                         Poker r = new Poker ();
214                         r.ID = "Rep";
215                         Assert.AreEqual (false, r.Initialized, "Initialized#1");
216                         r.DataSource = Databound ();
217                         p.Form.Controls.Add (r);
218                 }
219
220                 public static void Initialized_Load (Page p)
221                 {
222                         Poker r = p.FindControl ("Rep") as Poker; 
223                         Assert.AreEqual (true, r.Initialized, "Initialized#2");
224                 }
225
226                 [Test]
227                 public void IsBoundUsingDataSourceID ()
228                 {
229                         Poker p = new Poker ();
230                         Assert.AreEqual (false, p.IsBoundUsingDataSourceID, "IsBoundUsingDataSourceID#1");
231                         p.DataSourceID = "Fake";
232                         Assert.AreEqual (true, p.IsBoundUsingDataSourceID, "IsBoundUsingDataSourceID#2");
233                 }
234
235                 [Test]
236                 public void OnDataPropertyChanged ()
237                 {
238                         Poker p = new Poker ();
239                         p.clearEvents ();
240                         p.DataSourceID = "Fake";
241                         Assert.AreEqual (true, p.EventChecker, "OnDataPropertyChanged#1");
242                 }
243
244                 [Test]
245                 public void OnDataSourceViewChanged ()
246                 {
247                         Poker p = new Poker ();
248                         Assert.AreEqual (false, p.RequiresDataBinding, "OnDataSourceViewChanged#1");
249                         p.DoOnDataSourceViewChanged (p, new EventArgs ());
250                         Assert.AreEqual (true, p.RequiresDataBinding, "OnDataSourceViewChanged#2");
251                 }
252
253                 #region help_class_for_select_args
254                 class PokerS : Repeater
255                 {
256
257                         public void TrackState ()
258                         {
259                                 TrackViewState ();
260                         }
261
262                         public object SaveState ()
263                         {
264                                 return SaveViewState ();
265                         }
266
267                         public void LoadState (object o)
268                         {
269                                 LoadViewState (o);
270                         }
271
272 #if NET_2_0
273                         public DataSourceSelectArguments GetSelectArguments ()
274                         {
275                                 return SelectArguments;
276                         }
277
278                         protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
279                         {
280                                 DataSourceSelectArguments arg = new DataSourceSelectArguments ("SortExp");
281                                 return arg;
282                         }
283 #endif
284                 }
285                 #endregion
286
287                 [Test]
288                 public void GetSelectArguments ()
289                 {
290                         PokerS p = new PokerS ();
291                         DataSourceSelectArguments arg = p.GetSelectArguments ();
292                         Assert.AreEqual ("SortExp", arg.SortExpression, "GetSelectArguments");
293                 }
294
295                 [TestFixtureTearDown]
296                 public void TearDown ()
297                 {
298                         WebTest.Unload ();
299                 }
300 #endif
301         }
302 }