* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / Test / mainsoft / MainsoftWebApp / utils / GHTControlBase.cs
1 //
2 // Authors:
3 //   Rafael Mizrahi   <rafim@mainsoft.com>
4 //   Erez Lotan       <erezl@mainsoft.com>
5 //   Vladimir Krasnov <vladimirk@mainsoft.com>
6 //   
7 // 
8 // Copyright (c) 2002-2005 Mainsoft Corporation.
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 System;
30 using System.Web.UI;
31 using System.Web.UI.HtmlControls;
32 using System.Web.UI.WebControls;
33 using System.Collections;
34 using System.Data;
35
36 namespace GHTTests
37 {
38         /// <summary>
39         /// Provides basic functionalities for testing System.Web.UI.Control derived classes.
40         /// </summary>
41         public class GHTControlBase : GHTBaseWeb
42         {
43                 #region "Data members"
44                 protected Control m_cToTest;                            //The control that is currently being tested.
45                 protected TextBox m_tbToValidate;               //will be used by validation controls as the control to validate.
46                 protected Item[] m_aDataSource;                 //Array data source to use in data bound objects.
47                 protected DataTable m_dtDataSource;//DataTable data source to use in data bound objects.
48                 protected  ArrayList m_derivedTypes;    //The array that wil contain all types that are derived from Control, and need to be tested.
49                 protected long m_controlsCounter;               //Used to generate a unique id for each of the controls created using GHTActiveSubTestControlClone
50                 #endregion
51
52                 #region "Construction"
53                 /// <summary>
54                 /// Default c'tor.
55                 /// handles basic initialization of the page, and contents.
56                 /// </summary>
57                 public GHTControlBase()
58                 {
59                         InitTypes();
60                         InitDataSource();
61                         InitTbToValidate();
62                         m_controlsCounter = 0;
63                 }
64                 #endregion
65
66                 #region "Properties"
67                 protected Control TestedControl
68                 {
69                         get
70                         {
71                                 return m_cToTest;
72                         }
73                 }
74                 public  Type[] TypesToTest
75                 {
76                         get
77                         {
78                                 return (System.Type[])(m_derivedTypes.ToArray(typeof(System.Type)));
79                         }
80                 }
81                 #endregion
82
83                 #region "Methods"
84                 /// <summary>
85                 /// Initializes all the derived types that need to be tested.
86                 /// </summary>
87                 protected virtual  void InitTypes()
88                 {
89                         m_derivedTypes = new ArrayList();
90
91                         //System.Web.UI:
92                         //                              typeof(System.Web.UI.PartialCachingControl),                                            Excluded from test.
93                         //                              typeof(System.Web.UI.StaticPartialCachingControl),              Excluded from test.
94                         //                              typeof(System.Web.UI.DataBoundLiteralControl),                          Excluded from test.
95                         m_derivedTypes.Add(typeof(System.Web.UI.LiteralControl));
96                         m_derivedTypes.Add(typeof(System.Web.UI.Page));
97                         m_derivedTypes.Add(typeof(System.Web.UI.UserControl));
98
99                         //System.Web.UI.HtmlControls:
100                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlAnchor));
101                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlButton));
102                         //                              typeof(System.Web.UI.HtmlControls.HtmlForm),                            Excluded from test.
103                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlGenericControl));
104                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlImage));
105                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputButton));
106                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputCheckBox));
107                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputFile));
108                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputHidden));
109                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputImage));
110                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputRadioButton));
111                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputText));
112                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlSelect));
113                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlTable));
114                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlTableCell));
115                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlTableRow));
116                         m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlTextArea));
117
118                         //System.Web.UI.WebControls basic:
119                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Button));
120                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.CheckBox));
121                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.HyperLink));
122                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Image));
123                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.ImageButton));
124                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Label));
125                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.LinkButton));
126                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Literal));
127                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Panel));
128                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.PlaceHolder));
129                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RadioButton));
130                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.TextBox));
131
132                         //System.Web.UI.WebControls basic list controls:
133                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DropDownList));
134                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.ListBox));
135                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RadioButtonList));
136                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.CheckBoxList));
137
138                         //System.Web.UI.WebControls validation controls:
139 #if KNOWN_BUG //BUG_NUM:935
140                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.CompareValidator));
141                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.CustomValidator));
142                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RangeValidator));
143                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RegularExpressionValidator));
144                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RequiredFieldValidator));
145 #endif
146 #if KNOWN_BUG //BUG_NUM:1195
147                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.ValidationSummary));
148 #endif
149                         //System.Web.UI.WebControls rich controls (currently not supported):
150 //                      m_derivedTypes.Add(typeof(System.Web.UI.WebControls.AdRotator));
151 //                      m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Calendar));
152
153                         //System.Web.UI.WebControls advanced list controls:
154                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DataGrid));
155                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DataGridItem));
156                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DataList));
157                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DataListItem));
158                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Repeater));
159                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RepeaterItem));
160                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Table));
161                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.TableCell));
162                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.TableHeaderCell));
163                         m_derivedTypes.Add(typeof(System.Web.UI.WebControls.TableRow));
164                         //m_derivedTypes.Add(typeof( System.Web.UI.WebControls.Xml));
165                 }
166
167                 /// <summary>
168                 /// Adds a control to page.
169                 /// If the control is in the context of other control (Have a parent) 
170                 /// (e.g.) TableCell, then the parent control is added to the page.
171                 /// </summary>
172                 /// <param name="a_toAdd">The control to add to the page.</param>
173                 protected void GHTAddToActiveForm(Control a_toAdd)
174                 {
175                         if (a_toAdd.Parent == null)
176                         {
177                                 GHTActiveForm.Controls.Add(a_toAdd);
178                         }
179                         else
180                         {
181                                 GHTAddToActiveForm(a_toAdd.Parent);
182                         }
183                 }
184                 
185
186                 /// <summary>
187                 /// Creates a control to test, and adds it to a new subtest.
188                 /// </summary>
189                 /// <param name="ctrlType">Type of control to test.</param>
190                 /// <param name="description">description to add to subtest.</param>
191                 protected void GHTSubTestBegin(Type ctrlType, string description)
192                 {
193                         GHTSubTestBegin(ctrlType, description, true);
194                 }
195
196                 /// <summary>
197                 /// Creates a new subtest, and a control to test.
198                 /// </summary>
199                 /// <param name="ctrlType">Type of control to test.</param>
200                 /// <param name="description">description to add to subtest.</param>
201                 /// <param name="a_AddToPage">Whether to add the control to the subtest continer.</param>
202                 protected void GHTSubTestBegin(Type a_ctrlType, string description, bool a_addToPage)
203                 {
204                         base.GHTSubTestBegin(a_ctrlType.ToString() + ": " + description);
205                         GHTActiveSubtestControlClone(a_ctrlType, a_addToPage);
206                 }
207                 /// <summary>
208                 /// Clones a control within its context, and adds it to the active subtest.
209                 /// e.g. if the control is a table cell, then a table will be created to wrap the table cell.
210                 /// The table will be added to the active subtest, and the cell will be referened by m_m_cToTest.
211                 /// </summary>
212                 /// <param name="a_ctrlType">Type of control to test.</param>
213                 /// <param name="a_addToPage">Whether to add the control to the subtest continer.</param>
214                 protected void GHTActiveSubtestControlClone(Type a_ctrlType, bool a_addToPage)
215                 {
216                         if (IsListControlDerived(a_ctrlType))
217                         {
218                                 m_cToTest = GetListControlDerived(a_ctrlType, a_addToPage);
219                         }
220                         else if (IsIterativeControlControl(a_ctrlType))
221                         {
222                                 m_cToTest = GetIterativeControl(a_ctrlType, a_addToPage);
223                         }
224                         else if (IsIterativeControlControlItem(a_ctrlType))
225                         {
226                                 m_cToTest = GetIterativeControlItem(a_ctrlType, a_addToPage);
227                         }
228                         else if (IsTableRelated(a_ctrlType))
229                         {
230                                 m_cToTest = GetTableRelated(a_ctrlType, a_addToPage);
231                         }
232                         else if (IsHTMLTableRelated(a_ctrlType))
233                         {
234                                 m_cToTest = GetHtmlTableRelated(a_ctrlType, a_addToPage);
235                         }
236                         else
237                         {
238                                 m_cToTest = (Control)GHTElementClone(a_ctrlType);
239                                 if (a_addToPage)
240                                 {
241                                         GHTActiveSubTest.Controls.Add(m_cToTest);
242                                 }
243                         }
244                         HandleValidationControls();     
245                         HandleCausesValidation();
246                         SetId();        //Set a unique id to the control.
247                 }
248
249                 /// <summary>
250                 /// checks if a given type is derived from ListControl, and thus should be bound to data in order to display content.
251                 /// </summary>
252                 /// <param name="ctrlType">The type to test.</param>
253                 /// <returns>True if the ctrlType is derived from ListControl, otherwise false.</returns>
254                 private bool IsListControlDerived(Type ctrlType)
255                 {
256                         return ctrlType.IsSubclassOf(typeof(ListControl));
257                 }
258
259                 /// <summary>
260                 /// Creates a control of the specified type, if it is a ListControl derived, then bounds it to data source.
261                 /// </summary>
262                 /// <param name="ctrlType">Type of control to create.</param>
263                 private Control GetListControlDerived(Type ctrlType, bool a_AddToPage)
264                 {
265                         ListControl l_listcontrol = GHTElementClone(ctrlType) as ListControl;
266                         if (l_listcontrol != null)
267                         {
268                                 l_listcontrol.DataSource = m_aDataSource;
269                                 l_listcontrol.DataTextField = "Description";
270                                 l_listcontrol.DataValueField = "Id";
271                                 l_listcontrol.DataBind();
272                                 if (a_AddToPage)
273                                 {
274                                         GHTActiveSubTest.Controls.Add(l_listcontrol);
275                                 }
276                                 return l_listcontrol;
277                         }
278                         else
279                         {
280                                 throw new ArgumentOutOfRangeException("ctrlType", ctrlType, "Must be a ListControl derived type.");
281                         }
282                 }
283
284                 /// <summary>
285                 /// checks if a given type is an item of an iterative control, and thus should be checked in tis controls context.
286                 /// </summary>
287                 /// <param name="ctrlType">The type to test.</param>
288                 /// <returns>True if the ctrlType is an iteative control item, otherwise false.</returns>
289                 private bool IsIterativeControlControlItem(Type ctrlType)
290                 {
291                         if ( ctrlType.Equals(typeof(RepeaterItem)))
292                         {
293                                 return true;
294                         }
295                         else if (ctrlType.Equals(typeof(DataListItem)))
296                         {
297                                 return true;
298                         }
299                         else if (ctrlType.Equals(typeof(DataGridItem)))
300                         {
301                                 return true;
302                         }
303                         else
304                         {
305                                 return false;
306                         }
307                 }
308
309                 /// <summary>
310                 /// Creates an iterative control Item, included inside an iterative control..
311                 /// </summary>
312                 /// <param name="ctrlType">Type of control to create.</param>
313                 private Control GetIterativeControlItem(Type ctrlType, bool a_AddToPage)
314                 {
315                         if (ctrlType.Equals(typeof(RepeaterItem)))
316                         {
317                                 Repeater l_rep = GetIterativeControl(typeof(Repeater), a_AddToPage) as Repeater;
318                                 SetId(l_rep);
319                                 return l_rep.Items[0];
320                         }
321
322                         if (ctrlType.Equals(typeof(DataListItem)))
323                         {
324                                 DataList l_datalist = GetIterativeControl(typeof(DataList), a_AddToPage) as DataList;
325                                 SetId(l_datalist);
326                                 return l_datalist.Items[0];
327                         }
328                 
329                         if (ctrlType.Equals(typeof(DataGridItem)))
330                         {
331                                 DataGrid l_datagrid = GetIterativeControl(typeof(DataGrid), a_AddToPage) as DataGrid;
332                                 SetId(l_datagrid);
333                                 return l_datagrid.Items[0];
334                         }
335                         else
336                         {
337                                 throw new ArgumentOutOfRangeException("ctrlType", ctrlType, "Allowed types are RepeaterItem, DataListItem or DataGridItem");
338                         }
339                 }
340
341                 /// <summary>
342                 /// Checks if a given type is an iterative control, and thus should be boud to data.
343                 /// </summary>
344                 /// <param name="ctrlType">The type to test.</param>
345                 /// <returns>True if the ctrlType is an iteative control, otherwise false.</returns>
346                 private bool IsIterativeControlControl(Type ctrlType)
347                 {
348                         if ( ctrlType.Equals(typeof(Repeater)))
349                         {
350                                 return true;
351                         }
352                         else if (ctrlType.Equals(typeof(DataList)))
353                         {
354                                 return true;
355                         }
356                         else if (ctrlType.Equals(typeof(DataGrid)))
357                         {
358                                 return true;
359                         }
360                         else
361                         {
362                                 return false;
363                         }
364                 }
365
366                 /// <summary>
367                 /// Creates an iterative control.
368                 /// </summary>
369                 /// <param name="ctrlType">Type of control to create.</param>
370                 protected Control GetIterativeControl(Type ctrlType, bool a_AddToPage)
371                 {
372                         if (ctrlType.Equals(typeof(Repeater)))
373                         {
374                                         Repeater l_rep = new Repeater();
375                                         l_rep.ItemTemplate = new RepeaterTemplate();
376                                         l_rep.DataSource = m_aDataSource;
377                                         l_rep.DataBind();
378                                         if (a_AddToPage)
379                                         {
380                                                 GHTActiveSubTest.Controls.Add(l_rep);
381                                         }
382                                         return l_rep;
383                         }
384                         else if (ctrlType.Equals(typeof(DataList)))
385                         {
386                                 DataList l_dataList = new DataList();
387                                 l_dataList.ItemTemplate =  new DataListTemplate();
388                                 l_dataList.DataSource = m_aDataSource;
389                                 l_dataList.DataBind();
390                                 if (a_AddToPage)
391                                 {
392                                         GHTActiveSubTest.Controls.Add(l_dataList);
393                                 }
394                                 return l_dataList;
395                         }
396                         else if (ctrlType.Equals(typeof(DataGrid)))
397                         {
398                                 DataGrid l_dataGrid = new DataGrid();
399                                 l_dataGrid.DataSource = m_dtDataSource;
400                                 l_dataGrid.DataBind();
401                                 if (a_AddToPage)
402                                 {
403                                         GHTActiveSubTest.Controls.Add(l_dataGrid);
404                                 }
405                                 return l_dataGrid;
406                         }
407                         else
408                         {
409                                 throw new ArgumentOutOfRangeException("ctrlType", ctrlType, "Allowed types are Repeater, DataList or DataGrid");
410                         }
411                 }
412
413                 /// <summary>
414                 /// checks if a given type is a control that should be tested in the context of a HtmlTable.
415                 /// </summary>
416                 /// <param name="ctrlType">The type to test.</param>
417                 /// <returns>True if the ctrlType is a HtmlTable related type, otherwise false.</returns>
418                 private bool IsHTMLTableRelated(Type ctrlType)
419                 {
420                         if ( ctrlType.Equals(typeof(HtmlTable)))
421                         {
422                                 return true;
423                         }
424                         else if (ctrlType.Equals(typeof(HtmlTableRow)))
425                         {
426                                 return true;
427                         }
428                         else if (ctrlType.Equals(typeof(HtmlTableCell)))
429                         {
430                                 return true;
431                         }
432                         else
433                         {
434                                 return false;
435                         }
436                 }
437
438                 /// <summary>
439                 /// Creates a new table, and adds it to the GHTActivesubTest.
440                 /// </summary>
441                 /// <param name="ctrlType">The type of the ctrl to test. must be one of </param>
442                 /// <param name="description"></param>
443                 /// <returns></returns>
444                 protected Control GetHtmlTableRelated(Type ctrlType, bool a_AddToPage)
445                 {
446                         HtmlTable l_table = new HtmlTable();
447                         HtmlTableRow l_row = new HtmlTableRow();
448                         HtmlTableCell l_cell = new HtmlTableCell();
449                         
450                         if (a_AddToPage)
451                         {
452                                 GHTActiveSubTest.Controls.Add(l_table);
453                         }
454                         l_table.Rows.Add(l_row);
455                         l_row.Cells.Add(l_cell);                        
456
457                         l_cell.InnerText = "Cell";
458
459                         if ( l_table.GetType() == ctrlType)
460                         {
461                                 return l_table;
462                         }
463                         else if (l_row.GetType() == ctrlType)
464                         {
465                                 return l_row;
466                         }
467                         else if (l_cell.GetType() == ctrlType)
468                         {
469                                 return l_cell;
470                         }
471                         else
472                         {
473                                 throw new ArgumentException("Should be HtmlTable related type.", "ctrlType = " + ctrlType.ToString() );
474                         }
475
476                 }
477                 /// <summary>
478                 /// checks if a given type is a control that should be tested in the context of a table.
479                 /// </summary>
480                 /// <param name="ctrlType">The type to test.</param>
481                 /// <returns>True if the ctrlType is a table related type, otherwise false.</returns>
482                 private bool IsTableRelated(Type ctrlType)
483                 {
484                         if ( ctrlType.Equals(typeof(Table)))
485                         {
486                                 return true;
487                         }
488                         else if (ctrlType.Equals(typeof(TableRow)))
489                         {
490                                 return true;
491                         }
492                         else if (ctrlType.Equals(typeof(TableCell)))
493                         {
494                                 return true;
495                         }
496                         else if(ctrlType.Equals(typeof(TableHeaderCell)))
497                         {
498                                 return true;
499                         }
500                         else
501                         {
502                                 return false;
503                         }
504                 }
505
506                 /// <summary>
507                 /// Creates a new table, and adds it to the GHTActivesubTest.
508                 /// </summary>
509                 /// <param name="ctrlType">The type of the ctrl to test. must be one of </param>
510                 /// <param name="description"></param>
511                 /// <returns></returns>
512                 protected Control GetTableRelated(Type ctrlType, bool a_AddToPage)
513                 {
514                         Table l_table = new Table();
515                         TableRow l_headerRow = new TableRow();
516                         TableRow l_row = new TableRow();
517                         TableCell l_cell = new TableCell();
518                         TableHeaderCell l_headerCell = new TableHeaderCell();
519                         
520                         if (a_AddToPage)
521                         {
522                                 GHTActiveSubTest.Controls.Add(l_table);
523                         }
524
525                         l_table.Rows.Add(l_headerRow);
526                         l_table.Rows.Add(l_row);
527
528                         l_headerRow.Cells.Add(l_headerCell);
529                         l_row.Cells.Add(l_cell);                
530         
531                         l_headerCell.Text = "Header cell";
532                         l_cell.Text = "Table cell";
533
534                         if ( l_table.GetType() == ctrlType)
535                         {
536                                 return l_table;
537                         }
538                         else if (l_row.GetType() == ctrlType)
539                         {
540                                 return l_row;
541                         }
542                         else if (l_cell.GetType() == ctrlType)
543                         {
544                                 return l_cell;
545                         }
546                         else if(l_headerCell.GetType() == ctrlType)
547                         {
548                                 return l_headerCell;
549                         }
550                         else
551                         {
552                                 throw new ArgumentException("Should be table related type.", "ctrlType = " + ctrlType.ToString() );
553                         }
554
555                 }
556
557                 /// <summary>
558                 /// All validation controls must be assigned a control to validate before the page is loaded:
559                 /// </summary>
560                 protected void HandleValidationControls(Control l_toTest)
561                 {
562                         BaseValidator l_validator = l_toTest as BaseValidator;
563                         if (l_validator == null)
564                         {
565                                 return;
566                         }
567                         l_validator.ControlToValidate = "m_tbToValidate";
568                         HandleCompareValidator(l_toTest);
569                 }
570                 /// <summary>
571                 /// All validation controls must be assigned a control to validate before the page is loaded:
572                 /// </summary>
573                 protected void HandleValidationControls()
574                 {
575                         HandleValidationControls(m_cToTest);
576                 }
577
578                 /// <summary>
579                 /// Compare validator must have either ValueToCompare or ControlToCompare set, before the page is loaded.
580                 /// </summary>
581                 private void HandleCompareValidator(Control l_cToTest)
582                 {
583                         CompareValidator l_compareValidator = l_cToTest as CompareValidator;
584                         if (l_compareValidator == null)
585                         {
586                                 return;
587                         }
588                         l_compareValidator.ValueToCompare = "value to compare";
589                 }
590                 /// <summary>
591                 /// Compare validator must have either ValueToCompare or ControlToCompare set, before the page is loaded.
592                 /// </summary>
593                 private void HandleCompareValidator()
594                 {
595                         HandleCompareValidator(m_cToTest);
596                 }
597
598                 /// <summary>
599                 /// Sets the causes validation property to false, if such a property exists in the m_cToTest.
600                 /// This is needed because for controls that cause validation, the ValidationControls on the page
601                 /// generate a client side script.
602                 /// That feature is not working properly in GrassHopper, and is not the main issue of this test.
603                 /// It will be testes specificly in the Validation control tests.
604                 /// </summary>
605                 private void HandleCausesValidation()
606                 {
607                         //HtmlButton:
608                         HtmlButton hButton  = m_cToTest as HtmlButton;
609                         if (hButton != null)
610                         {
611                                 hButton.CausesValidation = false;
612                         }
613                         //HtmlInputButton
614                         HtmlInputButton hInputButton  = m_cToTest as HtmlInputButton;
615                         if (hInputButton != null)
616                         {
617                                 hInputButton.CausesValidation = false;
618                         }
619                         //HtmlInputImage
620                         HtmlInputImage hInputImage = m_cToTest as HtmlInputImage;
621                         if (hInputImage != null)
622                         {
623                                 hInputImage.CausesValidation = false;
624                         }
625                         //Button:
626                         Button button  = m_cToTest as Button;
627                         if (button != null)
628                         {
629                                 button.CausesValidation = false;
630                         }
631                         //ImageButton
632                         ImageButton imageButton  = m_cToTest as ImageButton;
633                         if (imageButton != null)
634                         {
635                                 imageButton.CausesValidation = false;
636                         }
637                         //LinkButton
638                         LinkButton linkButton  = m_cToTest as LinkButton;
639                         if (linkButton != null)
640                         {
641                                 linkButton.CausesValidation = false;
642                         }
643                 }
644                 /// <summary>
645                 /// Creates a textbox, and adds it to the page as not visable.
646                 /// Creates a Required Field validator, and adds it to the page as not visible.
647                 /// </summary>
648                 private void InitTbToValidate()
649                 {
650                         m_tbToValidate = new TextBox();
651                         m_tbToValidate.Visible = false;
652                         m_tbToValidate.ID = "m_tbToValidate";
653                         this.Controls.Add(m_tbToValidate);
654                 }
655
656                 /// <summary>
657                 /// Creates the data sources to bind to.
658                 /// </summary>
659                 private void InitDataSource()
660                 {
661                         InitDataArray();
662                         InitDataTable();
663                 }
664
665                 /// <summary>
666                 /// Creates an array to be used as a data source.
667                 /// </summary>
668                 private void InitDataArray()
669                 {
670                         m_aDataSource = new Item[] {    new Item(1,"aaaa"),
671                                                                                   new Item(2,"bbbb"),
672                                                                                   new Item(3,"cccc"),
673                                                                                   new Item(4,"dddd")};
674                 }
675
676                 /// <summary>
677                 /// creates a data table to be used as a data source.
678                 /// </summary>
679                 private void InitDataTable()
680                 {
681                         m_dtDataSource = new DataTable("SourceTable");
682
683                         DataColumn l_dcId = new DataColumn("Id", typeof(int));
684                         DataColumn l_dcDescription = new DataColumn("Description", typeof(string));
685
686                         m_dtDataSource.Columns.Add(l_dcId);
687                         m_dtDataSource.Columns.Add(l_dcDescription);
688                         
689                         for (int i=0; i<m_aDataSource.Length; i++)
690                         {
691                                 DataRow l_drCurrent = m_dtDataSource.NewRow();
692                                 l_drCurrent["Id"] = m_aDataSource[i].Id;
693                                 l_drCurrent["Description"] = m_aDataSource[i].Description;
694                                 m_dtDataSource.Rows.Add(l_drCurrent);
695                         }
696                 }
697                 
698                 /// <summary>
699                 /// Sets a unique unified id to the tested control based on the count of controls produced by this base.
700                 /// </summary>
701                 private void SetId()
702                 {
703                         SetId(m_cToTest);
704                 }
705
706                 /// <summary>
707                 /// Sets a unique unified id to a control based on the count of controls produced by this base.
708                 /// </summary>
709                 private void SetId(Control a_toSet)
710                 {
711                         a_toSet.ID = "ctrl_" + m_controlsCounter.ToString();
712                         m_controlsCounter++;
713                 }
714                 #endregion
715
716                 /// <summary>
717                 /// Used as an array item, for the data source of data bound controls in this test.
718                 /// </summary>
719                 public class Item
720                 {
721                         #region "Construction"
722                         public Item() : this(0, String.Empty)
723                         {
724                         }
725
726                         public Item(int a_id, string a_description)
727                         {
728                                 m_id = a_id;
729                                 m_description = a_description;
730                         }
731                         #endregion
732
733                         #region "Data Members"
734                         private int m_id;
735                         private string m_description;
736                         #endregion
737
738                         #region "Properties"
739                         public int Id 
740                         {
741                                 get
742                                 {
743                                         return m_id;
744                                 }
745                                 set
746                                 {
747                                         m_id = value;
748                                 }
749                         }
750
751                         public string Description
752                         {
753                                 get
754                                 {
755                                         return m_description;
756                                 }
757                                 set
758                                 {
759                                         m_description = value;
760                                 }
761                         }
762
763                         #endregion
764
765                         #region "Overrides"
766                         public override string ToString()
767                         {
768                                 return this.Id + " " + this.Description;
769                         }
770
771 #endregion
772                 }
773         
774                 /// <summary>
775                 /// The templte used by repeater to render its items.
776                 /// </summary>
777                 public class RepeaterTemplate : ITemplate
778                 {
779                         #region ITemplate Members
780                         /// <summary>
781                         /// Implements ITemplate.instantiateIn(..)
782                         /// Adds an item to the repeater.
783                         /// </summary>
784                         /// <param name="a_container">The controls to add the templated item to.</param>
785                         public void InstantiateIn(Control a_container)
786                         {
787                                 //Header labels:                                
788                                 //~~~~~~~~~~~
789                                 Label l_lIdHeader = new Label();
790                                 l_lIdHeader.Text = "ID: ";
791                                 l_lIdHeader.Font.Bold = true;
792                                 Label l_lDescrptionHeader = new Label();
793                                 l_lDescrptionHeader.Text = "    Description: ";
794                                 l_lDescrptionHeader.Font.Bold = true;
795
796                                 //Data labels:
797                                 //~~~~~~~~~
798                                 Label l_lIdData = new Label();
799                                 l_lIdData.DataBinding += new EventHandler(this.BindId);
800                                 Label l_lDescriptionData = new Label();
801                                 l_lDescriptionData.DataBinding += new EventHandler(this.BindDescription);
802
803                                 //The complete panel.
804                                 Panel l_pItemPanel = new Panel();
805                                 l_pItemPanel.Controls.Add(l_lIdHeader);
806                                 l_pItemPanel.Controls.Add(l_lIdData);
807                                 l_pItemPanel.Controls.Add(l_lDescrptionHeader);
808                                 l_pItemPanel.Controls.Add(l_lDescriptionData);
809
810                                 a_container.Controls.Add(l_pItemPanel);
811                         }
812                         #endregion
813
814                         /// <summary>
815                         /// Handles the data binding event of the Id label in the templated item.
816                         /// </summary>
817                         /// <param name="sender">The source of the event.</param>
818                         /// <param name="args">Additional information about the event.</param>
819                         private void BindId(Object sender, EventArgs args)
820                         {
821                                 Label l_lIddata = (Label)sender;
822                                 RepeaterItem l_riContainer = (RepeaterItem)l_lIddata.NamingContainer;
823                                 l_lIddata.Text = (DataBinder.Eval(l_riContainer.DataItem, "Id")).ToString();
824                         }
825
826                         /// <summary>
827                         /// Handles the data binding event of the Description label in the templated item.
828                         /// </summary>
829                         /// <param name="sender">The source of the event.</param>
830                         /// <param name="args">Additional information about the event.</param>
831                         private void BindDescription(Object sender, EventArgs args)
832                         {
833                                 Label l_lDescriptionData = (Label)sender;
834                                 RepeaterItem l_riContainer = (RepeaterItem)l_lDescriptionData.NamingContainer;
835                                 l_lDescriptionData.Text = (DataBinder.Eval(l_riContainer.DataItem, "Description")).ToString();
836                         }
837                 }
838
839                 /// <summary>
840                 /// The templte used by DataList to render its items.
841                 /// </summary>
842                 public class DataListTemplate : ITemplate
843                 {
844                         #region ITemplate Members
845                         /// <summary>
846                         /// Implements ITemplate.instantiateIn(..)
847                         /// Adds an item to the data list.
848                         /// </summary>
849                         /// <param name="a_container">The controls to add the templated item to.</param>
850                         public void InstantiateIn(Control a_container)
851                         {
852                                 //Header labels:                                
853                                 //~~~~~~~~~~~
854                                 Label l_lIdHeader = new Label();
855                                 l_lIdHeader.Text = "ID: ";
856                                 l_lIdHeader.Font.Bold = true;
857                                 Label l_lDescrptionHeader = new Label();
858                                 l_lDescrptionHeader.Text = "    Description: ";
859                                 l_lDescrptionHeader.Font.Bold = true;
860
861                                 //Data labels:
862                                 //~~~~~~~~~
863                                 Label l_lIdData = new Label();
864                                 l_lIdData.DataBinding += new EventHandler(this.BindId);
865                                 Label l_lDescriptionData = new Label();
866                                 l_lDescriptionData.DataBinding += new EventHandler(this.BindDescription);
867
868                                 //The complete panel.
869                                 Panel l_pItemPanel = new Panel();
870                                 l_pItemPanel.Controls.Add(l_lIdHeader);
871                                 l_pItemPanel.Controls.Add(l_lIdData);
872                                 l_pItemPanel.Controls.Add(l_lDescrptionHeader);
873                                 l_pItemPanel.Controls.Add(l_lDescriptionData);
874
875                                 a_container.Controls.Add(l_pItemPanel);
876                         }
877                         #endregion
878
879                         /// <summary>
880                         /// Handles the data binding event of the Id label in the templated item.
881                         /// </summary>
882                         /// <param name="sender">The source of the event.</param>
883                         /// <param name="args">Additional information about the event.</param>
884                         private void BindId(Object sender, EventArgs args)
885                         {
886                                 Label l_lIddata = (Label)sender;
887                                 DataListItem l_dliContainer = (DataListItem)l_lIddata.NamingContainer;
888                                 l_lIddata.Text = (DataBinder.Eval(l_dliContainer.DataItem, "Id")).ToString();
889                         }
890
891                         /// <summary>
892                         /// Handles the data binding event of the Description label in the templated item.
893                         /// </summary>
894                         /// <param name="sender">The source of the event.</param>
895                         /// <param name="args">Additional information about the event.</param>
896                         private void BindDescription(Object sender, EventArgs args)
897                         {
898                                 Label l_lDescriptionData = (Label)sender;
899                                 DataListItem l_dliContainer = (DataListItem)l_lDescriptionData.NamingContainer;
900                                 l_lDescriptionData.Text = (DataBinder.Eval(l_dliContainer.DataItem, "Description")).ToString();
901                         }
902                 }
903
904         }
905 }