Ejemplo n.º 1
0
        ///// <summary>
        ///// Given a collection, will move an item from the oldIndex to the newIndex.
        ///// </summary>
        //public void MoveItemTest(CiccioSet<string> collection, int oldIndex, int newIndex)
        //{
        //    INotifyPropertyChanged collectionPropertyChanged = collection;
        //    collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
        //    _expectedPropertyChanged = new[] { new PropertyNameExpected(ITEMARRAY) };

        //    collection.CollectionChanged += Collection_CollectionChanged;

        //    string itemAtOldIndex = collection[oldIndex];

        //    ExpectedCollectionChangedFired++;
        //    ExpectedAction = NotifyCollectionChangedAction.Move;
        //    ExpectedNewItems = new string[] { itemAtOldIndex };
        //    ExpectedNewStartingIndex = newIndex;
        //    ExpectedOldItems = new string[] { itemAtOldIndex };
        //    ExpectedOldStartingIndex = oldIndex;

        //    int expectedCount = collection.Count;

        //    collection.Move(oldIndex, newIndex);
        //    Assert.Equal(expectedCount, collection.Count);
        //    Assert.Equal(itemAtOldIndex, collection[newIndex]);
        //    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 moved an item");

        //    collection.CollectionChanged -= Collection_CollectionChanged;
        //    collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        //}

        ///// <summary>
        ///// Will set that new item at the specified index in the given collection.
        ///// </summary>
        //public void ReplaceItemTest(CiccioSet<string> collection, int index, string newItem)
        //{
        //    INotifyPropertyChanged collectionPropertyChanged = collection;
        //    collectionPropertyChanged.PropertyChanged += Collection_PropertyChanged;
        //    _expectedPropertyChanged = new[] { new PropertyNameExpected(ITEMARRAY) };

        //    collection.CollectionChanged += Collection_CollectionChanged;

        //    string itemAtOldIndex = collection.ToArray()[index];

        //    ExpectedCollectionChangedFired++;
        //    ExpectedAction = NotifyCollectionChangedAction.Replace;
        //    ExpectedNewItems = new string[] { newItem };
        //    ExpectedNewStartingIndex = index;
        //    ExpectedOldItems = new string[] { itemAtOldIndex };
        //    ExpectedOldStartingIndex = index;

        //    int expectedCount = collection.Count;

        //    collection[index] = newItem;
        //    Assert.Equal(expectedCount, collection.Count);
        //    Assert.Equal(newItem, collection[index]);
        //    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 replaced an item");

        //    collection.CollectionChanged -= Collection_CollectionChanged;
        //    collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        //}

        /// <summary>
        /// Given a collection, index and item to remove, will try to remove that item
        /// from the index. If the item has duplicates, will verify that only the first
        /// instance was removed.
        /// </summary>
        public void RemoveItemTest(CiccioSet <string> collection, int itemIndex, string itemToRemove, bool isSuccessfulRemove, bool hasDuplicates)
        {
            INotifyPropertyChanged collectionPropertyChanged = collection;

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

            collection.CollectionChanged += Collection_CollectionChanged;

            if (isSuccessfulRemove)
            {
                ExpectedCollectionChangedFired++;
            }

            ExpectedAction           = NotifyCollectionChangedAction.Remove;
            ExpectedNewItems         = null;
            ExpectedNewStartingIndex = -1;
            ExpectedOldItems         = new string[] { itemToRemove };
            ExpectedOldStartingIndex = itemIndex;

            int expectedCount = isSuccessfulRemove ? collection.Count - 1 : collection.Count;

            bool removedItem = collection.Remove(itemToRemove);

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

            if (isSuccessfulRemove)
            {
                foreach (var item in _expectedPropertyChanged)
                {
                    Assert.True(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since there were items removed.");
                }

                Assert.True(removedItem, "Should have been successful in removing the item.");
            }
            else
            {
                foreach (var item in _expectedPropertyChanged)
                {
                    Assert.False(item.IsFound, "The propertychanged event should have fired for" + item.Name + ", since there were no items removed.");
                }

                Assert.False(removedItem, "Should not have been successful in removing the item.");
            }
            if (hasDuplicates)
            {
                return;
            }

            Assert.DoesNotContain(itemToRemove, collection);

            collection.CollectionChanged -= Collection_CollectionChanged;
            collectionPropertyChanged.PropertyChanged -= Collection_PropertyChanged;
        }
        public void Remove_Invoke_CallsListChanged()
        {
            var obj  = new object();
            var list = new List <object> {
                obj
            };
            var bindingList = new CiccioSet <object>(list);

            bool calledListChanged = false;

            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                Assert.Equal(0, e.NewIndex);
                Assert.Equal(ListChangedType.ItemDeleted, e.ListChangedType);

                // The event is raised after the removal.
                Assert.Equal(0, bindingList.Count);
            };
            bindingList.Remove(obj);

            Assert.True(calledListChanged);
        }
        public void Remove_INotifyPropertyChangedItems_RemovesPropertyChangedEventHandlers()
        {
            var item        = new Item();
            var bindingList = new CiccioSet <Item> {
                item
            };

            Assert.Equal(1, item.InvocationList.Length);

            bool calledListChanged = false;

            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                Assert.Equal(ListChangedType.ItemDeleted, e.ListChangedType);
                Assert.Equal(0, e.NewIndex);
            };

            bindingList.Remove(item);
            Assert.True(calledListChanged);
            Assert.Empty(bindingList);
            Assert.Null(item.InvocationList);
        }