2 // ListBindingHelperTest.cs: Test cases for ListBindingHelper class.
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 // Carlos Alberto Cortez <calberto.cortez@gmail.com>
26 // (C) 2008 Novell, Inc. (http://www.novell.com)
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Collections.Generic;
37 using System.Windows.Forms;
39 using NUnit.Framework;
41 namespace MonoTests.System.Windows.Forms
44 public class ListBindingHelperTest : TestHelper
47 public void GetListTest ()
49 ListSource lsource = new ListSource (true);
50 Stack stack = new Stack ();
53 Assert.IsTrue (ListBindingHelper.GetList (lsource) is SimpleItem [], "#A1");
54 Assert.AreEqual ("NonList", ListBindingHelper.GetList ("NonList"), "#A2");
55 Assert.AreEqual (null, ListBindingHelper.GetList (null), "#A3");
56 Assert.AreEqual (stack, ListBindingHelper.GetList (stack), "#A4"); // IEnumerable
58 Assert.IsTrue (ListBindingHelper.GetList (lsource, String.Empty) is SimpleItem [], "#B1");
59 Assert.AreEqual ("NonList", ListBindingHelper.GetList ("NonList", String.Empty), "#B2");
60 Assert.AreEqual (null, ListBindingHelper.GetList (null, "DontExist"), "#B3");
61 Assert.IsTrue (ListBindingHelper.GetList (lsource, null) is SimpleItem [], "#B4");
63 ListContainer list_container = new ListContainer ();
64 Assert.AreEqual (new object [0], ListBindingHelper.GetList (list_container, "List"), "#C1");
66 // Even if IListSource.ContainsListCollection is false, we return the result of GetList ()
67 lsource = new ListSource (false);
68 Assert.IsTrue (ListBindingHelper.GetList (lsource) is SimpleItem [], "#D1");
70 // DataMember is not if IList type
71 Assert.AreEqual (new SimpleItem (), ListBindingHelper.GetList (list_container, "NonList"), "#E1");
75 stack.Push (new SimpleItem (3));
76 stack.Push (new SimpleItem (7));
77 object obj = ListBindingHelper.GetList (stack, "Value");
78 Assert.IsTrue (obj != null, "#F1");
79 Assert.IsTrue (obj is int, "#F2");
80 Assert.AreEqual (7, (int) obj, "#F3");
82 // ListSource returning an IEnumerable,
83 // which in turn retrieves dataMember
84 obj = ListBindingHelper.GetList (lsource, "Value");
85 Assert.IsTrue (obj != null, "#G1");
86 Assert.IsTrue (obj is int, "#G2");
87 Assert.AreEqual (0, (int)obj, "#G3");
89 // Empty IEnumerable - valid property for list item type
90 // Since it's empty, it needs to check whether the datamember is
91 // a valid value, and thus we need the datasource to also be IList
92 // Then we need a parameterized IEnumerable, which returns null.
93 // *Observation: if it is empty and it doesn't implement IList,
94 // it doesn't have a way to get the properties, and will throw an exc
95 StringCollection str_coll = new StringCollection ();
96 obj = ListBindingHelper.GetList (str_coll, "Length");
97 Assert.IsNull (obj, "#H1");
99 // IEnumerable that returns instances of ICustomTypeDescriptor
100 // Use DataTable as source, which returns, when enumerating,
101 // instances of DataRowView, which in turn implement ICustomTypeDescriptor
102 DataTable table = new DataTable ();
103 table.Columns.Add ("Id", typeof (int));
104 table.Rows.Add (666);
105 object l = ListBindingHelper.GetList (table, "Id");
106 Assert.AreEqual (666, l, "#J1");
109 ListBindingHelper.GetList (list_container, "DontExist");
110 Assert.Fail ("#EXC1");
111 } catch (ArgumentException) {
114 // Empty IEnumerable not implementing IList
115 // Don't have a way to know whether at least datamember is valid or not.
118 obj = ListBindingHelper.GetList (stack, "Value");
119 Assert.Fail ("#EXC3");
120 } catch (ArgumentException) {
125 internal class ListSource : IListSource
127 bool contains_collection;
129 public ListSource (bool containsCollection)
131 contains_collection = containsCollection;
134 public bool ContainsListCollection {
136 return contains_collection;
140 public IList GetList ()
142 return new SimpleItem [] { new SimpleItem () };
148 public ListContainer ListContainer
152 return new ListContainer ();
161 return new SimpleItem [0];
165 public SimpleItem NonList {
167 return new SimpleItem ();
180 public SimpleItem (int value)
197 public override int GetHashCode ()
199 return base.GetHashCode ();
202 public override bool Equals (object obj)
204 return value == ((SimpleItem)obj).value;
209 public void GetListItemPropertiesTest ()
211 SimpleItem [] items = new SimpleItem [0];
212 PropertyDescriptorCollection properties = ListBindingHelper.GetListItemProperties (items);
214 Assert.AreEqual (1, properties.Count, "#A1");
215 Assert.AreEqual ("Value", properties [0].Name, "#A2");
217 List<SimpleItem> items_list = new List<SimpleItem> ();
218 properties = ListBindingHelper.GetListItemProperties (items_list);
220 Assert.AreEqual (1, properties.Count, "#B1");
221 Assert.AreEqual ("Value", properties [0].Name, "#B2");
224 ArrayList items_arraylist = new ArrayList ();
225 properties = ListBindingHelper.GetListItemProperties (items_arraylist);
227 Assert.AreEqual (0, properties.Count, "#C1");
229 // Non empty arraylist
230 items_arraylist.Add (new SimpleItem ());
231 properties = ListBindingHelper.GetListItemProperties (items_arraylist);
233 Assert.AreEqual (1, properties.Count, "#D1");
234 Assert.AreEqual ("Value", properties [0].Name, "#D2");
237 properties = ListBindingHelper.GetListItemProperties (new SimpleItem ());
239 Assert.AreEqual (1, properties.Count, "#E1");
240 Assert.AreEqual ("Value", properties [0].Name, "#E2");
243 properties = ListBindingHelper.GetListItemProperties (null);
245 Assert.AreEqual (0, properties.Count, "#F1");
248 properties = ListBindingHelper.GetListItemProperties (new ListSource (true));
250 Assert.AreEqual (1, properties.Count, "#G1");
251 Assert.AreEqual ("Value", properties [0].Name, "#G2");
254 DataTable table = new DataTable ();
255 table.Columns.Add ("Name", typeof (string));
257 properties = ListBindingHelper.GetListItemProperties (table);
258 Assert.AreEqual (1, properties.Count, "#H1");
259 Assert.AreEqual ("Name", properties [0].Name, "#H2");
262 // tests for the overloads of the method
264 [NUnit.Framework.Category ("NotWorking")]
265 public void GetListItemPropertiesTest2 ()
267 ListContainer list_container = new ListContainer ();
268 PropertyDescriptorCollection list_properties = TypeDescriptor.GetProperties (list_container);
269 PropertyDescriptor property = list_properties ["List"];
271 PropertyDescriptorCollection property_coll = ListBindingHelper.GetListItemProperties (list_container,
272 new PropertyDescriptor [] { property });
273 Assert.AreEqual (1, property_coll.Count, "#A1");
274 Assert.AreEqual ("Value", property_coll [0].Name, "#A2");
276 // Empty property descriptor array
277 // Returns list_container properties, since it's not a list
278 property_coll = ListBindingHelper.GetListItemProperties (list_container, new PropertyDescriptor [0]);
279 Assert.AreEqual (2, property_coll.Count, "#B1");
282 // Returns the propeties of the type of that property
283 property = list_properties ["NonList"];
284 property_coll = ListBindingHelper.GetListItemProperties (list_container,
285 new PropertyDescriptor [] { property });
286 Assert.AreEqual (1, property_coll.Count, "#C1");
287 Assert.AreEqual ("Value", property_coll [0].Name, "#C2");
289 // Pass two properties
290 property = list_properties ["List"];
291 PropertyDescriptor property2 = list_properties ["NonList"];
292 property_coll = ListBindingHelper.GetListItemProperties (list_container,
293 new PropertyDescriptor [] { property2, property });
294 Assert.AreEqual (0, property_coll.Count, "#D1");
297 // Third overload - doble re-direction
299 SuperContainer super_container = new SuperContainer ();
300 property = list_properties ["List"];
302 property_coll = ListBindingHelper.GetListItemProperties (super_container, "ListContainer",
303 new PropertyDescriptor [] { property });
304 Assert.AreEqual (1, property_coll.Count, "#E1");
308 public void GetListItemTypeTest ()
310 List<int> list = new List<int> ();
311 DateTime [] date_list = new DateTime [0];
312 StringCollection string_coll = new StringCollection ();
314 Assert.AreEqual (typeof (int), ListBindingHelper.GetListItemType (list), "#A1");
315 Assert.AreEqual (typeof (DateTime), ListBindingHelper.GetListItemType (date_list), "#A2");
316 Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (string_coll), "#A4");
318 // Returns the type of the first item if could enumerate
319 ArrayList arraylist = new ArrayList ();
320 arraylist.Add ("hellou");
321 arraylist.Add (3.1416);
322 Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (arraylist), "#B1");
324 // Returns the type of the public Item property, not the explicit one
325 ListView.ColumnHeaderCollection col_collection = new ListView.ColumnHeaderCollection (null);
326 Assert.AreEqual (typeof (ColumnHeader), ListBindingHelper.GetListItemType (col_collection), "#C1");
328 ListContainer list_container = new ListContainer ();
330 Assert.AreEqual (typeof (IList), ListBindingHelper.GetListItemType (list_container, "List"), "#D1");
331 Assert.AreEqual (typeof (int), ListBindingHelper.GetListItemType (str, "Length"), "#D2");
332 // Property doesn't exist - fallback to object type
333 Assert.AreEqual (typeof (object), ListBindingHelper.GetListItemType (str, "DoesntExist"), "#D3");
335 // finally, objects that are not array nor list
336 Assert.AreEqual (typeof (double), ListBindingHelper.GetListItemType (3.1416), "#E1");
337 Assert.AreEqual (null, ListBindingHelper.GetListItemType (null), "#E2");
339 // bug #507120 - an IEnumerator instance with a Current value returning null,
340 // falling back to IList.this detection, if possible
341 Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (new NullEnumerable (), null), "#F1");
344 // useless class that help us with a simple enumerator with a null Current property
345 // and implementing IList to let the ListBindingHelper get info from the this [] property
346 class NullEnumerable : IList, ICollection, IEnumerable
348 public IEnumerator GetEnumerator ()
350 return new NullEnumerator ();
353 class NullEnumerator : IEnumerator
357 // the idea is that we just move one time - the first time
358 public bool MoveNext ()
372 public object Current {
379 // make this return a string, and hide the interface impl,
380 // so we are sure ListBindingHelper is actually accessing this property
381 public string this [int index] {
384 throw new ArgumentOutOfRangeException ("index");
392 object IList.this [int index] {
400 public int Add (object o)
409 public bool Contains (object o)
414 public int IndexOf (object o)
419 public void Insert (int index, object o)
423 public void Remove (object o)
427 public void RemoveAt (int index)
431 public bool IsFixedSize {
437 public bool IsReadOnly {
443 public void CopyTo (Array array, int offset)
453 public bool IsSynchronized {
459 public object SyncRoot {
467 public void GetListNameTest ()
469 List<int> list = new List<int> ();
470 int [] arr = new int [0];
471 SimpleItem item = new SimpleItem ();
473 Assert.AreEqual (typeof (int).Name, ListBindingHelper.GetListName (list, null), "1");
474 Assert.AreEqual (typeof (int).Name, ListBindingHelper.GetListName (arr, null), "2");
475 Assert.AreEqual (typeof (SimpleItem).Name, ListBindingHelper.GetListName (item, null), "3");
476 Assert.AreEqual (String.Empty, ListBindingHelper.GetListName (null, null), "4");