diff options
3 files changed, 92 insertions, 1 deletions
diff --git a/Xamarin.PropertyEditing.Tests/PanelGroupViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PanelGroupViewModelTests.cs new file mode 100644 index 0000000..62389ac --- /dev/null +++ b/Xamarin.PropertyEditing.Tests/PanelGroupViewModelTests.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Moq; +using NUnit.Framework; +using Xamarin.PropertyEditing.ViewModels; + +namespace Xamarin.PropertyEditing.Tests +{ + [TestFixture] + internal class PanelGroupViewModelTests + { + [Test] + public void HasChildElementsUpdates () + { + var property = new Mock<IPropertyInfo> (); + property.Setup (p => p.Name).Returns ("Name"); + property.Setup (p => p.Category).Returns ("Category"); + property.Setup (p => p.Type).Returns (typeof(string)); + property.Setup (p => p.IsUncommon).Returns (false); + + var editor = new MockObjectEditor (property.Object); + var propertyVm = new StringPropertyViewModel (MockEditorProvider.MockPlatform, property.Object, new[] { editor }); + var group = new PanelGroupViewModel (MockEditorProvider.MockPlatform, "Category", new[] { propertyVm }); + + Assert.That (group.HasChildElements, Is.True); + + bool changed = false; + group.PropertyChanged += (sender, args) => { + if (args.PropertyName == nameof(PanelGroupViewModel.HasChildElements)) + changed = true; + }; + + group.Remove (propertyVm); + Assert.That (group.HasChildElements, Is.False); + Assert.That (changed, Is.True, "INPC did not fire for HasChildElements during remove"); + changed = false; + + group.Add (propertyVm); + Assert.That (group.HasChildElements, Is.True); + Assert.That (changed, Is.True, "INPC did not fire for HasChildElements during add"); + } + + [Test] + public void HasUncommonElementsUpdates () + { + var property = new Mock<IPropertyInfo> (); + property.Setup (p => p.Name).Returns ("Name"); + property.Setup (p => p.Category).Returns ("Category"); + property.Setup (p => p.Type).Returns (typeof (string)); + property.Setup (p => p.IsUncommon).Returns (true); + + var editor = new MockObjectEditor (property.Object); + var propertyVm = new StringPropertyViewModel (MockEditorProvider.MockPlatform, property.Object, new[] { editor }); + var group = new PanelGroupViewModel (MockEditorProvider.MockPlatform, "Category", new[] { propertyVm }); + + Assert.That (group.HasChildElements, Is.True); + Assert.That (group.HasUncommonElements, Is.True); + + bool childChanged = false, uncommonChanged = false; + group.PropertyChanged += (sender, args) => { + if (args.PropertyName == nameof (PanelGroupViewModel.HasChildElements)) + childChanged = true; + else if (args.PropertyName == nameof(PanelGroupViewModel.HasUncommonElements)) + uncommonChanged = true; + }; + + group.Remove (propertyVm); + Assert.That (group.HasChildElements, Is.False); + Assert.That (group.HasUncommonElements, Is.False); + Assert.That (childChanged, Is.True, "INPC did not fire for HasChildElements during remove"); + Assert.That (uncommonChanged, Is.True, "INPC did not fire for HasUncommonElements during remove"); + childChanged = false; + + group.Add (propertyVm); + Assert.That (group.HasChildElements, Is.True); + Assert.That (group.HasUncommonElements, Is.True); + Assert.That (childChanged, Is.True, "INPC did not fire for HasChildElements during add"); + Assert.That (uncommonChanged, Is.True, "INPC did not fire for HasUncommonElements during add"); + } + } +} diff --git a/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj b/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj index 85d1e3a..683293f 100644 --- a/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj +++ b/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj @@ -77,6 +77,7 @@ <Compile Include="NumericTests.cs" /> <Compile Include="NumericViewModelTests.cs" /> <Compile Include="OrderedDictionaryTests.cs" /> + <Compile Include="PanelGroupViewModelTests.cs" /> <Compile Include="ResourceSelectorViewModelTests.cs" /> <Compile Include="ResourceTests.cs" /> <Compile Include="SimpleCollectionViewTests.cs" /> diff --git a/Xamarin.PropertyEditing/ViewModels/PanelViewModel.cs b/Xamarin.PropertyEditing/ViewModels/PanelViewModel.cs index 6aaca99..1d9e7a1 100644 --- a/Xamarin.PropertyEditing/ViewModels/PanelViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/PanelViewModel.cs @@ -69,7 +69,13 @@ namespace Xamarin.PropertyEditing.ViewModels } } - return list.Remove (editor); + bool result = list.Remove (editor); + if (result) { + OnPropertyChanged (nameof(HasChildElements)); + OnPropertyChanged (nameof (HasUncommonElements)); + } + + return result; } public bool GetIsExpanded (PropertyArrangeMode mode) |