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

Language C# Lines 482
MD5 Hash 2a23c5b3683710f7c432b85fbba1d495 Estimated Cost $8,381 (why?)
Repository https://bitbucket.org/foobar22/mono.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//
// ListBindingHelperTest.cs: Test cases for ListBindingHelper class.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// Author:
// 	Carlos Alberto Cortez <calberto.cortez@gmail.com>
//
// (C) 2008 Novell, Inc. (http://www.novell.com)
//

#if NET_2_0

using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

using NUnit.Framework;

namespace MonoTests.System.Windows.Forms
{
	[TestFixture]
	public class ListBindingHelperTest : TestHelper
	{
		[Test]
		public void GetListTest ()
		{
			ListSource lsource = new ListSource (true);
			Stack stack = new Stack ();
			stack.Push (3);

			Assert.IsTrue (ListBindingHelper.GetList (lsource) is SimpleItem [], "#A1");
			Assert.AreEqual ("NonList", ListBindingHelper.GetList ("NonList"), "#A2");
			Assert.AreEqual (null, ListBindingHelper.GetList (null), "#A3");
			Assert.AreEqual (stack, ListBindingHelper.GetList (stack), "#A4"); // IEnumerable

			Assert.IsTrue (ListBindingHelper.GetList (lsource, String.Empty) is SimpleItem [], "#B1");
			Assert.AreEqual ("NonList", ListBindingHelper.GetList ("NonList", String.Empty), "#B2");
			Assert.AreEqual (null, ListBindingHelper.GetList (null, "DontExist"), "#B3");
			Assert.IsTrue (ListBindingHelper.GetList (lsource, null) is SimpleItem [], "#B4");

			ListContainer list_container = new ListContainer ();
			Assert.AreEqual (new object [0], ListBindingHelper.GetList (list_container, "List"), "#C1");

			// Even if IListSource.ContainsListCollection is false, we return the result of GetList ()
			lsource = new ListSource (false);
			Assert.IsTrue (ListBindingHelper.GetList (lsource) is SimpleItem [], "#D1");

			// DataMember is not if IList type
			Assert.AreEqual (new SimpleItem (), ListBindingHelper.GetList (list_container, "NonList"), "#E1");

			// List (IEnumerable)
			stack.Clear ();
			stack.Push (new SimpleItem (3));
			stack.Push (new SimpleItem (7));
			object obj = ListBindingHelper.GetList (stack, "Value");
			Assert.IsTrue (obj != null, "#F1");
			Assert.IsTrue (obj is int, "#F2");
			Assert.AreEqual (7, (int) obj, "#F3");

			// ListSource returning an IEnumerable,
			// which in turn retrieves dataMember
			obj = ListBindingHelper.GetList (lsource, "Value");
			Assert.IsTrue (obj != null, "#G1");
			Assert.IsTrue (obj is int, "#G2");
			Assert.AreEqual (0, (int)obj, "#G3");

			// Empty IEnumerable - valid property for list item type
			// Since it's empty, it needs to check whether the datamember is
			// a valid value, and thus we need the datasource to also be IList
			// Then we need a parameterized IEnumerable, which returns null.
			// *Observation: if it is empty and it doesn't implement IList,
			// it doesn't have a way to get the properties, and will throw an exc
			StringCollection str_coll = new StringCollection ();
			obj = ListBindingHelper.GetList (str_coll, "Length");
			Assert.IsNull (obj, "#H1");

			// IEnumerable that returns instances of ICustomTypeDescriptor
			// Use DataTable as source, which returns, when enumerating,
			// instances of DataRowView, which in turn implement ICustomTypeDescriptor
			DataTable table = new DataTable ();
			table.Columns.Add ("Id", typeof (int));
			table.Rows.Add (666);
			object l = ListBindingHelper.GetList (table, "Id");
			Assert.AreEqual (666, l, "#J1");

			try {
				ListBindingHelper.GetList (list_container, "DontExist");
				Assert.Fail ("#EXC1");
			} catch (ArgumentException) {
			}

			// Empty IEnumerable not implementing IList
			// Don't have a way to know whether at least datamember is valid or not.
			try {
				stack.Clear ();
				obj = ListBindingHelper.GetList (stack, "Value");
				Assert.Fail ("#EXC3");
			} catch (ArgumentException) {
			}

		}

		internal class ListSource : IListSource
		{
			bool contains_collection;

			public ListSource (bool containsCollection)
			{
				contains_collection = containsCollection;
			}

			public bool ContainsListCollection {
				get {
					return contains_collection;
				}
			}

