Example #1
0
        public async Task GetItemConsistentOnUIThread()
        {
            var notifier = new MockCollectionChangedNotifier();
            var source   = new ObservableCollection <string>
            {
                "zero",
                "one",
                "two"
            };

            IItemsViewSource ois = ItemsSourceFactory.Create(source, notifier);

            string itemAtPosition2BeforeNotify = string.Empty;

            // Add an item from a threadpool thread
            await Task.Run(() => {
                source.Insert(0, "foo");

                // Post a check ahead of the queued update on the main thread
                _handler.PostAtFrontOfQueue(() => itemAtPosition2BeforeNotify = (string)ois.GetItem(2));
            });

            // Check the result on the main thread
            var onMainThreadGetItem = await Device.InvokeOnMainThreadAsync(() => (string)ois.GetItem(2));

            Assert.That(itemAtPosition2BeforeNotify, Is.EqualTo("two"));
            Assert.That(onMainThreadGetItem, Is.EqualTo("one"));
            Assert.That(notifier.InsertCount, Is.EqualTo(1));
        }
Example #2
0
        public async Task AddItemCountConsistentOnUIThread()
        {
            var notifier         = new MockCollectionChangedNotifier();
            var source           = new ObservableCollection <int>();
            IItemsViewSource ois = ItemsSourceFactory.Create(source, notifier);

            int countBeforeNotify = -1;

            // Add an item from a threadpool thread
            await Task.Run(() => {
                source.Add(1);

                // Post a check ahead of the queued update on the main thread
                _handler.PostAtFrontOfQueue(() => {
                    countBeforeNotify = ois.Count;
                });
            });

            // Check the result on the main thread
            var onMainThreadCount = await Device.InvokeOnMainThreadAsync(() => ois.Count);

            Assert.That(countBeforeNotify, Is.EqualTo(0), "Count should still be reporting no items before the notify resolves");
            Assert.That(onMainThreadCount, Is.EqualTo(1));
            Assert.That(notifier.InsertCount, Is.EqualTo(1), "Should have recorded exactly one Add");
        }
Example #3
0
        public async Task RemoveitemCountConsistentOnUIThread()
        {
            var notifier = new MockCollectionChangedNotifier();
            var source   = new ObservableCollection <int> {
                1
            };
            IItemsViewSource ois = ItemsSourceFactory.Create(source, notifier);

            int countBeforeNotify = -1;

            // Remove an item from a threadpool thread
            await Task.Run(() =>
            {
                source.Remove(1);

                // Post a check ahead of the queued update on the main thread
                _handler.PostAtFrontOfQueue(() => countBeforeNotify = ois.Count);
            });

            // Check the result on the main thread
            var onMainThreadCount = await Device.InvokeOnMainThreadAsync(() => ois.Count);

            Assert.That(countBeforeNotify, Is.EqualTo(1));
            Assert.That(onMainThreadCount, Is.EqualTo(0));
            Assert.That(notifier.RemoveCount, Is.EqualTo(1));
        }
Example #4
0
        public async Task GetPositionConsistentOnUIThread()
        {
            var notifier = new MockCollectionChangedNotifier();
            var source   = new ObservableCollection <string>
            {
                "zero",
                "one",
                "two"
            };

            IItemsViewSource ois = ItemsSourceFactory.Create(source, Application.Current, notifier);

            int positionBeforeNotify = -1;

            // Add an item from a threadpool thread
            await Task.Run(() =>
            {
                source.Insert(0, "foo");

                // Post a check ahead of the queued update on the main thread
                _handler.PostAtFrontOfQueue(() => positionBeforeNotify = ois.GetPosition("zero"));
            });

            // Check the result on the main thread
            var onMainThreadGetItem = await Application.Current.Dispatcher.DispatchAsync(() => ois.GetPosition("zero"));

            Assert.That(positionBeforeNotify, Is.EqualTo(0));
            Assert.That(onMainThreadGetItem, Is.EqualTo(1));
            Assert.That(notifier.InsertCount, Is.EqualTo(1));
        }