Example #1
0
        public static void CopyToTest()
        {
            string[] anArray = new string[] { "one", "two", "three", "four" };
            ObservableSet <string> collection = new ObservableSet <string>((IEnumerable <string>)anArray);

            string[] aCopy = new string[collection.Count];
            collection.CopyTo(aCopy, 0);
            for (int i = 0; i < anArray.Length; ++i)
            {
                Assert.Equal(anArray[i], aCopy[i]);
            }

            // copy observable collection starting in middle, where array is larger than source.
            aCopy = new string[collection.Count + 2];
            int offsetIndex = 1;

            collection.CopyTo(aCopy, offsetIndex);
            for (int i = 0; i < aCopy.Length; i++)
            {
                string value = aCopy[i];
                if (i == 0)
                {
                    Assert.True(null == value, "Should not have a value since we did not start copying there.");
                }
                else if (i == (aCopy.Length - 1))
                {
                    Assert.True(null == value, "Should not have a value since the collection is shorter than the copy array..");
                }
                else
                {
                    int indexInCollection = i - offsetIndex;
                    Assert.Equal(collection.ToArray()[indexInCollection], aCopy[i]);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Will perform an Add or Insert on the given Collection depending on whether the
        /// insertIndex is null or not. If it is null, will Add, otherwise, will Insert.
        /// </summary>
        public void AddOrInsertItemTest(ObservableSet <string> collection, string itemToAdd, int?insertIndex = null)
        {
            INotifyPropertyChanged collectionPropertyChanged = collection;

            collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
            _expectedPropertyChanged = new[]
            {
                new PropertyNameExpected(COUNT),
                //new PropertyNameExpected(ITEMARRAY)
            };

            collection.CollectionChanged += Collection_CollectionChanged;

            ExpectedCollectionChangedFired++;
            ExpectedAction   = NotifyCollectionChangedAction.Add;
            ExpectedNewItems = new string[] { itemToAdd };
            if (insertIndex.HasValue)
            {
                ExpectedNewStartingIndex = insertIndex.Value;
            }
            else
            {
                ExpectedNewStartingIndex = collection.Count;
            }
            ExpectedOldItems         = null;
            ExpectedOldStartingIndex = -1;

            int expectedCount = collection.Count + 1;

            if (insertIndex.HasValue)
            {
                //collection.Insert(insertIndex.Value, itemToAdd);
                //Assert.Equal(itemToAdd, collection[insertIndex.Value]);
            }
            else
            {
                collection.Add(itemToAdd);
                Assert.Equal(itemToAdd, collection.ToArray()[collection.Count - 1]);
            }

            Assert.Equal(expectedCount, collection.Count);
            Assert.Equal(ExpectedCollectionChangedFired, NumCollectionChangedFired);


            foreach (var item in _expectedPropertyChanged)
            {
                Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since we just added an item");
            }

            collection.CollectionChanged -= Collection_CollectionChanged;
            collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        }
        public void Reentrancy_SingleListener_DoesNotThrow()
        {
            bool handlerCalled = false;

            var collection = new ObservableSet <int>();

            collection.CollectionChanged += (sender, e) =>
            {
                if (!handlerCalled)
                {
                    handlerCalled = true;

                    // Single listener; does not throw.
                    collection.Add(2);
                }
            };
            collection.Add(1);

            Assert.True(handlerCalled);
            Assert.Equal(2, collection.Count);
            Assert.Equal(1, collection.ToArray()[0]);
            Assert.Equal(2, collection.ToArray()[1]);
        }
        public void Reentrancy_MultipleListeners_Throws()
        {
            bool handler1Called = false;
            bool handler2Called = false;

            var collection = new ObservableSet <int>();

            collection.CollectionChanged += (sender, e) => { handler1Called = true; };
            collection.CollectionChanged += (sender, e) =>
            {
                handler2Called = true;

                // More than one listener; throws.
                Assert.Throws <InvalidOperationException>(() => collection.Add(2));
            };
            collection.Add(1);

            Assert.True(handler1Called);
            Assert.True(handler2Called);
            Assert.Equal(1, collection.Count);
            Assert.Equal(1, collection.ToArray()[0]);
        }