Skip to content

[release/8.0.1xx-sr9] [C] avoid NRE on null GestureRecognizer #25029

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

Merged
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
13 changes: 8 additions & 5 deletions src/Controls/src/Core/View/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ void AddItems(IList newItems)
foreach (IElementDefinition item in newItems)
{
var gestureRecognizer = item as IGestureRecognizer;
ValidateGesture(gestureRecognizer);
item.Parent = this;
GestureController.CompositeGestureRecognizers.Add(gestureRecognizer);
if (ValidateGesture(gestureRecognizer))
{
item.Parent = this;
GestureController.CompositeGestureRecognizers.Add(gestureRecognizer);
}
}
}

Expand Down Expand Up @@ -266,12 +268,13 @@ static void MarginPropertyChanged(BindableObject bindable, object oldValue, obje
((View)bindable).InvalidateMeasureInternal(InvalidationTrigger.MarginChanged);
}

void ValidateGesture(IGestureRecognizer gesture)
bool ValidateGesture(IGestureRecognizer gesture)
{
if (gesture == null)
return;
return false;
if (gesture is PinchGestureRecognizer && _gestureRecognizers.GetGesturesFor<PinchGestureRecognizer>().Count() > 1)
throw new InvalidOperationException($"Only one {nameof(PinchGestureRecognizer)} per view is allowed");
return true;
}

#nullable enable
Expand Down
20 changes: 20 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui24900.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui24900">
<Border>
<Border.GestureRecognizers>
<OnPlatform x:TypeArguments="GestureRecognizer">
<OnPlatform.Platforms>
<On Platform="Android">
<TapGestureRecognizer Tapped="Header_Tapped" />
</On>
<!-- <On Platform="WinUI">
<TapGestureRecognizer Tapped="Header_Tapped" />
</On> -->
</OnPlatform.Platforms>
</OnPlatform>
</Border.GestureRecognizers>
</Border>
</ContentPage>
65 changes: 65 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui24900.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Dispatching;

using Microsoft.Maui.Graphics;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui24900 : ContentPage
{
public Maui24900()
{
InitializeComponent();
}

public Maui24900(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

void Header_Tapped(object sender, TappedEventArgs e)
{

}

[TestFixture]
class Test
{
MockDeviceInfo mockDeviceInfo;

[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DeviceInfo.SetCurrent(mockDeviceInfo = new MockDeviceInfo());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}


[TearDown] public void TearDown()
{
AppInfo.SetCurrent(null);
DeviceInfo.SetCurrent(null);
}

[Test]
public void OnPlatformDownNotThrow([Values(false, true)] bool useCompiledXaml)
{
mockDeviceInfo.Platform = DevicePlatform.WinUI;
Assert.DoesNotThrow(()=> new Maui24900(useCompiledXaml));



}
}
}