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"
171 GHTListContorlSubTestBegin(ctrlType, "Non existing table in a dataset");
175 m_lcToTest.DataSource = m_dsData;
176 m_lcToTest.DataMember = "not a table name";
177 m_lcToTest.DataBind();
178 GHTSubTestExpectedExceptionNotCaught("HttpException");
180 catch (HttpException ex)
182 GHTSubTestExpectedExceptionCaught(ex);
186 GHTSubTestUnexpectedExceptionCaught(ex);
190 #region "DataSource is not a dataset - set"
191 GHTListContorlSubTestBegin(ctrlType, "DataSource is not a dataset");
196 m_lcToTest.DataSource = m_items;
197 m_lcToTest.DataMember = "not a table name";
198 m_lcToTest.DataBind();
202 GHTSubTestUnexpectedExceptionCaught(ex);
207 protected void ListControl_DataSource(Type ctrlType)
211 #region "DataSource that implements IEnumerable"
212 GHTListContorlSubTestBegin(ctrlType, "DataSource that implements IEnumerable");
216 IEnumerable dataSource = m_items;
217 m_lcToTest.DataSource = dataSource;
218 m_lcToTest.DataBind();
222 GHTSubTestUnexpectedExceptionCaught(ex);
227 #region "DataSource that implements IListSource"
228 GHTListContorlSubTestBegin(ctrlType, "DataSource that implements IListSource");
232 IListSource dataSource = m_dsData;
233 m_lcToTest.DataSource = dataSource;
234 m_lcToTest.DataTextField = "char Column";
235 m_lcToTest.DataBind();
239 GHTSubTestUnexpectedExceptionCaught(ex);
244 #region "DataSource that does not implement IListSource or IEnumerable"
245 GHTListContorlSubTestBegin(ctrlType, "DataSource that does not implement IListSource or IEnumerable");
249 DataItem dataSource = new DataItem(1, "aaa");
250 m_lcToTest.DataSource = dataSource;
251 m_lcToTest.DataBind();
252 GHTSubTestExpectedExceptionNotCaught("ArgumentException");
255 catch (InvalidOperationException ex)
257 GHTSubTestExpectedExceptionCaught(ex);
260 catch (ArgumentException ex)
262 GHTSubTestExpectedExceptionCaught(ex);
266 GHTSubTestUnexpectedExceptionCaught(ex);
273 protected void ListControl_DataTextField(Type ctrlType)
277 #region "string.empty - user defined items"
278 GHTListContorlSubTestBegin(ctrlType, "string.empty");
282 m_lcToTest.DataSource = m_items;
283 m_lcToTest.DataTextField = string.Empty;
284 m_lcToTest.DataBind();
288 GHTSubTestUnexpectedExceptionCaught(ex);
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");
301 // m_lcToTest.DataSource = m_dsData;
302 // m_lcToTest.DataTextField = string.Empty;
303 // m_lcToTest.DataBind();
305 // catch (Exception ex)
307 // GHTSubTestUnexpectedExceptionCaught(ex);
312 #region "Name of an items property"
313 GHTListContorlSubTestBegin(ctrlType, "Name of an items property");
317 m_lcToTest.DataSource = m_items;
318 m_lcToTest.DataTextField = "Name";
319 m_lcToTest.DataBind();
323 GHTSubTestUnexpectedExceptionCaught(ex);
328 #region "Name of an item non-existing property"
329 GHTListContorlSubTestBegin(ctrlType, "Name of an item non-existing property");
333 m_lcToTest.DataSource = m_items;
334 m_lcToTest.DataTextField = "non-existing property";
335 m_lcToTest.DataBind();
336 GHTSubTestExpectedExceptionNotCaught("HttpException");
338 catch (HttpException ex)
340 GHTSubTestExpectedExceptionCaught(ex);
344 GHTSubTestUnexpectedExceptionCaught(ex);
349 #region "Name of a column"
350 GHTListContorlSubTestBegin(ctrlType, "Name of a column");
354 m_lcToTest.DataSource = m_dsData;
355 m_lcToTest.DataTextField = "int Column";
356 m_lcToTest.DataBind();
360 GHTSubTestUnexpectedExceptionCaught(ex);
365 #region "Name of a non-existing column"
366 GHTListContorlSubTestBegin(ctrlType, "Name of a non-existing column");
370 m_lcToTest.DataSource = m_dsData;
371 m_lcToTest.DataTextField = "non-existing column";
372 m_lcToTest.DataBind();
373 GHTSubTestExpectedExceptionNotCaught("HttpException");
375 catch (HttpException ex)
377 GHTSubTestExpectedExceptionCaught(ex);
381 GHTSubTestUnexpectedExceptionCaught(ex);
388 protected void ListControl_DataTextFormatString(Type ctrlType)
391 #region "string.empty"
392 GHTListContorlSubTestBegin(ctrlType, "string.empty");
396 m_lcToTest.DataSource = m_items;
397 m_lcToTest.DataTextField = "Name";
398 m_lcToTest.DataTextFormatString = string.Empty;
399 m_lcToTest.DataBind();
403 GHTSubTestUnexpectedExceptionCaught(ex);
408 #region "Valid format"
409 GHTListContorlSubTestBegin(ctrlType, "Valid format");
413 m_lcToTest.DataSource = m_items;
414 m_lcToTest.DataTextField = "Name";
415 m_lcToTest.DataTextFormatString = "format {0} format";
416 m_lcToTest.DataBind();
420 GHTSubTestUnexpectedExceptionCaught(ex);
425 #region "Invalid format"
426 GHTListContorlSubTestBegin(ctrlType, "Invalid format");
430 m_lcToTest.DataSource = m_items;
431 m_lcToTest.DataTextField = "Name";
432 m_lcToTest.DataTextFormatString = "{invalid format}";
433 m_lcToTest.DataBind();
434 GHTSubTestExpectedExceptionNotCaught("FormatException");
436 catch (FormatException ex)
438 GHTSubTestExpectedExceptionCaught(ex);
442 GHTSubTestUnexpectedExceptionCaught(ex);
449 protected void ListControl_Items(Type ctrlType)
452 GHTListContorlSubTestBegin(ctrlType, "Type & contents");
456 m_lcToTest.DataSource = m_items;
457 m_lcToTest.DataTextField = "Name";
458 m_lcToTest.DataValueField = "Id";
459 m_lcToTest.DataBind();
461 Compare(m_lcToTest.Items.GetType().ToString(), typeof(ListItemCollection).ToString());
462 //Check all the items.
463 for (int i=0; i<7; i++)
465 Compare(m_lcToTest.Items[i].Text, m_items[i].Name);
466 Compare(m_lcToTest.Items[i].Value.ToString(), m_items[i].Id.ToString());
471 GHTSubTestUnexpectedExceptionCaught(ex);
477 protected void ListControl_DataValueField(Type ctrlType)
481 #region "string.empty - user defined items"
482 GHTListContorlSubTestBegin(ctrlType, "string.empty");
486 m_lcToTest.DataSource = m_items;
487 m_lcToTest.DataValueField = string.Empty;
488 m_lcToTest.DataBind();
492 GHTSubTestUnexpectedExceptionCaught(ex);
497 #region "string.empty - bound to a table"
498 GHTListContorlSubTestBegin(ctrlType, "string.empty - bound to a table");
502 m_lcToTest.DataSource = m_dsData;
503 m_lcToTest.DataValueField= string.Empty;
504 m_lcToTest.DataTextField = "char Column";
505 m_lcToTest.DataBind();
509 GHTSubTestUnexpectedExceptionCaught(ex);
514 #region "Name of an items property"
515 GHTListContorlSubTestBegin(ctrlType, "Name of an items property");
519 m_lcToTest.DataSource = m_items;
520 m_lcToTest.DataValueField = "Id";
521 m_lcToTest.DataBind();
525 GHTSubTestUnexpectedExceptionCaught(ex);
530 #region "Name of an item non-existing property"
531 GHTListContorlSubTestBegin(ctrlType, "Name of an item non-existing property");
535 m_lcToTest.DataSource = m_items;
536 m_lcToTest.DataValueField = "non-existing property";
537 m_lcToTest.DataBind();
538 GHTSubTestExpectedExceptionNotCaught("HttpException");
540 catch (HttpException ex)
542 GHTSubTestExpectedExceptionCaught(ex);
546 GHTSubTestUnexpectedExceptionCaught(ex);
551 #region "Name of a column"
552 GHTListContorlSubTestBegin(ctrlType, "Name of a column");
556 m_lcToTest.DataSource = m_dsData;
557 m_lcToTest.DataValueField = "int Column";
558 m_lcToTest.DataBind();
562 GHTSubTestUnexpectedExceptionCaught(ex);
567 #region "Name of a non-existing column"
568 GHTListContorlSubTestBegin(ctrlType, "Name of a non-existing column");
572 m_lcToTest.DataSource = m_dsData;
573 m_lcToTest.DataValueField = "non-existing column";
574 m_lcToTest.DataBind();
575 GHTSubTestExpectedExceptionNotCaught("HttpException");
577 catch (HttpException ex)
579 GHTSubTestExpectedExceptionCaught(ex);
583 GHTSubTestUnexpectedExceptionCaught(ex);
589 protected void ListControl_SelectedIndex(Type ctrlType)
592 #region "None selected"
593 GHTListContorlSubTestBegin(ctrlType, "None selected");
597 m_lcToTest.DataSource = m_items;
598 m_lcToTest.DataBind();
599 m_lcToTest.SelectedIndex = -1;
600 GHTSubTestAddResult(m_lcToTest.SelectedIndex.ToString());
604 GHTSubTestUnexpectedExceptionCaught(ex);
607 #region "valid value"
608 GHTListContorlSubTestBegin(ctrlType, "valid value");
612 m_lcToTest.DataSource = m_items;
613 m_lcToTest.DataBind();
614 m_lcToTest.SelectedIndex = 5;
615 GHTSubTestAddResult(m_lcToTest.SelectedIndex.ToString());
619 GHTSubTestUnexpectedExceptionCaught(ex);
622 #region "Invalid value - above length of items."
623 GHTListContorlSubTestBegin(ctrlType, "Invalid value - above length of items.");
627 m_lcToTest.DataSource = m_items;
628 m_lcToTest.DataBind();
629 m_lcToTest.SelectedIndex = 10;
630 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
632 catch (ArgumentOutOfRangeException ex)
634 GHTSubTestExpectedExceptionCaught(ex);
638 GHTSubTestUnexpectedExceptionCaught(ex);
641 #region "Invalid value - below -1."
642 GHTListContorlSubTestBegin(ctrlType, "Invalid value - below -1.");
646 m_lcToTest.DataSource = m_items;
647 m_lcToTest.DataBind();
648 m_lcToTest.SelectedIndex = -2;
649 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
651 catch (ArgumentOutOfRangeException ex)
653 GHTSubTestExpectedExceptionCaught(ex);
657 GHTSubTestUnexpectedExceptionCaught(ex);
662 protected void ListControl_SelectedItem(Type ctrlType)
665 #region "None selected"
666 GHTListContorlSubTestBegin(ctrlType, "None selected");
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)
676 GHTSubTestAddResult("Test passed: SelectedItem is null");
680 GHTSubTestAddResult("Test failede: SelectedItem is not null");
685 GHTSubTestUnexpectedExceptionCaught(ex);
688 #region "Single Item selected"
689 GHTListContorlSubTestBegin(ctrlType, "valid value");
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 );
701 GHTSubTestUnexpectedExceptionCaught(ex);
704 #region "multiple Items selected"
705 GHTListContorlSubTestBegin(ctrlType, "multiple Items selected");
706 if (m_lcToTest is DropDownList)
710 if (m_lcToTest is ListBox)
712 ((ListBox)m_lcToTest).SelectionMode = ListSelectionMode.Multiple;
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 );
726 GHTSubTestUnexpectedExceptionCaught(ex);
732 protected void ListControl_SelectedValue(Type ctrlType)
735 #region "None selected - get"
736 GHTListContorlSubTestBegin(ctrlType, "None selected - get");
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);
748 GHTSubTestUnexpectedExceptionCaught(ex);
751 #region "valid value - get"
752 GHTListContorlSubTestBegin(ctrlType, "valid value - get");
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());
764 GHTSubTestUnexpectedExceptionCaught(ex);
767 #region "valid value - set"
768 GHTListContorlSubTestBegin(ctrlType, "valid value - set");
772 m_lcToTest.DataSource = m_items;
773 m_lcToTest.DataValueField = "Id";
774 m_lcToTest.DataBind();
775 m_lcToTest.SelectedValue = "5";
779 GHTSubTestUnexpectedExceptionCaught(ex);
782 #region "Invalid value - set"
783 GHTListContorlSubTestBegin(ctrlType, "Invalid value - set");
787 m_lcToTest.DataSource = m_items;
788 m_lcToTest.DataBind();
789 m_lcToTest.DataValueField = "Id";
790 m_lcToTest.SelectedValue = "10";
791 GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
793 catch (ArgumentOutOfRangeException ex)
795 GHTSubTestExpectedExceptionCaught(ex);
799 GHTSubTestUnexpectedExceptionCaught(ex);
806 #region "Construction"
808 /// Default constructor.
810 public GHTListControlBase()
814 /// Static constructor.
815 /// Initializes the static field m_types[].
817 static GHTListControlBase()
824 public static Type[] TestedTypes
828 return (Type[])(m_types.ToArray(typeof(Type)));
835 /// Holds the ListControl that is tested in the current subtest.
837 private ListControl m_lcToTest;
838 private int m_controlsCounter = 0;
841 /// Holds all the types that are derived from ListControl, and should be tested.
843 private static readonly ArrayList m_types = new ArrayList();
845 /// Two data set to use while testing.
846 /// Initialize this dataset using InitDataSet()
848 private DataSet m_dsData;
849 private DataTable m_dtFirst;
850 private DataTable m_dtSecond;
853 /// An array of Item objects that can be used as a datasource.
854 /// initialize the array using InitArray()
856 private DataItem[] m_items;
859 #region "Private methods"
861 /// Initializes the collection of types derived from ListControl.
863 private static void initTypes()
865 m_types.Add(typeof(ListBox));
866 m_types.Add(typeof(DropDownList));
867 m_types.Add(typeof(RadioButtonList));
868 m_types.Add(typeof(CheckBoxList));
871 /// Creates new sub test and adds a new ListControl to it.
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)
877 m_lcToTest = (ListControl)GHTElementClone(ctrlType);
878 m_lcToTest.ID = "_ctrl" + m_controlsCounter;
880 GHTSubTestBegin(description);
881 GHTActiveSubTest.Controls.Add(m_lcToTest);
885 /// Initializes both m_dtFirst, and m_dtSecond with names ("First", "Second"), columns, and data.
887 private void InitDataSet()
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));
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));
899 for (int i=0; i<10; i++)
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);
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);
914 m_dsData = new DataSet("Test dataset");
915 m_dsData.Tables.Add(m_dtFirst);
916 m_dsData.Tables.Add(m_dtSecond);
920 private void InitArray()
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")};
932 /// Nested class, to use as the items of the m_items array.
934 private class DataItem
936 public DataItem(int a_id, string a_name)
971 public override string ToString()
973 return id.ToString() + name;