			public IList GetList ()
			{
				return new SimpleItem [] { new SimpleItem () };
			}
		}

		class SuperContainer
		{
			public ListContainer ListContainer
			{
				get
				{
					return new ListContainer ();
				}
			}
		}

		class ListContainer
		{
			public IList List {
				get {
					return new SimpleItem [0];
				}
			}

			public SimpleItem NonList {
				get {
					return new SimpleItem ();
				}
			}
		}

		class SimpleItem
		{
			int value;

			public SimpleItem ()
			{
			}

			public SimpleItem (int value)
			{
				this.value = value;
			}

			public int Value
			{
				get
				{
					return value;
				}
				set
				{
					this.value = value;
				}
			}

			public override int GetHashCode ()
			{
				return base.GetHashCode ();
			}

			public override bool Equals (object obj)
			{
				return value == ((SimpleItem)obj).value;
			}
		}

		[Test]
		public void GetListItemPropertiesTest ()
		{
			SimpleItem [] items = new SimpleItem [0];
			PropertyDescriptorCollection properties = ListBindingHelper.GetListItemProperties (items);

			Assert.AreEqual (1, properties.Count, "#A1");
			Assert.AreEqual ("Value", properties [0].Name, "#A2");

			List<SimpleItem> items_list = new List<SimpleItem> ();
			properties = ListBindingHelper.GetListItemProperties (items_list);

			Assert.AreEqual (1, properties.Count, "#B1");
			Assert.AreEqual ("Value", properties [0].Name, "#B2");

			// Empty arraylist
			ArrayList items_arraylist = new ArrayList ();
			properties = ListBindingHelper.GetListItemProperties (items_arraylist);

			Assert.AreEqual (0, properties.Count, "#C1");

			// Non empty arraylist
			items_arraylist.Add (new SimpleItem ());
			properties = ListBindingHelper.GetListItemProperties (items_arraylist);

			Assert.AreEqual (1, properties.Count, "#D1");
			Assert.AreEqual ("Value", properties [0].Name, "#D2");

			// non list object
			properties = ListBindingHelper.GetListItemProperties (new SimpleItem ());

			Assert.AreEqual (1, properties.Count, "#E1");
			Assert.AreEqual ("Value", properties [0].Name, "#E2");

			// null value
			properties = ListBindingHelper.GetListItemProperties (null);

			Assert.AreEqual (0, properties.Count, "#F1");

			// ListSource
			properties = ListBindingHelper.GetListItemProperties (new ListSource (true));

			Assert.AreEqual (1, properties.Count, "#G1");
			Assert.AreEqual ("Value", properties [0].Name, "#G2");

			// ITypedList
			DataTable table = new DataTable ();
			table.Columns.Add ("Name", typeof (string));

			properties = ListBindingHelper.GetListItemProperties (table);
			Assert.AreEqual (1, properties.Count, "#H1");
			Assert.AreEqual ("Name", properties [0].Name, "#H2");
		}

		// tests for the overloads of the method
		[Test]
		[NUnit.Framework.Category ("NotWorking")]
		public void GetListItemPropertiesTest2 ()
		{
			ListContainer list_container = new ListContainer ();
			PropertyDescriptorCollection list_properties = TypeDescriptor.GetProperties (list_container);
			PropertyDescriptor property = list_properties ["List"];

			PropertyDescriptorCollection property_coll = ListBindingHelper.GetListItemProperties (list_container,
				new PropertyDescriptor [] { property });
			Assert.AreEqual (1, property_coll.Count, "#A1");
			Assert.AreEqual ("Value", property_coll [0].Name, "#A2");

			// Empty property descriptor array 
			// Returns list_container properties, since it's not a list
			property_coll = ListBindingHelper.GetListItemProperties (list_container, new PropertyDescriptor [0]);
			Assert.AreEqual (2, property_coll.Count, "#B1");

			// Non list property
			// Returns the propeties of the type of that property
			property = list_properties ["NonList"];
			property_coll = ListBindingHelper.GetListItemProperties (list_container,
				new PropertyDescriptor [] { property });
			Assert.AreEqual (1, property_coll.Count, "#C1");
			Assert.AreEqual ("Value", property_coll [0].Name, "#C2");

			// Pass two properties
			property = list_properties ["List"];
			PropertyDescriptor property2 = list_properties ["NonList"];
			property_coll = ListBindingHelper.GetListItemProperties (list_container,
				new PropertyDescriptor [] { property2, property });
			Assert.AreEqual (0, property_coll.Count, "#D1");

			//
			// Third overload - doble re-direction
			//
			SuperContainer super_container = new SuperContainer ();
			property = list_properties ["List"];

			property_coll = ListBindingHelper.GetListItemProperties (super_container, "ListContainer",
				new PropertyDescriptor [] { property });
			Assert.AreEqual (1, property_coll.Count, "#E1");
		}

