| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2007 Novell, Inc.
- //
- #if NET_2_0
- using System;
- using System.Data;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Windows.Forms;
- using NUnit.Framework;
- using CategoryAttribute = NUnit.Framework.CategoryAttribute;
- namespace MonoTests.System.Windows.Forms.DataBinding {
- [TestFixture]
- public class BindingSourceTest : TestHelper
- {
- [Test]
- public void DefaultDataSource ()
- {
- BindingSource source = new BindingSource ();
- Assert.IsTrue (source.List is BindingList<object>, "1");
- Assert.AreEqual (0, source.List.Count, "2");
- }
- [Test]
- public void DataSource_InitialAddChangingType ()
- {
- BindingSource source = new BindingSource ();
- source.Add ((int)32);
- Assert.IsTrue (source.List is BindingList<int>, "1");
- source = new BindingSource ();
- source.DataSource = new ArrayList ();
- source.Add ((int)32);
- Assert.IsFalse (source.List is BindingList<int>, "2");
- }
- class EmptyEnumerable : IEnumerable {
- class EmptyEnumerator : IEnumerator {
- public object Current {
- get { throw new InvalidOperationException (); }
- }
- public void Reset () {
- // nada
- }
- public bool MoveNext () {
- return false;
- }
- }
- public IEnumerator GetEnumerator () {
- return new EmptyEnumerator ();
- }
- }
- class GenericEnumerable : IEnumerable<int> {
- int length;
- public GenericEnumerable (int length) {
- this.length = length;
- }
- class MyEnumerator : IEnumerator<int> {
- public int count;
- public int index;
- public int Current {
- get { return index; }
- }
- object IEnumerator.Current {
- get { return Current; }
- }
- public void Reset () {
- index = 0;
- }
- public bool MoveNext () {
- if (index++ == count)
- return false;
- else
- return true;
- }
- void IDisposable.Dispose () {
- }
- }
- public IEnumerator<int> GetEnumerator () {
- MyEnumerator e = new MyEnumerator ();
- e.count = length;
- return e;
- }
- IEnumerator IEnumerable.GetEnumerator () {
- return GetEnumerator ();
- }
- }
- class WorkingEnumerable : IEnumerable {
- int length;
- public WorkingEnumerable (int length) {
- this.length = length;
- }
- class MyEnumerator : IEnumerator {
- public int count;
- public int index;
- public object Current {
- get { return index; }
- }
- public void Reset () {
- index = 0;
- }
- public bool MoveNext () {
- if (index++ == count)
- return false;
- else
- return true;
- }
- }
- public IEnumerator GetEnumerator () {
- MyEnumerator e = new MyEnumerator ();
- e.count = length;
- return e;
- }
- }
- [Test]
- public void DataSource_ListRelationship ()
- {
- BindingSource source = new BindingSource ();
- // null
- source.DataSource = null;
- Assert.IsTrue (source.List is BindingList<object>, "1");
- // a non-list object
- source.DataSource = new object ();
- Assert.IsTrue (source.List is BindingList<object>, "2");
- // array instance (value type)
- source.DataSource = new int[32];
- Assert.IsTrue (source.List is int[], "3");
- // an instance array with 0 elements
- source.DataSource = new int[0];
- Assert.IsTrue (source.List is int[], "4");
- // array instance (object type)
- source.DataSource = new string[32];
- Assert.IsTrue (source.List is string[], "5");
- // list type
- source.DataSource = new List<bool>();
- Assert.IsTrue (source.List is List<bool>, "6");
- // an IEnumerable type
- source.DataSource = "hi";
- Assert.IsTrue (source.List is BindingList<char>, "7");
- // an IEnumerable type with 0 items
- source.DataSource = "";
- Assert.IsTrue (source.List is BindingList<char>, "8");
- Assert.AreEqual (0, source.List.Count, "9");
- // a generic enumerable with no elements.
- // even though we can figure out the type
- // through reflection, we shouldn't..
- source.DataSource = new GenericEnumerable (0);
- Console.WriteLine (source.List.GetType());
- Assert.IsTrue (source.List is BindingList<char>, "10");
- Assert.AreEqual (0, source.List.Count, "11");
- // a non-generic IEnumerable type with 0 items
- // this doesn't seem to change the type of the
- // binding source's list, probably because it
- // can't determine the type of the
- // enumerable's elements.
- source.DataSource = new EmptyEnumerable ();
- Assert.IsTrue (source.List is BindingList<char>, "12");
- // an enumerable with some elements
- source.DataSource = new WorkingEnumerable (5);
- Assert.IsTrue (source.List is BindingList<int>, "13");
- Assert.AreEqual (5, source.List.Count, "14");
- // IListSource - returns an array
- source.DataSource = new ListBindingHelperTest.ListSource (true);
- Assert.IsTrue (source.List is Array, "#15");
- Assert.AreEqual (1, source.List.Count, "#16");
- }
- [Test]
- public void Filter ()
- {
- BindingSource source = new BindingSource ();
- DataTable table = new DataTable ();
- string filter = "Name = 'Mono'";
- IBindingListView view;
- table.Columns.Add ("Id", typeof (int));
- table.Columns.Add ("Name", typeof (string));
- table.Rows.Add (0, "Mono");
- table.Rows.Add (1, "Miguel");
- table.Rows.Add (2, "Paolo");
- table.Rows.Add (3, "Mono");
- source.DataSource = table;
- Assert.AreEqual (null, source.Filter, "A1");
- source.Filter = filter;
- view = (IBindingListView)((IListSource)table).GetList ();
- Assert.AreEqual (filter, source.Filter, "B1");
- Assert.AreEqual (filter, view.Filter, "B2");
- Assert.AreEqual (2, view.Count, "B3");
- Assert.AreEqual (2, source.List.Count, "B4");
- source.Filter = String.Empty;
- Assert.AreEqual (String.Empty, source.Filter, "C1");
- Assert.AreEqual (String.Empty, view.Filter, "C2");
- Assert.AreEqual (4, view.Count, "C3");
- Assert.AreEqual (4, source.List.Count, "C4");
- source.DataSource = null;
- Assert.AreEqual (String.Empty, source.Filter, "D1"); // Keep previous value
- filter = "Name = 'Miguel'"; // Apply filter before assigning data source
- source.Filter = filter;
- source.DataSource = table;
- view = (IBindingListView)((IListSource)table).GetList ();
- Assert.AreEqual (filter, source.Filter, "E1");
- Assert.AreEqual (filter, view.Filter, "E2");
- Assert.AreEqual (1, view.Count, "E3");
- Assert.AreEqual (1, source.List.Count, "E4");
- }
- [Test]
- public void Filter_NonBindingListView ()
- {
- BindingSource source = new BindingSource ();
- List<int> list = new List<int> ();
- list.AddRange (new int [] { 0, 1, 2 });
- string filter = "NonExistentColumn = 'A'"; ;
- source.DataSource = list;
- Assert.AreEqual (null, source.Filter, "A1");
- // List<> doesn't implement IBindingListView, but
- // the filter string is saved
- source.Filter = filter;
- Assert.AreEqual (filter, source.Filter, "B1");
- source.Filter = null;
- Assert.AreEqual (null, source.Filter, "C1");
- }
- [Test]
- public void RemoveFilter ()
- {
- BindingSource source = new BindingSource ();
- source.Filter = "Name = 'Something'";
- source.RemoveFilter ();
- Assert.AreEqual (null, source.Filter, "A1");
- }
- [Test]
- public void RemoveSort ()
- {
- BindingSource source = new BindingSource ();
- DataTable table = CreateTable ();
- source.DataSource = table;
- source.Sort = "Name";
- IBindingListView view = (IBindingListView)source.List;
- Assert.AreEqual ("Name", source.Sort, "A1");
- Assert.AreEqual ("Name", view.SortProperty.Name, "A2");
- source.RemoveSort ();
- Assert.AreEqual (null, source.Sort, "B1");
- Assert.AreEqual (null, view.SortProperty, "B2");
- // Non IBindingListView source - No exception, as opposed to what
- // the documentation says
- source.Sort = null;
- source.DataSource = new List<string> ();
- source.RemoveSort ();
- }
- [Test]
- public void ResetItem ()
- {
- BindingSource source = new BindingSource ();
- bool delegate_reached = false;
- int old_index = 5;
- int new_index = 5;
- ListChangedType type = ListChangedType.Reset;
- source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
- delegate_reached = true;
- type = e.ListChangedType;
- old_index = e.OldIndex;
- new_index = e.NewIndex;
- };
- source.ResetItem (0);
- Assert.IsTrue (delegate_reached, "1");
- Assert.AreEqual (-1, old_index, "2");
- Assert.AreEqual (0, new_index, "3");
- Assert.AreEqual (ListChangedType.ItemChanged, type, "3");
- }
- DataTable CreateTable ()
- {
- DataTable table = new DataTable ();
- table.Columns.Add ("Id", typeof (int));
- table.Columns.Add ("Name", typeof (string));
- table.Rows.Add (0, "Mono");
- table.Rows.Add (2, "JPobst");
- table.Rows.Add (1, "Miguel");
- return table;
- }
- [Test]
- public void Sort_IBindingList ()
- {
- BindingSource source = new BindingSource ();
- BindingList<string> list = new BindingList<string> ();
- source.DataSource = list;
- // Implements IBindingList but SupportsSorting is false
- try {
- source.Sort = "Name";
- Assert.Fail ("A1");
- } catch (ArgumentException) {
- }
- }
- [Test]
- public void Sort_IBindingListView ()
- {
- BindingSource source = new BindingSource ();
- DataTable table = CreateTable ();
- //
- // Simple
- //
- source.DataSource = table;
- source.Sort = "Name";
- DataView view = (DataView)((IListSource)table).GetList ();
- Assert.AreEqual ("Name", source.Sort, "A1");
- Assert.AreEqual (ListSortDirection.Ascending, ((IBindingListView) source).SortDirection, "A2");
- Assert.AreEqual (ListSortDirection.Ascending, ((IBindingListView)view).SortDirection, "A3");
- Assert.AreEqual ("Name", ((IBindingListView)source).SortProperty.Name, "A4");
- Assert.AreEqual ("Name", ((IBindingListView)view).SortProperty.Name, "A5");
- Assert.AreEqual (1, ((IBindingListView)view).SortDescriptions.Count, "A6");
- Assert.AreEqual ("Name", ((IBindingListView)view).SortDescriptions [0].PropertyDescriptor.Name, "A7");
- Assert.AreEqual ("JPobst", view [0]["Name"], "A8");
- Assert.AreEqual ("Miguel", view [1]["Name"], "A9");
- Assert.AreEqual ("Mono", view [2]["Name"], "A10");
- //
- // Simple with direction (extra white spaces)
- //
- source.Sort = " Name DESC ";
- //Assert.AreEqual ("Name DESC", source.Sort, "B1");
- Assert.AreEqual (ListSortDirection.Descending, ((IBindingListView)view).SortDirection, "B2");
- Assert.AreEqual ("Name", ((IBindingListView)view).SortProperty.Name, "B3");
- Assert.AreEqual ("Mono", view [0]["Name"], "B4");
- Assert.AreEqual ("Miguel", view [1]["Name"], "B5");
- Assert.AreEqual ("JPobst", view [2]["Name"], "B6");
- //
- // Multiple
- //
- source.Sort = "Name DESC, Id asc";
- ListSortDescriptionCollection desc_coll = ((IBindingListView)view).SortDescriptions;
- Assert.AreEqual ("Name DESC, Id asc", source.Sort, "C1");
- Assert.AreEqual (2, desc_coll.Count, "C2");
- Assert.AreEqual (ListSortDirection.Descending, desc_coll [0].SortDirection, "C3");
- Assert.AreEqual ("Name", desc_coll [0].PropertyDescriptor.Name, "C4");
- Assert.AreEqual (ListSortDirection.Ascending, desc_coll [1].SortDirection, "C5");
- Assert.AreEqual ("Id", desc_coll [1].PropertyDescriptor.Name, "C6");
- }
- [Test]
- public void Sort_NonBindingList ()
- {
- BindingSource source = new BindingSource ();
- List<int> list = new List<int> (new int [] { 0, 1, 2, 3 });
- source.DataSource = list;
- Assert.AreEqual (null, source.Sort, "A1");
- try {
- source.Sort = "Name";
- Assert.Fail ("B1");
- } catch (ArgumentException) {
- }
- source.Sort = String.Empty;
- Assert.AreEqual (String.Empty, source.Sort, "C1");
- }
- [Test]
- public void Sort_Exceptions ()
- {
- BindingSource source = new BindingSource ();
- DataTable table = CreateTable ();
- source.DataSource = table;
- // Non-existant property
- try {
- source.Sort = "Name, DontExist DESC";
- Assert.Fail ("exc1");
- } catch (ArgumentException) {
- }
- // Wrong direction
- try {
- source.Sort = "Name WRONGDIR";
- Assert.Fail ("exc2");
- } catch (ArgumentException) {
- }
- // Wrong format
- try {
- source.Sort = "Name, , Id";
- Assert.Fail ("exc3");
- } catch (ArgumentException) {
- }
- }
- [Test]
- public void Movement ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = new WorkingEnumerable (5);
- int raised = 0;
- source.PositionChanged += delegate (object sender, EventArgs e) { raised ++; };
- Console.WriteLine ("count = {0}", source.Count);
- source.Position = 3;
- Assert.AreEqual (3, source.Position, "1");
- source.MoveFirst ();
- Assert.AreEqual (0, source.Position, "2");
- source.MoveNext ();
- Assert.AreEqual (1, source.Position, "3");
- source.MovePrevious ();
- Assert.AreEqual (0, source.Position, "4");
- source.MoveLast ();
- Assert.AreEqual (4, source.Position, "5");
- Assert.AreEqual (5, raised, "6");
- }
- [Test]
- public void Position ()
- {
- BindingSource source = new BindingSource ();
- CurrencyManager currency_manager = source.CurrencyManager;
- Assert.AreEqual (-1, source.Position, "A1");
- Assert.AreEqual (-1, currency_manager.Position, "A2");
- source.DataSource = new WorkingEnumerable (5);
- int raised = 0;
- int currency_raised = 0;
- source.PositionChanged += delegate (object sender, EventArgs e) { raised ++; };
- currency_manager.PositionChanged += delegate (object sender, EventArgs e) { currency_raised++; };
- Assert.AreEqual (0, source.Position, "B1");
- Assert.AreEqual (0, currency_manager.Position, "B2");
- source.Position = -1;
- Assert.AreEqual (0, source.Position, "C1");
- Assert.AreEqual (0, currency_manager.Position, "C2");
- Assert.AreEqual (0, raised, "C3");
- Assert.AreEqual (0, currency_raised, "C4");
- source.Position = 10;
- Assert.AreEqual (4, source.Position, "D1");
- Assert.AreEqual (4, currency_manager.Position, "D2");
- Assert.AreEqual (1, raised, "D3");
- Assert.AreEqual (1, currency_raised, "D4");
- source.Position = 10;
- Assert.AreEqual (4, source.Position, "E1");
- Assert.AreEqual (1, raised, "E2");
- // Now make some changes in CurrencyManager.Position, which should be visible
- // in BindingSource.Position
- currency_manager.Position = 0;
- Assert.AreEqual (0, source.Position, "F1");
- Assert.AreEqual (0, currency_manager.Position, "F2");
- Assert.AreEqual (2, raised, "F3");
- Assert.AreEqual (2, currency_raised, "F4");
- // Finally an etmpy collection
- source.DataSource = new List<int> ();
- Assert.AreEqual (-1, source.Position, "G1");
- Assert.AreEqual (-1, currency_manager.Position, "G2");
- }
- [Test]
- public void ResetCurrentItem ()
- {
- BindingSource source = new BindingSource ();
- bool delegate_reached = false;
- int old_index = 5;
- int new_index = 5;
- ListChangedType type = ListChangedType.Reset;
- source.DataSource = new WorkingEnumerable (5);
- source.Position = 2;
- source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
- delegate_reached = true;
- type = e.ListChangedType;
- old_index = e.OldIndex;
- new_index = e.NewIndex;
- };
- source.ResetCurrentItem ();
- Assert.IsTrue (delegate_reached, "1");
- Assert.AreEqual (-1, old_index, "2");
- Assert.AreEqual (2, new_index, "3");
- Assert.AreEqual (ListChangedType.ItemChanged, type, "3");
- }
- [Test]
- public void Remove ()
- {
- BindingSource source = new BindingSource ();
- List<string> list = new List<string> ();
- list.Add ("A");
- source.DataSource = list;
- Assert.AreEqual (1, source.List.Count, "1");
- source.Remove ("A");
- Assert.AreEqual (0, list.Count, "2");
- // Different type, - no exception
- source.Remove (7);
- // Fixed size
- try {
- source.DataSource = new int [0];
- source.Remove (7);
- Assert.Fail ("exc1");
- } catch (NotSupportedException) {
- }
- // Read only
- try {
- source.DataSource = Array.AsReadOnly (new int [0]);
- source.Remove (7);
- Assert.Fail ("exc2");
- } catch (NotSupportedException) {
- }
- }
- [Test]
- public void RemoveCurrent ()
- {
- BindingSource source = new BindingSource ();
- List<string> list = new List<string> ();
- list.Add ("A");
- list.Add ("B");
- list.Add ("C");
- source.DataSource = list;
- source.Position = 1;
- Assert.AreEqual (1, source.Position, "A1");
- Assert.AreEqual ("B", source.Current, "A2");
- source.RemoveCurrent ();
- Assert.AreEqual (1, source.Position, "B1");
- Assert.AreEqual ("C", source.Current, "B2");
- Assert.AreEqual (2, source.Count, "B3");
- Assert.AreEqual ("A", source [0], "B4");
- Assert.AreEqual ("C", source [1], "B5");
- // Position is -1, since there are no items
- source.Clear ();
- try {
- source.RemoveCurrent ();
- Assert.Fail ("exc1");
- } catch (InvalidOperationException) {
- }
- source.DataSource = new int [1];
- try {
- source.RemoveCurrent ();
- Assert.Fail ("exc2");
- } catch (InvalidOperationException) {
- }
- }
- [Test]
- public void ResetBindings ()
- {
- BindingSource source;
- int event_count = 0;
- ListChangedType[] types = new ListChangedType[2];
- int[] old_index = new int[2];
- int[] new_index = new int[2];
- source = new BindingSource ();
- source.ListChanged += delegate (object sender, ListChangedEventArgs e) {
- types[event_count] = e.ListChangedType;
- old_index[event_count] = e.OldIndex;
- new_index[event_count] = e.NewIndex;
- event_count ++;
- };
- event_count = 0;
- source.ResetBindings (false);
- Assert.AreEqual (1, event_count, "1");
- Assert.AreEqual (ListChangedType.Reset, types[0], "2");
- Assert.AreEqual (-1, old_index[0], "3");
- Assert.AreEqual (-1, new_index[0], "4");
- event_count = 0;
- source.ResetBindings (true);
- Assert.AreEqual (2, event_count, "5");
- Assert.AreEqual (ListChangedType.PropertyDescriptorChanged, types[0], "6");
- Assert.AreEqual (0, old_index[0], "7");
- Assert.AreEqual (0, new_index[0], "8");
- Assert.AreEqual (ListChangedType.Reset, types[1], "9");
- Assert.AreEqual (-1, old_index[1], "10");
- Assert.AreEqual (-1, new_index[1], "11");
- }
- [Test]
- public void AllowEdit ()
- {
- BindingSource source = new BindingSource ();
- Assert.IsTrue (source.AllowEdit, "1");
- source.DataSource = "";
- Assert.IsTrue (source.AllowEdit, "2");
- source.DataSource = new int[10];
- Assert.IsTrue (source.AllowEdit, "3");
- source.DataSource = new WorkingEnumerable (5);
- Assert.IsTrue (source.AllowEdit, "4");
- ArrayList al = new ArrayList ();
- al.Add (5);
- source.DataSource = al;
- Assert.IsTrue (source.AllowEdit, "5");
- source.DataSource = ArrayList.ReadOnly (al);
- Assert.IsFalse (source.AllowEdit, "6");
- }
- [Test]
- public void AllowRemove ()
- {
- BindingSource source = new BindingSource ();
- Assert.IsTrue (source.AllowRemove, "1");
- source.DataSource = "";
- Assert.IsTrue (source.AllowRemove, "2");
- source.DataSource = new ArrayList ();
- Assert.IsTrue (source.AllowRemove, "3");
- source.DataSource = new int[10];
- Assert.IsFalse (source.AllowRemove, "4");
- source.DataSource = new WorkingEnumerable (5);
- Assert.IsTrue (source.AllowRemove, "5");
- }
- [Test]
- public void DataMember_ListRelationship ()
- {
- ListView lv = new ListView ();
- BindingSource source = new BindingSource ();
- // Empty IEnumerable, that also implements IList
- source.DataSource = lv.Items;
- source.DataMember = "Text";
- Assert.IsTrue (source.List is BindingList<string>, "1");
- Assert.AreEqual (0, source.List.Count, "2");
- }
- [Test]
- public void DataMemberNull ()
- {
- BindingSource source = new BindingSource ();
- Assert.AreEqual ("", source.DataMember, "1");
- source.DataMember = null;
- Assert.AreEqual ("", source.DataMember, "2");
- }
- [Test]
- public void DataSourceChanged ()
- {
- ArrayList list = new ArrayList ();
- BindingSource source = new BindingSource ();
- bool event_raised = false;
- source.DataSourceChanged += delegate (object sender, EventArgs e) {
- event_raised = true;
- };
- source.DataSource = list;
- Assert.IsTrue (event_raised, "1");
- event_raised = false;
- source.DataSource = list;
- Assert.IsFalse (event_raised, "2");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))] // DataMember property 'hi' cannot be found on the DataSource.
- public void DataMemberArgumentException ()
- {
- ArrayList list = new ArrayList ();
- BindingSource source = new BindingSource ();
- source.DataSource = list;
- source.DataMember = "hi";
- }
- [Test]
- public void DataMemberBeforeDataSource ()
- {
- ArrayList list = new ArrayList ();
- BindingSource source = new BindingSource ();
- source.DataMember = "hi";
- Assert.AreEqual ("hi", source.DataMember, "1");
- source.DataSource = list;
- Assert.AreEqual ("", source.DataMember, "2");
- }
- [Test]
- public void DataSourceAssignToDefaultType()
- {
- BindingSource source = new BindingSource ();
- source.DataMember = "hi";
- Assert.AreEqual ("hi", source.DataMember, "1");
- Assert.IsTrue (source.List is BindingList<object>, "2");
- source.DataSource = new BindingList<object>();
- Assert.AreEqual ("", source.DataMember, "3");
- }
- [Test]
- public void DataSourceSetType ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = typeof (DateTime);
- Assert.IsTrue (source.List is BindingList<DateTime>, "A1");
- Assert.AreEqual (0, source.List.Count, "A2");
- Assert.AreEqual (typeof (DateTime), source.DataSource);
- }
- [Test]
- public void DataMemberChanged ()
- {
- ArrayList list = new ArrayList ();
- BindingSource source = new BindingSource ();
- bool event_raised = false;
- list.Add ("hi"); // make the type System.String
- source.DataMemberChanged += delegate (object sender, EventArgs e) {
- event_raised = true;
- };
- source.DataSource = list;
- source.DataMember = "Length";
- Assert.IsTrue (event_raised, "1");
- event_raised = false;
- source.DataMember = "Length";
- Assert.IsFalse (event_raised, "2");
- }
- [Test]
- public void DataMemberNullDataSource ()
- {
- BindingSource source = new BindingSource ();
- source.Add ("hellou");
- source.DataMember = "SomeProperty"; // Should reset the list, even if data source is null
- Assert.IsTrue (source.List is BindingList<object>, "A1");
- Assert.AreEqual (0, source.List.Count, "A2");
- }
- [Test]
- public void SuppliedDataSource ()
- {
- List<string> list = new List<string>();
- BindingSource source;
- source = new BindingSource (list, "");
- Assert.IsTrue (source.List is List<string>, "1");
- source.DataMember = "Length";
- Assert.IsTrue (source.List is BindingList<int>, "2");
- source = new BindingSource (list, "Length");
- Assert.IsTrue (source.List is BindingList<int>, "3");
- }
- [Test]
- public void DataSourceMember_set ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = new List<string>();
- source.DataMember = "Length";
- Assert.IsNotNull (source.CurrencyManager, "1");
- source.DataSource = new List<string>();
- Assert.AreEqual ("Length", source.DataMember, "2");
- Assert.IsNotNull (source.CurrencyManager, "3");
- source.DataSource = new List<string[]>();
- Assert.AreEqual ("Length", source.DataMember, "4");
- Assert.IsNotNull (source.CurrencyManager, "5");
- }
- [Test]
- public void DataSourceMemberChangedEvents ()
- {
- BindingSource source = new BindingSource ();
- bool data_source_changed = false;
- bool data_member_changed = false;
- source.DataSourceChanged += delegate (object sender, EventArgs e) {
- data_source_changed = true;
- };
- source.DataMemberChanged += delegate (object sender, EventArgs e) {
- data_member_changed = true;
- };
- data_source_changed = false;
- data_member_changed = false;
- source.DataSource = new List<string>();
- Assert.IsTrue (data_source_changed, "1");
- Assert.IsFalse (data_member_changed, "2");
- data_source_changed = false;
- data_member_changed = false;
- source.DataMember = "Length";
- Assert.IsFalse (data_source_changed, "3");
- Assert.IsTrue (data_member_changed, "4");
- }
- [Test]
- public void IsBindingSuspended ()
- {
- BindingSource source = new BindingSource ();
- CurrencyManager currency_manager = source.CurrencyManager;
- source.DataSource = new object [1];
- source.SuspendBinding ();
- Assert.AreEqual (true, source.IsBindingSuspended, "A1");
- Assert.AreEqual (true, currency_manager.IsBindingSuspended, "A2");
- source.ResumeBinding ();
- Assert.AreEqual (false, source.IsBindingSuspended, "B1");
- Assert.AreEqual (false, currency_manager.IsBindingSuspended, "B2");
- // Changes made to CurrencyManager should be visible in BindingSource
- currency_manager.SuspendBinding ();
- Assert.AreEqual (true, source.IsBindingSuspended, "C1");
- Assert.AreEqual (true, currency_manager.IsBindingSuspended, "C2");
- currency_manager.ResumeBinding ();
- Assert.AreEqual (false, source.IsBindingSuspended, "D1");
- Assert.AreEqual (false, currency_manager.IsBindingSuspended, "D2");
- }
- [Test]
- public void Add ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = new List<string> ();
- source.Add ("A");
- Assert.AreEqual (1, source.List.Count, "2");
- // Different item type
- try {
- source.Add (4);
- Assert.Fail ("exc1");
- } catch (InvalidOperationException) {
- }
- // FixedSize
- try {
- source.DataSource = new int [0];
- source.Add (7);
- Assert.Fail ("exc2");
- } catch (NotSupportedException) {
- }
- // ReadOnly
- try {
- source.DataSource = Array.AsReadOnly (new int [0]);
- source.Add (7);
- Assert.Fail ("exc3");
- } catch (NotSupportedException) {
- }
- }
- [Test]
- public void Add_NullDataSource ()
- {
- BindingSource source = new BindingSource ();
- source.Add ("A");
- Assert.AreEqual (1, source.List.Count, "1");
- Assert.IsTrue (source.List is BindingList<string>, "2");
- Assert.IsNull (source.DataSource, "3");
- source = new BindingSource ();
- source.Add (null);
- Assert.IsTrue (source.List is BindingList<object>, "4");
- Assert.AreEqual (1, source.List.Count, "5");
- }
- [Test]
- public void AddNew ()
- {
- BindingSource source = new BindingSource ();
- source.AddNew ();
- Assert.AreEqual (1, source.Count, "1");
- }
- [Test]
- public void AddNew_NonBindingList ()
- {
- IList list = new List<object> ();
- BindingSource source = new BindingSource ();
- source.DataSource = list;
- Assert.IsTrue (source.List is List<object>, "1");
- source.AddNew ();
- Assert.AreEqual (1, source.Count, "2");
- }
- [Test]
- public void ApplySort ()
- {
- BindingSource source = new BindingSource ();
- DataTable table = CreateTable ();
- source.DataSource = table;
- IBindingListView source_view = ((IBindingListView)source);
- IBindingListView view = ((IBindingListView)source.List);
- PropertyDescriptor property = source.GetItemProperties (null) ["Name"];
- source_view.ApplySort (property, ListSortDirection.Ascending);
- Assert.AreEqual (property, view.SortProperty, "A1");
- // Non IBindingList source - Passing an invalid property
- // but the method is not called since source is not of the required type
- source.DataSource = new List<string> ();
- try {
- source_view.ApplySort (property, ListSortDirection.Ascending);
- Assert.Fail ("B1");
- } catch (NotSupportedException) {
- }
- }
- [Test]
- public void AllowNew_getter ()
- {
- BindingSource source = new BindingSource ();
- // true because the default datasource is BindingList<object>
- Assert.IsTrue (source.AllowNew, "1");
- source.DataSource = new object[10];
- // fixed size
- Assert.IsFalse (source.AllowNew, "2");
- source.DataSource = new BindingList<string>();
- // no default ctor
- Assert.IsFalse (source.AllowNew, "3");
- }
- [Test]
- public void AllowNew ()
- {
- BindingSource source = new BindingSource ();
- source.AllowNew = false;
- Assert.IsFalse (source.AllowNew, "1");
- source.ResetAllowNew ();
- Assert.IsTrue (source.AllowNew, "2");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- // "AllowNew can only be set to true on an
- // IBindingList or on a read-write list with a default
- // public constructor."
- public void AllowNew_FixedSize ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = new object[10];
- source.AllowNew = true;
- }
- #if false
- // According to the MS docs, this should fail with a MissingMethodException
- [Test]
- public void AllowNew_NoDefaultCtor ()
- {
- List<string> s = new List<string>();
- s.Add ("hi");
- BindingSource source = new BindingSource ();
- source.DataSource = s;
- source.AllowNew = true;
- Assert.Fail ();
- }
- #endif
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- // "Item cannot be added to a read-only or fixed-size list."
- public void AddNew_BindingListWithoutAllowNew ()
- {
- BindingList<int> l = new BindingList<int>();
- l.AllowNew = false;
- BindingSource source = new BindingSource ();
- source.DataSource = l;
- source.AddNew ();
- Assert.AreEqual (1, source.Count, "1");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- // "Item cannot be added to a read-only or fixed-size list."
- public void AddNew_FixedSize ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = new int[5];
- object o = source.AddNew ();
- Assert.IsTrue (o is int, "1");
- Assert.AreEqual (6, source.Count, "2");
- }
- class ReadOnlyList : List<int>, IList {
- public int Add (object value) {
- throw new Exception ();
- }
- public bool Contains (object value) {
- throw new Exception ();
- }
- public int IndexOf (object value) {
- throw new Exception ();
- }
- public void Insert (int index, object value) {
- throw new Exception ();
- }
- public void Remove (object value) {
- throw new Exception ();
- }
- public bool IsFixedSize {
- get { return false; }
- }
- public bool IsReadOnly {
- get { return true; }
- }
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- // "Item cannot be added to a read-only or fixed-size list."
- public void AddNew_ReadOnly ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = new ReadOnlyList ();
- object o = source.AddNew ();
-
- TestHelper.RemoveWarning (o);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- // "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."
- public void AddNew_Invalid ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = new List<string>();
- object o = source.AddNew ();
-
- TestHelper.RemoveWarning (o);
- }
- [Test]
- public void AddingNew ()
- {
- // Need to use a class missing a default .ctor
- BindingSource source = new BindingSource ();
- List<DateTime> list = new List<DateTime> ();
- source.DataSource = list;
- Assert.AreEqual (false, source.AllowNew, "A1");
- source.AllowNew = true;
- source.AddingNew += delegate (object o, AddingNewEventArgs args)
- {
- args.NewObject = DateTime.Now;
- };
- source.AddNew ();
- Assert.AreEqual (1, source.Count, "A1");
- }
- [Test]
- public void AddingNew_Exceptions ()
- {
- BindingSource source = new BindingSource ();
- List<DateTime> list = new List<DateTime> ();
- source.DataSource = list;
- Assert.AreEqual (false, source.AllowNew, "A1");
- source.AllowNew = true;
- // No handler for AddingNew
- try {
- source.AddNew ();
- Assert.Fail ("exc1");
- } catch (InvalidOperationException) {
- }
- // Adding new handled, but AddingNew is false
- source.AllowNew = false;
- source.AddingNew += delegate (object o, AddingNewEventArgs args)
- {
- args.NewObject = DateTime.Now;
- };
- try {
- source.AddNew ();
- Assert.Fail ("exc2");
- } catch (InvalidOperationException) {
- }
- // Wrong type
- source = new BindingSource ();
- source.DataSource = new List<string> ();
- source.AllowNew = true;
- source.AddingNew += delegate (object o, AddingNewEventArgs args)
- {
- args.NewObject = 3.1416;
- };
- try {
- source.AddNew ();
- Assert.Fail ("exc3");
- } catch (InvalidOperationException) {
- }
- // Null value
- source = new BindingSource ();
- source.DataSource = new List<string> ();
- source.AllowNew = true;
- source.AddingNew += delegate (object o, AddingNewEventArgs args)
- {
- args.NewObject = null;
- };
- try {
- source.AddNew ();
- Assert.Fail ("exc4");
- } catch (InvalidOperationException) {
- }
- }
- [Test]
- public void BindingSuspended1 ()
- {
- // Empty collection as datasource means CurrencyManager will remain
- // as suspended
- BindingSource source = new BindingSource ();
- source.DataSource = new List<string>();
- Assert.IsTrue (source.IsBindingSuspended, "1");
- source.SuspendBinding ();
- Assert.IsTrue (source.IsBindingSuspended, "2");
- source.ResumeBinding ();
- Assert.IsTrue (source.IsBindingSuspended, "3");
- source.ResumeBinding ();
- Assert.IsTrue (source.IsBindingSuspended, "4");
- }
- [Test]
- public void ICancelAddNew ()
- {
- BindingSource source = new BindingSource ();
- source.DataSource = new List<string> ();
- source.AddingNew += delegate (object o, AddingNewEventArgs args) { args.NewObject = "A"; };
- source.AllowNew = true;
- // CancelNew
- source.AddNew ();
- Assert.AreEqual (1, source.Count, "A1");
- Assert.AreEqual ("A", source [0], "A2");
- ((ICancelAddNew)source).CancelNew (0);
- Assert.AreEqual (0, source.Count, "A3");
- // EndNew
- source.AddNew ();
- ((ICancelAddNew)source).EndNew (0);
- ((ICancelAddNew)source).CancelNew (0);
- Assert.AreEqual (1, source.Count, "B1");
- Assert.AreEqual ("A", source [0], "B2");
- //
- // Access operations are suppoused to automatically
- // call EndNew, but that only happens with AddNew
- //
- // AddNew
- source.Clear ();
- source.AddNew ();
- source.AddNew ();
- ((ICancelAddNew)source).CancelNew (0);
- Assert.AreEqual (2, source.Count, "C1");
- Assert.AreEqual ("A", source [0], "C2");
- Assert.AreEqual ("A", source [1], "C3");
- // Add - Does not call EndNew
- source.Clear ();
- source.AddNew ();
- source.Add ("B");
- ((ICancelAddNew)source).CancelNew (0);
- Assert.AreEqual (1, source.Count, "D1");
- Assert.AreEqual ("B", source [0], "D2");
- // Remove - Does not neither
- source.Clear ();
- source.AddNew ();
- source.Add ("B");
- source.Remove ("B");
- ((ICancelAddNew)source).CancelNew (0);
- Assert.AreEqual (0, source.Count, "E1");
- // Wrong index param passed to CancelNew
- source.Clear ();
- source.AddNew ();
- source.Add ("B");
- ((ICancelAddNew)source).CancelNew (1); // Should pass 0
- Assert.AreEqual (2, source.Count, "F1");
- // Multiple pending items - Only takes into account the last one
- source.Clear ();
- source.AddNew ();
- source.AddNew ();
- ((ICancelAddNew)source).CancelNew (1);
- ((ICancelAddNew)source).CancelNew (0);
- Assert.AreEqual (1, source.Count, "G1");
- // Bug?
- source.Clear ();
- source.AddNew ();
- source.Add ("B");
- source.RemoveAt (0); // Added with AddNew
- ((ICancelAddNew)source).CancelNew (0); // Removed item that wasn't added with AddNew
- Assert.AreEqual (0, source.Count, "H1");
- }
- [Test]
- public void Clear ()
- {
- BindingSource source = new BindingSource ();
- List<string> list = new List<string> ();
- list.Add ("A");
- list.Add ("B");
- source.DataSource = list;
- source.Clear ();
- Assert.AreEqual (0, source.List.Count, "1");
- Assert.IsTrue (source.List is List<string>, "2");
- // Exception only for ReadOnly, not for fixed size
- source.DataSource = new int [0];
- source.Clear ();
- try {
- source.DataSource = Array.AsReadOnly (new int [0]);
- source.Clear ();
- Assert.Fail ("exc1");
- } catch (NotSupportedException) {
- }
- }
- [Test]
- public void Clear_NullDataSource ()
- {
- BindingSource source = new BindingSource ();
- source.Add ("A");
- Assert.AreEqual (1, source.List.Count, "1");
- Assert.IsTrue (source.List is BindingList<string>, "2");
- Assert.IsNull (source.DataSource, "3");
- source.Clear ();
- Assert.AreEqual (0, source.List.Count, "4");
- Assert.IsTrue (source.List is BindingList<string>, "5");
- // Change list item type after clearing
- source.Add (7);
- Assert.AreEqual (1, source.List.Count, "6");
- Assert.IsTrue (source.List is BindingList<int>, "7");
- }
- [Test]
- public void CurrencyManager ()
- {
- BindingSource source = new BindingSource ();
- CurrencyManager curr_manager;
- //
- // Null data source
- //
- curr_manager = source.CurrencyManager;
- Assert.IsTrue (curr_manager != null, "A1");
- Assert.IsTrue (curr_manager.List != null, "A2");
- Assert.IsTrue (curr_manager.List is BindingSource, "A3");
- Assert.AreEqual (0, curr_manager.List.Count, "A4");
- Assert.AreEqual (0, curr_manager.Count, "A5");
- Assert.AreEqual (-1, curr_manager.Position, "A6");
- Assert.IsTrue (curr_manager.Bindings != null, "A7");
- Assert.AreEqual (0, curr_manager.Bindings.Count, "A8");
- Assert.AreEqual (source, curr_manager.List, "A9");
- //
- // Non null data source
- //
- List<string> list = new List<string> ();
- list.Add ("A");
- list.Add ("B");
- source.DataSource = list;
- curr_manager = source.CurrencyManager;
- Assert.IsTrue (curr_manager != null, "B1");
- Assert.IsTrue (curr_manager.List != null, "B2");
- Assert.IsTrue (curr_manager.List is BindingSource, "B3");
- Assert.AreEqual (2, curr_manager.List.Count, "B4");
- Assert.AreEqual (2, curr_manager.Count, "B5");
- Assert.AreEqual (0, curr_manager.Position, "B6");
- Assert.IsTrue (curr_manager.Bindings != null, "B7");
- Assert.AreEqual (0, curr_manager.Bindings.Count, "B8");
- Assert.AreEqual (source, curr_manager.List, "B9");
- curr_manager.Position = source.Count - 1;
- Assert.AreEqual (1, curr_manager.Position, "C1");
- Assert.AreEqual ("B", curr_manager.Current, "C2");
- Assert.AreEqual (1, source.Position, "C3");
- Assert.AreEqual ("B", source.Current, "C4");
- }
- [Test]
- public void GetRelatedCurrencyManagerList ()
- {
- ListView lv = new ListView ();
- lv.Columns.Add ("A");
- BindingSource source = new BindingSource ();
- source.DataSource = lv;
- CurrencyManager cm = source.GetRelatedCurrencyManager ("Columns");
- BindingSource related_source = (BindingSource)cm.List;
- Assert.AreEqual (1, cm.Count, "A1");
- Assert.AreEqual (1, related_source.Count, "A2");
- Assert.AreEqual ("Columns", related_source.DataMember, "A3");
- Assert.AreSame (source, related_source.DataSource, "A4");
- Assert.IsTrue (related_source.List is ListView.ColumnHeaderCollection, "A5");
- Assert.AreSame (cm, source.GetRelatedCurrencyManager ("Columns"), "A6");
- // A path string returns null
- cm = source.GetRelatedCurrencyManager ("Columns.Count");
- Assert.IsNull (cm, "B1");
- // String.Empty and null
- Assert.AreSame (source.CurrencyManager, source.GetRelatedCurrencyManager (String.Empty), "C1");
- Assert.AreSame (source.CurrencyManager, source.GetRelatedCurrencyManager (null), "C2");
- }
- [Test]
- public void GetRelatedCurrencyManagerObject ()
- {
- BindingSource source = new BindingSource ();
- ListViewItem item = new ListViewItem ();
- source.DataSource = item;
- CurrencyManager font_cm = source.GetRelatedCurrencyManager ("Font");
- CurrencyManager name_cm = source.GetRelatedCurrencyManager ("Font.Name");
- Assert.IsNull (name_cm, "A1");
- }
- class BindingListViewPoker : BindingList<string>, IBindingListView
- {
- public bool supports_filter;
- public bool supports_advanced_sorting;
- public string Filter {
- get { return ""; }
- set { }
- }
- public ListSortDescriptionCollection SortDescriptions {
- get { return null; }
- }
- public bool SupportsAdvancedSorting {
- get { return supports_advanced_sorting; }
- }
- public bool SupportsFiltering {
- get { return supports_filter; }
- }
- public void ApplySort (ListSortDescriptionCollection sorts)
- {
- }
- public void RemoveFilter ()
- {
- }
- }
- [Test]
- public void SupportsFilter ()
- {
- BindingListViewPoker c = new BindingListViewPoker ();
- BindingSource source = new BindingSource ();
- // because the default list is a BindingList<object>
- Assert.IsFalse (source.SupportsFiltering, "1");
- source.DataSource = c;
- // the DataSource is IBindingListView, but
- // SupportsFilter is false.
- Assert.IsFalse (source.SupportsFiltering, "2");
- c.supports_filter = true;
- Assert.IsTrue (source.SupportsFiltering, "3");
- }
- [Test]
- public void SupportsAdvancedSorting ()
- {
- BindingListViewPoker c = new BindingListViewPoker ();
- BindingSource source = new BindingSource ();
- // because the default list is a BindingList<object>
- Assert.IsFalse (source.SupportsAdvancedSorting, "1");
- source.DataSource = c;
- // the DataSource is IBindingListView, but
- // SupportsAdvancedSorting is false.
- Assert.IsFalse (source.SupportsAdvancedSorting, "2");
- c.supports_advanced_sorting = true;
- Assert.IsTrue (source.SupportsAdvancedSorting, "3");
- }
- class IBindingListPoker : BindingList<string>, IBindingList {
- public void AddIndex (PropertyDescriptor property)
- {
- }
- public void ApplySort (PropertyDescriptor property, ListSortDirection direction)
- {
- }
- public int Find (PropertyDescriptor property, object key)
- {
- throw new NotImplementedException ();
- }
- public void RemoveIndex (PropertyDescriptor property)
- {
- }
- public void RemoveSort ()
- {
- }
- public bool IsSorted {
- get { throw new NotImplementedException (); }
- }
- public ListSortDirection SortDirection {
- get { throw new NotImplementedException (); }
- }
- public PropertyDescriptor SortProperty {
- get { throw new NotImplementedException (); }
- }
- public bool SupportsChangeNotification {
- get { return supports_change_notification; }
- }
- public bool SupportsSearching {
- get { return supports_searching; }
- }
- public bool SupportsSorting {
- get { return supports_sorting; }
- }
- public bool supports_change_notification;
- public bool supports_searching;
- public bool supports_sorting;
- }
- [Test]
- public void SupportsSearching ()
- {
- IBindingListPoker c = new IBindingListPoker ();
- BindingSource source = new BindingSource ();
- // because the default list is a BindingList<object>
- Assert.IsFalse (source.SupportsSearching, "1");
- source.DataSource = c;
- // the DataSource is IBindingList, but
- // SupportsSearching is false.
- Assert.IsFalse (source.SupportsSearching, "2");
- c.supports_searching = true;
- Console.WriteLine ("set c.supports_searching to {0}, so c.SupportsSearching = {1}",
- c.supports_searching, c.SupportsSearching);
- Assert.IsTrue (source.SupportsSearching, "3");
- }
- [Test]
- public void SupportsSorting ()
- {
- IBindingListPoker c = new IBindingListPoker ();
- BindingSource source = new BindingSource ();
- // because the default list is a BindingList<object>
- Assert.IsFalse (source.SupportsSorting, "1");
- source.DataSource = c;
- // the DataSource is IBindingList, but
- // SupportsSorting is false.
- Assert.IsFalse (source.SupportsSorting, "2");
- c.supports_sorting = true;
- Assert.IsTrue (source.SupportsSorting, "3");
- }
- [Test]
- public void SupportsChangeNotification ()
- {
- IBindingListPoker c = new IBindingListPoker ();
- BindingSource source = new BindingSource ();
- // because the default list is a BindingList<object>
- Assert.IsTrue (source.SupportsChangeNotification, "1");
- source.DataSource = c;
- // the DataSource is IBindingList, but
- // SupportsChangeNotification is false.
- Assert.IsTrue (source.SupportsChangeNotification, "2");
- c.supports_change_notification = true;
- Assert.IsTrue (source.SupportsChangeNotification, "3");
- }
- [Test]
- public void ISupportInitializeNotification ()
- {
- BindingSource source = new BindingSource ();
- List<string> list = new List<string> ();
- bool initialized_handled = false;
- ISupportInitializeNotification inotification = (ISupportInitializeNotification)source;
- inotification.Initialized += delegate { initialized_handled = true; };
- Assert.AreEqual (true, inotification.IsInitialized, "A1");
- Assert.AreEqual (false, initialized_handled, "A2");
- inotification.BeginInit ();
- Assert.AreEqual (false, inotification.IsInitialized, "B1");
- Assert.AreEqual (false, initialized_handled, "B2");
- source.DataSource = list;
- Assert.AreEqual (list, source.DataSource, "C1");
- Assert.AreEqual (false, initialized_handled, "C2");
- inotification.EndInit ();
- Assert.AreEqual (true, inotification.IsInitialized, "D1");
- Assert.AreEqual (true, initialized_handled, "D2");
- // Reset event info
- initialized_handled = false;
- inotification.EndInit ();
- Assert.AreEqual (true, initialized_handled, "E1");
- //
- // Case 2: use a data source that implements ISupportsInitializeNotification
- //
- InitializableObject init_obj = new InitializableObject ();
- init_obj.BeginInit ();
- source.DataSource = null;
- inotification.BeginInit ();
- initialized_handled = false;
- source.DataSource = init_obj;
- Assert.AreEqual (false, inotification.IsInitialized, "G1");
- Assert.AreEqual (false, initialized_handled, "G2");
- Assert.AreEqual (false, init_obj.IsInitialized, "G3");
- Assert.AreEqual (init_obj, source.DataSource, "G4");
- Assert.IsTrue (source.List is BindingList<object>, "G5"); // Default list
- inotification.EndInit ();
- Assert.AreEqual (false, inotification.IsInitialized, "H1");
- Assert.AreEqual (false, initialized_handled, "H2");
- Assert.AreEqual (false, init_obj.IsInitialized, "H3");
- init_obj.EndInit ();
- Assert.AreEqual (true, inotification.IsInitialized, "J1");
- Assert.AreEqual (true, initialized_handled, "J2");
- Assert.AreEqual (true, init_obj.IsInitialized, "J3");
- Assert.IsTrue (source.List is BindingList<InitializableObject>, "K");
- // Call again EndInit on datasource, which should *not* cause a
- // EndInit call in BindingSource, since it is already initialized
- initialized_handled = false;
- init_obj.EndInit ();
- Assert.AreEqual (false, initialized_handled, "L");
- }
- class InitializableObject : ISupportInitializeNotification
- {
- bool is_initialized = true;
- public void BeginInit ()
- {
- is_initialized = false;
- }
- public void EndInit ()
- {
- is_initialized = true;
- if (Initialized != null)
- Initialized (this, EventArgs.Empty);
- }
- public bool IsInitialized {
- get {
- return is_initialized;
- }
- }
- public event EventHandler Initialized;
- }
- //
- // Events section
- //
- int iblist_raised;
- int ilist_raised;
- ListChangedEventArgs iblist_changed_args;
- ListChangedEventArgs ilist_changed_args;
- BindingSource iblist_source;
- BindingSource ilist_source;
- void ResetEventsInfo ()
- {
- iblist_raised = ilist_raised = 0;
- iblist_source = new BindingSource ();
- ilist_source = new BindingSource ();
- iblist_source.ListChanged += delegate (object o, ListChangedEventArgs e)
- {
- iblist_raised++;
- iblist_changed_args = e;
- };
- ilist_source.ListChanged += delegate (object o, ListChangedEventArgs e)
- {
- ilist_raised++;
- ilist_changed_args = e;
- };
- }
- [Test]
- public void ListChanged_DataSourceSet ()
- {
- IBindingList bindinglist = new BindingList<string> ();
- bindinglist.Add ("A");
- IList arraylist = new ArrayList (bindinglist);
- ResetEventsInfo ();
- iblist_source.DataSource = bindinglist;
- ilist_source.DataSource = arraylist;
- Assert.AreEqual (2, iblist_raised, "A1");
- Assert.AreEqual (2, ilist_raised, "A2");
- Assert.AreEqual (ListChangedType.Reset, iblist_changed_args.ListChangedType, "A3");
- Assert.AreEqual (ListChangedType.Reset, ilist_changed_args.ListChangedType, "A4");
- Assert.AreEqual (-1, iblist_changed_args.NewIndex, "A5");
- Assert.AreEqual (-1, ilist_changed_args.NewIndex, "A6");
- }
- [Test]
- public void ListChanged_ItemAdded ()
- {
- IBindingList bindinglist = new BindingList<string> ();
- bindinglist.Add ("A");
- IList arraylist = new ArrayList (bindinglist);
- ResetEventsInfo ();
- iblist_source.DataSource = bindinglist;
- ilist_source.DataSource = arraylist;
- // Clear after setting DataSource generated some info
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- iblist_source.Add ("B");
- ilist_source.Add ("B");
- Assert.AreEqual (1, iblist_raised, "A1");
- Assert.AreEqual (1, ilist_raised, "A2");
- Assert.AreEqual (ListChangedType.ItemAdded, iblist_changed_args.ListChangedType, "A3");
- Assert.AreEqual (ListChangedType.ItemAdded, ilist_changed_args.ListChangedType, "A4");
- Assert.AreEqual (1, iblist_changed_args.NewIndex, "A5");
- Assert.AreEqual (1, ilist_changed_args.NewIndex, "A6");
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- iblist_source.Insert (0, "C");
- ilist_source.Insert (0, "C");
- Assert.AreEqual (1, iblist_raised, "B1");
- Assert.AreEqual (1, ilist_raised, "B2");
- Assert.AreEqual (ListChangedType.ItemAdded, iblist_changed_args.ListChangedType, "B3");
- Assert.AreEqual (ListChangedType.ItemAdded, ilist_changed_args.ListChangedType, "B4");
- Assert.AreEqual (0, iblist_changed_args.NewIndex, "B5");
- Assert.AreEqual (0, ilist_changed_args.NewIndex, "B6");
- // AddNew
- iblist_source.AddingNew += delegate (object o, AddingNewEventArgs e) { e.NewObject = "Z"; };
- ilist_source.AddingNew += delegate (object o, AddingNewEventArgs e) { e.NewObject = "Z"; };
- iblist_source.AllowNew = true;
- ilist_source.AllowNew = true;
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- iblist_source.AddNew ();
- ilist_source.AddNew ();
- Assert.AreEqual (1, iblist_raised, "C1");
- Assert.AreEqual (1, ilist_raised, "C2");
- Assert.AreEqual (ListChangedType.ItemAdded, iblist_changed_args.ListChangedType, "C3");
- Assert.AreEqual (ListChangedType.ItemAdded, ilist_changed_args.ListChangedType, "C4");
- Assert.AreEqual (3, iblist_changed_args.NewIndex, "C5");
- Assert.AreEqual (3, ilist_changed_args.NewIndex, "C6");
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- // This only applies to IBindingList - Direct access, not through BindingSource
- bindinglist.Add ("D");
- Assert.AreEqual (1, iblist_raised, "D1");
- Assert.AreEqual (ListChangedType.ItemAdded, iblist_changed_args.ListChangedType, "D2");
- Assert.AreEqual (4, iblist_changed_args.NewIndex, "D3");
- }
- [Test]
- public void ListChanged_ItemDeleted ()
- {
- IBindingList bindinglist = new BindingList<string> ();
- bindinglist.Add ("A");
- bindinglist.Add ("B");
- bindinglist.Add ("C");
- IList arraylist = new ArrayList (bindinglist);
- ResetEventsInfo ();
- iblist_source.DataSource = bindinglist;
- ilist_source.DataSource = arraylist;
- // Clear after setting DataSource generated some info
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- iblist_source.RemoveAt (2);
- ilist_source.RemoveAt (2);
- Assert.AreEqual (1, iblist_raised, "A1");
- Assert.AreEqual (1, ilist_raised, "A2");
- Assert.AreEqual (ListChangedType.ItemDeleted, iblist_changed_args.ListChangedType, "A3");
- Assert.AreEqual (ListChangedType.ItemDeleted, ilist_changed_args.ListChangedType, "A4");
- Assert.AreEqual (2, iblist_changed_args.NewIndex, "A5");
- Assert.AreEqual (2, ilist_changed_args.NewIndex, "A6");
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- iblist_source.Remove ("A");
- ilist_source.Remove ("A");
- Assert.AreEqual (1, iblist_raised, "B1");
- Assert.AreEqual (1, ilist_raised, "B2");
- Assert.AreEqual (ListChangedType.ItemDeleted, iblist_changed_args.ListChangedType, "B3");
- Assert.AreEqual (ListChangedType.ItemDeleted, ilist_changed_args.ListChangedType, "B4");
- Assert.AreEqual (0, iblist_changed_args.NewIndex, "B5");
- Assert.AreEqual (0, ilist_changed_args.NewIndex, "B6");
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- // This only applies to IBindingList - Direct access, not through BindingSource
- bindinglist.Remove ("B");
- Assert.AreEqual (1, iblist_raised, "C1");
- Assert.AreEqual (ListChangedType.ItemDeleted, iblist_changed_args.ListChangedType, "C2");
- Assert.AreEqual (0, iblist_changed_args.NewIndex, "C3");
- }
- [Test]
- public void ListChanged_Reset ()
- {
- IBindingList bindinglist = new BindingList<string> ();
- bindinglist.Add ("A");
- bindinglist.Add ("B");
- bindinglist.Add ("C");
- IList arraylist = new ArrayList (bindinglist);
- ResetEventsInfo ();
- iblist_source.DataSource = bindinglist;
- ilist_source.DataSource = arraylist;
- // Clear after setting DataSource generated some info
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- iblist_source.Clear ();
- ilist_source.Clear ();
- Assert.AreEqual (1, iblist_raised, "A1");
- Assert.AreEqual (1, ilist_raised, "A2");
- Assert.AreEqual (ListChangedType.Reset, iblist_changed_args.ListChangedType, "A3");
- Assert.AreEqual (ListChangedType.Reset, ilist_changed_args.ListChangedType, "A4");
- Assert.AreEqual (-1, iblist_changed_args.NewIndex, "A5");
- Assert.AreEqual (-1, ilist_changed_args.NewIndex, "A6");
- // This is only for BindingList - Direct access to Clear
- // First add some items
- bindinglist.Add ("D");
- bindinglist.Add ("E");
- iblist_raised = ilist_raised = 0;
- iblist_changed_args = ilist_changed_args = null;
- bindinglist.Clear ();
- Assert.AreEqual (1, iblist_raised, "B1");
- Assert.AreEqual (ListChangedType.Reset, iblist_changed_args.ListChangedType, "B2");
- Assert.AreEqual (-1, iblist_changed_args.NewIndex, "B3");
- }
- }
- }
- #endif
|