diff options
Diffstat (limited to 'Xamarin.PropertyEditing.Tests')
4 files changed, 138 insertions, 1 deletions
diff --git a/Xamarin.PropertyEditing.Tests/MockControls/MockControl.cs b/Xamarin.PropertyEditing.Tests/MockControls/MockControl.cs index 8004461..d11daf9 100644 --- a/Xamarin.PropertyEditing.Tests/MockControls/MockControl.cs +++ b/Xamarin.PropertyEditing.Tests/MockControls/MockControl.cs @@ -14,7 +14,8 @@ namespace Xamarin.PropertyEditing.Tests.MockControls public void AddProperty<T> (string name, string category = null, bool canWrite = true, bool flag = false, IEnumerable<Type> converterTypes = null, - string description = null, bool constrained = true, ValueSources valueSources = ValueSources.Local | ValueSources.Default | ValueSources.Binding) + string description = null, bool constrained = true, ValueSources valueSources = ValueSources.Local | ValueSources.Default | ValueSources.Binding, + IReadOnlyList<InputMode> inputModes = null) { IPropertyInfo propertyInfo; if (typeof(T).IsEnum) { @@ -22,6 +23,8 @@ namespace Xamarin.PropertyEditing.Tests.MockControls var enumPropertyInfoType = typeof (MockEnumPropertyInfo<,>) .MakeGenericType (underlyingType, typeof (T)); propertyInfo = (IPropertyInfo)Activator.CreateInstance (enumPropertyInfoType, name, description, category, canWrite, flag, converterTypes, constrained); + } else if (inputModes != null) { + propertyInfo = new MockPropertyInfoWithInputTypes<T> (name, inputModes, description, category, canWrite, converterTypes, valueSources); } else { propertyInfo = new MockPropertyInfo<T> (name, description, category, canWrite, converterTypes, valueSources); } diff --git a/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs b/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs index 3fe6e51..c4d88fb 100644 --- a/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs +++ b/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs @@ -16,6 +16,7 @@ namespace Xamarin.PropertyEditing.Tests.MockControls AddProperty<int> ("UnsetInteger", ReadWrite, valueSources: ValueSources.Local); AddProperty<float> ("FloatingPoint", ReadWrite); AddProperty<string> ("String", ReadWrite, valueSources: ValueSources.Local | ValueSources.Resource | ValueSources.Binding); + AddProperty<int> ("Width", ReadWrite, valueSources: ValueSources.Local | ValueSources.Resource | ValueSources.Binding, inputModes: new[] { new InputMode("Auto", true), new InputMode("Star"), new InputMode("Pixel"), }); AddProperty<Enumeration> ("Enumeration", ReadWrite, constrained: false); AddProperty<FlagsNoValues> ("FlagsNoValues", ReadWrite, canWrite: true, flag: true); AddProperty<FlagsWithValues> ("FlagsWithValues", ReadWrite, canWrite: true, flag: true); diff --git a/Xamarin.PropertyEditing.Tests/MockPropertyInfo/MockPropertyInfo.cs b/Xamarin.PropertyEditing.Tests/MockPropertyInfo/MockPropertyInfo.cs index 035b646..e21737e 100644 --- a/Xamarin.PropertyEditing.Tests/MockPropertyInfo/MockPropertyInfo.cs +++ b/Xamarin.PropertyEditing.Tests/MockPropertyInfo/MockPropertyInfo.cs @@ -6,6 +6,21 @@ using Xamarin.PropertyEditing.Tests.MockControls; namespace Xamarin.PropertyEditing.Tests.MockPropertyInfo { + public class MockPropertyInfoWithInputTypes<T> + : MockPropertyInfo<T>, IHaveInputModes + { + public MockPropertyInfoWithInputTypes (string name, IReadOnlyList<InputMode> inputModes, string description = null, string category = null, bool canWrite = true, IEnumerable<Type> converterTypes = null, ValueSources valueSources = ValueSources.Default | ValueSources.Local) + : base (name, description, category, canWrite, converterTypes, valueSources) + { + InputModes = inputModes.ToArray (); + } + + public IReadOnlyList<InputMode> InputModes + { + get; + } + } + public class MockPropertyInfo<T> : IPropertyInfo, IPropertyConverter, IEquatable<MockPropertyInfo<T>> { public MockPropertyInfo (string name, string description = null, string category = null, bool canWrite = true, IEnumerable<Type> converterTypes = null, ValueSources valueSources = ValueSources.Local | ValueSources.Default) diff --git a/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs index c207b4f..ae2fa61 100644 --- a/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs @@ -1175,6 +1175,124 @@ namespace Xamarin.PropertyEditing.Tests Assert.That (tcs.Task.IsCanceled, Is.True); } + [Test] + public void DoesntHaveInputModes () + { + var vm = GetBasicTestModel (); + Assert.That (vm.HasInputModes, Is.False); + } + + [Test] + public void HasInputModes () + { + var modes = new[] { new InputMode ("TestMode") }; + + var property = GetPropertyMock (); + var input = property.As<IHaveInputModes> (); + input.SetupGet (im => im.InputModes).Returns (modes); + + var vm = GetViewModel (property.Object, new MockObjectEditor (property.Object)); + + Assert.That (vm.HasInputModes, Is.True, "HasInputModes was false"); + Assert.That (vm.InputModes, Contains.Item (modes[0])); + Assert.That (vm.InputModes.Count, Is.EqualTo (1)); + Assert.That (vm.InputMode, Is.EqualTo (modes[0]), "InputMode not set to a default on no value set for it"); + } + + [Test] + public void InputModeCommits () + { + var modes = new[] { new InputMode ("TestMode"), new InputMode("TestMode2"), }; + + var property = GetPropertyMock (); + var input = property.As<IHaveInputModes> (); + input.SetupGet (im => im.InputModes).Returns (modes); + + var target = new object (); + var editor = new Mock<IObjectEditor> (); + editor.SetupGet (e => e.Target).Returns (target); + editor.SetupGet (e => e.Properties).Returns (new[] { property.Object }); + SetupPropertySetAndGet (editor, property.Object); + + var vm = GetViewModel (property.Object, editor.Object); + Assume.That (vm.InputMode, Is.EqualTo (modes[0])); + + vm.InputMode = modes[1]; + editor.Verify (oe => oe.SetValueAsync (property.Object, It.Is<ValueInfo<TValue>> (vi => vi.ValueDescriptor == modes[1]), It.IsAny<PropertyVariation> ())); + } + + [Test] + public async Task InputModeRestores () + { + var modes = new[] { new InputMode ("TestMode"), new InputMode ("TestMode2"), }; + + var property = GetPropertyMock (); + var input = property.As<IHaveInputModes> (); + input.SetupGet (im => im.InputModes).Returns (modes); + + var target = new object (); + var editor = new Mock<IObjectEditor> (); + editor.SetupGet (e => e.Target).Returns (target); + editor.SetupGet (e => e.Properties).Returns (new[] { property.Object }); + SetupPropertySetAndGet (editor, property.Object); + + TValue value = GetRandomTestValue (); + + await editor.Object.SetValueAsync (property.Object, new ValueInfo<TValue> { + Value = value, + ValueDescriptor = modes[1] + }); + + var vm = GetViewModel (property.Object, editor.Object); + Assert.That (vm.InputMode, Is.EqualTo (modes[1])); + } + + [TestCase (true)] + [TestCase (false)] + public void InputEnabled (bool writeEnabled) + { + var property = GetPropertyMock (); + property.SetupGet (pi => pi.CanWrite).Returns (writeEnabled); + + var vm = GetViewModel (property.Object, new MockObjectEditor (property.Object)); + Assert.That (vm.IsInputEnabled, Is.EqualTo (writeEnabled)); + } + + [TestCase (true, false, true)] + [TestCase (true, true, false)] + [TestCase (false, true, false)] + [TestCase (false, false, false)] + public void InputEnabledSingleValueInputMode (bool writeEnabled, bool singleValue, bool expectation) + { + var modes = new[] { new InputMode ("TestMode"), new InputMode ("TestMode2", singleValue), }; + + var property = GetPropertyMock (); + property.SetupGet (pi => pi.CanWrite).Returns (writeEnabled); + var input = property.As<IHaveInputModes> (); + input.SetupGet (im => im.InputModes).Returns (modes); + + var target = new object (); + var editor = new Mock<IObjectEditor> (); + editor.SetupGet (e => e.Target).Returns (target); + editor.SetupGet (e => e.Properties).Returns (new[] { property.Object }); + SetupPropertySetAndGet (editor, property.Object); + + var vm = GetViewModel (property.Object, editor.Object); + Assume.That (vm.InputMode, Is.EqualTo (modes[0])); + Assume.That (vm.IsInputEnabled, Is.EqualTo (writeEnabled), "Initial state didn't match property"); + + bool changed = false; + vm.PropertyChanged += (o, e) => { + if (e.PropertyName == nameof(PropertyViewModel<TValue>.IsInputEnabled)) + changed = true; + }; + + vm.InputMode = modes[1]; + + Assert.That (changed, Is.EqualTo (writeEnabled != expectation)); + Assert.That (vm.IsInputEnabled, Is.EqualTo (expectation)); + } + protected TViewModel GetViewModel (IPropertyInfo property, IObjectEditor editor) { return GetViewModel (property, new[] { editor }); |