Mark tests as not working under TARGET_JVM
[mono.git] / mcs / class / System.Web / Test / mainsoft / MainsoftWebApp / utils / GHTListControlBase.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.Collections;
31 using System.ComponentModel;
32 using System.Web;
33 using System.Web.SessionState;
34 using System.Web.UI;
35 using System.Web.UI.WebControls;
36 using System.Web.UI.HtmlControls;
37 using GHTWebControls;
38 using System.Drawing;
39 using System.Data;
40  
41 namespace GHTTests
42 {
43         /// <summary>
44         /// Summary description for GHTListControlBase.
45         /// </summary>
46         public class GHTListControlBase : GHTBaseWeb
47         {
48                 #region "Tests"
49                 protected void ListControl_AutoPostBack(Type ctrlType)
50                 {
51                         #region "Setting to true"
52                         GHTListContorlSubTestBegin(ctrlType, "AutoPostBack = True");
53                         try
54                         {
55                                 m_lcToTest.AutoPostBack = true;
56                         }
57                         catch (Exception ex)
58                         {
59                                 GHTSubTestUnexpectedExceptionCaught(ex);
60                         }
61                         GHTSubTestEnd();
62                         #endregion
63                         #region "Getting true:"
64                         GHTListContorlSubTestBegin(ctrlType, "Get AutoPostBack true");
65                         try
66                         {
67                                 m_lcToTest.AutoPostBack = true;
68                                 Compare(m_lcToTest.AutoPostBack, true);
69                         }
70                         catch (Exception ex)
71                         {
72                                 GHTSubTestUnexpectedExceptionCaught(ex);
73                         }
74                         #endregion
75                         #region "Setting to false"
76                         GHTListContorlSubTestBegin(ctrlType, "AutoPostBack = false");
77                         try
78                         {
79                                 m_lcToTest.AutoPostBack = false;
80                         }
81                         catch (Exception ex)
82                         {
83                                 GHTSubTestUnexpectedExceptionCaught(ex);
84                         }
85                         GHTSubTestEnd();
86                         #endregion
87                         #region "Getting false:"
88                         GHTListContorlSubTestBegin(ctrlType, "Get AutoPostBack false");
89                         try
90                         {
91                                 m_lcToTest.AutoPostBack = false;
92                                 Compare(m_lcToTest.AutoPostBack, false);
93                         }
94                         catch (Exception ex)
95                         {
96                                 GHTSubTestUnexpectedExceptionCaught(ex);
97                         }
98                         #endregion
99                 }
100                 protected void ListControl_ClearSelection(Type ctrlType)
101                 {
102                         #region "No item selected"
103                         GHTListContorlSubTestBegin(ctrlType, "No ite, selected");
104                         try
105                         {
106                                 m_lcToTest.Items.Add("A");
107                                 m_lcToTest.Items.Add("B");
108                                 m_lcToTest.Items.Add("C");
109                                 m_lcToTest.ClearSelection();
110                         }
111                         catch (Exception ex)
112                         {
113                                 GHTSubTestUnexpectedExceptionCaught(ex);
114                         }
115                         GHTSubTestEnd();
116                         #endregion
117                         #region "One item selected"
118                         GHTListContorlSubTestBegin(ctrlType, "One item selected");
119                         try
120                         {
121                                 m_lcToTest.Items.Add("A");
122                                 m_lcToTest.Items.Add("B");
123                                 m_lcToTest.Items.Add("C");
124                                 m_lcToTest.SelectedIndex = 1;
125                                 m_lcToTest.ClearSelection();
126                         }
127                         catch (Exception ex)
128                         {
129                                 GHTSubTestUnexpectedExceptionCaught(ex);
130                         }
131                         #endregion
132                 }
133                 protected void ListControl_DataMember(Type ctrlType)
134                 {
135                         InitDataSet();
136                         #region "Existing Table"
137                         GHTListContorlSubTestBegin(ctrlType, "Existing table");
138                         
139                         try
140                         {
141                                 m_lcToTest.DataSource = m_dsData;
142                                 m_lcToTest.DataMember = "Second";
143                                 m_lcToTest.DataTextField = "double Column";
144                                 m_lcToTest.DataBind();
145                                 Compare(m_lcToTest.DataMember, "Second");
146                         }
147                         catch (Exception ex)
148                         {
149                                 GHTSubTestUnexpectedExceptionCaught(ex);
150                         }
151
152                         GHTSubTestEnd();
153                         #endregion
154                         #region "Null"
155                         GHTListContorlSubTestBegin(ctrlType, "Null");
156                         try
157                         {
158                                 m_lcToTest.DataSource = m_dsData;
159                                 m_lcToTest.DataMember = null;
160                                 m_lcToTest.DataTextField = "char Column";
161                                 m_lcToTest.DataBind();
162                         }
163                         catch (Exception ex)
164                         {
165                                 GHTSubTestUnexpectedExceptionCaught(ex);
166                         }
167
168                         #endregion
169                         #region "Non existing table in a dataset"
170 #if !NET_2_0
171                         GHTListContorlSubTestBegin(ctrlType, "Non existing table in a dataset");
172                         
173                         try
174                         {
175                                 m_lcToTest.DataSource = m_dsData;
176                                 m_lcToTest.DataMember = "not a table name";
177                                 m_lcToTest.DataBind();
178                                 GHTSubTestExpectedExceptionNotCaught("HttpException");
179                         }
180                         catch (HttpException ex)
181                         {
182                                 GHTSubTestExpectedExceptionCaught(ex);
183                         }
184                         catch (Exception ex)
185                         {
186                                 GHTSubTestUnexpectedExceptionCaught(ex);
187                         }
188 #endif
189                         #endregion
190                         #region "DataSource is not a dataset - set"
191                         GHTListContorlSubTestBegin(ctrlType, "DataSource is not a dataset");
192                         
193                         try
194                         {
195                                 InitArray();
196                                 m_lcToTest.DataSource = m_items;
197                                 m_lcToTest.DataMember = "not a table name";
198                                 m_lcToTest.DataBind();
199                         }
200                         catch (Exception ex)
201                         {
202                                 GHTSubTestUnexpectedExceptionCaught(ex);
203                         }
204                         #endregion
205                 }
206
207                 protected void ListControl_DataSource(Type ctrlType)
208                 {
209                         InitDataSet();
210                         InitArray();
211                         #region "DataSource that implements IEnumerable"
212                         GHTListContorlSubTestBegin(ctrlType, "DataSource that implements IEnumerable");
213                         
214                         try
215                         {
216                                 IEnumerable dataSource = m_items;
217                                 m_lcToTest.DataSource = dataSource;
218                                 m_lcToTest.DataBind();
219                         }
220                         catch (Exception ex)
221                         {
222                                 GHTSubTestUnexpectedExceptionCaught(ex);
223                         }
224
225                         GHTSubTestEnd();
226                         #endregion
227                         #region "DataSource that implements IListSource"
228                         GHTListContorlSubTestBegin(ctrlType, "DataSource that implements IListSource");
229                         
230                         try
231                         {
232                                 IListSource dataSource = m_dsData;
233                                 m_lcToTest.DataSource = dataSource;
234                                 m_lcToTest.DataTextField = "char Column";
235                                 m_lcToTest.DataBind();
236                         }
237                         catch (Exception ex)
238                         {
239                                 GHTSubTestUnexpectedExceptionCaught(ex);
240                         }
241
242                         GHTSubTestEnd();
243                         #endregion
244                         #region "DataSource that does not implement IListSource or IEnumerable"
245                         GHTListContorlSubTestBegin(ctrlType, "DataSource that does not implement IListSource or IEnumerable");
246                         
247                         try
248                         {
249                                 DataItem dataSource = new DataItem(1, "aaa");
250                                 m_lcToTest.DataSource = dataSource;
251                                 m_lcToTest.DataBind();
252                                 GHTSubTestExpectedExceptionNotCaught("ArgumentException");
253                         }
254 #if NET_2_0
255                         catch (InvalidOperationException ex)
256                         {
257                                 GHTSubTestExpectedExceptionCaught(ex);
258                         }
259 #endif
260                         catch (ArgumentException ex)
261                         {
262                                 GHTSubTestExpectedExceptionCaught(ex);
263                         }
264                         catch (Exception ex)
265                         {
266                                 GHTSubTestUnexpectedExceptionCaught(ex);
267                         }
268
269                         GHTSubTestEnd();
270                         #endregion
271                 }
272
273                 protected void ListControl_DataTextField(Type ctrlType)
274                 {
275                         InitArray();
276                         InitDataSet();
277                         #region "string.empty - user defined items"
278                         GHTListContorlSubTestBegin(ctrlType, "string.empty");
279                         
280                         try
281                         {
282                                 m_lcToTest.DataSource = m_items;
283                                 m_lcToTest.DataTextField = string.Empty;
284                                 m_lcToTest.DataBind();
285                         }
286                         catch (Exception ex)
287                         {
288                                 GHTSubTestUnexpectedExceptionCaught(ex);
289                         }
290
291                         GHTSubTestEnd();
292                         #endregion
293
294 //The result is ToString of DataRow which is default Object.ToString()
295 //In Java we get @addres at the end.
296 //                      #region "string.empty - bound to a table"
297 //                      GHTListContorlSubTestBegin(ctrlType, "string.empty - bound to a table");
298 //                      
299 //                      try
300 //                      {
301 //                              m_lcToTest.DataSource = m_dsData;
302 //                              m_lcToTest.DataTextField = string.Empty;
303 //                              m_lcToTest.DataBind();
304 //                      }
305 //                      catch (Exception ex)
306 //                      {
307 //                              GHTSubTestUnexpectedExceptionCaught(ex);
308 //                      }
309 //
310 //                      GHTSubTestEnd();
311 //                      #endregion
312                         #region "Name of an items property"
313                         GHTListContorlSubTestBegin(ctrlType, "Name of an items property");
314                         
315                         try
316                         {
317                                 m_lcToTest.DataSource = m_items;
318                                 m_lcToTest.DataTextField = "Name";
319                                 m_lcToTest.DataBind();
320                         }
321                         catch (Exception ex)
322                         {
323                                 GHTSubTestUnexpectedExceptionCaught(ex);
324                         }
325
326                         GHTSubTestEnd();
327                         #endregion
328                         #region "Name of an item non-existing property"
329                         GHTListContorlSubTestBegin(ctrlType, "Name of an item non-existing property");
330                         
331                         try
332                         {
333                                 m_lcToTest.DataSource = m_items;
334                                 m_lcToTest.DataTextField = "non-existing property";
335                                 m_lcToTest.DataBind();
336                                 GHTSubTestExpectedExceptionNotCaught("HttpException");
337                         }
338                         catch (HttpException ex)
339                         {
340                                 GHTSubTestExpectedExceptionCaught(ex);
341                         }
342                         catch (Exception ex)
343                         {
344                                 GHTSubTestUnexpectedExceptionCaught(ex);
345                         }
346
347                         GHTSubTestEnd();
348                         #endregion
349                         #region "Name of a column"
350                         GHTListContorlSubTestBegin(ctrlType, "Name of a column");
351                         
352                         try
353                         {
354                                 m_lcToTest.DataSource = m_dsData;
355                                 m_lcToTest.DataTextField = "int Column";
356                                 m_lcToTest.DataBind();
357                         }
358                         catch (Exception ex)
359                         {
360                                 GHTSubTestUnexpectedExceptionCaught(ex);
361                         }
362
363                         GHTSubTestEnd();
364                         #endregion
365                         #region "Name of a non-existing column"
366                         GHTListContorlSubTestBegin(ctrlType, "Name of a non-existing column");
367                         
368                         try
369                         {
370                                 m_lcToTest.DataSource = m_dsData;
371                                 m_lcToTest.DataTextField = "non-existing column";
372                                 m_lcToTest.DataBind();
373                                 GHTSubTestExpectedExceptionNotCaught("HttpException");
374                         }
375                         catch (HttpException ex)
376                         {
377                                 GHTSubTestExpectedExceptionCaught(ex);
378                         }
379                         catch (Exception ex)
380                         {
381                                 GHTSubTestUnexpectedExceptionCaught(ex);
382                         }
383
384                         GHTSubTestEnd();
385                         #endregion
386                 }
387
388                 protected void ListControl_DataTextFormatString(Type ctrlType)
389                 {
390                         InitArray();
391                         #region "string.empty"
392                         GHTListContorlSubTestBegin(ctrlType, "string.empty");
393                         
394                         try
395                         {
396                                 m_lcToTest.DataSource = m_items;
397                                 m_lcToTest.DataTextField = "Name";
398                                 m_lcToTest.DataTextFormatString = string.Empty;
399                                 m_lcToTest.DataBind();
400                         }
401                         catch (Exception ex)
402                         {
403                                 GHTSubTestUnexpectedExceptionCaught(ex);
404                         }
405
406                         GHTSubTestEnd();
407                         #endregion
408                         #region "Valid format"
409                         GHTListContorlSubTestBegin(ctrlType, "Valid format");
410                         
411                         try
412                         {
413                                 m_lcToTest.DataSource = m_items;
414                                 m_lcToTest.DataTextField = "Name";
415                                 m_lcToTest.DataTextFormatString = "format {0} format";
416                                 m_lcToTest.DataBind();
417                         }
418                         catch (Exception ex)
419                         {
420                                 GHTSubTestUnexpectedExceptionCaught(ex);
421                         }
422
423                         GHTSubTestEnd();
424                         #endregion
425                         #region "Invalid format"
426                         GHTListContorlSubTestBegin(ctrlType, "Invalid format");
427                         
428                         try
429                         {
430                                 m_lcToTest.DataSource = m_items;
431                                 m_lcToTest.DataTextField = "Name";
432                                 m_lcToTest.DataTextFormatString = "{invalid format}";
433                                 m_lcToTest.DataBind();
434                                 GHTSubTestExpectedExceptionNotCaught("FormatException");
435                         }
436                         catch (FormatException ex)
437                         {
438                                 GHTSubTestExpectedExceptionCaught(ex);
439                         }
440                         catch (Exception ex)
441                         {
442                                 GHTSubTestUnexpectedExceptionCaught(ex);
443                         }
444
445                         GHTSubTestEnd();
446                         #endregion
447                 }
448
449                 protected void ListControl_Items(Type ctrlType)
450                 {
451                         InitArray();
452                         GHTListContorlSubTestBegin(ctrlType, "Type & contents");
453                         
454                         try
455                         {
456                                 m_lcToTest.DataSource = m_items;
457                                 m_lcToTest.DataTextField = "Name";
458                                 m_lcToTest.DataValueField = "Id";
459                                 m_lcToTest.DataBind();
460                                 //Check the type:
461                                 Compare(m_lcToTest.Items.GetType().ToString(), typeof(ListItemCollection).ToString());
462                                 //Check all the items.
463                                 for (int i=0; i<7; i++)
464                                 {
465                                         Compare(m_lcToTest.Items[i].Text, m_items[i].Name);
466                                         Compare(m_lcToTest.Items[i].Value.ToString(), m_items[i].Id.ToString());
467                                 }
468                         }
469                         catch (Exception ex)
470                         {
471                                 GHTSubTestUnexpectedExceptionCaught(ex);
472                         }
473
474                         GHTSubTestEnd();
475                 }
476
477                 protected void ListControl_DataValueField(Type ctrlType)
478                 {
479                         InitArray();
480                         InitDataSet();
481                         #region "string.empty - user defined items"
482                         GHTListContorlSubTestBegin(ctrlType, "string.empty");
483                         
484                         try
485                         {
486                                 m_lcToTest.DataSource = m_items;
487                                 m_lcToTest.DataValueField = string.Empty;
488                                 m_lcToTest.DataBind();
489                         }
490                         catch (Exception ex)
491                         {
492                                 GHTSubTestUnexpectedExceptionCaught(ex);
493                         }
494
495                         GHTSubTestEnd();
496                         #endregion
497                         #region "string.empty - bound to a table"
498                         GHTListContorlSubTestBegin(ctrlType, "string.empty - bound to a table");
499                         
500                         try
501                         {
502                                 m_lcToTest.DataSource = m_dsData;
503                                 m_lcToTest.DataValueField= string.Empty;
504                                 m_lcToTest.DataTextField = "char Column";
505                                 m_lcToTest.DataBind();
506                         }
507                         catch (Exception ex)
508                         {
509                                 GHTSubTestUnexpectedExceptionCaught(ex);
510                         }
511
512                         GHTSubTestEnd();
513                         #endregion
514                         #region "Name of an items property"
515                         GHTListContorlSubTestBegin(ctrlType, "Name of an items property");
516                         
517                         try
518                         {
519                                 m_lcToTest.DataSource = m_items;
520                                 m_lcToTest.DataValueField = "Id";
521                                 m_lcToTest.DataBind();
522                         }
523                         catch (Exception ex)
524                         {
525                                 GHTSubTestUnexpectedExceptionCaught(ex);
526                         }
527
528                         GHTSubTestEnd();
529                         #endregion
530                         #region "Name of an item non-existing property"
531                         GHTListContorlSubTestBegin(ctrlType, "Name of an item non-existing property");
532                         
533                         try
534                         {
535                                 m_lcToTest.DataSource = m_items;
536                                 m_lcToTest.DataValueField = "non-existing property";
537                                 m_lcToTest.DataBind();
538                                 GHTSubTestExpectedExceptionNotCaught("HttpException");
539                         }
540                         catch (HttpException ex)
541                         {
542                                 GHTSubTestExpectedExceptionCaught(ex);
543                         }
544                         catch (Exception ex)
545                         {
546                                 GHTSubTestUnexpectedExceptionCaught(ex);
547                         }
548
549                         GHTSubTestEnd();
550                         #endregion
551                         #region "Name of a column"
552                         GHTListContorlSubTestBegin(ctrlType, "Name of a column");
553                         
554                         try
555                         {
556                                 m_lcToTest.DataSource = m_dsData;
557                                 m_lcToTest.DataValueField = "int Column";
558                                 m_lcToTest.DataBind();
559                         }
560                         catch (Exception ex)
561                         {
562                                 GHTSubTestUnexpectedExceptionCaught(ex);
563                         }
564
565                         GHTSubTestEnd();
566                         #endregion
567                         #region "Name of a non-existing column"
568                         GHTListContorlSubTestBegin(ctrlType, "Name of a non-existing column");
569                         
570                         try
571                         {
572                                 m_lcToTest.DataSource = m_dsData;
573                                 m_lcToTest.DataValueField = "non-existing column";
574                                 m_lcToTest.DataBind();
575                                 GHTSubTestExpectedExceptionNotCaught("HttpException");
576                         }
577                         catch (HttpException ex)
578                         {
579                                 GHTSubTestExpectedExceptionCaught(ex);
580                         }
581                         catch (Exception ex)
582                         {
583                                 GHTSubTestUnexpectedExceptionCaught(ex);
584                         }
585
586                         GHTSubTestEnd();
587                         #endregion
588                 }
589                 protected void ListControl_SelectedIndex(Type ctrlType)
590                 {
591                         InitArray();
592                         #region "None selected"
593                         GHTListContorlSubTestBegin(ctrlType, "None selected");
594                         
595                         try
596                         {
597                                 m_lcToTest.DataSource = m_items;
598                                 m_lcToTest.DataBind();
599                                 m_lcToTest.SelectedIndex = -1;
600                                 GHTSubTestAddResult(m_lcToTest.SelectedIndex.ToString());
601                         }
602                         catch (Exception ex)
603                         {
604                                 GHTSubTestUnexpectedExceptionCaught(ex);
605                         }
606                         #endregion
607                         #region "valid value"
608                         GHTListContorlSubTestBegin(ctrlType, "valid value");
609                         
610                         try
611                         {
612                                 m_lcToTest.DataSource = m_items;
613                                 m_lcToTest.DataBind();
614                                 m_lcToTest.SelectedIndex = 5;
615                                 GHTSubTestAddResult(m_lcToTest.SelectedIndex.ToString());
616                         }
617                         catch (Exception ex)
618                         {
619                                 GHTSubTestUnexpectedExceptionCaught(ex);
620                         }
621                         #endregion
622                         #region "Invalid value - above length of items."
623                         GHTListContorlSubTestBegin(ctrlType, "Invalid value - above length of items.");
624                         
625                         try
626                         {
627                                 m_lcToTest.DataSource = m_items;
628                                 m_lcToTest.DataBind();
629                                 m_lcToTest.SelectedIndex = 10;
630                                 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
631                         }
632                         catch (ArgumentOutOfRangeException ex)
633                         {
634                                 GHTSubTestExpectedExceptionCaught(ex);
635                         }
636                         catch (Exception ex)
637                         {
638                                 GHTSubTestUnexpectedExceptionCaught(ex);
639                         }
640                         #endregion
641                         #region "Invalid value - below -1."
642                         GHTListContorlSubTestBegin(ctrlType, "Invalid value - below -1.");
643                         
644                         try
645                         {
646                                 m_lcToTest.DataSource = m_items;
647                                 m_lcToTest.DataBind();
648                                 m_lcToTest.SelectedIndex = -2;
649                                 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
650                         }
651                         catch (ArgumentOutOfRangeException ex)
652                         {
653                                 GHTSubTestExpectedExceptionCaught(ex);
654                         }
655                         catch (Exception ex)
656                         {
657                                 GHTSubTestUnexpectedExceptionCaught(ex);
658                         }
659                         #endregion
660                 }
661
662                 protected void ListControl_SelectedItem(Type ctrlType)
663                 {
664                         InitArray();
665                         #region "None selected"
666                         GHTListContorlSubTestBegin(ctrlType, "None selected");
667                         
668                         try
669                         {
670                                 m_lcToTest.DataSource = m_items;
671                                 m_lcToTest.DataTextField = "Name";
672                                 m_lcToTest.DataBind();
673                                 m_lcToTest.SelectedIndex = -1;
674                                 if (m_lcToTest.SelectedItem == null)
675                                 {
676                                         GHTSubTestAddResult("Test passed: SelectedItem is null");
677                                 }
678                                 else
679                                 {
680                                         GHTSubTestAddResult("Test failede: SelectedItem is not null");
681                                 }
682                         }
683                         catch (Exception ex)
684                         {
685                                 GHTSubTestUnexpectedExceptionCaught(ex);
686                         }
687                         #endregion
688                         #region "Single Item selected"
689                         GHTListContorlSubTestBegin(ctrlType, "valid value");
690                         
691                         try
692                         {
693                                 m_lcToTest.DataSource = m_items;
694                                 m_lcToTest.DataTextField = "Name";
695                                 m_lcToTest.DataBind();
696                                 m_lcToTest.SelectedIndex = 5;
697                                 Compare(m_lcToTest.SelectedItem.Text, m_items[5].Name );
698                         }
699                         catch (Exception ex)
700                         {
701                                 GHTSubTestUnexpectedExceptionCaught(ex);
702                         }
703                         #endregion
704                         #region "multiple Items selected"
705                         GHTListContorlSubTestBegin(ctrlType, "multiple Items selected");
706                         if (m_lcToTest is DropDownList)
707                         {
708                                 return;
709                         }
710                         if (m_lcToTest is ListBox)
711                         {
712                                 ((ListBox)m_lcToTest).SelectionMode = ListSelectionMode.Multiple;
713                         }
714                         try
715                         {
716                                 m_lcToTest.DataSource = m_items;
717                                 m_lcToTest.DataTextField = "Name";
718                                 m_lcToTest.DataBind();
719                                 m_lcToTest.Items[2].Selected = true;
720                                 m_lcToTest.Items[4].Selected = true;
721                                 m_lcToTest.Items[6].Selected = true;
722                                 Compare(m_lcToTest.SelectedItem.Text, m_items[2].Name );
723                         }
724                         catch (Exception ex)
725                         {
726                                 GHTSubTestUnexpectedExceptionCaught(ex);
727                         }
728
729                         #endregion
730                 }
731
732                 protected void ListControl_SelectedValue(Type ctrlType)
733                 {
734                         InitArray();
735                         #region "None selected - get"
736                         GHTListContorlSubTestBegin(ctrlType, "None selected - get");
737                         
738                         try
739                         {
740                                 m_lcToTest.DataSource = m_items;
741                                 m_lcToTest.DataValueField = "Id";
742                                 m_lcToTest.DataBind();
743                                 m_lcToTest.SelectedIndex = -1;
744                                 Compare(m_lcToTest.SelectedValue, string.Empty);
745                         }
746                         catch (Exception ex)
747                         {
748                                 GHTSubTestUnexpectedExceptionCaught(ex);
749                         }
750                         #endregion
751                         #region "valid value - get"
752                         GHTListContorlSubTestBegin(ctrlType, "valid value - get");
753                         
754                         try
755                         {
756                                 m_lcToTest.DataSource = m_items;
757                                 m_lcToTest.DataValueField = "Id";
758                                 m_lcToTest.DataBind();
759                                 m_lcToTest.SelectedIndex = 5;
760                                 Compare(m_lcToTest.SelectedValue, m_items[5].Id.ToString());
761                         }
762                         catch (Exception ex)
763                         {
764                                 GHTSubTestUnexpectedExceptionCaught(ex);
765                         }
766                         #endregion
767                         #region "valid value - set"
768                         GHTListContorlSubTestBegin(ctrlType, "valid value - set");
769                         
770                         try
771                         {
772                                 m_lcToTest.DataSource = m_items;
773                                 m_lcToTest.DataValueField = "Id";
774                                 m_lcToTest.DataBind();
775                                 m_lcToTest.SelectedValue = "5";
776                         }
777                         catch (Exception ex)
778                         {
779                                 GHTSubTestUnexpectedExceptionCaught(ex);
780                         }
781                         #endregion
782                         #region "Invalid value - set"
783                         GHTListContorlSubTestBegin(ctrlType, "Invalid value - set");
784                         
785                         try
786                         {
787                                 m_lcToTest.DataSource = m_items;
788                                 m_lcToTest.DataBind();
789                                 m_lcToTest.DataValueField = "Id";
790                                 m_lcToTest.SelectedValue = "10";
791                                 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
792                         }
793                         catch (ArgumentOutOfRangeException ex)
794                         {
795                                 GHTSubTestExpectedExceptionCaught(ex);
796                         }
797                         catch (Exception ex)
798                         {
799                                 GHTSubTestUnexpectedExceptionCaught(ex);
800                         }
801                         #endregion
802                 }
803
804                 #endregion
805
806                 #region "Construction"
807                 /// <summary>
808                 /// Default constructor.
809                 /// </summary>
810                 public GHTListControlBase()
811                 {
812                 }
813                 /// <summary>
814                 /// Static constructor.
815                 /// Initializes the static field m_types[].
816                 /// </summary>
817                 static GHTListControlBase()
818                 {
819                         initTypes();
820                 }
821                 #endregion
822                 
823                 #region "properties"
824                 public static Type[] TestedTypes
825                 {
826                         get
827                         {
828                                 return (Type[])(m_types.ToArray(typeof(Type)));
829                         }
830                 }
831                 #endregion
832
833                 #region "members"
834                 /// <summary>
835                 /// Holds the ListControl that is tested in the current subtest.
836                 /// </summary>
837                 private ListControl m_lcToTest;
838                 private int m_controlsCounter = 0;
839
840                 /// <summary>
841                 /// Holds all the types that are derived from ListControl, and should be tested.
842                 /// </summary>
843                 private  static readonly  ArrayList m_types = new ArrayList();
844                 /// <summary>
845                 /// Two data set to use while testing.
846                 /// Initialize this dataset using InitDataSet()
847                 /// </summary>
848                 private DataSet m_dsData;
849                 private DataTable m_dtFirst;
850                 private DataTable m_dtSecond;
851
852                 /// <summary>
853                 /// An array of Item objects that can be used as a datasource.
854                 /// initialize the array using InitArray()
855                 /// </summary>
856                 private DataItem[] m_items;
857                 #endregion
858
859                 #region "Private methods"
860                 /// <summary>
861                 /// Initializes the collection of types derived from ListControl.
862                 /// </summary>
863                 private static void initTypes()
864                 {
865                         m_types.Add(typeof(ListBox));
866                         m_types.Add(typeof(DropDownList));
867                         m_types.Add(typeof(RadioButtonList));
868                         m_types.Add(typeof(CheckBoxList));
869                 }
870                 /// <summary>
871                 /// Creates new sub test and adds a new ListControl to it.
872                 /// </summary>
873                 /// <param name="ctrlType">Actual type of the tested control</param>
874                 /// <param name="description">subtests description</param>
875                 private void GHTListContorlSubTestBegin(Type ctrlType, string description)
876                 {
877                         m_lcToTest = (ListControl)GHTElementClone(ctrlType);
878                         m_lcToTest.ID = "_ctrl" + m_controlsCounter;
879                         m_controlsCounter++;
880                         GHTSubTestBegin(description);
881                         GHTActiveSubTest.Controls.Add(m_lcToTest);
882                 }
883                 
884                 /// <summary>
885                 /// Initializes both m_dtFirst, and m_dtSecond with names ("First", "Second"), columns, and data.
886                 /// </summary>
887                 private void InitDataSet()
888                 {
889                         m_dtFirst = new DataTable("First");
890                         m_dtFirst.Columns.Add("int Column", typeof(int));
891                         m_dtFirst.Columns.Add("bool Column", typeof(bool));
892                         m_dtFirst.Columns.Add("char Column", typeof(char));
893
894                         m_dtSecond = new DataTable("Second");
895                         m_dtSecond.Columns.Add("double Column", typeof(int));
896                         m_dtSecond.Columns.Add("byte Column", typeof(bool));
897                         m_dtSecond.Columns.Add("time Column", typeof(DateTime));
898
899                         for (int i=0; i<10; i++)
900                         {
901                                 DataRow dr1 = m_dtFirst.NewRow();
902                                 dr1["int Column"] = i;
903                                 dr1["bool Column"] = ( i % 2  == 0 ) ? true : false;
904                                 dr1["char Column"] = (char)(i + 'a');
905                                 m_dtFirst.Rows.Add(dr1);
906
907                                 DataRow dr2 = m_dtSecond.NewRow();
908                                 dr2["double Column"] = double.Epsilon * i;
909                                 dr2["byte Column"] = (byte)( i % 10);
910                                 dr2["time Column"] = DateTime.Now;
911                                 m_dtSecond.Rows.Add(dr2);
912                         }
913
914                         m_dsData = new DataSet("Test dataset");
915                         m_dsData.Tables.Add(m_dtFirst);
916                         m_dsData.Tables.Add(m_dtSecond);
917                 }
918
919
920                 private void InitArray()
921                 {
922                         m_items = new DataItem[] {      new DataItem(1, "aaa"),
923                                                                                                                                                                         new DataItem(2, "bbb"),
924                                                                                                                                                                         new DataItem(3, "ccc"),
925                                                                                                                                                                         new DataItem(4, "ddd"),
926                                                                                                                                                                         new DataItem(5, "eee"),
927                                                                                                                                                                         new DataItem(6, "fff"),
928                                                                                                                                                                         new DataItem(7, "ggg")};
929                 }
930
931                 /// <summary>
932                 /// Nested class, to use as the items of the m_items array.
933                 /// </summary>
934                 private class DataItem
935                 {
936                         public DataItem(int a_id, string a_name)
937                         {
938                                 id = a_id;
939                                 name = a_name;
940                         }
941
942                         private int id;
943                         private string name;
944
945                         public int Id
946                         {
947                                 get
948                                 {
949                                         return id;
950                                 }
951                                 set
952                                 {
953                                         id = value;
954                                 }
955                         }
956
957                         public string Name
958                         {
959                                 get
960                                 {
961                                         return name;
962                                 }
963                                 set
964                                 {
965                                         name = value;
966                                 }
967
968                         }
969
970
971                         public override string ToString()
972                         {
973                                 return id.ToString() + name;
974                         }
975                 }
976         }
977         #endregion
978
979 }