From 72427b781ad361d66f2ea893254848346db7a2aa Mon Sep 17 00:00:00 2001 From: Tomasz Cielecki Date: Tue, 13 Feb 2018 15:57:22 +0100 Subject: [PATCH] Add observable collection tests --- .../ViewModels/MvxObservableCollectionTest.cs | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 UnitTests/MvvmCross.UnitTest/ViewModels/MvxObservableCollectionTest.cs diff --git a/UnitTests/MvvmCross.UnitTest/ViewModels/MvxObservableCollectionTest.cs b/UnitTests/MvvmCross.UnitTest/ViewModels/MvxObservableCollectionTest.cs new file mode 100644 index 000000000..641878ed2 --- /dev/null +++ b/UnitTests/MvvmCross.UnitTest/ViewModels/MvxObservableCollectionTest.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Specialized; +using MvvmCross.Base; +using MvvmCross.Test; +using MvvmCross.ViewModels; +using Xunit; + +namespace MvvmCross.UnitTest.ViewModels +{ + [Collection("MvxTest")] + public class MvxObservableCollectionTest + { + MvxTestFixture _fixture; + + public MvxObservableCollectionTest(MvxTestFixture fixture) + { + _fixture = fixture; + _fixture.Ioc.RegisterSingleton(new DummyDispatcher()); + } + + public class DummyDispatcher : MvxSingleton, IMvxMainThreadDispatcher + { + public bool RequestMainThreadAction(Action action, bool maskExceptions = true) + { + action?.Invoke(); + return true; + } + } + + [Fact] + public void AddRangeSuppressesChangesTest() + { + var collection = new MvxObservableCollection(); + var invokeCount = 0; + collection.CollectionChanged += (s, e) => + { + invokeCount++; + }; + + collection.Add("Foo"); + + Assert.Equal(1, invokeCount); + Assert.Contains("Foo", collection); + + var newItems = new[] { "Bar", "Baz", "Herp", "Derp" }; + collection.AddRange(newItems); + + Assert.Equal(2, invokeCount); + + foreach (var item in newItems) + Assert.Contains(item, collection); + } + + [Fact] + public void AddRangeIsAddActionTest() + { + var collection = new MvxObservableCollection(); + collection.CollectionChanged += (s, e) => { + Assert.Equal(NotifyCollectionChangedAction.Add, e.Action); + }; + collection.AddRange(new[] { "Bar", "Baz", "Herp", "Derp" }); + } + + [Fact] + public void ValidateStartingIndexOnAddRangeTest() + { + var collection = new MvxObservableCollection(); + var newItems = new[] { "Bar", "Baz", "Herp", "Derp" }; + + NotifyCollectionChangedEventHandler handler = (s, a) => + { + Assert.Equal(0, a.NewStartingIndex); + Assert.Equal(newItems.Length, a.NewItems.Count); + Assert.Null(a.OldItems); + }; + + collection.CollectionChanged += handler; + + collection.AddRange(newItems); + var newStartIndex = collection.Count; + + collection.CollectionChanged -= handler; + + handler = (s, a) => { + Assert.Equal(newStartIndex, a.NewStartingIndex); + Assert.Equal(newItems.Length, a.NewItems.Count); + }; + + collection.CollectionChanged += handler; + + collection.AddRange(newItems); + } + + [Fact] + public void AddRangeThrowsArgumentNullExceptionOnNullInput() + { + var collection = new MvxObservableCollection(); + Assert.Throws(() => collection.AddRange(null)); + } + } +}