Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/CommunityToolkit.Maui.UnitTests/Behaviors/BaseBehaviorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CommunityToolkit.Maui.UnitTests.Mocks;
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Behaviors;

public class BaseBehaviorTests
{
[Fact]
public void AttachAndDetachCallsShouldCorrectlyAssignView()
{
var label = new Label();
var mockBehavior = new MockBehavior();

mockBehavior.AssertViewIsNull();
mockBehavior.IsAttached.Should().BeFalse();

label.Behaviors.Add(mockBehavior);

mockBehavior.AssertViewIsEqual(label);
mockBehavior.IsAttached.Should().BeTrue();

label.Behaviors.Remove(mockBehavior);

mockBehavior.AssertViewIsNull();
mockBehavior.IsAttached.Should().BeFalse();
}

[Fact]
public void ViewPropertyChangesShouldBeHandledWithinBehavior()
{
var label = new Label();
var mockBehavior = new MockBehavior();

label.Behaviors.Add(mockBehavior);

mockBehavior.PropertyChanges.Should().BeEmpty();

label.Text = "Text";

mockBehavior.PropertyChanges.Should().ContainSingle();
var change = mockBehavior.PropertyChanges.Single();

change.Should().NotBeNull();
change.PropertyName.Should().Be(nameof(Label.Text));
}
}
35 changes: 35 additions & 0 deletions src/CommunityToolkit.Maui.UnitTests/Mocks/MockBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.ComponentModel;
using CommunityToolkit.Maui.Behaviors;
using FluentAssertions;

namespace CommunityToolkit.Maui.UnitTests.Mocks;

public class MockBehavior : BaseBehavior<Label>
{
internal bool IsAttached { get; private set; }

protected override void OnAttachedTo(Label bindable)
{
base.OnAttachedTo(bindable);
IsAttached = true;
}

protected override void OnDetachingFrom(Label bindable)
{
base.OnDetachingFrom(bindable);
IsAttached = false;
}

internal void AssertViewIsEqual(Label label) => View.Should().Be(label);

internal void AssertViewIsNull() => View.Should().BeNull();

protected override void OnViewPropertyChanged(Label sender, PropertyChangedEventArgs e)
{
base.OnViewPropertyChanged(sender, e);

PropertyChanges.Add(e);
}

internal List<PropertyChangedEventArgs> PropertyChanges { get; } = [];
Comment thread
TheCodeTraveler marked this conversation as resolved.
}
5 changes: 0 additions & 5 deletions src/CommunityToolkit.Maui/Behaviors/BaseBehavior.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ namespace CommunityToolkit.Maui.Behaviors;
/// <typeparam name="TView">The <see cref="VisualElement"/> that the behavior can be applied to</typeparam>
public abstract class BaseBehavior<TView> : Behavior<TView>, ICommunityToolkitBehavior<TView> where TView : VisualElement
{
private protected BaseBehavior()
{

}

/// <summary>
/// View used by the Behavior
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions src/CommunityToolkit.Maui/Converters/BaseConverter.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ namespace CommunityToolkit.Maui.Converters;
/// <typeparam name="TParam">Type of parameter</typeparam>
public abstract class BaseConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] TFrom, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] TTo, TParam> : ValueConverterExtension, ICommunityToolkitValueConverter
{
private protected BaseConverter()
{

}

/// <summary>
/// Default value to return when <see cref="IValueConverter.Convert(object?, Type, object?, CultureInfo?)"/> throws an <see cref="Exception"/>.
/// This value is used when <see cref="Maui.Options.ShouldSuppressExceptionsInConverters"/> is set to <see langword="true"/>.
Expand Down