Skip to content

Commit

Permalink
Handle null/empty PropertyName in the INPC.PropertyChanged event (#8418
Browse files Browse the repository at this point in the history
…) Fixes #1111

* Remove duplicate classes and fix issue

* Fix all overloads and added tests
  • Loading branch information
jfversluis authored Jul 27, 2022
1 parent 5ae30e0 commit 7bc3da8
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 192 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

15 changes: 15 additions & 0 deletions src/Controls/src/Core/PropertyChangedEventArgsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ internal static class PropertyChangedEventArgsExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Is(this PropertyChangedEventArgs args, BindableProperty property)
{
if (string.IsNullOrEmpty(args.PropertyName))
return true;

return args.PropertyName == property.PropertyName;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOneOf(this PropertyChangedEventArgs args, BindableProperty p0, BindableProperty p1)
{
if (string.IsNullOrEmpty(args.PropertyName))
return true;

return args.PropertyName == p0.PropertyName ||
args.PropertyName == p1.PropertyName;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOneOf(this PropertyChangedEventArgs args, BindableProperty p0, BindableProperty p1, BindableProperty p2)
{
if (string.IsNullOrEmpty(args.PropertyName))
return true;

return args.PropertyName == p0.PropertyName ||
args.PropertyName == p1.PropertyName ||
args.PropertyName == p2.PropertyName;
Expand All @@ -29,6 +38,9 @@ public static bool IsOneOf(this PropertyChangedEventArgs args, BindableProperty
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOneOf(this PropertyChangedEventArgs args, BindableProperty p0, BindableProperty p1, BindableProperty p2, BindableProperty p3)
{
if (string.IsNullOrEmpty(args.PropertyName))
return true;

return args.PropertyName == p0.PropertyName ||
args.PropertyName == p1.PropertyName ||
args.PropertyName == p2.PropertyName ||
Expand All @@ -38,6 +50,9 @@ public static bool IsOneOf(this PropertyChangedEventArgs args, BindableProperty
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOneOf(this PropertyChangedEventArgs args, BindableProperty p0, BindableProperty p1, BindableProperty p2, BindableProperty p3, BindableProperty p4)
{
if (string.IsNullOrEmpty(args.PropertyName))
return true;

return args.PropertyName == p0.PropertyName ||
args.PropertyName == p1.PropertyName ||
args.PropertyName == p2.PropertyName ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System.ComponentModel;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Core.UnitTests
{
[TestFixture]
public class PropertyChangedEventArgsExtensionsTests : BaseTestFixture
{
[Test]
public void IsWithMathingStringValues()
{
PropertyChangedEventArgs args = new("MyProperty");
BindableProperty bindable = BindableProperty.Create("MyProperty", typeof(object), typeof(object));

Assert.IsTrue(args.Is(bindable));
}

[Test]
public void IsWithNonMathingValues()
{
PropertyChangedEventArgs args = new("MyProperty");
BindableProperty bindable = BindableProperty.Create("NotMyProperty", typeof(object), typeof(object));

Assert.IsFalse(args.Is(bindable));
}

[Test]
[TestCase("")]
[TestCase(null)]
public void IsWithNullOrEmptyValue(string changedPropertyName)
{
PropertyChangedEventArgs args = new(changedPropertyName);
BindableProperty bindable = BindableProperty.Create("MyProperty", typeof(object), typeof(object));

Assert.IsTrue(args.Is(bindable));
}

[Test]
public void IsOneOfTwoParameters()
{
PropertyChangedEventArgs args = new("MyProperty");
BindableProperty bindable0 = BindableProperty.Create("MyProperty", typeof(object), typeof(object));
BindableProperty bindable1 = BindableProperty.Create("AlsoMyProperty", typeof(object), typeof(object));

Assert.IsTrue(args.IsOneOf(bindable0, bindable1));
}

[Test]
public void IsOneOfThreeParameters()
{
PropertyChangedEventArgs args = new("MyProperty");
BindableProperty bindable0 = BindableProperty.Create("MyProperty", typeof(object), typeof(object));
BindableProperty bindable1 = BindableProperty.Create("AlsoMyProperty", typeof(object), typeof(object));
BindableProperty bindable2 = BindableProperty.Create("AlsoMyProperty1", typeof(object), typeof(object));

Assert.IsTrue(args.IsOneOf(bindable0, bindable1, bindable2));
}

[Test]
public void IsOneOfFourParameters()
{
PropertyChangedEventArgs args = new("MyProperty");
BindableProperty bindable0 = BindableProperty.Create("MyProperty", typeof(object), typeof(object));
BindableProperty bindable1 = BindableProperty.Create("AlsoMyProperty", typeof(object), typeof(object));
BindableProperty bindable2 = BindableProperty.Create("AlsoMyProperty1", typeof(object), typeof(object));
BindableProperty bindable3 = BindableProperty.Create("NotMyProperty", typeof(object), typeof(object));

Assert.IsTrue(args.IsOneOf(bindable0, bindable1, bindable2, bindable3));
}

[Test]
public void IsOneOfFiveParameters()
{
PropertyChangedEventArgs args = new("MyProperty");
BindableProperty bindable0 = BindableProperty.Create("MyProperty", typeof(object), typeof(object));
BindableProperty bindable1 = BindableProperty.Create("AlsoMyProperty", typeof(object), typeof(object));
BindableProperty bindable2 = BindableProperty.Create("AlsoMyProperty1", typeof(object), typeof(object));
BindableProperty bindable3 = BindableProperty.Create("NotMyProperty", typeof(object), typeof(object));
BindableProperty bindable4 = BindableProperty.Create("AlsNotMyProperty", typeof(object), typeof(object));

Assert.IsTrue(args.IsOneOf(bindable0, bindable1, bindable2, bindable3, bindable4));
}

[Test]
[TestCase("")]
[TestCase(null)]
public void IsOneOfTwoParametersNullOrEmptyValue(string changedPropertyName)
{
PropertyChangedEventArgs args = new(changedPropertyName);
BindableProperty bindable0 = BindableProperty.Create("MyProperty", typeof(object), typeof(object));
BindableProperty bindable1 = BindableProperty.Create("AlsoMyProperty", typeof(object), typeof(object));

Assert.IsTrue(args.IsOneOf(bindable0, bindable1));
}

[Test]
[TestCase("")]
[TestCase(null)]
public void IsOneOfThreeParametersNullOrEmptyValue(string changedPropertyName)
{
PropertyChangedEventArgs args = new(changedPropertyName);
BindableProperty bindable0 = BindableProperty.Create("MyProperty", typeof(object), typeof(object));
BindableProperty bindable1 = BindableProperty.Create("AlsoMyProperty", typeof(object), typeof(object));
BindableProperty bindable2 = BindableProperty.Create("AlsoMyProperty1", typeof(object), typeof(object));

Assert.IsTrue(args.IsOneOf(bindable0, bindable1, bindable2));
}

[Test]
[TestCase("")]
[TestCase(null)]
public void IsOneOfFourParametersNullOrEmptyValue(string changedPropertyName)
{
PropertyChangedEventArgs args = new(changedPropertyName);
BindableProperty bindable0 = BindableProperty.Create("MyProperty", typeof(object), typeof(object));
BindableProperty bindable1 = BindableProperty.Create("AlsoMyProperty", typeof(object), typeof(object));
BindableProperty bindable2 = BindableProperty.Create("AlsoMyProperty1", typeof(object), typeof(object));
BindableProperty bindable3 = BindableProperty.Create("NotMyProperty", typeof(object), typeof(object));

Assert.IsTrue(args.IsOneOf(bindable0, bindable1, bindable2, bindable3));
}

[Test]
[TestCase("")]
[TestCase(null)]
public void IsOneOfFiveParametersNullOrEmptyValue(string changedPropertyName)
{
PropertyChangedEventArgs args = new(changedPropertyName);
BindableProperty bindable0 = BindableProperty.Create("MyProperty", typeof(object), typeof(object));
BindableProperty bindable1 = BindableProperty.Create("AlsoMyProperty", typeof(object), typeof(object));
BindableProperty bindable2 = BindableProperty.Create("AlsoMyProperty1", typeof(object), typeof(object));
BindableProperty bindable3 = BindableProperty.Create("NotMyProperty", typeof(object), typeof(object));
BindableProperty bindable4 = BindableProperty.Create("AlsNotMyProperty", typeof(object), typeof(object));

Assert.IsTrue(args.IsOneOf(bindable0, bindable1, bindable2, bindable3, bindable4));
}
}
}

0 comments on commit 7bc3da8

Please sign in to comment.