|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Part of PyBLE (https://pyble.dev) — see /LICENSE. |
| 3 | + |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter/services.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | +import 'package:pyble/console/console.dart'; |
| 8 | +import 'package:pyble/pble/pble.dart'; |
| 9 | + |
| 10 | +import '../support/shell_harness.dart'; |
| 11 | + |
| 12 | +const MethodChannel _channel = MethodChannel('dev.pyble/keyboard/insets'); |
| 13 | + |
| 14 | +void main() { |
| 15 | + final List<MethodCall> calls = <MethodCall>[]; |
| 16 | + setUp(() { |
| 17 | + calls.clear(); |
| 18 | + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger |
| 19 | + .setMockMethodCallHandler(_channel, (MethodCall call) async { |
| 20 | + calls.add(call); |
| 21 | + return null; |
| 22 | + }); |
| 23 | + }); |
| 24 | + tearDown(() { |
| 25 | + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger |
| 26 | + .setMockMethodCallHandler(_channel, null); |
| 27 | + }); |
| 28 | + |
| 29 | + int generation() => |
| 30 | + (calls.lastWhere((MethodCall call) => call.method == 'observe').arguments |
| 31 | + as int); |
| 32 | + |
| 33 | + Future<void> report(WidgetTester tester, double bottom, {int? epoch}) async { |
| 34 | + await tester.binding.defaultBinaryMessenger.handlePlatformMessage( |
| 35 | + _channel.name, |
| 36 | + _channel.codec.encodeMethodCall( |
| 37 | + MethodCall('inset', <String, Object>{ |
| 38 | + 'generation': epoch ?? generation(), |
| 39 | + 'bottom': bottom, |
| 40 | + }), |
| 41 | + ), |
| 42 | + (_) {}, |
| 43 | + ); |
| 44 | + await tester.pump(); |
| 45 | + } |
| 46 | + |
| 47 | + MediaQueryData geometry(WidgetTester tester) => |
| 48 | + MediaQuery.of(tester.element(find.byType(Scaffold).first)); |
| 49 | + |
| 50 | + testWidgets( |
| 51 | + 'iPad palette reopening keeps Expand above the actual keyboard', |
| 52 | + (WidgetTester tester) async { |
| 53 | + await pumpShell( |
| 54 | + tester, |
| 55 | + connection: fakeConnection(initial: ConnState.ready), |
| 56 | + ); |
| 57 | + expect( |
| 58 | + calls.where((MethodCall call) => call.method == 'observe'), |
| 59 | + isNotEmpty, |
| 60 | + ); |
| 61 | + final EditableText editor = tester.widget( |
| 62 | + find.byType(EditableText).first, |
| 63 | + ); |
| 64 | + editor.controller.text = '# retained across native geometry'; |
| 65 | + editor.controller.selection = const TextSelection.collapsed(offset: 5); |
| 66 | + await report(tester, 81.5); |
| 67 | + expect(geometry(tester).viewInsets.bottom, 81.5); |
| 68 | + await report(tester, 498); |
| 69 | + expect(geometry(tester).viewInsets.bottom, 498); |
| 70 | + expect( |
| 71 | + tester.getRect(find.byKey(kConsoleStripToggleKey)).bottom, |
| 72 | + lessThanOrEqualTo(1024 - 498), |
| 73 | + ); |
| 74 | + expect(find.byKey(kConsoleStripToggleKey).hitTestable(), findsOneWidget); |
| 75 | + expect(editor.controller.text, '# retained across native geometry'); |
| 76 | + expect( |
| 77 | + editor.controller.selection, |
| 78 | + const TextSelection.collapsed(offset: 5), |
| 79 | + ); |
| 80 | + expect(tester.takeException(), isNull); |
| 81 | + }, |
| 82 | + variant: TargetPlatformVariant.only(TargetPlatform.iOS), |
| 83 | + ); |
| 84 | + |
| 85 | + testWidgets( |
| 86 | + 'guide and Flutter insets combine by maximum and restore padding', |
| 87 | + (WidgetTester tester) async { |
| 88 | + await pumpShell( |
| 89 | + tester, |
| 90 | + connection: fakeConnection(initial: ConnState.ready), |
| 91 | + ); |
| 92 | + tester.view.viewPadding = const FakeViewPadding(bottom: 40); |
| 93 | + tester.view.padding = const FakeViewPadding(bottom: 40); |
| 94 | + tester.view.viewInsets = const FakeViewPadding(bottom: 996); |
| 95 | + addTearDown(tester.view.resetViewPadding); |
| 96 | + addTearDown(tester.view.resetPadding); |
| 97 | + addTearDown(tester.view.resetViewInsets); |
| 98 | + await tester.pump(); |
| 99 | + await report(tester, 498); |
| 100 | + expect(geometry(tester).viewInsets.bottom, 498); |
| 101 | + expect(geometry(tester).padding.bottom, 0); |
| 102 | + await report(tester, 81.5); |
| 103 | + expect(geometry(tester).viewInsets.bottom, 498); |
| 104 | + tester.view.resetViewInsets(); |
| 105 | + await tester.pump(); |
| 106 | + expect(geometry(tester).viewInsets.bottom, 81.5); |
| 107 | + await report(tester, 0); |
| 108 | + expect(geometry(tester).viewInsets.bottom, 0); |
| 109 | + expect(geometry(tester).padding.bottom, 20); |
| 110 | + }, |
| 111 | + variant: TargetPlatformVariant.only(TargetPlatform.iOS), |
| 112 | + ); |
| 113 | + |
| 114 | + testWidgets( |
| 115 | + 'background resets observation and stale or invalid reports are inert', |
| 116 | + (WidgetTester tester) async { |
| 117 | + await pumpShell( |
| 118 | + tester, |
| 119 | + connection: fakeConnection(initial: ConnState.ready), |
| 120 | + ); |
| 121 | + final int old = generation(); |
| 122 | + await report(tester, 498); |
| 123 | + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused); |
| 124 | + await tester.pump(); |
| 125 | + expect(geometry(tester).viewInsets.bottom, 0); |
| 126 | + await report(tester, 498, epoch: old); |
| 127 | + expect(geometry(tester).viewInsets.bottom, 0); |
| 128 | + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); |
| 129 | + await tester.pump(); |
| 130 | + expect(generation(), isNot(old)); |
| 131 | + await report(tester, 498, epoch: old); |
| 132 | + await report(tester, -1); |
| 133 | + await report(tester, double.infinity); |
| 134 | + await report(tester, 2048); |
| 135 | + expect(geometry(tester).viewInsets.bottom, 0); |
| 136 | + await report(tester, 70.5); |
| 137 | + expect(geometry(tester).viewInsets.bottom, 70.5); |
| 138 | + await tester.pumpWidget(const SizedBox()); |
| 139 | + expect(calls.last.method, 'stop'); |
| 140 | + }, |
| 141 | + variant: TargetPlatformVariant.only(TargetPlatform.iOS), |
| 142 | + ); |
| 143 | + |
| 144 | + testWidgets( |
| 145 | + 'Android retains ordinary Flutter keyboard geometry', |
| 146 | + (WidgetTester tester) async { |
| 147 | + await pumpShell( |
| 148 | + tester, |
| 149 | + connection: fakeConnection(initial: ConnState.ready), |
| 150 | + ); |
| 151 | + expect(calls, isEmpty); |
| 152 | + tester.view.viewInsets = const FakeViewPadding(bottom: 808); |
| 153 | + addTearDown(tester.view.resetViewInsets); |
| 154 | + await tester.pump(); |
| 155 | + expect(geometry(tester).viewInsets.bottom, 404); |
| 156 | + }, |
| 157 | + variant: TargetPlatformVariant.only(TargetPlatform.android), |
| 158 | + ); |
| 159 | +} |
0 commit comments