BindingContextTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jackson Harper [email protected]
  24. using System;
  25. using System.Data;
  26. using System.Collections;
  27. using System.ComponentModel;
  28. using System.Windows.Forms;
  29. using NUnit.Framework;
  30. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  31. namespace MonoTests.System.Windows.Forms {
  32. [TestFixture]
  33. public class BindingContextTest {
  34. private class BindingContextPoker : BindingContext {
  35. public int collection_changed;
  36. public void _Add (object data_source, BindingManagerBase list_manager)
  37. {
  38. Add (data_source, list_manager);
  39. }
  40. public void _AddCore (object data_source, BindingManagerBase list_manager)
  41. {
  42. AddCore (data_source, list_manager);
  43. }
  44. public void _Clear ()
  45. {
  46. Clear ();
  47. }
  48. public void _ClearCore ()
  49. {
  50. ClearCore ();
  51. }
  52. public void _Remove (object data_source)
  53. {
  54. Remove (data_source);
  55. }
  56. public void _RemoveCore (object data_source)
  57. {
  58. RemoveCore (data_source);
  59. }
  60. protected override void OnCollectionChanged (CollectionChangeEventArgs ce)
  61. {
  62. collection_changed = (int) ce.Action;
  63. base.OnCollectionChanged (ce);
  64. }
  65. }
  66. [SetUp]
  67. protected virtual void SetUp ()
  68. {
  69. }
  70. [TearDown]
  71. protected virtual void TearDown ()
  72. {
  73. }
  74. [Test]
  75. public void CtorTest ()
  76. {
  77. BindingContext bc = new BindingContext ();
  78. Assert.IsFalse (bc.IsReadOnly, "CT1");
  79. Assert.IsFalse (bc.Contains (this), "CT2");
  80. Assert.IsFalse (bc.Contains (this, String.Empty), "CT3");
  81. Assert.IsFalse (bc.Contains (this, "Me is String"), "CT4");
  82. // Test the ICollection interface
  83. ICollection ic = (ICollection) bc;
  84. Assert.AreEqual (ic.Count, 0, "CT5");
  85. Assert.IsFalse (ic.IsSynchronized, "CT6");
  86. Assert.IsNull (ic.SyncRoot, "CT7");
  87. object [] arr = new object [] { "A", "B", "C" };
  88. ic.CopyTo (arr, 0);
  89. Assert.AreEqual (0, ic.Count, "CT8");
  90. Assert.IsFalse (ic.GetEnumerator ().MoveNext (), "CT9");
  91. }
  92. [Test]
  93. [ExpectedException (typeof (ArgumentNullException))]
  94. public void TestIndexerNull ()
  95. {
  96. BindingContext bc = new BindingContext ();
  97. BindingManagerBase a, b;
  98. a = bc [null];
  99. }
  100. [Test]
  101. public void TestIndexerNoMember ()
  102. {
  103. BindingContext bc = new BindingContext ();
  104. ArrayList data_source = new ArrayList ();
  105. BindingManagerBase a, b;
  106. data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
  107. a = bc [data_source];
  108. b = bc [data_source];
  109. Assert.AreSame (a, b, "INNM1");
  110. b = bc [data_source, String.Empty];
  111. Assert.AreSame (a, b, "INNM2");
  112. // Only one is added to the list
  113. Assert.AreEqual (((ICollection) bc).Count, 1);
  114. }
  115. [Test]
  116. public void TestIndexerWithMember ()
  117. {
  118. BindingContext bc = new BindingContext ();
  119. ArrayList data_source = new ArrayList ();
  120. BindingManagerBase a, b, c;
  121. data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
  122. a = bc [data_source, "Length"];
  123. b = bc [data_source, "Length"];
  124. Assert.AreSame (a, b, "INWM1");
  125. b = bc [data_source];
  126. Assert.IsFalse (object.ReferenceEquals (a, b), "INWM2");
  127. c = bc [data_source];
  128. Assert.AreSame (b, c, "INWM3");
  129. b = bc [data_source, "Length"];
  130. Assert.AreSame (a, b, "INWM4");
  131. }
  132. [Test]
  133. [ExpectedException (typeof (ArgumentException))]
  134. public void CantCreateChildList ()
  135. {
  136. BindingContext bc = new BindingContext ();
  137. ArrayList data_source = new ArrayList ();
  138. BindingManagerBase a = bc [data_source, "Items"];
  139. }
  140. [Test]
  141. [ExpectedException (typeof (ArgumentException))]
  142. public void CantCreateChildList2 ()
  143. {
  144. BindingContext bc = new BindingContext ();
  145. ArrayList data_source = new ArrayList ();
  146. BindingManagerBase a = bc [data_source, "Count"];
  147. }
  148. [Test]
  149. public void CreateCurrencyManager ()
  150. {
  151. BindingContext bc = new BindingContext ();
  152. ArrayList data_source = new ArrayList ();
  153. CurrencyManager a = bc [data_source] as CurrencyManager;
  154. Assert.IsNotNull (a, "CCM1");
  155. }
  156. [Test]
  157. public void CreatePropertyManager ()
  158. {
  159. BindingContext bc = new BindingContext ();
  160. object data_source = new object ();
  161. PropertyManager a = bc [data_source] as PropertyManager;
  162. Assert.IsNotNull (a, "CPM1");
  163. }
  164. private DataSet CreateRelatedDataSet ()
  165. {
  166. DataSet dataset = new DataSet ("DataSet");
  167. DataTable dt1 = new DataTable ("Table1");
  168. DataTable dt2 = new DataTable ("Table2");
  169. DataColumn column;
  170. column = new DataColumn ("One");
  171. column.DataType = typeof (int);
  172. column.Unique = true;
  173. dt1.Columns.Add (column);
  174. for (int i = 0; i < 10; i++) {
  175. DataRow row = dt1.NewRow ();
  176. row ["One"] = i;
  177. dt1.Rows.Add (row);
  178. }
  179. column = new DataColumn ("Two");
  180. column.DataType = typeof (int);
  181. column.Unique = true;
  182. dt2.Columns.Add (column);
  183. for (int i = 0; i < 10; i++) {
  184. DataRow row = dt2.NewRow ();
  185. row ["Two"] = i;
  186. dt2.Rows.Add (row);
  187. }
  188. dataset.Tables.Add (dt1);
  189. dataset.Tables.Add (dt2);
  190. dataset.Relations.Add ("Relation", dt1.Columns ["One"], dt2.Columns ["Two"]);
  191. return dataset;
  192. }
  193. [Test]
  194. public void CreateComplexManager ()
  195. {
  196. BindingContext bc = new BindingContext ();
  197. DataSet dataset = CreateRelatedDataSet ();
  198. CurrencyManager cm = bc [dataset, "Table1.Relation"] as CurrencyManager;
  199. Assert.IsNotNull (cm, "CCCM1");
  200. }
  201. [Test]
  202. [ExpectedException (typeof (ArgumentException))]
  203. public void FailToCreateComplexManagerRelationDoesNotExist ()
  204. {
  205. BindingContext bc = new BindingContext ();
  206. DataSet dataset = CreateRelatedDataSet ();
  207. CurrencyManager cm = bc [dataset, "Table1.ImNotRelated"] as CurrencyManager;
  208. }
  209. [Test]
  210. [ExpectedException (typeof (ArgumentException))]
  211. public void FailToCreateComplexManagerNoTableSpecified ()
  212. {
  213. BindingContext bc = new BindingContext ();
  214. DataSet dataset = CreateRelatedDataSet ();
  215. CurrencyManager cm = bc [dataset, "Relation"] as CurrencyManager;
  216. }
  217. [Test]
  218. [ExpectedException (typeof (ArgumentException))]
  219. public void FailToCreateComplexChildTableSpecified ()
  220. {
  221. BindingContext bc = new BindingContext ();
  222. DataSet dataset = CreateRelatedDataSet ();
  223. CurrencyManager cm = bc [dataset, "Table2.Relation"] as CurrencyManager;
  224. }
  225. [Test]
  226. [ExpectedException (typeof (NotImplementedException))]
  227. public void CantSubscribeToCollectionChanged ()
  228. {
  229. BindingContext bc = new BindingContext ();
  230. bc.CollectionChanged += new CollectionChangeEventHandler (Dummy);
  231. }
  232. [Test]
  233. public void CantSubscribeToCollectionChanged2 ()
  234. {
  235. BindingContext bc = new BindingContext ();
  236. bc.CollectionChanged -= new CollectionChangeEventHandler (Dummy);
  237. }
  238. private void Dummy (object sender, CollectionChangeEventArgs e)
  239. {
  240. }
  241. [Test]
  242. [ExpectedException (typeof (ArgumentNullException))]
  243. public void AddNullDataSource ()
  244. {
  245. BindingContextPoker p = new BindingContextPoker ();
  246. p._Add (null, new PropertyManager ());
  247. }
  248. [Test]
  249. [ExpectedException (typeof (ArgumentNullException))]
  250. public void AddNullListManager ()
  251. {
  252. BindingContextPoker p = new BindingContextPoker ();
  253. p._Add (new object (), null);
  254. }
  255. [Test]
  256. public void Add ()
  257. {
  258. BindingContextPoker p = new BindingContextPoker ();
  259. object data_source = new object ();
  260. p.collection_changed = -1;
  261. Assert.IsFalse (p.Contains (data_source), "ADD1");
  262. Assert.AreEqual (0, ((ICollection) p).Count, "ADD2");
  263. p._Add (data_source, new PropertyManager ());
  264. Assert.IsTrue (p.Contains (data_source), "ADD3");
  265. Assert.AreEqual (1, ((ICollection) p).Count, "ADD4");
  266. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD5");
  267. p.collection_changed = -1;
  268. p._Add (data_source, new PropertyManager ());
  269. Assert.IsTrue (p.Contains (data_source), "ADD6");
  270. Assert.AreEqual (1, ((ICollection) p).Count, "ADD7");
  271. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD8");
  272. p.collection_changed = -1;
  273. data_source = new object ();
  274. p._Add (data_source, new PropertyManager ());
  275. Assert.IsTrue (p.Contains (data_source), "ADD9");
  276. Assert.AreEqual (2, ((ICollection) p).Count, "ADD10");
  277. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD9");
  278. }
  279. [Test]
  280. public void AddCore ()
  281. {
  282. BindingContextPoker p = new BindingContextPoker ();
  283. object data_source = new object ();
  284. p.collection_changed = -1;
  285. Assert.IsFalse (p.Contains (data_source), "ADDCORE1");
  286. Assert.AreEqual (0, ((ICollection) p).Count, "ADDCORE2");
  287. p._AddCore (data_source, new PropertyManager ());
  288. Assert.IsTrue (p.Contains (data_source), "ADDCORE3");
  289. Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE4");
  290. Assert.AreEqual (p.collection_changed, -1, "ADDCORE5");
  291. p.collection_changed = -1;
  292. p._AddCore (data_source, new PropertyManager ());
  293. Assert.IsTrue (p.Contains (data_source), "ADDCORE6");
  294. Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE7");
  295. Assert.AreEqual (p.collection_changed, -1, "ADDCORE8");
  296. p.collection_changed = -1;
  297. data_source = new object ();
  298. p._AddCore (data_source, new PropertyManager ());
  299. Assert.IsTrue (p.Contains (data_source), "ADDCORE9");
  300. Assert.AreEqual (2, ((ICollection) p).Count, "ADDCORE10");
  301. Assert.AreEqual (p.collection_changed, -1, "ADDCORE11");
  302. }
  303. [Test]
  304. [ExpectedException (typeof (ArgumentNullException))]
  305. public void RemoveNull ()
  306. {
  307. BindingContextPoker p = new BindingContextPoker ();
  308. p._Remove (null);
  309. }
  310. [Test]
  311. public void Remove ()
  312. {
  313. BindingContextPoker p = new BindingContextPoker ();
  314. object data_source = new object ();
  315. p.collection_changed = -1;
  316. p._Add (data_source, new PropertyManager ());
  317. Assert.IsTrue (p.Contains (data_source), "REMOVE1");
  318. Assert.AreEqual (1, ((ICollection) p).Count, "REMOVE2");
  319. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVE3");
  320. p._Remove (data_source);
  321. Assert.IsFalse (p.Contains (data_source), "REMOVE4");
  322. Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE5");
  323. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE6");
  324. // Double remove
  325. p.collection_changed = -1;
  326. p._Remove (data_source);
  327. Assert.IsFalse (p.Contains (data_source), "REMOVE7");
  328. Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE8");
  329. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE9");
  330. }
  331. [Test]
  332. public void RemoveCore ()
  333. {
  334. BindingContextPoker p = new BindingContextPoker ();
  335. object data_source = new object ();
  336. p.collection_changed = -1;
  337. p._Add (data_source, new PropertyManager ());
  338. Assert.IsTrue (p.Contains (data_source), "REMOVECORE1");
  339. Assert.AreEqual (1, ((ICollection) p).Count, "REMOVECORE2");
  340. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVECORE3");
  341. p.collection_changed = -1;
  342. p._RemoveCore (data_source);
  343. Assert.IsFalse (p.Contains (data_source), "REMOVECORE4");
  344. Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE5");
  345. Assert.AreEqual (p.collection_changed, -1, "REMOVECORE6");
  346. // Double remove
  347. p.collection_changed = -1;
  348. p._Remove (data_source);
  349. Assert.IsFalse (p.Contains (data_source), "REMOVECORE7");
  350. Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE8");
  351. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVECORE9");
  352. }
  353. [Test]
  354. public void Clear ()
  355. {
  356. BindingContextPoker p = new BindingContextPoker ();
  357. object data_source = new object ();
  358. p._Add (data_source, new PropertyManager ());
  359. p.collection_changed = -1;
  360. p._Clear ();
  361. Assert.IsFalse (p.Contains (data_source), "CLEAR1");
  362. Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
  363. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
  364. // Double clear
  365. p.collection_changed = -1;
  366. p._Clear ();
  367. Assert.IsFalse (p.Contains (data_source), "CLEAR1");
  368. Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
  369. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
  370. }
  371. [Test]
  372. public void ClearCore ()
  373. {
  374. BindingContextPoker p = new BindingContextPoker ();
  375. object data_source = new object ();
  376. p._Add (data_source, new PropertyManager ());
  377. p.collection_changed = -1;
  378. p._Clear ();
  379. Assert.IsFalse (p.Contains (data_source), "CLEARCORE1");
  380. Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE2");
  381. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE3");
  382. // Double clear
  383. p.collection_changed = -1;
  384. p._Clear ();
  385. Assert.IsFalse (p.Contains (data_source), "CLEARCORE4");
  386. Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE5");
  387. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE6");
  388. }
  389. [Test]
  390. public void TestContains ()
  391. {
  392. BindingContextPoker p = new BindingContextPoker ();
  393. object data_source = new object ();
  394. p._Add (data_source, new PropertyManager ());
  395. Assert.IsTrue (p.Contains (data_source), "#1");
  396. Assert.IsFalse (p.Contains ("nonexistent"), "#2");
  397. }
  398. [Test]
  399. [ExpectedException (typeof (ArgumentNullException))]
  400. public void TestContainsNull ()
  401. {
  402. BindingContextPoker p = new BindingContextPoker ();
  403. p.Contains (null);
  404. }
  405. [Test]
  406. public void TestContainsNull2 ()
  407. {
  408. BindingContextPoker p = new BindingContextPoker ();
  409. object data_source = new object ();
  410. p._Add (data_source, new PropertyManager ());
  411. Assert.IsTrue (p.Contains (data_source, null), "#1");
  412. Assert.IsFalse (p.Contains ("nonexistent", null), "#2");
  413. }
  414. [Test]
  415. public void TestGetEnumerator ()
  416. {
  417. BindingContextPoker p = new BindingContextPoker ();
  418. object data_source = new object ();
  419. PropertyManager pm = new PropertyManager ();
  420. p._Add (data_source, pm);
  421. IEnumerator e = ((IEnumerable) p).GetEnumerator ();
  422. Assert.IsNotNull (e, "#1");
  423. IDictionaryEnumerator de = e as IDictionaryEnumerator;
  424. Assert.IsNotNull (de, "#2");
  425. Assert.IsTrue (de.MoveNext (), "#3");
  426. // In .NET Key is its internal type.
  427. //Assert.AreEqual (data_source, de.Key, "#4");
  428. //Assert.AreEqual (pm, de.Value, "#5");
  429. Assert.IsFalse (de.MoveNext (), "#6");
  430. }
  431. }
  432. }