public void Can_clear()
        {
            var testData = new HashSet <int>(CreateTestData());

            var hashSet           = new ObservableHashSet <int>(testData);
            var countChanging     = 0;
            var countChanged      = 0;
            var collectionChanged = 0;
            var currentCount      = testData.Count;
            var countChange       = -testData.Count;

            hashSet.PropertyChanging  += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
            hashSet.PropertyChanged   += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
            hashSet.CollectionChanged += (s, a) =>
            {
                Assert.Equal(NotifyCollectionChangedAction.Replace, a.Action);
                Assert.Equal(testData.OrderBy(i => i), a.OldItems.OfType <int>().OrderBy(i => i));
                Assert.Empty(a.NewItems);
                collectionChanged++;
            };

            hashSet.Clear();

            Assert.Equal(testData.Count == 0 ? 0 : 1, countChanging);
            Assert.Equal(testData.Count == 0 ? 0 : 1, countChanged);
            Assert.Equal(testData.Count == 0 ? 0 : 1, collectionChanged);
            Assert.Empty(hashSet);

            hashSet.Clear();

            Assert.Equal(testData.Count == 0 ? 0 : 1, countChanging);
            Assert.Equal(testData.Count == 0 ? 0 : 1, countChanged);
            Assert.Equal(testData.Count == 0 ? 0 : 1, collectionChanged);
            Assert.Empty(hashSet);
        }
        public void Items_cleared_in_the_ObservableHashSet_are_cleared_in_the_binding_list()
        {
            var oc = new ObservableHashSet <ListElement> {
                3, 1, 4, 1, 5, 9
            };
            var obbl = new ObservableBackedBindingList <ListElement>(oc);

            oc.Clear();

            Assert.Equal(0, obbl.Count);
        }
        public void Entry_handles_clear_as_replace_with_ObservableHashSet()
        {
            var item1      = new ChangedOnlyNotificationEntity();
            var item2      = new ChangedOnlyNotificationEntity();
            var collection = new ObservableHashSet <ChangedOnlyNotificationEntity> {
                item1, item2
            };
            var testListener = SetupTestCollectionListener(collection);

            collection.Clear();

            Assert.Empty(collection);

            Assert.Equal("RelatedCollection", testListener.CollectionChanged.Single().Item2.Name);
            Assert.Empty(testListener.CollectionChanged.Single().Item3);
            Assert.Same(item1, testListener.CollectionChanged.Single().Item4.First());
            Assert.Same(item2, testListener.CollectionChanged.Single().Item4.Skip(1).Single());
        }
        public void WhenClear_ThenRaisesCollectionChanged() {
            // Arrange
            var target = new ObservableHashSet<int>();

            target.Add(1);
            target.Add(2);
            target.Add(3);

            bool wasEventRaised = false;
            target.CollectionChanged += (s, e) => {
                if (e.Action == NotifyCollectionChangedAction.Reset) {
                    wasEventRaised = true;
                }
            };

            // Act
            target.Clear();

            // Assert
            Assert.IsTrue(wasEventRaised);
        }
        public void WhenClear_ThenRaisesCountPropertyChanged() {
            // Arrange
            var target = new ObservableHashSet<int>();

            target.Add(1);
            target.Add(2);
            target.Add(3);

            bool wasEventRaised = false;
            target.PropertyChanged += (s, e) => {
                if (e.PropertyName == ObservableHashSet<int>.PropertyNames.Count) {
                    wasEventRaised = true;
                }
            };

            // Act
            target.Clear();

            // Assert
            Assert.IsTrue(wasEventRaised);
        }
Exemple #6
0
        public void DoesSupportObservableHashSet()
        {
            List <string> expected;
            List <int>    expectedForked;

            ObservableHashSet <int> source1 = new ObservableHashSet <int>();
            ObservableHashSet <int> source2 = new ObservableHashSet <int>();
            ObservableHashSet <int> source3 = new ObservableHashSet <int>();

            List <string> projection1 = new List <string>();
            List <string> projection2 = new List <string>();
            List <int>    projection3 = new List <int>();

            Func <int, string> transformation  = i => i.ToString();
            Func <int, bool>   filterPredicate = i => i <= 2;

            ListBinding <string> binding = new ListBinding <string>()
                                           .AddSource(source1, convert: transformation, filter: filterPredicate)
                                           .AddSource(source2, convert: transformation, filter: filterPredicate)
                                           .AddSource(source3, convert: transformation, filter: filterPredicate);

            source1.Add(0);
            source2.Add(1);
            source3.Add(2);
            source3.Add(3);
            source2.Add(4);

            binding.AddTarget(projection1);

            source1.Add(5);
            source2.Add(6);

            binding.AddTarget(projection2);

            source1.Add(7);
            source3.Add(8);

            expected = source1.Concat(source2).Concat(source3).Where(filterPredicate).Select(transformation).ToList();
            expected.Sort();
            Assert.Equal(expected, projection1);
            Assert.Equal(expected, projection2);

            var bindingForked = binding.Select <int>(item => int.Parse(item));

            source2.Remove(4);
            source1.Remove(5);
            source2.Remove(6);

            expected = source1.Concat(source2).Concat(source3).Where(filterPredicate).Select(transformation).ToList();
            expected.Sort();
            Assert.Equal(expected, projection1);
            Assert.Equal(expected, projection2);

            source1.Add(4);
            source3.Add(5);
            source1.Add(6);

            expected = source1.Concat(source2).Concat(source3).Where(filterPredicate).Select(transformation).ToList();
            expected.Sort();
            Assert.Equal(expected, projection1);
            Assert.Equal(expected, projection2);

            source1.Clear();

            expected = source1.Concat(source2).Concat(source3).Where(filterPredicate).Select(transformation).ToList();
            expected.Sort();
            Assert.Equal(expected, projection1);
            Assert.Equal(expected, projection2);

            source3.Clear();
            source2.Clear();

            expected = source1.Concat(source2).Concat(source3).Where(filterPredicate).Select(transformation).ToList();
            expected.Sort();
            Assert.Equal(expected, projection1);
            Assert.Equal(expected, projection2);

            expectedForked = source1.Concat(source2).Concat(source3).Where(filterPredicate).ToList();
            expectedForked.Sort();
            Assert.Equal(expectedForked, projection3);
        }
Exemple #7
0
 public void LoadXml(XElement xml)
 {
     _collection.Clear();
     _collection.UnionWith(xml.Elements().Select(e => e.LoadMetadata()));
 }