adaptive_text_selection_toolbar_test.dart 15 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
10
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
11 12
import '../widgets/clipboard_utils.dart';
import '../widgets/editable_text_utils.dart';
13
import '../widgets/live_text_utils.dart';
14 15 16 17 18 19

void main() {
  final MockClipboard mockClipboard = MockClipboard();

  setUp(() async {
    TestWidgetsFlutterBinding.ensureInitialized();
20
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
21 22 23 24 25 26 27 28
      SystemChannels.platform,
      mockClipboard.handleMethodCall,
    );
    // Fill the clipboard so that the Paste option is available in the text
    // selection menu.
    await Clipboard.setData(const ClipboardData(text: 'Clipboard data'));
  });

29 30 31 32 33 34 35
  Finder findOverflowNextButton() {
    return find.byWidgetPredicate((Widget widget) =>
    widget is CustomPaint &&
        '${widget.painter?.runtimeType}' == '_RightCupertinoChevronPainter',
    );
  }

36
  testWidgetsWithLeakTracking('Builds the right toolbar on each platform, including web, and shows buttonItems', (WidgetTester tester) async {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    const String buttonText = 'Click me';

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: AdaptiveTextSelectionToolbar.buttonItems(
              anchors: const TextSelectionToolbarAnchors(
                primaryAnchor: Offset.zero,
              ),
              buttonItems: <ContextMenuButtonItem>[
                ContextMenuButtonItem(
                  label: buttonText,
                  onPressed: () {
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );

    expect(find.text(buttonText), findsOneWidget);

    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        expect(find.byType(TextSelectionToolbar), findsOneWidget);
        expect(find.byType(CupertinoTextSelectionToolbar), findsNothing);
        expect(find.byType(DesktopTextSelectionToolbar), findsNothing);
        expect(find.byType(CupertinoDesktopTextSelectionToolbar), findsNothing);
      case TargetPlatform.iOS:
        expect(find.byType(TextSelectionToolbar), findsNothing);
        expect(find.byType(CupertinoTextSelectionToolbar), findsOneWidget);
        expect(find.byType(DesktopTextSelectionToolbar), findsNothing);
        expect(find.byType(CupertinoDesktopTextSelectionToolbar), findsNothing);
      case TargetPlatform.macOS:
        expect(find.byType(TextSelectionToolbar), findsNothing);
        expect(find.byType(CupertinoTextSelectionToolbar), findsNothing);
        expect(find.byType(DesktopTextSelectionToolbar), findsNothing);
        expect(find.byType(CupertinoDesktopTextSelectionToolbar), findsOneWidget);
      case TargetPlatform.fuchsia:
      case TargetPlatform.linux:
      case TargetPlatform.windows:
        expect(find.byType(TextSelectionToolbar), findsNothing);
        expect(find.byType(CupertinoTextSelectionToolbar), findsNothing);
        expect(find.byType(DesktopTextSelectionToolbar), findsOneWidget);
        expect(find.byType(CupertinoDesktopTextSelectionToolbar), findsNothing);
    }
  },
    variant: TargetPlatformVariant.all(),
    skip: isBrowser, // [intended] see https://github.com/flutter/flutter/issues/108382
  );

91
  testWidgetsWithLeakTracking('Can build children directly as well', (WidgetTester tester) async {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    final GlobalKey key = GlobalKey();

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: AdaptiveTextSelectionToolbar(
              anchors: const TextSelectionToolbarAnchors(
                primaryAnchor: Offset.zero,
              ),
              children: <Widget>[
                Container(key: key),
              ],
            ),
          ),
        ),
      ),
    );

    expect(find.byKey(key), findsOneWidget);
  });

114
  testWidgetsWithLeakTracking('Can build from EditableTextState', (WidgetTester tester) async {
115
    final GlobalKey key = GlobalKey();
116 117
    final TextEditingController controller = TextEditingController();
    final FocusNode focusNode = FocusNode();
118 119 120 121 122 123 124
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: SizedBox(
              width: 400,
              child: EditableText(
125
                controller: controller,
126
                backgroundCursorColor: const Color(0xff00ffff),
127
                focusNode: focusNode,
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                style: const TextStyle(),
                cursorColor: const Color(0xff00ffff),
                selectionControls: materialTextSelectionHandleControls,
                contextMenuBuilder: (
                  BuildContext context,
                  EditableTextState editableTextState,
                ) {
                  return AdaptiveTextSelectionToolbar.editableText(
                    key: key,
                    editableTextState: editableTextState,
                  );
                },
              ),
            ),
          ),
        ),
      ),
    );

    await tester.pump(); // Wait for autofocus to take effect.

    expect(find.byKey(key), findsNothing);

    // Long-press to bring up the context menu.
    final Finder textFinder = find.byType(EditableText);
    await tester.longPress(textFinder);
    tester.state<EditableTextState>(textFinder).showToolbar();
    await tester.pumpAndSettle();

    expect(find.byKey(key), findsOneWidget);
    expect(find.text('Copy'), findsNothing);
    expect(find.text('Cut'), findsNothing);
    expect(find.text('Select all'), findsNothing);
    expect(find.text('Paste'), findsOneWidget);

    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
        expect(find.byType(TextSelectionToolbarTextButton), findsOneWidget);
      case TargetPlatform.iOS:
        expect(find.byType(CupertinoTextSelectionToolbarButton), findsOneWidget);
      case TargetPlatform.linux:
      case TargetPlatform.windows:
        expect(find.byType(DesktopTextSelectionToolbarButton), findsOneWidget);
      case TargetPlatform.macOS:
        expect(find.byType(CupertinoDesktopTextSelectionToolbarButton), findsOneWidget);
    }
