Skip to content

Commit

Permalink
Ensure TouchBehavior.LongPressCompleted event fires when `LongPress…
Browse files Browse the repository at this point in the history
…Command is null` (#2239)

* Use `CanExecute` to match TapGesture

* Create GestureManagerTests.cs

* Update GestureManagerTests.cs

* Rename `sender` -> `touchBehavior`

* Resolve failing Unit Tests

* Update GestureManagerTests.cs

---------

Co-authored-by: Shaun Lawrence <[email protected]>
  • Loading branch information
brminnick and bijington authored Oct 4, 2024
1 parent 95cff18 commit 1319fb9
Show file tree
Hide file tree
Showing 2 changed files with 301 additions and 178 deletions.
123 changes: 123 additions & 0 deletions src/CommunityToolkit.Maui.UnitTests/Behaviors/GestureManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Maui.Core;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Behaviors;

public class GestureManagerTests : BaseTest
{
[Fact]
public void HandleUserInteraction_ShouldUpdateInteractionStatus()
{
// Arrange
var touchBehavior = new TouchBehavior();
var interactionStatus = TouchInteractionStatus.Completed;

// Act
GestureManager.HandleUserInteraction(in touchBehavior, in interactionStatus);

// Assert
Assert.Equal(interactionStatus, touchBehavior.CurrentInteractionStatus);
}

[Theory]
[InlineData(HoverStatus.Entered, HoverState.Hovered)]
[InlineData(HoverStatus.Exited, HoverState.Default)]
public void HandleHover_ShouldUpdateHoverStateAndStatus(HoverStatus hoverStatus, HoverState expectedHoverState)
{
// Arrange
var touchBehavior = new TouchBehavior
{
IsEnabled = true
};

// Act
GestureManager.HandleHover(in touchBehavior, in hoverStatus);

// Assert
Assert.Equal(expectedHoverState, touchBehavior.CurrentHoverState);
Assert.Equal(hoverStatus, touchBehavior.CurrentHoverStatus);
}

[Theory]
[InlineData(TouchStatus.Started, true, TouchState.Pressed)]
[InlineData(TouchStatus.Completed, true, TouchState.Default)]
[InlineData(TouchStatus.Canceled, false, TouchState.Default)]
public void HandleTouch_ShouldUpdateTouchStatusAndState(TouchStatus status, bool isEnabled, TouchState expectedTouchState)
{
// Arrange
var touchBehavior = new TouchBehavior
{
IsEnabled = isEnabled,
Element = new Label()
};
var tappedCompletedRaised = false;
touchBehavior.TouchGestureCompleted += HandleTouchGestureCompleted;

// Act
var gestureManager = new GestureManager();
gestureManager.HandleTouch(in touchBehavior, in status);

// Assert
Assert.Equal(expectedTouchState, touchBehavior.CurrentTouchState);
Assert.Equal(status is TouchStatus.Completed && isEnabled, tappedCompletedRaised);

void HandleTouchGestureCompleted(object? sender, TouchGestureCompletedEventArgs e)
{
touchBehavior.TouchGestureCompleted -= HandleTouchGestureCompleted;
tappedCompletedRaised = true;
}
}

[Fact]
public async Task ChangeStateAsync_ShouldUpdateVisualState()
{
// Arrange
var touchBehavior = new TouchBehavior
{
IsEnabled = true,
Element = new Label()
};
var token = new CancellationTokenSource().Token;

// Act
var gestureManager = new GestureManager();
await gestureManager.ChangeStateAsync(touchBehavior, false, token);

// Assert
Assert.Equal(TouchState.Default, touchBehavior.CurrentTouchState);
Assert.Equal(HoverState.Default, touchBehavior.CurrentHoverState);
}

[Fact(Timeout = (int)TestDuration.Short)]
public async Task HandleLongPress_ShouldRaiseLongPressCompleted()
{
// Arrange
var touchBehavior = new TouchBehavior
{
IsEnabled = true,
Element = new Button(),
CurrentTouchState = TouchState.Pressed,
CurrentInteractionStatus = TouchInteractionStatus.Started,
LongPressDuration = 100,
LongPressCommand = null
};
var longPressCompletedRaised = false;
touchBehavior.LongPressCompleted += HandleLongPressCompleted;

// Act
var gestureManager = new GestureManager();
var token = new CancellationTokenSource().Token;
await gestureManager.HandleLongPress(touchBehavior, token);

// Assert
Assert.True(longPressCompletedRaised);


void HandleLongPressCompleted(object? sender, LongPressCompletedEventArgs e)
{
touchBehavior.LongPressCompleted -= HandleLongPressCompleted;
longPressCompletedRaised = true;
}
}
}
Loading

0 comments on commit 1319fb9

Please sign in to comment.