From 89d6c8d90be74967bb6e41a40eec811675902e39 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Wed, 9 Oct 2019 14:45:42 -0700 Subject: [PATCH] Enables setting of semantics focused and focusable attributes within Focus widgets. (#41814) This adds a Semantics node to the Focus and FocusScope widgets, setting the focused and focusable attributes so that the accessibility subsystem can be told when a control has the input focus. Includes an engine roll to flutter/engine@77252d2, and the following 8 engine changes: flutter/engine@77252d2 Greg Spencer Add missing focusable testing info (flutter/engine#13013) flutter/engine@0e42a29 skia-flutter-.. Roll src/third_party/skia 54548626a977..e27a503a0a21 (1 commits) (flutter/engine#13024) flutter/engine@6b56ed7 gaaclarke Refactor: FlutterDartProject (flutter/engine#13006) flutter/engine@393480c skia-flutter-.. Roll src/third_party/skia 77dde599c98a..54548626a977 (1 commits) (flutter/engine#13023) flutter/engine@080b89d skia-flutter-.. Roll src/third_party/skia 2b1a25a4d324..77dde599c98a (1 commits) (flutter/engine#13021) flutter/engine@90b0f30 Ben Konyi Roll src/third_party/dart f4a72bfc64..bb04f145b2 (18 commits) (flutter/engine#13020) flutter/engine@049fb89 skia-flutter-.. Roll fuchsia/sdk/core/linux-amd64 from q_uYX... to cknsi... (flutter/engine#13019) flutter/engine@6925b2a skia-flutter-.. Roll fuchsia/sdk/core/mac-amd64 from wuAtw... to u0JpE... (flutter/engine#13018) Related Issues Addresses #40101 Landing on red in order to fix the build: it's red because of the needed engine roll. --- bin/internal/engine.version | 2 +- .../android_semantics_testing/lib/main.dart | 18 +- .../lib/src/constants.dart | 3 + .../lib/src/matcher.dart | 9 +- .../lib/src/tests/popup_constants.dart | 26 + .../lib/src/tests/popup_page.dart | 105 +++ .../lib/test_constants.dart | 1 + .../test_driver/main_test.dart | 634 +++++++++++++----- .../test/demo/material/chip_demo_test.dart | 2 + .../flutter/lib/src/material/popup_menu.dart | 2 +- .../lib/src/rendering/custom_paint.dart | 3 + .../flutter/lib/src/rendering/proxy_box.dart | 14 + .../flutter/lib/src/semantics/semantics.dart | 28 +- packages/flutter/lib/src/widgets/basic.dart | 4 + .../flutter/lib/src/widgets/focus_scope.dart | 22 +- .../test/material/back_button_test.dart | 1 + .../material/bottom_navigation_bar_test.dart | 10 + .../flutter/test/material/buttons_test.dart | 12 +- packages/flutter/test/material/card_test.dart | 3 +- packages/flutter/test/material/chip_test.dart | 6 +- .../test/material/date_picker_test.dart | 28 +- .../test/material/expand_icon_test.dart | 2 + .../test/material/expansion_panel_test.dart | 3 + .../material/floating_action_button_test.dart | 7 +- .../test/material/icon_button_test.dart | 3 +- .../test/material/outline_button_test.dart | 3 +- .../material/raw_material_button_test.dart | 3 +- .../flutter/test/material/search_test.dart | 6 +- .../test/widgets/custom_painter_test.dart | 4 +- .../test/widgets/focus_scope_test.dart | 49 ++ .../test/widgets/platform_view_test.dart | 11 +- .../flutter/test/widgets/semantics_test.dart | 1 + .../lib/src/extension/extension.dart | 6 +- .../test/src/extension_test.dart | 2 +- packages/flutter_test/lib/src/matchers.dart | 2 + packages/flutter_test/test/matchers_test.dart | 1 + 36 files changed, 833 insertions(+), 203 deletions(-) create mode 100644 dev/integration_tests/android_semantics_testing/lib/src/tests/popup_constants.dart create mode 100644 dev/integration_tests/android_semantics_testing/lib/src/tests/popup_page.dart diff --git a/bin/internal/engine.version b/bin/internal/engine.version index a4cec45b67..f06d61c58d 100644 --- a/bin/internal/engine.version +++ b/bin/internal/engine.version @@ -1 +1 @@ -0d749933bcc63b53771e37e6cc28fa43c3d1fd06 +77252d2371f3341f9f3a7e3ec2eb64bfa29bf975 diff --git a/dev/integration_tests/android_semantics_testing/lib/main.dart b/dev/integration_tests/android_semantics_testing/lib/main.dart index 0cecafaa02..af2561f346 100644 --- a/dev/integration_tests/android_semantics_testing/lib/main.dart +++ b/dev/integration_tests/android_semantics_testing/lib/main.dart @@ -11,6 +11,8 @@ import 'package:flutter/services.dart'; import 'package:flutter_driver/driver_extension.dart'; import 'src/tests/controls_page.dart'; +import 'src/tests/popup_constants.dart'; +import 'src/tests/popup_page.dart'; import 'src/tests/text_field_page.dart'; void main() { @@ -40,10 +42,11 @@ Future dataHandler(String message) async { throw UnimplementedError(); } -const List routes = [ - selectionControlsRoute, - textFieldRoute, -]; +Map routes = { + selectionControlsRoute : (BuildContext context) => SelectionControlsPage(), + popupControlsRoute : (BuildContext context) => PopupControlsPage(), + textFieldRoute : (BuildContext context) => TextFieldPage(), +}; class TestApp extends StatelessWidget { const TestApp(); @@ -51,15 +54,12 @@ class TestApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - routes: { - selectionControlsRoute: (BuildContext context) => SelectionControlsPage(), - textFieldRoute: (BuildContext context) => TextFieldPage(), - }, + routes: routes, home: Builder( builder: (BuildContext context) { return Scaffold( body: ListView( - children: routes.map((String value) { + children: routes.keys.map((String value) { return MaterialButton( child: Text(value), onPressed: () { diff --git a/dev/integration_tests/android_semantics_testing/lib/src/constants.dart b/dev/integration_tests/android_semantics_testing/lib/src/constants.dart index b1fe948058..465b9a5849 100644 --- a/dev/integration_tests/android_semantics_testing/lib/src/constants.dart +++ b/dev/integration_tests/android_semantics_testing/lib/src/constants.dart @@ -24,6 +24,9 @@ class AndroidClassName { /// The class name used for toggle switches. static const String toggleSwitch = 'android.widget.Switch'; + + /// The default className for buttons. + static const String button = 'android.widget.Button'; } /// Action constants which correspond to `AccessibilityAction` in Android. diff --git a/dev/integration_tests/android_semantics_testing/lib/src/matcher.dart b/dev/integration_tests/android_semantics_testing/lib/src/matcher.dart index f45d250861..e43139720c 100644 --- a/dev/integration_tests/android_semantics_testing/lib/src/matcher.dart +++ b/dev/integration_tests/android_semantics_testing/lib/src/matcher.dart @@ -135,8 +135,13 @@ class _AndroidSemanticsMatcher extends Matcher { return _failWithMessage('Expected size: $size', matchState); if (actions != null) { final List itemActions = item.getActions(); - if (!unorderedEquals(actions).matches(itemActions, matchState)) - return _failWithMessage('Expected actions: $actions', matchState); + if (!unorderedEquals(actions).matches(itemActions, matchState)) { + final List actionsString = actions.map((AndroidSemanticsAction action) => action.toString()).toList()..sort(); + final List itemActionsString = itemActions.map((AndroidSemanticsAction action) => action.toString()).toList()..sort(); + final Set unexpected = itemActionsString.toSet().difference(actionsString.toSet()); + final Set missing = actionsString.toSet().difference(itemActionsString.toSet()); + return _failWithMessage('Expected actions: $actionsString\nActual actions: $itemActionsString\nUnexpected: $unexpected\nMissing: $missing', matchState); + } } if (isChecked != null && isChecked != item.isChecked) return _failWithMessage('Expected isChecked: $isChecked', matchState); diff --git a/dev/integration_tests/android_semantics_testing/lib/src/tests/popup_constants.dart b/dev/integration_tests/android_semantics_testing/lib/src/tests/popup_constants.dart new file mode 100644 index 0000000000..b9a91d51a8 --- /dev/null +++ b/dev/integration_tests/android_semantics_testing/lib/src/tests/popup_constants.dart @@ -0,0 +1,26 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// The name of the route containing the test suite. +const String popupControlsRoute = 'popups'; + +/// The string supplied to the [ValueKey] for the popup menu button. +const String popupButtonKeyValue = 'PopupControls#PopupButton1'; + +/// The string supplied to the [ValueKey] for the popup menu. +const String popupKeyValue = 'PopupControls#Popup1'; + +/// The string supplied to the [ValueKey] for the dropdown button. +const String dropdownButtonKeyValue = 'PopupControls#DropdownButton1'; + +/// The string supplied to the [ValueKey] for the dropdown button menu. +const String dropdownKeyValue = 'PopupControls#Dropdown1'; + +/// The string supplied to the [ValueKey] for the alert button. +const String alertButtonKeyValue = 'PopupControls#AlertButton1'; + +/// The string supplied to the [ValueKey] for the alert dialog. +const String alertKeyValue = 'PopupControls#Alert1'; + +const List popupItems = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']; diff --git a/dev/integration_tests/android_semantics_testing/lib/src/tests/popup_page.dart b/dev/integration_tests/android_semantics_testing/lib/src/tests/popup_page.dart new file mode 100644 index 0000000000..542aeff737 --- /dev/null +++ b/dev/integration_tests/android_semantics_testing/lib/src/tests/popup_page.dart @@ -0,0 +1,105 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; + +import 'popup_constants.dart'; + +export 'popup_constants.dart'; + +/// A page with a popup menu, a dropdown menu, and a modal alert. +class PopupControlsPage extends StatefulWidget { + @override + State createState() => _PopupControlsPageState(); +} + +class _PopupControlsPageState extends State { + final Key popupKey = const ValueKey(popupKeyValue); + final Key dropdownKey = const ValueKey(dropdownKeyValue); + final Key alertKey = const ValueKey(alertKeyValue); + + String popupValue = popupItems.first; + String dropdownValue = popupItems.first; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(leading: const BackButton(key: ValueKey('back'))), + body: SafeArea( + child: Center( + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + mainAxisSize: MainAxisSize.min, + children: [ + PopupMenuButton( + key: const ValueKey(popupButtonKeyValue), + icon: const Icon(Icons.arrow_drop_down), + itemBuilder: (BuildContext context) { + return popupItems.map>((String item) { + return PopupMenuItem( + key: ValueKey('$popupKeyValue.$item'), + value: item, + child: Text(item), + ); + }).toList(); + }, + onSelected: (String value) { + popupValue = value; + }, + ), + DropdownButton( + key: const ValueKey(dropdownButtonKeyValue), + value: dropdownValue, + items: popupItems.map>((String item) { + return DropdownMenuItem( + key: ValueKey('$dropdownKeyValue.$item'), + value: item, + child: Text(item), + ); + }).toList(), + onChanged: (String value) { + setState(() { + dropdownValue = value; + }); + }, + ), + MaterialButton( + key: const ValueKey(alertButtonKeyValue), + child: const Text('Alert'), + onPressed: () { + showDialog( + context: context, + barrierDismissible: false, // user must tap button! + builder: (BuildContext context) { + return AlertDialog( + key: const ValueKey(alertKeyValue), + title: const Text('Title text', key: ValueKey('$alertKeyValue.Title')), + content: SingleChildScrollView( + child: ListBody( + children: const [ + Text('Body text line 1.', key: ValueKey('$alertKeyValue.Body1')), + Text('Body text line 2.', key: ValueKey('$alertKeyValue.Body2')), + ], + ), + ), + actions: [ + FlatButton( + child: const Text('OK', key: ValueKey('$alertKeyValue.OK')), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ], + ); + }, + ); + }, + ), + ], + ), + ), + ), + ); + } +} diff --git a/dev/integration_tests/android_semantics_testing/lib/test_constants.dart b/dev/integration_tests/android_semantics_testing/lib/test_constants.dart index 7764189ed8..7faf9a2c9f 100644 --- a/dev/integration_tests/android_semantics_testing/lib/test_constants.dart +++ b/dev/integration_tests/android_semantics_testing/lib/test_constants.dart @@ -3,4 +3,5 @@ // found in the LICENSE file. export 'src/tests/controls_constants.dart'; +export 'src/tests/popup_constants.dart'; export 'src/tests/text_field_constants.dart'; diff --git a/dev/integration_tests/android_semantics_testing/test_driver/main_test.dart b/dev/integration_tests/android_semantics_testing/test_driver/main_test.dart index e2cd340af9..9e21a8268b 100644 --- a/dev/integration_tests/android_semantics_testing/test_driver/main_test.dart +++ b/dev/integration_tests/android_semantics_testing/test_driver/main_test.dart @@ -69,50 +69,61 @@ void main() { matching: find.byType('Semantics'), firstMatchOnly: true, ); - expect(await getSemantics(normalTextField), hasAndroidSemantics( - className: AndroidClassName.editText, - isEditable: true, - isFocusable: true, - isFocused: false, - isPassword: false, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(normalTextField), + hasAndroidSemantics( + className: AndroidClassName.editText, + isEditable: true, + isFocusable: true, + isFocused: false, + isPassword: false, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); await driver.tap(normalTextField); + await Future.delayed(const Duration(milliseconds: 500)); - expect(await getSemantics(normalTextField), hasAndroidSemantics( - className: AndroidClassName.editText, - isFocusable: true, - isFocused: true, - isEditable: true, - isPassword: false, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - AndroidSemanticsAction.setSelection, - AndroidSemanticsAction.copy, - ], - )); + expect( + await getSemantics(normalTextField), + hasAndroidSemantics( + className: AndroidClassName.editText, + isFocusable: true, + isFocused: true, + isEditable: true, + isPassword: false, + actions: [ + AndroidSemanticsAction.clearAccessibilityFocus, + AndroidSemanticsAction.click, + AndroidSemanticsAction.copy, + AndroidSemanticsAction.setSelection, + ], + ), + ); await driver.enterText('hello world'); + await Future.delayed(const Duration(milliseconds: 500)); - expect(await getSemantics(normalTextField), hasAndroidSemantics( - text: 'hello world', - className: AndroidClassName.editText, - isFocusable: true, - isFocused: true, - isEditable: true, - isPassword: false, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - AndroidSemanticsAction.setSelection, - AndroidSemanticsAction.copy, - ], - )); + expect( + await getSemantics(normalTextField), + hasAndroidSemantics( + text: 'hello world', + className: AndroidClassName.editText, + isFocusable: true, + isFocused: true, + isEditable: true, + isPassword: false, + actions: [ + AndroidSemanticsAction.clearAccessibilityFocus, + AndroidSemanticsAction.click, + AndroidSemanticsAction.copy, + AndroidSemanticsAction.setSelection, + ], + ), + ); }); test('password TextField has correct Android semantics', () async { @@ -121,50 +132,59 @@ void main() { matching: find.byType('Semantics'), firstMatchOnly: true, ); - expect(await getSemantics(passwordTextField), hasAndroidSemantics( - className: AndroidClassName.editText, - isEditable: true, - isFocusable: true, - isFocused: false, - isPassword: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(passwordTextField), + hasAndroidSemantics( + className: AndroidClassName.editText, + isEditable: true, + isFocusable: true, + isFocused: false, + isPassword: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); await driver.tap(passwordTextField); - expect(await getSemantics(passwordTextField), hasAndroidSemantics( - className: AndroidClassName.editText, - isFocusable: true, - isFocused: true, - isEditable: true, - isPassword: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - AndroidSemanticsAction.setSelection, - AndroidSemanticsAction.copy, - ], - )); + expect( + await getSemantics(passwordTextField), + hasAndroidSemantics( + className: AndroidClassName.editText, + isFocusable: true, + isFocused: true, + isEditable: true, + isPassword: true, + actions: [ + AndroidSemanticsAction.clearAccessibilityFocus, + AndroidSemanticsAction.click, + AndroidSemanticsAction.copy, + AndroidSemanticsAction.setSelection, + ], + ), + ); await driver.enterText('hello world'); - expect(await getSemantics(passwordTextField), hasAndroidSemantics( - text: '\u{2022}' * ('hello world'.length), - className: AndroidClassName.editText, - isFocusable: true, - isFocused: true, - isEditable: true, - isPassword: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - AndroidSemanticsAction.setSelection, - AndroidSemanticsAction.copy, - ], - )); + expect( + await getSemantics(passwordTextField), + hasAndroidSemantics( + text: '\u{2022}' * ('hello world'.length), + className: AndroidClassName.editText, + isFocusable: true, + isFocused: true, + isEditable: true, + isPassword: true, + actions: [ + AndroidSemanticsAction.clearAccessibilityFocus, + AndroidSemanticsAction.click, + AndroidSemanticsAction.copy, + AndroidSemanticsAction.setSelection, + ], + ), + ); }); tearDownAll(() async { @@ -178,114 +198,404 @@ void main() { }); test('Checkbox has correct Android semantics', () async { - expect(await getSemantics(find.byValueKey(checkboxKeyValue)), hasAndroidSemantics( - className: AndroidClassName.checkBox, - isChecked: false, - isCheckable: true, - isEnabled: true, - isFocusable: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(find.byValueKey(checkboxKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.checkBox, + isChecked: false, + isCheckable: true, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); await driver.tap(find.byValueKey(checkboxKeyValue)); - expect(await getSemantics(find.byValueKey(checkboxKeyValue)), hasAndroidSemantics( - className: AndroidClassName.checkBox, - isChecked: true, - isCheckable: true, - isEnabled: true, - isFocusable: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); - expect(await getSemantics(find.byValueKey(disabledCheckboxKeyValue)), hasAndroidSemantics( - className: AndroidClassName.checkBox, - isCheckable: true, - isEnabled: false, - actions: const [ - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(find.byValueKey(checkboxKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.checkBox, + isChecked: true, + isCheckable: true, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); + expect( + await getSemantics(find.byValueKey(disabledCheckboxKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.checkBox, + isCheckable: true, + isEnabled: false, + actions: const [ + AndroidSemanticsAction.accessibilityFocus, + ], + ), + ); }); test('Radio has correct Android semantics', () async { - expect(await getSemantics(find.byValueKey(radio2KeyValue)), hasAndroidSemantics( - className: AndroidClassName.radio, - isChecked: false, - isCheckable: true, - isEnabled: true, - isFocusable: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(find.byValueKey(radio2KeyValue)), + hasAndroidSemantics( + className: AndroidClassName.radio, + isChecked: false, + isCheckable: true, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); await driver.tap(find.byValueKey(radio2KeyValue)); - expect(await getSemantics(find.byValueKey(radio2KeyValue)), hasAndroidSemantics( - className: AndroidClassName.radio, - isChecked: true, - isCheckable: true, - isEnabled: true, - isFocusable: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(find.byValueKey(radio2KeyValue)), + hasAndroidSemantics( + className: AndroidClassName.radio, + isChecked: true, + isCheckable: true, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); }); test('Switch has correct Android semantics', () async { - expect(await getSemantics(find.byValueKey(switchKeyValue)), hasAndroidSemantics( - className: AndroidClassName.toggleSwitch, - isChecked: false, - isCheckable: true, - isEnabled: true, - isFocusable: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(find.byValueKey(switchKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.toggleSwitch, + isChecked: false, + isCheckable: true, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); await driver.tap(find.byValueKey(switchKeyValue)); - expect(await getSemantics(find.byValueKey(switchKeyValue)), hasAndroidSemantics( - className: AndroidClassName.toggleSwitch, - isChecked: true, - isCheckable: true, - isEnabled: true, - isFocusable: true, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(find.byValueKey(switchKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.toggleSwitch, + isChecked: true, + isCheckable: true, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); }); // Regression test for https://github.com/flutter/flutter/issues/20820. test('Switch can be labeled', () async { - expect(await getSemantics(find.byValueKey(labeledSwitchKeyValue)), hasAndroidSemantics( - className: AndroidClassName.toggleSwitch, - isChecked: false, - isCheckable: true, - isEnabled: true, - isFocusable: true, - contentDescription: switchLabel, - actions: [ - AndroidSemanticsAction.click, - AndroidSemanticsAction.accessibilityFocus, - ], - )); + expect( + await getSemantics(find.byValueKey(labeledSwitchKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.toggleSwitch, + isChecked: false, + isCheckable: true, + isEnabled: true, + isFocusable: true, + contentDescription: switchLabel, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); }); tearDownAll(() async { await driver.tap(find.byValueKey('back')); }); }); + + group('Popup Controls', () { + setUpAll(() async { + await driver.tap(find.text(popupControlsRoute)); + }); + + test('Popup Menu has correct Android semantics', () async { + expect( + await getSemantics(find.byValueKey(popupButtonKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.button, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); + + await driver.tap(find.byValueKey(popupButtonKeyValue)); + try { + // We have to wait wall time here because we're waiting for TalkBack to + // catch up. + await Future.delayed(const Duration(milliseconds: 1500)); + + for (String item in popupItems) { + expect( + await getSemantics(find.byValueKey('$popupKeyValue.$item')), + hasAndroidSemantics( + className: AndroidClassName.view, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + if (item == popupItems.first) AndroidSemanticsAction.clearAccessibilityFocus, + if (item != popupItems.first) AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + reason: "Popup $item doesn't have the right semantics"); + } + await driver.tap(find.byValueKey('$popupKeyValue.${popupItems.first}')); + + // Pop up the menu again, to verify that TalkBack gets the right answer + // more than just the first time. + await driver.tap(find.byValueKey(popupButtonKeyValue)); + await Future.delayed(const Duration(milliseconds: 1500)); + + for (String item in popupItems) { + expect( + await getSemantics(find.byValueKey('$popupKeyValue.$item')), + hasAndroidSemantics( + className: AndroidClassName.view, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + // TODO(gspencergoog): This should really be identical to the first time, + // but TalkBack doesn't find it the second time for some reason. + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + reason: "Popup $item doesn't have the right semantics the second time"); + } + } finally { + await driver.tap(find.byValueKey('$popupKeyValue.${popupItems.first}')); + } + }); + + test('Dropdown Menu has correct Android semantics', () async { + expect( + await getSemantics(find.byValueKey(dropdownButtonKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.button, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); + + await driver.tap(find.byValueKey(dropdownButtonKeyValue)); + try { + await Future.delayed(const Duration(milliseconds: 1500)); + + for (String item in popupItems) { + // There are two copies of each item, so we want to find the version + // that is in the overlay, not the one in the dropdown. + expect( + await getSemantics(find.descendant( + of: find.byType('Scrollable'), + matching: find.byValueKey('$dropdownKeyValue.$item'), + )), + hasAndroidSemantics( + className: AndroidClassName.view, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + // TODO(gspencergoog): This should really be different for the first item: + // It should have clearAccessibilityFocus instead, but for some reason + // TalkBack doesn't ask to focus it. + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + reason: "Dropdown $item doesn't have the right semantics"); + } + await driver.tap( + find.descendant( + of: find.byType('Scrollable'), + matching: find.byValueKey('$dropdownKeyValue.${popupItems.first}'), + ), + ); + + // Pop up the dropdown again, to verify that TalkBack gets the right answer + // more than just the first time. + await driver.tap(find.byValueKey(dropdownButtonKeyValue)); + await Future.delayed(const Duration(milliseconds: 1500)); + + for (String item in popupItems) { + // There are two copies of each item, so we want to find the version + // that is in the overlay, not the one in the dropdown. + expect( + await getSemantics(find.descendant( + of: find.byType('Scrollable'), + matching: find.byValueKey('$dropdownKeyValue.$item'), + )), + hasAndroidSemantics( + className: AndroidClassName.view, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + // TODO(gspencergoog): This should really be different for the first item: + // It should have clearAccessibilityFocus instead, but for some reason + // TalkBack doesn't ask to focus it. + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + reason: "Dropdown $item doesn't have the right semantics the second time."); + } + } finally { + await driver.tap( + find.descendant( + of: find.byType('Scrollable'), + matching: find.byValueKey('$dropdownKeyValue.${popupItems.first}'), + ), + ); + } + }); + + test('Modal alert dialog has correct Android semantics', () async { + expect( + await getSemantics(find.byValueKey(alertButtonKeyValue)), + hasAndroidSemantics( + className: AndroidClassName.button, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + ); + + await driver.tap(find.byValueKey(alertButtonKeyValue)); + try { + await Future.delayed(const Duration(milliseconds: 1500)); + + expect( + await getSemantics(find.byValueKey('$alertKeyValue.OK')), + hasAndroidSemantics( + className: AndroidClassName.button, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + reason: "Alert OK button doesn't have the right semantics"); + + for (String item in ['Title', 'Body1', 'Body2']) { + expect( + await getSemantics(find.byValueKey('$alertKeyValue.$item')), + hasAndroidSemantics( + className: AndroidClassName.view, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + if (item == 'Body1') AndroidSemanticsAction.clearAccessibilityFocus, + if (item != 'Body1') AndroidSemanticsAction.accessibilityFocus, + ], + ), + reason: "Alert $item button doesn't have the right semantics"); + } + + await driver.tap(find.byValueKey('$alertKeyValue.OK')); + + // Pop up the alert again, to verify that TalkBack gets the right answer + // more than just the first time. + await driver.tap(find.byValueKey(alertButtonKeyValue)); + await Future.delayed(const Duration(milliseconds: 1500)); + + expect( + await getSemantics(find.byValueKey('$alertKeyValue.OK')), + hasAndroidSemantics( + className: AndroidClassName.button, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + AndroidSemanticsAction.accessibilityFocus, + AndroidSemanticsAction.click, + ], + ), + reason: "Alert OK button doesn't have the right semantics"); + + for (String item in ['Title', 'Body1', 'Body2']) { + expect( + await getSemantics(find.byValueKey('$alertKeyValue.$item')), + hasAndroidSemantics( + className: AndroidClassName.view, + isChecked: false, + isCheckable: false, + isEnabled: true, + isFocusable: true, + actions: [ + // TODO(gspencergoog): This should really be identical to the first time, + // but TalkBack doesn't find it the second time for some reason. + AndroidSemanticsAction.accessibilityFocus, + ], + ), + reason: "Alert $item button doesn't have the right semantics"); + } + } finally { + await driver.tap(find.byValueKey('$alertKeyValue.OK')); + } + }); + + tearDownAll(() async { + await Future.delayed(const Duration(milliseconds: 500)); + await driver.tap(find.byValueKey('back')); + }); + }); }); } diff --git a/examples/flutter_gallery/test/demo/material/chip_demo_test.dart b/examples/flutter_gallery/test/demo/material/chip_demo_test.dart index cfc9c65671..406dc34a38 100644 --- a/examples/flutter_gallery/test/demo/material/chip_demo_test.dart +++ b/examples/flutter_gallery/test/demo/material/chip_demo_test.dart @@ -18,6 +18,7 @@ void main() { isButton: true, hasEnabledState: true, isEnabled: true, + isFocusable: true, hasTapAction: true, label: 'Update border shape', )); @@ -26,6 +27,7 @@ void main() { isButton: true, hasEnabledState: true, isEnabled: true, + isFocusable: true, hasTapAction: true, label: 'Reset chips', )); diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 94d399c4b6..1a64089af2 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -723,7 +723,7 @@ class _PopupMenuRoute extends PopupRoute { } else { // For the sake of backwards compatibility. An (unlikely) app that relied // on having menus only inherit from the material Theme could set - // captureInheritedThemes to false and get the original behvaior. + // captureInheritedThemes to false and get the original behavior. if (theme != null) menu = Theme(data: theme, child: menu); } diff --git a/packages/flutter/lib/src/rendering/custom_paint.dart b/packages/flutter/lib/src/rendering/custom_paint.dart index 6b12a18e5d..4a5afeab69 100644 --- a/packages/flutter/lib/src/rendering/custom_paint.dart +++ b/packages/flutter/lib/src/rendering/custom_paint.dart @@ -840,6 +840,9 @@ class RenderCustomPaint extends RenderProxyBox { if (properties.readOnly != null) { config.isReadOnly = properties.readOnly; } + if (properties.focusable != null) { + config.isFocusable = properties.focusable; + } if (properties.focused != null) { config.isFocused = properties.focused; } diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index cfe57a5f07..1a5895a25d 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -3489,6 +3489,7 @@ class RenderSemanticsAnnotations extends RenderProxyBox { bool header, bool textField, bool readOnly, + bool focusable, bool focused, bool inMutuallyExclusiveGroup, bool obscured, @@ -3541,6 +3542,7 @@ class RenderSemanticsAnnotations extends RenderProxyBox { _header = header, _textField = textField, _readOnly = readOnly, + _focusable = focusable, _focused = focused, _inMutuallyExclusiveGroup = inMutuallyExclusiveGroup, _obscured = obscured, @@ -3720,6 +3722,16 @@ class RenderSemanticsAnnotations extends RenderProxyBox { markNeedsSemanticsUpdate(); } + /// If non-null, sets the [SemanticsNode.isFocusable] semantic to the given value. + bool get focusable => _focusable; + bool _focusable; + set focusable(bool value) { + if (focusable == value) + return; + _focusable = value; + markNeedsSemanticsUpdate(); + } + /// If non-null, sets the [SemanticsNode.isFocused] semantic to the given value. bool get focused => _focused; bool _focused; @@ -4380,6 +4392,8 @@ class RenderSemanticsAnnotations extends RenderProxyBox { config.isTextField = textField; if (readOnly != null) config.isReadOnly = readOnly; + if (focusable != null) + config.isFocusable = focusable; if (focused != null) config.isFocused = focused; if (inMutuallyExclusiveGroup != null) diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index 64655a698a..a92b9b7cd5 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -595,6 +595,7 @@ class SemanticsProperties extends DiagnosticableTree { this.header, this.textField, this.readOnly, + this.focusable, this.focused, this.inMutuallyExclusiveGroup, this.hidden, @@ -698,14 +699,25 @@ class SemanticsProperties extends DiagnosticableTree { /// TalkBack/VoiceOver will treat it as non-editable text field. final bool readOnly; + /// If non-null, whether the node is able to hold input focus. + /// + /// If [focusable] is set to false, then [focused] must not be true. + /// + /// Input focus indicates that the node will receive keyboard events. It is not + /// to be confused with accessibility focus. Accessibility focus is the + /// green/black rectangular highlight that TalkBack/VoiceOver draws around the + /// element it is reading, and is separate from input focus. + final bool focusable; + /// If non-null, whether the node currently holds input focus. /// - /// At most one node in the tree should hold input focus at any point in time. + /// At most one node in the tree should hold input focus at any point in time, + /// and it should not be set to true if [focusable] is false. /// - /// Input focus (indicates that the node will receive keyboard events) is not + /// Input focus indicates that the node will receive keyboard events. It is not /// to be confused with accessibility focus. Accessibility focus is the - /// green/black rectangular that TalkBack/VoiceOver on the screen and is - /// separate from input focus. + /// green/black rectangular highlight that TalkBack/VoiceOver draws around the + /// element it is reading, and is separate from input focus. final bool focused; /// If non-null, whether a semantic node is in a mutually exclusive group. @@ -3611,7 +3623,13 @@ class SemanticsConfiguration { _setFlag(SemanticsFlag.isInMutuallyExclusiveGroup, value); } - /// Whether the owning [RenderObject] currently holds the user's focus. + /// Whether the owning [RenderObject] can hold the input focus. + bool get isFocusable => _hasFlag(SemanticsFlag.isFocusable); + set isFocusable(bool value) { + _setFlag(SemanticsFlag.isFocusable, value); + } + + /// Whether the owning [RenderObject] currently holds the input focus. bool get isFocused => _hasFlag(SemanticsFlag.isFocused); set isFocused(bool value) { _setFlag(SemanticsFlag.isFocused, value); diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 6fb1b79603..3b9c69bc4f 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -6188,6 +6188,7 @@ class Semantics extends SingleChildRenderObjectWidget { bool header, bool textField, bool readOnly, + bool focusable, bool focused, bool inMutuallyExclusiveGroup, bool obscured, @@ -6242,6 +6243,7 @@ class Semantics extends SingleChildRenderObjectWidget { header: header, textField: textField, readOnly: readOnly, + focusable: focusable, focused: focused, inMutuallyExclusiveGroup: inMutuallyExclusiveGroup, obscured: obscured, @@ -6355,6 +6357,7 @@ class Semantics extends SingleChildRenderObjectWidget { header: properties.header, textField: properties.textField, readOnly: properties.readOnly, + focusable: properties.focusable, focused: properties.focused, liveRegion: properties.liveRegion, maxValueLength: properties.maxValueLength, @@ -6425,6 +6428,7 @@ class Semantics extends SingleChildRenderObjectWidget { ..header = properties.header ..textField = properties.textField ..readOnly = properties.readOnly + ..focusable = properties.focusable ..focused = properties.focused ..inMutuallyExclusiveGroup = properties.inMutuallyExclusiveGroup ..obscured = properties.obscured diff --git a/packages/flutter/lib/src/widgets/focus_scope.dart b/packages/flutter/lib/src/widgets/focus_scope.dart index f829d05de7..8ae41f447e 100644 --- a/packages/flutter/lib/src/widgets/focus_scope.dart +++ b/packages/flutter/lib/src/widgets/focus_scope.dart @@ -137,7 +137,7 @@ class Focus extends StatefulWidget { /// /// The [child] argument is required and must not be null. /// - /// The [autofocus] and [skipTraversal] arguments must not be null. + /// The [autofocus] argument must not be null. const Focus({ Key key, @required this.child, @@ -323,6 +323,8 @@ class _FocusState extends State { FocusNode _internalNode; FocusNode get focusNode => widget.focusNode ?? _internalNode; bool _hasFocus; + bool _hasPrimaryFocus; + bool _canRequestFocus; bool _didAutofocus = false; FocusAttachment _focusAttachment; @@ -343,6 +345,8 @@ class _FocusState extends State { focusNode.skipTraversal = widget.skipTraversal ?? focusNode.skipTraversal; focusNode.canRequestFocus = widget.canRequestFocus ?? focusNode.canRequestFocus; _hasFocus = focusNode.hasFocus; + _canRequestFocus = focusNode.canRequestFocus; + _hasPrimaryFocus = focusNode.hasPrimaryFocus; // Add listener even if the _internalNode existed before, since it should // not be listening now if we're re-using a previous one because it should @@ -426,6 +430,16 @@ class _FocusState extends State { widget.onFocusChange(focusNode.hasFocus); } } + if (_hasPrimaryFocus != focusNode.hasPrimaryFocus) { + setState(() { + _hasPrimaryFocus = focusNode.hasPrimaryFocus; + }); + } + if (_canRequestFocus != focusNode.canRequestFocus) { + setState(() { + _canRequestFocus = focusNode.canRequestFocus; + }); + } } @override @@ -433,7 +447,11 @@ class _FocusState extends State { _focusAttachment.reparent(); return _FocusMarker( node: focusNode, - child: widget.child, + child: Semantics( + focusable: _canRequestFocus, + focused: _hasPrimaryFocus, + child: widget.child, + ), ); } } diff --git a/packages/flutter/test/material/back_button_test.dart b/packages/flutter/test/material/back_button_test.dart index 34d3f1cef9..a64d310499 100644 --- a/packages/flutter/test/material/back_button_test.dart +++ b/packages/flutter/test/material/back_button_test.dart @@ -119,6 +119,7 @@ void main() { hasEnabledState: true, isEnabled: true, hasTapAction: true, + isFocusable: true, )); handle.dispose(); }); diff --git a/packages/flutter/test/material/bottom_navigation_bar_test.dart b/packages/flutter/test/material/bottom_navigation_bar_test.dart index 6dfdcfcd26..bf32b1f431 100644 --- a/packages/flutter/test/material/bottom_navigation_bar_test.dart +++ b/packages/flutter/test/material/bottom_navigation_bar_test.dart @@ -1191,6 +1191,7 @@ void main() { children: [ TestSemantics( flags: [ + SemanticsFlag.isFocusable, SemanticsFlag.isSelected, ], actions: [SemanticsAction.tap], @@ -1198,11 +1199,13 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + flags: [SemanticsFlag.isFocusable], actions: [SemanticsAction.tap], label: 'Alarm\nTab 2 of 3', textDirection: TextDirection.ltr, ), TestSemantics( + flags: [SemanticsFlag.isFocusable], actions: [SemanticsAction.tap], label: 'Hot Tub\nTab 3 of 3', textDirection: TextDirection.ltr, @@ -1252,6 +1255,7 @@ void main() { children: [ TestSemantics( flags: [ + SemanticsFlag.isFocusable, SemanticsFlag.isSelected, ], actions: [SemanticsAction.tap], @@ -1259,11 +1263,13 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + flags: [SemanticsFlag.isFocusable], actions: [SemanticsAction.tap], label: 'Alarm\nTab 2 of 3', textDirection: TextDirection.ltr, ), TestSemantics( + flags: [SemanticsFlag.isFocusable], actions: [SemanticsAction.tap], label: 'Hot Tub\nTab 3 of 3', textDirection: TextDirection.ltr, @@ -1553,6 +1559,7 @@ void main() { children: [ TestSemantics( flags: [ + SemanticsFlag.isFocusable, SemanticsFlag.isSelected, ], actions: [SemanticsAction.tap], @@ -1560,6 +1567,7 @@ void main() { textDirection: TextDirection.ltr, ), TestSemantics( + flags: [SemanticsFlag.isFocusable], actions: [SemanticsAction.tap], label: 'Green\nTab 2 of 2', textDirection: TextDirection.ltr, @@ -1608,12 +1616,14 @@ void main() { TestSemantics( flags: [ SemanticsFlag.isSelected, + SemanticsFlag.isFocusable, ], actions: [SemanticsAction.tap], label: 'Red\nTab 1 of 2', textDirection: TextDirection.ltr, ), TestSemantics( + flags: [SemanticsFlag.isFocusable], actions: [SemanticsAction.tap], label: 'Green\nTab 2 of 2', textDirection: TextDirection.ltr, diff --git a/packages/flutter/test/material/buttons_test.dart b/packages/flutter/test/material/buttons_test.dart index b813af2c37..1fb3f93486 100644 --- a/packages/flutter/test/material/buttons_test.dart +++ b/packages/flutter/test/material/buttons_test.dart @@ -565,9 +565,10 @@ void main() { rect: const Rect.fromLTRB(0.0, 0.0, 88.0, 48.0), transform: Matrix4.translationValues(356.0, 276.0, 0.0), flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], ), ], @@ -605,9 +606,10 @@ void main() { rect: const Rect.fromLTRB(0.0, 0.0, 88.0, 48.0), transform: Matrix4.translationValues(356.0, 276.0, 0.0), flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], ), ], @@ -889,9 +891,10 @@ void main() { SemanticsAction.tap, ], flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], ), ], @@ -920,8 +923,9 @@ void main() { transform: expectedButtonTransform, label: 'Button', flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, + SemanticsFlag.isFocusable, ], ), ], diff --git a/packages/flutter/test/material/card_test.dart b/packages/flutter/test/material/card_test.dart index e021d450fc..7f596a5a58 100644 --- a/packages/flutter/test/material/card_test.dart +++ b/packages/flutter/test/material/card_test.dart @@ -62,9 +62,10 @@ void main() { SemanticsAction.tap, ], flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], ), ], diff --git a/packages/flutter/test/material/chip_test.dart b/packages/flutter/test/material/chip_test.dart index 457cdc8764..365bac1587 100644 --- a/packages/flutter/test/material/chip_test.dart +++ b/packages/flutter/test/material/chip_test.dart @@ -1353,6 +1353,7 @@ void main() { TestSemantics( label: 'test', textDirection: TextDirection.ltr, + flags: [SemanticsFlag.isFocusable], ), ], ), @@ -1390,6 +1391,7 @@ void main() { flags: [ SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], actions: [SemanticsAction.tap], ), @@ -1436,6 +1438,7 @@ void main() { flags: [ SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], actions: [SemanticsAction.tap], ), @@ -1474,9 +1477,10 @@ void main() { label: 'test', textDirection: TextDirection.ltr, flags: [ - SemanticsFlag.isSelected, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + SemanticsFlag.isSelected, ], actions: [SemanticsAction.tap], ), diff --git a/packages/flutter/test/material/date_picker_test.dart b/packages/flutter/test/material/date_picker_test.dart index 36b46e66f7..b4f62d97d6 100644 --- a/packages/flutter/test/material/date_picker_test.dart +++ b/packages/flutter/test/material/date_picker_test.dart @@ -636,25 +636,45 @@ void _tests() { ], ), TestSemantics( - flags: [SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled], + flags: [ + SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + ], actions: [SemanticsAction.tap], label: 'Previous month December 2015', textDirection: TextDirection.ltr, ), TestSemantics( - flags: [SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled], + flags: [ + SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + ], actions: [SemanticsAction.tap], label: 'Next month February 2016', textDirection: TextDirection.ltr, ), TestSemantics( - flags: [SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled], + flags: [ + SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + ], actions: [SemanticsAction.tap], label: 'CANCEL', textDirection: TextDirection.ltr, ), TestSemantics( - flags: [SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled], + flags: [ + SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + ], actions: [SemanticsAction.tap], label: 'OK', textDirection: TextDirection.ltr, diff --git a/packages/flutter/test/material/expand_icon_test.dart b/packages/flutter/test/material/expand_icon_test.dart index 1198b929ec..504ab16798 100644 --- a/packages/flutter/test/material/expand_icon_test.dart +++ b/packages/flutter/test/material/expand_icon_test.dart @@ -148,6 +148,7 @@ void main() { hasTapAction: true, hasEnabledState: true, isEnabled: true, + isFocusable: true, isButton: true, onTapHint: localizations.expandedIconTapHint, )); @@ -163,6 +164,7 @@ void main() { hasTapAction: true, hasEnabledState: true, isEnabled: true, + isFocusable: true, isButton: true, onTapHint: localizations.collapsedIconTapHint, )); diff --git a/packages/flutter/test/material/expansion_panel_test.dart b/packages/flutter/test/material/expansion_panel_test.dart index 7963099e4e..4a97a1645f 100644 --- a/packages/flutter/test/material/expansion_panel_test.dart +++ b/packages/flutter/test/material/expansion_panel_test.dart @@ -194,6 +194,7 @@ void main() { isButton: true, hasEnabledState: true, isEnabled: true, + isFocusable: true, hasTapAction: true, )); @@ -937,6 +938,7 @@ void main() { isButton: true, hasEnabledState: true, isEnabled: true, + isFocusable: true, hasTapAction: true, onTapHint: localizations.expandedIconTapHint, )); @@ -960,6 +962,7 @@ void main() { isButton: true, hasEnabledState: true, isEnabled: true, + isFocusable: true, hasTapAction: true, onTapHint: localizations.collapsedIconTapHint, )); diff --git a/packages/flutter/test/material/floating_action_button_test.dart b/packages/flutter/test/material/floating_action_button_test.dart index 1f2fd0f3e3..65dd4bf5ab 100644 --- a/packages/flutter/test/material/floating_action_button_test.dart +++ b/packages/flutter/test/material/floating_action_button_test.dart @@ -560,9 +560,10 @@ void main() { TestSemantics.rootChild( label: 'Add', flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], actions: [ SemanticsAction.tap, @@ -634,9 +635,10 @@ void main() { SemanticsAction.tap, ], flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], ), ], @@ -780,6 +782,7 @@ void main() { hasEnabledState: true, isButton: true, isEnabled: true, + isFocusable: true, ), ); }, semanticsEnabled: true); diff --git a/packages/flutter/test/material/icon_button_test.dart b/packages/flutter/test/material/icon_button_test.dart index a97bc763fc..247441fe57 100644 --- a/packages/flutter/test/material/icon_button_test.dart +++ b/packages/flutter/test/material/icon_button_test.dart @@ -294,8 +294,9 @@ void main() { ], flags: [ SemanticsFlag.hasEnabledState, - SemanticsFlag.isEnabled, SemanticsFlag.isButton, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], label: 'link', ), diff --git a/packages/flutter/test/material/outline_button_test.dart b/packages/flutter/test/material/outline_button_test.dart index abadf74f5e..87b26cd11d 100644 --- a/packages/flutter/test/material/outline_button_test.dart +++ b/packages/flutter/test/material/outline_button_test.dart @@ -666,9 +666,10 @@ void main() { rect: const Rect.fromLTRB(0.0, 0.0, 88.0, 48.0), transform: Matrix4.translationValues(356.0, 276.0, 0.0), flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], ), ], diff --git a/packages/flutter/test/material/raw_material_button_test.dart b/packages/flutter/test/material/raw_material_button_test.dart index bed44b050a..7c89a639e0 100644 --- a/packages/flutter/test/material/raw_material_button_test.dart +++ b/packages/flutter/test/material/raw_material_button_test.dart @@ -55,9 +55,10 @@ void main() { TestSemantics( id: 1, flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], actions: [ SemanticsAction.tap, diff --git a/packages/flutter/test/material/search_test.dart b/packages/flutter/test/material/search_test.dart index 297d1c1c5c..e24e6e97b1 100644 --- a/packages/flutter/test/material/search_test.dart +++ b/packages/flutter/test/material/search_test.dart @@ -558,9 +558,10 @@ void main() { TestSemantics( id: 10, flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], actions: [SemanticsAction.tap], label: 'Back', @@ -588,9 +589,10 @@ void main() { TestSemantics( id: 8, flags: [ - SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, + SemanticsFlag.isButton, SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, ], actions: [SemanticsAction.tap], label: 'Suggestions', diff --git a/packages/flutter/test/widgets/custom_painter_test.dart b/packages/flutter/test/widgets/custom_painter_test.dart index 1dd63091da..19fea4a897 100644 --- a/packages/flutter/test/widgets/custom_painter_test.dart +++ b/packages/flutter/test/widgets/custom_painter_test.dart @@ -417,6 +417,7 @@ void _defineTests() { textField: true, readOnly: true, focused: true, + focusable: true, inMutuallyExclusiveGroup: true, header: true, obscured: true, @@ -431,6 +432,7 @@ void _defineTests() { ), )); List flags = SemanticsFlag.values.values.toList(); + print('flags: $flags'); // [SemanticsFlag.hasImplicitScrolling] isn't part of [SemanticsProperties] // therefore it has to be removed. flags.remove(SemanticsFlag.hasImplicitScrolling); @@ -466,6 +468,7 @@ void _defineTests() { textField: true, readOnly: true, focused: true, + focusable: true, inMutuallyExclusiveGroup: true, header: true, obscured: true, @@ -482,7 +485,6 @@ void _defineTests() { // [SemanticsFlag.hasImplicitScrolling] isn't part of [SemanticsProperties] // therefore it has to be removed. flags.remove(SemanticsFlag.hasImplicitScrolling); - expectedSemantics = TestSemantics.root( children: [ TestSemantics.rootChild( diff --git a/packages/flutter/test/widgets/focus_scope_test.dart b/packages/flutter/test/widgets/focus_scope_test.dart index 56806b09dc..7273ed8d26 100644 --- a/packages/flutter/test/widgets/focus_scope_test.dart +++ b/packages/flutter/test/widgets/focus_scope_test.dart @@ -2,8 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:ui'; + import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; +import 'package:flutter/semantics.dart'; class TestFocus extends StatefulWidget { const TestFocus({ @@ -24,6 +27,7 @@ class TestFocus extends StatefulWidget { class TestFocusState extends State { FocusNode focusNode; String _label; + bool built = false; @override void dispose() { @@ -50,6 +54,7 @@ class TestFocusState extends State { @override Widget build(BuildContext context) { + built = true; return GestureDetector( onTap: () { FocusScope.of(context).requestFocus(focusNode); @@ -1290,4 +1295,48 @@ void main() { expect(WidgetsBinding.instance.focusManager.rootScope.descendants, isEmpty); }); + testWidgets('Focus widgets set Semantics information about focus', (WidgetTester tester) async { + final GlobalKey key = GlobalKey(); + + await tester.pumpWidget( + TestFocus(key: key, name: 'a'), + ); + + final SemanticsNode semantics = tester.getSemantics(find.byKey(key)); + + expect(key.currentState.focusNode.hasFocus, isFalse); + expect(semantics.hasFlag(SemanticsFlag.isFocused), isFalse); + expect(semantics.hasFlag(SemanticsFlag.isFocusable), isTrue); + + FocusScope.of(key.currentContext).requestFocus(key.currentState.focusNode); + await tester.pumpAndSettle(); + + expect(key.currentState.focusNode.hasFocus, isTrue); + expect(semantics.hasFlag(SemanticsFlag.isFocused), isTrue); + expect(semantics.hasFlag(SemanticsFlag.isFocusable), isTrue); + + key.currentState.focusNode.canRequestFocus = false; + await tester.pumpAndSettle(); + + expect(key.currentState.focusNode.hasFocus, isFalse); + expect(key.currentState.focusNode.canRequestFocus, isFalse); + expect(semantics.hasFlag(SemanticsFlag.isFocused), isFalse); + expect(semantics.hasFlag(SemanticsFlag.isFocusable), isFalse); + }); + testWidgets('Setting canRequestFocus on focus node causes update.', (WidgetTester tester) async { + final GlobalKey key = GlobalKey(); + + final TestFocus testFocus = TestFocus(key: key, name: 'a'); + await tester.pumpWidget( + testFocus, + ); + + await tester.pumpAndSettle(); + key.currentState.built = false; + key.currentState.focusNode.canRequestFocus = false; + await tester.pumpAndSettle(); + key.currentState.built = true; + + expect(key.currentState.focusNode.canRequestFocus, isFalse); + }); } diff --git a/packages/flutter/test/widgets/platform_view_test.dart b/packages/flutter/test/widgets/platform_view_test.dart index e5de7851b8..cc3fd902b8 100644 --- a/packages/flutter/test/widgets/platform_view_test.dart +++ b/packages/flutter/test/widgets/platform_view_test.dart @@ -4,6 +4,7 @@ @TestOn('!chrome') import 'dart:async'; import 'dart:typed_data'; +import 'dart:ui'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; @@ -836,7 +837,15 @@ void main() { ), ); - final SemanticsNode semantics = tester.getSemantics(find.byType(AndroidView)); + // Find the first _AndroidPlatformView widget inside of the AndroidView so + // that it finds the right RenderObject when looking for semantics. + final Finder semanticsFinder = find.byWidgetPredicate( + (Widget widget) { + return widget.runtimeType.toString() == '_AndroidPlatformView'; + }, + description: '_AndroidPlatformView widget inside AndroidView', + ); + final SemanticsNode semantics = tester.getSemantics(semanticsFinder.first); // Platform view has not been created yet, no platformViewId. expect(semantics.platformViewId, null); diff --git a/packages/flutter/test/widgets/semantics_test.dart b/packages/flutter/test/widgets/semantics_test.dart index 92e5990985..d8501387b3 100644 --- a/packages/flutter/test/widgets/semantics_test.dart +++ b/packages/flutter/test/widgets/semantics_test.dart @@ -477,6 +477,7 @@ void main() { textField: true, readOnly: true, focused: true, + focusable: true, inMutuallyExclusiveGroup: true, header: true, obscured: true, diff --git a/packages/flutter_driver/lib/src/extension/extension.dart b/packages/flutter_driver/lib/src/extension/extension.dart index d4d3255a7a..c7a83bcd4b 100644 --- a/packages/flutter_driver/lib/src/extension/extension.dart +++ b/packages/flutter_driver/lib/src/extension/extension.dart @@ -428,7 +428,11 @@ class FlutterDriverExtension { Future _getSemanticsId(Command command) async { final GetSemanticsId semanticsCommand = command; final Finder target = await _waitForElement(_createFinder(semanticsCommand.finder)); - final Element element = target.evaluate().single; + final Iterable elements = target.evaluate(); + if (elements.length > 1) { + throw StateError('Found more than one element with the same ID: $elements'); + } + final Element element = elements.single; RenderObject renderObject = element.renderObject; SemanticsNode node; while (renderObject != null && node == null) { diff --git a/packages/flutter_driver/test/src/extension_test.dart b/packages/flutter_driver/test/src/extension_test.dart index e920a7b8f6..98450ae015 100644 --- a/packages/flutter_driver/test/src/extension_test.dart +++ b/packages/flutter_driver/test/src/extension_test.dart @@ -494,7 +494,7 @@ void main() { final Map response = await extension.call(arguments); expect(response['isError'], true); - expect(response['response'], contains('Bad state: Too many elements')); + expect(response['response'], contains('Bad state: Found more than one element with the same ID')); semantics.dispose(); }); }); diff --git a/packages/flutter_test/lib/src/matchers.dart b/packages/flutter_test/lib/src/matchers.dart index 107f704e92..fd4bfceb5b 100644 --- a/packages/flutter_test/lib/src/matchers.dart +++ b/packages/flutter_test/lib/src/matchers.dart @@ -442,6 +442,7 @@ Matcher matchesSemantics({ bool isButton = false, bool isLink = false, bool isFocused = false, + bool isFocusable = false, bool isTextField = false, bool isReadOnly = false, bool hasEnabledState = false, @@ -494,6 +495,7 @@ Matcher matchesSemantics({ if (isTextField) SemanticsFlag.isTextField, if (isReadOnly) SemanticsFlag.isReadOnly, if (isFocused) SemanticsFlag.isFocused, + if (isFocusable) SemanticsFlag.isFocusable, if (hasEnabledState) SemanticsFlag.hasEnabledState, if (isEnabled) SemanticsFlag.isEnabled, if (isInMutuallyExclusiveGroup) SemanticsFlag.isInMutuallyExclusiveGroup, diff --git a/packages/flutter_test/test/matchers_test.dart b/packages/flutter_test/test/matchers_test.dart index 22f1e7d1dc..314ac5280a 100644 --- a/packages/flutter_test/test/matchers_test.dart +++ b/packages/flutter_test/test/matchers_test.dart @@ -553,6 +553,7 @@ void main() { isReadOnly: true, hasEnabledState: true, isFocused: true, + isFocusable: true, isEnabled: true, isInMutuallyExclusiveGroup: true, isHeader: true,