예제 #1
0
        public void TestCollections()
        {
            var numbers = new EditableCollection <int>();
            var results = new EditableCollection <int>();

            using (results.SyncWith(numbers, x => x))
            {
                var viewableResults = results.AsViewableCollection();

                // when doing more than one change:
                // use beginupdate/endupdate to reduce syncing events
                numbers.BeginUpdate();
                try
                {
                    numbers.Add(4);
                    numbers.Add(7);
                }
                finally
                {
                    numbers.EndUpdate();
                }

                // Magically results (x) are already added to the Results list
                CollectionAssert.AreEqual(numbers, viewableResults);

                // You can't add a result to the public Results list
                // Results.Add(17);

                // again: change source collection:
                numbers.Add(8);

                // synced results collection is already updated.
                CollectionAssert.AreEqual(numbers, viewableResults);
            }
        }
예제 #2
0
        public void TestEventsFired()
        {
            bool eventFired = false;

            var sampleData = GenerateSampleData(10);
            IEditableCollection <int> sourceCollection = new EditableCollection <int>();
            IEditableCollection <int> targetCollection = new EditableCollection <int>();

            var syncer = targetCollection.SyncWith(sourceCollection, (itemB) => itemB);

            syncer.Synced += delegate(object sender, SyncEventArgs <int, int> args) { eventFired = true; };

            sourceCollection.Add(sampleData[0]);
            Assert.AreEqual(true, eventFired);

            eventFired = false;
            sourceCollection.Remove(sampleData[0]);
            Assert.AreEqual(true, eventFired);

            eventFired = false;
            sourceCollection.BeginUpdate();
            sourceCollection.Add(sampleData[0]);
            sourceCollection.Add(sampleData[1]);
            sourceCollection.EndUpdate();
            Assert.AreEqual(true, eventFired);
        }
예제 #3
0
        public void TestSyncerChain()
        {
            var sampleData = GenerateSampleData(10);
            IEditableCollection <int> sourceCollection = new EditableCollection <int>();
            IEditableCollection <int> interCollection  = new EditableCollection <int>();
            IEditableCollection <int> targetCollection = new EditableCollection <int>();

            var syncer1 = targetCollection.SyncWith(interCollection, (itemB) => itemB);
            var syncer2 = interCollection.SyncWith(sourceCollection, (itemB) => itemB);

            sourceCollection.BeginUpdate();
            sourceCollection.Add(sampleData[0]);
            sourceCollection.Add(sampleData[1]);
            sourceCollection.Add(sampleData[2]);

            CollectionAssert.AreNotEqual(sourceCollection, targetCollection);

            sourceCollection.Remove(sampleData[0]);

            CollectionAssert.AreNotEqual(sourceCollection, targetCollection);

            bool eventFired = false;

            syncer1.Synced += delegate(object sender, SyncEventArgs <int, int> args) { eventFired = true; };

            sourceCollection.EndUpdate();

            Assert.AreEqual(true, eventFired);
            CollectionAssert.AreEqual(sourceCollection, targetCollection);

            sourceCollection.AddRange(sampleData);
            CollectionAssert.AreEqual(sourceCollection, targetCollection);

            sourceCollection.Clear();
            CollectionAssert.AreEqual(sourceCollection, targetCollection);
        }
예제 #4
0
        public void TestBatchUpdate()
        {
            var sampleData = GenerateSampleData(10);
            IEditableCollection <int> sourceCollection = new EditableCollection <int>();
            IEditableCollection <int> targetCollection = new EditableCollection <int>();

            var syncer = targetCollection.SyncWith(sourceCollection, (itemB) => itemB);

            sourceCollection.BeginUpdate();
            sourceCollection.Add(sampleData[0]);
            sourceCollection.Add(sampleData[1]);
            sourceCollection.Add(sampleData[2]);

            CollectionAssert.AreNotEqual(sourceCollection, targetCollection);

            sourceCollection.Remove(sampleData[0]);
            sourceCollection.Remove(sampleData[2]);

            CollectionAssert.AreNotEqual(sourceCollection, targetCollection);

            sourceCollection.EndUpdate();

            CollectionAssert.AreEqual(sourceCollection, targetCollection);
        }