BindingSourceTest.cs 24 KB

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