3 // Rafael Mizrahi <rafim@mainsoft.com>
4 // Erez Lotan <erezl@mainsoft.com>
5 // Vladimir Krasnov <vladimirk@mainsoft.com>
8 // Copyright (c) 2002-2005 Mainsoft Corporation.
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.ComponentModel;
33 using System.Web.SessionState;
35 using System.Web.UI.WebControls;
36 using System.Web.UI.HtmlControls;
44 /// Summary description for GHTListControlBase.
46 public class GHTListControlBase : GHTBaseWeb
49 protected void ListControl_AutoPostBack(Type ctrlType)
51 #region "Setting to true"
52 GHTListContorlSubTestBegin(ctrlType, "AutoPostBack = True");
55 m_lcToTest.AutoPostBack = true;
59 GHTSubTestUnexpectedExceptionCaught(ex);
63 #region "Getting true:"
64 GHTListContorlSubTestBegin(ctrlType, "Get AutoPostBack true");
67 m_lcToTest.AutoPostBack = true;
68 Compare(m_lcToTest.AutoPostBack, true);
72 GHTSubTestUnexpectedExceptionCaught(ex);
75 #region "Setting to false"
76 GHTListContorlSubTestBegin(ctrlType, "AutoPostBack = false");
79 m_lcToTest.AutoPostBack = false;
83 GHTSubTestUnexpectedExceptionCaught(ex);
87 #region "Getting false:"
88 GHTListContorlSubTestBegin(ctrlType, "Get AutoPostBack false");
91 m_lcToTest.AutoPostBack = false;
92 Compare(m_lcToTest.AutoPostBack, false);
96 GHTSubTestUnexpectedExceptionCaught(ex);
100 protected void ListControl_ClearSelection(Type ctrlType)
102 #region "No item selected"
103 GHTListContorlSubTestBegin(ctrlType, "No ite, selected");
106 m_lcToTest.Items.Add("A");
107 m_lcToTest.Items.Add("B");
108 m_lcToTest.Items.Add("C");
109 m_lcToTest.ClearSelection();
113 GHTSubTestUnexpectedExceptionCaught(ex);
117 #region "One item selected"
118 GHTListContorlSubTestBegin(ctrlType, "One item selected");
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();
129 GHTSubTestUnexpectedExceptionCaught(ex);
133 protected void ListControl_DataMember(Type ctrlType)
136 #region "Existing Table"
137 GHTListContorlSubTestBegin(ctrlType, "Existing table");
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");
149 GHTSubTestUnexpectedExceptionCaught(ex);
155 GHTListContorlSubTestBegin(ctrlType, "Null");
158 m_lcToTest.DataSource = m_dsData;
159 m_lcToTest.DataMember = null;
160 m_lcToTest.DataTextField = "char Column";
161 m_lcToTest.DataBind();
165 GHTSubTestUnexpectedExceptionCaught(ex);
169 #region "Non existing table in a dataset"
170 GHTListContorlSubTestBegin(ctrlType, "Non existing table in a dataset");
174 m_lcToTest.DataSource = m_dsData;
175 m_lcToTest.DataMember = "not a table name";
176 m_lcToTest.DataBind();
177 GHTSubTestExpectedExceptionNotCaught("HttpException");
179 catch (HttpException ex)
181 GHTSubTestExpectedExceptionCaught(ex);
185 GHTSubTestUnexpectedExceptionCaught(ex);
188 #region "DataSource is not a dataset - set"
189 GHTListContorlSubTestBegin(ctrlType, "DataSource is not a dataset");
194 m_lcToTest.DataSource = m_items;
195 m_lcToTest.DataMember = "not a table name";
196 m_lcToTest.DataBind();
200 GHTSubTestUnexpectedExceptionCaught(ex);
205 protected void ListControl_DataSource(Type ctrlType)
209 #region "DataSource that implements IEnumerable"
210 GHTListContorlSubTestBegin(ctrlType, "DataSource that implements IEnumerable");
214 IEnumerable dataSource = m_items;
215 m_lcToTest.DataSource = dataSource;
216 m_lcToTest.DataBind();
220 GHTSubTestUnexpectedExceptionCaught(ex);
225 #region "DataSource that implements IListSource"
226 GHTListContorlSubTestBegin(ctrlType, "DataSource that implements IListSource");
230 IListSource dataSource = m_dsData;
231 m_lcToTest.DataSource = dataSource;
232 m_lcToTest.DataTextField = "char Column";
233 m_lcToTest.DataBind();
237 GHTSubTestUnexpectedExceptionCaught(ex);
242 #region "DataSource that does not implement IListSource or IEnumerable"
243 GHTListContorlSubTestBegin(ctrlType, "DataSource that does not implement IListSource or IEnumerable");
247 DataItem dataSource = new DataItem(1, "aaa");
248 m_lcToTest.DataSource = dataSource;
249 m_lcToTest.DataBind();
250 GHTSubTestExpectedExceptionNotCaught("ArgumentException");
252 catch (ArgumentException ex)
254 GHTSubTestExpectedExceptionCaught(ex);
258 GHTSubTestUnexpectedExceptionCaught(ex);
265 protected void ListControl_DataTextField(Type ctrlType)
269 #region "string.empty - user defined items"
270 GHTListContorlSubTestBegin(ctrlType, "string.empty");
274 m_lcToTest.DataSource = m_items;
275 m_lcToTest.DataTextField = string.Empty;
276 m_lcToTest.DataBind();
280 GHTSubTestUnexpectedExceptionCaught(ex);
286 //The result is ToString of DataRow which is default Object.ToString()
287 //In Java we get @addres at the end.
288 // #region "string.empty - bound to a table"
289 // GHTListContorlSubTestBegin(ctrlType, "string.empty - bound to a table");
293 // m_lcToTest.DataSource = m_dsData;
294 // m_lcToTest.DataTextField = string.Empty;
295 // m_lcToTest.DataBind();
297 // catch (Exception ex)
299 // GHTSubTestUnexpectedExceptionCaught(ex);
304 #region "Name of an items property"
305 GHTListContorlSubTestBegin(ctrlType, "Name of an items property");
309 m_lcToTest.DataSource = m_items;
310 m_lcToTest.DataTextField = "Name";
311 m_lcToTest.DataBind();
315 GHTSubTestUnexpectedExceptionCaught(ex);
320 #region "Name of an item non-existing property"
321 GHTListContorlSubTestBegin(ctrlType, "Name of an item non-existing property");
325 m_lcToTest.DataSource = m_items;
326 m_lcToTest.DataTextField = "non-existing property";
327 m_lcToTest.DataBind();
328 GHTSubTestExpectedExceptionNotCaught("HttpException");
330 catch (HttpException ex)
332 GHTSubTestExpectedExceptionCaught(ex);
336 GHTSubTestUnexpectedExceptionCaught(ex);
341 #region "Name of a column"
342 GHTListContorlSubTestBegin(ctrlType, "Name of a column");
346 m_lcToTest.DataSource = m_dsData;
347 m_lcToTest.DataTextField = "int Column";
348 m_lcToTest.DataBind();
352 GHTSubTestUnexpectedExceptionCaught(ex);
357 #region "Name of a non-existing column"
358 GHTListContorlSubTestBegin(ctrlType, "Name of a non-existing column");
362 m_lcToTest.DataSource = m_dsData;
363 m_lcToTest.DataTextField = "non-existing column";
364 m_lcToTest.DataBind();
365 GHTSubTestExpectedExceptionNotCaught("HttpException");
367 catch (HttpException ex)
369 GHTSubTestExpectedExceptionCaught(ex);
373 GHTSubTestUnexpectedExceptionCaught(ex);
380 protected void ListControl_DataTextFormatString(Type ctrlType)
383 #region "string.empty"
384 GHTListContorlSubTestBegin(ctrlType, "string.empty");
388 m_lcToTest.DataSource = m_items;
389 m_lcToTest.DataTextField = "Name";
390 m_lcToTest.DataTextFormatString = string.Empty;
391 m_lcToTest.DataBind();
395 GHTSubTestUnexpectedExceptionCaught(ex);
400 #region "Valid format"
401 GHTListContorlSubTestBegin(ctrlType, "Valid format");
405 m_lcToTest.DataSource = m_items;
406 m_lcToTest.DataTextField = "Name";
407 m_lcToTest.DataTextFormatString = "format {0} format";
408 m_lcToTest.DataBind();
412 GHTSubTestUnexpectedExceptionCaught(ex);
417 #region "Invalid format"
418 GHTListContorlSubTestBegin(ctrlType, "Invalid format");
422 m_lcToTest.DataSource = m_items;
423 m_lcToTest.DataTextField = "Name";
424 m_lcToTest.DataTextFormatString = "{invalid format}";
425 m_lcToTest.DataBind();
426 GHTSubTestExpectedExceptionNotCaught("FormatException");
428 catch (FormatException ex)
430 GHTSubTestExpectedExceptionCaught(ex);
434 GHTSubTestUnexpectedExceptionCaught(ex);
441 protected void ListControl_Items(Type ctrlType)
444 GHTListContorlSubTestBegin(ctrlType, "Type & contents");
448 m_lcToTest.DataSource = m_items;
449 m_lcToTest.DataTextField = "Name";
450 m_lcToTest.DataValueField = "Id";
451 m_lcToTest.DataBind();
453 Compare(m_lcToTest.Items.GetType().ToString(), typeof(ListItemCollection).ToString());
454 //Check all the items.
455 for (int i=0; i<7; i++)
457 Compare(m_lcToTest.Items[i].Text, m_items[i].Name);
458 Compare(m_lcToTest.Items[i].Value.ToString(), m_items[i].Id.ToString());
463 GHTSubTestUnexpectedExceptionCaught(ex);
469 protected void ListControl_DataValueField(Type ctrlType)
473 #region "string.empty - user defined items"
474 GHTListContorlSubTestBegin(ctrlType, "string.empty");
478 m_lcToTest.DataSource = m_items;
479 m_lcToTest.DataValueField = string.Empty;
480 m_lcToTest.DataBind();
484 GHTSubTestUnexpectedExceptionCaught(ex);
489 #region "string.empty - bound to a table"
490 GHTListContorlSubTestBegin(ctrlType, "string.empty - bound to a table");
494 m_lcToTest.DataSource = m_dsData;
495 m_lcToTest.DataValueField= string.Empty;
496 m_lcToTest.DataTextField = "char Column";
497 m_lcToTest.DataBind();
501 GHTSubTestUnexpectedExceptionCaught(ex);
506 #region "Name of an items property"
507 GHTListContorlSubTestBegin(ctrlType, "Name of an items property");
511 m_lcToTest.DataSource = m_items;
512 m_lcToTest.DataValueField = "Id";
513 m_lcToTest.DataBind();
517 GHTSubTestUnexpectedExceptionCaught(ex);
522 #region "Name of an item non-existing property"
523 GHTListContorlSubTestBegin(ctrlType, "Name of an item non-existing property");
527 m_lcToTest.DataSource = m_items;
528 m_lcToTest.DataValueField = "non-existing property";
529 m_lcToTest.DataBind();
530 GHTSubTestExpectedExceptionNotCaught("HttpException");
532 catch (HttpException ex)
534 GHTSubTestExpectedExceptionCaught(ex);
538 GHTSubTestUnexpectedExceptionCaught(ex);
543 #region "Name of a column"
544 GHTListContorlSubTestBegin(ctrlType, "Name of a column");
548 m_lcToTest.DataSource = m_dsData;
549 m_lcToTest.DataValueField = "int Column";
550 m_lcToTest.DataBind();
554 GHTSubTestUnexpectedExceptionCaught(ex);
559 #region "Name of a non-existing column"
560 GHTListContorlSubTestBegin(ctrlType, "Name of a non-existing column");
564 m_lcToTest.DataSource = m_dsData;
565 m_lcToTest.DataValueField = "non-existing column";
566 m_lcToTest.DataBind();
567 GHTSubTestExpectedExceptionNotCaught("HttpException");
569 catch (HttpException ex)
571 GHTSubTestExpectedExceptionCaught(ex);
575 GHTSubTestUnexpectedExceptionCaught(ex);
581 protected void ListControl_SelectedIndex(Type ctrlType)
584 #region "None selected"
585 GHTListContorlSubTestBegin(ctrlType, "None selected");
589 m_lcToTest.DataSource = m_items;
590 m_lcToTest.DataBind();
591 m_lcToTest.SelectedIndex = -1;
592 GHTSubTestAddResult(m_lcToTest.SelectedIndex.ToString());
596 GHTSubTestUnexpectedExceptionCaught(ex);
599 #region "valid value"
600 GHTListContorlSubTestBegin(ctrlType, "valid value");
604 m_lcToTest.DataSource = m_items;
605 m_lcToTest.DataBind();
606 m_lcToTest.SelectedIndex = 5;
607 GHTSubTestAddResult(m_lcToTest.SelectedIndex.ToString());
611 GHTSubTestUnexpectedExceptionCaught(ex);
614 #region "Invalid value - above length of items."
615 GHTListContorlSubTestBegin(ctrlType, "Invalid value - above length of items.");
619 m_lcToTest.DataSource = m_items;
620 m_lcToTest.DataBind();
621 m_lcToTest.SelectedIndex = 10;
622 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
624 catch (ArgumentOutOfRangeException ex)
626 GHTSubTestExpectedExceptionCaught(ex);
630 GHTSubTestUnexpectedExceptionCaught(ex);
633 #region "Invalid value - below -1."
634 GHTListContorlSubTestBegin(ctrlType, "Invalid value - below -1.");
638 m_lcToTest.DataSource = m_items;
639 m_lcToTest.DataBind();
640 m_lcToTest.SelectedIndex = -2;
641 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
643 catch (ArgumentOutOfRangeException ex)
645 GHTSubTestExpectedExceptionCaught(ex);
649 GHTSubTestUnexpectedExceptionCaught(ex);
654 protected void ListControl_SelectedItem(Type ctrlType)
657 #region "None selected"
658 GHTListContorlSubTestBegin(ctrlType, "None selected");
662 m_lcToTest.DataSource = m_items;
663 m_lcToTest.DataTextField = "Name";
664 m_lcToTest.DataBind();
665 m_lcToTest.SelectedIndex = -1;
666 if (m_lcToTest.SelectedItem == null)
668 GHTSubTestAddResult("Test passed: SelectedItem is null");
672 GHTSubTestAddResult("Test failede: SelectedItem is not null");
677 GHTSubTestUnexpectedExceptionCaught(ex);
680 #region "Single Item selected"
681 GHTListContorlSubTestBegin(ctrlType, "valid value");
685 m_lcToTest.DataSource = m_items;
686 m_lcToTest.DataTextField = "Name";
687 m_lcToTest.DataBind();
688 m_lcToTest.SelectedIndex = 5;
689 Compare(m_lcToTest.SelectedItem.Text, m_items[5].Name );
693 GHTSubTestUnexpectedExceptionCaught(ex);
696 #region "multiple Items selected"
697 GHTListContorlSubTestBegin(ctrlType, "multiple Items selected");
698 if (m_lcToTest is DropDownList)
702 if (m_lcToTest is ListBox)
704 ((ListBox)m_lcToTest).SelectionMode = ListSelectionMode.Multiple;
708 m_lcToTest.DataSource = m_items;
709 m_lcToTest.DataTextField = "Name";
710 m_lcToTest.DataBind();
711 m_lcToTest.Items[2].Selected = true;
712 m_lcToTest.Items[4].Selected = true;
713 m_lcToTest.Items[6].Selected = true;
714 Compare(m_lcToTest.SelectedItem.Text, m_items[2].Name );
718 GHTSubTestUnexpectedExceptionCaught(ex);
724 protected void ListControl_SelectedValue(Type ctrlType)
727 #region "None selected - get"
728 GHTListContorlSubTestBegin(ctrlType, "None selected - get");
732 m_lcToTest.DataSource = m_items;
733 m_lcToTest.DataValueField = "Id";
734 m_lcToTest.DataBind();
735 m_lcToTest.SelectedIndex = -1;
736 Compare(m_lcToTest.SelectedValue, string.Empty);
740 GHTSubTestUnexpectedExceptionCaught(ex);
743 #region "valid value - get"
744 GHTListContorlSubTestBegin(ctrlType, "valid value - get");
748 m_lcToTest.DataSource = m_items;
749 m_lcToTest.DataValueField = "Id";
750 m_lcToTest.DataBind();
751 m_lcToTest.SelectedIndex = 5;
752 Compare(m_lcToTest.SelectedValue, m_items[5].Id.ToString());
756 GHTSubTestUnexpectedExceptionCaught(ex);
759 #region "valid value - set"
760 GHTListContorlSubTestBegin(ctrlType, "valid value - set");
764 m_lcToTest.DataSource = m_items;
765 m_lcToTest.DataValueField = "Id";
766 m_lcToTest.DataBind();
767 m_lcToTest.SelectedValue = "5";
771 GHTSubTestUnexpectedExceptionCaught(ex);
774 #region "Invalid value - set"
775 GHTListContorlSubTestBegin(ctrlType, "Invalid value - set");
779 m_lcToTest.DataSource = m_items;
780 m_lcToTest.DataBind();
781 m_lcToTest.DataValueField = "Id";
782 m_lcToTest.SelectedValue = "10";
783 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
785 catch (ArgumentOutOfRangeException ex)
787 GHTSubTestExpectedExceptionCaught(ex);
791 GHTSubTestUnexpectedExceptionCaught(ex);
798 #region "Construction"
800 /// Default constructor.
802 public GHTListControlBase()
806 /// Static constructor.
807 /// Initializes the static field m_types[].
809 static GHTListControlBase()
816 public static Type[] TestedTypes
820 return (Type[])(m_types.ToArray(typeof(Type)));
827 /// Holds the ListControl that is tested in the current subtest.
829 private ListControl m_lcToTest;
830 private int m_controlsCounter = 0;
833 /// Holds all the types that are derived from ListControl, and should be tested.
835 private static readonly ArrayList m_types = new ArrayList();
837 /// Two data set to use while testing.
838 /// Initialize this dataset using InitDataSet()
840 private DataSet m_dsData;
841 private DataTable m_dtFirst;
842 private DataTable m_dtSecond;
845 /// An array of Item objects that can be used as a datasource.
846 /// initialize the array using InitArray()
848 private DataItem[] m_items;
851 #region "Private methods"
853 /// Initializes the collection of types derived from ListControl.
855 private static void initTypes()
857 m_types.Add(typeof(ListBox));
858 m_types.Add(typeof(DropDownList));
859 m_types.Add(typeof(RadioButtonList));
860 m_types.Add(typeof(CheckBoxList));
863 /// Creates new sub test and adds a new ListControl to it.
865 /// <param name="ctrlType">Actual type of the tested control</param>
866 /// <param name="description">subtests description</param>
867 private void GHTListContorlSubTestBegin(Type ctrlType, string description)
869 m_lcToTest = (ListControl)GHTElementClone(ctrlType);
870 m_lcToTest.ID = "_ctrl" + m_controlsCounter;
872 GHTSubTestBegin(description);
873 GHTActiveSubTest.Controls.Add(m_lcToTest);
877 /// Initializes both m_dtFirst, and m_dtSecond with names ("First", "Second"), columns, and data.
879 private void InitDataSet()
881 m_dtFirst = new DataTable("First");
882 m_dtFirst.Columns.Add("int Column", typeof(int));
883 m_dtFirst.Columns.Add("bool Column", typeof(bool));
884 m_dtFirst.Columns.Add("char Column", typeof(char));
886 m_dtSecond = new DataTable("Second");
887 m_dtSecond.Columns.Add("double Column", typeof(int));
888 m_dtSecond.Columns.Add("byte Column", typeof(bool));
889 m_dtSecond.Columns.Add("time Column", typeof(DateTime));
891 for (int i=0; i<10; i++)
893 DataRow dr1 = m_dtFirst.NewRow();
894 dr1["int Column"] = i;
895 dr1["bool Column"] = ( i % 2 == 0 ) ? true : false;
896 dr1["char Column"] = (char)(i + 'a');
897 m_dtFirst.Rows.Add(dr1);
899 DataRow dr2 = m_dtSecond.NewRow();
900 dr2["double Column"] = double.Epsilon * i;
901 dr2["byte Column"] = (byte)( i % 10);
902 dr2["time Column"] = DateTime.Now;
903 m_dtSecond.Rows.Add(dr2);
906 m_dsData = new DataSet("Test dataset");
907 m_dsData.Tables.Add(m_dtFirst);
908 m_dsData.Tables.Add(m_dtSecond);
912 private void InitArray()
914 m_items = new DataItem[] { new DataItem(1, "aaa"),
915 new DataItem(2, "bbb"),
916 new DataItem(3, "ccc"),
917 new DataItem(4, "ddd"),
918 new DataItem(5, "eee"),
919 new DataItem(6, "fff"),
920 new DataItem(7, "ggg")};
924 /// Nested class, to use as the items of the m_items array.
926 private class DataItem
928 public DataItem(int a_id, string a_name)
963 public override string ToString()
965 return id.ToString() + name;