-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10430 from AvaloniaUI/add-radioButtonAutomationPeer
Add RadioButtonAutomationPeer
- Loading branch information
Showing
6 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Avalonia.Controls/Automation/Peers/RadioButtonAutomationPeer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using Avalonia.Automation; | ||
using Avalonia.Automation.Peers; | ||
using Avalonia.Automation.Provider; | ||
|
||
namespace Avalonia.Controls.Automation.Peers | ||
{ | ||
public class RadioButtonAutomationPeer : ToggleButtonAutomationPeer, ISelectionItemProvider | ||
{ | ||
public RadioButtonAutomationPeer(RadioButton owner) : base(owner) | ||
{ | ||
owner.PropertyChanged += (a, e) => | ||
{ | ||
if (e.Property == RadioButton.IsCheckedProperty) | ||
{ | ||
RaiseToggleStatePropertyChangedEvent((bool?)e.OldValue, (bool?)e.NewValue); | ||
} | ||
}; | ||
} | ||
|
||
override protected string GetClassNameCore() | ||
{ | ||
return "RadioButton"; | ||
} | ||
|
||
override protected AutomationControlType GetAutomationControlTypeCore() | ||
{ | ||
return AutomationControlType.RadioButton; | ||
} | ||
|
||
public bool IsSelected => ((RadioButton)Owner).IsChecked == true; | ||
|
||
public ISelectionProvider? SelectionContainer => null; | ||
|
||
public void AddToSelection() | ||
{ | ||
if (((RadioButton)Owner).IsChecked != true) | ||
throw new InvalidOperationException("Operation cannot be performed"); | ||
} | ||
|
||
public void RemoveFromSelection() | ||
{ | ||
if (((RadioButton)Owner).IsChecked == true) | ||
throw new InvalidOperationException("Operation cannot be performed"); | ||
} | ||
|
||
public void Select() | ||
{ | ||
if (!IsEnabled()) | ||
throw new InvalidOperationException("Element is disabled thus it cannot be selected"); | ||
|
||
((RadioButton)Owner).IsChecked = true; | ||
} | ||
|
||
internal virtual void RaiseToggleStatePropertyChangedEvent(bool? oldValue, bool? newValue) | ||
{ | ||
RaisePropertyChangedEvent( | ||
SelectionItemPatternIdentifiers.IsSelectedProperty, | ||
oldValue == true, | ||
newValue == true); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Avalonia.Controls/Automation/SelectionItemPatternIdentifiers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Avalonia.Automation.Provider; | ||
|
||
namespace Avalonia.Automation | ||
{ | ||
/// <summary> | ||
/// Contains values used as identifiers by <see cref="ISelectionItemProvider"/>. | ||
/// </summary> | ||
public static class SelectionItemPatternIdentifiers | ||
{ | ||
/// <summary>Indicates the element is currently selected.</summary> | ||
public static AutomationProperty IsSelectedProperty { get; } = new AutomationProperty(); | ||
|
||
/// <summary>Indicates the element is currently selected.</summary> | ||
public static AutomationProperty SelectionContainerProperty { get; } = new AutomationProperty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/Avalonia.IntegrationTests.Appium/RadioButtonTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OpenQA.Selenium.Appium; | ||
using Xunit; | ||
|
||
namespace Avalonia.IntegrationTests.Appium | ||
{ | ||
[Collection("Default")] | ||
public class RadioButtonTests | ||
{ | ||
private readonly AppiumDriver<AppiumWebElement> _session; | ||
|
||
public RadioButtonTests(TestAppFixture fixture) | ||
{ | ||
_session = fixture.Session; | ||
|
||
var tabs = _session.FindElementByAccessibilityId("MainTabs"); | ||
tabs.FindElementByName("RadioButton").Click(); | ||
} | ||
|
||
|
||
[Fact] | ||
public void RadioButton_IsChecked_True_When_Clicked() | ||
{ | ||
var button = _session.FindElementByAccessibilityId("BasicRadioButton"); | ||
Assert.False(button.GetIsChecked()); | ||
button.Click(); | ||
Assert.True(button.GetIsChecked()); | ||
} | ||
|
||
[Fact] | ||
public void ThreeState_RadioButton_IsChecked_False_When_Other_ThreeState_RadioButton_Checked() | ||
{ | ||
var button1 = _session.FindElementByAccessibilityId("ThreeStatesRadioButton1"); | ||
var button2 = _session.FindElementByAccessibilityId("ThreeStatesRadioButton2"); | ||
Assert.True(button1.GetIsChecked()); | ||
Assert.False(button2.GetIsChecked()); | ||
button2.Click(); | ||
Assert.False(button1.GetIsChecked()); | ||
Assert.True(button2.GetIsChecked()); | ||
} | ||
|
||
} | ||
} |