Skip to content

Commit

Permalink
[Android] Removing TapGestureRecognizer with at least 2 taps causes E…
Browse files Browse the repository at this point in the history
…xception - fix (#21714)

* [Android] Added null checks (#21437)

* Updated test

* Updated test

* Update Issue21513.cs

---------

Co-authored-by: Javier Suárez <[email protected]>
  • Loading branch information
kubaflo and jsuarezruiz authored Aug 17, 2024
1 parent fe89b55 commit 61e984e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public InnerGestureListener(

bool HasAnyGestures()
{
return _panGestureHandler.HasAnyGestures() || _tapGestureHandler.HasAnyGestures() || _swipeGestureHandler.HasAnyGestures();
return (_panGestureHandler?.HasAnyGestures() ?? false) || (_tapGestureHandler?.HasAnyGestures() ?? false) || (_swipeGestureHandler?.HasAnyGestures() ?? false);
}

// This is needed because GestureRecognizer callbacks can be delayed several hundred milliseconds
Expand Down
21 changes: 21 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue21437.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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.Issue21437"
xmlns:issues="clr-namespace:Maui.Controls.Sample.Issues"
Title="Issue21437">
<VerticalStackLayout BindableLayout.ItemsSource="{Binding Items}" HorizontalOptions="Start">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding .}" AutomationId="{Binding .}" HorizontalOptions="Start">
<Label.GestureRecognizers>
<TapGestureRecognizer
NumberOfTapsRequired="2"
Command="{Binding TapCommand, Source={RelativeSource AncestorType={x:Type issues:Issue21437}}}"
CommandParameter="{Binding .}" />
</Label.GestureRecognizers>
</Label>
</DataTemplate>
</BindableLayout.ItemTemplate>
</VerticalStackLayout>
</ContentPage>
21 changes: 21 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue21437.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 21437, "Removing TapGestureRecognizer with at least 2 taps causes Exception", PlatformAffected.Android)]

public partial class Issue21437 : ContentPage
{
public ObservableCollection<string> Items { get; } = new() { "Item1", "Item2", "Item3" };

public Command TapCommand => new Command<string>(obj =>
{
Items.Remove(obj);
});

public Issue21437()
{
InitializeComponent();
BindingContext = this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if ANDROID
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue21437 : _IssuesUITest
{
public override string Issue => "Removing TapGestureRecognizer with at least 2 taps causes Exception";

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

[Test]
[Category(UITestCategories.Gestures)]
public void ExceptionShouldNotBeThrown()
{
_ = App.WaitForElement("Item2");
App.DoubleClick("Item2");

//The test passes if no exception is thrown
}
}
#endif

0 comments on commit 61e984e

Please sign in to comment.