|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:path/path.dart' as path; |
| 10 | + |
| 11 | +import '../framework/devices.dart'; |
| 12 | +import '../framework/framework.dart'; |
| 13 | +import '../framework/task_result.dart'; |
| 14 | +import '../framework/utils.dart'; |
| 15 | + |
| 16 | +// This test verifies that hot restart hides the keyboard if it is visible. |
| 17 | +// |
| 18 | +// Steps: |
| 19 | +// |
| 20 | +// 1. Launch an app that focuses a text field at startup. |
| 21 | +// This makes the keyboard visible. |
| 22 | +// 2. Wait until the keyboard is visible. |
| 23 | +// 3. Update the app's source code to no longer focus a text field at startup. |
| 24 | +// 4. Hot restart the app |
| 25 | +// 5. Wait until the keyboard is no longer visible. |
| 26 | +// |
| 27 | +// App under test: //dev/integration_tests/keyboard_hot_restart/lib/main.dart |
| 28 | +// |
| 29 | +// Since this test must hot restart the app under test, this test cannot use |
| 30 | +// testing frameworks like XCUITest or Flutter's integration_test as they don't |
| 31 | +// support hot restart. Instead, this test uses the Flutter tool to run the app, |
| 32 | +// hot restart it, and verify its log output. |
| 33 | +TaskFunction createKeyboardHotRestartTest({ |
| 34 | + String? deviceIdOverride, |
| 35 | + bool checkAppRunningOnLocalDevice = false, |
| 36 | + List<String>? additionalOptions, |
| 37 | +}) { |
| 38 | + final Directory appDir = dir( |
| 39 | + path.join(flutterDirectory.path, 'dev/integration_tests/keyboard_hot_restart'), |
| 40 | + ); |
| 41 | + |
| 42 | + // This file is modified during the test and needs to be restored at the end. |
| 43 | + final File mainFile = file(path.join(appDir.path, 'lib/main.dart')); |
| 44 | + final String oldContents = mainFile.readAsStringSync(); |
| 45 | + |
| 46 | + // When the test starts, the app forces the keyboard to be visible. |
| 47 | + // The test turns off this behavior by mutating the app's source code from |
| 48 | + // `forceKeyboardOn` to `forceKeyboardOff`. |
| 49 | + // See: //dev/integration_tests/keyboard_hot_restart/lib/main.dart |
| 50 | + const String forceKeyboardOn = 'const bool forceKeyboard = true;'; |
| 51 | + const String forceKeyboardOff = 'const bool forceKeyboard = false;'; |
| 52 | + |
| 53 | + return () async { |
| 54 | + if (deviceIdOverride == null) { |
| 55 | + final Device device = await devices.workingDevice; |
| 56 | + await device.unlock(); |
| 57 | + deviceIdOverride = device.deviceId; |
| 58 | + } |
| 59 | + |
| 60 | + return inDirectory<TaskResult>(appDir, () async { |
| 61 | + try { |
| 62 | + section('Create app'); |
| 63 | + await createAppProject(); |
| 64 | + |
| 65 | + // Ensure the app forces the keyboard to be visible. |
| 66 | + final String newContents = oldContents.replaceFirst(forceKeyboardOff, forceKeyboardOn); |
| 67 | + mainFile.writeAsStringSync(newContents); |
| 68 | + |
| 69 | + section('Launch app and wait for keyboard to be visible'); |
| 70 | + |
| 71 | + TestState state = TestState.waitUntilKeyboardOpen; |
| 72 | + |
| 73 | + final int exitCode = await runApp( |
| 74 | + options: <String>['-d', deviceIdOverride!], |
| 75 | + onLine: (String line, Process process) { |
| 76 | + if (state == TestState.waitUntilKeyboardOpen) { |
| 77 | + if (!line.contains('flutter: Keyboard is open')) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + section('Update the app to no longer force the keyboard to be visible'); |
| 82 | + final String newContents = oldContents.replaceFirst( |
| 83 | + forceKeyboardOn, |
| 84 | + forceKeyboardOff, |
| 85 | + ); |
| 86 | + mainFile.writeAsStringSync(newContents); |
| 87 | + |
| 88 | + section('Hot restart the app'); |
| 89 | + process.stdin.writeln('R'); |
| 90 | + |
| 91 | + section('Wait until the keyboard is no longer visible'); |
| 92 | + state = TestState.waitUntilKeyboardClosed; |
| 93 | + } else if (state == TestState.waitUntilKeyboardClosed) { |
| 94 | + if (!line.contains('flutter: Keyboard is closed')) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Quit the app. This makes the 'flutter run' process exit. |
| 99 | + process.stdin.writeln('q'); |
| 100 | + } |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + if (exitCode != 0) { |
| 105 | + return TaskResult.failure('flutter run exited with non-zero exit code: $exitCode'); |
| 106 | + } |
| 107 | + } finally { |
| 108 | + mainFile.writeAsStringSync(oldContents); |
| 109 | + } |
| 110 | + |
| 111 | + return TaskResult.success(null); |
| 112 | + }); |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +enum TestState { waitUntilKeyboardOpen, waitUntilKeyboardClosed } |
| 117 | + |
| 118 | +Future<void> createAppProject() async { |
| 119 | + await exec(path.join(flutterDirectory.path, 'bin', 'flutter'), <String>[ |
| 120 | + 'create', |
| 121 | + '--platforms=android,ios', |
| 122 | + '.', |
| 123 | + ]); |
| 124 | +} |
| 125 | + |
| 126 | +Future<int> runApp({ |
| 127 | + required List<String> options, |
| 128 | + required void Function(String, Process) onLine, |
| 129 | +}) async { |
| 130 | + final Process process = await startFlutter('run', options: options); |
| 131 | + |
| 132 | + final Completer<void> stdoutDone = Completer<void>(); |
| 133 | + final Completer<void> stderrDone = Completer<void>(); |
| 134 | + |
| 135 | + void onStdout(String line) { |
| 136 | + onLine(line, process); |
| 137 | + print('stdout: $line'); |
| 138 | + } |
| 139 | + |
| 140 | + process.stdout |
| 141 | + .transform<String>(utf8.decoder) |
| 142 | + .transform<String>(const LineSplitter()) |
| 143 | + .listen(onStdout, onDone: stdoutDone.complete); |
| 144 | + |
| 145 | + process.stderr |
| 146 | + .transform<String>(utf8.decoder) |
| 147 | + .transform<String>(const LineSplitter()) |
| 148 | + .listen((String line) => print('stderr: $line'), onDone: stderrDone.complete); |
| 149 | + |
| 150 | + await Future.wait<void>(<Future<void>>[stdoutDone.future, stderrDone.future]); |
| 151 | + return process.exitCode; |
| 152 | +} |
0 commit comments