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
26 using System
.Collections
;
27 using System
.ComponentModel
;
29 using System
.Reflection
;
30 using System
.Windows
.Forms
;
32 using NUnit
.Framework
;
34 namespace MonoTests
.System
.Windows
.Forms
.DataBinding
37 public class CurrencyManagerTest
40 public void Defaults ()
42 BindingContext bc
= new BindingContext ();
43 ArrayList data_source
= new ArrayList ();
44 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
46 Assert
.AreSame (data_source
, cm
.List
, "DEFAULTS1");
47 Assert
.AreEqual (0, cm
.Count
, "DEFAULTS2");
48 Assert
.AreEqual (-1, cm
.Position
, "DEFAULTS3");
52 [ExpectedException (typeof (IndexOutOfRangeException
))]
53 public void UninitializedCurrent ()
55 BindingContext bc
= new BindingContext ();
56 ArrayList data_source
= new ArrayList ();
57 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
59 // This line should throw
60 Assert
.AreSame (null, cm
.Current
, "CurrentOfEmpty");
64 public void DataSetList ()
66 DataSet dataset
= new DataSet ("DataSet");
67 DataTable table
= new DataTable ("Table");
68 BindingContext bc
= new BindingContext ();
71 dataset
.Tables
.Add (table
);
72 cm
= bc
[dataset
] as CurrencyManager
;
74 Assert
.AreEqual (typeof (DataViewManager
), cm
.List
.GetType (), "DATASETLIST1");
75 Assert
.AreEqual (1, cm
.Count
, "DATASETLIST2");
76 Assert
.AreEqual (0, cm
.Position
, "DATASETLIST3");
77 // Assert.AreEqual (typeof (DataViewManagerListItemTypeDescriptor), cm.Current.GetType (),
82 public void DataSetListTable ()
84 DataSet dataset
= new DataSet ("DataSet");
85 DataTable table
= new DataTable ("Table");
86 BindingContext bc
= new BindingContext ();
89 dataset
.Tables
.Add (table
);
90 cm
= bc
[dataset
, "Table"] as CurrencyManager
;
92 Assert
.AreEqual (typeof (DataView
), cm
.List
.GetType (), "DATASETLIST1");
93 Assert
.AreEqual (0, cm
.Count
, "DATASETLIST2");
94 Assert
.AreEqual (-1, cm
.Position
, "DATASETLIST3");
98 [ExpectedException (typeof (ArgumentException
))]
99 public void DataSetListTableBogusField ()
101 DataSet dataset
= new DataSet ("DataSet");
102 DataTable table
= new DataTable ("Table.Column");
103 BindingContext bc
= new BindingContext ();
106 dataset
.Tables
.Add (table
);
108 // child list can't be created
109 cm
= bc
[dataset
, "Table"] as CurrencyManager
;
111 TestHelper
.RemoveWarning (cm
);
115 public void DataView ()
117 DataView dv
= new DataView ();
119 BindingContext bc
= new BindingContext ();
120 CurrencyManager cm
= bc
[dv
, string.Empty
] as CurrencyManager
;
121 Assert
.IsNotNull (cm
, "#A1");
122 Assert
.AreEqual (0, cm
.Count
, "#A2");
123 Assert
.AreEqual (-1, cm
.Position
, "#A3");
125 DataTable dt
= new DataTable ("Testdata");
126 dt
.Columns
.Add ("A");
127 dt
.Columns
.Add ("B");
128 dt
.Rows
.Add (new object [] { "A1", "B1" }
);
129 dt
.Rows
.Add (new object [] { "A2", "B2" }
);
132 Assert
.AreEqual (2, cm
.Count
, "#B1");
133 Assert
.AreEqual (0, cm
.Position
, "#B2");
137 public void MoveArrayListForward ()
139 ArrayList data_source
= new ArrayList ();
140 BindingContext bc
= new BindingContext ();
142 for (int i
= 0; i
< 10; i
++)
143 data_source
.Add (new object ());
145 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
146 for (int i
= 0; i
< 10; i
++) {
147 Assert
.AreSame (data_source
[i
], cm
.Current
, "MOVEALF" + i
);
154 Assert
.AreSame (data_source
[9], cm
.Current
, "MOVEALFEND");
158 public void MoveArrayListBackward ()
160 ArrayList data_source
= new ArrayList ();
161 BindingContext bc
= new BindingContext ();
163 for (int i
= 0; i
< 10; i
++)
164 data_source
.Add (new object ());
166 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
168 for (int i
= 9; i
>= 0; i
--) {
169 Assert
.AreSame (data_source
[i
], cm
.Current
, "MOVEALB" + i
);
176 Assert
.AreSame (data_source
[0], cm
.Current
, "MOVEALBSTART");
180 public void SetPositionArrayList ()
182 ArrayList data_source
= new ArrayList ();
183 BindingContext bc
= new BindingContext ();
185 for (int i
= 0; i
< 10; i
++)
186 data_source
.Add (new object ());
188 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
189 for (int i
= 3; i
>= 0; i
--) {
191 Assert
.AreSame (data_source
[i
], cm
.Current
, "MOVEAL1-" + i
);
196 for (int i
= 0; i
< 10; i
++) {
198 Assert
.AreSame (data_source
[i
], cm
.Current
, "MOVEAL2-" + i
);
201 for (int i
= 5; i
< 10; i
++) {
203 Assert
.AreSame (data_source
[i
], cm
.Current
, "MOVEAL3-" + i
);
208 public void LateBuildDataTable ()
210 DataTable data_source
= new DataTable ("Table");
211 BindingContext bc
= new BindingContext ();
212 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
214 Assert
.AreEqual (-1, cm
.Position
, "LATEBUILDTABLE1");
215 Assert
.AreEqual (0, cm
.Count
, "LATEBUILDTABLE2");
217 DataColumn column
= new DataColumn ("Column");
218 column
.DataType
= typeof (int);
219 data_source
.Columns
.Add (column
);
221 for (int i
= 0; i
< 10; i
++) {
222 DataRow row
= data_source
.NewRow ();
224 data_source
.Rows
.Add (row
);
227 Assert
.AreEqual (0, cm
.Position
, "LATEBUILDTABLE3");
228 Assert
.AreEqual (10, cm
.Count
, "LATEBUILDTABLE4");
232 public void LateBuildArrayList ()
234 ArrayList data_source
= new ArrayList ();
235 BindingContext bc
= new BindingContext ();
236 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
238 Assert
.AreEqual (-1, cm
.Position
, "LATEBUILDLIST1");
239 Assert
.AreEqual (0, cm
.Count
, "LATEBUILDLIST2");
241 data_source
.AddRange (new object [] { 1, 2, 3, 4, 5, 6, 7 }
);
243 Assert
.AreEqual (-1, cm
.Position
, "LATEBUILDLIST3");
244 Assert
.AreEqual (7, cm
.Count
, "LATEBUILDLIST4");
248 public void MoveDataTableForward ()
250 DataTable data_source
= new DataTable ("Table");
251 BindingContext bc
= new BindingContext ();
252 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
253 DataColumn column
= new DataColumn ("Column");
255 column
.DataType
= typeof (int);
256 data_source
.Columns
.Add (column
);
257 for (int i
= 0; i
< 10; i
++) {
258 DataRow row
= data_source
.NewRow ();
260 data_source
.Rows
.Add (row
);
264 for (int i
= 0; i
< 10; i
++) {
265 DataRowView row
= cm
.Current
as DataRowView
;
266 Assert
.IsFalse (row
== null, "MOVETABLEF-NULL-" + i
);
267 Assert
.AreEqual (row
["Column"], i
, "MOVETABLEF-" + i
);
274 Assert
.AreEqual (9, ((DataRowView
) cm
.Current
) ["Column"], "MOVETABLEF-END");
278 public void MoveDataTableBackward ()
280 DataTable data_source
= new DataTable ("Table");
281 BindingContext bc
= new BindingContext ();
282 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
283 DataColumn column
= new DataColumn ("Column");
285 column
.DataType
= typeof (int);
286 data_source
.Columns
.Add (column
);
287 for (int i
= 0; i
< 10; i
++) {
288 DataRow row
= data_source
.NewRow ();
290 data_source
.Rows
.Add (row
);
295 for (int i
= 9; i
>= 0; i
--) {
296 DataRowView row
= cm
.Current
as DataRowView
;
297 Assert
.IsFalse (row
== null, "MOVETABLEB-NULL-" + i
);
298 Assert
.AreEqual (row
["Column"], i
, "MOVETABLEB-" + i
);
305 Assert
.AreEqual (0, ((DataRowView
) cm
.Current
) ["Column"], "MOVETABLEB-START");
309 public void SetPositionDataTable ()
311 DataTable data_source
= new DataTable ("Table");
312 BindingContext bc
= new BindingContext ();
313 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
314 DataColumn column
= new DataColumn ("Column");
316 column
.DataType
= typeof (int);
317 data_source
.Columns
.Add (column
);
318 for (int i
= 0; i
< 10; i
++) {
319 DataRow row
= data_source
.NewRow ();
321 data_source
.Rows
.Add (row
);
325 for (int i
= 5; i
< 10; i
++) {
327 DataRowView row
= cm
.Current
as DataRowView
;
328 Assert
.IsFalse (row
== null, "SETTABLE1-NULL-" + i
);
329 Assert
.AreEqual (row
["Column"], i
, "SETTABLE1-" + i
);
332 for (int i
= 5; i
>= 0; i
--) {
334 DataRowView row
= cm
.Current
as DataRowView
;
335 Assert
.IsFalse (row
== null, "SETTABLE2-NULL-" + i
);
336 Assert
.AreEqual (row
["Column"], i
, "SETTABLE2-" + i
);
341 public void NavigateDataSetToTable ()
343 DataSet data_source
= new DataSet ("DataSet");
344 DataTable table
= new DataTable ("Table");
345 DataColumn column
= new DataColumn ("Column");
346 BindingContext bc
= new BindingContext ();
348 data_source
.Tables
.Add (table
);
350 column
.DataType
= typeof (int);
351 table
.Columns
.Add (column
);
352 for (int i
= 0; i
< 10; i
++) {
353 DataRow row
= table
.NewRow ();
355 table
.Rows
.Add (row
);
358 CurrencyManager cm
= bc
[data_source
, "Table"] as CurrencyManager
;
360 Assert
.AreEqual (0, cm
.Position
, "NAVSETTOTABLE1");
361 Assert
.AreEqual (10, cm
.Count
, "NAVSETTOTABLE2");
362 Assert
.AreEqual (typeof (DataView
), cm
.List
.GetType (), "NAVSETTOTABLE3");
364 for (int i
= 0; i
< 10; i
++) {
365 DataRowView row
= cm
.Current
as DataRowView
;
366 Assert
.IsFalse (row
== null, "NAVSETTOTABLE-NULL-" + i
);
367 Assert
.AreEqual (i
, row
["Column"], "NAVSETTOTABLE-" + i
);
374 Assert
.AreEqual (9, ((DataRowView
) cm
.Current
) ["Column"], "NAVSETTOTABLE-END");
378 public void NavigateDataSetToColumn ()
380 DataSet data_source
= new DataSet ("DataSet");
381 DataTable table
= new DataTable ("Table");
382 DataColumn column
= new DataColumn ("Column");
383 BindingContext bc
= new BindingContext ();
385 data_source
.Tables
.Add (table
);
387 column
.DataType
= typeof (int);
388 table
.Columns
.Add (column
);
389 for (int i
= 0; i
< 10; i
++) {
390 DataRow row
= table
.NewRow ();
392 table
.Rows
.Add (row
);
395 CurrencyManager cm
= bc
[data_source
, "Table.Column"] as CurrencyManager
;
397 Assert
.AreEqual (null, cm
, "NAVSETTOCOLUMN1");
401 public void NavigateDataSetToParentRelation ()
403 DataSet data_source
= CreateRelatedDataSet ();
404 BindingContext bc
= new BindingContext ();
405 CurrencyManager cm
= bc
[data_source
, "Table1.Relation"] as CurrencyManager
;
407 Assert
.AreEqual (0, cm
.Position
, "NAVSETTORELATION1");
408 Assert
.AreEqual (1, cm
.Count
, "NAVSETTORELATION2");
409 Assert
.IsTrue (cm
.List
is DataView
, "NAVSETTORELATION3");
411 DataRowView row
= cm
.Current
as DataRowView
;
412 Assert
.IsFalse (row
== null, "NAVSETTORELATION-NULL-VALUE");
413 Assert
.AreEqual (0, row
["Two"], "NAVSETTORELATION-VALUE");
418 Assert
.AreEqual (0, ((DataRowView
) cm
.Current
) ["Two"], "NAVSETTORELATION-END");
422 [ExpectedException (typeof (ArgumentException
))]
423 public void DataSetToChildRelation ()
425 DataSet data_source
= CreateRelatedDataSet ();
426 BindingContext bc
= new BindingContext ();
428 // Can't create a list on a child relation
429 CurrencyManager cm
= bc
[data_source
, "Table2.Relation"] as CurrencyManager
;
431 TestHelper
.RemoveWarning (cm
);
435 public void DataSetToParentRelationField ()
437 DataSet data_source
= CreateRelatedDataSet ();
438 BindingContext bc
= new BindingContext ();
440 CurrencyManager cm
= bc
[data_source
, "Table1.Relation.Two"] as CurrencyManager
;
442 Assert
.AreEqual (null, cm
, "SETTOPARENTRELATIONFIELD");
446 public void MultiColumnedRelation ()
448 DataSet dataset
= new DataSet ();
449 DataTable sports
= new DataTable ("Sports");
450 DataTable athletes
= new DataTable ("Athletes");
455 column
= new DataColumn ();
456 column
.DataType
= typeof (int);
457 column
.ColumnName
= "SportID";
458 column
.Unique
= true;
459 sports
.Columns
.Add (column
);
461 column
= new DataColumn ();
462 column
.DataType
= typeof (string);
463 column
.ColumnName
= "SportName";
464 sports
.Columns
.Add (column
);
467 string [] sports_names
= new string [] { "Hockey", "Baseball", "Basketball", "Football", "Boxing", "Surfing" }
;
468 for (int i
= 0; i
< sports_names
.Length
; i
++) {
469 row
= sports
.NewRow ();
471 row
["SportName"] = sports_names
[i
];
472 sports
.Rows
.Add (row
);
477 column
= new DataColumn ();
478 column
.DataType
= typeof (int);
479 column
.ColumnName
= "AthleteID";
480 column
.Unique
= true;
481 athletes
.Columns
.Add (column
);
483 column
= new DataColumn ();
484 column
.DataType
= typeof (int);
485 column
.ColumnName
= "Sport";
486 athletes
.Columns
.Add (column
);
488 column
= new DataColumn ();
489 column
.DataType
= typeof (string);
490 column
.ColumnName
= "AthleteName";
491 athletes
.Columns
.Add (column
);
493 string [] athlete_names
= new string [] { "@alp", "@lupus", "@tjfontaine", "duncan", "marv", "WindowsUninstall",
494 "@jackson", "@migHome", "_Synced[work]", "GodZhila", "Raboo",
495 "@jchambers", "@mkestner", "barbosa", "IzeBurn", "squinky86",
496 "@kangaroo", "@paco", "Demian", "logiclrd", "tenshiKur0" };
497 for (int i
= 0; i
< athlete_names
.Length
; i
++) {
498 row
= athletes
.NewRow ();
499 row
["AthleteID"] = i
;
500 row
["Sport"] = i
% sports_names
.Length
;
501 row
["AthleteName"] = athlete_names
[i
];
502 athletes
.Rows
.Add (row
);
505 dataset
.Tables
.Add (sports
);
506 dataset
.Tables
.Add (athletes
);
507 dataset
.Relations
.Add ("AthletesSports", sports
.Columns
["SportID"], athletes
.Columns
["Sport"]);
509 BindingContext bc
= new BindingContext ();
510 CurrencyManager cm
= bc
[dataset
, "Sports.AthletesSports"] as CurrencyManager
;
512 Assert
.AreEqual (0, cm
.Position
, "MC1");
513 Assert
.AreEqual (4, cm
.Count
, "MC2");
515 DataRowView rowview
= cm
.Current
as DataRowView
;
516 Assert
.IsFalse (rowview
== null, "MC3");
517 Assert
.AreEqual (0, rowview
["AthleteID"], "MC4");
518 Assert
.AreEqual ("@alp", rowview
["AthleteName"], "MC5");
519 Assert
.AreEqual (0, rowview
["Sport"], "MC6");
523 rowview
= cm
.Current
as DataRowView
;
524 Assert
.IsFalse (rowview
== null, "MC7");
525 Assert
.AreEqual (6, rowview
["AthleteID"], "MC8");
526 Assert
.AreEqual ("@jackson", rowview
["AthleteName"], "MC9");
527 Assert
.AreEqual (0, rowview
["Sport"], "MC10");
531 rowview
= cm
.Current
as DataRowView
;
532 Assert
.IsFalse (rowview
== null, "MC11");
533 Assert
.AreEqual (12, rowview
["AthleteID"], "MC12");
534 Assert
.AreEqual ("@mkestner", rowview
["AthleteName"], "MC13");
535 Assert
.AreEqual (0, rowview
["Sport"], "MC14");
539 rowview
= cm
.Current
as DataRowView
;
540 Assert
.IsFalse (rowview
== null, "MC15");
541 Assert
.AreEqual (18, rowview
["AthleteID"], "MC16");
542 Assert
.AreEqual ("Demian", rowview
["AthleteName"], "MC17");
543 Assert
.AreEqual (0, rowview
["Sport"], "MC18");
547 rowview
= cm
.Current
as DataRowView
;
548 Assert
.IsFalse (rowview
== null, "MC19");
549 Assert
.AreEqual (18, rowview
["AthleteID"], "MC20");
550 Assert
.AreEqual ("Demian", rowview
["AthleteName"], "MC21");
551 Assert
.AreEqual (0, rowview
["Sport"], "MC22");
554 private DataSet
CreateRelatedDataSet ()
556 DataSet dataset
= new DataSet ("DataSet");
557 DataTable dt1
= new DataTable ("Table1");
558 DataTable dt2
= new DataTable ("Table2");
561 column
= new DataColumn ("One");
562 column
.DataType
= typeof (int);
563 column
.Unique
= true;
564 dt1
.Columns
.Add (column
);
566 for (int i
= 0; i
< 10; i
++) {
567 DataRow row
= dt1
.NewRow ();
572 column
= new DataColumn ("Two");
573 column
.DataType
= typeof (int);
574 column
.Unique
= true;
575 dt2
.Columns
.Add (column
);
577 for (int i
= 0; i
< 10; i
++) {
578 DataRow row
= dt2
.NewRow ();
583 dataset
.Tables
.Add (dt1
);
584 dataset
.Tables
.Add (dt2
);
585 dataset
.Relations
.Add ("Relation", dt1
.Columns
["One"], dt2
.Columns
["Two"]);
590 public void EndUninitializedEdit ()
592 ArrayList list
= new ArrayList ();
593 BindingContext bc
= new BindingContext ();
594 CurrencyManager cm
= bc
[list
] as CurrencyManager
;
596 cm
.EndCurrentEdit ();
600 public void CancelUninitializedEdit ()
602 ArrayList list
= new ArrayList ();
603 BindingContext bc
= new BindingContext ();
604 CurrencyManager cm
= bc
[list
] as CurrencyManager
;
606 cm
.CancelCurrentEdit ();
610 public void CheckPositionOfRelatedSibling1 ()
612 DataSet data_source
= CreateRelatedDataSet ();
613 BindingContext bc
= new BindingContext ();
614 CurrencyManager cm
= bc
[data_source
, "Table1.Relation"] as CurrencyManager
;
615 CurrencyManager scm
= bc
[data_source
, "Table1"] as CurrencyManager
;
620 // position is not updated
621 Assert
.AreEqual (0, scm
.Position
, "#8");
625 public void CheckPositionOfRelatedSibling2 ()
627 DataSet data_source
= CreateRelatedDataSet ();
628 BindingContext bc
= new BindingContext ();
629 CurrencyManager cm
= bc
[data_source
, "Table1.Relation"] as CurrencyManager
;
630 CurrencyManager scm
= bc
[data_source
, "Table1"] as CurrencyManager
;
632 Assert
.AreEqual (0, cm
.Position
, "#1");
636 Assert
.AreEqual (0, cm
.Position
, "#2");
641 int position_changed
;
643 int metadata_changed
;
644 string event_log
= "";
645 ItemChangedEventArgs item_changed_args
;
647 void CurrentChanged (object sender
, EventArgs args
)
649 current_changed
= ++event_num
;
650 Console
.WriteLine ("current_changed = {0}", current_changed
);
651 Console
.WriteLine (Environment
.StackTrace
);
652 event_log
+= String
.Format ("{0}: CurrentChanged\n", current_changed
);
654 void PositionChanged (object sender
, EventArgs args
)
656 position_changed
= ++event_num
;
657 Console
.WriteLine ("position_changed = {0}", position_changed
);
658 Console
.WriteLine (Environment
.StackTrace
);
659 event_log
+= String
.Format ("{0}: PositionChanged (to {1})\n", position_changed
, ((CurrencyManager
)sender
).Position
);
661 void ItemChanged (object sender
, ItemChangedEventArgs args
)
663 item_changed
= ++event_num
;
664 item_changed_args
= args
;
665 Console
.WriteLine ("item_changed = {0}, index = {1}", item_changed
, args
.Index
);
666 Console
.WriteLine (Environment
.StackTrace
);
667 event_log
+= String
.Format ("{0}: ItemChanged (index = {1})\n", item_changed
, args
.Index
);
669 void ListChanged (object sender
, ListChangedEventArgs args
)
671 Console
.WriteLine ("ListChanged ({0},{1},{2})", args
.ListChangedType
, args
.OldIndex
, args
.NewIndex
);
672 Console
.WriteLine (Environment
.StackTrace
);
673 event_log
+= String
.Format (" : ListChanged ({0}, {1}, {2})\n", args
.ListChangedType
, args
.OldIndex
, args
.NewIndex
);
675 void MetaDataChanged (object sender
, EventArgs args
)
677 metadata_changed
= ++event_num
;
678 Console
.WriteLine ("metadata_changed = {0}", metadata_changed
);
679 Console
.WriteLine (Environment
.StackTrace
);
680 event_log
+= String
.Format ("{0}: MetaDataChanged\n", metadata_changed
);
684 public void AddNew ()
686 DataSet data_source
= CreateRelatedDataSet ();
687 BindingContext bc
= new BindingContext ();
688 CurrencyManager cm
= bc
[data_source
, "Table1"] as CurrencyManager
;
690 event_num
= current_changed
= position_changed
= -1;
691 cm
.CurrentChanged
+= new EventHandler (CurrentChanged
);
692 cm
.PositionChanged
+= new EventHandler (PositionChanged
);
693 cm
.ItemChanged
+= new ItemChangedEventHandler (ItemChanged
);
695 Assert
.AreEqual (0, cm
.Position
, "AddNew1");
696 Assert
.AreEqual (10, cm
.Count
, "AddNew2");
697 Assert
.AreEqual (cm
.Count
, cm
.List
.Count
, "AddNew2.5");
701 Assert
.AreEqual (10, cm
.Position
, "AddNew3");
702 Assert
.AreEqual (11, cm
.Count
, "AddNew4");
703 Assert
.AreEqual (cm
.Count
, cm
.List
.Count
, "AddNew4.5");
705 Assert
.AreEqual (0, item_changed
, "AddNew5");
706 Assert
.AreEqual (-1, item_changed_args
.Index
, "AddNew6");
707 Assert
.AreEqual (1, current_changed
, "AddNew7");
708 Assert
.AreEqual (2, position_changed
, "AddNew8");
710 cm
.CurrentChanged
-= new EventHandler (CurrentChanged
);
711 cm
.PositionChanged
-= new EventHandler (PositionChanged
);
715 public void CancelAddNew ()
717 if (TestHelper
.RunningOnUnix
) {
718 Assert
.Ignore ("Fails at the moment");
721 DataSet data_source
= CreateRelatedDataSet ();
722 BindingContext bc
= new BindingContext ();
723 CurrencyManager cm
= bc
[data_source
, "Table1"] as CurrencyManager
;
725 DataView dv
= cm
.List
as DataView
;
727 event_num
= current_changed
= position_changed
= -1;
728 cm
.CurrentChanged
+= new EventHandler (CurrentChanged
);
729 cm
.PositionChanged
+= new EventHandler (PositionChanged
);
730 cm
.ItemChanged
+= new ItemChangedEventHandler (ItemChanged
);
731 dv
.ListChanged
+= new ListChangedEventHandler (ListChanged
);
733 Assert
.AreEqual (0, cm
.Position
, "CancelAddNew1");
734 Assert
.AreEqual (10, cm
.Count
, "CancelAddNew2");
735 Assert
.AreEqual (cm
.Count
, cm
.List
.Count
, "AddNew2.5");
739 Assert
.AreEqual (0, item_changed
, "CancelAddNew3");
740 Assert
.AreEqual (-1, item_changed_args
.Index
, "CancelAddNew4");
741 Assert
.AreEqual (1, current_changed
, "CancelAddNew5");
742 Assert
.AreEqual (2, position_changed
, "CancelAddNew6");
744 cm
.CancelCurrentEdit ();
746 Assert
.AreEqual (6, item_changed
, "CancelAddNew7");
747 Assert
.AreEqual (9, item_changed_args
.Index
, "CancelAddNew8");
748 Assert
.AreEqual (3, current_changed
, "CancelAddNew9");
749 Assert
.AreEqual (4, position_changed
, "CancelAddNew10");
751 Assert
.AreEqual (9, cm
.Position
, "CancelAddNew11");
752 Assert
.AreEqual (10, cm
.Count
, "CancelAddNew12");
753 Assert
.AreEqual (cm
.Count
, cm
.List
.Count
, "AddNew12.5");
755 cm
.CurrentChanged
-= new EventHandler (CurrentChanged
);
756 cm
.PositionChanged
-= new EventHandler (PositionChanged
);
760 public void EndAddNew ()
763 if (TestHelper
.RunningOnUnix
) {
764 Assert
.Ignore ("Fails with 2.0 profile");
767 DataSet data_source
= CreateRelatedDataSet ();
768 BindingContext bc
= new BindingContext ();
769 CurrencyManager cm
= bc
[data_source
.Tables
["Table1"], ""] as CurrencyManager
;
771 data_source
.Tables
["Table1"].DefaultView
.ListChanged
+=
772 new ListChangedEventHandler (DataView_ListChanged
);
774 event_num
= current_changed
= position_changed
= -1;
775 cm
.CurrentChanged
+= new EventHandler (CurrentChanged
);
776 cm
.PositionChanged
+= new EventHandler (PositionChanged
);
777 cm
.ItemChanged
+= new ItemChangedEventHandler (ItemChanged
);
779 Assert
.AreEqual (0, cm
.Position
, "EndAddNew1");
780 Assert
.AreEqual (10, cm
.Count
, "EndAddNew2");
783 Console
.WriteLine ("position = {0}", cm
.Position
);
785 Assert
.AreEqual (0, item_changed
, "EndAddNew3");
786 Assert
.AreEqual (-1, item_changed_args
.Index
, "EndAddNew4");
787 Assert
.AreEqual (1, current_changed
, "EndAddNew5");
788 Assert
.AreEqual (2, position_changed
, "EndAddNew6");
790 cm
.EndCurrentEdit ();
791 Console
.WriteLine ("position = {0}", cm
.Position
);
793 Assert
.AreEqual (3, item_changed
, "EndAddNew7");
794 Assert
.AreEqual (-1, item_changed_args
.Index
, "EndAddNew8");
795 Assert
.AreEqual (1, current_changed
, "EndAddNew9");
796 Assert
.AreEqual (2, position_changed
, "EndAddNew10");
798 Assert
.AreEqual (10, cm
.Position
, "EndAddNew11");
799 Assert
.AreEqual (11, cm
.Count
, "EndAddNew12");
801 cm
.CurrentChanged
-= new EventHandler (CurrentChanged
);
802 cm
.PositionChanged
-= new EventHandler (PositionChanged
);
805 void DataView_ListChanged (object sender
, ListChangedEventArgs e
)
807 Console
.WriteLine ("{0} {1} {2}", e
.ListChangedType
, e
.OldIndex
, e
.NewIndex
);
811 public void AddNew2 ()
813 if (TestHelper
.RunningOnUnix
) {
814 Assert
.Ignore ("Fails at the moment due to a System.Data constraint violation");
817 DataSet data_source
= CreateRelatedDataSet ();
818 BindingContext bc
= new BindingContext ();
819 CurrencyManager cm
= bc
[data_source
, "Table1"] as CurrencyManager
;
821 DataView dv
= cm
.List
as DataView
;
823 event_num
= current_changed
= position_changed
= -1;
824 cm
.CurrentChanged
+= new EventHandler (CurrentChanged
);
825 cm
.PositionChanged
+= new EventHandler (PositionChanged
);
826 cm
.ItemChanged
+= new ItemChangedEventHandler (ItemChanged
);
827 dv
.ListChanged
+= new ListChangedEventHandler (ListChanged
);
829 Assert
.AreEqual (0, cm
.Position
, "AddNew1");
830 Assert
.AreEqual (10, cm
.Count
, "AddNew2");
834 Assert
.AreEqual (10, cm
.Position
, "AddNew3");
835 Assert
.AreEqual (11, cm
.Count
, "AddNew4");
837 // this does an implicit EndCurrentEdit
840 Assert
.AreEqual (11, cm
.Position
, "AddNew5");
841 Assert
.AreEqual (12, cm
.Count
, "AddNew6");
845 DataSet
CreateRelatedDataSetLarge ()
847 DataSet dataset
= new DataSet ("CustomerSet");
848 DataTable dt1
= new DataTable ("Customers");
849 DataTable dt2
= new DataTable ("Orders");
850 DataTable dt3
= new DataTable ("Addresses");
854 column
= new DataColumn ("CustomerID");
855 column
.DataType
= typeof (int);
856 column
.Unique
= true;
857 dt1
.Columns
.Add (column
);
859 column
= new DataColumn ("CustomerName");
860 column
.DataType
= typeof (string);
861 column
.Unique
= false;
862 dt1
.Columns
.Add (column
);
865 column
= new DataColumn ("OrderID");
866 column
.DataType
= typeof (int);
867 column
.Unique
= true;
868 dt2
.Columns
.Add (column
);
870 column
= new DataColumn ("ItemName");
871 column
.DataType
= typeof (string);
872 column
.Unique
= false;
873 dt2
.Columns
.Add (column
);
875 column
= new DataColumn ("CustomerID");
876 column
.DataType
= typeof (int);
877 column
.Unique
= false;
878 dt2
.Columns
.Add (column
);
880 column
= new DataColumn ("AddressID");
881 column
.DataType
= typeof (int);
882 column
.Unique
= false;
883 dt2
.Columns
.Add (column
);
886 column
= new DataColumn ("AddressID");
887 column
.DataType
= typeof (int);
888 column
.Unique
= true;
889 dt3
.Columns
.Add (column
);
891 column
= new DataColumn ("AddressString");
892 column
.DataType
= typeof (string);
893 column
.Unique
= false;
894 dt3
.Columns
.Add (column
);
896 column
= new DataColumn ("CustomerID");
897 column
.DataType
= typeof (int);
898 column
.Unique
= false;
899 dt3
.Columns
.Add (column
);
901 for (int i
= 0; i
< 10; i
++) {
902 DataRow row
= dt1
.NewRow ();
903 row
["CustomerID"] = i
;
904 row
["CustomerName"] = String
.Format ("Customer Name #{0}", i
);
909 for (int i
= 0; i
< 10; i
++) {
910 for (int j
= 0; j
< (i
< 5 ? 3 : 5); j
++) {
911 DataRow row
= dt2
.NewRow ();
912 row
["OrderID"] = ordernum
++;
913 row
["ItemName"] = String
.Format ("Item order #{0}", j
);
914 row
["CustomerID"] = i
;
915 row
["AddressID"] = j
;
921 for (int i
= 0; i
< 4; i
++) {
922 for (int j
= 0; j
< 4; j
++) {
923 DataRow row
= dt3
.NewRow ();
924 row
["AddressID"] = addressid
++;
925 row
["AddressString"] = String
.Format ("Customer Address {0}", j
);
926 row
["CustomerID"] = i
;
931 dataset
.Tables
.Add (dt1
);
932 dataset
.Tables
.Add (dt2
);
933 dataset
.Tables
.Add (dt3
);
934 dataset
.Relations
.Add ("Customer_Orders", dt1
.Columns
["CustomerID"], dt2
.Columns
["CustomerID"]);
935 dataset
.Relations
.Add ("Customer_Addresses", dt1
.Columns
["CustomerID"], dt3
.Columns
["CustomerID"]);
936 dataset
.Relations
.Add ("Address_Orders", dt3
.Columns
["AddressID"], dt2
.Columns
["AddressID"]);
942 public void RelatedCurrencyManagerTest ()
944 DataSet data_source
= CreateRelatedDataSetLarge ();
945 BindingContext bc
= new BindingContext ();
946 CurrencyManager cm
= bc
[data_source
, "Customers"] as CurrencyManager
;
947 CurrencyManager rcm
= bc
[data_source
, "Customers.Customer_Orders"] as CurrencyManager
;
949 IList list
= rcm
.List
;
950 Assert
.AreEqual (3, rcm
.Count
, "count1");
951 Assert
.AreEqual (3, list
.Count
, "listcount1");
954 Assert
.AreEqual (3, rcm
.Count
, "count2");
955 Assert
.AreEqual (3, list
.Count
, "listcount2");
958 Assert
.AreEqual (5, rcm
.Count
, "count3");
959 Assert
.AreEqual (3, list
.Count
, "listcount3");
963 public void TestCurrencyManagerBindings ()
965 DataSet data_source
= CreateRelatedDataSetLarge ();
966 BindingContext bc
= new BindingContext ();
968 CurrencyManager cm
= bc
[data_source
] as CurrencyManager
;
970 Console
.WriteLine ("cm properties:");
971 foreach (PropertyDescriptor pd
in cm
.GetItemProperties ())
972 Console
.WriteLine (" + {0}", pd
.Name
);
973 Console
.WriteLine ();
975 Console
.WriteLine ("dataset:");
976 Console
.WriteLine ("cm = {0}", cm
.GetType());
977 Console
.WriteLine ("cm.Count = {0}", cm
.Count
);
979 Console
.WriteLine ("cm.Current = {0}", cm
.Current
);
980 Console
.WriteLine ("cm.Current properties");
981 foreach (PropertyDescriptor pd
in ((ICustomTypeDescriptor
)cm
.Current
).GetProperties ())
982 Console
.WriteLine (" + {0}", pd
.Name
);
983 Console
.WriteLine ();
985 cm
= bc
[data_source
.Tables
["Customers"]] as CurrencyManager
;
986 Console
.WriteLine ("datatable:");
987 Console
.WriteLine ("cm = {0}", cm
.GetType());
988 Console
.WriteLine ("cm.Count = {0}", cm
.Count
);
990 Console
.WriteLine ("cm.Current = {0}", cm
.Current
);
991 Console
.WriteLine ("cm.Current properties");
992 foreach (PropertyDescriptor pd
in ((ICustomTypeDescriptor
)cm
.Current
).GetProperties ())
993 Console
.WriteLine (" + {0}", pd
.Name
);
995 Console
.WriteLine ();
997 DataViewManager vm
= new DataViewManager (data_source
);
998 Console
.WriteLine ("vm properties:");
999 foreach (PropertyDescriptor pd
in ((ITypedList
)vm
).GetItemProperties (null))
1000 Console
.WriteLine (" + {0}", pd
.Name
);
1001 Console
.WriteLine ();
1005 Type
GetFinalType (CurrencyManager cm
)
1007 FieldInfo fi
= cm
.GetType().GetField ("finalType", BindingFlags
.NonPublic
| BindingFlags
.Instance
);
1009 return (Type
)fi
.GetValue (cm
);
1013 public void FinalTypeTest ()
1015 BindingContext bc
= new BindingContext ();
1018 DataSet data_source
= CreateRelatedDataSetLarge ();
1020 /* empty arraylist */
1021 al
= new ArrayList ();
1022 cm
= bc
[al
] as CurrencyManager
;
1023 Assert
.AreEqual (typeof (ArrayList
), GetFinalType (cm
), "A1");
1025 /* arraylist with a string element*/
1026 al
= new ArrayList ();
1028 cm
= bc
[al
] as CurrencyManager
;
1029 Assert
.AreEqual (typeof (ArrayList
), GetFinalType (cm
), "A2");
1032 string[] s
= new string[1];
1034 cm
= bc
[s
] as CurrencyManager
;
1035 Assert
.AreEqual (typeof (string[]), GetFinalType (cm
), "A3");
1038 cm
= bc
[data_source
, "Customers"] as CurrencyManager
;
1039 Assert
.AreEqual (typeof (DataView
), GetFinalType (cm
), "A4");
1042 cm
= bc
[data_source
, "Customers.Customer_Orders"] as CurrencyManager
;
1043 /* on MS this is a RelatedView, on Mono a RelatedDataView. both subclass from DataView, so let's check that. */
1044 Assert
.IsFalse (typeof (DataView
) == GetFinalType (cm
), "A5");
1045 Assert
.IsTrue (typeof (DataView
).IsAssignableFrom (GetFinalType (cm
)), "A6");