Honor mixed state of tristate Checkbox in Semantics (flutter/engine#35868)

* Honor mixed state

* Add to unit test

* Add state enums

* Fix delimiter

* Add enums

* Scope subtests, rename enum

* Rename enums

* Comment specification

* Tidy up comments

* Rename java enum

* Trailing whitespace
This commit is contained in:
yaakovschectman
2022-09-06 10:59:25 -04:00
committed by GitHub
parent d53aa19aa8
commit e2e7168850
9 changed files with 103 additions and 40 deletions

View File

@@ -319,6 +319,7 @@ class SemanticsFlag {
static const int _kIsLinkIndex = 1 << 22;
static const int _kIsSliderIndex = 1 << 23;
static const int _kIsKeyboardKeyIndex = 1 << 24;
static const int _kIsCheckStateMixedIndex = 1 << 25;
// READ THIS: if you add a flag here, you MUST update the numSemanticsFlags
// value in testing/dart/semantics_test.dart, or tests will fail. Also,
// please update the Flag enum in
@@ -348,6 +349,17 @@ class SemanticsFlag {
/// * [SemanticsFlag.hasCheckedState], which enables a checked state.
static const SemanticsFlag isChecked = SemanticsFlag._(_kIsCheckedIndex);
/// Whether a tristate checkbox is in its mixed state.
///
/// If this is true, the check box this semantics node represents
/// is in a mixed state.
///
/// For example, a [Checkbox] with [Checkbox.tristate] set to true
/// can have checked, unchecked, or mixed state.
///
/// Must be false when the checkbox is either checked or unchecked.
static const SemanticsFlag isCheckStateMixed = SemanticsFlag._(_kIsCheckStateMixedIndex);
/// Whether a semantics node is selected.
///
@@ -578,6 +590,7 @@ class SemanticsFlag {
_kIsLinkIndex: isLink,
_kIsSliderIndex: isSlider,
_kIsKeyboardKeyIndex: isKeyboardKey,
_kIsCheckStateMixedIndex: isCheckStateMixed,
};
@override
@@ -633,6 +646,8 @@ class SemanticsFlag {
return 'SemanticsFlag.isSlider';
case _kIsKeyboardKeyIndex:
return 'SemanticsFlag.isKeyboardKey';
case _kIsCheckStateMixedIndex:
return 'SemanticsFlag.isCheckStateMixed';
}
assert(false, 'Unhandled index: $index (0x${index.toRadixString(8).padLeft(4, "0")})');
return '';

View File

@@ -87,6 +87,7 @@ enum class SemanticsFlags : int32_t {
kIsLink = 1 << 22,
kIsSlider = 1 << 23,
kIsKeyboardKey = 1 << 24,
kIsCheckStateMixed = 1 << 25,
};
const int kScrollableSemanticsFlags =

View File

@@ -163,6 +163,7 @@ class SemanticsFlag {
static const int _kIsLinkIndex = 1 << 22;
static const int _kIsSliderIndex = 1 << 23;
static const int _kIsKeyboardKeyIndex = 1 << 24;
static const int _kIsCheckStateMixedIndex = 1 << 25;
static const SemanticsFlag hasCheckedState = SemanticsFlag._(_kHasCheckedStateIndex);
static const SemanticsFlag isChecked = SemanticsFlag._(_kIsCheckedIndex);
@@ -189,6 +190,7 @@ class SemanticsFlag {
static const SemanticsFlag hasToggledState = SemanticsFlag._(_kHasToggledStateIndex);
static const SemanticsFlag isToggled = SemanticsFlag._(_kIsToggledIndex);
static const SemanticsFlag hasImplicitScrolling = SemanticsFlag._(_kHasImplicitScrollingIndex);
static const SemanticsFlag isCheckStateMixed = SemanticsFlag._(_kIsCheckStateMixedIndex);
static const Map<int, SemanticsFlag> values = <int, SemanticsFlag>{
_kHasCheckedStateIndex: hasCheckedState,
@@ -216,6 +218,7 @@ class SemanticsFlag {
_kIsLinkIndex: isLink,
_kIsSliderIndex: isSlider,
_kIsKeyboardKeyIndex: isKeyboardKey,
_kIsCheckStateMixedIndex: isCheckStateMixed,
};
@override
@@ -271,6 +274,8 @@ class SemanticsFlag {
return 'SemanticsFlag.isSlider';
case _kIsKeyboardKeyIndex:
return 'SemanticsFlag.isKeyboardKey';
case _kIsCheckStateMixedIndex:
return 'SemanticsFlag.isCheckStateMixed';
}
assert(false, 'Unhandled index: $index (0x${index.toRadixString(8).padLeft(4, "0")})');
return '';

View File

@@ -17,7 +17,7 @@ void main() {
void testMain() {
// This must match the number of flags in lib/ui/semantics.dart
const int numSemanticsFlags = 25;
const int numSemanticsFlags = 26;
test('SemanticsFlag.values refers to all flags.', () async {
expect(SemanticsFlag.values.length, equals(numSemanticsFlags));
for (int index = 0; index < numSemanticsFlags; ++index) {

View File

@@ -2131,7 +2131,8 @@ public class AccessibilityBridge extends AccessibilityNodeProvider {
IS_FOCUSABLE(1 << 21),
IS_LINK(1 << 22),
IS_SLIDER(1 << 23),
IS_KEYBOARD_KEY(1 << 24);
IS_KEYBOARD_KEY(1 << 24),
IS_CHECK_STATE_MIXED(1 << 25);
final int value;

View File

@@ -380,7 +380,9 @@ void AccessibilityBridge::SetIntAttributesFromFlutterUpdate(
node_data.AddIntAttribute(
ax::mojom::IntAttribute::kCheckedState,
static_cast<int32_t>(
flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsChecked
flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsCheckStateMixed
? ax::mojom::CheckedState::kMixed
: flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsChecked
? ax::mojom::CheckedState::kTrue
: ax::mojom::CheckedState::kFalse));
}

View File

@@ -227,6 +227,8 @@ typedef enum {
kFlutterSemanticsFlagIsSlider = 1 << 23,
/// Whether the semantics node represents a keyboard key.
kFlutterSemanticsFlagIsKeyboardKey = 1 << 24,
/// Whether the semantics node represents a tristate checkbox in mixed state.
kFlutterSemanticsFlagIsCheckStateMixed = 1 << 25,
} FlutterSemanticsFlag;
typedef enum {

View File

@@ -638,53 +638,90 @@ TEST(FlutterWindowsViewTest, CheckboxNativeState) {
bridge->CommitUpdates();
auto root_node = bridge
->GetFlutterPlatformNodeDelegateFromID(
AccessibilityBridge::kRootNodeId)
.lock();
EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kCheckBox);
EXPECT_EQ(root_node->GetData().GetCheckedState(),
ax::mojom::CheckedState::kTrue);
{
auto root_node = bridge
->GetFlutterPlatformNodeDelegateFromID(
AccessibilityBridge::kRootNodeId)
.lock();
EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kCheckBox);
EXPECT_EQ(root_node->GetData().GetCheckedState(),
ax::mojom::CheckedState::kTrue);
// Get the IAccessible for the root node.
IAccessible* native_view = root_node->GetNativeViewAccessible();
ASSERT_TRUE(native_view != nullptr);
// Get the IAccessible for the root node.
IAccessible* native_view = root_node->GetNativeViewAccessible();
ASSERT_TRUE(native_view != nullptr);
// Look up against the node itself (not one of its children)
VARIANT varchild = {};
varchild.vt = VT_I4;
// Look up against the node itself (not one of its children).
VARIANT varchild = {};
varchild.vt = VT_I4;
// Verify the checkbox is checked.
varchild.lVal = CHILDID_SELF;
VARIANT native_state = {};
ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_CHECKED);
// Verify the checkbox is checked.
varchild.lVal = CHILDID_SELF;
VARIANT native_state = {};
ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_CHECKED);
}
// Test unchecked too
// Test unchecked too.
root.flags = static_cast<FlutterSemanticsFlag>(
FlutterSemanticsFlag::kFlutterSemanticsFlagHasCheckedState);
bridge->AddFlutterSemanticsNodeUpdate(&root);
bridge->CommitUpdates();
root_node = bridge
->GetFlutterPlatformNodeDelegateFromID(
AccessibilityBridge::kRootNodeId)
.lock();
EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kCheckBox);
EXPECT_EQ(root_node->GetData().GetCheckedState(),
ax::mojom::CheckedState::kFalse);
// Get the IAccessible for the root node.
native_view = root_node->GetNativeViewAccessible();
{
auto root_node = bridge
->GetFlutterPlatformNodeDelegateFromID(
AccessibilityBridge::kRootNodeId)
.lock();
EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kCheckBox);
EXPECT_EQ(root_node->GetData().GetCheckedState(),
ax::mojom::CheckedState::kFalse);
// Look up against the node itself (not one of its children)
varchild = {};
varchild.vt = VT_I4;
// Get the IAccessible for the root node.
IAccessible* native_view = root_node->GetNativeViewAccessible();
ASSERT_TRUE(native_view != nullptr);
// Verify the checkbox is checked.
varchild.lVal = CHILDID_SELF;
native_state = {};
ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
EXPECT_FALSE(native_state.lVal & STATE_SYSTEM_CHECKED);
// Look up against the node itself (not one of its children).
VARIANT varchild = {};
varchild.vt = VT_I4;
// Verify the checkbox is checked.
varchild.lVal = CHILDID_SELF;
VARIANT native_state = {};
ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
EXPECT_FALSE(native_state.lVal & STATE_SYSTEM_CHECKED);
}
// Now check mixed state.
root.flags = static_cast<FlutterSemanticsFlag>(
FlutterSemanticsFlag::kFlutterSemanticsFlagHasCheckedState |
FlutterSemanticsFlag::kFlutterSemanticsFlagIsCheckStateMixed);
bridge->AddFlutterSemanticsNodeUpdate(&root);
bridge->CommitUpdates();
{
auto root_node = bridge
->GetFlutterPlatformNodeDelegateFromID(
AccessibilityBridge::kRootNodeId)
.lock();
EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kCheckBox);
EXPECT_EQ(root_node->GetData().GetCheckedState(),
ax::mojom::CheckedState::kMixed);
// Get the IAccessible for the root node.
IAccessible* native_view = root_node->GetNativeViewAccessible();
ASSERT_TRUE(native_view != nullptr);
// Look up against the node itself (not one of its children).
VARIANT varchild = {};
varchild.vt = VT_I4;
// Verify the checkbox is checked.
varchild.lVal = CHILDID_SELF;
VARIANT native_state = {};
ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_MIXED);
}
}
} // namespace testing

View File

@@ -11,7 +11,7 @@ import 'package:litetest/litetest.dart';
void main() {
// This must match the number of flags in lib/ui/semantics.dart
const int numSemanticsFlags = 25;
const int numSemanticsFlags = 26;
test('SemanticsFlag.values refers to all flags.', () async {
expect(SemanticsFlag.values.length, equals(numSemanticsFlags));
for (int index = 0; index < numSemanticsFlags; ++index) {