|
| 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:convert'; |
| 6 | + |
| 7 | +import 'package:flutter_driver/flutter_driver.dart'; |
| 8 | +import 'package:integration_ui/keys.dart' as keys; |
| 9 | +import 'package:test/test.dart' hide isInstanceOf; |
| 10 | + |
| 11 | +/// End-to-end Flutter Driver test validating soft keyboard animation smoothness. |
| 12 | +/// |
| 13 | +/// What it tests: |
| 14 | +/// 1. Connects to keyboard_inset_jump application on a live device/emulator. |
| 15 | +/// 2. Enables edgeToEdge system UI mode. |
| 16 | +/// 3. Focuses text field to trigger native soft keyboard opening animation. |
| 17 | +/// 4. Checks that viewInsets.bottom increases monotonically and that the final |
| 18 | +/// step is < 20.0dp (below the 24dp+ nav bar jump from #190974, with some |
| 19 | +/// room for dropped frames on CI). |
| 20 | +/// 5. Unfocuses text field and verifies smooth return to 0dp insets. |
| 21 | +/// |
| 22 | +/// Why it was added: |
| 23 | +/// Automated integration/driver test for flutter/flutter#190974. |
| 24 | +void main() { |
| 25 | + group('IME Insets Animation Jump Driver Test', () { |
| 26 | + late FlutterDriver driver; |
| 27 | + |
| 28 | + setUpAll(() async { |
| 29 | + driver = await FlutterDriver.connect(); |
| 30 | + }); |
| 31 | + |
| 32 | + tearDownAll(() async { |
| 33 | + await driver.close(); |
| 34 | + }); |
| 35 | + |
| 36 | + test('validates smooth IME insets animation without terminal jump in edgeToEdge mode', () async { |
| 37 | + final SerializableFinder edgeToEdgeButton = find.byValueKey(keys.kEdgeToEdgeButton); |
| 38 | + final SerializableFinder defaultTextField = find.byValueKey(keys.kDefaultTextField); |
| 39 | + final SerializableFinder insetsText = find.byValueKey(keys.kInsetsText); |
| 40 | + final SerializableFinder unfocusButton = find.byValueKey(keys.kUnfocusButton); |
| 41 | + |
| 42 | + await driver.waitFor(edgeToEdgeButton); |
| 43 | + await driver.tap(edgeToEdgeButton); |
| 44 | + |
| 45 | + // Reset recorded insets frame history before opening keyboard. |
| 46 | + await driver.requestData('reset_history'); |
| 47 | + |
| 48 | + // Tap text field to trigger IME opening animation. |
| 49 | + await driver.waitFor(defaultTextField); |
| 50 | + await driver.tap(defaultTextField); |
| 51 | + |
| 52 | + // Wait for keyboard to open and settle. |
| 53 | + const pollDelay = Duration(milliseconds: 300); |
| 54 | + var settled = false; |
| 55 | + var lastReportedInset = 0.0; |
| 56 | + for (var i = 0; i < 40; ++i) { |
| 57 | + await Future<void>.delayed(pollDelay); |
| 58 | + final String insetsStr = await driver.getText(insetsText); |
| 59 | + final double currentInset = double.tryParse(insetsStr) ?? 0.0; |
| 60 | + if (currentInset > 50.0 && currentInset == lastReportedInset) { |
| 61 | + settled = true; |
| 62 | + break; |
| 63 | + } |
| 64 | + lastReportedInset = currentInset; |
| 65 | + } |
| 66 | + expect(settled, isTrue, reason: 'Keyboard should open and reach a settled non-zero inset'); |
| 67 | + |
| 68 | + // Fetch frame-by-frame trajectory recorded during opening. |
| 69 | + final String rawHistory = await driver.requestData('get_history'); |
| 70 | + final dynamic decoded = jsonDecode(rawHistory); |
| 71 | + // #190974 causes a jump equal to the nav bar height (24dp for gesture nav, |
| 72 | + // 48dp for 3-button) when onEnd fires. Since unchanged frames aren't |
| 73 | + // recorded, deltas.last is the final onProgress step when there's no jump. |
| 74 | + // 20dp is below the 24dp nav bar height while leaving room for a dropped |
| 75 | + // frame near the end of the animation on CI (which can be 10-15dp). |
| 76 | + final Map<String, dynamic> history = decoded is Map<String, dynamic> |
| 77 | + ? decoded |
| 78 | + : <String, dynamic>{}; |
| 79 | + final List<dynamic> insets = history['insets'] as List<dynamic>? ?? <dynamic>[]; |
| 80 | + |
| 81 | + expect( |
| 82 | + insets.length, |
| 83 | + greaterThan(1), |
| 84 | + reason: 'Should have captured multiple animation frames', |
| 85 | + ); |
| 86 | + |
| 87 | + final double finalInset = (insets.last as num).toDouble(); |
| 88 | + expect( |
| 89 | + finalInset, |
| 90 | + greaterThan(100.0), |
| 91 | + reason: 'Keyboard should reach full height, but reached $finalInset', |
| 92 | + ); |
| 93 | + |
| 94 | + // Verify that the opening animation trajectory is monotonically non-decreasing |
| 95 | + // and does not exhibit erratic reversals or jumping backwards. |
| 96 | + for (var i = 1; i < insets.length; i++) { |
| 97 | + final double current = (insets[i] as num).toDouble(); |
| 98 | + final double previous = (insets[i - 1] as num).toDouble(); |
| 99 | + expect( |
| 100 | + current, |
| 101 | + greaterThanOrEqualTo(previous), |
| 102 | + reason: |
| 103 | + 'Opening animation must be monotonic: frame $i ($current) < frame ${i - 1} ($previous)', |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + // Test dismissal |
| 108 | + await driver.requestData('reset_history'); |
| 109 | + await driver.waitFor(unfocusButton); |
| 110 | + await driver.tap(unfocusButton); |
| 111 | + |
| 112 | + var closed = false; |
| 113 | + for (var i = 0; i < 30; ++i) { |
| 114 | + await Future<void>.delayed(pollDelay); |
| 115 | + final String insetsStr = await driver.getText(insetsText); |
| 116 | + final double currentInset = double.tryParse(insetsStr) ?? 0.0; |
| 117 | + if (currentInset == 0.0) { |
| 118 | + closed = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + expect(closed, isTrue, reason: 'Keyboard should dismiss back to 0 inset'); |
| 123 | + }, timeout: Timeout.none); |
| 124 | + }); |
| 125 | +} |
0 commit comments