diff options
author | Eric Maupin <ermaup@microsoft.com> | 2018-09-17 21:01:03 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2018-09-18 00:16:59 +0300 |
commit | 02f43060012c53246f0bb5cd848b96e45bdc59ef (patch) | |
tree | 605dff35518d6963f908ed2aee9e1836d832d2de | |
parent | 6988236fac0452757dca56b4e472d96d32e64f8d (diff) |
[Core] If unconstrained, respect ValueDescriptor
The placement of this is important. Since the value type is likely an
enum, it's not going to have an out of bounds default() value. So,
despite only setting the ValueDescriptor, Value will still look like a
valid value, so ValueDescriptor takes presedence.
-rw-r--r-- | Xamarin.PropertyEditing.Tests/PredefinedValuesViewModelTests.cs | 65 | ||||
-rw-r--r-- | Xamarin.PropertyEditing/ViewModels/PredefinedValuesViewModel.cs | 8 |
2 files changed, 56 insertions, 17 deletions
diff --git a/Xamarin.PropertyEditing.Tests/PredefinedValuesViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PredefinedValuesViewModelTests.cs index bb33343..f62b7d6 100644 --- a/Xamarin.PropertyEditing.Tests/PredefinedValuesViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PredefinedValuesViewModelTests.cs @@ -270,14 +270,38 @@ namespace Xamarin.PropertyEditing.Tests [TestFixture] internal class UnconstrainedPredefinedViewModelTests - : PredefinedValuesViewModelTests<string> + : PredefinedValuesViewModelTests<int> { [Test] + public async Task ValueNameRepresentsValueDescriptor () + { + var value = GetNonDefaultRandomTestValue (); + + var property = GetPropertyMock (); + var predefined = property.As<IHavePredefinedValues<int>> (); + predefined.SetupGet (p => p.PredefinedValues).Returns (new Dictionary<string, int> { + { "Value", 0 }, + }); + predefined.SetupGet (p => p.IsConstrainedToPredefined).Returns (false); + + var outOfBounds = "random"; + + var editor = GetBasicEditor (property.Object); + await editor.SetValueAsync (property.Object, new ValueInfo<int> { + Source = ValueSource.Local, + ValueDescriptor = outOfBounds + }); + + var vm = GetViewModel (property.Object, new[] { editor }); + Assert.That (vm.ValueName, Is.EqualTo (outOfBounds)); + } + + [Test] public void ValueDescriptorForUnconstrained () { var property = GetPropertyMock (); - var predefined = property.As<IHavePredefinedValues<string>> (); - predefined.SetupGet (p => p.PredefinedValues).Returns (new Dictionary<string, string> { + var predefined = property.As<IHavePredefinedValues<int>> (); + predefined.SetupGet (p => p.PredefinedValues).Returns (new Dictionary<string, int> { { "Value", GetNonDefaultRandomTestValue () }, }); predefined.SetupGet (p => p.IsConstrainedToPredefined).Returns (false); @@ -289,7 +313,7 @@ namespace Xamarin.PropertyEditing.Tests vm.ValueName = "test"; - var info = editor.values[property.Object] as ValueInfo<string>; + var info = editor.values[property.Object] as ValueInfo<int>; Assert.That (info, Is.Not.Null); Assert.That (info.ValueDescriptor, Is.EqualTo ("test")); Assert.That (info.Source, Is.EqualTo (ValueSource.Local)); @@ -300,8 +324,8 @@ namespace Xamarin.PropertyEditing.Tests public void ValueDescriptorEmptyNotNullUnconstrained () { var property = GetPropertyMock (); - var predefined = property.As<IHavePredefinedValues<string>> (); - predefined.SetupGet (p => p.PredefinedValues).Returns (new Dictionary<string, string> { + var predefined = property.As<IHavePredefinedValues<int>> (); + predefined.SetupGet (p => p.PredefinedValues).Returns (new Dictionary<string, int> { { "Value", GetNonDefaultRandomTestValue () }, }); predefined.SetupGet (p => p.IsConstrainedToPredefined).Returns (false); @@ -314,28 +338,39 @@ namespace Xamarin.PropertyEditing.Tests vm.ValueName = "test"; vm.ValueName = null; - var info = editor.values[property.Object] as ValueInfo<string>; + var info = editor.values[property.Object] as ValueInfo<int>; Assert.That (info, Is.Not.Null); Assert.That (info.ValueDescriptor, Is.EqualTo (String.Empty)); Assert.That (info.Source, Is.EqualTo (ValueSource.Local)); } - protected override string GetRandomTestValue (Random rand) + protected override int GetRandomTestValue (Random rand) { - string[] names = Enum.GetNames (typeof(PredefinedEnumTest)); - int index = rand.Next (0, names.Length); - return names[index]; + int[] values = (int[])Enum.GetValues (typeof(PredefinedEnumTest)); + int index = rand.Next (0, values.Length); + return values[index]; } - protected override PredefinedValuesViewModel<string> GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) + protected override PredefinedValuesViewModel<int> GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) { - return new PredefinedValuesViewModel<string> (platform, property, editors); + return new PredefinedValuesViewModel<int> (platform, property, editors); } protected override bool IsConstrained => false; - protected override IReadOnlyDictionary<string, string> Values => - Enum.GetNames (typeof(PredefinedEnumTest)).ToDictionary (s => s, s => s); + protected override IReadOnlyDictionary<string, int> Values + { + get + { + string[] names = Enum.GetNames (typeof (PredefinedEnumTest)); + int[] values = (int[])Enum.GetValues (typeof(PredefinedEnumTest)); + var dict = new Dictionary<string, int> (); + for (int i = 0; i < names.Length; i++) + dict.Add (names[i], values[i]); + + return dict; + } + } } [TestFixture] diff --git a/Xamarin.PropertyEditing/ViewModels/PredefinedValuesViewModel.cs b/Xamarin.PropertyEditing/ViewModels/PredefinedValuesViewModel.cs index 1a863c0..2e0dbf7 100644 --- a/Xamarin.PropertyEditing/ViewModels/PredefinedValuesViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/PredefinedValuesViewModel.cs @@ -38,7 +38,7 @@ namespace Xamarin.PropertyEditing.ViewModels public string ValueName { get { return this.valueName; } - set { SetValue (value); } + set { SetValueName (value); } } public bool IsConstrainedToPredefined => this.predefinedValues.IsConstrainedToPredefined; @@ -64,7 +64,7 @@ namespace Xamarin.PropertyEditing.ViewModels private string valueName; private readonly IHavePredefinedValues<TValue> predefinedValues; - private async void SetValue (string value) + private async void SetValueName (string value) { if (value == this.valueName) return; @@ -110,6 +110,10 @@ namespace Xamarin.PropertyEditing.ViewModels if (ValueSource == ValueSource.Unset) { this.valueName = String.Empty; OnPropertyChanged (nameof(ValueName)); + // Order relevant: Value may default() to a valid value, so ValueDescriptor takes precedence + } else if (!IsConstrainedToPredefined && CurrentValue != null && CurrentValue.ValueDescriptor is string custom) { + this.valueName = custom; + OnPropertyChanged (nameof (ValueName)); } else if (TryGetValueName (Value, out string newValueName)) { this.valueName = newValueName; OnPropertyChanged (nameof(ValueName)); |