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