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

Set a GroupName when creating new WinUI RadioButtons #13611

Merged
merged 3 commits into from
Mar 3, 2023
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,13 @@
using Microsoft.Maui.Handlers;

namespace Microsoft.Maui.DeviceTests
{
public partial class RadioButtonTests
{
UI.Xaml.Controls.RadioButton GetNativeRadioButton(RadioButtonHandler radioButtonHandler) =>
radioButtonHandler.PlatformView;

bool GetNativeIsChecked(RadioButtonHandler radioButtonHandler) =>
GetNativeRadioButton(radioButtonHandler).IsChecked ?? false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#if WINDOWS
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.RadioButton)]
public partial class RadioButtonTests : ControlsHandlerTestBase
{
[Theory(DisplayName = "IsChecked Initializes Correctly")]
[InlineData(false)]
[InlineData(true)]
public async Task IsCheckedInitializesCorrectly(bool isChecked)
{
bool xplatIsChecked = isChecked;
var radioButton = new RadioButton() { IsChecked = xplatIsChecked };
bool expectedValue = isChecked;
var layoutFirst = new VerticalStackLayout();
var rdFirst = new RadioButton { GroupName = "FirstGroup", IsChecked = xplatIsChecked };
layoutFirst.Add(rdFirst);
layoutFirst.Add(new RadioButton { GroupName = "FirstGroup" });
layoutFirst.Add(new RadioButton { GroupName = "FirstGroup" });
var layoutSecond = new VerticalStackLayout();
layoutSecond.Add(new RadioButton { GroupName = "SecondGroup" });
var rdSecond = new RadioButton { GroupName = "SecondGroup", IsChecked = xplatIsChecked };
rmarinho marked this conversation as resolved.
Show resolved Hide resolved
layoutSecond.Add(rdSecond);
layoutSecond.Add(new RadioButton { GroupName = "SecondGroup" });
var layout = new VerticalStackLayout
{
layoutFirst,
layoutSecond
};
var valuesFirst = await GetValueAsync(rdFirst, (handler) => { return new { ViewValue = rdFirst.IsChecked, PlatformViewValue = GetNativeIsChecked(handler as RadioButtonHandler) }; });
var valuesSecond = await GetValueAsync(rdSecond, (handler) => { return new { ViewValue = rdSecond.IsChecked, PlatformViewValue = GetNativeIsChecked(handler as RadioButtonHandler) }; });
Assert.Equal(xplatIsChecked, valuesFirst.ViewValue);
Assert.Equal(expectedValue, valuesFirst.PlatformViewValue);
Assert.Equal(xplatIsChecked, valuesSecond.ViewValue);
Assert.Equal(expectedValue, valuesSecond.PlatformViewValue);
}
}
}
#endif
1 change: 1 addition & 0 deletions src/Controls/tests/DeviceTests/TestCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static class TestCategory
public const string NavigationPage = "NavigationPage";
public const string Page = "Page";
public const string Picker = "Picker";
public const string RadioButton = "RadioButton";
public const string ScrollView = "ScrollView";
public const string SearchBar = "SearchBar";
public const string Shell = "Shell";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using Microsoft.UI.Xaml;
using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.Handlers
{
public partial class RadioButtonHandler : ViewHandler<IRadioButton, RadioButton>
{
protected override RadioButton CreatePlatformView() => new RadioButton();
protected override RadioButton CreatePlatformView()
{
// Note: We set a random GUID as the GroupName as part of the work-around in https://github.com/dotnet/maui/issues/11418
return new RadioButton() { GroupName = Guid.NewGuid().ToString() };
}

protected override void ConnectHandler(RadioButton platformView)
{
Expand Down