PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/BindingSourceTest.cs

https://bitbucket.org/foobar22/mono
C# | 1999 lines | 1450 code | 404 blank | 145 comment | 12 complexity | 8483e84710dc72a1849bf099a742864f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense, Apache-2.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  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 : TestHelper
  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. BindingSource source = new BindingSource ();
  46. source.Add ((int)32);
  47. Assert.IsTrue (source.List is BindingList<int>, "1");
  48. source = new BindingSource ();
  49. source.DataSource = new ArrayList ();
  50. source.Add ((int)32);
  51. Assert.IsFalse (source.List is BindingList<int>, "2");
  52. }
  53. class EmptyEnumerable : IEnumerable {
  54. class EmptyEnumerator : IEnumerator {
  55. public object Current {
  56. get { throw new InvalidOperationException (); }
  57. }
  58. public void Reset () {
  59. // nada
  60. }
  61. public bool MoveNext () {
  62. return false;
  63. }
  64. }
  65. public IEnumerator GetEnumerator () {
  66. return new EmptyEnumerator ();
  67. }
  68. }
  69. class GenericEnumerable : IEnumerable<int> {
  70. int length;
  71. public GenericEnumerable (int length) {
  72. this.length = length;
  73. }
  74. class MyEnumerator : IEnumerator<int> {
  75. public int count;
  76. public int index;
  77. public int Current {
  78. get { return index; }
  79. }
  80. object IEnumerator.Current {
  81. get { return Current; }
  82. }
  83. public void Reset () {
  84. index = 0;
  85. }
  86. public bool MoveNext () {
  87. if (index++ == count)
  88. return false;
  89. else
  90. return true;
  91. }
  92. void IDisposable.Dispose () {
  93. }
  94. }
  95. public IEnumerator<int> GetEnumerator () {
  96. MyEnumerator e = new MyEnumerator ();
  97. e.count = length;
  98. return e;
  99. }
  100. IEnumerator IEnumerable.GetEnumerator () {
  101. return GetEnumerator ();
  102. }
  103. }
  104. class WorkingEnumerable : IEnumerable {
  105. int length;
  106. public WorkingEnumerable (int length) {
  107. this.length = length;
  108. }
  109. class MyEnumerator : IEnumerator {
  110. public int count;
  111. public int index;
  112. public object Current {
  113. get { return index; }
  114. }
  115. public void Reset () {
  116. index = 0;
  117. }
  118. public bool MoveNext () {
  119. if (index++ == count)
  120. return false;
  121. else
  122. return true;
  123. }
  124. }
  125. public IEnumerator GetEnumerator () {
  126. MyEnumerator e = new MyEnumerator ();
  127. e.count = length;
  128. return e;
  129. }
  130. }
  131. [Test]
  132. public void DataSource_ListRelationship ()
  133. {
  134. BindingSource source = new BindingSource ();
  135. // null
  136. source.DataSource = null;
  137. Assert.IsTrue (source.List is BindingList<object>, "1");
  138. // a non-list object
  139. source.DataSource = new object ();
  140. Assert.IsTrue (source.List is BindingList<object>, "2");
  141. // array instance (value type)
  142. source.DataSource = new int[32];
  143. Assert.IsTrue (source.List is int[], "3");
  144. // an instance array with 0 elements
  145. source.DataSource = new int[0];
  146. Assert.IsTrue (source.List is int[], "4");
  147. // array instance (object type)
  148. source.DataSource = new string[32];
  149. Assert.IsTrue (source.List is string[], "5");
  150. // list type
  151. source.DataSource = new List<bool>();
  152. Assert.IsTrue (source.List is List<bool>, "6");
  153. // an IEnumerable type
  154. source.DataSource = "hi";
  155. Assert.IsTrue (source.List is BindingList<char>, "7");
  156. // an IEnumerable type with 0 items
  157. source.DataSource = "";
  158. Assert.IsTrue (source.List is BindingList<char>, "8");
  159. Assert.AreEqual (0, source.List.Count, "9");
  160. // a generic enumerable with no elements.
  161. // even though we can figure out the type
  162. // through reflection, we shouldn't..
  163. source.DataSource = new GenericEnumerable (0);
  164. Console.WriteLine (source.List.GetType());
  165. Assert.IsTrue (source.List is BindingList<char>, "10");
  166. Assert.AreEqual (0, source.List.Count, "11");
  167. // a non-generic IEnumerable type with 0 items
  168. // this doesn't seem to change the type of the
  169. // binding source's list, probably because it
  170. // can't determine the type of the
  171. // enumerable's elements.
  172. source.DataSource = new EmptyEnumerable ();
  173. Assert.IsTrue (source.List is BindingList<char>, "12");
  174. // an enumerable with some elements
  175. source.DataSource = new WorkingEnumerable (5);
  176. Assert.IsTrue (source.List is BindingList<int>, "13");
  177. Assert.AreEqual (5, source.List.Count, "14");
  178. // IListSource - returns an array
  179. source.DataSource = new ListBindingHelperTest.ListSource (true);
  180. Assert.IsTrue (source.List is Array, "#15");
  181. Assert.AreEqual (1, source.List.Count, "#16");
  182. }
  183. [Test]
  184. public void Filter ()
  185. {
  186. BindingSource source = new BindingSource ();
  187. DataTable table = new DataTable ();
  188. string filter = "Name = 'Mono'";
  189. IBindingListView view;
  190. table.Columns.Add ("Id", typeof (int));
  191. table.Columns.Add ("Name", typeof (string));
  192. table.Rows.Add (0, "Mono");
  193. table.Rows.Add (1, "Miguel");
  194. table.Rows.Add (2, "Paolo");
  195. table.Rows.Add (3, "Mono");
  196. source.DataSource = table;
  197. Assert.AreEqual (null, source.Filter, "A1");
  198. source.Filter = filter;
  199. view = (IBindingListView)((IListSource)table).GetList ();
  200. Assert.AreEqual (filter, source.Filter, "B1");
  201. Assert.AreEqual (filter, view.Filter, "B2");
  202. Assert.AreEqual (2, view.Count, "B3");
  203. Assert.AreEqual (2, source.List.Count, "B4");
  204. source.Filter = String.Empty;
  205. Assert.AreEqual (String.Empty, source.Filter, "C1");
  206. Assert.AreEqual (String.Empty, view.Filter, "C2");
  207. Assert.AreEqual (4, view.Count, "C3");
  208. Assert.AreEqual (4, source.List.Count, "C4");
  209. source.DataSource = null;
  210. Assert.AreEqual (String.Empty, source.Filter, "D1"); // Keep previous value
  211. filter = "Name = 'Miguel'"; // Apply filter before assigning data source
  212. source.Filter = filter;
  213. source.DataSource = table;
  214. view = (IBindingListView)((IListSource)table).GetList ();
  215. Assert.AreEqual (filter, source.Filter, "E1");
  216. Assert.AreEqual (filter, view.Filter, "E2");
  217. Assert.AreEqual (1, view.Count, "E3");
  218. Assert.AreEqual (1, source.List.Count, "E4");
  219. }
  220. [Test]
  221. public void Filter_NonBindingListView ()
  222. {
  223. BindingSource source = new BindingSource ();
  224. List<int> list = new List<int> ();
  225. list.AddRange (new int [] { 0, 1, 2 });
  226. string filter = "NonExistentColumn = 'A'"; ;
  227. source.DataSource = list;
  228. Assert.AreEqual (null, source.Filter, "A1");
  229. // List<> doesn't implement IBindingListView, but
  230. // the filter string is saved
  231. source.Filter = filter;
  232. Assert.AreEqual (filter, source.Filter, "B1");
  233. source.Filter = null;
  234. Assert.AreEqual (null, source.Filter, "C1");
  235. }
  236. [Test]
  237. public void RemoveFilter ()
  238. {
  239. BindingSource source = new BindingSource ();
  240. source.Filter = "Name = 'Something'";
  241. source.RemoveFilter ();
  242. Assert.AreEqual (null, source.Filter, "A1");
  243. }
  244. [Test]
  245. public void RemoveSort ()
  246. {
  247. BindingSource source = new BindingSource ();
  248. DataTable table = CreateTable ();
  249. source.DataSource = table;
  250. source.Sort = "Name";
  251. IBindingListView view = (IBindingListView)source.List;
  252. Assert.AreEqual ("Name", source.Sort, "A1");
  253. Assert.AreEqual ("Name", view.SortProperty.Name, "A2");
  254. source.RemoveSort ();
  255. Assert.AreEqual (null, source.Sort, "B1");
  256. Assert.AreEqual (null, view.SortProperty, "B2");
  257. // Non IBindingListView source - No exception, as opposed to what
  258. // the documentation says
  259. source.Sort = null;
  260. source.DataSource = new List<string> ();
  261. source.RemoveSort ();
  262. }
  263. [Test]
  264. public void ResetItem ()
  265. {
  266. BindingSource source = new BindingSource ();
  267. bool delegate_reached = false;
  268. int old_index = 5;
  269. int new_index = 5;
  270. ListChangedType type = ListChangedType.Reset;
  271. source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
  272. delegate_reached = true;
  273. type = e.ListChangedType;
  274. old_index = e.OldIndex;
  275. new_index = e.NewIndex;
  276. };
  277. source.ResetItem (0);
  278. Assert.IsTrue (delegate_reached, "1");
  279. Assert.AreEqual (-1, old_index, "2");
  280. Assert.AreEqual (0, new_index, "3");
  281. Assert.AreEqual (ListChangedType.ItemChanged, type, "3");
  282. }
  283. DataTable CreateTable ()
  284. {
  285. DataTable table = new DataTable ();
  286. table.Columns.Add ("Id", typeof (int));
  287. table.Columns.Add ("Name", typeof (string));
  288. table.Rows.Add (0, "Mono");
  289. table.Rows.Add (2, "JPobst");
  290. table.Rows.Add (1, "Miguel");
  291. return table;
  292. }
  293. [Test]
  294. public void Sort_IBindingList ()
  295. {
  296. BindingSource source = new BindingSource ();
  297. BindingList<string> list = new BindingList<string> ();
  298. source.DataSource = list;
  299. // Implements IBindingList but SupportsSorting is false
  300. try {
  301. source.Sort = "Name";
  302. Assert.Fail ("A1");
  303. } catch (ArgumentException) {
  304. }
  305. }
  306. [Test]
  307. public void Sort_IBindingListView ()
  308. {
  309. BindingSource source = new BindingSource ();
  310. DataTable table = CreateTable ();
  311. //
  312. // Simple
  313. //
  314. source.DataSource = table;
  315. source.Sort = "Name";
  316. DataView view = (DataView)((IListSource)table).GetList ();
  317. Assert.AreEqual ("Name", source.Sort, "A1");
  318. Assert.AreEqual (ListSortDirection.Ascending, ((IBindingListView) source).SortDirection, "A2");
  319. Assert.AreEqual (ListSortDirection.Ascending, ((IBindingListView)view).SortDirection, "A3");
  320. Assert.AreEqual ("Name", ((IBindingListView)source).SortProperty.Name, "A4");
  321. Assert.AreEqual ("Name", ((IBindingListView)view).SortProperty.Name, "A5");
  322. Assert.AreEqual (1, ((IBindingListView)view).SortDescriptions.Count, "A6");
  323. Assert.AreEqual ("Name", ((IBindingListView)view).SortDescriptions [0].PropertyDescriptor.Name, "A7");
  324. Assert.AreEqual ("JPobst", view [0]["Name"], "A8");
  325. Assert.AreEqual ("Miguel", view [1]["Name"], "A9");
  326. Assert.AreEqual ("Mono", view [2]["Name"], "A10");
  327. //
  328. // Simple with direction (extra white spaces)
  329. //
  330. source.Sort = " Name DESC ";
  331. //Assert.AreEqual ("Name DESC", source.Sort, "B1");
  332. Assert.AreEqual (ListSortDirection.Descending, ((IBindingListView)view).SortDirection, "B2");
  333. Assert.AreEqual ("Name", ((IBindingListView)view).SortProperty.Name, "B3");
  334. Assert.AreEqual ("Mono", view [0]["Name"], "B4");
  335. Assert.AreEqual ("Miguel", view [1]["Name"], "B5");
  336. Assert.AreEqual ("JPobst", view [2]["Name"], "B6");
  337. //
  338. // Multiple
  339. //
  340. source.Sort = "Name DESC, Id asc";
  341. ListSortDescriptionCollection desc_coll = ((IBindingListView)view).SortDescriptions;
  342. Assert.AreEqual ("Name DESC, Id asc", source.Sort, "C1");
  343. Assert.AreEqual (2, desc_coll.Count, "C2");
  344. Assert.AreEqual (ListSortDirection.Descending, desc_coll [0].SortDirection, "C3");
  345. Assert.AreEqual ("Name", desc_coll [0].PropertyDescriptor.Name, "C4");
  346. Assert.AreEqual (ListSortDirection.Ascending, desc_coll [1].SortDirection, "C5");
  347. Assert.AreEqual ("Id", desc_coll [1].PropertyDescriptor.Name, "C6");
  348. }
  349. [Test]
  350. public void Sort_NonBindingList ()
  351. {
  352. BindingSource source = new BindingSource ();
  353. List<int> list = new List<int> (new int [] { 0, 1, 2, 3 });
  354. source.DataSource = list;
  355. Assert.AreEqual (null, source.Sort, "A1");
  356. try {
  357. source.Sort = "Name";
  358. Assert.Fail ("B1");
  359. } catch (ArgumentException) {
  360. }
  361. source.Sort = String.Empty;
  362. Assert.AreEqual (String.Empty, source.Sort, "C1");
  363. }
  364. [Test]
  365. public void Sort_Exceptions ()
  366. {
  367. BindingSource source = new BindingSource ();
  368. DataTable table = CreateTable ();
  369. source.DataSource = table;
  370. // Non-existant property
  371. try {
  372. source.Sort = "Name, DontExist DESC";
  373. Assert.Fail ("exc1");
  374. } catch (ArgumentException) {
  375. }
  376. // Wrong direction
  377. try {
  378. source.Sort = "Name WRONGDIR";
  379. Assert.Fail ("exc2");
  380. } catch (ArgumentException) {
  381. }
  382. // Wrong format
  383. try {
  384. source.Sort = "Name, , Id";
  385. Assert.Fail ("exc3");
  386. } catch (ArgumentException) {
  387. }
  388. }
  389. [Test]
  390. public void Movement ()
  391. {
  392. BindingSource source = new BindingSource ();
  393. source.DataSource = new WorkingEnumerable (5);
  394. int raised = 0;
  395. source.PositionChanged += delegate (object sender, EventArgs e) { raised ++; };
  396. Console.WriteLine ("count = {0}", source.Count);
  397. source.Position = 3;
  398. Assert.AreEqual (3, source.Position, "1");
  399. source.MoveFirst ();
  400. Assert.AreEqual (0, source.Position, "2");
  401. source.MoveNext ();
  402. Assert.AreEqual (1, source.Position, "3");
  403. source.MovePrevious ();
  404. Assert.AreEqual (0, source.Position, "4");
  405. source.MoveLast ();
  406. Assert.AreEqual (4, source.Position, "5");
  407. Assert.AreEqual (5, raised, "6");
  408. }
  409. [Test]
  410. public void Position ()
  411. {
  412. BindingSource source = new BindingSource ();
  413. CurrencyManager currency_manager = source.CurrencyManager;
  414. Assert.AreEqual (-1, source.Position, "A1");
  415. Assert.AreEqual (-1, currency_manager.Position, "A2");
  416. source.DataSource = new WorkingEnumerable (5);
  417. int raised = 0;
  418. int currency_raised = 0;
  419. source.PositionChanged += delegate (object sender, EventArgs e) { raised ++; };
  420. currency_manager.PositionChanged += delegate (object sender, EventArgs e) { currency_raised++; };
  421. Assert.AreEqual (0, source.Position, "B1");
  422. Assert.AreEqual (0, currency_manager.Position, "B2");
  423. source.Position = -1;
  424. Assert.AreEqual (0, source.Position, "C1");
  425. Assert.AreEqual (0, currency_manager.Position, "C2");
  426. Assert.AreEqual (0, raised, "C3");
  427. Assert.AreEqual (0, currency_raised, "C4");
  428. source.Position = 10;
  429. Assert.AreEqual (4, source.Position, "D1");
  430. Assert.AreEqual (4, currency_manager.Position, "D2");
  431. Assert.AreEqual (1, raised, "D3");
  432. Assert.AreEqual (1, currency_raised, "D4");
  433. source.Position = 10;
  434. Assert.AreEqual (4, source.Position, "E1");
  435. Assert.AreEqual (1, raised, "E2");
  436. // Now make some changes in CurrencyManager.Position, which should be visible
  437. // in BindingSource.Position
  438. currency_manager.Position = 0;
  439. Assert.AreEqual (0, source.Position, "F1");
  440. Assert.AreEqual (0, currency_manager.Position, "F2");
  441. Assert.AreEqual (2, raised, "F3");
  442. Assert.AreEqual (2, currency_raised, "F4");
  443. // Finally an etmpy collection
  444. source.DataSource = new List<int> ();
  445. Assert.AreEqual (-1, source.Position, "G1");
  446. Assert.AreEqual (-1, currency_manager.Position, "G2");
  447. }
  448. [Test]
  449. public void ResetCurrentItem ()
  450. {
  451. BindingSource source = new BindingSource ();
  452. bool delegate_reached = false;
  453. int old_index = 5;
  454. int new_index = 5;
  455. ListChangedType type = ListChangedType.Reset;
  456. source.DataSource = new WorkingEnumerable (5);
  457. source.Position = 2;
  458. source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
  459. delegate_reached = true;
  460. type = e.ListChangedType;
  461. old_index = e.OldIndex;
  462. new_index = e.NewIndex;
  463. };
  464. source.ResetCurrentItem ();
  465. Assert.IsTrue (delegate_reached, "1");
  466. Assert.AreEqual (-1, old_index, "2");
  467. Assert.AreEqual (2, new_index, "3");
  468. Assert.AreEqual (ListChangedType.ItemChanged, type, "3");
  469. }
  470. [Test]
  471. public void Remove ()
  472. {
  473. BindingSource source = new BindingSource ();
  474. List<string> list = new List<string> ();
  475. list.Add ("A");
  476. source.DataSource = list;
  477. Assert.AreEqual (1, source.List.Count, "1");
  478. source.Remove ("A");
  479. Assert.AreEqual (0, list.Count, "2");
  480. // Different type, - no exception
  481. source.Remove (7);
  482. // Fixed size
  483. try {
  484. source.DataSource = new int [0];
  485. source.Remove (7);
  486. Assert.Fail ("exc1");
  487. } catch (NotSupportedException) {
  488. }
  489. // Read only
  490. try {
  491. source.DataSource = Array.AsReadOnly (new int [0]);
  492. source.Remove (7);
  493. Assert.Fail ("exc2");
  494. } catch (NotSupportedException) {
  495. }
  496. }
  497. [Test]
  498. public void RemoveCurrent ()
  499. {
  500. BindingSource source = new BindingSource ();
  501. List<string> list = new List<string> ();
  502. list.Add ("A");
  503. list.Add ("B");
  504. list.Add ("C");
  505. source.DataSource = list;
  506. source.Position = 1;
  507. Assert.AreEqual (1, source.Position, "A1");
  508. Assert.AreEqual ("B", source.Current, "A2");
  509. source.RemoveCurrent ();
  510. Assert.AreEqual (1, source.Position, "B1");
  511. Assert.AreEqual ("C", source.Current, "B2");
  512. Assert.AreEqual (2, source.Count, "B3");
  513. Assert.AreEqual ("A", source [0], "B4");
  514. Assert.AreEqual ("C", source [1], "B5");
  515. // Position is -1, since there are no items
  516. source.Clear ();
  517. try {
  518. source.RemoveCurrent ();
  519. Assert.Fail ("exc1");
  520. } catch (InvalidOperationException) {
  521. }
  522. source.DataSource = new int [1];
  523. try {
  524. source.RemoveCurrent ();
  525. Assert.Fail ("exc2");
  526. } catch (InvalidOperationException) {
  527. }
  528. }
  529. [Test]
  530. public void ResetBindings ()
  531. {
  532. BindingSource source;
  533. int event_count = 0;
  534. ListChangedType[] types = new ListChangedType[2];
  535. int[] old_index = new int[2];
  536. int[] new_index = new int[2];
  537. source = new BindingSource ();
  538. source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
  539. types[event_count] = e.ListChangedType;
  540. old_index[event_count] = e.OldIndex;
  541. new_index[event_count] = e.NewIndex;
  542. event_count ++;
  543. };
  544. event_count = 0;
  545. source.ResetBindings (false);
  546. Assert.AreEqual (1, event_count, "1");
  547. Assert.AreEqual (ListChangedType.Reset, types[0], "2");
  548. Assert.AreEqual (-1, old_index[0], "3");
  549. Assert.AreEqual (-1, new_index[0], "4");
  550. event_count = 0;
  551. source.ResetBindings (true);
  552. Assert.AreEqual (2, event_count, "5");
  553. Assert.AreEqual (ListChangedType.PropertyDescriptorChanged, types[0], "6");
  554. Assert.AreEqual (0, old_index[0], "7");
  555. Assert.AreEqual (0, new_index[0], "8");
  556. Assert.AreEqual (ListChangedType.Reset, types[1], "9");
  557. Assert.AreEqual (-1, old_index[1], "10");
  558. Assert.AreEqual (-1, new_index[1], "11");
  559. }
  560. [Test]
  561. public void AllowEdit ()
  562. {
  563. BindingSource source = new BindingSource ();
  564. Assert.IsTrue (source.AllowEdit, "1");
  565. source.DataSource = "";
  566. Assert.IsTrue (source.AllowEdit, "2");
  567. source.DataSource = new int[10];
  568. Assert.IsTrue (source.AllowEdit, "3");
  569. source.DataSource = new WorkingEnumerable (5);
  570. Assert.IsTrue (source.AllowEdit, "4");
  571. ArrayList al = new ArrayList ();
  572. al.Add (5);
  573. source.DataSource = al;
  574. Assert.IsTrue (source.AllowEdit, "5");
  575. source.DataSource = ArrayList.ReadOnly (al);
  576. Assert.IsFalse (source.AllowEdit, "6");
  577. }
  578. [Test]
  579. public void AllowRemove ()
  580. {
  581. BindingSource source = new BindingSource ();
  582. Assert.IsTrue (source.AllowRemove, "1");
  583. source.DataSource = "";
  584. Assert.IsTrue (source.AllowRemove, "2");
  585. source.DataSource = new ArrayList ();
  586. Assert.IsTrue (source.AllowRemove, "3");
  587. source.DataSource = new int[10];
  588. Assert.IsFalse (source.AllowRemove, "4");
  589. source.DataSource = new WorkingEnumerable (5);
  590. Assert.IsTrue (source.AllowRemove, "5");
  591. }
  592. [Test]
  593. public void DataMember_ListRelationship ()
  594. {
  595. ListView lv = new ListView ();
  596. BindingSource source = new BindingSource ();
  597. // Empty IEnumerable, that also implements IList
  598. source.DataSource = lv.Items;
  599. source.DataMember = "Text";
  600. // FIXME: The test below does not pass with .NET (type is BindingList<char>)
  601. Assert.IsTrue (source.List is BindingList<string>, "1");
  602. Assert.AreEqual (0, source.List.Count, "2");
  603. }
  604. [Test]
  605. public void DataMemberNull ()
  606. {
  607. BindingSource source = new BindingSource ();
  608. Assert.AreEqual ("", source.DataMember, "1");
  609. source.DataMember = null;
  610. Assert.AreEqual ("", source.DataMember, "2");
  611. }
  612. [Test]
  613. public void DataSourceChanged ()
  614. {
  615. ArrayList list = new ArrayList ();
  616. BindingSource source = new BindingSource ();
  617. bool event_raised = false;
  618. source.DataSourceChanged += delegate (object sender, EventArgs e) {
  619. event_raised = true;
  620. };
  621. source.DataSource = list;
  622. Assert.IsTrue (event_raised, "1");
  623. event_raised = false;
  624. source.DataSource = list;
  625. Assert.IsFalse (event_raised, "2");
  626. }
  627. [Test]
  628. [ExpectedException (typeof (ArgumentException))] // DataMember property 'hi' cannot be found on the DataSource.
  629. public void DataMemberArgumentException ()
  630. {
  631. ArrayList list = new ArrayList ();
  632. BindingSource source = new BindingSource ();
  633. source.DataSource = list;
  634. source.DataMember = "hi";
  635. }
  636. [Test]
  637. public void DataMemberBeforeDataSource ()
  638. {
  639. ArrayList list = new ArrayList ();
  640. BindingSource source = new BindingSource ();
  641. source.DataMember = "hi";
  642. Assert.AreEqual ("hi", source.DataMember, "1");
  643. source.DataSource = list;
  644. Assert.AreEqual ("", source.DataMember, "2");
  645. }
  646. [Test]
  647. public void DataSourceAssignToDefaultType()
  648. {
  649. BindingSource source = new BindingSource ();
  650. source.DataMember = "hi";
  651. Assert.AreEqual ("hi", source.DataMember, "1");
  652. Assert.IsTrue (source.List is BindingList<object>, "2");
  653. source.DataSource = new BindingList<object>();
  654. Assert.AreEqual ("", source.DataMember, "3");
  655. }
  656. [Test]
  657. public void DataSourceSetType ()
  658. {
  659. BindingSource source = new BindingSource ();
  660. source.DataSource = typeof (DateTime);
  661. Assert.IsTrue (source.List is BindingList<DateTime>, "A1");
  662. Assert.AreEqual (0, source.List.Count, "A2");
  663. Assert.AreEqual (typeof (DateTime), source.DataSource);
  664. }
  665. [Test]
  666. public void DataMemberChanged ()
  667. {
  668. ArrayList list = new ArrayList ();
  669. BindingSource source = new BindingSource ();
  670. bool event_raised = false;
  671. list.Add ("hi"); // make the type System.String
  672. source.DataMemberChanged += delegate (object sender, EventArgs e) {
  673. event_raised = true;
  674. };
  675. source.DataSource = list;
  676. source.DataMember = "Length";
  677. Assert.IsTrue (event_raised, "1");
  678. event_raised = false;
  679. source.DataMember = "Length";
  680. Assert.IsFalse (event_raised, "2");
  681. }
  682. [Test]
  683. public void DataMemberNullDataSource ()
  684. {
  685. BindingSource source = new BindingSource ();
  686. source.Add ("hellou");
  687. source.DataMember = "SomeProperty"; // Should reset the list, even if data source is null
  688. Assert.IsTrue (source.List is BindingList<object>, "A1");
  689. Assert.AreEqual (0, source.List.Count, "A2");
  690. }
  691. [Test]
  692. public void SuppliedDataSource ()
  693. {
  694. List<string> list = new List<string>();
  695. BindingSource source;
  696. source = new BindingSource (list, "");
  697. Assert.IsTrue (source.List is List<string>, "1");
  698. source.DataMember = "Length";
  699. Assert.IsTrue (source.List is BindingList<int>, "2");
  700. source = new BindingSource (list, "Length");
  701. Assert.IsTrue (source.List is BindingList<int>, "3");
  702. }
  703. [Test]
  704. public void DataSourceMember_set ()
  705. {
  706. BindingSource source = new BindingSource ();
  707. source.DataSource = new List<string>();
  708. source.DataMember = "Length";
  709. Assert.IsNotNull (source.CurrencyManager, "1");
  710. source.DataSource = new List<string>();
  711. Assert.AreEqual ("Length", source.DataMember, "2");
  712. Assert.IsNotNull (source.CurrencyManager, "3");
  713. source.DataSource = new List<string[]>();
  714. Assert.AreEqual ("Length", source.DataMember, "4");
  715. Assert.IsNotNull (source.CurrencyManager, "5");
  716. }
  717. [Test]
  718. public void DataSourceMemberChangedEvents ()
  719. {
  720. BindingSource source = new BindingSource ();
  721. bool data_source_changed = false;
  722. bool data_member_changed = false;
  723. source.DataSourceChanged += delegate (object sender, EventArgs e) {
  724. data_source_changed = true;
  725. };
  726. source.DataMemberChanged += delegate (object sender, EventArgs e) {
  727. data_member_changed = true;
  728. };
  729. data_source_changed = false;
  730. data_member_changed = false;
  731. source.DataSource = new List<string>();
  732. Assert.IsTrue (data_source_changed, "1");
  733. Assert.IsFalse (data_member_changed, "2");
  734. data_source_changed = false;
  735. data_member_changed = false;
  736. source.DataMember = "Length";
  737. Assert.IsFalse (data_source_changed, "3");
  738. Assert.IsTrue (data_member_changed, "4");
  739. }
  740. [Test]
  741. public void IsBindingSuspended ()
  742. {
  743. BindingSource source = new BindingSource ();
  744. CurrencyManager currency_manager = source.CurrencyManager;
  745. source.DataSource = new object [1];
  746. source.SuspendBinding ();
  747. Assert.AreEqual (true, source.IsBindingSuspended, "A1");
  748. Assert.AreEqual (true, currency_manager.IsBindingSuspended, "A2");
  749. source.ResumeBinding ();
  750. Assert.AreEqual (false, source.IsBindingSuspended, "B1");
  751. Assert.AreEqual (false, currency_manager.IsBindingSuspended, "B2");
  752. // Changes made to CurrencyManager should be visible in BindingSource
  753. currency_manager.SuspendBinding ();
  754. Assert.AreEqual (true, source.IsBindingSuspended, "C1");
  755. Assert.AreEqual (true, currency_manager.IsBindingSuspended, "C2");
  756. currency_manager.ResumeBinding ();
  757. Assert.AreEqual (false, source.IsBindingSuspended, "D1");
  758. Assert.AreEqual (false, currency_manager.IsBindingSuspended, "D2");
  759. }
  760. [Test]
  761. public void Add ()
  762. {
  763. BindingSource source = new BindingSource ();
  764. source.DataSource = new List<string> ();
  765. source.Add ("A");
  766. Assert.AreEqual (1, source.List.Count, "2");
  767. // Different item type
  768. try {
  769. source.Add (4);
  770. Assert.Fail ("exc1");
  771. } catch (InvalidOperationException) {
  772. }
  773. // FixedSize
  774. try {
  775. source.DataSource = new int [0];
  776. source.Add (7);
  777. Assert.Fail ("exc2");
  778. } catch (NotSupportedException) {
  779. }
  780. // ReadOnly
  781. try {
  782. source.DataSource = Array.AsReadOnly (new int [0]);
  783. source.Add (7);
  784. Assert.Fail ("exc3");
  785. } catch (NotSupportedException) {
  786. }
  787. }
  788. [Test]
  789. public void Add_NullDataSource ()
  790. {
  791. BindingSource source = new BindingSource ();
  792. source.Add ("A");
  793. Assert.AreEqual (1, source.List.Count, "1");
  794. Assert.IsTrue (source.List is BindingList<string>, "2");
  795. Assert.IsNull (source.DataSource, "3");
  796. source = new BindingSource ();
  797. source.Add (null);
  798. Assert.IsTrue (source.List is BindingList<object>, "4");
  799. Assert.AreEqual (1, source.List.Count, "5");
  800. }
  801. [Test]
  802. public void AddNew ()
  803. {
  804. BindingSource source = new BindingSource ();
  805. source.AddNew ();
  806. Assert.AreEqual (1, source.Count, "1");
  807. }
  808. [Test]
  809. public void AddNew_NonBindingList ()
  810. {
  811. IList list = new List<object> ();
  812. BindingSource source = new BindingSource ();
  813. source.DataSource = list;
  814. Assert.IsTrue (source.List is List<object>, "1");
  815. source.AddNew ();
  816. Assert.AreEqual (1, source.Count, "2");
  817. }
  818. [Test]
  819. public void ApplySort ()
  820. {
  821. BindingSource source = new BindingSource ();
  822. DataTable table = CreateTable ();
  823. source.DataSource = table;
  824. IBindingListView source_view = ((IBindingListView)source);
  825. IBindingListView view = ((IBindingListView)source.List);
  826. PropertyDescriptor property = source.GetItemProperties (null) ["Name"];
  827. source_view.ApplySort (property, ListSortDirection.Ascending);
  828. Assert.AreEqual (property, view.SortProperty, "A1");
  829. // Non IBindingList source - Passing an invalid property
  830. // but the method is not called since source is not of the required type
  831. source.DataSource = new List<string> ();
  832. try {
  833. source_view.ApplySort (property, ListSortDirection.Ascending);
  834. Assert.Fail ("B1");
  835. } catch (NotSupportedException) {
  836. }
  837. }
  838. [Test]
  839. public void AllowNew_getter ()
  840. {
  841. BindingSource source = new BindingSource ();
  842. // true because the default datasource is BindingList<object>
  843. Assert.IsTrue (source.AllowNew, "1");
  844. source.DataSource = new object[10];
  845. // fixed size
  846. Assert.IsFalse (source.AllowNew, "2");
  847. source.DataSource = new BindingList<string>();
  848. // no default ctor
  849. Assert.IsFalse (source.AllowNew, "3");
  850. }
  851. [Test]
  852. public void AllowNew ()
  853. {
  854. BindingSource source = new BindingSource ();
  855. source.AllowNew = false;
  856. Assert.IsFalse (source.AllowNew, "1");
  857. source.ResetAllowNew ();
  858. Assert.IsTrue (source.AllowNew, "2");
  859. }
  860. [Test]
  861. [ExpectedException (typeof (InvalidOperationException))]
  862. // "AllowNew can only be set to true on an
  863. // IBindingList or on a read-write list with a default
  864. // public constructor."
  865. public void AllowNew_FixedSize ()
  866. {
  867. BindingSource source = new BindingSource ();
  868. source.DataSource = new object[10];
  869. source.AllowNew = true;
  870. }
  871. #if false
  872. // According to the MS docs, this should fail with a MissingMethodException
  873. [Test]
  874. public void AllowNew_NoDefaultCtor ()
  875. {
  876. List<string> s = new List<string>();
  877. s.Add ("hi");
  878. BindingSource source = new BindingSource ();
  879. source.DataSource = s;
  880. source.AllowNew = true;
  881. Assert.Fail ();
  882. }
  883. #endif
  884. [Test]
  885. [ExpectedException (typeof (InvalidOperationException))]
  886. // "Item cannot be added to a read-only or fixed-size list."
  887. public void AddNew_BindingListWithoutAllowNew ()
  888. {
  889. BindingList<int> l = new BindingList<int>();
  890. l.AllowNew = false;
  891. BindingSource source = new BindingSource ();
  892. source.DataSource = l;
  893. source.AddNew ();
  894. Assert.AreEqual (1, source.Count, "1");
  895. }
  896. [Test]
  897. [ExpectedException (typeof (InvalidOperationException))]
  898. // "Item cannot be added to a read-only or fixed-size list."
  899. public void AddNew_FixedSize ()
  900. {
  901. BindingSource source = new BindingSource ();
  902. source.DataSource = new int[5];
  903. object o = source.AddNew ();
  904. Assert.IsTrue (o is int, "1");
  905. Assert.AreEqual (6, source.Count, "2");
  906. }
  907. class ReadOnlyList : List<int>, IList {
  908. public int Add (object value) {
  909. throw new Exception ();
  910. }
  911. public bool Contains (object value) {
  912. throw new Exception ();
  913. }
  914. public int IndexOf (object value) {
  915. throw new Exception ();
  916. }
  917. public void Insert (int index, object value) {
  918. throw new Exception ();
  919. }
  920. public void Remove (object value) {
  921. throw new Exception ();
  922. }
  923. public bool IsFixedSize {
  924. get { return false; }
  925. }
  926. public bool IsReadOnly {
  927. get { return true; }
  928. }
  929. }
  930. [Test]
  931. [ExpectedException (typeof (InvalidOperationException))]
  932. // "Item cannot be added to a read-only or fixed-size list."
  933. public void AddNew_ReadOnly ()
  934. {
  935. BindingSource source = new BindingSource ();
  936. source.DataSource = new ReadOnlyList ();
  937. object o = source.AddNew ();
  938. TestHelper.RemoveWarning (o);
  939. }
  940. [Test]
  941. [ExpectedException (typeof (InvalidOperationException))]
  942. // "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."
  943. public void AddNew_Invalid ()
  944. {
  945. BindingSource source = new BindingSource ();
  946. source.DataSource = new List<string>();
  947. object o = source.AddNew ();
  948. TestHelper.RemoveWarning (o);
  949. }
  950. [Test]
  951. public void AddingNew ()
  952. {
  953. // Need to use a class missing a default .ctor
  954. BindingSource source = new BindingSource ();
  955. List<DateTime> list = new List<DateTime> ();
  956. source.DataSource = list;
  957. Assert.AreEqual (false, source.AllowNew, "A1");
  958. source.AllowNew = true;
  959. source.AddingNew += delegate (object o, AddingNewEventArgs args)
  960. {
  961. args.NewObject = DateTime.Now;
  962. };
  963. source.AddNew ();
  964. Assert.AreEqual (1, source.Count, "A1");
  965. }
  966. [Test]
  967. public void AddingNew_Exceptions ()
  968. {
  969. BindingSource source = new BindingSource ();
  970. List<DateTime> list = new List<DateTime> ();
  971. source.DataSource = list;
  972. Assert.AreEqual (false, source.AllowNew, "A1");
  973. source.AllowNew = true;
  974. // No handler for AddingNew
  975. try {
  976. source.AddNew ();
  977. Assert.Fail ("exc1");
  978. } catch (InvalidOperationException) {
  979. }
  980. // Adding new handled, but AddingNew is false
  981. source.AllowNew = false;
  982. source.AddingNew += delegate (object o, AddingNewEventArgs args)
  983. {
  984. args.NewObject = DateTime.Now;
  985. };
  986. try {
  987. source.AddNew ();
  988. Assert.Fail ("exc2");
  989. } catch (InvalidOperationException) {
  990. }
  991. // Wrong type
  992. source = new BindingSource ();
  993. source.DataSource = new List<string> ();
  994. source.AllowNew = true;
  995. source.AddingNew += delegate (object o, AddingNewEventArgs args)
  996. {
  997. args.NewObject = 3.1416;
  998. };
  999. try {
  1000. source.AddNew ();
  1001. Assert.Fail ("exc3");
  1002. } catch (InvalidOperationException) {
  1003. }
  1004. // Null value
  1005. source = new BindingSource ();
  1006. source.DataSource = new List<string> ();
  1007. source.AllowNew = true;
  1008. source.AddingNew += delegate (object o, AddingNewEventArgs args)
  1009. {
  1010. args.NewObject = null;
  1011. };
  1012. try {
  1013. source.AddNew ();
  1014. Assert.Fail ("exc4");
  1015. } catch (InvalidOperationException) {
  1016. }
  1017. }
  1018. [Test]
  1019. public void BindingSuspended1 ()
  1020. {
  1021. // Empty collection as datasource means CurrencyManager will remain
  1022. // as suspended
  1023. BindingSource source = new BindingSource ();
  1024. source.DataSource = new List<string>();
  1025. Assert.IsTrue (source.IsBindingSuspended, "1");
  1026. source.SuspendBinding ();
  1027. Assert.IsTrue (source.IsBindingSuspended, "2");
  1028. source.ResumeBinding ();
  1029. Assert.IsTrue (source.IsBindingSuspended, "3");
  1030. source.ResumeBinding ();
  1031. Assert.IsTrue (source.IsBindingSuspended, "4");
  1032. }
  1033. [Test]
  1034. public void ICancelAddNew ()
  1035. {
  1036. BindingSource source = new BindingSource ();
  1037. source.DataSource = new List<string> ();
  1038. source.AddingNew += delegate (object o, AddingNewEventArgs args) { args.NewObject = "A"; };
  1039. source.AllowNew = true;
  1040. // CancelNew
  1041. source.AddNew ();
  1042. Assert.AreEqual (1, source.Count, "A1");
  1043. Assert.AreEqual ("A", source [0], "A2");
  1044. ((ICancelAddNew)source).CancelNew (0);
  1045. Assert.AreEqual (0, source.Count, "A3");
  1046. // EndNew
  1047. source.AddNew ();
  1048. ((ICancelAddNew)source).EndNew (0);
  1049. ((ICancelAddNew)source).CancelNew (0);
  1050. Assert.AreEqual (1, source.Count, "B1");
  1051. Assert.AreEqual ("A", source [0], "B2");
  1052. //
  1053. // Access operations are suppoused to automatically
  1054. // call EndNew, but that only happens with AddNew
  1055. //
  1056. // AddNew
  1057. source.Clear ();
  1058. source.AddNew ();
  1059. source.AddNew ();
  1060. ((ICancelAddNew)source).CancelNew (0);
  1061. Assert.AreEqual (2, source.Count, "C1");
  1062. Assert.AreEqual ("A", source [0], "C2");
  1063. Assert.AreEqual ("A", source [1], "C3");
  1064. // Add - Does not call EndNew
  1065. source.Clear ();
  1066. source.AddNew ();
  1067. source.Add ("B");
  1068. ((ICancelAddNew)source).CancelNew (0);
  1069. Assert.AreEqual (1, source.Count, "D1");
  1070. Assert.AreEqual ("B", source [0], "D2");
  1071. // Remove - Does not neither
  1072. source.Clear ();
  1073. source.AddNew ();
  1074. source.Add ("B");
  1075. source.Remove ("B");
  1076. ((ICancelAddNew)source).CancelNew (0);
  1077. Assert.AreEqual (0, source.Count, "E1");
  1078. // Wrong index param passed to CancelNew
  1079. source.Clear ();
  1080. source.AddNew ();
  1081. source.Add ("B");
  1082. ((ICancelAddNew)source).CancelNew (1); // Should pass 0
  1083. Assert.AreEqual (2, source.Count, "F1");
  1084. // Multiple pending items - Only takes into account the last one
  1085. source.Clear ();
  1086. source.AddNew ();
  1087. source.AddNew ();
  1088. ((ICancelAddNew)source).CancelNew (1);
  1089. ((ICancelAddNew)source).CancelNew (0);
  1090. Assert.AreEqual (1, source.Count, "G1");
  1091. // Bug?
  1092. source.Clear ();
  1093. source.AddNew ();
  1094. source.Add ("B");
  1095. source.RemoveAt (0); // Added with AddNew
  1096. ((ICancelAddNew)source).CancelNew (0); // Removed item that wasn't added with AddNew
  1097. Assert.AreEqual (0, source.Count, "H1");
  1098. }
  1099. [Test]
  1100. public void Clear ()
  1101. {
  1102. BindingSource source = new BindingSource ();
  1103. List<string> list = new List<string> ();
  1104. list.Add ("A");
  1105. list.Add ("B");
  1106. source.DataSource = list;
  1107. source.Clear ();
  1108. Assert.AreEqual (0, source.List.Count, "1");
  1109. Assert.IsTrue (source.List is List<string>, "2");
  1110. // Exception only for ReadOnly, not for fixed size
  1111. source.DataSource = new int [0];
  1112. source.Clear ();
  1113. try {
  1114. source.DataSource = Array.AsReadOnly (new int [0]);
  1115. source.Clear ();
  1116. Assert.Fail ("exc1");
  1117. } catch (NotSupportedException) {
  1118. }
  1119. }
  1120. [Test]
  1121. public void Clear_NullDataSource ()
  1122. {
  1123. BindingSource source = new BindingSource ();
  1124. source.Add ("A");
  1125. Assert.AreEqual (1, source.List.Count, "1");
  1126. Assert.IsTrue (source.List is BindingList<string>, "2");
  1127. Assert.IsNull (source.DataSource, "3");
  1128. source.Clear ();
  1129. Assert.AreEqual (0, source.List.Count, "4");
  1130. Assert.IsTrue (source.List is BindingList<string>, "5");
  1131. // Change list item type after clearing
  1132. source.Add (7);
  1133. Assert.AreEqual (1, source.List.Count, "6");
  1134. Assert.IsTrue (source.List is BindingList<int>, "7");
  1135. }
  1136. [Test]
  1137. public void CurrencyManager ()
  1138. {
  1139. BindingSource source = new BindingSource ();
  1140. CurrencyManager curr_manager;
  1141. //
  1142. // Null data source
  1143. //
  1144. curr_manager = source.CurrencyManager;
  1145. Assert.IsTrue (curr_manager != null, "A1");
  1146. Assert.IsTrue (curr_manager.List != null, "A2");
  1147. Assert.IsTrue (curr_manager.List is BindingSource, "A3");
  1148. Assert.AreEqual (0, curr_manager.List.Count, "A4");
  1149. Assert.AreEqual (0, curr_manager.Count, "A5");
  1150. Assert.AreEqual (-1, curr_manager.Position, "A6");
  1151. Assert.IsTrue (curr_manager.Bindings != null, "A7");
  1152. Assert.AreEqual (0, curr_manager.Bindings.Count, "A8");
  1153. Assert.AreEqual (source, curr_manager.List, "A9");
  1154. //
  1155. // Non null data source
  1156. //
  1157. List<string> list = new List<string> ();
  1158. list.Add ("A");
  1159. list.Add ("B");
  1160. source.DataSource = list;
  1161. curr_manager = source.CurrencyManager;
  1162. Assert.IsTrue (curr_manager != null, "B1");
  1163. Assert.IsTrue (curr_manager.List != null, "B2");
  1164. Assert.IsTrue (curr_manager.List is BindingSource, "B3");
  1165. Assert.AreEqual (2, curr_manager.List.Count, "B4");
  1166. Assert.AreEqual (2, curr_manager.Count, "B5");
  1167. Assert.AreEqual (0, curr_manager.Position, "B6");
  1168. Assert.IsTrue (curr_manager.Bindings != null, "B7");
  1169. Assert.AreEqual (0, curr_manager.Bindings.Count, "B8");
  1170. Assert.AreEqual (source, curr_manager.List, "B9");
  1171. curr_manager.Position = source.Count - 1;
  1172. Assert.AreEqual (1, curr_manager.Position, "C1");
  1173. Assert.AreEqual ("B", curr_manager.Current, "C2");
  1174. Assert.AreEqual (1, source.Position, "C3");
  1175. Assert.AreEqual ("B", source.Current, "C4");
  1176. }
  1177. [Test]
  1178. public void GetRelatedCurrencyManagerList ()
  1179. {
  1180. ListView lv = new ListView ();
  1181. lv.Columns.Add ("A");
  1182. BindingSource source = new BindingSource ();
  1183. source.DataSource = lv;
  1184. CurrencyManager cm = source.GetRelatedCurrencyManager ("Columns");
  1185. BindingSource related_source = (BindingSource)cm.List;
  1186. Assert.AreEqual (1, cm.Count, "A1");
  1187. Assert.AreEqual (1, related_source.Count, "A2");
  1188. Assert.AreEqual ("Columns", related_source.DataMember, "A3");
  1189. Assert.AreSame (source, related_source.DataSource, "A4");
  1190. Assert.IsTrue (related_source.List is ListView.ColumnHeaderCollection, "A5");
  1191. Assert.AreSame (cm, source.GetRelatedCurrencyManager ("Columns"), "A6");
  1192. // A path string returns null
  1193. cm = source.GetRelatedCurrencyManager ("Columns.Count");
  1194. Assert.IsNull (cm, "B1");
  1195. // String.Empty and null
  1196. Assert.AreSame (source.CurrencyManager, source.GetRelatedCurrencyManager (String.Empty), "C1");
  1197. Assert.AreSame (source.CurrencyManager, source.GetRelatedCurrencyManager (null), "C2");
  1198. }
  1199. [Test]
  1200. public void GetRelatedCurrencyManagerObject ()
  1201. {
  1202. BindingSource source = new BindingSource ();
  1203. ListViewItem item = new ListViewItem ();
  1204. source.DataSource = item;
  1205. CurrencyManager font_cm = source.GetRelatedCurrencyManager ("Font");
  1206. CurrencyManager name_cm = source.GetRelatedCurrencyManager ("Font.Name");
  1207. Assert.IsNull (name_cm, "A1");
  1208. }
  1209. class BindingListViewPoker : BindingList<string>, IBindingListView
  1210. {
  1211. public bool supports_filter;
  1212. public bool supports_advanced_sorting;
  1213. public string Filter {
  1214. get { return ""; }
  1215. set { }
  1216. }
  1217. public ListSortDescriptionCollection SortDescriptions {
  1218. get { return null; }
  1219. }
  1220. public bool SupportsAdvancedSorting {
  1221. get { return supports_advanced_sorting; }
  1222. }
  1223. public bool SupportsFiltering {
  1224. get { return supports_filter; }
  1225. }
  1226. public void ApplySort (ListSortDescriptionCollection sorts)
  1227. {
  1228. }
  1229. public void RemoveFilter ()
  1230. {
  1231. }
  1232. }
  1233. [Test]
  1234. public void SupportsFilter ()
  1235. {
  1236. BindingListViewPoker c = new BindingListViewPoker ();
  1237. BindingSource source = new BindingSource ();
  1238. // because the default list is a BindingList<object>
  1239. Assert.IsFalse (source.SupportsFiltering, "1");
  1240. source.DataSource = c;
  1241. // the DataSource is IBindingListView, but
  1242. // SupportsFilter is false.
  1243. Assert.IsFalse (source.SupportsFiltering, "2");
  1244. c.supports_filter = true;
  1245. Assert.IsTrue (source.SupportsFiltering, "3");
  1246. }
  1247. [Test]
  1248. public void SupportsAdvancedSorting ()
  1249. {
  1250. BindingListViewPoker c = new BindingListViewPoker ();
  1251. BindingSource source = new BindingSource ();
  1252. // because the default list is a BindingList<object>
  1253. Assert.IsFalse (source.SupportsAdvancedSorting, "1");
  1254. source.DataSource = c;
  1255. // the DataSource is IBindingListView, but
  1256. // SupportsAdvancedSorting is false.
  1257. Assert.IsFalse (source.SupportsAdvancedSorting, "2");
  1258. c.supports_advanced_sorting = true;
  1259. Assert.IsTrue (source.SupportsAdvancedSorting, "3");
  1260. }
  1261. class IBindingListPoker : BindingList<string>, IBindingList {
  1262. public void AddIndex (PropertyDescriptor property)
  1263. {
  1264. }
  1265. public void ApplySort (PropertyDescriptor property, ListSortDirection direction)
  1266. {
  1267. }
  1268. public int Find (PropertyDescriptor property, object key)
  1269. {
  1270. throw new NotImplementedException ();
  1271. }
  1272. public void RemoveIndex (PropertyDescriptor property)
  1273. {
  1274. }
  1275. public void RemoveSort ()
  1276. {
  1277. }
  1278. public bool IsSorted {
  1279. get { throw new NotImplementedException (); }
  1280. }
  1281. public ListSortDirection SortDirection {
  1282. get { throw new NotImplementedException (); }
  1283. }
  1284. public PropertyDescriptor SortProperty {
  1285. get { throw new NotImplementedException (); }
  1286. }
  1287. public bool SupportsChangeNotification {
  1288. get { return supports_change_notification; }
  1289. }
  1290. public bool SupportsSearching {
  1291. get { return supports_searching; }
  1292. }
  1293. public bool SupportsSorting {
  1294. get { return supports_sorting; }
  1295. }
  1296. public bool supports_change_notification;
  1297. public bool supports_searching;
  1298. public bool supports_sorting;
  1299. }
  1300. [Test]
  1301. public void SupportsSearching ()
  1302. {
  1303. IBindingListPoker c = new IBindingListPoker ();
  1304. BindingSource source = new BindingSource ();
  1305. // because the default list is a BindingList<object>
  1306. Assert.IsFalse (source.SupportsSearching, "1");
  1307. source.DataSource = c;
  1308. // the DataSource is IBindingList, but
  1309. // SupportsSearching is false.
  1310. Assert.IsFalse (source.SupportsSearching, "2");
  1311. c.supports_searching = true;
  1312. Console.WriteLine ("set c.supports_searching to {0}, so c.SupportsSearching = {1}",
  1313. c.supports_searching, c.SupportsSearching);
  1314. Assert.IsTrue (source.SupportsSearching, "3");
  1315. }
  1316. [Test]
  1317. public void SupportsSorting ()
  1318. {
  1319. IBindingListPoker c = new IBindingListPoker ();
  1320. BindingSource source = new BindingSource ();
  1321. // because the default list is a BindingList<object>
  1322. Assert.IsFalse (source.SupportsSorting, "1");
  1323. source.DataSource = c;
  1324. // the DataSource is IBindingList, but
  1325. // SupportsSorting is false.
  1326. Assert.IsFalse (source.SupportsSorting, "2");
  1327. c.supports_sorting = true;
  1328. Assert.IsTrue (source.SupportsSorting, "3");
  1329. }
  1330. [Test]
  1331. public void SupportsChangeNotification ()
  1332. {
  1333. IBindingListPoker c = new IBindingListPoker ();
  1334. BindingSource source = new BindingSource ();
  1335. // because the default list is a BindingList<object>
  1336. Assert.IsTrue (source.SupportsChangeNotification, "1");
  1337. source.DataSource = c;
  1338. // the DataSource is IBindingList, but
  1339. // SupportsChangeNotification is false.
  1340. Assert.IsTrue (source.SupportsChangeNotification, "2");
  1341. c.supports_change_notification = true;
  1342. Assert.IsTrue (source.SupportsChangeNotification, "3");
  1343. }
  1344. [Test]
  1345. public void ISupportInitializeNotification ()
  1346. {
  1347. BindingSource source = new BindingSource ();
  1348. List<string> list = new List<string> ();
  1349. bool initialized_handled = false;
  1350. ISupportInitializeNotification inotification = (ISupportInitializeNotification)source;
  1351. inotification.Initialized += delegate { initialized_handled = true; };
  1352. Assert.AreEqual (true, inotification.IsInitialized, "A1");
  1353. Assert.AreEqual (false, initialized_handled, "A2");
  1354. inotification.BeginInit ();
  1355. Assert.AreEqual (false, inotification.IsInitialized, "B1");
  1356. Assert.AreEqual (false, initialized_handled, "B2");
  1357. source.DataSource = list;
  1358. Assert.AreEqual (list, source.DataSource, "C1");
  1359. Assert.AreEqual (false, initialized_handled, "C2");
  1360. inotification.EndInit ();
  1361. Assert.AreEqual (true, inotification.IsInitialized, "D1");
  1362. Assert.AreEqual (true, initialized_handled, "D2");
  1363. // Reset event info
  1364. initialized_handled = false;
  1365. inotification.EndInit ();
  1366. Assert.AreEqual (true, initialized_handled, "E1");
  1367. //
  1368. // Case 2: use a data source that implements ISupportsInitializeNotification
  1369. //
  1370. InitializableObject init_obj = new InitializableObject ();
  1371. init_obj.BeginInit ();
  1372. source.DataSource = null;
  1373. inotification.BeginInit ();
  1374. initialized_handled = false;
  1375. source.DataSource = init_obj;
  1376. Assert.AreEqual (false, inotification.IsInitialized, "G1");
  1377. Assert.AreEqual (false, initialized_handled, "G2");
  1378. Assert.AreEqual (false, init_obj.IsInitialized, "G3");
  1379. Assert.AreEqual (init_obj, source.DataSource, "G4");
  1380. Assert.IsTrue (source.List is BindingList<object>, "G5"); // Default list
  1381. inotification.EndInit ();
  1382. Assert.AreEqual (false, inotification.IsInitialized, "H1");
  1383. Assert.AreEqual (false, initialized_handled, "H2");
  1384. Assert.AreEqual (false, init_obj.IsInitialized, "H3");
  1385. init_obj.EndInit ();
  1386. Assert.AreEqual (true, inotification.IsInitialized, "J1");
  1387. Assert.AreEqual (true, initialized_handled, "J2");
  1388. Assert.AreEqual (true, init_obj.IsInitialized, "J3");
  1389. Assert.IsTrue (source.List is BindingList<InitializableObject>, "K");
  1390. // Call again EndInit on datasource, which should *not* cause a
  1391. // EndInit call in BindingSource, since it is already initialized
  1392. initialized_handled = false;
  1393. init_obj.EndInit ();
  1394. Assert.AreEqual (false, initialized_handled, "L");
  1395. }
  1396. class InitializableObject : ISupportInitializeNotification
  1397. {
  1398. bool is_initialized = true;
  1399. public void BeginInit ()
  1400. {
  1401. is_initialized = false;
  1402. }
  1403. public void EndInit ()
  1404. {
  1405. is_initialized = true;
  1406. if (Initialized != null)
  1407. Initialized (this, EventArgs.Empty);
  1408. }
  1409. public bool IsInitialized {
  1410. get {
  1411. return is_initialized;
  1412. }
  1413. }
  1414. public event EventHandler Initialized;
  1415. }
  1416. //
  1417. // Events section
  1418. //
  1419. int iblist_raised;
  1420. int ilist_raised;
  1421. ListChangedEventArgs iblist_changed_args;
  1422. ListChangedEventArgs ilist_changed_args;
  1423. BindingSource iblist_source;
  1424. BindingSource ilist_source;
  1425. void ResetEventsInfo ()
  1426. {
  1427. iblist_raised = ilist_raised = 0;
  1428. iblist_source = new BindingSource ();
  1429. ilist_source = new BindingSource ();
  1430. iblist_source.ListChanged += delegate (object o, ListChangedEventArgs e)
  1431. {
  1432. iblist_raised++;
  1433. iblist_changed_args = e;
  1434. };
  1435. ilist_source.ListChanged += delegate (object o, ListChangedEventArgs e)
  1436. {
  1437. ilist_raised++;
  1438. ilist_changed_args = e;
  1439. };
  1440. }
  1441. [Test]
  1442. public void ListChanged_DataSourceSet ()
  1443. {
  1444. IBindingList bindinglist = new BindingList<string> ();
  1445. bindinglist.Add ("A");
  1446. IList arraylist = new ArrayList (bindinglist);
  1447. ResetEventsInfo ();
  1448. iblist_source.DataSource = bindinglist;
  1449. ilist_source.DataSource = arraylist;
  1450. Assert.AreEqual (2, iblist_raised, "A1");
  1451. Assert.AreEqual (2, ilist_raised, "A2");
  1452. Assert.AreEqual (ListChangedType.Reset, iblist_changed_args.ListChangedType, "A3");
  1453. Assert.AreEqual (ListChangedType.Reset, ilist_changed_args.ListChangedType, "A4");
  1454. Assert.AreEqual (-1, iblist_changed_args.NewIndex, "A5");
  1455. Assert.AreEqual (-1, ilist_changed_args.NewIndex, "A6");
  1456. }
  1457. [Test]
  1458. public void ListChanged_ItemAdded ()
  1459. {
  1460. IBindingList bindinglist = new BindingList<string> ();
  1461. bindinglist.Add ("A");
  1462. IList arraylist = new ArrayList (bindinglist);
  1463. ResetEventsInfo ();
  1464. iblist_source.DataSource = bindinglist;
  1465. ilist_source.DataSource = arraylist;
  1466. // Clear after setting DataSource generated some info
  1467. iblist_raised = ilist_raised = 0;
  1468. iblist_changed_args = ilist_changed_args = null;
  1469. iblist_source.Add ("B");
  1470. ilist_source.Add ("B");
  1471. Assert.AreEqual (1, iblist_raised, "A1");
  1472. Assert.AreEqual (1, ilist_raised, "A2");
  1473. Assert.AreEqual (ListChangedType.ItemAdded, iblist_changed_args.ListChangedType, "A3");
  1474. Assert.AreEqual (ListChangedType.ItemAdded, il

Large files files are truncated, but you can click here to view the full file