comparison UnitTests/CS/EditableObjects/NotifyCollectionChangeTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Specialized;
3 using System.ComponentModel;
4 using System.IO;
5 using System.Runtime.Serialization.Formatters.Binary;
6 using System.Windows.Forms;
7 using BLToolkit.EditableObjects;
8 using BLToolkit.Reflection;
9 using NUnit.Framework;
10
11 namespace EditableObjects
12 {
13 [TestFixture]
14 public class NotifyCollectionChangeTest
15 {
16 public abstract class EditableTestObject : EditableObject
17 {
18 public abstract int ID { get; set; }
19 public abstract string Name { get; set; }
20 public abstract int Seconds { get; set; }
21
22 public static EditableTestObject CreateInstance()
23 {
24 return TypeAccessor<EditableTestObject>.CreateInstance();
25 }
26
27 public static EditableTestObject CreateInstance(int id, string name, int seconds)
28 {
29 EditableTestObject instance = CreateInstance();
30
31 instance.ID = id;
32 instance.Name = name;
33 instance.Seconds = seconds;
34
35 return instance;
36 }
37
38 public override string ToString()
39 {
40 return string.Format("EditableTestObject - ID:({0}) Name: ({1}) Seconds({2})", ID, Name, Seconds);
41 }
42 }
43
44 private static readonly EditableTestObject[] _testObjects = new EditableTestObject[6]
45 {
46 EditableTestObject.CreateInstance(0, "Smith", 24),
47 EditableTestObject.CreateInstance(1, "John", 22),
48 EditableTestObject.CreateInstance(2, "Anna", 48),
49 EditableTestObject.CreateInstance(3, "Tim", 56),
50 EditableTestObject.CreateInstance(4, "Xiniu", 39),
51 EditableTestObject.CreateInstance(5, "Kirill", 30)
52 };
53
54 public NotifyCollectionChangeTest()
55 {
56 _testList = new EditableList<EditableTestObject>();
57 _testList.CollectionChanged += TestList_CollectionChanged;
58 }
59
60 private readonly EditableArrayList _testList;
61
62 private void TestList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
63 {
64 EditableArrayList array = sender as EditableArrayList;
65
66 Assert.IsNotNull(array);
67 if (e.Action != NotifyCollectionChangedAction.Reset)
68 Assert.That(array.IsDirty);
69
70 EditableTestObject o = (EditableTestObject)(e.NewItems != null? e.NewItems[0]:
71 e.OldItems != null ? e.OldItems[0]: null);
72
73 Console.WriteLine("CollectionChanged (ID:{3}). Type: {0}, OldIndex: {1}, NewIndex: {2}",
74 e.Action, e.OldStartingIndex, e.NewStartingIndex, o != null? o.ID: -1);
75 }
76
77 [Test]
78 public void TestAdd()
79 {
80 Console.WriteLine("--- TestAdd ---");
81 _testList.Clear();
82 _testList.RemoveSort();
83
84 for (int i = 0; i < 3; i++)
85 _testList.Add(_testObjects[i]);
86
87 Assert.AreEqual(_testList.Count, 3);
88 Assert.AreEqual(_testList[0], _testObjects[0]);
89 Assert.AreEqual(_testList[1], _testObjects[1]);
90 Assert.AreEqual(_testList[2], _testObjects[2]);
91
92 EditableTestObject[] subArray = new EditableTestObject[3];
93
94 for (int i = 3; i < _testObjects.Length; i++)
95 subArray[i-3] = _testObjects[i];
96
97 _testList.AddRange(subArray);
98 Assert.AreEqual(_testList.Count, 6);
99
100 for (int i = 3; i < _testObjects.Length; i++)
101 Assert.AreEqual(subArray[i - 3], _testObjects[i]);
102
103 PrintList();
104
105 _testList.Clear();
106 }
107
108 [Test]
109 public void TestAddNew()
110 {
111 EditableList<EditableTestObject> list = new EditableList<EditableTestObject>();
112 bool collectionChangedFired = false;
113 list.CollectionChanged += delegate { collectionChangedFired = true; };
114
115 list.AddNew();
116 Assert.That(collectionChangedFired);
117 Assert.AreEqual(1, list.Count);
118 Assert.AreEqual(1, list.NewItems.Count);
119 Assert.AreEqual(0, list.DelItems.Count);
120
121 collectionChangedFired = false;
122 list.CancelNew(0);
123 Assert.That(collectionChangedFired);
124 Assert.IsEmpty(list);
125 Assert.IsEmpty(list.NewItems);
126 Assert.IsEmpty(list.DelItems);
127 }
128
129 [Test]
130 public void TestInsert()
131 {
132 Console.WriteLine("--- TestInsert ---");
133 _testList.Clear();
134 _testList.RemoveSort();
135
136 for (int i = 0; i < 3; i++)
137 _testList.Insert(0, _testObjects[i]);
138
139 Assert.AreEqual(_testList.Count, 3);
140 Assert.AreEqual(_testList[0], _testObjects[2]);
141 Assert.AreEqual(_testList[1], _testObjects[1]);
142 Assert.AreEqual(_testList[2], _testObjects[0]);
143
144 EditableTestObject[] _subArray = new EditableTestObject[3];
145
146 for (int i = 3; i < _testObjects.Length; i++)
147 _subArray[i-3] = _testObjects[i];
148
149 _testList.InsertRange(0, _subArray);
150 Assert.AreEqual(_testList.Count, 6);
151
152 Assert.AreEqual(_testList[0], _testObjects[3]);
153 Assert.AreEqual(_testList[1], _testObjects[4]);
154 Assert.AreEqual(_testList[2], _testObjects[5]);
155
156 PrintList();
157
158 _testList.Clear();
159 }
160
161 [Test]
162 public void TestRemove()
163 {
164 Console.WriteLine("--- TestRemove ---");
165
166 _testList.Clear();
167 _testList.RemoveSort();
168 _testList.AddRange(_testObjects);
169
170 _testList.RemoveAt(2);
171 Assert.AreEqual(_testList.Count, 5);
172 _testList.Remove(_testList[0]);
173 Assert.AreEqual(_testList.Count, 4);
174 _testList.Clear();
175 Assert.AreEqual(_testList.Count, 0);
176 }
177
178 [Test]
179 public void TestRemoveRange()
180 {
181 Console.WriteLine("--- TestRemoveRange ---");
182
183 _testList.Clear();
184 _testList.RemoveSort();
185 _testList.AcceptChanges();
186 _testList.AddRange(_testObjects);
187
188 Assert.AreEqual(6, _testList.Count);
189 Assert.AreEqual(6, _testList.NewItems.Count);
190 Assert.AreEqual(0, _testList.DelItems.Count);
191
192 _testList.RemoveRange(1, 3);
193
194 Assert.AreEqual(3, _testList.Count);
195 Assert.AreEqual(3, _testList.NewItems.Count);
196 Assert.AreEqual(0, _testList.DelItems.Count);
197 Assert.AreEqual(0, ((EditableTestObject)_testList[0]).ID);
198 Assert.AreEqual(4, ((EditableTestObject)_testList[1]).ID);
199
200 _testList.AcceptChanges();
201
202 Assert.AreEqual(3, _testList.Count);
203 Assert.AreEqual(0, _testList.NewItems.Count);
204 Assert.AreEqual(0, _testList.DelItems.Count);
205
206 _testList.RemoveRange(0, 1);
207
208 Assert.AreEqual(2, _testList.Count);
209 Assert.AreEqual(0, _testList.NewItems.Count);
210 Assert.AreEqual(1, _testList.DelItems.Count);
211
212 _testList.Clear();
213
214 Assert.AreEqual(0, _testList.Count);
215 Assert.AreEqual(0, _testList.NewItems.Count);
216 Assert.AreEqual(3, _testList.DelItems.Count);
217
218 _testList.AcceptChanges();
219
220 Assert.AreEqual(0, _testList.Count);
221 Assert.AreEqual(0, _testList.NewItems.Count);
222 Assert.AreEqual(0, _testList.DelItems.Count);
223 }
224
225 [Test]
226 public void TestSortedAdd()
227 {
228 Console.WriteLine("--- TestSortedAdd ---");
229 _testList.Clear();
230 _testList.RemoveSort();
231 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Descending);
232
233 for (int i = 0; i < 3; i++)
234 _testList.Add(_testObjects[i]);
235
236 Assert.AreEqual(_testList.Count, 3);
237 Assert.AreEqual(_testList[1], _testObjects[0]);
238 Assert.AreEqual(_testList[2], _testObjects[1]);
239 Assert.AreEqual(_testList[0], _testObjects[2]);
240
241 EditableTestObject[] _subArray = new EditableTestObject[3];
242
243 for (int i = 3; i < _testObjects.Length; i++)
244 _subArray[i-3] = _testObjects[i];
245
246 _testList.AddRange(_subArray);
247 Assert.AreEqual(_testList.Count, 6);
248
249 Assert.AreEqual(_testList[0], _testObjects[3]);
250 Assert.AreEqual(_testList[2], _testObjects[4]);
251 Assert.AreEqual(_testList[3], _testObjects[5]);
252
253 PrintList();
254
255 _testList.Clear();
256 Assert.AreEqual(_testList.Count, 0);
257
258 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Ascending);
259
260 for (int i = 0; i < 3; i++)
261 _testList.Add(_testObjects[i]);
262
263 Assert.AreEqual(_testList.Count, 3);
264 Assert.AreEqual(_testList[1], _testObjects[0]);
265 Assert.AreEqual(_testList[0], _testObjects[1]);
266 Assert.AreEqual(_testList[2], _testObjects[2]);
267
268 _testList.AddRange(_subArray);
269 Assert.AreEqual(_testList.Count, 6);
270
271 Assert.AreEqual(_testList[5], _testObjects[3]);
272 Assert.AreEqual(_testList[3], _testObjects[4]);
273 Assert.AreEqual(_testList[2], _testObjects[5]);
274
275 PrintList();
276
277 _testList.Clear();
278 }
279
280 [Test]
281 public void TestSortedInsert()
282 {
283 Console.WriteLine("--- TestSortedInsert ---");
284 _testList.Clear();
285 _testList.RemoveSort();
286 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Descending);
287
288 for (int i = 0; i < 3; i++)
289 _testList.Insert(0, _testObjects[i]);
290
291 Assert.AreEqual(_testList.Count, 3);
292 Assert.AreEqual(_testList[0], _testObjects[2]);
293 Assert.AreEqual(_testList[1], _testObjects[0]);
294 Assert.AreEqual(_testList[2], _testObjects[1]);
295
296 EditableTestObject[] _subArray = new EditableTestObject[3];
297
298 for (int i = 3; i < _testObjects.Length; i++)
299 _subArray[i-3] = _testObjects[i];
300
301 _testList.InsertRange(0, _subArray);
302 Assert.AreEqual(_testList.Count, 6);
303
304 Assert.AreEqual(_testList[0], _testObjects[3]);
305 Assert.AreEqual(_testList[2], _testObjects[4]);
306 Assert.AreEqual(_testList[3], _testObjects[5]);
307
308 PrintList();
309
310 _testList.Clear();
311 Assert.AreEqual(_testList.Count, 0);
312
313 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Ascending);
314
315 for (int i = 0; i < 3; i++)
316 _testList.Insert(0, _testObjects[i]);
317
318 Assert.AreEqual(_testList.Count, 3);
319 Assert.AreEqual(_testList[0], _testObjects[1]);
320 Assert.AreEqual(_testList[1], _testObjects[0]);
321 Assert.AreEqual(_testList[2], _testObjects[2]);
322
323 _testList.InsertRange(0, _subArray);
324 Assert.AreEqual(_testList.Count, 6);
325
326 Assert.AreEqual(_testList[2], _testObjects[5]);
327 Assert.AreEqual(_testList[3], _testObjects[4]);
328 Assert.AreEqual(_testList[5], _testObjects[3]);
329
330 PrintList();
331
332 _testList.Clear();
333 }
334
335 [Test]
336 public void TestSortedPropertyChange()
337 {
338 Console.WriteLine("--- TestSortedPropertyChange ---");
339 _testList.Clear();
340 _testList.RemoveSort();
341 _testList.ApplySort(_testList.GetItemProperties(null)["Seconds"], ListSortDirection.Descending);
342 _testList.AddRange(_testObjects);
343
344 EditableTestObject eto = EditableTestObject.CreateInstance(6, "Dummy", 10);
345
346 _testList.Add(eto);
347 Assert.AreEqual(_testList.Count, 7);
348 Assert.AreEqual(_testList[6], eto);
349
350 eto.Seconds = 20;
351 Assert.AreEqual(_testList[6], eto);
352
353 eto.Seconds = 23;
354 Assert.AreEqual(_testList[5], eto);
355
356 eto.Seconds = 30;
357 Assert.AreEqual(_testList[4], eto);
358
359 eto.Seconds = 40;
360 Assert.AreEqual(_testList[2], eto);
361
362 eto.Seconds = 50;
363 Assert.AreEqual(_testList[1], eto);
364
365 eto.Seconds = 60;
366 Assert.AreEqual(_testList[0], eto);
367 }
368
369 private void PrintList()
370 {
371 Console.WriteLine("--- Print List ---");
372 foreach (EditableTestObject o in _testList)
373 Console.WriteLine(o);
374 }
375
376 [Test]
377 public void SerializationTest()
378 {
379 //Configuration.NotifyOnEqualSet = true;
380
381 SerializableObject test = TypeAccessor<SerializableObject>.CreateInstance();
382
383 MemoryStream stream = new MemoryStream();
384 BinaryFormatter bf = new BinaryFormatter();
385
386 bf.Serialize(stream, test);
387
388 //Configuration.NotifyOnChangesOnly = false;
389 }
390
391 //////[Test] Resharpe 8 issue
392 public void SerializationTest2()
393 {
394 //Configuration.NotifyOnChangesOnly = true;
395
396 EditableList<SerializableObject> list = new EditableList<SerializableObject>();
397
398 list.Add(TypeAccessor<SerializableObject>.CreateInstance());
399
400 BinaryFormatter formatter = new BinaryFormatter();
401
402 using (MemoryStream stream = new MemoryStream())
403 {
404 formatter.Serialize(stream, list);
405 stream.Position = 0;
406
407 object result = formatter.Deserialize(stream);
408
409 Assert.IsNotNull(result);
410
411 EditableList<SerializableObject> eal = (EditableList<SerializableObject>)result;
412
413 Console.WriteLine(eal.Count);
414
415 eal.CollectionChanged += eal_CollectionChanged;
416
417 eal[0].ID = 0;
418 _notificationCount = 0;
419 eal[0].ID = -100;
420 eal[0].ID = -100;
421 eal[0].ID = -100;
422 eal[0].ID = -100;
423 eal[0].ID = -100;
424 eal[0].ID = -100;
425
426 Console.WriteLine(eal.Count);
427
428 //Assert.AreEqual(_notificationCount, 1);
429 }
430
431 //Configuration.NotifyOnChangesOnly = false;
432 }
433
434 private static int _notificationCount = 0;
435
436 static void eal_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
437 {
438 Console.WriteLine(e.Action);
439 _notificationCount++;
440 }
441
442 [Test]
443 public void SortTest()
444 {
445 EditableList<EditableTestObject> dataList = new EditableList<EditableTestObject>();
446
447 dataList.Add(EditableTestObject.CreateInstance(1, "John", 60));
448 dataList.Add(EditableTestObject.CreateInstance(1, "John", 60));
449 dataList.Add(EditableTestObject.CreateInstance(1, "John", 60));
450 dataList.Add(EditableTestObject.CreateInstance(2, "Tester", 70));
451 dataList.Add(EditableTestObject.CreateInstance(2, "Tester", 70));
452 dataList.Add(EditableTestObject.CreateInstance(2, "Tester", 70));
453 dataList.Add(EditableTestObject.CreateInstance(3, "Tester", 70));
454 dataList.Add(EditableTestObject.CreateInstance(3, "Tester", 70));
455 dataList.Add(EditableTestObject.CreateInstance(3, "Tester", 70));
456
457 BindingSource bindingSource = new BindingSource(dataList, null);
458 bindingSource.Sort = "ID";
459
460 int prev = 0;
461 foreach (EditableTestObject o in dataList)
462 {
463 Assert.IsTrue(o.ID >= prev);
464 prev = o.ID;
465 }
466
467 bindingSource[0] = EditableTestObject.CreateInstance(2, "John", 60);
468
469 prev = 0;
470 foreach (EditableTestObject o in dataList)
471 {
472 Assert.IsTrue(o.ID >= prev);
473 prev = o.ID;
474 }
475 }
476
477 public class DerivedEditableList<T> : EditableList<T>
478 {
479 public event EventHandler OnListChangedCalled;
480
481 protected void OnOnListChangedCalled()
482 {
483 if (OnListChangedCalled != null)
484 OnListChangedCalled(this, EventArgs.Empty);
485 }
486
487 protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
488 {
489 OnOnListChangedCalled();
490 base.OnCollectionChanged(e);
491 }
492 }
493
494
495 [Test]
496 public void DerivedOnListChanged()
497 {
498 bool called = false;
499
500 DerivedEditableList<int> list = new DerivedEditableList<int>();
501 list.OnListChangedCalled += delegate
502 {
503 called = true;
504 };
505
506 list.Add(1);
507
508 Assert.IsTrue(called);
509 Assert.AreEqual(1, list.NewItems.Count);
510 Assert.AreEqual(1, list.Count);
511 Assert.AreEqual(0, list.DelItems.Count);
512
513 called = false;
514 list.RemoveAt(0);
515
516 Assert.IsTrue(called);
517 Assert.AreEqual(0, list.NewItems.Count);
518 Assert.AreEqual(0, list.Count);
519 Assert.AreEqual(0, list.DelItems.Count);
520 }
521
522 }
523 }