BindingSourceTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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) 2007 Novell, Inc.
  21. //
  22. #if NET_2_0
  23. using System;
  24. using System.Data;
  25. using System.Collections;
  26. using System.Collections.Generic;
  27. using System.ComponentModel;
  28. using System.Windows.Forms;
  29. using NUnit.Framework;
  30. namespace MonoTests.System.Windows.Forms {
  31. [TestFixture]
  32. public class BindingSourceTest
  33. {
  34. [Test]
  35. public void DefaultDataSource ()
  36. {
  37. BindingSource source = new BindingSource ();
  38. Assert.IsTrue (source.List is BindingList<object>, "1");
  39. Assert.AreEqual (0, source.List.Count, "2");
  40. }
  41. [Test]
  42. public void DataSource_InitialAddChangingType ()
  43. {
  44. BindingSource source = new BindingSource ();
  45. source.Add ((int)32);
  46. Assert.IsTrue (source.List is BindingList<int>, "1");
  47. }
  48. class EmptyEnumerable : IEnumerable {
  49. class EmptyEnumerator : IEnumerator {
  50. public object Current {
  51. get { throw new InvalidOperationException (); }
  52. }
  53. public void Reset () {
  54. // nada
  55. }
  56. public bool MoveNext () {
  57. return false;
  58. }
  59. }
  60. public IEnumerator GetEnumerator () {
  61. return new EmptyEnumerator ();
  62. }
  63. }
  64. class InfiniteEnumerable : IEnumerable {
  65. class InfiniteEnumerator : IEnumerator {
  66. public object Current {
  67. get { return 5; }
  68. }
  69. public void Reset () {
  70. // nada
  71. }
  72. public bool MoveNext () {
  73. return true;
  74. }
  75. }
  76. public IEnumerator GetEnumerator () {
  77. return new InfiniteEnumerator ();
  78. }
  79. }
  80. class WorkingEnumerable : IEnumerable {
  81. int length;
  82. public WorkingEnumerable (int length) {
  83. this.length = length;
  84. }
  85. class MyEnumerator : IEnumerator {
  86. public int count;
  87. public int index;
  88. public object Current {
  89. get { return index; }
  90. }
  91. public void Reset () {
  92. index = 0;
  93. }
  94. public bool MoveNext () {
  95. if (index++ == count)
  96. return false;
  97. else
  98. return true;
  99. }
  100. }
  101. public IEnumerator GetEnumerator () {
  102. MyEnumerator e = new MyEnumerator ();
  103. e.count = length;
  104. return e;
  105. }
  106. }
  107. [Test]
  108. public void DataSource_ListRelationship ()
  109. {
  110. BindingSource source = new BindingSource ();
  111. // null
  112. source.DataSource = null;
  113. Assert.IsTrue (source.List is BindingList<object>, "1");
  114. // a non-list object
  115. source.DataSource = new object ();
  116. Assert.IsTrue (source.List is BindingList<object>, "2");
  117. // array instance (value type)
  118. source.DataSource = new int[32];
  119. Assert.IsTrue (source.List is int[], "3");
  120. // an instance array with 0 elements
  121. source.DataSource = new int[0];
  122. Assert.IsTrue (source.List is int[], "4");
  123. // array instance (object type)
  124. source.DataSource = new string[32];
  125. Assert.IsTrue (source.List is string[], "5");
  126. // list type
  127. source.DataSource = new List<bool>();
  128. Assert.IsTrue (source.List is List<bool>, "6");
  129. // an IEnumerable type
  130. source.DataSource = "hi";
  131. Assert.IsTrue (source.List is BindingList<char>, "7");
  132. // an IEnumerable type with 0 items
  133. source.DataSource = "";
  134. Assert.IsTrue (source.List is BindingList<char>, "8");
  135. Assert.AreEqual (0, source.List.Count, "9");
  136. // a non-string IEnumerable type with 0 items
  137. // this doesn't seem to change the type of the
  138. // binding source's list, probably because it
  139. // can't determine the type of the
  140. // enumerable's elements.
  141. source.DataSource = new EmptyEnumerable ();
  142. Assert.IsTrue (source.List is BindingList<char>, "10");
  143. // an enumerable with some elements
  144. source.DataSource = new WorkingEnumerable (5);
  145. Assert.IsTrue (source.List is BindingList<int>, "11");
  146. Assert.AreEqual (5, source.List.Count, "12");
  147. }
  148. #if false
  149. [Test]
  150. [Ignore ("BindingSource seems to try to get all elements of the enumerator, probably copying them to a propertly typed BindingList")]
  151. public void InfiniteEnumeratorTest ()
  152. {
  153. BindingSource source = new BindingSource ();
  154. // an infinite IEnumerable type
  155. source.DataSource = new InfiniteEnumerable ();
  156. Console.WriteLine (source.List.GetType());
  157. Assert.IsTrue (source.List is BindingList<int>, "10");
  158. }
  159. #endif
  160. [Test]
  161. public void DataMember_ListRelationship ()
  162. {
  163. }
  164. [Test]
  165. public void DataMemberNull ()
  166. {
  167. BindingSource source = new BindingSource ();
  168. Assert.AreEqual ("", source.DataMember, "1");
  169. source.DataMember = null;
  170. Assert.AreEqual ("", source.DataMember, "2");
  171. }
  172. [Test]
  173. public void DataSourceChanged ()
  174. {
  175. ArrayList list = new ArrayList ();
  176. BindingSource source = new BindingSource ();
  177. bool event_raised = false;
  178. source.DataSourceChanged += delegate (object sender, EventArgs e) {
  179. event_raised = true;
  180. };
  181. source.DataSource = list;
  182. Assert.IsTrue (event_raised, "1");
  183. event_raised = false;
  184. source.DataSource = list;
  185. Assert.IsFalse (event_raised, "2");
  186. }
  187. [Test]
  188. [ExpectedException (typeof (ArgumentException))] // DataMember property 'hi' cannot be found on the DataSource.
  189. public void DataMemberArgumentException ()
  190. {
  191. ArrayList list = new ArrayList ();
  192. BindingSource source = new BindingSource ();
  193. source.DataSource = list;
  194. source.DataMember = "hi";
  195. }
  196. [Test]
  197. public void DataMemberBeforeDataSource ()
  198. {
  199. ArrayList list = new ArrayList ();
  200. BindingSource source = new BindingSource ();
  201. source.DataMember = "hi";
  202. Assert.AreEqual ("hi", source.DataMember, "1");
  203. source.DataSource = list;
  204. Assert.AreEqual ("", source.DataMember, "2");
  205. }
  206. [Test]
  207. public void DataSourceAssignToDefaultType()
  208. {
  209. BindingSource source = new BindingSource ();
  210. source.DataMember = "hi";
  211. Assert.AreEqual ("hi", source.DataMember, "1");
  212. Assert.IsTrue (source.List is BindingList<object>, "2");
  213. source.DataSource = new BindingList<object>();
  214. Assert.AreEqual ("", source.DataMember, "3");
  215. }
  216. [Test]
  217. public void DataMemberChanged ()
  218. {
  219. ArrayList list = new ArrayList ();
  220. BindingSource source = new BindingSource ();
  221. bool event_raised = false;
  222. list.Add ("hi"); // make the type System.String
  223. source.DataMemberChanged += delegate (object sender, EventArgs e) {
  224. event_raised = true;
  225. };
  226. source.DataSource = list;
  227. source.DataMember = "Length";
  228. Assert.IsTrue (event_raised, "1");
  229. event_raised = false;
  230. source.DataMember = "Length";
  231. Assert.IsFalse (event_raised, "2");
  232. }
  233. [Test]
  234. public void SuppliedDataSource ()
  235. {
  236. List<string> list = new List<string>();
  237. BindingSource source;
  238. source = new BindingSource (list, "");
  239. Assert.IsTrue (source.List is List<string>, "1");
  240. source.DataMember = "Length";
  241. Assert.IsTrue (source.List is BindingList<int>, "2");
  242. source = new BindingSource (list, "Length");
  243. Assert.IsTrue (source.List is BindingList<int>, "3");
  244. }
  245. [Test]
  246. public void DataSourceMember_set ()
  247. {
  248. BindingSource source = new BindingSource ();
  249. source.DataSource = new List<string>();
  250. source.DataMember = "Length";
  251. Assert.IsNotNull (source.CurrencyManager, "1");
  252. source.DataSource = new List<string>();
  253. Assert.AreEqual ("Length", source.DataMember, "2");
  254. Assert.IsNotNull (source.CurrencyManager, "3");
  255. source.DataSource = new List<string>();
  256. Assert.AreEqual ("Length", source.DataMember, "4");
  257. Assert.IsNotNull (source.CurrencyManager, "5");
  258. }
  259. [Test]
  260. public void DataSourceMemberChangedEvents ()
  261. {
  262. BindingSource source = new BindingSource ();
  263. bool data_source_changed = false;
  264. bool data_member_changed = false;
  265. source.DataSourceChanged += delegate (object sender, EventArgs e) {
  266. data_source_changed = true;
  267. };
  268. source.DataMemberChanged += delegate (object sender, EventArgs e) {
  269. data_member_changed = true;
  270. };
  271. data_source_changed = false;
  272. data_member_changed = false;
  273. source.DataSource = new List<string>();
  274. Assert.IsTrue (data_source_changed, "1");
  275. Assert.IsFalse (data_member_changed, "2");
  276. data_source_changed = false;
  277. data_member_changed = false;
  278. source.DataMember = "Length";
  279. Assert.IsFalse (data_source_changed, "3");
  280. Assert.IsTrue (data_member_changed, "4");
  281. }
  282. [Test]
  283. public void AddNew ()
  284. {
  285. BindingSource source = new BindingSource ();
  286. source.AddNew ();
  287. Assert.AreEqual (1, source.Count, "1");
  288. }
  289. [Test]
  290. public void AddNew_NonBindingList ()
  291. {
  292. IList list = new List<object> ();
  293. BindingSource source = new BindingSource ();
  294. source.DataSource = list;
  295. Assert.IsTrue (source.List is List<object>, "1");
  296. source.AddNew ();
  297. Assert.AreEqual (1, source.Count, "2");
  298. }
  299. [Test]
  300. public void AllowNew_getter ()
  301. {
  302. BindingSource source = new BindingSource ();
  303. // true because the default datasource is BindingList<object>
  304. Assert.IsTrue (source.AllowNew, "1");
  305. source.DataSource = new object[10];
  306. // fixed size
  307. Assert.IsFalse (source.AllowNew, "2");
  308. source.DataSource = new BindingList<string>();
  309. // no default ctor
  310. Assert.IsFalse (source.AllowNew, "3");
  311. }
  312. [Test]
  313. public void AllowNew ()
  314. {
  315. BindingSource source = new BindingSource ();
  316. source.AllowNew = false;
  317. Assert.IsFalse (source.AllowNew, "1");
  318. source.ResetAllowNew ();
  319. Assert.IsTrue (source.AllowNew, "2");
  320. }
  321. [Test]
  322. [ExpectedException (typeof (InvalidOperationException))]
  323. // "AllowNew can only be set to true on an
  324. // IBindingList or on a read-write list with a default
  325. // public constructor."
  326. public void AllowNew_FixedSize ()
  327. {
  328. BindingSource source = new BindingSource ();
  329. source.DataSource = new object[10];
  330. source.AllowNew = true;
  331. }
  332. #if false
  333. // According to the MS docs, this should fail with a MissingMethodException
  334. [Test]
  335. public void AllowNew_NoDefaultCtor ()
  336. {
  337. List<string> s = new List<string>();
  338. s.Add ("hi");
  339. BindingSource source = new BindingSource ();
  340. source.DataSource = s;
  341. source.AllowNew = true;
  342. Assert.Fail ();
  343. }
  344. #endif
  345. [Test]
  346. [ExpectedException (typeof (InvalidOperationException))]
  347. // "Item cannot be added to a read-only or fixed-size list."
  348. public void AddNew_BindingListWithoutAllowNew ()
  349. {
  350. BindingList<int> l = new BindingList<int>();
  351. l.AllowNew = false;
  352. BindingSource source = new BindingSource ();
  353. source.DataSource = l;
  354. source.AddNew ();
  355. Assert.AreEqual (1, source.Count, "1");
  356. }
  357. [Test]
  358. [ExpectedException (typeof (InvalidOperationException))]
  359. // "Item cannot be added to a read-only or fixed-size list."
  360. public void AddNew_FixedSize ()
  361. {
  362. BindingSource source = new BindingSource ();
  363. source.DataSource = new int[5];
  364. object o = source.AddNew ();
  365. Assert.IsTrue (o is int, "1");
  366. Assert.AreEqual (6, source.Count, "2");
  367. }
  368. class ReadOnlyList : List<int>, IList {
  369. public int Add (object value) {
  370. throw new Exception ();
  371. }
  372. public void Clear () {
  373. throw new Exception ();
  374. }
  375. public bool Contains (object value) {
  376. throw new Exception ();
  377. }
  378. public int IndexOf (object value) {
  379. throw new Exception ();
  380. }
  381. public void Insert (int index, object value) {
  382. throw new Exception ();
  383. }
  384. public void Remove (object value) {
  385. throw new Exception ();
  386. }
  387. public void RemoveAt (int index) {
  388. throw new Exception ();
  389. }
  390. public bool IsFixedSize {
  391. get { return false; }
  392. }
  393. public bool IsReadOnly {
  394. get { return true; }
  395. }
  396. public object this [int index] {
  397. get { throw new Exception (); }
  398. set { }
  399. }
  400. }
  401. [Test]
  402. [ExpectedException (typeof (InvalidOperationException))]
  403. // "Item cannot be added to a read-only or fixed-size list."
  404. public void AddNew_ReadOnly ()
  405. {
  406. BindingSource source = new BindingSource ();
  407. source.DataSource = new ReadOnlyList ();
  408. object o = source.AddNew ();
  409. }
  410. [Test]
  411. [ExpectedException (typeof (InvalidOperationException))]
  412. // "AddNew cannot be called on the 'System.String' type. This type does not have a public default constructor. You can call AddNew on the 'System.String' type if you set AllowNew=true and handle the AddingNew event."
  413. public void AddNew_Invalid ()
  414. {
  415. BindingSource source = new BindingSource ();
  416. source.DataSource = new List<string>();
  417. object o = source.AddNew ();
  418. }
  419. [Test]
  420. public void BindingSuspended1 ()
  421. {
  422. /* how does this property work? */
  423. BindingSource source = new BindingSource ();
  424. source.DataSource = new List<string>();
  425. Assert.IsTrue (source.IsBindingSuspended, "1");
  426. source.SuspendBinding ();
  427. Assert.IsTrue (source.IsBindingSuspended, "2");
  428. source.ResumeBinding ();
  429. Assert.IsTrue (source.IsBindingSuspended, "3");
  430. source.ResumeBinding ();
  431. Assert.IsTrue (source.IsBindingSuspended, "4");
  432. }
  433. class BindingListViewPoker : BindingList<string>, IBindingListView
  434. {
  435. public bool supports_filter;
  436. public bool supports_advanced_sorting;
  437. public string Filter {
  438. get { return ""; }
  439. set { }
  440. }
  441. public ListSortDescriptionCollection SortDescriptions {
  442. get { return null; }
  443. }
  444. public bool SupportsAdvancedSorting {
  445. get { return supports_advanced_sorting; }
  446. }
  447. public bool SupportsFiltering {
  448. get { return supports_filter; }
  449. }
  450. public void ApplySort (ListSortDescriptionCollection sorts)
  451. {
  452. }
  453. public void RemoveFilter ()
  454. {
  455. }
  456. }
  457. [Test]
  458. public void SupportsFilter ()
  459. {
  460. BindingListViewPoker c = new BindingListViewPoker ();
  461. BindingSource source = new BindingSource ();
  462. // because the default list is a BindingList<object>
  463. Assert.IsFalse (source.SupportsFiltering, "1");
  464. source.DataSource = c;
  465. // the DataSource is IBindingListView, but
  466. // SupportsFilter is false.
  467. Assert.IsFalse (source.SupportsFiltering, "2");
  468. c.supports_filter = true;
  469. Assert.IsTrue (source.SupportsFiltering, "3");
  470. }
  471. [Test]
  472. public void SupportsAdvancedSorting ()
  473. {
  474. BindingListViewPoker c = new BindingListViewPoker ();
  475. BindingSource source = new BindingSource ();
  476. // because the default list is a BindingList<object>
  477. Assert.IsFalse (source.SupportsAdvancedSorting, "1");
  478. source.DataSource = c;
  479. // the DataSource is IBindingListView, but
  480. // SupportsAdvancedSorting is false.
  481. Assert.IsFalse (source.SupportsAdvancedSorting, "2");
  482. c.supports_advanced_sorting = true;
  483. Assert.IsTrue (source.SupportsAdvancedSorting, "3");
  484. }
  485. class IBindingListPoker : BindingList<string>, IBindingList {
  486. public void AddIndex (PropertyDescriptor property)
  487. {
  488. }
  489. public object AddNew ()
  490. {
  491. throw new NotImplementedException ();
  492. }
  493. public void ApplySort (PropertyDescriptor property, ListSortDirection direction)
  494. {
  495. }
  496. public int Find (PropertyDescriptor property, object key)
  497. {
  498. throw new NotImplementedException ();
  499. }
  500. public void RemoveIndex (PropertyDescriptor property)
  501. {
  502. }
  503. public void RemoveSort ()
  504. {
  505. }
  506. public bool AllowEdit {
  507. get { throw new NotImplementedException (); }
  508. }
  509. public bool AllowNew {
  510. get { throw new NotImplementedException (); }
  511. }
  512. public bool AllowRemove {
  513. get { throw new NotImplementedException (); }
  514. }
  515. public bool IsSorted {
  516. get { throw new NotImplementedException (); }
  517. }
  518. public ListSortDirection SortDirection {
  519. get { throw new NotImplementedException (); }
  520. }
  521. public PropertyDescriptor SortProperty {
  522. get { throw new NotImplementedException (); }
  523. }
  524. public bool SupportsChangeNotification {
  525. get { return supports_change_notification; }
  526. }
  527. public bool SupportsSearching {
  528. get { return supports_searching; }
  529. }
  530. public bool SupportsSorting {
  531. get { return supports_sorting; }
  532. }
  533. public event ListChangedEventHandler ListChanged;
  534. public bool supports_change_notification;
  535. public bool supports_searching;
  536. public bool supports_sorting;
  537. }
  538. [Test]
  539. public void SupportsSearching ()
  540. {
  541. IBindingListPoker c = new IBindingListPoker ();
  542. BindingSource source = new BindingSource ();
  543. // because the default list is a BindingList<object>
  544. Assert.IsFalse (source.SupportsSearching, "1");
  545. source.DataSource = c;
  546. // the DataSource is IBindingList, but
  547. // SupportsSearching is false.
  548. Assert.IsFalse (source.SupportsSearching, "2");
  549. c.supports_searching = true;
  550. Console.WriteLine ("set c.supports_searching to {0}, so c.SupportsSearching = {1}",
  551. c.supports_searching, c.SupportsSearching);
  552. Assert.IsTrue (source.SupportsSearching, "3");
  553. }
  554. [Test]
  555. public void SupportsSorting ()
  556. {
  557. IBindingListPoker c = new IBindingListPoker ();
  558. BindingSource source = new BindingSource ();
  559. // because the default list is a BindingList<object>
  560. Assert.IsFalse (source.SupportsSorting, "1");
  561. source.DataSource = c;
  562. // the DataSource is IBindingList, but
  563. // SupportsSorting is false.
  564. Assert.IsFalse (source.SupportsSorting, "2");
  565. c.supports_sorting = true;
  566. Assert.IsTrue (source.SupportsSorting, "3");
  567. }
  568. [Test]
  569. public void SupportsChangeNotification ()
  570. {
  571. IBindingListPoker c = new IBindingListPoker ();
  572. BindingSource source = new BindingSource ();
  573. // because the default list is a BindingList<object>
  574. Assert.IsTrue (source.SupportsChangeNotification, "1");
  575. source.DataSource = c;
  576. // the DataSource is IBindingList, but
  577. // SupportsChangeNotification is false.
  578. Assert.IsTrue (source.SupportsChangeNotification, "2");
  579. c.supports_change_notification = true;
  580. Assert.IsTrue (source.SupportsChangeNotification, "3");
  581. }
  582. [Test]
  583. public void InitializedEvent ()
  584. {
  585. // XXX this test is officially useless. does
  586. // BindingSource even raise the event? it
  587. // seems to always be initialized.
  588. BindingSource source = new BindingSource ();
  589. ISupportInitializeNotification n = (ISupportInitializeNotification)source;
  590. bool event_handled = false;
  591. n.Initialized += delegate (object sender, EventArgs e) {
  592. event_handled = true;
  593. };
  594. Assert.IsTrue (n.IsInitialized, "1");
  595. source.DataSource = "hi";
  596. Assert.IsTrue (n.IsInitialized, "2");
  597. Assert.IsFalse (event_handled, "3");
  598. }
  599. }
  600. }
  601. #endif