BindingSourceTest.cs 23 KB

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