ListBindingHelperTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //
  2. // ListBindingHelperTest.cs: Test cases for ListBindingHelper class.
  3. //
  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:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  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.
  22. // Author:
  23. // Carlos Alberto Cortez <[email protected]>
  24. //
  25. // (C) 2008 Novell, Inc. (http://www.novell.com)
  26. //
  27. #if NET_2_0
  28. using System;
  29. using System.ComponentModel;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Collections.Generic;
  33. using System.Data;
  34. using System.Windows.Forms;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Windows.Forms
  37. {
  38. [TestFixture]
  39. public class ListBindingHelperTest : TestHelper
  40. {
  41. [Test]
  42. public void GetListTest ()
  43. {
  44. ListSource lsource = new ListSource (true);
  45. Stack stack = new Stack ();
  46. stack.Push (3);
  47. Assert.IsTrue (ListBindingHelper.GetList (lsource) is SimpleItem [], "#A1");
  48. Assert.AreEqual ("NonList", ListBindingHelper.GetList ("NonList"), "#A2");
  49. Assert.AreEqual (null, ListBindingHelper.GetList (null), "#A3");
  50. Assert.AreEqual (stack, ListBindingHelper.GetList (stack), "#A4"); // IEnumerable
  51. Assert.IsTrue (ListBindingHelper.GetList (lsource, String.Empty) is SimpleItem [], "#B1");
  52. Assert.AreEqual ("NonList", ListBindingHelper.GetList ("NonList", String.Empty), "#B2");
  53. Assert.AreEqual (null, ListBindingHelper.GetList (null, "DontExist"), "#B3");
  54. Assert.IsTrue (ListBindingHelper.GetList (lsource, null) is SimpleItem [], "#B4");
  55. ListContainer list_container = new ListContainer ();
  56. Assert.AreEqual (new object [0], ListBindingHelper.GetList (list_container, "List"), "#C1");
  57. // Even if IListSource.ContainsListCollection is false, we return the result of GetList ()
  58. lsource = new ListSource (false);
  59. Assert.IsTrue (ListBindingHelper.GetList (lsource) is SimpleItem [], "#D1");
  60. // DataMember is not if IList type
  61. Assert.AreEqual (new SimpleItem (), ListBindingHelper.GetList (list_container, "NonList"), "#E1");
  62. // List (IEnumerable)
  63. stack.Clear ();
  64. stack.Push (new SimpleItem (3));
  65. stack.Push (new SimpleItem (7));
  66. object obj = ListBindingHelper.GetList (stack, "Value");
  67. Assert.IsTrue (obj != null, "#F1");
  68. Assert.IsTrue (obj is int, "#F2");
  69. Assert.AreEqual (7, (int) obj, "#F3");
  70. // ListSource returning an IEnumerable,
  71. // which in turn retrieves dataMember
  72. obj = ListBindingHelper.GetList (lsource, "Value");
  73. Assert.IsTrue (obj != null, "#G1");
  74. Assert.IsTrue (obj is int, "#G2");
  75. Assert.AreEqual (0, (int)obj, "#G3");
  76. // Empty IEnumerable - valid property for list item type
  77. // Since it's empty, it needs to check whether the datamember is
  78. // a valid value, and thus we need the datasource to also be IList
  79. // Then we need a parameterized IEnumerable, which returns null.
  80. // *Observation: if it is empty and it doesn't implement IList,
  81. // it doesn't have a way to get the properties, and will throw an exc
  82. StringCollection str_coll = new StringCollection ();
  83. obj = ListBindingHelper.GetList (str_coll, "Length");
  84. Assert.IsNull (obj, "#H1");
  85. // IEnumerable that returns instances of ICustomTypeDescriptor
  86. // Use DataTable as source, which returns, when enumerating,
  87. // instances of DataRowView, which in turn implement ICustomTypeDescriptor
  88. DataTable table = new DataTable ();
  89. table.Columns.Add ("Id", typeof (int));
  90. table.Rows.Add (666);
  91. object l = ListBindingHelper.GetList (table, "Id");
  92. Assert.AreEqual (666, l, "#J1");
  93. try {
  94. ListBindingHelper.GetList (list_container, "DontExist");
  95. Assert.Fail ("#EXC1");
  96. } catch (ArgumentException) {
  97. }
  98. // Empty IEnumerable not implementing IList
  99. // Don't have a way to know whether at least datamember is valid or not.
  100. try {
  101. stack.Clear ();
  102. obj = ListBindingHelper.GetList (stack, "Value");
  103. Assert.Fail ("#EXC3");
  104. } catch (ArgumentException) {
  105. }
  106. }
  107. internal class ListSource : IListSource
  108. {
  109. bool contains_collection;
  110. public ListSource (bool containsCollection)
  111. {
  112. contains_collection = containsCollection;
  113. }
  114. public bool ContainsListCollection {
  115. get {
  116. return contains_collection;
  117. }
  118. }
  119. public IList GetList ()
  120. {
  121. return new SimpleItem [] { new SimpleItem () };
  122. }
  123. }
  124. class SuperContainer
  125. {
  126. public ListContainer ListContainer
  127. {
  128. get
  129. {
  130. return new ListContainer ();
  131. }
  132. }
  133. }
  134. class ListContainer
  135. {
  136. public IList List {
  137. get {
  138. return new SimpleItem [0];
  139. }
  140. }
  141. public SimpleItem NonList {
  142. get {
  143. return new SimpleItem ();
  144. }
  145. }
  146. }
  147. class SimpleItem
  148. {
  149. int value;
  150. public SimpleItem ()
  151. {
  152. }
  153. public SimpleItem (int value)
  154. {
  155. this.value = value;
  156. }
  157. public int Value
  158. {
  159. get
  160. {
  161. return value;
  162. }
  163. set
  164. {
  165. this.value = value;
  166. }
  167. }
  168. public override int GetHashCode ()
  169. {
  170. return base.GetHashCode ();
  171. }
  172. public override bool Equals (object obj)
  173. {
  174. return value == ((SimpleItem)obj).value;
  175. }
  176. }
  177. [Test]
  178. public void GetListItemPropertiesTest ()
  179. {
  180. SimpleItem [] items = new SimpleItem [0];
  181. PropertyDescriptorCollection properties = ListBindingHelper.GetListItemProperties (items);
  182. Assert.AreEqual (1, properties.Count, "#A1");
  183. Assert.AreEqual ("Value", properties [0].Name, "#A2");
  184. List<SimpleItem> items_list = new List<SimpleItem> ();
  185. properties = ListBindingHelper.GetListItemProperties (items_list);
  186. Assert.AreEqual (1, properties.Count, "#B1");
  187. Assert.AreEqual ("Value", properties [0].Name, "#B2");
  188. // Empty arraylist
  189. ArrayList items_arraylist = new ArrayList ();
  190. properties = ListBindingHelper.GetListItemProperties (items_arraylist);
  191. Assert.AreEqual (0, properties.Count, "#C1");
  192. // Non empty arraylist
  193. items_arraylist.Add (new SimpleItem ());
  194. properties = ListBindingHelper.GetListItemProperties (items_arraylist);
  195. Assert.AreEqual (1, properties.Count, "#D1");
  196. Assert.AreEqual ("Value", properties [0].Name, "#D2");
  197. // non list object
  198. properties = ListBindingHelper.GetListItemProperties (new SimpleItem ());
  199. Assert.AreEqual (1, properties.Count, "#E1");
  200. Assert.AreEqual ("Value", properties [0].Name, "#E2");
  201. // null value
  202. properties = ListBindingHelper.GetListItemProperties (null);
  203. Assert.AreEqual (0, properties.Count, "#F1");
  204. // ListSource
  205. properties = ListBindingHelper.GetListItemProperties (new ListSource (true));
  206. Assert.AreEqual (1, properties.Count, "#G1");
  207. Assert.AreEqual ("Value", properties [0].Name, "#G2");
  208. // ITypedList
  209. DataTable table = new DataTable ();
  210. table.Columns.Add ("Name", typeof (string));
  211. properties = ListBindingHelper.GetListItemProperties (table);
  212. Assert.AreEqual (1, properties.Count, "#H1");
  213. Assert.AreEqual ("Name", properties [0].Name, "#H2");
  214. }
  215. // tests for the overloads of the method
  216. [Test]
  217. [NUnit.Framework.Category ("NotWorking")]
  218. public void GetListItemPropertiesTest2 ()
  219. {
  220. ListContainer list_container = new ListContainer ();
  221. PropertyDescriptorCollection list_properties = TypeDescriptor.GetProperties (list_container);
  222. PropertyDescriptor property = list_properties ["List"];
  223. PropertyDescriptorCollection property_coll = ListBindingHelper.GetListItemProperties (list_container,
  224. new PropertyDescriptor [] { property });
  225. Assert.AreEqual (1, property_coll.Count, "#A1");
  226. Assert.AreEqual ("Value", property_coll [0].Name, "#A2");
  227. // Empty property descriptor array
  228. // Returns list_container properties, since it's not a list
  229. property_coll = ListBindingHelper.GetListItemProperties (list_container, new PropertyDescriptor [0]);
  230. Assert.AreEqual (2, property_coll.Count, "#B1");
  231. // Non list property
  232. // Returns the propeties of the type of that property
  233. property = list_properties ["NonList"];
  234. property_coll = ListBindingHelper.GetListItemProperties (list_container,
  235. new PropertyDescriptor [] { property });
  236. Assert.AreEqual (1, property_coll.Count, "#C1");
  237. Assert.AreEqual ("Value", property_coll [0].Name, "#C2");
  238. // Pass two properties
  239. property = list_properties ["List"];
  240. PropertyDescriptor property2 = list_properties ["NonList"];
  241. property_coll = ListBindingHelper.GetListItemProperties (list_container,
  242. new PropertyDescriptor [] { property2, property });
  243. Assert.AreEqual (0, property_coll.Count, "#D1");
  244. //
  245. // Third overload - doble re-direction
  246. //
  247. SuperContainer super_container = new SuperContainer ();
  248. property = list_properties ["List"];
  249. property_coll = ListBindingHelper.GetListItemProperties (super_container, "ListContainer",
  250. new PropertyDescriptor [] { property });
  251. Assert.AreEqual (1, property_coll.Count, "#E1");
  252. }
  253. [Test]
  254. public void GetListItemTypeTest ()
  255. {
  256. List<int> list = new List<int> ();
  257. DateTime [] date_list = new DateTime [0];
  258. StringCollection string_coll = new StringCollection ();
  259. Assert.AreEqual (typeof (int), ListBindingHelper.GetListItemType (list), "#A1");
  260. Assert.AreEqual (typeof (DateTime), ListBindingHelper.GetListItemType (date_list), "#A2");
  261. Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (string_coll), "#A4");
  262. // Returns the type of the first item if could enumerate
  263. ArrayList arraylist = new ArrayList ();
  264. arraylist.Add ("hellou");
  265. arraylist.Add (3.1416);
  266. Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (arraylist), "#B1");
  267. // Returns the type of the public Item property, not the explicit one
  268. ListView.ColumnHeaderCollection col_collection = new ListView.ColumnHeaderCollection (null);
  269. Assert.AreEqual (typeof (ColumnHeader), ListBindingHelper.GetListItemType (col_collection), "#C1");
  270. ListContainer list_container = new ListContainer ();
  271. String str = "A";
  272. Assert.AreEqual (typeof (IList), ListBindingHelper.GetListItemType (list_container, "List"), "#D1");
  273. Assert.AreEqual (typeof (int), ListBindingHelper.GetListItemType (str, "Length"), "#D2");
  274. // Property doesn't exist - fallback to object type
  275. Assert.AreEqual (typeof (object), ListBindingHelper.GetListItemType (str, "DoesntExist"), "#D3");
  276. // finally, objects that are not array nor list
  277. Assert.AreEqual (typeof (double), ListBindingHelper.GetListItemType (3.1416), "#E1");
  278. Assert.AreEqual (null, ListBindingHelper.GetListItemType (null), "#E2");
  279. }
  280. [Test]
  281. public void GetListNameTest ()
  282. {
  283. List<int> list = new List<int> ();
  284. int [] arr = new int [0];
  285. SimpleItem item = new SimpleItem ();
  286. Assert.AreEqual (typeof (int).Name, ListBindingHelper.GetListName (list, null), "1");
  287. Assert.AreEqual (typeof (int).Name, ListBindingHelper.GetListName (arr, null), "2");
  288. Assert.AreEqual (typeof (SimpleItem).Name, ListBindingHelper.GetListName (item, null), "3");
  289. Assert.AreEqual (String.Empty, ListBindingHelper.GetListName (null, null), "4");
  290. }
  291. }
  292. }
  293. #endif