From fc1b304326214296ac02bddfed9396e2a716e63d Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 11 Sep 2018 09:39:41 -0700 Subject: [PATCH] Add text field to semantics integration test (#21665) --- .../android_semantics_testing/lib/main.dart | 23 +++- .../lib/src/tests/text_field_constants.dart | 12 ++ .../lib/src/tests/text_field_page.dart | 45 ++++++++ .../lib/test_constants.dart | 1 + .../test_driver/main_test.dart | 107 ++++++++++++++++++ 5 files changed, 183 insertions(+), 5 deletions(-) create mode 100644 dev/integration_tests/android_semantics_testing/lib/src/tests/text_field_constants.dart create mode 100644 dev/integration_tests/android_semantics_testing/lib/src/tests/text_field_page.dart diff --git a/dev/integration_tests/android_semantics_testing/lib/main.dart b/dev/integration_tests/android_semantics_testing/lib/main.dart index 1a08aa4614..27d2a1cd33 100644 --- a/dev/integration_tests/android_semantics_testing/lib/main.dart +++ b/dev/integration_tests/android_semantics_testing/lib/main.dart @@ -6,12 +6,15 @@ import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; import 'package:flutter_driver/driver_extension.dart'; import 'src/tests/controls_page.dart'; +import 'src/tests/text_field_page.dart'; void main() { + timeDilation = 0.05; // remove animations. enableFlutterDriverExtension(handler: dataHandler); runApp(const TestApp()); } @@ -20,17 +23,26 @@ const MethodChannel kSemanticsChannel = MethodChannel('semantics'); Future dataHandler(String message) async { if (message.contains('getSemanticsNode')) { + final Completer completer = new Completer(); final int id = int.tryParse(message.split('#')[1]) ?? 0; - final dynamic result = await kSemanticsChannel.invokeMethod('getSemanticsNode', { - 'id': id, - }); - return json.encode(result); + Future completeSemantics([Object _]) async { + final dynamic result = await kSemanticsChannel.invokeMethod('getSemanticsNode', { + 'id': id, + }); + completer.complete(json.encode(result)); + } + if (SchedulerBinding.instance.hasScheduledFrame) + SchedulerBinding.instance.addPostFrameCallback(completeSemantics); + else + completeSemantics(); + return completer.future; } throw new UnimplementedError(); } const List routes = [ selectionControlsRoute, + textFieldRoute, ]; class TestApp extends StatelessWidget { @@ -41,11 +53,12 @@ class TestApp extends StatelessWidget { return new MaterialApp( routes: { selectionControlsRoute: (BuildContext context) => new SelectionControlsPage(), + textFieldRoute: (BuildContext context) => new TextFieldPage(), }, home: new Builder( builder: (BuildContext context) { return new Scaffold( - body: new Column( + body: new ListView( children: routes.map((String value) { return new MaterialButton( child: new Text(value), diff --git a/dev/integration_tests/android_semantics_testing/lib/src/tests/text_field_constants.dart b/dev/integration_tests/android_semantics_testing/lib/src/tests/text_field_constants.dart new file mode 100644 index 0000000000..ca2d9a1cf3 --- /dev/null +++ b/dev/integration_tests/android_semantics_testing/lib/src/tests/text_field_constants.dart @@ -0,0 +1,12 @@ +// 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. + +/// The name of the route containing the text field tests. +const String textFieldRoute = 'textField'; + +/// The string supplied to the [ValueKey] for the normal text field. +const String normalTextFieldKeyValue = 'textFieldNormal'; + +/// The string supplied to the [ValueKey] for the password text field. +const String passwordTextFieldKeyValue = 'passwordField'; diff --git a/dev/integration_tests/android_semantics_testing/lib/src/tests/text_field_page.dart b/dev/integration_tests/android_semantics_testing/lib/src/tests/text_field_page.dart new file mode 100644 index 0000000000..2719265a50 --- /dev/null +++ b/dev/integration_tests/android_semantics_testing/lib/src/tests/text_field_page.dart @@ -0,0 +1,45 @@ +// 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 'text_field_constants.dart'; + +export 'text_field_constants.dart'; + +/// A page with a normal text field and a password field. +class TextFieldPage extends StatefulWidget { + @override + State createState() => new _TextFieldPageState(); +} + +class _TextFieldPageState extends State { + final TextEditingController _normalController = new TextEditingController(); + final TextEditingController _passwordController = new TextEditingController(); + final Key normalTextFieldKey = const ValueKey(normalTextFieldKeyValue); + final Key passwordTextFieldKey = const ValueKey(passwordTextFieldKeyValue); + + @override + Widget build(BuildContext context) { + return new Scaffold( + appBar: new AppBar(leading: const BackButton(key: ValueKey('back'))), + body: new Material( + child: new Column(children: [ + new TextField( + key: normalTextFieldKey, + controller: _normalController, + autofocus: false, + ), + const Spacer(), + new TextField( + key: passwordTextFieldKey, + controller: _passwordController, + obscureText: true, + autofocus: false, + ), + ], + ), + )); + } +} 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 2ae68a8d93..7764189ed8 100644 --- a/dev/integration_tests/android_semantics_testing/lib/test_constants.dart +++ b/dev/integration_tests/android_semantics_testing/lib/test_constants.dart @@ -3,3 +3,4 @@ // found in the LICENSE file. export 'src/tests/controls_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 ca856e9e9d..07172aa57f 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 @@ -47,6 +47,113 @@ void main() { await run.exitCode; driver?.close(); }); + + group('TextField', () { + setUpAll(() async { + await driver.tap(find.text(textFieldRoute)); + }); + + test('TextField has correct Android semantics', () async { + final SerializableFinder normalTextField = find.byValueKey(normalTextFieldKeyValue); + expect(await getSemantics(normalTextField), hasAndroidSemantics( + className: AndroidClassName.editText, + isEditable: true, + isFocusable: true, + isFocused: false, + isPassword: false, + actions: [ + AndroidSemanticsAction.click, + AndroidSemanticsAction.accessibilityFocus, + ], + )); + + await driver.tap(normalTextField); + + 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, + ], + )); + + await driver.enterText('hello world'); + + 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, + ], + )); + }); + + test('password TextField has correct Android semantics', () async { + final SerializableFinder passwordTextField = find.byValueKey(passwordTextFieldKeyValue); + expect(await getSemantics(passwordTextField), hasAndroidSemantics( + className: AndroidClassName.editText, + isEditable: true, + isFocusable: true, + isFocused: false, + isPassword: true, + actions: [ + AndroidSemanticsAction.click, + AndroidSemanticsAction.accessibilityFocus, + ], + )); + + 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, + ], + )); + + 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, + ], + )); + }); + + tearDownAll(() async { + await driver.tap(find.byValueKey('back')); + }); + }); + group('SelectionControls', () { setUpAll(() async { await driver.tap(find.text(selectionControlsRoute));