mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
## Description This adds `find.backButton()` in the Common Finders to allow finding different types of standard UI elements. It works by attaching a key made from an enum value in a new enum called `StandardComponentType` to all of the standard widgets that perform the associated function. I also substituted the finder in several places where it is useful in tests. This allows writing tests that want to find the "back" button without having to know exactly which icon the back button uses under what circumstances. To do it correctly is actually quite complicated, since there are several adaptations that occur (based on platform, and whether it is web or not). ## Tests - Added tests.
206 lines
7.4 KiB
Dart
206 lines
7.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/material.dart';
|
|
import 'package:flutter/src/foundation/platform.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import '../widgets/editable_text_utils.dart';
|
|
|
|
Finder findCupertinoOverflowNextButton() {
|
|
return find.byWidgetPredicate((Widget widget) {
|
|
return widget is CustomPaint && '${widget.painter?.runtimeType}' == '_RightCupertinoChevronPainter';
|
|
});
|
|
}
|
|
|
|
Finder findCupertinoOverflowBackButton() {
|
|
return find.byWidgetPredicate((Widget widget) {
|
|
return widget is CustomPaint && '${widget.painter?.runtimeType}' == '_LeftCupertinoChevronPainter';
|
|
});
|
|
}
|
|
|
|
Future<void> tapCupertinoOverflowNextButton(WidgetTester tester) async{
|
|
await tester.tapAt(tester.getCenter(findCupertinoOverflowNextButton()));
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
void expectNoCupertinoToolbar() {
|
|
expect(find.byType(CupertinoButton), findsNothing);
|
|
}
|
|
|
|
// Check that the Cupertino text selection toolbars show the expected buttons
|
|
// when the content is partially selected.
|
|
void expectCupertinoToolbarForPartialSelection() {
|
|
if (isContextMenuProvidedByPlatform) {
|
|
expectNoCupertinoToolbar();
|
|
return;
|
|
}
|
|
|
|
switch (defaultTargetPlatform) {
|
|
case TargetPlatform.android:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(5));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Share...'), findsOneWidget);
|
|
expect(find.text('Select All'), findsOneWidget);
|
|
case TargetPlatform.iOS:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(6));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Share...'), findsOneWidget);
|
|
expect(find.text('Look Up'), findsOneWidget);
|
|
expect(find.text('Search Web'), findsOneWidget);
|
|
case TargetPlatform.macOS:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(3));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
case TargetPlatform.fuchsia:
|
|
case TargetPlatform.linux:
|
|
case TargetPlatform.windows:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(4));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Select All'), findsOneWidget);
|
|
}
|
|
}
|
|
|
|
// Check that the Cupertino text selection toolbar shows the expected buttons
|
|
// when the content is fully selected.
|
|
void expectCupertinoToolbarForFullSelection() {
|
|
if (isContextMenuProvidedByPlatform) {
|
|
expectNoCupertinoToolbar();
|
|
return;
|
|
}
|
|
|
|
switch (defaultTargetPlatform) {
|
|
case TargetPlatform.android:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(4));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Share...'), findsOneWidget);
|
|
case TargetPlatform.iOS:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(6));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Share...'), findsOneWidget);
|
|
expect(find.text('Look Up'), findsOneWidget);
|
|
expect(find.text('Search Web'), findsOneWidget);
|
|
case TargetPlatform.fuchsia:
|
|
case TargetPlatform.linux:
|
|
case TargetPlatform.macOS:
|
|
case TargetPlatform.windows:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(3));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
}
|
|
}
|
|
|
|
// Check that the Cupertino text selection toolbar is correct for a collapsed selection.
|
|
void expectCupertinoToolbarForCollapsedSelection() {
|
|
if (isContextMenuProvidedByPlatform) {
|
|
expectNoCupertinoToolbar();
|
|
return;
|
|
}
|
|
|
|
switch (defaultTargetPlatform) {
|
|
case TargetPlatform.android:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(4));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Share...'), findsOneWidget);
|
|
case TargetPlatform.iOS:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(2));
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Select All'), findsOneWidget);
|
|
case TargetPlatform.fuchsia:
|
|
case TargetPlatform.linux:
|
|
case TargetPlatform.windows:
|
|
case TargetPlatform.macOS:
|
|
expect(find.byType(CupertinoButton), findsNWidgets(1));
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
}
|
|
}
|
|
|
|
void expectNoMaterialToolbar() {
|
|
expect(find.byType(TextButton), findsNothing);
|
|
}
|
|
|
|
// Check that the Material text selection toolbars show the expected buttons
|
|
// when the content is partially selected.
|
|
void expectMaterialToolbarForPartialSelection() {
|
|
if (isContextMenuProvidedByPlatform) {
|
|
expectNoMaterialToolbar();
|
|
return;
|
|
}
|
|
|
|
switch (defaultTargetPlatform) {
|
|
case TargetPlatform.android:
|
|
expect(find.byType(TextButton), findsNWidgets(5));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Share'), findsOneWidget);
|
|
expect(find.text('Select all'), findsOneWidget);
|
|
case TargetPlatform.iOS:
|
|
case TargetPlatform.macOS:
|
|
case TargetPlatform.fuchsia:
|
|
case TargetPlatform.linux:
|
|
case TargetPlatform.windows:
|
|
expect(find.byType(TextButton), findsNWidgets(4));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Select all'), findsOneWidget);
|
|
}
|
|
}
|
|
|
|
// Check that the Material text selection toolbar shows the expected buttons
|
|
// when the content is fully selected.
|
|
void expectMaterialToolbarForFullSelection() {
|
|
if (isContextMenuProvidedByPlatform) {
|
|
expectNoMaterialToolbar();
|
|
return;
|
|
}
|
|
|
|
switch (defaultTargetPlatform) {
|
|
case TargetPlatform.android:
|
|
expect(find.byType(TextButton), findsNWidgets(4));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
expect(find.text('Share'), findsOneWidget);
|
|
case TargetPlatform.iOS:
|
|
case TargetPlatform.fuchsia:
|
|
case TargetPlatform.linux:
|
|
case TargetPlatform.macOS:
|
|
case TargetPlatform.windows:
|
|
expect(find.byType(TextButton), findsNWidgets(3));
|
|
expect(find.text('Cut'), findsOneWidget);
|
|
expect(find.text('Copy'), findsOneWidget);
|
|
expect(find.text('Paste'), findsOneWidget);
|
|
}
|
|
}
|
|
|
|
Finder findMaterialOverflowNextButton() {
|
|
return find.byKey(StandardComponentType.moreButton.key);
|
|
}
|
|
|
|
Finder findMaterialOverflowBackButton() {
|
|
return find.byKey(StandardComponentType.backButton.key);
|
|
}
|
|
|
|
Future<void> tapMaterialOverflowNextButton(WidgetTester tester) async {
|
|
await tester.tapAt(tester.getCenter(findMaterialOverflowNextButton()));
|
|
await tester.pumpAndSettle();
|
|
}
|