From b815f76293591d0eb8080f6ca7cae09478985b9f Mon Sep 17 00:00:00 2001 From: Darren Austin Date: Mon, 4 Nov 2019 16:35:04 -0800 Subject: [PATCH] Added tests for the new Android heading semantic flag to android_semantics_testing (#44031) Added tests for the new Android heading semantic flag to android_semantics_testing. --- .../platforminteraction/MainActivity.java | 5 +++ .../android_semantics_testing/lib/main.dart | 2 ++ .../lib/src/common.dart | 4 +++ .../lib/src/matcher.dart | 14 +++++++- .../lib/src/tests/headings_constants.dart | 12 +++++++ .../lib/src/tests/headings_page.dart | 28 ++++++++++++++++ .../lib/test_constants.dart | 1 + .../test_driver/main_test.dart | 33 +++++++++++++++++++ 8 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 dev/integration_tests/android_semantics_testing/lib/src/tests/headings_constants.dart create mode 100644 dev/integration_tests/android_semantics_testing/lib/src/tests/headings_page.dart diff --git a/dev/integration_tests/android_semantics_testing/android/app/src/main/java/com/yourcompany/platforminteraction/MainActivity.java b/dev/integration_tests/android_semantics_testing/android/app/src/main/java/com/yourcompany/platforminteraction/MainActivity.java index 22733fe528..3718f74b74 100644 --- a/dev/integration_tests/android_semantics_testing/android/app/src/main/java/com/yourcompany/platforminteraction/MainActivity.java +++ b/dev/integration_tests/android_semantics_testing/android/app/src/main/java/com/yourcompany/platforminteraction/MainActivity.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.lang.StringBuilder; import android.graphics.Rect; +import android.os.Build; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.WindowManager; @@ -82,6 +83,10 @@ public class MainActivity extends FlutterActivity { flags.put("isEnabled", node.isEnabled()); flags.put("isFocusable", node.isFocusable()); flags.put("isFocused", node.isFocused()); + // heading flag is only available on Android Pie or newer + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + flags.put("isHeading", node.isHeading()); + } flags.put("isPassword", node.isPassword()); flags.put("isLongClickable", node.isLongClickable()); result.put("flags", flags); diff --git a/dev/integration_tests/android_semantics_testing/lib/main.dart b/dev/integration_tests/android_semantics_testing/lib/main.dart index af2561f346..9ae4a84dfc 100644 --- a/dev/integration_tests/android_semantics_testing/lib/main.dart +++ b/dev/integration_tests/android_semantics_testing/lib/main.dart @@ -11,6 +11,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_driver/driver_extension.dart'; import 'src/tests/controls_page.dart'; +import 'src/tests/headings_page.dart'; import 'src/tests/popup_constants.dart'; import 'src/tests/popup_page.dart'; import 'src/tests/text_field_page.dart'; @@ -46,6 +47,7 @@ Map routes = { selectionControlsRoute : (BuildContext context) => SelectionControlsPage(), popupControlsRoute : (BuildContext context) => PopupControlsPage(), textFieldRoute : (BuildContext context) => TextFieldPage(), + headingsRoute: (BuildContext context) => HeadingsPage(), }; class TestApp extends StatelessWidget { diff --git a/dev/integration_tests/android_semantics_testing/lib/src/common.dart b/dev/integration_tests/android_semantics_testing/lib/src/common.dart index c53eb76d76..d282c92586 100644 --- a/dev/integration_tests/android_semantics_testing/lib/src/common.dart +++ b/dev/integration_tests/android_semantics_testing/lib/src/common.dart @@ -33,6 +33,7 @@ class AndroidSemanticsNode { /// "isEnabled": bool, /// "isFocusable": bool, /// "isFocused": bool, + /// "isHeading": bool, /// "isPassword": bool, /// "isLongClickable": bool, /// }, @@ -115,6 +116,9 @@ class AndroidSemanticsNode { /// Whether the node is focused. bool get isFocused => _flags['isFocused']; + /// Whether the node is considered a heading. + bool get isHeading => _flags['isHeading']; + /// Whether the node represents a password field. /// /// Equivalent to [SemanticsFlag.isObscured]. 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 e43139720c..e6a6a1ec55 100644 --- a/dev/integration_tests/android_semantics_testing/lib/src/matcher.dart +++ b/dev/integration_tests/android_semantics_testing/lib/src/matcher.dart @@ -8,7 +8,10 @@ import 'flutter_test_alternative.dart'; /// Matches an [AndroidSemanticsNode]. /// -/// Any properties which aren't supplied are ignored during the comparison. +/// Any properties which aren't supplied are ignored during the comparison, +/// with the exception of `isHeading`. The heading property is not available +/// on all versions of Android. If it is not available on the tested version, +/// it will be match whatever it is compared against. /// /// This matcher is intended to compare the accessibility values generated by /// the Android accessibility bridge, and not the semantics object created by @@ -28,6 +31,7 @@ Matcher hasAndroidSemantics({ bool isEnabled, bool isFocusable, bool isFocused, + bool isHeading, bool isPassword, bool isLongClickable, }) { @@ -45,6 +49,7 @@ Matcher hasAndroidSemantics({ isEnabled: isEnabled, isFocusable: isFocusable, isFocused: isFocused, + isHeading: isHeading, isPassword: isPassword, isLongClickable: isLongClickable, ); @@ -65,6 +70,7 @@ class _AndroidSemanticsMatcher extends Matcher { this.isEditable, this.isFocusable, this.isFocused, + this.isHeading, this.isPassword, this.isLongClickable, }); @@ -82,6 +88,7 @@ class _AndroidSemanticsMatcher extends Matcher { final bool isEnabled; final bool isFocusable; final bool isFocused; + final bool isHeading; final bool isPassword; final bool isLongClickable; @@ -112,6 +119,8 @@ class _AndroidSemanticsMatcher extends Matcher { description.add(' with flag isFocusable: $isFocusable'); if (isFocused != null) description.add(' with flag isFocused: $isFocused'); + if (isHeading != null) + description.add(' with flag isHeading: $isHeading'); if (isPassword != null) description.add(' with flag isPassword: $isPassword'); if (isLongClickable != null) @@ -155,6 +164,9 @@ class _AndroidSemanticsMatcher extends Matcher { return _failWithMessage('Expected isFocusable: $isFocusable', matchState); if (isFocused != null && isFocused != item.isFocused) return _failWithMessage('Expected isFocused: $isFocused', matchState); + // Heading is not available in all Android versions, so match anything if it is not set by the platform + if (isHeading != null && isHeading != item.isHeading && item.isHeading != null) + return _failWithMessage('Expected isHeading: $isHeading', matchState); if (isPassword != null && isPassword != item.isPassword) return _failWithMessage('Expected isPassword: $isPassword', matchState); if (isLongClickable != null && isLongClickable != item.isLongClickable) diff --git a/dev/integration_tests/android_semantics_testing/lib/src/tests/headings_constants.dart b/dev/integration_tests/android_semantics_testing/lib/src/tests/headings_constants.dart new file mode 100644 index 0000000000..afc97db9d0 --- /dev/null +++ b/dev/integration_tests/android_semantics_testing/lib/src/tests/headings_constants.dart @@ -0,0 +1,12 @@ +// 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 headingsRoute = 'headings'; + +/// The string supplied to the [ValueKey] for the app bar title widget. +const String appBarTitleKeyValue = 'Headings#AppBarTitle'; + +/// The string supplied to the [ValueKey] for the body text widget. +const String bodyTextKeyValue = 'Headings#BodyText'; diff --git a/dev/integration_tests/android_semantics_testing/lib/src/tests/headings_page.dart b/dev/integration_tests/android_semantics_testing/lib/src/tests/headings_page.dart new file mode 100644 index 0000000000..6fe68ca37f --- /dev/null +++ b/dev/integration_tests/android_semantics_testing/lib/src/tests/headings_page.dart @@ -0,0 +1,28 @@ +// 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. + +import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; + +import 'headings_constants.dart'; +export 'headings_constants.dart'; + +/// A test page with an app bar and some body text for testing heading flags. +class HeadingsPage extends StatelessWidget { + static const ValueKey _appBarTitleKey = ValueKey(appBarTitleKeyValue); + static const ValueKey _bodyTextKey = ValueKey(bodyTextKeyValue); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + leading: const BackButton(key: ValueKey('back')), + title: const Text('Heading', key: _appBarTitleKey), + ), + body: const Center( + child: Text('Body text', key: _bodyTextKey), + ), + ); + } +} 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 7faf9a2c9f..fc59a3ee6a 100644 --- a/dev/integration_tests/android_semantics_testing/lib/test_constants.dart +++ b/dev/integration_tests/android_semantics_testing/lib/test_constants.dart @@ -3,5 +3,6 @@ // found in the LICENSE file. export 'src/tests/controls_constants.dart'; +export 'src/tests/headings_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 242d487367..0972d67b0f 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 @@ -61,6 +61,8 @@ void main() { group('TextField', () { setUpAll(() async { await driver.tap(find.text(textFieldRoute)); + // Delay for TalkBack to update focus as of November 2019 with Pixel 3 and Android API 28 + await Future.delayed(const Duration(milliseconds: 500)); }); test('TextField has correct Android semantics', () async { @@ -85,6 +87,7 @@ void main() { ); await driver.tap(normalTextField); + // Delay for TalkBack to update focus as of November 2019 with Pixel 3 and Android API 28 await Future.delayed(const Duration(milliseconds: 500)); expect( @@ -105,6 +108,7 @@ void main() { ); await driver.enterText('hello world'); + // Delay for TalkBack to update focus as of November 2019 with Pixel 3 and Android API 28 await Future.delayed(const Duration(milliseconds: 500)); expect( @@ -148,6 +152,8 @@ void main() { ); await driver.tap(passwordTextField); + // Delay for TalkBack to update focus as of November 2019 with Pixel 3 and Android API 28 + await Future.delayed(const Duration(milliseconds: 500)); expect( await getSemantics(passwordTextField), @@ -167,6 +173,8 @@ void main() { ); await driver.enterText('hello world'); + // Delay for TalkBack to update focus as of November 2019 with Pixel 3 and Android API 28 + await Future.delayed(const Duration(milliseconds: 500)); expect( await getSemantics(passwordTextField), @@ -630,5 +638,30 @@ void main() { await driver.tap(find.byValueKey('back')); }); }); + + group('Headings', () { + setUpAll(() async { + await driver.tap(find.text(headingsRoute)); + }); + + test('AppBar title has correct Android heading semantics', () async { + expect( + await getSemantics(find.byValueKey(appBarTitleKeyValue)), + hasAndroidSemantics(isHeading: true), + ); + }); + + test('body text does not have Android heading semantics', () async { + expect( + await getSemantics(find.byValueKey(bodyTextKeyValue)), + hasAndroidSemantics(isHeading: false), + ); + }); + + tearDownAll(() async { + await driver.tap(find.byValueKey('back')); + }); + }); + }); }