175 176
    controller.dispose();
    focusNode.dispose();
177 178 179 180 181
  },
    skip: kIsWeb, // [intended] on web the browser handles the context menu.
    variant: TargetPlatformVariant.all(),
  );

182
  testWidgetsWithLeakTracking('Can build for editable text from raw parameters', (WidgetTester tester) async {
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: AdaptiveTextSelectionToolbar.editable(
              key: key,
              anchors: const TextSelectionToolbarAnchors(
                primaryAnchor: Offset.zero,
              ),
              clipboardStatus: ClipboardStatus.pasteable,
              onCopy: () {},
              onCut: () {},
              onPaste: () {},
              onSelectAll: () {},
198
              onLiveTextInput: () {},
199
              onLookUp: () {},
200
              onSearchWeb: () {},
201
              onShare: () {},
202 203 204 205 206 207 208 209 210 211 212 213 214 215
            ),
          ),
        ),
      ),
    );

    expect(find.byKey(key), findsOneWidget);
    expect(find.text('Copy'), findsOneWidget);
    expect(find.text('Cut'), findsOneWidget);
    expect(find.text('Paste'), findsOneWidget);

    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        expect(find.text('Select all'), findsOneWidget);
216
        expect(find.byType(TextSelectionToolbarTextButton), findsNWidgets(6));
217 218
      case TargetPlatform.fuchsia:
        expect(find.text('Select all'), findsOneWidget);
219
        expect(find.byType(TextSelectionToolbarTextButton), findsNWidgets(8));
220 221
      case TargetPlatform.iOS:
        expect(find.text('Select All'), findsOneWidget);
222
        expect(find.byType(CupertinoTextSelectionToolbarButton), findsNWidgets(6));
223 224 225
        await tester.tapAt(tester.getCenter(findOverflowNextButton()));
        await tester.pumpAndSettle();
        expect(findLiveTextButton(), findsOneWidget);
226 227 228
      case TargetPlatform.linux:
      case TargetPlatform.windows:
        expect(find.text('Select all'), findsOneWidget);
229
        expect(find.byType(DesktopTextSelectionToolbarButton), findsNWidgets(8));
230 231
      case TargetPlatform.macOS:
        expect(find.text('Select All'), findsOneWidget);
232
        expect(find.byType(CupertinoDesktopTextSelectionToolbarButton), findsNWidgets(8));
