Skip to content

Commit

Permalink
[iOS] TapGestureRecognizer should not fire when view is not enabled (#…
Browse files Browse the repository at this point in the history
…23049)

* Don't receive touch when view is disabled

* Add UITest for Single Tap enabled/disabled

This removes the first pass unit test version which isn't really testing the scenario properly.

* Fix element automation id

* Don't fire tap for windows if control disabled

* Make failure case text less confusing
  • Loading branch information
Redth committed Jun 18, 2024
1 parent 0f6a922 commit 51720b9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ void OnTap(object sender, RoutedEventArgs e)
if (view == null)
return;

if (!view.IsEnabled)
{
return;
}

var tapPosition = e.GetPositionRelativeToPlatformElement(Control);

if (tapPosition == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,11 @@ public bool ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
return false;
}

if (!virtualView.IsEnabled)
{
return false;
}

if (touch.View == platformView)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,35 @@ public void DoubleTap()
var result = App.FindElement("DoubleTapResults").GetText();
ClassicAssert.AreEqual("Success", result);
}

[Test]
[Category(UITestCategories.Gestures)]
public void SingleTap()
{
App.WaitForElement("TargetView");
App.EnterText("TargetView", "SingleTapGallery");
App.Tap("GoButton");

App.WaitForElement("SingleTapSurface");
App.Tap("SingleTapSurface");

var result = App.FindElement("SingleTapGestureResults").GetText();
ClassicAssert.AreEqual("Success", result);
}

[Test]
[Category(UITestCategories.Gestures)]
public void DisabledSingleTap()
{
App.WaitForElement("TargetView");
App.EnterText("TargetView", "SingleTapGallery");
App.Tap("GoButton");

App.WaitForElement("DisabledTapSurface");
App.Tap("DisabledTapSurface");

var result = App.FindElement("DisabledTapGestureResults").GetText();
ClassicAssert.AreNotEqual("Failed", result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public GestureRecognizerGallery()
{
Add(new PointerGestureRecognizerEvents());
Add(new DoubleTapGallery());
Add(new SingleTapGallery());
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions src/Controls/tests/TestCases/Elements/TapGestureGallery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample
{
public class SingleTapGallery : Microsoft.Maui.Controls.ContentView
{
public SingleTapGallery()
{
AutomationId = nameof(SingleTapGallery);

var layout = new VerticalStackLayout { Margin = 10, Spacing = 10 };

var singleTapResults = new Label { AutomationId = "SingleTapGestureResults", Text = "Should succeed."};
var singleTapSurface = new Grid()
{
HeightRequest = 100,
WidthRequest = 200,
BackgroundColor = Microsoft.Maui.Graphics.Colors.AliceBlue,
Children = { new Label { Text = "SingleTapSurface", AutomationId = "SingleTapSurface" } }
};

var singleTapRecognizer = new TapGestureRecognizer();
singleTapRecognizer.Tapped += (sender, args) => { singleTapResults.Text = "Success"; };
singleTapSurface.GestureRecognizers.Add(singleTapRecognizer);

layout.Add(singleTapSurface);
layout.Add(singleTapResults);


// Now add a tap surface that is disabled and a results label that should not change when the surface is tapped
var disabledTapResults = new Label { AutomationId = "DisabledTapGestureResults", Text = "Should not change when tapped" };
var disabledTapSurface = new Grid()
{
IsEnabled = false, // Disabled
HeightRequest = 100,
WidthRequest = 200,
BackgroundColor = Microsoft.Maui.Graphics.Colors.Bisque,
Children = { new Label { Text = "DisabledTapSurface", AutomationId = "DisabledTapSurface" } }
};

// Wire up the tap gesture recognizer, it should never fire
var disabledTapRecognizer = new TapGestureRecognizer();
disabledTapRecognizer.Tapped += (sender, args) => { disabledTapResults.Text = "Failed"; };
disabledTapSurface.GestureRecognizers.Add(disabledTapRecognizer);

layout.Add(disabledTapSurface);
layout.Add(disabledTapResults);

Content = layout;
}
}
}

0 comments on commit 51720b9

Please sign in to comment.