diff options
Diffstat (limited to 'Xamarin.PropertyEditing.Tests')
9 files changed, 203 insertions, 9 deletions
diff --git a/Xamarin.PropertyEditing.Tests/ConstrainedPropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/ConstrainedPropertyViewModelTests.cs index c14e390..654683a 100644 --- a/Xamarin.PropertyEditing.Tests/ConstrainedPropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/ConstrainedPropertyViewModelTests.cs @@ -108,10 +108,5 @@ namespace Xamarin.PropertyEditing.Tests protected abstract T GetConstrainedRandomValueAboveBounds (Random rand, out T max, out T min); protected abstract T GetConstrainedRandomValueBelowBounds (Random rand, out T max, out T min); - - protected TViewModel GetViewModel (IPropertyInfo property, IObjectEditor editor) - { - return GetViewModel (property, new[] { editor }); - } } } diff --git a/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs b/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs index 32d8d6f..faafc83 100644 --- a/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs +++ b/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs @@ -22,6 +22,7 @@ namespace Xamarin.PropertyEditing.Tests.MockControls AddProperty<CommonPoint> ("Point", ReadWrite); AddProperty<CommonSize> ("Size", ReadWrite); AddProperty<CommonRectangle> ("Rectangle", ReadWrite); + AddProperty<CommonRatio> ("Ratio", ReadWrite); // AddProperty<CommonThickness> ("Thickness", ReadWrite); // Lacking support on the mac at this point in time. AddProperty<object> ("Object", ReadWrite); AddProperty<IList> ("Collection", ReadWrite); @@ -36,6 +37,7 @@ namespace Xamarin.PropertyEditing.Tests.MockControls AddReadOnlyProperty<CommonPoint> ("ReadOnlyPoint", ReadOnly); AddReadOnlyProperty<CommonSize> ("ReadOnlySize", ReadOnly); AddReadOnlyProperty<CommonRectangle> ("ReadOnlyRectangle", ReadOnly); + AddReadOnlyProperty<CommonRatio> ("ReadOnlyRatio", ReadOnly); // AddReadOnlyProperty<CommonThickness> ("ReadOnlyThickness", ReadOnly); AddProperty<NotImplemented> ("Uncategorized", None); diff --git a/Xamarin.PropertyEditing.Tests/MockControls/MockWpfControl.cs b/Xamarin.PropertyEditing.Tests/MockControls/MockWpfControl.cs index e9f9680..c9eb3fb 100644 --- a/Xamarin.PropertyEditing.Tests/MockControls/MockWpfControl.cs +++ b/Xamarin.PropertyEditing.Tests/MockControls/MockWpfControl.cs @@ -105,6 +105,7 @@ namespace Xamarin.PropertyEditing.Tests.MockControls AddProperty<VerticalAlignment> ("VerticalContentAlignment", Layout); AddProperty<Visibility> ("Visibility"); AddProperty<double> ("Width"); + AddProperty<CommonRatio> ("Ratio"); #endregion #region Events AddEvents ( diff --git a/Xamarin.PropertyEditing.Tests/ObservableLookupTests.cs b/Xamarin.PropertyEditing.Tests/ObservableLookupTests.cs index 6bb2968..c0ab5b7 100644 --- a/Xamarin.PropertyEditing.Tests/ObservableLookupTests.cs +++ b/Xamarin.PropertyEditing.Tests/ObservableLookupTests.cs @@ -56,7 +56,7 @@ namespace Xamarin.PropertyEditing.Tests var grouping = lookup[key]; ((INotifyCollectionChanged) grouping).CollectionChanged += (sender, args) => { if (args.Action == NotifyCollectionChangedAction.Remove) { - if (args.OldItems[0] == value) + if ((string)args.OldItems[0] == value) itemRemoved = true; } }; diff --git a/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs index a00ccd8..7b51e8f 100644 --- a/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs @@ -18,8 +18,9 @@ namespace Xamarin.PropertyEditing.Tests : PropertiesViewModelTests<PanelViewModel> { [SetUp] - public void Setup () + public override void Setup () { + base.Setup (); SynchronizationContext.SetSynchronizationContext (this.context = new TestContext ()); } diff --git a/Xamarin.PropertyEditing.Tests/PropertiesViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PropertiesViewModelTests.cs index 8bd7ae2..a7e98a9 100644 --- a/Xamarin.PropertyEditing.Tests/PropertiesViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PropertiesViewModelTests.cs @@ -37,7 +37,7 @@ namespace Xamarin.PropertyEditing.Tests private Exception unhandled; [SetUp] - public void Setup () + public virtual void Setup () { AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; } diff --git a/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs index 8232d80..f206158 100644 --- a/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs @@ -429,7 +429,7 @@ namespace Xamarin.PropertyEditing.Tests resourcesMock.Setup (rp => rp.GetResourcesAsync (editor.Target, mockProperty.Object, It.IsAny<CancellationToken> ())).ReturnsAsync (new[] { resource }); editor.ValueEvaluator = (info, val, source) => { - if (source == resource) + if ((Resource)source == resource) return value; return default(TValue); diff --git a/Xamarin.PropertyEditing.Tests/RatioViewModelTests.cs b/Xamarin.PropertyEditing.Tests/RatioViewModelTests.cs new file mode 100644 index 0000000..9aeedd0 --- /dev/null +++ b/Xamarin.PropertyEditing.Tests/RatioViewModelTests.cs @@ -0,0 +1,194 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.PropertyEditing.Drawing; +using Xamarin.PropertyEditing.ViewModels; + +namespace Xamarin.PropertyEditing.Tests +{ + internal class RatioViewModelTests + : PropertyViewModelTests<CommonRatio, RatioViewModel> + { + [Test] + public void Numerator () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + Assume.That (vm.Value, Is.EqualTo (new CommonRatio ())); + + bool xChanged = false, valueChanged = false; + vm.PropertyChanged += (sender, args) => { + if (args.PropertyName == nameof(RatioViewModel.Numerator)) + xChanged = true; + if (args.PropertyName == nameof(RatioViewModel.Value)) + valueChanged = true; + }; + + vm.Numerator = 5; + Assert.That (vm.Value.Numerator, Is.EqualTo (5)); + Assert.That (xChanged, Is.True); + Assert.That (valueChanged, Is.True); + } + + [Test] + public void Denominator () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + Assume.That (vm.Value, Is.EqualTo (new CommonRatio ())); + + bool xChanged = false, valueChanged = false; + vm.PropertyChanged += (sender, args) => { + if (args.PropertyName == nameof (RatioViewModel.Denominator)) + xChanged = true; + if (args.PropertyName == nameof (RatioViewModel.Value)) + valueChanged = true; + }; + + vm.Denominator = 5; + Assert.That (vm.Value.Denominator, Is.EqualTo (5)); + Assert.That (xChanged, Is.True); + Assert.That (valueChanged, Is.True); + } + + [Test] + public void RatioSeparator () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + Assume.That (vm.Value, Is.EqualTo (new CommonRatio ())); + + bool xChanged = false, valueChanged = false; + vm.PropertyChanged += (sender, args) => { + if (args.PropertyName == nameof (RatioViewModel.RatioSeparator)) + xChanged = true; + if (args.PropertyName == nameof (RatioViewModel.Value)) + valueChanged = true; + }; + + vm.RatioSeparator = '/'; + Assert.That (vm.Value.RatioSeparator, Is.EqualTo ('/')); + Assert.That (xChanged, Is.True); + Assert.That (valueChanged, Is.True); + } + + [Test] + public void ValueChangesNumeratorDenominatorRatioSeparator () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + Assume.That (vm.Numerator, Is.EqualTo (0)); + Assume.That (vm.Denominator, Is.EqualTo (0)); + + bool nChanged = false, dChanged = false, sChanged = false, valueChanged = false; + vm.PropertyChanged += (sender, args) => { + if (args.PropertyName == nameof(RatioViewModel.Numerator)) + nChanged = true; + if (args.PropertyName == nameof(RatioViewModel.Denominator)) + dChanged = true; + if (args.PropertyName == nameof (RatioViewModel.RatioSeparator)) + sChanged = true; + if (args.PropertyName == nameof(SizePropertyViewModel.Value)) + valueChanged = true; + }; + + vm.Value = new CommonRatio (5, 10, ':'); + + Assert.That (vm.Numerator, Is.EqualTo (5)); + Assert.That (vm.Denominator, Is.EqualTo (10)); + Assert.That (nChanged, Is.True); + Assert.That (dChanged, Is.True); + Assert.That (sChanged, Is.True); + Assert.That (valueChanged, Is.True); + } + + [Test] + public void RatioWithoutDenominator () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + + vm.ValueString = "21"; + + Assert.That (vm.Numerator, Is.EqualTo (21)); + Assert.That (vm.Denominator, Is.EqualTo (1)); + Assert.That (vm.RatioSeparator, Is.EqualTo (':')); + } + + [Test] + public void RatioWithNumeratorDenominatorAndColonSeparator () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + + vm.ValueString = "21:2"; + + Assert.That (vm.Numerator, Is.EqualTo (21)); + Assert.That (vm.Denominator, Is.EqualTo (2)); + Assert.That (vm.RatioSeparator, Is.EqualTo (':')); + } + + [Test] + public void RatioWithNumeratorDenominatorAndSlashSeparator () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + + vm.ValueString = "21/2"; + + Assert.That (vm.Numerator, Is.EqualTo (21)); + Assert.That (vm.Denominator, Is.EqualTo (2)); + Assert.That (vm.RatioSeparator, Is.EqualTo ('/')); + } + + [Test] + public void RatioWithWrongSeparator () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + bool eChanged = false; + vm.ErrorsChanged += (sender, args) => { + eChanged = true; + }; + + vm.ValueString = "21@2"; + + // Was Error raised? + Assert.That (eChanged, Is.True); + } + + [Test] + public void RatioWithSpaces () + { + var property = GetPropertyMock (); + var editor = GetBasicEditor (property.Object); + var vm = GetViewModel (property.Object, new[] { editor }); + + vm.ValueString = " 21: 2 "; + + Assert.That (vm.ValueString, Is.EqualTo ("21:2")); + } + + protected override CommonRatio GetRandomTestValue (Random rand) + { + var randomDenominator = rand.Next (); + // Can't be less than 1 + if (randomDenominator < 1) + randomDenominator = 1; + return new CommonRatio (rand.Next (), randomDenominator, '/'); + } + + protected override RatioViewModel GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) + { + return new RatioViewModel (platform, property, editors); + } + } +} diff --git a/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj b/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj index ba02175..c5419cd 100644 --- a/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj +++ b/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj @@ -119,6 +119,7 @@ <Compile Include="ValueInfoTests.cs" /> <Compile Include="MockControls\MockNSControl.cs" /> <Compile Include="MockControls\MockNSButton.cs" /> + <Compile Include="RatioViewModelTests.cs" /> </ItemGroup> <ItemGroup> <None Include="Manual\BrushEditor.md" /> |