PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ListBindingHelperTest.cs

https://bitbucket.org/danipen/mono
C# | 481 lines | 328 code | 85 blank | 68 comment | 6 complexity | 2a23c5b3683710f7c432b85fbba1d495 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  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 <calberto.cortez@gmail.com>
  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. // bug #507120 - an IEnumerator instance with a Current value returning null,
  280. // falling back to IList.this detection, if possible
  281. Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (new NullEnumerable (), null), "#F1");
  282. }
  283. // useless class that help us with a simple enumerator with a null Current property
  284. // and implementing IList to let the ListBindingHelper get info from the this [] property
  285. class NullEnumerable : IList, ICollection, IEnumerable
  286. {
  287. public IEnumerator GetEnumerator ()
  288. {
  289. return new NullEnumerator ();
  290. }
  291. class NullEnumerator : IEnumerator
  292. {
  293. int pos = -1;
  294. // the idea is that we just move one time - the first time
  295. public bool MoveNext ()
  296. {
  297. if (pos > -1)
  298. return false;
  299. pos = 0;
  300. return true;
  301. }
  302. public void Reset ()
  303. {
  304. pos = -1;
  305. }
  306. public object Current {
  307. get {
  308. return null;
  309. }
  310. }
  311. }
  312. // make this return a string, and hide the interface impl,
  313. // so we are sure ListBindingHelper is actually accessing this property
  314. public string this [int index] {
  315. get {
  316. if (index != 0)
  317. throw new ArgumentOutOfRangeException ("index");
  318. return null;
  319. }
  320. set {
  321. }
  322. }
  323. object IList.this [int index] {
  324. get {
  325. return this [index];
  326. }
  327. set {
  328. }
  329. }
  330. public int Add (object o)
  331. {
  332. return 0;
  333. }
  334. public void Clear ()
  335. {
  336. }
  337. public bool Contains (object o)
  338. {
  339. return false;
  340. }
  341. public int IndexOf (object o)
  342. {
  343. return -1;
  344. }
  345. public void Insert (int index, object o)
  346. {
  347. }
  348. public void Remove (object o)
  349. {
  350. }
  351. public void RemoveAt (int index)
  352. {
  353. }
  354. public bool IsFixedSize {
  355. get {
  356. return true;
  357. }
  358. }
  359. public bool IsReadOnly {
  360. get {
  361. return true;
  362. }
  363. }
  364. public void CopyTo (Array array, int offset)
  365. {
  366. }
  367. public int Count {
  368. get {
  369. return 1;
  370. }
  371. }
  372. public bool IsSynchronized {
  373. get {
  374. return false;
  375. }
  376. }
  377. public object SyncRoot {
  378. get {
  379. return this;
  380. }
  381. }
  382. }
  383. [Test]
  384. public void GetListNameTest ()
  385. {
  386. List<int> list = new List<int> ();
  387. int [] arr = new int [0];
  388. SimpleItem item = new SimpleItem ();
  389. Assert.AreEqual (typeof (int).Name, ListBindingHelper.GetListName (list, null), "1");
  390. Assert.AreEqual (typeof (int).Name, ListBindingHelper.GetListName (arr, null), "2");
  391. Assert.AreEqual (typeof (SimpleItem).Name, ListBindingHelper.GetListName (item, null), "3");
  392. Assert.AreEqual (String.Empty, ListBindingHelper.GetListName (null, null), "4");
  393. }
  394. }
  395. }
  396. #endif