flutter_flutter/packages/flutter/test/widgets/editable_text_scribe_test.dart
Justin McCandless b473698019
Scribe Android handwriting text input (#148784)
Enables the Scribe feature, or Android stylus handwriting text input.


![scribe](https://github.com/flutter/flutter/assets/389558/25a54ae9-9399-4772-8482-913ec7a9b330)

This PR only implements basic handwriting input. Other features will be
done in subsequent PRs:

 * https://github.com/flutter/flutter/issues/155948
 * https://github.com/flutter/flutter/issues/156018

I created and fixed issue about stylus hovering while working on this:
https://github.com/flutter/flutter/issues/148810

Original PR for iOS Scribble, the iOS version of this feature:
https://github.com/flutter/flutter/pull/75472
FYI @fbcouch 

~~Depends on https://github.com/flutter/engine/pull/52943~~ (merged).

Fixes https://github.com/flutter/flutter/issues/115607

<details>

<summary>Example code I'm using to test this feature (but any TextField
works)</summary>

```dart
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final FocusNode _focusNode1 = FocusNode();
  final FocusNode _focusNode2 = FocusNode();
  final FocusNode _focusNode3 = FocusNode();
  final TextEditingController _controller3 = TextEditingController(
    text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Scribe demo'),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 74.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                focusNode: _focusNode1,
                autofocus: false,
              ),
              TextField(
                focusNode: _focusNode2,
              ),
              TextField(
                focusNode: _focusNode3,
                minLines: 4,
                maxLines: 4,
                controller: _controller3,
              ),
              TextButton(
                onPressed: () {
                  _focusNode1.unfocus();
                  _focusNode2.unfocus();
                  _focusNode3.unfocus();
                },
                child: const Text('Unfocus'),
              ),
              TextButton(
                onPressed: () {
                  _focusNode1.requestFocus();
                  SchedulerBinding.instance.addPostFrameCallback((Duration _) {
                    SystemChannels.textInput.invokeMethod('TextInput.hide');
                  });
                },
                child: const Text('Focus 1'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
```

</details>

---------

Co-authored-by: Nate Wilson <nate.w5687@gmail.com>
2024-11-21 01:33:14 +00:00

230 lines
8.4 KiB
Dart

// Copyright 2014 The Flutter 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 'dart:ui' show PointerDeviceKind;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
const TextStyle textStyle = TextStyle();
const Color cursorColor = Color.fromARGB(0xFF, 0xFF, 0x00, 0x00);
final List<MethodCall> calls = <MethodCall>[];
bool isFeatureAvailableReturnValue = true;
late TextEditingController controller;
late FocusNode focusNode;
setUp(() async {
calls.clear();
isFeatureAvailableReturnValue = true;
binding.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.scribe, (MethodCall methodCall) {
calls.add(methodCall);
return switch (methodCall.method) {
'Scribe.isFeatureAvailable' => Future<bool>.value(isFeatureAvailableReturnValue),
'Scribe.startStylusHandwriting' => Future<void>.value(),
_=> throw FlutterError('Unexpected method call: ${methodCall.method}'),
};
});
controller = TextEditingController(
text: 'Lorem ipsum dolor sit amet',
);
focusNode = FocusNode(debugLabel: 'EditableText Node');
});
tearDown(() {
controller.dispose();
focusNode.dispose();
});
Future<void> pumpTextSelectionGestureDetectorBuilder(
WidgetTester tester, {
bool forcePressEnabled = true,
bool selectionEnabled = true,
}) async {
final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>();
final FakeTextSelectionGestureDetectorBuilderDelegate delegate = FakeTextSelectionGestureDetectorBuilderDelegate(
editableTextKey: editableTextKey,
forcePressEnabled: forcePressEnabled,
selectionEnabled: selectionEnabled,
);
final TextSelectionGestureDetectorBuilder provider =
TextSelectionGestureDetectorBuilder(delegate: delegate);
await tester.pumpWidget(
MaterialApp(
home: provider.buildGestureDetector(
behavior: HitTestBehavior.translucent,
child: EditableText(
key: editableTextKey,
controller: controller,
backgroundCursorColor: Colors.grey,
focusNode: focusNode,
style: textStyle,
cursorColor: cursorColor,
selectionControls: materialTextSelectionControls,
showSelectionHandles: true,
),
),
),
);
}
testWidgets('when Scribe is available, starts handwriting on tap down', (WidgetTester tester) async {
isFeatureAvailableReturnValue = true;
await pumpTextSelectionGestureDetectorBuilder(tester);
expect(focusNode.hasFocus, isFalse);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.stylus, pointer: 1);
await gesture.down(tester.getCenter(find.byType(EditableText)));
// Wait for the gesture arena.
await tester.pumpAndSettle();
expect(calls, hasLength(2));
expect(calls.first.method, 'Scribe.isFeatureAvailable');
expect(calls[1].method, 'Scribe.startStylusHandwriting');
await gesture.up();
expect(focusNode.hasFocus, isTrue);
// On web, let the browser handle handwriting input.
}, skip: kIsWeb, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android })); // [intended]
testWidgets('when Scribe is unavailable, does not start handwriting on tap down', (WidgetTester tester) async {
isFeatureAvailableReturnValue = false;
await pumpTextSelectionGestureDetectorBuilder(tester);
expect(focusNode.hasFocus, isFalse);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.stylus, pointer: 1);
await gesture.down(tester.getCenter(find.byType(EditableText)));
// Wait for the gesture arena.
await tester.pumpAndSettle();
expect(calls, hasLength(1));
expect(calls.first.method, 'Scribe.isFeatureAvailable');
await gesture.up();
}, skip: kIsWeb, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android })); // [intended]
testWidgets('tap down event must be from a stylus in order to start handwriting', (WidgetTester tester) async {
isFeatureAvailableReturnValue = true;
await pumpTextSelectionGestureDetectorBuilder(tester);
expect(focusNode.hasFocus, isFalse);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
await gesture.down(tester.getCenter(find.byType(EditableText)));
expect(calls, isEmpty);
await gesture.up();
}, skip: kIsWeb, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android })); // [intended]
testWidgets('tap down event on a collapsed selection handle is handled by the handle and does not start handwriting', (WidgetTester tester) async {
isFeatureAvailableReturnValue = true;
await pumpTextSelectionGestureDetectorBuilder(tester);
expect(focusNode.hasFocus, isFalse);
expect(find.byType(CompositedTransformFollower), findsNothing);
// Tap to show the collapsed selection handle.
final Offset fieldOffset = tester.getTopLeft(find.byType(EditableText));
await tester.tapAt(fieldOffset + const Offset(20.0, 10.0));
await tester.pump();
expect(find.byType(CompositedTransformFollower), findsOneWidget);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.stylus, pointer: 1);
final Finder handleFinder = find.descendant(
of: find.byType(CompositedTransformFollower),
matching: find.byType(CustomPaint),
);
await gesture.down(tester.getCenter(handleFinder));
// Wait for the gesture arena.
await tester.pumpAndSettle();
expect(calls, hasLength(0));
expect(controller.selection.isCollapsed, isTrue);
final int cursorStart = controller.selection.start;
// Dragging on top of the handle moves it like normal.
await gesture.moveBy(const Offset(20.0, 0.0));
expect(controller.selection.isCollapsed, isTrue);
expect(controller.selection.start, greaterThan(cursorStart));
expect(calls, hasLength(0));
await gesture.up();
}, skip: kIsWeb, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android })); // [intended]
testWidgets('tap down event on the end selection handle is handled by the handle and does not start handwriting', (WidgetTester tester) async {
isFeatureAvailableReturnValue = true;
await pumpTextSelectionGestureDetectorBuilder(tester);
expect(focusNode.hasFocus, isFalse);
expect(find.byType(CompositedTransformFollower), findsNothing);
// Long press to select the first word and show both handles.
final Offset fieldOffset = tester.getTopLeft(find.byType(EditableText));
await tester.longPressAt(fieldOffset + const Offset(20.0, 10.0));
await tester.pump();
expect(find.byType(CompositedTransformFollower), findsNWidgets(2));
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.stylus, pointer: 1);
final Finder endHandleFinder = find.descendant(
of: find.byType(CompositedTransformFollower).at(1),
matching: find.byType(CustomPaint),
);
await gesture.down(tester.getCenter(endHandleFinder));
// Wait for the gesture arena.
await tester.pumpAndSettle();
expect(calls, isEmpty);
expect(controller.selection.isCollapsed, isFalse);
final TextSelection selectionStart = controller.selection;
// Dragging on top of the handle extends selection like normal.
await gesture.moveBy(const Offset(20.0, 0.0));
expect(controller.selection.isCollapsed, isFalse);
expect(controller.selection.start, equals(selectionStart.start));
expect(controller.selection.end, greaterThan(selectionStart.end));
expect(calls, isEmpty);
await gesture.up();
}, skip: kIsWeb, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android })); // [intended]
}
class FakeTextSelectionGestureDetectorBuilderDelegate implements TextSelectionGestureDetectorBuilderDelegate {
FakeTextSelectionGestureDetectorBuilderDelegate({
required this.editableTextKey,
required this.forcePressEnabled,
required this.selectionEnabled,
});
@override
final GlobalKey<EditableTextState> editableTextKey;
@override
final bool forcePressEnabled;
@override
final bool selectionEnabled;
}