public void Write_InsertElement_ElementInserted()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });

            col.Insert(1, "x");
            CollectionAssert.AreEqual(col, new[] { "a", "x", "b", "c" });
        }
예제 #2
0
        public async Task WriteInConcurrentObservableCollectionUIThread()
        {
            using (var collection = new ConcurrentObservableCollection <int>())
            {
                _collectionChangedCount = 0;

                collection.CollectionChanged += Collection_CollectionChanged;

                await TaskHelper.RunOnUIThreadAsync(() =>
                {
                    for (int i = 1; i < 10000; i++)
                    {
                        for (int j = 1; j < 10; j++)
                        {
                            collection.Add(i *j);
                        }

                        collection.RemoveAt(collection.Count - 1);
                        collection.Remove(i);
                        collection.Move(0, 6);
                        collection.Insert(0, i *i);
                    }
                }).ConfigureAwait(false);

                Assert.AreEqual(79992, collection.Count);
                Assert.AreEqual(129987, _collectionChangedCount);
            }
        }
예제 #3
0
        public void Write_InsertElement_ElementInserted()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });

            col.Insert(1, "x");

            col.Should().BeEquivalentTo("a", "x", "b", "c");
        }
        public void Write_ComplexOperation_CollectionUpdatedProperly()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });

            col.Add("d");
            col.Remove("b");
            col.Insert(0, "x");
            col.AddRange(new[] { "z", "f", "y" });
            col.RemoveAt(4);
            col.RemoveRange(new[] { "y", "c" });
            col[2] = "p";
            CollectionAssert.AreEquivalent(new[] { "x", "a", "p", "f" }, col);
        }
예제 #5
0
        public void InsertIsThreadSafe()
        {
            var test = new ConcurrentObservableCollection <int>(Concurrent)
            {
                1, 2, 3
            };

            var task1 = Task.Run(() =>
            {
                foreach (var i in test)
                {
                    Task.Delay(DelayTime).Wait();
                }
            });

            var task2 = Task.Run(() =>
            {
                test.Insert(0, 4);
                Task.Delay(DelayTime).Wait();
                test.Insert(0, 5);
            });

            Task.WaitAll(task1, task2);
        }
        public void Write_InsertElement_FiresAddEvent()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });
            NotifyCollectionChangedEventArgs receivedArgs = null;

            col.CollectionChanged += (sender, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Add)
                {
                    receivedArgs = args;
                }
            };
            col.Insert(1, "x");
            Assert.That(receivedArgs.NewStartingIndex, Is.EqualTo(1), "Index is wrong");
            CollectionAssert.AreEquivalent(receivedArgs.NewItems, new[] { "x" }, "New items collection wrong");
        }
예제 #7
0
        public void Write_InsertElement_FiresAddEvent()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });
            NotifyCollectionChangedEventArgs receivedArgs = null;

            col.CollectionChanged += (sender, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Add)
                {
                    receivedArgs = args;
                }
            };
            col.Insert(1, "x");

            receivedArgs.NewStartingIndex.Should().Be(1, "the index should be correct");
            receivedArgs.NewItems.Should().BeEquivalentTo(new[] { "x" });
        }