233 234 235 236 237 238 239
    }
  },
    skip: kIsWeb, // [intended] on web the browser handles the context menu.
    variant: TargetPlatformVariant.all(),
  );

  group('buttonItems', () {
240
    testWidgetsWithLeakTracking('getEditableTextButtonItems builds the correct button items per-platform', (WidgetTester tester) async {
241 242 243 244 245 246
      // Fill the clipboard so that the Paste option is available in the text
      // selection menu.
      await Clipboard.setData(const ClipboardData(text: 'Clipboard data'));

      Set<ContextMenuButtonType> buttonTypes = <ContextMenuButtonType>{};
      final TextEditingController controller = TextEditingController();
247
      final FocusNode focusNode = FocusNode();
248 249 250 251 252 253 254 255

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Center(
              child: EditableText(
                controller: controller,
                backgroundCursorColor: Colors.grey,
256
                focusNode: focusNode,
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
                style: const TextStyle(),
                cursorColor: Colors.red,
                selectionControls: materialTextSelectionHandleControls,
                contextMenuBuilder: (
                  BuildContext context,
                  EditableTextState editableTextState,
                ) {
                  buttonTypes = editableTextState.contextMenuButtonItems
                    .map((ContextMenuButtonItem buttonItem) => buttonItem.type)
                    .toSet();
                  return const SizedBox.shrink();
                },
              ),
            ),
          ),
        ),
      );

      final EditableTextState state =
          tester.state<EditableTextState>(find.byType(EditableText));

      // With no text in the field.
      await tester.tapAt(textOffsetToPosition(tester, 0));
      await tester.pump();
      expect(state.showToolbar(), true);
      await tester.pump();

      expect(buttonTypes, isNot(contains(ContextMenuButtonType.cut)));
      expect(buttonTypes, isNot(contains(ContextMenuButtonType.copy)));
      expect(buttonTypes, contains(ContextMenuButtonType.paste));
      expect(buttonTypes, isNot(contains(ContextMenuButtonType.selectAll)));

      // With text but no selection.
      controller.text = 'lorem ipsum';
      await tester.pump();

      expect(buttonTypes, isNot(contains(ContextMenuButtonType.cut)));
      expect(buttonTypes, isNot(contains(ContextMenuButtonType.copy)));
      expect(buttonTypes, contains(ContextMenuButtonType.paste));

      switch (defaultTargetPlatform) {
        case TargetPlatform.android:
        case TargetPlatform.iOS:
        case TargetPlatform.fuchsia:
        case TargetPlatform.linux:
        case TargetPlatform.windows:
          expect(buttonTypes, contains(ContextMenuButtonType.selectAll));
        case TargetPlatform.macOS:
          expect(buttonTypes, isNot(contains(ContextMenuButtonType.selectAll)));
      }

      // With text and selection.
      controller.value = controller.value.copyWith(
        selection: const TextSelection(
          baseOffset: 0,
          extentOffset: 'lorem'.length,
        ),
      );
      await tester.pump();

      expect(buttonTypes, contains(ContextMenuButtonType.cut));
      expect(buttonTypes, contains(ContextMenuButtonType.copy));
      expect(buttonTypes, contains(ContextMenuButtonType.paste));

      switch (defaultTargetPlatform) {
        case TargetPlatform.android:
        case TargetPlatform.fuchsia:
        case TargetPlatform.linux:
        case TargetPlatform.windows:
          expect(buttonTypes, contains(ContextMenuButtonType.selectAll));
        case TargetPlatform.iOS:
        case TargetPlatform.macOS:
          expect(buttonTypes, isNot(contains(ContextMenuButtonType.selectAll)));
      }
331 332 333

      focusNode.dispose();
      controller.dispose();
334 335 336 337 338
    },
      variant: TargetPlatformVariant.all(),
      skip: kIsWeb, // [intended]
    );

339
    testWidgetsWithLeakTracking('getAdaptiveButtons builds the correct button widgets per-platform', (WidgetTester tester) async {
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
      const String buttonText = 'Click me';

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: Center(
              child: Builder(
                builder: (BuildContext context) {
                  final List<ContextMenuButtonItem> buttonItems = <ContextMenuButtonItem>[
                    ContextMenuButtonItem(
                      label: buttonText,
                      onPressed: () {
                      },
                    ),
                  ];
                  return ListView(
                    children: AdaptiveTextSelectionToolbar.getAdaptiveButtons(
                      context,
                      buttonItems,
                    ).toList(),
                  );
                },
              ),
            ),
          ),
        ),
      );

      expect(find.text(buttonText), findsOneWidget);

      switch (defaultTargetPlatform) {
        case TargetPlatform.fuchsia:
        case TargetPlatform.android:
          expect(find.byType(TextSelectionToolbarTextButton), findsOneWidget);
          expect(find.byType(CupertinoTextSelectionToolbarButton), findsNothing);
          expect(find.byType(DesktopTextSelectionToolbarButton), findsNothing);
          expect(find.byType(CupertinoDesktopTextSelectionToolbarButton), findsNothing);
        case TargetPlatform.iOS:
          expect(find.byType(TextSelectionToolbarTextButton), findsNothing);
          expect(find.byType(CupertinoTextSelectionToolbarButton), findsOneWidget);
          expect(find.byType(DesktopTextSelectionToolbarButton), findsNothing);
          expect(find.byType(CupertinoDesktopTextSelectionToolbarButton), findsNothing);
        case TargetPlatform.macOS:
          expect(find.byType(TextSelectionToolbarTextButton), findsNothing);
          expect(find.byType(CupertinoTextSelectionToolbarButton), findsNothing);
          expect(find.byType(DesktopTextSelectionToolbarButton), findsNothing);
          expect(find.byType(CupertinoDesktopTextSelectionToolbarButton), findsOneWidget);
        case TargetPlatform.linux:
        case TargetPlatform.windows:
          expect(find.byType(TextSelectionToolbarTextButton), findsNothing);
          expect(find.byType(CupertinoTextSelectionToolbarButton), findsNothing);
          expect(find.byType(DesktopTextSelectionToolbarButton), findsOneWidget);
          expect(find.byType(CupertinoDesktopTextSelectionToolbarButton), findsNothing);
      }
    },
      variant: TargetPlatformVariant.all(),
    );
  });
}