mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
## Description This PR adds the 'Share' button to the text selection toolbar on Android. ## Related Issue Fixes https://github.com/flutter/flutter/issues/138728 ## Tests Refactor a lot of existing tests in order to: - make them more readable (avoid duplication by introducing helper functions, specify explictly check which buttons are expected). - make them more accurate (check that expected buttons are visible instead of just checking the number of buttons). For instance, previous tests contained sections such as: ```dart // Collapsed toolbar shows 3 buttons. expect( find.byType(CupertinoButton), isContextMenuProvidedByPlatform ? findsNothing : isTargetPlatformIOS ? findsNWidgets(6) : findsNWidgets(3) ); ``` Where the comment is obsolete, the two cases (6 widgets and 3 widgets) are not explicit (which buttons are expected?), and not accurate (will pass if the number of buttons is right but the buttons are the wrong ones).
58 lines
2.4 KiB
Dart
58 lines
2.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 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
/// A mock class to control the return result of Live Text input functions.
|
|
class LiveTextInputTester {
|
|
LiveTextInputTester() {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, _handler);
|
|
}
|
|
|
|
bool mockLiveTextInputEnabled = false;
|
|
|
|
Future<Object?> _handler(MethodCall methodCall) async {
|
|
// Need to set Clipboard.hasStrings method handler because when showing the tool bar,
|
|
// the Clipboard.hasStrings will also be invoked. If this isn't handled,
|
|
// an exception will be thrown.
|
|
if (methodCall.method == 'Clipboard.hasStrings') {
|
|
return <String, bool>{'value': true};
|
|
}
|
|
if (methodCall.method == 'LiveText.isLiveTextInputAvailable') {
|
|
return mockLiveTextInputEnabled;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void dispose() {
|
|
assert(TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.checkMockMessageHandler(SystemChannels.platform.name, _handler));
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, null);
|
|
}
|
|
}
|
|
|
|
/// A function to find the live text button.
|
|
///
|
|
/// LiveText button is displayed either using a custom painter,
|
|
/// a Text with an empty label, or a Text with the 'Scan text' label.
|
|
Finder findLiveTextButton() {
|
|
final bool isMobile = defaultTargetPlatform == TargetPlatform.android ||
|
|
defaultTargetPlatform == TargetPlatform.fuchsia ||
|
|
defaultTargetPlatform == TargetPlatform.iOS;
|
|
if (isMobile) {
|
|
return find.byWidgetPredicate((Widget widget) {
|
|
return (widget is CustomPaint && '${widget.painter?.runtimeType}' == '_LiveTextIconPainter')
|
|
|| (widget is Text && widget.data == 'Scan text'); // Android and Fuchsia when inside a MaterialApp.
|
|
});
|
|
}
|
|
if (defaultTargetPlatform == TargetPlatform.macOS) {
|
|
return find.ancestor(of: find.text(''), matching: find.byType(CupertinoDesktopTextSelectionToolbarButton));
|
|
}
|
|
return find.byWidgetPredicate((Widget widget) {
|
|
return widget is Text && (widget.data == '' || widget.data == 'Scan text');
|
|
});
|
|
}
|