From 96044ed481d24cb834dd27cb3ee489b5df9e94ab Mon Sep 17 00:00:00 2001 From: Eric Maupin Date: Thu, 23 Aug 2018 15:01:22 -0400 Subject: [Win] Fix canceled Add Type result Also fixes Add not having a restriction. Fixes #367 --- .../CollectionPropertyViewModelTests.cs | 83 ++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-) (limited to 'Xamarin.PropertyEditing.Tests') diff --git a/Xamarin.PropertyEditing.Tests/CollectionPropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/CollectionPropertyViewModelTests.cs index c8e8cde..0139aad 100644 --- a/Xamarin.PropertyEditing.Tests/CollectionPropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/CollectionPropertyViewModelTests.cs @@ -2,9 +2,11 @@ using System; using System.Collections; using System.Collections.Specialized; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Moq; using NUnit.Framework; +using Xamarin.PropertyEditing.Properties; using Xamarin.PropertyEditing.Reflection; using Xamarin.PropertyEditing.Tests.MockControls; using Xamarin.PropertyEditing.ViewModels; @@ -14,8 +16,24 @@ namespace Xamarin.PropertyEditing.Tests [TestFixture] internal class CollectionPropertyViewModelTests { + private AsyncSynchronizationContext syncContext; + + [SetUp] + public void Setup () + { + this.syncContext = new AsyncSynchronizationContext (); + SynchronizationContext.SetSynchronizationContext (this.syncContext); + } + + [TearDown] + public void TearDown () + { + this.syncContext.WaitForPendingOperationsToComplete (); + SynchronizationContext.SetSynchronizationContext (null); + } + [Test] - public async Task AddType () + public void AddType () { TargetPlatform platform = new TargetPlatform (new MockEditorProvider()); var obj = new { @@ -30,14 +48,14 @@ namespace Xamarin.PropertyEditing.Tests e.SelectedType = buttonType; }; - await vm.AssignableTypes.Task; + vm.AssignableTypes.Task.Wait(); vm.SelectedType = vm.SuggestedTypes.First (); Assert.That (vm.SuggestedTypes, Contains.Item (buttonType)); } [Test] - public async Task AddTarget () + public void AddTarget () { TargetPlatform platform = new TargetPlatform (new MockEditorProvider()); @@ -48,7 +66,7 @@ namespace Xamarin.PropertyEditing.Tests var editor = new ReflectionObjectEditor (obj); var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor }); - await vm.AssignableTypes.Task; + vm.AssignableTypes.Task.Wait(); vm.SelectedType = GetTypeInfo (typeof(MockWpfButton)); vm.AddTargetCommand.Execute (null); @@ -57,6 +75,63 @@ namespace Xamarin.PropertyEditing.Tests Assume.That (vm.Targets.Single ().Item, Is.InstanceOf (typeof(MockWpfButton))); } + [Test] + public async Task AddTargetCanAdd () + { + TargetPlatform platform = new TargetPlatform (new MockEditorProvider ()); + + var obj = new { + Collection = new ArrayList () + }; + + var editor = new ReflectionObjectEditor (obj); + + var vm = new CollectionPropertyViewModel (platform, editor.Properties.First (), new[] { editor }); + await vm.AssignableTypes.Task; + + Assume.That (vm.SelectedType, Is.Null); + Assert.That (vm.AddTargetCommand.CanExecute (null), Is.False); + + bool changed = false; + vm.AddTargetCommand.CanExecuteChanged += (o, e) => { changed = true; }; + vm.SelectedType = GetTypeInfo (typeof (MockWpfButton)); + Assert.That (vm.AddTargetCommand.CanExecute (null), Is.True); + Assert.That (changed, Is.True, "CanExecuteChanged did not fire"); + + changed = false; + vm.SelectedType = null; + Assert.That (vm.AddTargetCommand.CanExecute (null), Is.False); + Assert.That (changed, Is.True, "CanExecuteChanged did not fire"); + } + + [Test] + [Description ("When selecting a new type, if it's canceled the suggested type should return to something else")] + public void AddTypeCanceled () + { + TargetPlatform platform = new TargetPlatform (new MockEditorProvider ()); + + var obj = new { + Collection = new ArrayList () + }; + + var editor = new ReflectionObjectEditor (obj); + + var vm = new CollectionPropertyViewModel (platform, editor.Properties.First (), new[] { editor }); + vm.AssignableTypes.Task.Wait(); + + var buttonType = GetTypeInfo (typeof (MockWpfButton)); + vm.TypeRequested += (o, e) => { + e.SelectedType = buttonType; + }; + + vm.SelectedType = vm.SuggestedTypes.Last (); + Assume.That (vm.SuggestedTypes, Contains.Item (buttonType)); + + buttonType = null; + vm.SelectedType = vm.SuggestedTypes.Last (); + Assert.That (vm.SelectedType, Is.EqualTo (vm.SuggestedTypes.First ())); + } + [Test] public async Task RemoveTarget () { -- cgit v1.2.3