BindingSourceTest.cs 24 KB

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