[Cleanup] Removed TARGET_JVM
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / DataBoundControlTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.DataBoundControl.cs 
3 //
4 // Author:
5 //      Chris Toshok (toshok@ximian.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 #if NET_2_0
32 using NUnit.Framework;
33 using System;
34 using System.IO;
35 using System.Globalization;
36 using System.Web;
37 using System.Web.UI;
38 using System.Web.UI.Adapters;
39 using System.Web.UI.WebControls;
40 using System.Web.UI.WebControls.Adapters;
41 using System.Text;
42 using System.Collections;
43 using System.Data;
44 using MonoTests.SystemWeb.Framework;
45
46 namespace MonoTests.System.Web.UI.WebControls
47 {
48         [TestFixture]   
49         public class DataBoundControlTest {     
50                 class Poker : DataBoundControl {
51                         protected override void PerformSelect () 
52                         {
53                                 //Console.WriteLine ("PerformSelect\n{0}", Environment.StackTrace);
54                                 Assert.IsTrue (RequiresDataBinding);
55                                 base.PerformSelect ();
56                                 Assert.IsFalse (RequiresDataBinding);
57                         }
58
59                         protected internal override void PerformDataBinding (IEnumerable data)
60                         {
61                                 Assert.IsFalse (RequiresDataBinding);
62                                 base.PerformDataBinding (data);
63                         }
64
65                         public void DoValidateDataSource (object dataSource)
66                         {
67                                 ValidateDataSource (dataSource);
68                         }
69                         
70                         public bool GetInitialized ()
71                         {
72                                 return Initialized;
73                         }
74
75                         public bool GetRequiresDataBinding ()
76                         {
77                                 return RequiresDataBinding;
78                         }
79                         
80                         public void SetRequiresDataBinding (bool value)
81                         {
82                                 RequiresDataBinding = value;
83                         }
84
85                         public override void DataBind ()
86                         {
87                                 Assert.IsTrue (RequiresDataBinding);
88                                 base.DataBind ();
89                                 Assert.IsFalse (RequiresDataBinding);
90                         }
91
92                         public void DoEnsureDataBound ()
93                         {
94                                 Assert.IsTrue (RequiresDataBinding);
95                                 EnsureDataBound ();
96                                 Assert.IsFalse (RequiresDataBinding);
97                         }
98                 }
99
100                 class MyDataBoundControl : DataBoundControl
101                 {
102                         public int CreateDataSourceSelectArgumentsCalled;
103                         public DataSourceSelectArguments CreatedDataSourceSelectArguments;
104                         private StringBuilder dataBindTrace = new StringBuilder ();
105                         public string DataBindTrace {
106                                 get { return dataBindTrace.ToString (); }
107                         }
108
109                         public override void DataBind () {
110                                 dataBindTrace = new StringBuilder ();
111                                 dataBindTrace.Append ("[Start DataBind]");
112                                 base.DataBind ();
113                                 dataBindTrace.Append ("[End DataBind]");
114                         }
115
116                         protected override void PerformSelect () {
117                                 dataBindTrace.Append ("[Start PerformSelect]");
118                                 base.PerformSelect ();
119                                 dataBindTrace.Append ("[End PerformSelect]");
120                         }
121                         
122                         protected internal override void PerformDataBinding (IEnumerable data) {
123                                 dataBindTrace.Append ("[Start PerformDataBinding]");
124                                 base.PerformDataBinding (data);
125                                 dataBindTrace.Append ("[End PerformDataBinding]");
126                         }
127
128                         protected override void OnDataBinding (EventArgs e) {
129                                 dataBindTrace.Append ("[Start OnDataBinding]");
130                                 base.OnDataBinding (e);
131                                 dataBindTrace.Append ("[End OnDataBinding]");
132                         }
133
134                         protected override void OnDataBound (EventArgs e) {
135                                 dataBindTrace.Append ("[Start OnDataBound]");
136                                 base.OnDataBound (e);
137                                 dataBindTrace.Append ("[End OnDataBound]");
138                         }
139
140                         protected override DataSourceView GetData () {
141                                 dataBindTrace.Append ("[Start GetData]");
142                                 DataSourceView d = base.GetData ();
143                                 dataBindTrace.Append ("[End GetData]");
144                                 return d;
145                         }
146
147                         public DataSourceView DoGetData () {
148                                 return GetData ();
149                         }
150
151                         public IDataSource DoGetDataSource () {
152                                 return GetDataSource ();
153                         }
154
155                         public void DoEnsureDataBound () {
156                                 EnsureDataBound();
157                         }
158
159                         protected override DataSourceSelectArguments CreateDataSourceSelectArguments () {
160                                 CreateDataSourceSelectArgumentsCalled++;
161                                 CreatedDataSourceSelectArguments = base.CreateDataSourceSelectArguments ();
162                                 return CreatedDataSourceSelectArguments;
163                         }
164
165                         public DataSourceSelectArguments GetSelectArguments () {
166                                 return SelectArguments;
167                         }
168                         
169                         internal ControlAdapter controlAdapter;
170                         protected override global::System.Web.UI.Adapters.ControlAdapter ResolveAdapter ()
171                         {
172                                 return controlAdapter;
173                         }
174
175                 }
176
177                 [TestFixtureTearDown]
178                 public void Unload ()
179                 {
180                         WebTest.Unload ();
181                 }
182
183                 [Test]
184                 public void DataBoundControl_GetData () {
185                         Page p = new Page ();
186                         MyDataBoundControl dc = new MyDataBoundControl ();
187                         p.Controls.Add (dc);
188
189                         DataSourceView data = dc.DoGetData ();
190                         Assert.IsNotNull (data, "GetData");
191
192                         IDataSource dataSource = dc.DoGetDataSource ();
193                         Assert.IsNotNull (dataSource, "GetDataSource");
194                 }
195
196                 [Test]
197                 public void DataBoundControl_DataBindFlow () {
198                         Page p = new Page ();
199                         MyDataBoundControl dc = new MyDataBoundControl ();
200                         p.Controls.Add (dc);
201                         dc.DataBind ();
202                         string expected = "[Start DataBind][Start PerformSelect][Start OnDataBinding][End OnDataBinding][Start GetData][End GetData][Start PerformDataBinding][End PerformDataBinding][Start OnDataBound][End OnDataBound][End PerformSelect][End DataBind]";
203                         Assert.AreEqual (expected, dc.DataBindTrace, "DataBindFlow");
204                 }
205                 
206                 [Test]
207                 [Category ("NunitWeb")]
208                 public void DataBoundControl_DataBindFlow2 () {
209                         new WebTest (PageInvoker.CreateOnLoad (DataBoundControl_DataBindFlow2_Load)).Run ();
210                 }
211
212                 public static void DataBoundControl_DataBindFlow2_Load(Page p){
213                         MyDataBoundControl dc = new MyDataBoundControl ();
214                         p.Controls.Add (dc);
215                         dc.DataSourceID = "ObjectDataSource1";
216                         ObjectDataSource ods = new ObjectDataSource (typeof(Control).FullName, "ToString");
217                         ods.ID = "ObjectDataSource1";
218                         p.Controls.Add (ods);
219                         dc.DataBind ();
220                         string expected = "[Start DataBind][Start PerformSelect][Start GetData][End GetData][Start OnDataBinding][End OnDataBinding][Start PerformDataBinding][End PerformDataBinding][Start OnDataBound][End OnDataBound][End PerformSelect][End DataBind]";
221                         Assert.AreEqual (expected, dc.DataBindTrace, "DataBindFlow");
222                 }
223                 
224                 [Test]
225                 public void DataBoundControl_DataBindFlow3 () {
226                         Page p = new Page ();
227                         MyDataBoundControl dc = new MyDataBoundControl ();
228                         p.Controls.Add (dc);
229                         DataSourceSelectArguments arg1 = dc.GetSelectArguments ();
230                         Assert.AreEqual (1, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#1");
231                         dc.DataBind ();
232                         DataSourceSelectArguments argCreated2 = dc.CreatedDataSourceSelectArguments;
233                         DataSourceSelectArguments arg2 = dc.GetSelectArguments ();
234                         Assert.AreEqual (2, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#2");
235                         dc.DataBind ();
236                         DataSourceSelectArguments argCreated3 = dc.CreatedDataSourceSelectArguments;
237                         Assert.AreEqual (3, dc.CreateDataSourceSelectArgumentsCalled, "CreateDataSourceSelectArgumentsCalled#3");
238                         Assert.IsTrue (object.ReferenceEquals (argCreated2, arg2), "CreateDataSourceSelectArgumentsCalled#4");
239                 }
240                 
241                 [Test]
242                 public void Defaults ()
243                 {
244                         Poker p = new Poker ();
245                         Assert.AreEqual ("", p.DataMember, "A1");
246                         Assert.AreEqual ("", p.DataSourceID, "A2");
247                 }
248                 
249                 [Test]
250                 public void ValidateDataSource () {
251                         Poker p = new Poker ();
252                         // Allows null
253                         p.DoValidateDataSource (null);
254                 }
255
256                 // MSDN: The ConfirmInitState method sets the initialized state of the data-bound 
257                 // control. The method is called by the DataBoundControl class in its OnLoad method.
258                 [Test]
259                 [Category ("NunitWeb")]
260                 public void Initialized ()
261                 {
262                         WebTest t = new WebTest ();
263                         PageDelegates pd = new PageDelegates ();
264                         pd.Load = Initialized_Load;
265                         pd.PreRenderComplete = Initialized_PreRender;
266                         t.Invoker = new PageInvoker (pd);
267                         t.Run ();
268                 }
269
270                 public static void Initialized_Load (Page p)
271                 {
272                         Poker c = new Poker ();
273                         p.Form.Controls.Clear ();
274                         p.Form.Controls.Add (c);
275                         Assert.IsFalse (c.GetInitialized (), "Initialized_Load");
276                         Assert.IsFalse (c.GetRequiresDataBinding (), "RequiresDataBinding_Load");
277                 }
278
279                 public static void Initialized_PreRender (Page p)
280                 {
281                         Poker c = (Poker) p.Form.Controls [0];
282                         Assert.IsTrue (c.GetInitialized (), "Initialized_PreRender");
283                         Assert.IsTrue (c.GetRequiresDataBinding (), "RequiresDataBinding_PreRender");
284                 }
285                 
286                 [Test]
287                 [Category ("NunitWeb")]
288                 public void Initialized2 () {
289                         WebTest t = new WebTest ();
290                         PageDelegates pd = new PageDelegates ();
291                         pd.Init = Initialized2_Init;
292                         pd.Load = Initialized2_Load;
293                         pd.PreRenderComplete = Initialized2_PreRender;
294                         t.Invoker = new PageInvoker (pd);
295                         t.Run ();
296                 }
297
298                 public static void Initialized2_Init (Page p) {
299                         Poker c = new Poker ();
300                         p.Form.Controls.Clear ();
301                         p.Form.Controls.Add (c);
302                         Assert.IsFalse (c.GetInitialized (), "Initialized_Init");
303                         Assert.IsFalse (c.GetRequiresDataBinding (), "RequiresDataBinding_Init");
304                 }
305
306                 public static void Initialized2_Load (Page p) {
307                         Poker c = (Poker) p.Form.Controls [0];
308                         Assert.IsTrue (c.GetInitialized (), "Initialized_Load");
309                         Assert.IsTrue (c.GetRequiresDataBinding (), "RequiresDataBinding_Load");
310                         c.SetRequiresDataBinding (false);
311                 }
312
313                 public static void Initialized2_PreRender (Page p) {
314                         Poker c = (Poker) p.Form.Controls [0];
315                         Assert.IsTrue (c.GetInitialized (), "Initialized_PreRender");
316                         Assert.IsFalse (c.GetRequiresDataBinding (), "RequiresDataBinding_PreRender");
317                 }
318
319                 [Test]
320                 public void DataBind ()
321                 {
322                         Page p = new Page ();
323                         
324                         ObjectDataSource ods = new ObjectDataSource (typeof (Control).FullName, "ToString");
325                         ods.ID = "ObjectDataSource1";
326                         p.Controls.Add (ods);
327                         
328                         Poker c = new Poker ();
329                         c.DataSourceID = "ObjectDataSource1";
330                         c.SetRequiresDataBinding (true);
331                         p.Controls.Add (c);
332                         
333                         c.DataBind ();
334                 }
335
336                 [Test]
337                 public void EnsureDataBound ()
338                 {
339                         Page p = new Page ();
340
341                         ObjectDataSource ods = new ObjectDataSource (typeof (Control).FullName, "ToString");
342                         ods.ID = "ObjectDataSource1";
343                         p.Controls.Add (ods);
344
345                         Poker c = new Poker ();
346                         c.DataSourceID = "ObjectDataSource1";
347                         c.SetRequiresDataBinding (true);
348                         p.Controls.Add (c);
349                         
350                         c.DoEnsureDataBound ();
351                 }
352
353                 class MyControlAdapter : ControlAdapter
354                 {
355                 }
356                 
357                 class MyDataBoundControlAdapter : DataBoundControlAdapter
358                 {
359                         internal bool perform_data_binding_called;
360                         protected internal override void PerformDataBinding (IEnumerable data)
361                         {
362                                 perform_data_binding_called = true;
363                         }
364                 }
365
366                 [Test]
367                 [Category ("NotDotNet")] // Adapter binding does work on .NET but not by calling ResolveAdapter
368                 public void PerformDataBinding_UsesAdapter ()
369                 {
370                         MyDataBoundControl c = new MyDataBoundControl ();
371                         MyDataBoundControlAdapter a = new MyDataBoundControlAdapter();;
372                         c.controlAdapter = a;
373                         c.DataBind ();
374                         Assert.IsTrue (a.perform_data_binding_called, "PerformDataBinding_UsesAdapter");
375                 }
376
377                 [Test]
378                 public void PerformDataBinding_WorksWithControlAdapter ()
379                 {
380                         MyDataBoundControl c = new MyDataBoundControl ();
381                         ControlAdapter a = new MyControlAdapter();;
382                         c.controlAdapter = a;
383                         c.DataBind ();
384                 }
385         }
386 }
387 #endif