BindingSourceTest.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  31. namespace MonoTests.System.Windows.Forms {
  32. [TestFixture]
  33. public class BindingSourceTest
  34. {
  35. [Test]
  36. public void DefaultDataSource ()
  37. {
  38. BindingSource source = new BindingSource ();
  39. Assert.IsTrue (source.List is BindingList<object>, "1");
  40. Assert.AreEqual (0, source.List.Count, "2");
  41. }
  42. [Test]
  43. [Category ("NotWorking")]
  44. public void DataSource_InitialAddChangingType ()
  45. {
  46. BindingSource source = new BindingSource ();
  47. source.Add ((int)32);
  48. Assert.IsTrue (source.List is BindingList<int>, "1");
  49. }
  50. class EmptyEnumerable : IEnumerable {
  51. class EmptyEnumerator : IEnumerator {
  52. public object Current {
  53. get { throw new InvalidOperationException (); }
  54. }
  55. public void Reset () {
  56. // nada
  57. }
  58. public bool MoveNext () {
  59. return false;
  60. }
  61. }
  62. public IEnumerator GetEnumerator () {
  63. return new EmptyEnumerator ();
  64. }
  65. }
  66. class GenericEnumerable : IEnumerable<int> {
  67. int length;
  68. public GenericEnumerable (int length) {
  69. this.length = length;
  70. }
  71. class MyEnumerator : IEnumerator<int> {
  72. public int count;
  73. public int index;
  74. public int Current {
  75. get { return index; }
  76. }
  77. object IEnumerator.Current {
  78. get { return Current; }
  79. }
  80. public void Reset () {
  81. index = 0;
  82. }
  83. public bool MoveNext () {
  84. if (index++ == count)
  85. return false;
  86. else
  87. return true;
  88. }
  89. void IDisposable.Dispose () {
  90. }
  91. }
  92. public IEnumerator<int> GetEnumerator () {
  93. MyEnumerator e = new MyEnumerator ();
  94. e.count = length;
  95. return e;
  96. }
  97. IEnumerator IEnumerable.GetEnumerator () {
  98. return GetEnumerator ();
  99. }
  100. }
  101. class WorkingEnumerable : IEnumerable {
  102. int length;
  103. public WorkingEnumerable (int length) {
  104. this.length = length;
  105. }
  106. class MyEnumerator : IEnumerator {
  107. public int count;
  108. public int index;
  109. public object Current {
  110. get { return index; }
  111. }
  112. public void Reset () {
  113. index = 0;
  114. }
  115. public bool MoveNext () {
  116. if (index++ == count)
  117. return false;
  118. else
  119. return true;
  120. }
  121. }
  122. public IEnumerator GetEnumerator () {
  123. MyEnumerator e = new MyEnumerator ();
  124. e.count = length;
  125. return e;
  126. }
  127. }
  128. [Test]
  129. public void DataSource_ListRelationship ()
  130. {
  131. BindingSource source = new BindingSource ();
  132. // null
  133. source.DataSource = null;
  134. Assert.IsTrue (source.List is BindingList<object>, "1");
  135. // a non-list object
  136. source.DataSource = new object ();
  137. Assert.IsTrue (source.List is BindingList<object>, "2");
  138. // array instance (value type)
  139. source.DataSource = new int[32];
  140. Assert.IsTrue (source.List is int[], "3");
  141. // an instance array with 0 elements
  142. source.DataSource = new int[0];
  143. Assert.IsTrue (source.List is int[], "4");
  144. // array instance (object type)
  145. source.DataSource = new string[32];
  146. Assert.IsTrue (source.List is string[], "5");
  147. // list type
  148. source.DataSource = new List<bool>();
  149. Assert.IsTrue (source.List is List<bool>, "6");
  150. // an IEnumerable type
  151. source.DataSource = "hi";
  152. Assert.IsTrue (source.List is BindingList<char>, "7");
  153. // an IEnumerable type with 0 items
  154. source.DataSource = "";
  155. Assert.IsTrue (source.List is BindingList<char>, "8");
  156. Assert.AreEqual (0, source.List.Count, "9");
  157. // a generic enumerable with no elements.
  158. // even though we can figure out the type
  159. // through reflection, we shouldn't..
  160. source.DataSource = new GenericEnumerable (0);
  161. Console.WriteLine (source.List.GetType());
  162. Assert.IsTrue (source.List is BindingList<char>, "10");
  163. Assert.AreEqual (0, source.List.Count, "11");
  164. // a non-generic IEnumerable type with 0 items
  165. // this doesn't seem to change the type of the
  166. // binding source's list, probably because it
  167. // can't determine the type of the
  168. // enumerable's elements.
  169. source.DataSource = new EmptyEnumerable ();
  170. Assert.IsTrue (source.List is BindingList<char>, "12");
  171. // an enumerable with some elements
  172. source.DataSource = new WorkingEnumerable (5);
  173. Assert.IsTrue (source.List is BindingList<int>, "13");
  174. Assert.AreEqual (5, source.List.Count, "14");
  175. }
  176. [Test]
  177. public void ResetItem ()
  178. {
  179. BindingSource source = new BindingSource ();
  180. bool delegate_reached = false;
  181. int old_index = 5;
  182. int new_index = 5;
  183. ListChangedType type = ListChangedType.Reset;
  184. source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
  185. delegate_reached = true;
  186. type = e.ListChangedType;
  187. old_index = e.OldIndex;
  188. new_index = e.NewIndex;
  189. };
  190. source.ResetItem (0);
  191. Assert.IsTrue (delegate_reached, "1");
  192. Assert.AreEqual (-1, old_index, "2");
  193. Assert.AreEqual (0, new_index, "3");
  194. Assert.AreEqual (ListChangedType.ItemChanged, type, "3");
  195. }
  196. [Test]
  197. public void Movement ()
  198. {
  199. BindingSource source = new BindingSource ();
  200. source.DataSource = new WorkingEnumerable (5);
  201. int raised = 0;
  202. source.PositionChanged += delegate (object sender, EventArgs e) { raised ++; };
  203. Console.WriteLine ("count = {0}", source.Count);
  204. source.Position = 3;
  205. Assert.AreEqual (3, source.Position, "1");
  206. source.MoveFirst ();
  207. Assert.AreEqual (0, source.Position, "2");
  208. source.MoveNext ();
  209. Assert.AreEqual (1, source.Position, "3");
  210. source.MovePrevious ();
  211. Assert.AreEqual (0, source.Position, "4");
  212. source.MoveLast ();
  213. Assert.AreEqual (4, source.Position, "5");
  214. Assert.AreEqual (5, raised, "6");
  215. }
  216. [Test]
  217. public void Position ()
  218. {
  219. BindingSource source = new BindingSource ();
  220. Assert.AreEqual (-1, source.Position, "0");
  221. source.DataSource = new WorkingEnumerable (5);
  222. int raised = 0;
  223. source.PositionChanged += delegate (object sender, EventArgs e) { raised ++; };
  224. Assert.AreEqual (0, source.Position, "1");
  225. source.Position = -1;
  226. Assert.AreEqual (0, source.Position, "2");
  227. Assert.AreEqual (0, raised, "3");
  228. source.Position = 10;
  229. Assert.AreEqual (4, source.Position, "4");
  230. Assert.AreEqual (1, raised, "5");
  231. source.Position = 10;
  232. Assert.AreEqual (4, source.Position, "6");
  233. Assert.AreEqual (1, raised, "7");
  234. }
  235. [Test]
  236. public void ResetCurrentItem ()
  237. {
  238. BindingSource source = new BindingSource ();
  239. bool delegate_reached = false;
  240. int old_index = 5;
  241. int new_index = 5;
  242. ListChangedType type = ListChangedType.Reset;
  243. source.DataSource = new WorkingEnumerable (5);
  244. source.Position = 2;
  245. source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
  246. delegate_reached = true;
  247. type = e.ListChangedType;
  248. old_index = e.OldIndex;
  249. new_index = e.NewIndex;
  250. };
  251. source.ResetCurrentItem ();
  252. Assert.IsTrue (delegate_reached, "1");
  253. Assert.AreEqual (-1, old_index, "2");
  254. Assert.AreEqual (2, new_index, "3");
  255. Assert.AreEqual (ListChangedType.ItemChanged, type, "3");
  256. }
  257. [Test]
  258. public void ResetBindings ()
  259. {
  260. BindingSource source;
  261. int event_count = 0;
  262. ListChangedType[] types = new ListChangedType[2];
  263. int[] old_index = new int[2];
  264. int[] new_index = new int[2];
  265. source = new BindingSource ();
  266. source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
  267. types[event_count] = e.ListChangedType;
  268. old_index[event_count] = e.OldIndex;
  269. new_index[event_count] = e.NewIndex;
  270. event_count ++;
  271. };
  272. event_count = 0;
  273. source.ResetBindings (false);
  274. Assert.AreEqual (1, event_count, "1");
  275. Assert.AreEqual (ListChangedType.Reset, types[0], "2");
  276. Assert.AreEqual (-1, old_index[0], "3");
  277. Assert.AreEqual (-1, new_index[0], "4");
  278. event_count = 0;
  279. source.ResetBindings (true);
  280. Assert.AreEqual (2, event_count, "5");
  281. Assert.AreEqual (ListChangedType.PropertyDescriptorChanged, types[0], "6");
  282. Assert.AreEqual (0, old_index[0], "7");
  283. Assert.AreEqual (0, new_index[0], "8");
  284. Assert.AreEqual (ListChangedType.Reset, types[1], "9");
  285. Assert.AreEqual (-1, old_index[1], "10");
  286. Assert.AreEqual (-1, new_index[1], "11");
  287. }
  288. [Test]
  289. public void AllowEdit ()
  290. {
  291. BindingSource source = new BindingSource ();
  292. Assert.IsTrue (source.AllowEdit, "1");
  293. source.DataSource = "";
  294. Assert.IsTrue (source.AllowEdit, "2");
  295. source.DataSource = new int[10];
  296. Assert.IsTrue (source.AllowEdit, "3");
  297. source.DataSource = new WorkingEnumerable (5);
  298. Assert.IsTrue (source.AllowEdit, "4");
  299. ArrayList al = new ArrayList ();
  300. al.Add (5);
  301. source.DataSource = al;
  302. Assert.IsTrue (source.AllowEdit, "5");
  303. source.DataSource = ArrayList.ReadOnly (al);
  304. Assert.IsFalse (source.AllowEdit, "6");
  305. }
  306. [Test]
  307. public void AllowRemove ()
  308. {
  309. BindingSource source = new BindingSource ();
  310. Assert.IsTrue (source.AllowRemove, "1");
  311. source.DataSource = "";
  312. Assert.IsTrue (source.AllowRemove, "2");
  313. source.DataSource = new ArrayList ();
  314. Assert.IsTrue (source.AllowRemove, "3");
  315. source.DataSource = new int[10];
  316. Assert.IsFalse (source.AllowRemove, "4");
  317. source.DataSource = new WorkingEnumerable (5);
  318. Assert.IsTrue (source.AllowRemove, "5");
  319. }
  320. [Test]
  321. public void DataMember_ListRelationship ()
  322. {
  323. }
  324. [Test]
  325. public void DataMemberNull ()
  326. {
  327. BindingSource source = new BindingSource ();
  328. Assert.AreEqual ("", source.DataMember, "1");
  329. source.DataMember = null;
  330. Assert.AreEqual ("", source.DataMember, "2");
  331. }
  332. [Test]
  333. public void DataSourceChanged ()
  334. {
  335. ArrayList list = new ArrayList ();
  336. BindingSource source = new BindingSource ();
  337. bool event_raised = false;
  338. source.DataSourceChanged += delegate (object sender, EventArgs e) {
  339. event_raised = true;
  340. };
  341. source.DataSource = list;
  342. Assert.IsTrue (event_raised, "1");
  343. event_raised = false;
  344. source.DataSource = list;
  345. Assert.IsFalse (event_raised, "2");
  346. }
  347. [Test]
  348. [ExpectedException (typeof (ArgumentException))] // DataMember property 'hi' cannot be found on the DataSource.
  349. [Category ("NotWorking")]
  350. public void DataMemberArgumentException ()
  351. {
  352. ArrayList list = new ArrayList ();
  353. BindingSource source = new BindingSource ();
  354. source.DataSource = list;
  355. source.DataMember = "hi";
  356. }
  357. [Test]
  358. public void DataMemberBeforeDataSource ()
  359. {
  360. ArrayList list = new ArrayList ();
  361. BindingSource source = new BindingSource ();
  362. source.DataMember = "hi";
  363. Assert.AreEqual ("hi", source.DataMember, "1");
  364. source.DataSource = list;
  365. Assert.AreEqual ("", source.DataMember, "2");
  366. }
  367. [Test]
  368. public void DataSourceAssignToDefaultType()
  369. {
  370. BindingSource source = new BindingSource ();
  371. source.DataMember = "hi";
  372. Assert.AreEqual ("hi", source.DataMember, "1");
  373. Assert.IsTrue (source.List is BindingList<object>, "2");
  374. source.DataSource = new BindingList<object>();
  375. Assert.AreEqual ("", source.DataMember, "3");
  376. }
  377. [Test]
  378. public void DataMemberChanged ()
  379. {
  380. ArrayList list = new ArrayList ();
  381. BindingSource source = new BindingSource ();
  382. bool event_raised = false;
  383. list.Add ("hi"); // make the type System.String
  384. source.DataMemberChanged += delegate (object sender, EventArgs e) {
  385. event_raised = true;
  386. };
  387. source.DataSource = list;
  388. source.DataMember = "Length";
  389. Assert.IsTrue (event_raised, "1");
  390. event_raised = false;
  391. source.DataMember = "Length";
  392. Assert.IsFalse (event_raised, "2");
  393. }
  394. [Test]
  395. [Category ("NotWorking")]
  396. public void SuppliedDataSource ()
  397. {
  398. List<string> list = new List<string>();
  399. BindingSource source;
  400. source = new BindingSource (list, "");
  401. Assert.IsTrue (source.List is List<string>, "1");
  402. source.DataMember = "Length";
  403. Assert.IsTrue (source.List is BindingList<int>, "2");
  404. source = new BindingSource (list, "Length");
  405. Assert.IsTrue (source.List is BindingList<int>, "3");
  406. }
  407. [Test]
  408. [Category ("NotWorking")]
  409. public void DataSourceMember_set ()
  410. {
  411. BindingSource source = new BindingSource ();
  412. source.DataSource = new List<string>();
  413. source.DataMember = "Length";
  414. Assert.IsNotNull (source.CurrencyManager, "1");
  415. source.DataSource = new List<string>();
  416. Assert.AreEqual ("Length", source.DataMember, "2");
  417. Assert.IsNotNull (source.CurrencyManager, "3");
  418. source.DataSource = new List<string>();
  419. Assert.AreEqual ("Length", source.DataMember, "4");
  420. Assert.IsNotNull (source.CurrencyManager, "5");
  421. }
  422. [Test]
  423. public void DataSourceMemberChangedEvents ()
  424. {
  425. BindingSource source = new BindingSource ();
  426. bool data_source_changed = false;
  427. bool data_member_changed = false;
  428. source.DataSourceChanged += delegate (object sender, EventArgs e) {
  429. data_source_changed = true;
  430. };
  431. source.DataMemberChanged += delegate (object sender, EventArgs e) {
  432. data_member_changed = true;
  433. };
  434. data_source_changed = false;
  435. data_member_changed = false;
  436. source.DataSource = new List<string>();
  437. Assert.IsTrue (data_source_changed, "1");
  438. Assert.IsFalse (data_member_changed, "2");
  439. data_source_changed = false;
  440. data_member_changed = false;
  441. source.DataMember = "Length";
  442. Assert.IsFalse (data_source_changed, "3");
  443. Assert.IsTrue (data_member_changed, "4");
  444. }
  445. [Test]
  446. [Category ("NotWorking")]
  447. public void AddNew ()
  448. {
  449. BindingSource source = new BindingSource ();
  450. source.AddNew ();
  451. Assert.AreEqual (1, source.Count, "1");
  452. }
  453. [Test]
  454. [Category ("NotWorking")]
  455. public void AddNew_NonBindingList ()
  456. {
  457. IList list = new List<object> ();
  458. BindingSource source = new BindingSource ();
  459. source.DataSource = list;
  460. Assert.IsTrue (source.List is List<object>, "1");
  461. source.AddNew ();
  462. Assert.AreEqual (1, source.Count, "2");
  463. }
  464. [Test]
  465. public void AllowNew_getter ()
  466. {
  467. BindingSource source = new BindingSource ();
  468. // true because the default datasource is BindingList<object>
  469. Assert.IsTrue (source.AllowNew, "1");
  470. source.DataSource = new object[10];
  471. // fixed size
  472. Assert.IsFalse (source.AllowNew, "2");
  473. source.DataSource = new BindingList<string>();
  474. // no default ctor
  475. Assert.IsFalse (source.AllowNew, "3");
  476. }
  477. [Test]
  478. [Category ("NotWorking")]
  479. public void AllowNew ()
  480. {
  481. BindingSource source = new BindingSource ();
  482. source.AllowNew = false;
  483. Assert.IsFalse (source.AllowNew, "1");
  484. source.ResetAllowNew ();
  485. Assert.IsTrue (source.AllowNew, "2");
  486. }
  487. [Test]
  488. [ExpectedException (typeof (InvalidOperationException))]
  489. [Category ("NotWorking")]
  490. // "AllowNew can only be set to true on an
  491. // IBindingList or on a read-write list with a default
  492. // public constructor."
  493. public void AllowNew_FixedSize ()
  494. {
  495. BindingSource source = new BindingSource ();
  496. source.DataSource = new object[10];
  497. source.AllowNew = true;
  498. }
  499. #if false
  500. // According to the MS docs, this should fail with a MissingMethodException
  501. [Test]
  502. public void AllowNew_NoDefaultCtor ()
  503. {
  504. List<string> s = new List<string>();
  505. s.Add ("hi");
  506. BindingSource source = new BindingSource ();
  507. source.DataSource = s;
  508. source.AllowNew = true;
  509. Assert.Fail ();
  510. }
  511. #endif
  512. [Test]
  513. [ExpectedException (typeof (InvalidOperationException))]
  514. // "Item cannot be added to a read-only or fixed-size list."
  515. public void AddNew_BindingListWithoutAllowNew ()
  516. {
  517. BindingList<int> l = new BindingList<int>();
  518. l.AllowNew = false;
  519. BindingSource source = new BindingSource ();
  520. source.DataSource = l;
  521. source.AddNew ();
  522. Assert.AreEqual (1, source.Count, "1");
  523. }
  524. [Test]
  525. [ExpectedException (typeof (InvalidOperationException))]
  526. // "Item cannot be added to a read-only or fixed-size list."
  527. public void AddNew_FixedSize ()
  528. {
  529. BindingSource source = new BindingSource ();
  530. source.DataSource = new int[5];
  531. object o = source.AddNew ();
  532. Assert.IsTrue (o is int, "1");
  533. Assert.AreEqual (6, source.Count, "2");
  534. }
  535. class ReadOnlyList : List<int>, IList {
  536. public int Add (object value) {
  537. throw new Exception ();
  538. }
  539. public bool Contains (object value) {
  540. throw new Exception ();
  541. }
  542. public int IndexOf (object value) {
  543. throw new Exception ();
  544. }
  545. public void Insert (int index, object value) {
  546. throw new Exception ();
  547. }
  548. public void Remove (object value) {
  549. throw new Exception ();
  550. }
  551. public bool IsFixedSize {
  552. get { return false; }
  553. }
  554. public bool IsReadOnly {
  555. get { return true; }
  556. }
  557. }
  558. [Test]
  559. [ExpectedException (typeof (InvalidOperationException))]
  560. // "Item cannot be added to a read-only or fixed-size list."
  561. public void AddNew_ReadOnly ()
  562. {
  563. BindingSource source = new BindingSource ();
  564. source.DataSource = new ReadOnlyList ();
  565. object o = source.AddNew ();
  566. }
  567. [Test]
  568. [ExpectedException (typeof (InvalidOperationException))]
  569. [Category ("NotWorking")]
  570. // "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."
  571. public void AddNew_Invalid ()
  572. {
  573. BindingSource source = new BindingSource ();
  574. source.DataSource = new List<string>();
  575. object o = source.AddNew ();
  576. }
  577. [Test]
  578. [Category ("NotWorking")]
  579. public void BindingSuspended1 ()
  580. {
  581. /* how does this property work? */
  582. BindingSource source = new BindingSource ();
  583. source.DataSource = new List<string>();
  584. Assert.IsTrue (source.IsBindingSuspended, "1");
  585. source.SuspendBinding ();
  586. Assert.IsTrue (source.IsBindingSuspended, "2");
  587. source.ResumeBinding ();
  588. Assert.IsTrue (source.IsBindingSuspended, "3");
  589. source.ResumeBinding ();
  590. Assert.IsTrue (source.IsBindingSuspended, "4");
  591. }
  592. class BindingListViewPoker : BindingList<string>, IBindingListView
  593. {
  594. public bool supports_filter;
  595. public bool supports_advanced_sorting;
  596. public string Filter {
  597. get { return ""; }
  598. set { }
  599. }
  600. public ListSortDescriptionCollection SortDescriptions {
  601. get { return null; }
  602. }
  603. public bool SupportsAdvancedSorting {
  604. get { return supports_advanced_sorting; }
  605. }
  606. public bool SupportsFiltering {
  607. get { return supports_filter; }
  608. }
  609. public void ApplySort (ListSortDescriptionCollection sorts)
  610. {
  611. }
  612. public void RemoveFilter ()
  613. {
  614. }
  615. }
  616. [Test]
  617. public void SupportsFilter ()
  618. {
  619. BindingListViewPoker c = new BindingListViewPoker ();
  620. BindingSource source = new BindingSource ();
  621. // because the default list is a BindingList<object>
  622. Assert.IsFalse (source.SupportsFiltering, "1");
  623. source.DataSource = c;
  624. // the DataSource is IBindingListView, but
  625. // SupportsFilter is false.
  626. Assert.IsFalse (source.SupportsFiltering, "2");
  627. c.supports_filter = true;
  628. Assert.IsTrue (source.SupportsFiltering, "3");
  629. }
  630. [Test]
  631. public void SupportsAdvancedSorting ()
  632. {
  633. BindingListViewPoker c = new BindingListViewPoker ();
  634. BindingSource source = new BindingSource ();
  635. // because the default list is a BindingList<object>
  636. Assert.IsFalse (source.SupportsAdvancedSorting, "1");
  637. source.DataSource = c;
  638. // the DataSource is IBindingListView, but
  639. // SupportsAdvancedSorting is false.
  640. Assert.IsFalse (source.SupportsAdvancedSorting, "2");
  641. c.supports_advanced_sorting = true;
  642. Assert.IsTrue (source.SupportsAdvancedSorting, "3");
  643. }
  644. class IBindingListPoker : BindingList<string>, IBindingList {
  645. public void AddIndex (PropertyDescriptor property)
  646. {
  647. }
  648. public void ApplySort (PropertyDescriptor property, ListSortDirection direction)
  649. {
  650. }
  651. public int Find (PropertyDescriptor property, object key)
  652. {
  653. throw new NotImplementedException ();
  654. }
  655. public void RemoveIndex (PropertyDescriptor property)
  656. {
  657. }
  658. public void RemoveSort ()
  659. {
  660. }
  661. public bool IsSorted {
  662. get { throw new NotImplementedException (); }
  663. }
  664. public ListSortDirection SortDirection {
  665. get { throw new NotImplementedException (); }
  666. }
  667. public PropertyDescriptor SortProperty {
  668. get { throw new NotImplementedException (); }
  669. }
  670. public bool SupportsChangeNotification {
  671. get { return supports_change_notification; }
  672. }
  673. public bool SupportsSearching {
  674. get { return supports_searching; }
  675. }
  676. public bool SupportsSorting {
  677. get { return supports_sorting; }
  678. }
  679. public bool supports_change_notification;
  680. public bool supports_searching;
  681. public bool supports_sorting;
  682. }
  683. [Test]
  684. public void SupportsSearching ()
  685. {
  686. IBindingListPoker c = new IBindingListPoker ();
  687. BindingSource source = new BindingSource ();
  688. // because the default list is a BindingList<object>
  689. Assert.IsFalse (source.SupportsSearching, "1");
  690. source.DataSource = c;
  691. // the DataSource is IBindingList, but
  692. // SupportsSearching is false.
  693. Assert.IsFalse (source.SupportsSearching, "2");
  694. c.supports_searching = true;
  695. Console.WriteLine ("set c.supports_searching to {0}, so c.SupportsSearching = {1}",
  696. c.supports_searching, c.SupportsSearching);
  697. Assert.IsTrue (source.SupportsSearching, "3");
  698. }
  699. [Test]
  700. public void SupportsSorting ()
  701. {
  702. IBindingListPoker c = new IBindingListPoker ();
  703. BindingSource source = new BindingSource ();
  704. // because the default list is a BindingList<object>
  705. Assert.IsFalse (source.SupportsSorting, "1");
  706. source.DataSource = c;
  707. // the DataSource is IBindingList, but
  708. // SupportsSorting is false.
  709. Assert.IsFalse (source.SupportsSorting, "2");
  710. c.supports_sorting = true;
  711. Assert.IsTrue (source.SupportsSorting, "3");
  712. }
  713. [Test]
  714. public void SupportsChangeNotification ()
  715. {
  716. IBindingListPoker c = new IBindingListPoker ();
  717. BindingSource source = new BindingSource ();
  718. // because the default list is a BindingList<object>
  719. Assert.IsTrue (source.SupportsChangeNotification, "1");
  720. source.DataSource = c;
  721. // the DataSource is IBindingList, but
  722. // SupportsChangeNotification is false.
  723. Assert.IsTrue (source.SupportsChangeNotification, "2");
  724. c.supports_change_notification = true;
  725. Assert.IsTrue (source.SupportsChangeNotification, "3");
  726. }
  727. [Test]
  728. public void InitializedEvent ()
  729. {
  730. // XXX this test is officially useless. does
  731. // BindingSource even raise the event? it
  732. // seems to always be initialized.
  733. BindingSource source = new BindingSource ();
  734. ISupportInitializeNotification n = (ISupportInitializeNotification)source;
  735. bool event_handled = false;
  736. n.Initialized += delegate (object sender, EventArgs e) {
  737. event_handled = true;
  738. };
  739. Assert.IsTrue (n.IsInitialized, "1");
  740. source.DataSource = "hi";
  741. Assert.IsTrue (n.IsInitialized, "2");
  742. Assert.IsFalse (event_handled, "3");
  743. }
  744. }
  745. }
  746. #endif