BindingContextTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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;
  98. a = bc [null];
  99. TestHelper.RemoveWarning (a, bc);
  100. }
  101. [Test]
  102. public void TestIndexerNoMember ()
  103. {
  104. BindingContext bc = new BindingContext ();
  105. ArrayList data_source = new ArrayList ();
  106. BindingManagerBase a, b;
  107. data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
  108. a = bc [data_source];
  109. b = bc [data_source];
  110. Assert.AreSame (a, b, "INNM1");
  111. b = bc [data_source, String.Empty];
  112. Assert.AreSame (a, b, "INNM2");
  113. // Only one is added to the list
  114. Assert.AreEqual (((ICollection) bc).Count, 1);
  115. }
  116. [Test]
  117. public void TestIndexerWithMember ()
  118. {
  119. BindingContext bc = new BindingContext ();
  120. ArrayList data_source = new ArrayList ();
  121. BindingManagerBase a, b, c;
  122. data_source.AddRange (new string [] { "1", "2", "3", "4", "5" });
  123. a = bc [data_source, "Length"];
  124. b = bc [data_source, "Length"];
  125. Assert.AreSame (a, b, "INWM1");
  126. b = bc [data_source];
  127. Assert.IsFalse (object.ReferenceEquals (a, b), "INWM2");
  128. c = bc [data_source];
  129. Assert.AreSame (b, c, "INWM3");
  130. b = bc [data_source, "Length"];
  131. Assert.AreSame (a, b, "INWM4");
  132. }
  133. [Test]
  134. [ExpectedException (typeof (ArgumentException))]
  135. public void CantCreateChildList ()
  136. {
  137. BindingContext bc = new BindingContext ();
  138. ArrayList data_source = new ArrayList ();
  139. BindingManagerBase a = bc [data_source, "Items"];
  140. TestHelper.RemoveWarning (a);
  141. }
  142. [Test]
  143. [ExpectedException (typeof (ArgumentException))]
  144. public void CantCreateChildList2 ()
  145. {
  146. BindingContext bc = new BindingContext ();
  147. ArrayList data_source = new ArrayList ();
  148. BindingManagerBase a = bc [data_source, "Count"];
  149. TestHelper.RemoveWarning (a);
  150. }
  151. [Test]
  152. public void CreateCurrencyManager ()
  153. {
  154. BindingContext bc = new BindingContext ();
  155. ArrayList data_source = new ArrayList ();
  156. CurrencyManager a = bc [data_source] as CurrencyManager;
  157. Assert.IsNotNull (a, "CCM1");
  158. }
  159. [Test]
  160. public void CreatePropertyManager ()
  161. {
  162. BindingContext bc = new BindingContext ();
  163. object data_source = new object ();
  164. PropertyManager a = bc [data_source] as PropertyManager;
  165. Assert.IsNotNull (a, "CPM1");
  166. }
  167. private DataSet CreateRelatedDataSet ()
  168. {
  169. DataSet dataset = new DataSet ("DataSet");
  170. DataTable dt1 = new DataTable ("Table1");
  171. DataTable dt2 = new DataTable ("Table2");
  172. DataColumn column;
  173. column = new DataColumn ("One");
  174. column.DataType = typeof (int);
  175. column.Unique = true;
  176. dt1.Columns.Add (column);
  177. for (int i = 0; i < 10; i++) {
  178. DataRow row = dt1.NewRow ();
  179. row ["One"] = i;
  180. dt1.Rows.Add (row);
  181. }
  182. column = new DataColumn ("Two");
  183. column.DataType = typeof (int);
  184. column.Unique = true;
  185. dt2.Columns.Add (column);
  186. for (int i = 0; i < 10; i++) {
  187. DataRow row = dt2.NewRow ();
  188. row ["Two"] = i;
  189. dt2.Rows.Add (row);
  190. }
  191. dataset.Tables.Add (dt1);
  192. dataset.Tables.Add (dt2);
  193. dataset.Relations.Add ("Relation", dt1.Columns ["One"], dt2.Columns ["Two"]);
  194. return dataset;
  195. }
  196. [Test]
  197. public void CreateComplexManager ()
  198. {
  199. BindingContext bc = new BindingContext ();
  200. DataSet dataset = CreateRelatedDataSet ();
  201. CurrencyManager cm = bc [dataset, "Table1.Relation"] as CurrencyManager;
  202. Assert.IsNotNull (cm, "CCCM1");
  203. }
  204. [Test]
  205. [ExpectedException (typeof (ArgumentException))]
  206. public void FailToCreateComplexManagerRelationDoesNotExist ()
  207. {
  208. BindingContext bc = new BindingContext ();
  209. DataSet dataset = CreateRelatedDataSet ();
  210. CurrencyManager cm = bc [dataset, "Table1.ImNotRelated"] as CurrencyManager;
  211. TestHelper.RemoveWarning (cm);
  212. }
  213. [Test]
  214. [ExpectedException (typeof (ArgumentException))]
  215. public void FailToCreateComplexManagerNoTableSpecified ()
  216. {
  217. BindingContext bc = new BindingContext ();
  218. DataSet dataset = CreateRelatedDataSet ();
  219. CurrencyManager cm = bc [dataset, "Relation"] as CurrencyManager;
  220. TestHelper.RemoveWarning (cm);
  221. }
  222. [Test]
  223. [ExpectedException (typeof (ArgumentException))]
  224. public void FailToCreateComplexChildTableSpecified ()
  225. {
  226. BindingContext bc = new BindingContext ();
  227. DataSet dataset = CreateRelatedDataSet ();
  228. CurrencyManager cm = bc [dataset, "Table2.Relation"] as CurrencyManager;
  229. TestHelper.RemoveWarning (cm);
  230. }
  231. [Test]
  232. [ExpectedException (typeof (NotImplementedException))]
  233. public void CantSubscribeToCollectionChanged ()
  234. {
  235. BindingContext bc = new BindingContext ();
  236. bc.CollectionChanged += new CollectionChangeEventHandler (Dummy);
  237. }
  238. [Test]
  239. public void CantSubscribeToCollectionChanged2 ()
  240. {
  241. BindingContext bc = new BindingContext ();
  242. bc.CollectionChanged -= new CollectionChangeEventHandler (Dummy);
  243. }
  244. private void Dummy (object sender, CollectionChangeEventArgs e)
  245. {
  246. }
  247. [Test]
  248. [ExpectedException (typeof (ArgumentNullException))]
  249. public void AddNullDataSource ()
  250. {
  251. BindingContextPoker p = new BindingContextPoker ();
  252. p._Add (null, new PropertyManager ());
  253. }
  254. [Test]
  255. [ExpectedException (typeof (ArgumentNullException))]
  256. public void AddNullListManager ()
  257. {
  258. BindingContextPoker p = new BindingContextPoker ();
  259. p._Add (new object (), null);
  260. }
  261. [Test]
  262. public void Add ()
  263. {
  264. BindingContextPoker p = new BindingContextPoker ();
  265. object data_source = new object ();
  266. p.collection_changed = -1;
  267. Assert.IsFalse (p.Contains (data_source), "ADD1");
  268. Assert.AreEqual (0, ((ICollection) p).Count, "ADD2");
  269. p._Add (data_source, new PropertyManager ());
  270. Assert.IsTrue (p.Contains (data_source), "ADD3");
  271. Assert.AreEqual (1, ((ICollection) p).Count, "ADD4");
  272. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD5");
  273. p.collection_changed = -1;
  274. p._Add (data_source, new PropertyManager ());
  275. Assert.IsTrue (p.Contains (data_source), "ADD6");
  276. Assert.AreEqual (1, ((ICollection) p).Count, "ADD7");
  277. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD8");
  278. p.collection_changed = -1;
  279. data_source = new object ();
  280. p._Add (data_source, new PropertyManager ());
  281. Assert.IsTrue (p.Contains (data_source), "ADD9");
  282. Assert.AreEqual (2, ((ICollection) p).Count, "ADD10");
  283. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "ADD9");
  284. }
  285. [Test]
  286. public void AddCore ()
  287. {
  288. BindingContextPoker p = new BindingContextPoker ();
  289. object data_source = new object ();
  290. p.collection_changed = -1;
  291. Assert.IsFalse (p.Contains (data_source), "ADDCORE1");
  292. Assert.AreEqual (0, ((ICollection) p).Count, "ADDCORE2");
  293. p._AddCore (data_source, new PropertyManager ());
  294. Assert.IsTrue (p.Contains (data_source), "ADDCORE3");
  295. Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE4");
  296. Assert.AreEqual (p.collection_changed, -1, "ADDCORE5");
  297. p.collection_changed = -1;
  298. p._AddCore (data_source, new PropertyManager ());
  299. Assert.IsTrue (p.Contains (data_source), "ADDCORE6");
  300. Assert.AreEqual (1, ((ICollection) p).Count, "ADDCORE7");
  301. Assert.AreEqual (p.collection_changed, -1, "ADDCORE8");
  302. p.collection_changed = -1;
  303. data_source = new object ();
  304. p._AddCore (data_source, new PropertyManager ());
  305. Assert.IsTrue (p.Contains (data_source), "ADDCORE9");
  306. Assert.AreEqual (2, ((ICollection) p).Count, "ADDCORE10");
  307. Assert.AreEqual (p.collection_changed, -1, "ADDCORE11");
  308. }
  309. [Test]
  310. [ExpectedException (typeof (ArgumentNullException))]
  311. public void RemoveNull ()
  312. {
  313. BindingContextPoker p = new BindingContextPoker ();
  314. p._Remove (null);
  315. }
  316. [Test]
  317. public void Remove ()
  318. {
  319. BindingContextPoker p = new BindingContextPoker ();
  320. object data_source = new object ();
  321. p.collection_changed = -1;
  322. p._Add (data_source, new PropertyManager ());
  323. Assert.IsTrue (p.Contains (data_source), "REMOVE1");
  324. Assert.AreEqual (1, ((ICollection) p).Count, "REMOVE2");
  325. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVE3");
  326. p._Remove (data_source);
  327. Assert.IsFalse (p.Contains (data_source), "REMOVE4");
  328. Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE5");
  329. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE6");
  330. // Double remove
  331. p.collection_changed = -1;
  332. p._Remove (data_source);
  333. Assert.IsFalse (p.Contains (data_source), "REMOVE7");
  334. Assert.AreEqual (0, ((ICollection) p).Count, "REMOVE8");
  335. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVE9");
  336. }
  337. [Test]
  338. public void RemoveCore ()
  339. {
  340. BindingContextPoker p = new BindingContextPoker ();
  341. object data_source = new object ();
  342. p.collection_changed = -1;
  343. p._Add (data_source, new PropertyManager ());
  344. Assert.IsTrue (p.Contains (data_source), "REMOVECORE1");
  345. Assert.AreEqual (1, ((ICollection) p).Count, "REMOVECORE2");
  346. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Add, "REMOVECORE3");
  347. p.collection_changed = -1;
  348. p._RemoveCore (data_source);
  349. Assert.IsFalse (p.Contains (data_source), "REMOVECORE4");
  350. Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE5");
  351. Assert.AreEqual (p.collection_changed, -1, "REMOVECORE6");
  352. // Double remove
  353. p.collection_changed = -1;
  354. p._Remove (data_source);
  355. Assert.IsFalse (p.Contains (data_source), "REMOVECORE7");
  356. Assert.AreEqual (0, ((ICollection) p).Count, "REMOVECORE8");
  357. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Remove, "REMOVECORE9");
  358. }
  359. [Test]
  360. public void Clear ()
  361. {
  362. BindingContextPoker p = new BindingContextPoker ();
  363. object data_source = new object ();
  364. p._Add (data_source, new PropertyManager ());
  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. // Double clear
  371. p.collection_changed = -1;
  372. p._Clear ();
  373. Assert.IsFalse (p.Contains (data_source), "CLEAR1");
  374. Assert.AreEqual (0, ((ICollection) p).Count, "CLEAR2");
  375. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEAR3");
  376. }
  377. [Test]
  378. public void ClearCore ()
  379. {
  380. BindingContextPoker p = new BindingContextPoker ();
  381. object data_source = new object ();
  382. p._Add (data_source, new PropertyManager ());
  383. p.collection_changed = -1;
  384. p._Clear ();
  385. Assert.IsFalse (p.Contains (data_source), "CLEARCORE1");
  386. Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE2");
  387. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE3");
  388. // Double clear
  389. p.collection_changed = -1;
  390. p._Clear ();
  391. Assert.IsFalse (p.Contains (data_source), "CLEARCORE4");
  392. Assert.AreEqual (0, ((ICollection) p).Count, "CLEARCORE5");
  393. Assert.AreEqual (p.collection_changed, (int) CollectionChangeAction.Refresh, "CLEARCORE6");
  394. }
  395. [Test]
  396. public void TestContains ()
  397. {
  398. BindingContextPoker p = new BindingContextPoker ();
  399. object data_source = new object ();
  400. p._Add (data_source, new PropertyManager ());
  401. Assert.IsTrue (p.Contains (data_source), "#1");
  402. Assert.IsFalse (p.Contains ("nonexistent"), "#2");
  403. }
  404. [Test]
  405. [ExpectedException (typeof (ArgumentNullException))]
  406. public void TestContainsNull ()
  407. {
  408. BindingContextPoker p = new BindingContextPoker ();
  409. p.Contains (null);
  410. }
  411. [Test]
  412. public void TestContainsNull2 ()
  413. {
  414. BindingContextPoker p = new BindingContextPoker ();
  415. object data_source = new object ();
  416. p._Add (data_source, new PropertyManager ());
  417. Assert.IsTrue (p.Contains (data_source, null), "#1");
  418. Assert.IsFalse (p.Contains ("nonexistent", null), "#2");
  419. }
  420. [Test]
  421. public void TestGetEnumerator ()
  422. {
  423. BindingContextPoker p = new BindingContextPoker ();
  424. object data_source = new object ();
  425. PropertyManager pm = new PropertyManager ();
  426. p._Add (data_source, pm);
  427. IEnumerator e = ((IEnumerable) p).GetEnumerator ();
  428. Assert.IsNotNull (e, "#1");
  429. IDictionaryEnumerator de = e as IDictionaryEnumerator;
  430. Assert.IsNotNull (de, "#2");
  431. Assert.IsTrue (de.MoveNext (), "#3");
  432. // In .NET Key is its internal type.
  433. //Assert.AreEqual (data_source, de.Key, "#4");
  434. //Assert.AreEqual (pm, de.Value, "#5");
  435. Assert.IsFalse (de.MoveNext (), "#6");
  436. }
  437. }
  438. }