|
| 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 | +import 'dart:ui' as ui; |
| 7 | + |
| 8 | +import 'package:flutter/material.dart'; |
| 9 | +import 'package:flutter/scheduler.dart'; |
| 10 | +import 'package:flutter/services.dart'; |
| 11 | +import 'package:flutter_driver/driver_extension.dart'; |
| 12 | + |
| 13 | +import 'keys.dart' as keys; |
| 14 | + |
| 15 | +/// Integration test host application for measuring soft keyboard animation smoothness. |
| 16 | +/// |
| 17 | +/// What it tests: |
| 18 | +/// Hosts an edge-to-edge text editing UI and samples FlutterView.viewInsets.bottom |
| 19 | +/// on every frame via persistent frame callbacks, exposing frame-by-frame history and deltas |
| 20 | +/// over FlutterDriverExtension. |
| 21 | +/// |
| 22 | +/// Why it was added: |
| 23 | +/// Provides an end-to-end integration target for testing fix of flutter/flutter#190974 |
| 24 | +/// on live Android emulators/devices. |
| 25 | +void main() { |
| 26 | + final recordedDeltas = <double>[]; |
| 27 | + final recordedInsets = <double>[]; |
| 28 | + double? lastInset; |
| 29 | + |
| 30 | + enableFlutterDriverExtension( |
| 31 | + enableTextEntryEmulation: false, |
| 32 | + handler: (String? message) async { |
| 33 | + if (message == 'get_history') { |
| 34 | + return jsonEncode(<String, dynamic>{'insets': recordedInsets, 'deltas': recordedDeltas}); |
| 35 | + } |
| 36 | + if (message == 'reset_history') { |
| 37 | + recordedDeltas.clear(); |
| 38 | + recordedInsets.clear(); |
| 39 | + lastInset = null; |
| 40 | + return 'ok'; |
| 41 | + } |
| 42 | + return 'unknown_command'; |
| 43 | + }, |
| 44 | + ); |
| 45 | + |
| 46 | + SchedulerBinding.instance.addPersistentFrameCallback((Duration _) { |
| 47 | + final ui.FlutterView? view = WidgetsBinding.instance.platformDispatcher.views.firstOrNull; |
| 48 | + if (view == null) { |
| 49 | + return; |
| 50 | + } |
| 51 | + final double insets = view.viewInsets.bottom / view.devicePixelRatio; |
| 52 | + if (lastInset == null || (insets - lastInset!).abs() > 0.01) { |
| 53 | + final double delta = lastInset == null ? 0.0 : (insets - lastInset!).abs(); |
| 54 | + recordedInsets.add(insets); |
| 55 | + recordedDeltas.add(delta); |
| 56 | + lastInset = insets; |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + runApp(const KeyboardInsetJumpApp()); |
| 61 | +} |
| 62 | + |
| 63 | +class KeyboardInsetJumpApp extends StatefulWidget { |
| 64 | + const KeyboardInsetJumpApp({super.key}); |
| 65 | + |
| 66 | + @override |
| 67 | + State<KeyboardInsetJumpApp> createState() => _KeyboardInsetJumpAppState(); |
| 68 | +} |
| 69 | + |
| 70 | +class _KeyboardInsetJumpAppState extends State<KeyboardInsetJumpApp> { |
| 71 | + final TextEditingController _controller = TextEditingController(); |
| 72 | + final FocusNode _focusNode = FocusNode(); |
| 73 | + |
| 74 | + @override |
| 75 | + void initState() { |
| 76 | + super.initState(); |
| 77 | + // Default to edge-to-edge system UI mode for testing issue #190974 |
| 78 | + SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); |
| 79 | + } |
| 80 | + |
| 81 | + @override |
| 82 | + void dispose() { |
| 83 | + _controller.dispose(); |
| 84 | + _focusNode.dispose(); |
| 85 | + super.dispose(); |
| 86 | + } |
| 87 | + |
| 88 | + @override |
| 89 | + Widget build(BuildContext context) { |
| 90 | + final double insetsBottom = MediaQuery.viewInsetsOf(context).bottom; |
| 91 | + |
| 92 | + return MaterialApp( |
| 93 | + debugShowCheckedModeBanner: false, |
| 94 | + home: Scaffold( |
| 95 | + resizeToAvoidBottomInset: false, |
| 96 | + body: SafeArea( |
| 97 | + bottom: false, |
| 98 | + child: Padding( |
| 99 | + padding: EdgeInsets.only(bottom: insetsBottom), |
| 100 | + child: Column( |
| 101 | + children: <Widget>[ |
| 102 | + const SizedBox(height: 40), |
| 103 | + Text( |
| 104 | + insetsBottom.toStringAsFixed(1), |
| 105 | + key: const Key(keys.kInsetsText), |
| 106 | + style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold), |
| 107 | + ), |
| 108 | + ElevatedButton( |
| 109 | + key: const Key(keys.kEdgeToEdgeButton), |
| 110 | + onPressed: () { |
| 111 | + SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); |
| 112 | + }, |
| 113 | + child: const Text('Enable EdgeToEdge'), |
| 114 | + ), |
| 115 | + const Spacer(), |
| 116 | + Container(height: 8, color: Colors.red), |
| 117 | + Padding( |
| 118 | + padding: const EdgeInsets.all(8.0), |
| 119 | + child: TextField( |
| 120 | + key: const Key(keys.kDefaultTextField), |
| 121 | + controller: _controller, |
| 122 | + focusNode: _focusNode, |
| 123 | + decoration: const InputDecoration( |
| 124 | + hintText: 'Tap to open soft keyboard', |
| 125 | + border: OutlineInputBorder(), |
| 126 | + ), |
| 127 | + ), |
| 128 | + ), |
| 129 | + ], |
| 130 | + ), |
| 131 | + ), |
| 132 | + ), |
| 133 | + floatingActionButton: FloatingActionButton( |
| 134 | + key: const Key(keys.kUnfocusButton), |
| 135 | + onPressed: () { |
| 136 | + _focusNode.unfocus(); |
| 137 | + }, |
| 138 | + child: const Icon(Icons.keyboard_hide), |
| 139 | + ), |
| 140 | + ), |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
0 commit comments