		[Test]
		public void GetListItemTypeTest ()
		{
			List<int> list = new List<int> ();
			DateTime [] date_list = new DateTime [0];
			StringCollection string_coll = new StringCollection ();

			Assert.AreEqual (typeof (int), ListBindingHelper.GetListItemType (list), "#A1");
			Assert.AreEqual (typeof (DateTime), ListBindingHelper.GetListItemType (date_list), "#A2");
			Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (string_coll), "#A4");

			// Returns the type of the first item if could enumerate
			ArrayList arraylist = new ArrayList ();
			arraylist.Add ("hellou");
			arraylist.Add (3.1416);
			Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (arraylist), "#B1");

			// Returns the type of the public Item property, not the explicit one
			ListView.ColumnHeaderCollection col_collection = new ListView.ColumnHeaderCollection (null);
			Assert.AreEqual (typeof (ColumnHeader), ListBindingHelper.GetListItemType (col_collection), "#C1");

			ListContainer list_container = new ListContainer ();
			String str = "A";
			Assert.AreEqual (typeof (IList), ListBindingHelper.GetListItemType (list_container, "List"), "#D1");
			Assert.AreEqual (typeof (int), ListBindingHelper.GetListItemType (str, "Length"), "#D2");
			// Property doesn't exist - fallback to object type
			Assert.AreEqual (typeof (object), ListBindingHelper.GetListItemType (str, "DoesntExist"), "#D3");

			// finally, objects that are not array nor list
			Assert.AreEqual (typeof (double), ListBindingHelper.GetListItemType (3.1416), "#E1");
			Assert.AreEqual (null, ListBindingHelper.GetListItemType (null), "#E2");

			// bug #507120 - an IEnumerator instance with a Current value returning null,
			// falling back to IList.this detection, if possible
			Assert.AreEqual (typeof (string), ListBindingHelper.GetListItemType (new NullEnumerable (), null), "#F1");
		}

		// useless class that help us with a simple enumerator with a null Current property
		// and implementing IList to let the ListBindingHelper get info from the this [] property
		class NullEnumerable : IList, ICollection, IEnumerable
		{
			public IEnumerator GetEnumerator ()
			{
				return new NullEnumerator ();
			}

			class NullEnumerator : IEnumerator
			{
				int pos = -1;

				// the idea is that we just move one time - the first time
				public bool MoveNext ()
				{
					if (pos > -1)
						return false;

					pos = 0;
					return true;
				}

				public void Reset ()
				{
					pos = -1;
				}

				public object Current {
					get {
						return null;
					}
				}
			}

			// make this return a string, and hide the interface impl,
			// so we are sure ListBindingHelper is actually accessing this property
			public string this [int index] {
				get {
					if (index != 0)
						throw new ArgumentOutOfRangeException ("index");

					return null;
				}
				set {
				}
			}

			object IList.this [int index] {
				get {
					return this [index];
				}
				set {
				}
			}

			public int Add (object o)
			{
				return 0;
			}

			public void Clear ()
			{
			}

			public bool Contains (object o)
			{
				return false;
			}

			public int IndexOf (object o)
			{
				return -1;
			}

			public void Insert (int index, object o)
			{
			}

			public void Remove (object o)
			{
			}

			public void RemoveAt (int index)
			{
			}

			public bool IsFixedSize {
				get {
					return true;
				}
			}

			public bool IsReadOnly {
				get {
					return true;
				}
			}

			public void CopyTo (Array array, int offset)
			{
			}

			public int Count {
				get {
					return 1;
				}
			}

			public bool IsSynchronized {
				get {
					return false;
				}
			}

			public object SyncRoot {
				get {
					return this;
				}
			}
		}

		[Test]
		public void GetListNameTest ()
		{
			List<int> list = new List<int> ();
			int [] arr = new int [0];
			SimpleItem item = new SimpleItem ();

			Assert.AreEqual (typeof (int).Name, ListBindingHelper.GetListName (list, null), "1");
			Assert.AreEqual (typeof (int).Name, ListBindingHelper.GetListName (arr, null), "2");
			Assert.AreEqual (typeof (SimpleItem).Name, ListBindingHelper.GetListName (item, null), "3");
			Assert.AreEqual (String.Empty, ListBindingHelper.GetListName (null, null), "4");
		}
	}
}

#endif
Back to Top