Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix pointer released event not being handled on ImageButton #21766

Merged
merged 9 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue21706"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
<VerticalStackLayout>
<ImageButton
Source="groceries.png"
WidthRequest="200"
HeightRequest="200"
AutomationId="WaitForElement"
x:Name="PointerImageButton">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup
x:Name="CommonStates">
<VisualState
x:Name="Normal">
<VisualState.Setters>
<Setter
Property="BackgroundColor" Value="Blue" />
</VisualState.Setters>
</VisualState>
<VisualState
x:Name="PointerOver">
<VisualState.Setters>
<Setter
Property="BackgroundColor" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</ImageButton>

<Button
AutomationId="OtherButton"
WidthRequest="200"
HeightRequest="200"
Text="Other"/>
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 21706, "ImageButton stuck in PointerOver state", PlatformAffected.UWP)]
public partial class Issue21706 : ContentPage
{
public Issue21706()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
FontSize="Large"
BackgroundColor="Pink"
HorizontalTextAlignment="Center" />
</FlexLayout>
</FlexLayout>
</views:BasePage.Content>
</views:BasePage>
25 changes: 25 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue21706.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue21706 : _IssuesUITest
{
public override string Issue => "ImageButton stuck in PointerOver state";

public Issue21706(TestDevice device) : base(device) { }

[Test]
public async Task ImageButtonStuckAfterRightClick()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Android, TestDevice.Mac, TestDevice.iOS });
Copy link
Member

@jfversluis jfversluis May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason we want to not test this on the other platforms?

I get that this is about clicks/hovers so it might not work on some platforms, but if it reasonably can work, why not let them run there as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the ticket mentioned that it already worked fine in macOS, so I wrote the test to only check on Win. The Appium test does specifically use the pointer right click, so we could enable it for macOS as well.

App.WaitForElement("WaitForElement");
App.RightClick("WaitForElement");
await Task.Delay(200);
App.Click("OtherButton");
await Task.Delay(200);
VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public partial class ImageButtonHandler : ViewHandler<IImageButton, Button>
Image? _image;

PointerEventHandler? _pointerPressedHandler;
PointerEventHandler? _pointerReleasedHandler;

protected override Button CreatePlatformView()
{
Expand All @@ -34,6 +35,7 @@ protected override Button CreatePlatformView()
protected override void ConnectHandler(Button platformView)
{
_pointerPressedHandler = new PointerEventHandler(OnPointerPressed);
_pointerReleasedHandler = new PointerEventHandler(OnPointerReleased);

if (_image != null)
{
Expand All @@ -43,6 +45,7 @@ protected override void ConnectHandler(Button platformView)

platformView.Click += OnClick;
platformView.AddHandler(UIElement.PointerPressedEvent, _pointerPressedHandler, true);
platformView.AddHandler(UIElement.PointerReleasedEvent, _pointerReleasedHandler, true);

base.ConnectHandler(platformView);
}
Expand All @@ -57,8 +60,10 @@ protected override void DisconnectHandler(Button platformView)

platformView.Click -= OnClick;
platformView.RemoveHandler(UIElement.PointerPressedEvent, _pointerPressedHandler);
platformView.RemoveHandler(UIElement.PointerReleasedEvent, _pointerReleasedHandler);

_pointerPressedHandler = null;
_pointerReleasedHandler = null;

base.DisconnectHandler(platformView);

Expand Down Expand Up @@ -93,14 +98,18 @@ public static void MapPadding(IImageButtonHandler handler, IImageButton imageBut
void OnClick(object sender, RoutedEventArgs e)
{
VirtualView?.Clicked();
VirtualView?.Released();
}

void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
VirtualView?.Pressed();
}

void OnPointerReleased(object sender, PointerRoutedEventArgs e)
Foda marked this conversation as resolved.
Show resolved Hide resolved
{
VirtualView?.Released();
}

void OnImageOpened(object sender, RoutedEventArgs routedEventArgs)
{
VirtualView?.UpdateIsLoading(false);
Expand Down
35 changes: 34 additions & 1 deletion src/TestUtils/src/UITest.Appium/Actions/AppiumTouchActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ CommandResponse Tap(IDictionary<string, object> parameters)
{
return CommandResponse.FailedEmptyResponse;
}

if (parameters.TryGetValue("button", out var button) && button != null)
{
var buttonName = button.ToString();
if (!string.IsNullOrEmpty(buttonName) &&
buttonName.Equals("right", StringComparison.OrdinalIgnoreCase))
{
return RightClick(element.Id);
}
}
return TapElement(element);
}
else if (parameters.TryGetValue("x", out var x) &&
Expand Down Expand Up @@ -119,8 +129,31 @@ CommandResponse TapCoordinates(float x, float y)
return CommandResponse.SuccessEmptyResponse;
}

CommandResponse DoubleTap(IDictionary<string, object> parameters)

CommandResponse RightClick(string elementId)
{
// "ActionSequence" and "Actions" is not supported for right click on Windows
if (_appiumApp.GetTestDevice() == TestDevice.Windows)
{
_appiumApp.Driver.ExecuteScript("windows: click", new Dictionary<string, object>
{
{ "elementId", elementId },
{ "button", "right" },
});
}
else if (_appiumApp.GetTestDevice() == TestDevice.Mac)
{
_appiumApp.Driver.ExecuteScript("macos: rightClick", new Dictionary<string, object>
{
{ "elementId", elementId }
});
}

return CommandResponse.SuccessEmptyResponse;
}

CommandResponse DoubleTap(IDictionary<string, object> parameters)
{
var element = GetAppiumElement(parameters["element"]);

OpenQA.Selenium.Appium.Interactions.PointerInputDevice touchDevice = new OpenQA.Selenium.Appium.Interactions.PointerInputDevice(PointerKind.Touch);
Expand Down
10 changes: 10 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public static void Click(this IApp app, string element)
app.FindElement(element).Click();
}

public static void RightClick(this IApp app, string element)
{
var uiElement = app.FindElement(element);
uiElement.Command.Execute("click", new Dictionary<string, object>()
{
{ "element", uiElement },
{ "button", "right" }
});
}

public static string? GetText(this IUIElement element)
{
var response = element.Command.Execute("getText", new Dictionary<string, object>()
Expand Down
Loading