1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2006 Novell, Inc.
23 // Jackson Harper jackson@ximian.com
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Reflection;
31 using System.Windows.Forms;
33 using NUnit.Framework;
35 namespace MonoTests.System.Windows.Forms {
38 public class CurrencyManagerTest {
41 public void Defaults ()
43 BindingContext bc = new BindingContext ();
44 ArrayList data_source = new ArrayList ();
45 CurrencyManager cm = bc [data_source] as CurrencyManager;
47 Assert.AreSame (data_source, cm.List, "DEFAULTS1");
48 Assert.AreEqual (0, cm.Count, "DEFAULTS2");
49 Assert.AreEqual (-1, cm.Position, "DEFAULTS3");
53 [ExpectedException (typeof (IndexOutOfRangeException))]
54 public void UninitializedCurrent ()
56 BindingContext bc = new BindingContext ();
57 ArrayList data_source = new ArrayList ();
58 CurrencyManager cm = bc [data_source] as CurrencyManager;
60 // This line should throw
61 Assert.AreSame (null, cm.Current, "CurrentOfEmpty");
65 public void DataSetList ()
67 DataSet dataset = new DataSet ("DataSet");
68 DataTable table = new DataTable ("Table");
69 BindingContext bc = new BindingContext ();
72 dataset.Tables.Add (table);
73 cm = bc [dataset] as CurrencyManager;
75 Assert.AreEqual (typeof (DataViewManager), cm.List.GetType (), "DATASETLIST1");
76 Assert.AreEqual (1, cm.Count, "DATASETLIST2");
77 Assert.AreEqual (0, cm.Position, "DATASETLIST3");
78 // Assert.AreEqual (typeof (DataViewManagerListItemTypeDescriptor), cm.Current.GetType (),
83 public void DataSetListTable ()
85 DataSet dataset = new DataSet ("DataSet");
86 DataTable table = new DataTable ("Table");
87 BindingContext bc = new BindingContext ();
90 dataset.Tables.Add (table);
91 cm = bc [dataset, "Table"] as CurrencyManager;
93 Assert.AreEqual (typeof (DataView), cm.List.GetType (), "DATASETLIST1");
94 Assert.AreEqual (0, cm.Count, "DATASETLIST2");
95 Assert.AreEqual (-1, cm.Position, "DATASETLIST3");
99 [ExpectedException (typeof (ArgumentException))]
100 public void DataSetListTableBogusField ()
102 DataSet dataset = new DataSet ("DataSet");
103 DataTable table = new DataTable ("Table.Column");
104 BindingContext bc = new BindingContext ();
107 dataset.Tables.Add (table);
109 // child list can't be created
110 cm = bc [dataset, "Table"] as CurrencyManager;
114 public void MoveArrayListForward ()
116 ArrayList data_source = new ArrayList ();
117 BindingContext bc = new BindingContext ();
119 for (int i = 0; i < 10; i++)
120 data_source.Add (new object ());
122 CurrencyManager cm = bc [data_source] as CurrencyManager;
123 for (int i = 0; i < 10; i++) {
124 Assert.AreSame (data_source [i], cm.Current, "MOVEALF" + i);
131 Assert.AreSame (data_source [9], cm.Current, "MOVEALFEND");
135 public void MoveArrayListBackward ()
137 ArrayList data_source = new ArrayList ();
138 BindingContext bc = new BindingContext ();
140 for (int i = 0; i < 10; i++)
141 data_source.Add (new object ());
143 CurrencyManager cm = bc [data_source] as CurrencyManager;
145 for (int i = 9; i >= 0; i--) {
146 Assert.AreSame (data_source [i], cm.Current, "MOVEALB" + i);
153 Assert.AreSame (data_source [0], cm.Current, "MOVEALBSTART");
157 public void SetPositionArrayList ()
159 ArrayList data_source = new ArrayList ();
160 BindingContext bc = new BindingContext ();
162 for (int i = 0; i < 10; i++)
163 data_source.Add (new object ());
165 CurrencyManager cm = bc [data_source] as CurrencyManager;
166 for (int i = 3; i >= 0; i--) {
168 Assert.AreSame (data_source [i], cm.Current, "MOVEAL1-" + i);
173 for (int i = 0; i < 10; i++) {
175 Assert.AreSame (data_source [i], cm.Current, "MOVEAL2-" + i);
178 for (int i = 5; i < 10; i++) {
180 Assert.AreSame (data_source [i], cm.Current, "MOVEAL3-" + i);
185 public void LateBuildDataTable ()
187 DataTable data_source = new DataTable ("Table");
188 BindingContext bc = new BindingContext ();
189 CurrencyManager cm = bc [data_source] as CurrencyManager;
191 Assert.AreEqual (-1, cm.Position, "LATEBUILDTABLE1");
192 Assert.AreEqual (0, cm.Count, "LATEBUILDTABLE2");
194 DataColumn column = new DataColumn ("Column");
195 column.DataType = typeof (int);
196 data_source.Columns.Add (column);
198 for (int i = 0; i < 10; i++) {
199 DataRow row = data_source.NewRow ();
201 data_source.Rows.Add (row);
204 Assert.AreEqual (0, cm.Position, "LATEBUILDTABLE3");
205 Assert.AreEqual (10, cm.Count, "LATEBUILDTABLE4");
209 public void LateBuildArrayList ()
211 ArrayList data_source = new ArrayList ();
212 BindingContext bc = new BindingContext ();
213 CurrencyManager cm = bc [data_source] as CurrencyManager;
215 Assert.AreEqual (-1, cm.Position, "LATEBUILDLIST1");
216 Assert.AreEqual (0, cm.Count, "LATEBUILDLIST2");
218 data_source.AddRange (new object [] { 1, 2, 3, 4, 5, 6, 7 });
220 Assert.AreEqual (-1, cm.Position, "LATEBUILDLIST3");
221 Assert.AreEqual (7, cm.Count, "LATEBUILDLIST4");
225 public void MoveDataTableForward ()
227 DataTable data_source = new DataTable ("Table");
228 BindingContext bc = new BindingContext ();
229 CurrencyManager cm = bc [data_source] as CurrencyManager;
230 DataColumn column = new DataColumn ("Column");
232 column.DataType = typeof (int);
233 data_source.Columns.Add (column);
234 for (int i = 0; i < 10; i++) {
235 DataRow row = data_source.NewRow ();
237 data_source.Rows.Add (row);
241 for (int i = 0; i < 10; i++) {
242 DataRowView row = cm.Current as DataRowView;
243 Assert.IsFalse (row == null, "MOVETABLEF-NULL-" + i);
244 Assert.AreEqual (row ["Column"], i, "MOVETABLEF-" + i);
251 Assert.AreEqual (9, ((DataRowView) cm.Current) ["Column"], "MOVETABLEF-END");
255 public void MoveDataTableBackward ()
257 DataTable data_source = new DataTable ("Table");
258 BindingContext bc = new BindingContext ();
259 CurrencyManager cm = bc [data_source] as CurrencyManager;
260 DataColumn column = new DataColumn ("Column");
262 column.DataType = typeof (int);
263 data_source.Columns.Add (column);
264 for (int i = 0; i < 10; i++) {
265 DataRow row = data_source.NewRow ();
267 data_source.Rows.Add (row);
272 for (int i = 9; i >= 0; i--) {
273 DataRowView row = cm.Current as DataRowView;
274 Assert.IsFalse (row == null, "MOVETABLEB-NULL-" + i);
275 Assert.AreEqual (row ["Column"], i, "MOVETABLEB-" + i);
282 Assert.AreEqual (0, ((DataRowView) cm.Current) ["Column"], "MOVETABLEB-START");
286 public void SetPositionDataTable ()
288 DataTable data_source = new DataTable ("Table");
289 BindingContext bc = new BindingContext ();
290 CurrencyManager cm = bc [data_source] as CurrencyManager;
291 DataColumn column = new DataColumn ("Column");
293 column.DataType = typeof (int);
294 data_source.Columns.Add (column);
295 for (int i = 0; i < 10; i++) {
296 DataRow row = data_source.NewRow ();
298 data_source.Rows.Add (row);
302 for (int i = 5; i < 10; i++) {
304 DataRowView row = cm.Current as DataRowView;
305 Assert.IsFalse (row == null, "SETTABLE1-NULL-" + i);
306 Assert.AreEqual (row ["Column"], i, "SETTABLE1-" + i);
309 for (int i = 5; i >= 0; i--) {
311 DataRowView row = cm.Current as DataRowView;
312 Assert.IsFalse (row == null, "SETTABLE2-NULL-" + i);
313 Assert.AreEqual (row ["Column"], i, "SETTABLE2-" + i);
318 public void NavigateDataSetToTable ()
320 DataSet data_source = new DataSet ("DataSet");
321 DataTable table = new DataTable ("Table");
322 DataColumn column = new DataColumn ("Column");
323 BindingContext bc = new BindingContext ();
325 data_source.Tables.Add (table);
327 column.DataType = typeof (int);
328 table.Columns.Add (column);
329 for (int i = 0; i < 10; i++) {
330 DataRow row = table.NewRow ();
332 table.Rows.Add (row);
335 CurrencyManager cm = bc [data_source, "Table"] as CurrencyManager;
337 Assert.AreEqual (0, cm.Position, "NAVSETTOTABLE1");
338 Assert.AreEqual (10, cm.Count, "NAVSETTOTABLE2");
339 Assert.AreEqual (typeof (DataView), cm.List.GetType (), "NAVSETTOTABLE3");
341 for (int i = 0; i < 10; i++) {
342 DataRowView row = cm.Current as DataRowView;
343 Assert.IsFalse (row == null, "NAVSETTOTABLE-NULL-" + i);
344 Assert.AreEqual (i, row ["Column"], "NAVSETTOTABLE-" + i);
351 Assert.AreEqual (9, ((DataRowView) cm.Current) ["Column"], "NAVSETTOTABLE-END");
355 public void NavigateDataSetToColumn ()
357 DataSet data_source = new DataSet ("DataSet");
358 DataTable table = new DataTable ("Table");
359 DataColumn column = new DataColumn ("Column");
360 BindingContext bc = new BindingContext ();
362 data_source.Tables.Add (table);
364 column.DataType = typeof (int);
365 table.Columns.Add (column);
366 for (int i = 0; i < 10; i++) {
367 DataRow row = table.NewRow ();
369 table.Rows.Add (row);
372 CurrencyManager cm = bc [data_source, "Table.Column"] as CurrencyManager;
374 Assert.AreEqual (null, cm, "NAVSETTOCOLUMN1");
378 public void NavigateDataSetToParentRelation ()
380 DataSet data_source = CreateRelatedDataSet ();
381 BindingContext bc = new BindingContext ();
382 CurrencyManager cm = bc [data_source, "Table1.Relation"] as CurrencyManager;
384 Assert.AreEqual (0, cm.Position, "NAVSETTORELATION1");
385 Assert.AreEqual (1, cm.Count, "NAVSETTORELATION2");
386 Assert.IsTrue (cm.List is DataView, "NAVSETTORELATION3");
388 DataRowView row = cm.Current as DataRowView;
389 Assert.IsFalse (row == null, "NAVSETTORELATION-NULL-VALUE");
390 Assert.AreEqual (0, row ["Two"], "NAVSETTORELATION-VALUE");
395 Assert.AreEqual (0, ((DataRowView) cm.Current) ["Two"], "NAVSETTORELATION-END");
399 [ExpectedException (typeof (ArgumentException))]
400 public void DataSetToChildRelation ()
402 DataSet data_source = CreateRelatedDataSet ();
403 BindingContext bc = new BindingContext ();
405 // Can't create a list on a child relation
406 CurrencyManager cm = bc [data_source, "Table2.Relation"] as CurrencyManager;
410 public void DataSetToParentRelationField ()
412 DataSet data_source = CreateRelatedDataSet ();
413 BindingContext bc = new BindingContext ();
415 CurrencyManager cm = bc [data_source, "Table1.Relation.Two"] as CurrencyManager;
417 Assert.AreEqual (null, cm, "SETTOPARENTRELATIONFIELD");
421 public void MultiColumnedRelation ()
423 DataSet dataset = new DataSet ();
424 DataTable sports = new DataTable ("Sports");
425 DataTable athletes = new DataTable ("Athletes");
430 column = new DataColumn ();
431 column.DataType = typeof (int);
432 column.ColumnName = "SportID";
433 column.Unique = true;
434 sports.Columns.Add (column);
436 column = new DataColumn ();
437 column.DataType = typeof (string);
438 column.ColumnName = "SportName";
439 sports.Columns.Add (column);
442 string [] sports_names = new string [] { "Hockey", "Baseball", "Basketball", "Football", "Boxing", "Surfing" };
443 for (int i = 0; i < sports_names.Length; i++) {
444 row = sports.NewRow ();
446 row ["SportName"] = sports_names [i];
447 sports.Rows.Add (row);
452 column = new DataColumn ();
453 column.DataType = typeof (int);
454 column.ColumnName = "AthleteID";
455 column.Unique = true;
456 athletes.Columns.Add (column);
458 column = new DataColumn ();
459 column.DataType = typeof (int);
460 column.ColumnName = "Sport";
461 athletes.Columns.Add (column);
463 column = new DataColumn ();
464 column.DataType = typeof (string);
465 column.ColumnName = "AthleteName";
466 athletes.Columns.Add (column);
468 string [] athlete_names = new string [] { "@alp", "@lupus", "@tjfontaine", "duncan", "marv", "WindowsUninstall",
469 "@jackson", "@migHome", "_Synced[work]", "GodZhila", "Raboo",
470 "@jchambers", "@mkestner", "barbosa", "IzeBurn", "squinky86",
471 "@kangaroo", "@paco", "Demian", "logiclrd", "tenshiKur0" };
472 for (int i = 0; i < athlete_names.Length; i++) {
473 row = athletes.NewRow ();
474 row ["AthleteID"] = i;
475 row ["Sport"] = i % sports_names.Length;
476 row ["AthleteName"] = athlete_names [i];
477 athletes.Rows.Add (row);
480 dataset.Tables.Add (sports);
481 dataset.Tables.Add (athletes);
482 dataset.Relations.Add ("AthletesSports", sports.Columns ["SportID"], athletes.Columns ["Sport"]);
484 BindingContext bc = new BindingContext ();
485 CurrencyManager cm = bc [dataset, "Sports.AthletesSports"] as CurrencyManager;
487 Assert.AreEqual (0, cm.Position, "MC1");
488 Assert.AreEqual (4, cm.Count, "MC2");
490 DataRowView rowview = cm.Current as DataRowView;
491 Assert.IsFalse (rowview == null, "MC3");
492 Assert.AreEqual (0, rowview ["AthleteID"], "MC4");
493 Assert.AreEqual ("@alp", rowview ["AthleteName"], "MC5");
494 Assert.AreEqual (0, rowview ["Sport"], "MC6");
498 rowview = cm.Current as DataRowView;
499 Assert.IsFalse (rowview == null, "MC7");
500 Assert.AreEqual (6, rowview ["AthleteID"], "MC8");
501 Assert.AreEqual ("@jackson", rowview ["AthleteName"], "MC9");
502 Assert.AreEqual (0, rowview ["Sport"], "MC10");
506 rowview = cm.Current as DataRowView;
507 Assert.IsFalse (rowview == null, "MC11");
508 Assert.AreEqual (12, rowview ["AthleteID"], "MC12");
509 Assert.AreEqual ("@mkestner", rowview ["AthleteName"], "MC13");
510 Assert.AreEqual (0, rowview ["Sport"], "MC14");
514 rowview = cm.Current as DataRowView;
515 Assert.IsFalse (rowview == null, "MC15");
516 Assert.AreEqual (18, rowview ["AthleteID"], "MC16");
517 Assert.AreEqual ("Demian", rowview ["AthleteName"], "MC17");
518 Assert.AreEqual (0, rowview ["Sport"], "MC18");
522 rowview = cm.Current as DataRowView;
523 Assert.IsFalse (rowview == null, "MC19");
524 Assert.AreEqual (18, rowview ["AthleteID"], "MC20");
525 Assert.AreEqual ("Demian", rowview ["AthleteName"], "MC21");
526 Assert.AreEqual (0, rowview ["Sport"], "MC22");
529 private DataSet CreateRelatedDataSet ()
531 DataSet dataset = new DataSet ("DataSet");
532 DataTable dt1 = new DataTable ("Table1");
533 DataTable dt2 = new DataTable ("Table2");
536 column = new DataColumn ("One");
537 column.DataType = typeof (int);
538 column.Unique = true;
539 dt1.Columns.Add (column);
541 for (int i = 0; i < 10; i++) {
542 DataRow row = dt1.NewRow ();
547 column = new DataColumn ("Two");
548 column.DataType = typeof (int);
549 column.Unique = true;
550 dt2.Columns.Add (column);
552 for (int i = 0; i < 10; i++) {
553 DataRow row = dt2.NewRow ();
558 dataset.Tables.Add (dt1);
559 dataset.Tables.Add (dt2);
560 dataset.Relations.Add ("Relation", dt1.Columns ["One"], dt2.Columns ["Two"]);
565 public void EndUninitializedEdit ()
567 ArrayList list = new ArrayList ();
568 BindingContext bc = new BindingContext ();
569 CurrencyManager cm = bc [list] as CurrencyManager;
571 cm.EndCurrentEdit ();
575 public void CancelUninitializedEdit ()
577 ArrayList list = new ArrayList ();
578 BindingContext bc = new BindingContext ();
579 CurrencyManager cm = bc [list] as CurrencyManager;
581 cm.CancelCurrentEdit ();
585 public void CheckPositionOfRelatedSibling1 ()
587 DataSet data_source = CreateRelatedDataSet ();
588 BindingContext bc = new BindingContext ();
589 CurrencyManager cm = bc [data_source, "Table1.Relation"] as CurrencyManager;
590 CurrencyManager scm = bc [data_source, "Table1"] as CurrencyManager;
595 // position is not updated
596 Assert.AreEqual (0, scm.Position, "#8");
600 public void CheckPositionOfRelatedSibling2 ()
602 DataSet data_source = CreateRelatedDataSet ();
603 BindingContext bc = new BindingContext ();
604 CurrencyManager cm = bc [data_source, "Table1.Relation"] as CurrencyManager;
605 CurrencyManager scm = bc [data_source, "Table1"] as CurrencyManager;
607 Assert.AreEqual (0, cm.Position, "#1");
611 Assert.AreEqual (0, cm.Position, "#2");
616 int position_changed;
618 ItemChangedEventArgs item_changed_args;
620 void CurrentChanged (object sender, EventArgs args) { current_changed = ++event_num; /*Console.WriteLine ("current_changed = {0}", current_changed); Console.WriteLine (Environment.StackTrace);*/ }
621 void PositionChanged (object sender, EventArgs args) { position_changed = ++event_num; /*Console.WriteLine ("position_changed = {0}", position_changed); Console.WriteLine (Environment.StackTrace);*/}
622 void ItemChanged (object sender, ItemChangedEventArgs args) { item_changed = ++event_num; item_changed_args = args; /*Console.WriteLine ("item_changed = {0}, index = {1}", item_changed, args.Index); Console.WriteLine (Environment.StackTrace);*/ }
623 void ListChanged (object sender, ListChangedEventArgs args) { /*Console.WriteLine ("ListChanged ({0},{1},{2})", args.ListChangedType, args.OldIndex, args.NewIndex); Console.WriteLine (Environment.StackTrace);*/ }
626 public void AddNew ()
628 DataSet data_source = CreateRelatedDataSet ();
629 BindingContext bc = new BindingContext ();
630 CurrencyManager cm = bc [data_source, "Table1"] as CurrencyManager;
632 event_num = current_changed = position_changed = -1;
633 cm.CurrentChanged += new EventHandler (CurrentChanged);
634 cm.PositionChanged += new EventHandler (PositionChanged);
635 cm.ItemChanged += new ItemChangedEventHandler (ItemChanged);
637 Assert.AreEqual (0, cm.Position, "AddNew1");
638 Assert.AreEqual (10, cm.Count, "AddNew2");
639 Assert.AreEqual (cm.Count, cm.List.Count, "AddNew2.5");
643 Assert.AreEqual (10, cm.Position, "AddNew3");
644 Assert.AreEqual (11, cm.Count, "AddNew4");
645 Assert.AreEqual (cm.Count, cm.List.Count, "AddNew4.5");
647 Assert.AreEqual (0, item_changed, "AddNew5");
648 Assert.AreEqual (-1, item_changed_args.Index, "AddNew6");
649 Assert.AreEqual (1, current_changed, "AddNew7");
650 Assert.AreEqual (2, position_changed, "AddNew8");
652 cm.CurrentChanged -= new EventHandler (CurrentChanged);
653 cm.PositionChanged -= new EventHandler (PositionChanged);
657 public void CancelAddNew ()
659 DataSet data_source = CreateRelatedDataSet ();
660 BindingContext bc = new BindingContext ();
661 CurrencyManager cm = bc [data_source, "Table1"] as CurrencyManager;
663 DataView dv = cm.List as DataView;
665 event_num = current_changed = position_changed = -1;
666 cm.CurrentChanged += new EventHandler (CurrentChanged);
667 cm.PositionChanged += new EventHandler (PositionChanged);
668 cm.ItemChanged += new ItemChangedEventHandler (ItemChanged);
669 dv.ListChanged += new ListChangedEventHandler (ListChanged);
671 Assert.AreEqual (0, cm.Position, "CancelAddNew1");
672 Assert.AreEqual (10, cm.Count, "CancelAddNew2");
673 Assert.AreEqual (cm.Count, cm.List.Count, "AddNew2.5");
677 Assert.AreEqual (0, item_changed, "CancelAddNew3");
678 Assert.AreEqual (-1, item_changed_args.Index, "CancelAddNew4");
679 Assert.AreEqual (1, current_changed, "CancelAddNew5");
680 Assert.AreEqual (2, position_changed, "CancelAddNew6");
682 cm.CancelCurrentEdit ();
684 Assert.AreEqual (6, item_changed, "CancelAddNew7");
685 Assert.AreEqual (9, item_changed_args.Index, "CancelAddNew8");
686 Assert.AreEqual (3, current_changed, "CancelAddNew9");
687 Assert.AreEqual (4, position_changed, "CancelAddNew10");
689 Assert.AreEqual (9, cm.Position, "CancelAddNew11");
690 Assert.AreEqual (10, cm.Count, "CancelAddNew12");
691 Assert.AreEqual (cm.Count, cm.List.Count, "AddNew12.5");
693 cm.CurrentChanged -= new EventHandler (CurrentChanged);
694 cm.PositionChanged -= new EventHandler (PositionChanged);
698 public void EndAddNew ()
700 DataSet data_source = CreateRelatedDataSet ();
701 BindingContext bc = new BindingContext ();
702 CurrencyManager cm = bc [data_source, "Table1"] as CurrencyManager;
704 event_num = current_changed = position_changed = -1;
705 cm.CurrentChanged += new EventHandler (CurrentChanged);
706 cm.PositionChanged += new EventHandler (PositionChanged);
707 cm.ItemChanged += new ItemChangedEventHandler (ItemChanged);
709 Assert.AreEqual (0, cm.Position, "EndAddNew1");
710 Assert.AreEqual (10, cm.Count, "EndAddNew2");
714 Assert.AreEqual (0, item_changed, "EndAddNew3");
715 Assert.AreEqual (-1, item_changed_args.Index, "EndAddNew4");
716 Assert.AreEqual (1, current_changed, "EndAddNew5");
717 Assert.AreEqual (2, position_changed, "EndAddNew6");
719 cm.EndCurrentEdit ();
721 // Assert.AreEqual (3, item_changed, "EndAddNew7");
722 // Assert.AreEqual (-1, item_changed_args.Index, "EndAddNew8");
723 Assert.AreEqual (1, current_changed, "EndAddNew9");
724 Assert.AreEqual (2, position_changed, "EndAddNew10");
726 Assert.AreEqual (10, cm.Position, "EndAddNew11");
727 Assert.AreEqual (11, cm.Count, "EndAddNew12");
729 cm.CurrentChanged -= new EventHandler (CurrentChanged);
730 cm.PositionChanged -= new EventHandler (PositionChanged);
734 public void AddNew2 ()
736 DataSet data_source = CreateRelatedDataSet ();
737 BindingContext bc = new BindingContext ();
738 CurrencyManager cm = bc [data_source, "Table1"] as CurrencyManager;
740 DataView dv = cm.List as DataView;
742 event_num = current_changed = position_changed = -1;
743 cm.CurrentChanged += new EventHandler (CurrentChanged);
744 cm.PositionChanged += new EventHandler (PositionChanged);
745 cm.ItemChanged += new ItemChangedEventHandler (ItemChanged);
746 dv.ListChanged += new ListChangedEventHandler (ListChanged);
748 Assert.AreEqual (0, cm.Position, "AddNew1");
749 Assert.AreEqual (10, cm.Count, "AddNew2");
753 Assert.AreEqual (10, cm.Position, "AddNew3");
754 Assert.AreEqual (11, cm.Count, "AddNew4");
756 // this does an implicit EndCurrentEdit
759 Assert.AreEqual (11, cm.Position, "AddNew5");
760 Assert.AreEqual (12, cm.Count, "AddNew6");
764 DataSet CreateRelatedDataSetLarge ()
766 DataSet dataset = new DataSet ("CustomerSet");
767 DataTable dt1 = new DataTable ("Customers");
768 DataTable dt2 = new DataTable ("Orders");
769 DataTable dt3 = new DataTable ("Addresses");
773 column = new DataColumn ("CustomerID");
774 column.DataType = typeof (int);
775 column.Unique = true;
776 dt1.Columns.Add (column);
778 column = new DataColumn ("CustomerName");
779 column.DataType = typeof (string);
780 column.Unique = false;
781 dt1.Columns.Add (column);
784 column = new DataColumn ("OrderID");
785 column.DataType = typeof (int);
786 column.Unique = true;
787 dt2.Columns.Add (column);
789 column = new DataColumn ("ItemName");
790 column.DataType = typeof (string);
791 column.Unique = false;
792 dt2.Columns.Add (column);
794 column = new DataColumn ("CustomerID");
795 column.DataType = typeof (int);
796 column.Unique = false;
797 dt2.Columns.Add (column);
799 column = new DataColumn ("AddressID");
800 column.DataType = typeof (int);
801 column.Unique = false;
802 dt2.Columns.Add (column);
805 column = new DataColumn ("AddressID");
806 column.DataType = typeof (int);
807 column.Unique = true;
808 dt3.Columns.Add (column);
810 column = new DataColumn ("AddressString");
811 column.DataType = typeof (string);
812 column.Unique = false;
813 dt3.Columns.Add (column);
815 column = new DataColumn ("CustomerID");
816 column.DataType = typeof (int);
817 column.Unique = false;
818 dt3.Columns.Add (column);
820 for (int i = 0; i < 10; i ++) {
821 DataRow row = dt1.NewRow ();
822 row["CustomerID"] = i;
823 row["CustomerName"] = String.Format ("Customer Name #{0}", i);
828 for (int i = 0; i < 10; i ++) {
829 for (int j = 0; j < (i < 5 ? 3 : 5); j ++) {
830 DataRow row = dt2.NewRow ();
831 row["OrderID"] = ordernum++;
832 row["ItemName"] = String.Format ("Item order #{0}", j);
833 row["CustomerID"] = i;
834 row["AddressID"] = j;
840 for (int i = 0; i < 4; i ++) {
841 for (int j = 0; j < 4; j ++) {
842 DataRow row = dt3.NewRow ();
843 row["AddressID"] = addressid++;
844 row["AddressString"] = String.Format ("Customer Address {0}", j);
845 row["CustomerID"] = i;
850 dataset.Tables.Add (dt1);
851 dataset.Tables.Add (dt2);
852 dataset.Tables.Add (dt3);
853 dataset.Relations.Add ("Customer_Orders", dt1.Columns["CustomerID"], dt2.Columns["CustomerID"]);
854 dataset.Relations.Add ("Customer_Addresses", dt1.Columns["CustomerID"], dt3.Columns["CustomerID"]);
855 dataset.Relations.Add ("Address_Orders", dt3.Columns["AddressID"], dt2.Columns["AddressID"]);
861 public void RelatedCurrencyManagerTest ()
863 DataSet data_source = CreateRelatedDataSetLarge ();
864 BindingContext bc = new BindingContext ();
865 CurrencyManager cm = bc [data_source, "Customers"] as CurrencyManager;
866 CurrencyManager rcm = bc [data_source, "Customers.Customer_Orders"] as CurrencyManager;
868 IList list = rcm.List;
869 Assert.AreEqual (3, rcm.Count, "count1");
870 Assert.AreEqual (3, list.Count, "listcount1");
873 Assert.AreEqual (3, rcm.Count, "count2");
874 Assert.AreEqual (3, list.Count, "listcount2");
877 Assert.AreEqual (5, rcm.Count, "count3");
878 Assert.AreEqual (3, list.Count, "listcount3");
882 public void TestCurrencyManagerBindings ()
884 DataSet data_source = CreateRelatedDataSetLarge ();
885 BindingContext bc = new BindingContext ();
887 CurrencyManager cm = bc [data_source] as CurrencyManager;
889 Console.WriteLine ("cm properties:");
890 foreach (PropertyDescriptor pd in cm.GetItemProperties ())
891 Console.WriteLine (" + {0}", pd.Name);
892 Console.WriteLine ();
894 Console.WriteLine ("dataset:");
895 Console.WriteLine ("cm = {0}", cm.GetType());
896 Console.WriteLine ("cm.Count = {0}", cm.Count);
898 Console.WriteLine ("cm.Current = {0}", cm.Current);
899 Console.WriteLine ("cm.Current properties");
900 foreach (PropertyDescriptor pd in ((ICustomTypeDescriptor)cm.Current).GetProperties ())
901 Console.WriteLine (" + {0}", pd.Name);
902 Console.WriteLine ();
904 cm = bc [data_source.Tables["Customers"]] as CurrencyManager;
905 Console.WriteLine ("datatable:");
906 Console.WriteLine ("cm = {0}", cm.GetType());
907 Console.WriteLine ("cm.Count = {0}", cm.Count);
909 Console.WriteLine ("cm.Current = {0}", cm.Current);
910 Console.WriteLine ("cm.Current properties");
911 foreach (PropertyDescriptor pd in ((ICustomTypeDescriptor)cm.Current).GetProperties ())
912 Console.WriteLine (" + {0}", pd.Name);
914 Console.WriteLine ();
916 DataViewManager vm = new DataViewManager (data_source);
917 Console.WriteLine ("vm properties:");
918 foreach (PropertyDescriptor pd in ((ITypedList)vm).GetItemProperties (null))
919 Console.WriteLine (" + {0}", pd.Name);
920 Console.WriteLine ();
924 Type GetFinalType (CurrencyManager cm)
926 FieldInfo fi = cm.GetType().GetField ("finalType", BindingFlags.NonPublic | BindingFlags.Instance);
928 return (Type)fi.GetValue (cm);
932 public void FinalTypeTest ()
934 BindingContext bc = new BindingContext ();
937 DataSet data_source = CreateRelatedDataSetLarge ();
939 /* empty arraylist */
940 al = new ArrayList ();
941 cm = bc[al] as CurrencyManager;
942 Assert.AreEqual (typeof (ArrayList), GetFinalType (cm), "A1");
944 /* arraylist with a string element*/
945 al = new ArrayList ();
947 cm = bc[al] as CurrencyManager;
948 Assert.AreEqual (typeof (ArrayList), GetFinalType (cm), "A2");
951 string[] s = new string[1];
953 cm = bc[s] as CurrencyManager;
954 Assert.AreEqual (typeof (string[]), GetFinalType (cm), "A3");
957 cm = bc [data_source, "Customers"] as CurrencyManager;
958 Assert.AreEqual (typeof (DataView), GetFinalType (cm), "A4");
961 cm = bc [data_source, "Customers.Customer_Orders"] as CurrencyManager;
962 /* on MS this is a RelatedView, on Mono a RelatedDataView. both subclass from DataView, so let's check that. */
963 Assert.IsFalse (typeof (DataView) == GetFinalType (cm), "A5");
964 Assert.IsTrue (typeof (DataView).IsAssignableFrom (GetFinalType (cm)), "A6");