mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Move context menus out of experimental (#2134)
Now that the context menu code is on stable, move the sample out of experimental. Fixes https://github.com/flutter/samples/issues/2110 ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] All existing and new tests are passing. --------- Co-authored-by: Brett Morgan <brett.morgan@gmail.com>
This commit is contained in:
committed by
GitHub
parent
9b17f50671
commit
6c8d54a82b
36
context_menus/test/anywhere_page_test.dart
Normal file
36
context_menus/test/anywhere_page_test.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:context_menus/anywhere_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Right click works outside of text', (tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the AnywherePage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(AnywherePage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -100.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(AnywherePage.title));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Right click on the background of the app to show the context menu.
|
||||
final TestGesture gesture = await tester.startGesture(
|
||||
const Offset(100.0, 100.0),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The context menu is shown, with a custom back button.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('Back'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
126
context_menus/test/cascading_menu_page_test.dart
Normal file
126
context_menus/test/cascading_menu_page_test.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
import 'package:context_menus/cascading_menu_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Can show and use the cascading menu', (tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the CascadingMenuPage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(CascadingMenuPage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -250.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(CascadingMenuPage.title));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
const String message =
|
||||
'Right click or long press anywhere to show the cascading menu.';
|
||||
expect(find.text(message), findsOneWidget);
|
||||
|
||||
// Right click on the background of the app to show the cascading context
|
||||
// menu.
|
||||
TestGesture gesture = await tester.startGesture(
|
||||
const Offset(0.0, 100.0),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The custom cascading context menu is shown.
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('About'), findsOneWidget);
|
||||
expect(find.text('Show'), findsNothing);
|
||||
expect(find.text('Hide'), findsOneWidget);
|
||||
expect(find.text('Reset'), findsOneWidget);
|
||||
expect(find.text('Color'), findsOneWidget);
|
||||
|
||||
// Click "Hide Message" and the message and menu are hidden.
|
||||
await tester.tap(find.text('Hide'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsNothing);
|
||||
expect(find.text(message), findsNothing);
|
||||
|
||||
// Right click to show the menu again.
|
||||
gesture = await tester.startGesture(
|
||||
const Offset(0.0, 100.0),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('About'), findsOneWidget);
|
||||
expect(find.text('Show'), findsOneWidget);
|
||||
expect(find.text('Hide'), findsNothing);
|
||||
expect(find.text('Reset'), findsOneWidget);
|
||||
expect(find.text('Color'), findsOneWidget);
|
||||
|
||||
// Use the shortcut to show the message again.
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.control);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.keyS);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.control);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyS);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsNothing);
|
||||
expect(find.text(message), findsOneWidget);
|
||||
|
||||
// Right click to show the menu again.
|
||||
gesture = await tester.startGesture(
|
||||
const Offset(0.0, 100.0),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('About'), findsOneWidget);
|
||||
expect(find.text('Show'), findsNothing);
|
||||
expect(find.text('Hide'), findsOneWidget);
|
||||
expect(find.text('Reset'), findsOneWidget);
|
||||
expect(find.text('Color'), findsOneWidget);
|
||||
expect(find.text('Red'), findsNothing);
|
||||
expect(find.text('Green'), findsNothing);
|
||||
expect(find.text('Blue'), findsNothing);
|
||||
|
||||
// Hover Background Color.
|
||||
gesture = await tester.startGesture(
|
||||
//tester.getCenter(find.text('Color')),
|
||||
const Offset(0.0, 100),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.moveTo(tester.getCenter(find.text('Color')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Background colors are shown.
|
||||
expect(find.text('Red'), findsOneWidget);
|
||||
expect(find.text('Green'), findsOneWidget);
|
||||
expect(find.text('Blue'), findsOneWidget);
|
||||
|
||||
// Select green.
|
||||
await gesture.moveTo(tester.getCenter(find.text('Green')));
|
||||
await tester.pump();
|
||||
await tester.tap(find.text('Green'));
|
||||
await tester.pump();
|
||||
await gesture.removePointer();
|
||||
|
||||
// The menu is gone and the UI shows green was selected.
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsNothing);
|
||||
expect(find.text('Last Selected: Green Background'), findsOneWidget);
|
||||
},
|
||||
// TODO(justinmc): https://github.com/flutter/samples/issues/2086
|
||||
skip: true);
|
||||
}
|
||||
55
context_menus/test/custom_buttons_page_test.dart
Normal file
55
context_menus/test/custom_buttons_page_test.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'package:context_menus/custom_buttons_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Shows custom buttons in the built-in context menu',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the CustomButtonsPage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(CustomButtonsPage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -100.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(CustomButtonsPage.title));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Right click on the text field to show the context menu.
|
||||
final TestGesture gesture = await tester.startGesture(
|
||||
tester.getCenter(find.byType(EditableText)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The context menu is shown, and the buttons are custom widgets.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
expect(find.byType(CupertinoTextSelectionToolbarButton), findsNothing);
|
||||
expect(find.byType(CupertinoButton), findsNWidgets(2));
|
||||
case TargetPlatform.macOS:
|
||||
expect(find.byType(CupertinoButton), findsNWidgets(2));
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbarButton),
|
||||
findsNothing);
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
expect(find.byType(CupertinoButton), findsNWidgets(1));
|
||||
expect(find.byType(TextSelectionToolbarTextButton), findsNothing);
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(find.byType(CupertinoButton), findsNWidgets(1));
|
||||
expect(find.byType(DesktopTextSelectionToolbarButton), findsNothing);
|
||||
}
|
||||
});
|
||||
}
|
||||
53
context_menus/test/custom_menu_page_test.dart
Normal file
53
context_menus/test/custom_menu_page_test.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
import 'package:context_menus/custom_menu_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Shows default buttons in a custom context menu', (tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the CustomMenuPage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(CustomMenuPage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -200.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(CustomMenuPage.title));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Right click on the text field to show the context menu.
|
||||
final TestGesture gesture = await tester.startGesture(
|
||||
tester.getCenter(find.byType(EditableText)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// A custom context menu is shown, and the buttons are the default ones.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||
expect(find.byType(CupertinoAdaptiveTextSelectionToolbar), findsNothing);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
expect(
|
||||
find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(2));
|
||||
case TargetPlatform.macOS:
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbarButton),
|
||||
findsNWidgets(2));
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
expect(find.byType(TextSelectionToolbarTextButton), findsNWidgets(1));
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(
|
||||
find.byType(DesktopTextSelectionToolbarButton), findsNWidgets(1));
|
||||
}
|
||||
});
|
||||
}
|
||||
99
context_menus/test/default_values_page_test.dart
Normal file
99
context_menus/test/default_values_page_test.dart
Normal file
@@ -0,0 +1,99 @@
|
||||
import 'package:context_menus/default_values_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Gives correct behavior for all values of contextMenuBuilder',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the DefaultValuesPage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(DefaultValuesPage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -100.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(DefaultValuesPage.title));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(AppBar),
|
||||
matching: find.text(DefaultValuesPage.title),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
// Right click on the text field where contextMenuBuilder isn't passed.
|
||||
TestGesture gesture = await tester.startGesture(
|
||||
tester.getCenter(find.byType(EditableText).first),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The default context menu is shown.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
expect(
|
||||
find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(2));
|
||||
case TargetPlatform.macOS:
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbarButton),
|
||||
findsNWidgets(2));
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
expect(find.byType(TextSelectionToolbarTextButton), findsNWidgets(1));
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(
|
||||
find.byType(DesktopTextSelectionToolbarButton), findsNWidgets(1));
|
||||
}
|
||||
|
||||
// Tap the next field to hide the context menu.
|
||||
await tester.tap(find.byType(EditableText).at(1));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||
|
||||
// Right click on the text field where contextMenuBuilder is given null.
|
||||
gesture = await tester.startGesture(
|
||||
tester.getCenter(find.byType(EditableText).at(1)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// No context menu is shown.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||
|
||||
// Tap the next field to hide the context menu.
|
||||
await tester.tap(find.byType(EditableText).at(2));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||
|
||||
// Right click on the text field with the custom contextMenuBuilder.
|
||||
gesture = await tester.startGesture(
|
||||
tester.getCenter(find.byType(EditableText).at(2)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The custom context menu is shown.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('Custom button'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
111
context_menus/test/email_button_page_test.dart
Normal file
111
context_menus/test/email_button_page_test.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'package:context_menus/email_button_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'utils.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Selecting the email address shows a custom button',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the EmailButtonPage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(EmailButtonPage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -100.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(EmailButtonPage.title));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Select the first word, then right click to show the context menu.
|
||||
expect(find.byType(TextField), findsOneWidget);
|
||||
await tester.tapAt(tester.getTopLeft(find.byType(EditableText)));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shift);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
|
||||
}
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shift);
|
||||
await tester.pumpAndSettle();
|
||||
final TestGesture gesture1 = await tester.startGesture(
|
||||
textOffsetToPosition(tester, 4),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture1.up();
|
||||
await gesture1.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The context menu is shown, but no email button appears.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('Send email'), findsNothing);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
expect(
|
||||
find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(2));
|
||||
case TargetPlatform.macOS:
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbarButton),
|
||||
findsNWidgets(2));
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
expect(find.byType(TextSelectionToolbarTextButton), findsNWidgets(3));
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(
|
||||
find.byType(DesktopTextSelectionToolbarButton), findsNWidgets(3));
|
||||
}
|
||||
|
||||
// Click on "Copy" to hide the context menu.
|
||||
await tester.tap(find.text('Copy'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||
|
||||
// Select the email address, then right click it to show the context menu.
|
||||
for (int i = 0; i < 38; i++) {
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
|
||||
}
|
||||
await tester.pumpAndSettle();
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shift);
|
||||
for (int i = 0; i < 15; i++) {
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
|
||||
}
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shift);
|
||||
final TestGesture gesture2 = await tester.startGesture(
|
||||
textOffsetToPosition(tester, 48),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture2.up();
|
||||
await gesture2.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The context menu is shown, and the email button now appears.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('Send email'), findsOneWidget);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
expect(
|
||||
find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(3));
|
||||
case TargetPlatform.macOS:
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbarButton),
|
||||
findsNWidgets(3));
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
expect(find.byType(TextSelectionToolbarTextButton), findsNWidgets(4));
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(
|
||||
find.byType(DesktopTextSelectionToolbarButton), findsNWidgets(4));
|
||||
}
|
||||
// TODO: Test failing on Flutter 3.18.0-7.0.pre.57 https://github.com/flutter/samples/issues/2110
|
||||
}, skip: true);
|
||||
}
|
||||
179
context_menus/test/field_types_page_test.dart
Normal file
179
context_menus/test/field_types_page_test.dart
Normal file
@@ -0,0 +1,179 @@
|
||||
import 'package:context_menus/field_types_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'Gives correct behavior for all values of contextMenuBuilder',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the FieldTypesPage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(FieldTypesPage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -100.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(FieldTypesPage.title));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(AppBar),
|
||||
matching: find.text(FieldTypesPage.title),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
// Right click on the TextField.
|
||||
TestGesture gesture = await tester.startGesture(
|
||||
tester.getTopLeft(find.byType(TextField)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The default context menu for the current platform is shown.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
expect(find.byType(CupertinoTextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.android:
|
||||
expect(find.byType(TextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.fuchsia:
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.macOS:
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbar),
|
||||
findsOneWidget);
|
||||
}
|
||||
|
||||
// Tap the next field to hide the context menu.
|
||||
await tester.tap(find.byType(EditableText).at(1));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||
|
||||
// Right click on the first CupertinoTextField.
|
||||
gesture = await tester.startGesture(
|
||||
tester.getTopLeft(find.byType(CupertinoTextField).first),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The default Cupertino context menu is shown.
|
||||
expect(
|
||||
find.byType(CupertinoAdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
expect(find.byType(CupertinoTextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.macOS:
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbar),
|
||||
findsOneWidget);
|
||||
}
|
||||
|
||||
// Tap the next field to hide the context menu.
|
||||
await tester.tap(find.byType(CupertinoTextField).at(1));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(CupertinoAdaptiveTextSelectionToolbar), findsNothing);
|
||||
|
||||
// Right click on the fixed CupertinoTextField.
|
||||
gesture = await tester.startGesture(
|
||||
tester.getTopLeft(find.byType(CupertinoTextField).at(1)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The default adaptive context menu is shown.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
expect(find.byType(CupertinoTextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.android:
|
||||
expect(find.byType(TextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.fuchsia:
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.macOS:
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbar),
|
||||
findsOneWidget);
|
||||
}
|
||||
|
||||
// Tap the next field to hide the context menu.
|
||||
await tester.tap(find.byType(CupertinoTextField).at(2));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||
|
||||
// Right click on the forced CupertinoTextField.
|
||||
gesture = await tester.startGesture(
|
||||
tester.getTopLeft(find.byType(CupertinoTextField).at(2)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The DesktopTextSelectionToolbar is shown for all platforms.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsNothing);
|
||||
expect(find.byType(CupertinoAdaptiveTextSelectionToolbar), findsNothing);
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
|
||||
|
||||
// Tap the next field to hide the context menu.
|
||||
await tester.tap(find.byType(EditableText).last);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsNothing);
|
||||
|
||||
// Right click on the EditableText.
|
||||
gesture = await tester.startGesture(
|
||||
tester.getTopLeft(find.byType(EditableText).first),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Shows the default context menu.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.iOS:
|
||||
expect(find.byType(CupertinoTextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.android:
|
||||
expect(find.byType(TextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.fuchsia:
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
|
||||
case TargetPlatform.macOS:
|
||||
expect(find.byType(CupertinoDesktopTextSelectionToolbar),
|
||||
findsOneWidget);
|
||||
}
|
||||
},
|
||||
variant: TargetPlatformVariant.all(),
|
||||
// TODO(justinmc): https://github.com/flutter/samples/issues/2086
|
||||
skip: true,
|
||||
);
|
||||
}
|
||||
47
context_menus/test/global_selection_page_test.dart
Normal file
47
context_menus/test/global_selection_page_test.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:context_menus/global_selection_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Gives correct behavior for all values of contextMenuBuilder',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the GlobalSelectionPage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(GlobalSelectionPage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -100.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(GlobalSelectionPage.title));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(AppBar),
|
||||
matching: find.text(GlobalSelectionPage.title),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
// Right click on the plain Text widget.
|
||||
TestGesture gesture = await tester.startGesture(
|
||||
tester.getCenter(find.descendant(
|
||||
of: find.byType(ListView),
|
||||
matching: find.byType(Text),
|
||||
)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The default context menu is shown with a custom button.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('Back'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
47
context_menus/test/image_page_test.dart
Normal file
47
context_menus/test/image_page_test.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'package:context_menus/image_page.dart';
|
||||
import 'package:context_menus/main.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'Gives correct behavior for all values of contextMenuBuilder',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Navigate to the ImagePage example.
|
||||
await tester.dragUntilVisible(
|
||||
find.text(ImagePage.title),
|
||||
find.byType(ListView),
|
||||
const Offset(0.0, -100.0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(ImagePage.title));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(AppBar),
|
||||
matching: find.text(ImagePage.title),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
// Right click on the FlutterLogo.
|
||||
TestGesture gesture = await tester.startGesture(
|
||||
tester.getCenter(find.byType(FlutterLogo)),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
buttons: kSecondaryMouseButton,
|
||||
);
|
||||
await tester.pump();
|
||||
await gesture.up();
|
||||
await gesture.removePointer();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// The default context menu is shown with a custom button.
|
||||
expect(find.byType(AdaptiveTextSelectionToolbar), findsOneWidget);
|
||||
expect(find.text('Save'), findsOneWidget);
|
||||
},
|
||||
variant: TargetPlatformVariant.all(),
|
||||
);
|
||||
}
|
||||
46
context_menus/test/utils.dart
Normal file
46
context_menus/test/utils.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
// Returns the first RenderEditable.
|
||||
RenderEditable findRenderEditable(WidgetTester tester) {
|
||||
final RenderObject root = tester.renderObject(find.byType(EditableText));
|
||||
expect(root, isNotNull);
|
||||
|
||||
late RenderEditable renderEditable;
|
||||
void recursiveFinder(RenderObject child) {
|
||||
if (child is RenderEditable) {
|
||||
renderEditable = child;
|
||||
return;
|
||||
}
|
||||
child.visitChildren(recursiveFinder);
|
||||
}
|
||||
|
||||
root.visitChildren(recursiveFinder);
|
||||
expect(renderEditable, isNotNull);
|
||||
return renderEditable;
|
||||
}
|
||||
|
||||
Offset textOffsetToPosition(WidgetTester tester, int offset) {
|
||||
final RenderEditable renderEditable = findRenderEditable(tester);
|
||||
final List<TextSelectionPoint> endpoints = globalize(
|
||||
renderEditable.getEndpointsForSelection(
|
||||
TextSelection.collapsed(offset: offset),
|
||||
),
|
||||
renderEditable,
|
||||
);
|
||||
expect(endpoints.length, 1);
|
||||
return endpoints[0].point + const Offset(kIsWeb ? 1.0 : 0.0, -2.0);
|
||||
}
|
||||
|
||||
List<TextSelectionPoint> globalize(
|
||||
Iterable<TextSelectionPoint> points, RenderBox box) {
|
||||
return points.map<TextSelectionPoint>((point) {
|
||||
return TextSelectionPoint(
|
||||
box.localToGlobal(point.point),
|
||||
point.direction,
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
Reference in New Issue
Block a user