text_selection_test.dart 30.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])
8
library;
9

10
import 'package:flutter/material.dart';
11
import 'package:flutter/rendering.dart';
12
import 'package:flutter/services.dart';
13 14
import 'package:flutter_test/flutter_test.dart';

15
import '../widgets/clipboard_utils.dart';
16
import '../widgets/editable_text_utils.dart' show findRenderEditable, globalize, textOffsetToPosition;
17 18

void main() {
19 20 21 22
  TestWidgetsFlutterBinding.ensureInitialized();
  final MockClipboard mockClipboard = MockClipboard();

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

33
  tearDown(() {
34
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
35 36 37 38 39
      SystemChannels.platform,
      null,
    );
  });

40 41
  group('canSelectAll', () {
    Widget createEditableText({
42 43 44
      required Key key,
      String? text,
      TextSelection? selection,
45 46 47
    }) {
      final TextEditingController controller = TextEditingController(text: text)
        ..selection = selection ?? const TextSelection.collapsed(offset: -1);
48 49 50 51
      addTearDown(controller.dispose);
      final FocusNode focusNode = FocusNode();
      addTearDown(focusNode.dispose);

52 53 54 55
      return MaterialApp(
        home: EditableText(
          key: key,
          controller: controller,
56
          focusNode: focusNode,
57 58 59
          style: const TextStyle(),
          cursorColor: Colors.black,
          backgroundCursorColor: Colors.black,
60
        ),
61 62 63
      );
    }

64
    testWidgets('should return false when there is no text', (WidgetTester tester) async {
65 66
      final GlobalKey<EditableTextState> key = GlobalKey();
      await tester.pumpWidget(createEditableText(key: key));
67
      expect(materialTextSelectionControls.canSelectAll(key.currentState!), false);
68 69
    });

70
    testWidgets('should return true when there is text and collapsed selection', (WidgetTester tester) async {
71 72 73 74 75
      final GlobalKey<EditableTextState> key = GlobalKey();
      await tester.pumpWidget(createEditableText(
        key: key,
        text: '123',
      ));
76
      expect(materialTextSelectionControls.canSelectAll(key.currentState!), true);
77 78
    });

79
    testWidgets('should return true when there is text and partial uncollapsed selection', (WidgetTester tester) async {
80 81 82 83 84 85
      final GlobalKey<EditableTextState> key = GlobalKey();
      await tester.pumpWidget(createEditableText(
        key: key,
        text: '123',
        selection: const TextSelection(baseOffset: 1, extentOffset: 2),
      ));
86
      expect(materialTextSelectionControls.canSelectAll(key.currentState!), true);
87 88
    });

89
    testWidgets('should return false when there is text and full selection', (WidgetTester tester) async {
90 91 92 93 94 95
      final GlobalKey<EditableTextState> key = GlobalKey();
      await tester.pumpWidget(createEditableText(
        key: key,
        text: '123',
        selection: const TextSelection(baseOffset: 0, extentOffset: 3),
      ));
96
      expect(materialTextSelectionControls.canSelectAll(key.currentState!), false);
97 98
    });
  });
99

100
  group('Text selection menu overflow (Android)', () {
101
    testWidgets('All menu items show when they fit.', (WidgetTester tester) async {
102
      final TextEditingController controller = TextEditingController(text: 'abc def ghi');
103
      addTearDown(controller.dispose);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Directionality(
            textDirection: TextDirection.ltr,
            child: MediaQuery(
              data: const MediaQueryData(size: Size(800.0, 600.0)),
              child: Center(
                child: Material(
                  child: TextField(
                    controller: controller,
                  ),
                ),
              ),
            ),
          ),
      ));

      // Initially, the menu isn't shown at all.
122 123 124 125
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsNothing);
126 127 128 129 130 131 132 133 134 135 136 137 138 139
      expect(find.byType(IconButton), findsNothing);

      // Tap to place the cursor in the field, then tap the handle to show the
      // selection menu.
      await tester.tap(find.byType(TextField));
      await tester.pumpAndSettle();
      final RenderEditable renderEditable = findRenderEditable(tester);
      final List<TextSelectionPoint> endpoints = globalize(
        renderEditable.getEndpointsForSelection(controller.selection),
        renderEditable,
      );
      expect(endpoints.length, 1);
      final Offset handlePos = endpoints[0].point + const Offset(0.0, 1.0);
      await tester.tapAt(handlePos, pointer: 7);
140
      await tester.pumpAndSettle();
141 142 143 144
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsOneWidget);
145 146 147 148 149 150 151 152 153
      expect(find.byType(IconButton), findsNothing);

      // Long press to select a word and show the full selection menu.
      final Offset textOffset = textOffsetToPosition(tester, 1);
      await tester.longPressAt(textOffset);
      await tester.pump();
      await tester.pump();

      // The full menu is shown without the more button.
154 155 156 157
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsOneWidget);
158
      expect(find.byType(IconButton), findsNothing);
159
    },
160
      skip: isBrowser, // [intended] We do not use Flutter-rendered context menu on the Web.
161 162
      variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
    );
163

164
    testWidgets("When menu items don't fit, an overflow menu is used.", (WidgetTester tester) async {
165
      // Set the screen size to more narrow, so that Select all can't fit.
166 167
      tester.view.physicalSize = const Size(1000, 800);
      addTearDown(tester.view.reset);
168 169

      final TextEditingController controller = TextEditingController(text: 'abc def ghi');
170
      addTearDown(controller.dispose);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: const MediaQueryData(size: Size(800.0, 600.0)),
            child: Center(
              child: Material(
                child: TextField(
                  controller: controller,
                ),
              ),
            ),
          ),
        ),
      ));

      // Initially, the menu isn't shown at all.
189 190 191 192
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsNothing);
193 194 195 196 197 198 199 200
      expect(find.byType(IconButton), findsNothing);

      // Long press to show the menu.
      final Offset textOffset = textOffsetToPosition(tester, 1);
      await tester.longPressAt(textOffset);
      await tester.pumpAndSettle();

      // The last button is missing, and a more button is shown.
201 202 203 204
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsNothing);
205
      expect(find.byType(IconButton), findsOneWidget);
206
      final Offset cutOffset = tester.getTopLeft(find.text('Cut'));
207 208 209 210

      // Tapping the button shows the overflow menu.
      await tester.tap(find.byType(IconButton));
      await tester.pumpAndSettle();
211 212 213 214
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsOneWidget);
215 216 217
      expect(find.byType(IconButton), findsOneWidget);

      // The back button is at the bottom of the overflow menu.
218
      final Offset selectAllOffset = tester.getTopLeft(find.text('Select all'));
219 220 221 222 223 224 225 226 227 228
      final Offset moreOffset = tester.getTopLeft(find.byType(IconButton));
      expect(moreOffset.dy, greaterThan(selectAllOffset.dy));

      // The overflow menu grows upward.
      expect(selectAllOffset.dy, lessThan(cutOffset.dy));

      // Tapping the back button shows the selection menu again.
      expect(find.byType(IconButton), findsOneWidget);
      await tester.tap(find.byType(IconButton));
      await tester.pumpAndSettle();
229 230 231 232
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsNothing);
233
      expect(find.byType(IconButton), findsOneWidget);
234
    },
235
      skip: isBrowser, // [intended] We do not use Flutter-rendered context menu on the Web.
236 237
      variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
    );
238

239
    testWidgets('A smaller menu bumps more items to the overflow menu.', (WidgetTester tester) async {
240
      // Set the screen size so narrow that only Cut and Copy can fit.
241 242
      tester.view.physicalSize = const Size(800, 800);
      addTearDown(tester.view.reset);
243 244

      final TextEditingController controller = TextEditingController(text: 'abc def ghi');
245
      addTearDown(controller.dispose);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: const MediaQueryData(size: Size(800.0, 600.0)),
            child: Center(
              child: Material(
                child: TextField(
                  controller: controller,
                ),
              ),
            ),
          ),
        ),
      ));

      // Initially, the menu isn't shown at all.
264 265 266 267
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsNothing);
268 269 270 271 272
      expect(find.byType(IconButton), findsNothing);

      // Long press to show the menu.
      final Offset textOffset = textOffsetToPosition(tester, 1);
      await tester.longPressAt(textOffset);
273
      await tester.pumpAndSettle();
274 275

      // The last two buttons are missing, and a more button is shown.
276 277 278 279
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsNothing);
280 281 282 283 284 285
      expect(find.byType(IconButton), findsOneWidget);

      // Tapping the button shows the overflow menu, which contains both buttons
      // missing from the main menu, and a back button.
      await tester.tap(find.byType(IconButton));
      await tester.pumpAndSettle();
286 287 288 289
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsOneWidget);
290 291 292 293 294
      expect(find.byType(IconButton), findsOneWidget);

      // Tapping the back button shows the selection menu again.
      await tester.tap(find.byType(IconButton));
      await tester.pumpAndSettle();
295 296 297 298
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsNothing);
299
      expect(find.byType(IconButton), findsOneWidget);
300
    },
301
      skip: isBrowser, // [intended] We do not use Flutter-rendered context menu on the Web.
302 303
      variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
    );
304

305
    testWidgets('When the menu renders below the text, the overflow menu back button is at the top.', (WidgetTester tester) async {
306
      // Set the screen size to more narrow, so that Select all can't fit.
307 308
      tester.view.physicalSize = const Size(1000, 800);
      addTearDown(tester.view.reset);
309 310

      final TextEditingController controller = TextEditingController(text: 'abc def ghi');
311
      addTearDown(controller.dispose);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: const MediaQueryData(size: Size(800.0, 600.0)),
            child: Align(
              alignment: Alignment.topLeft,
              child: Material(
                child: TextField(
                  controller: controller,
                ),
              ),
            ),
          ),
        ),
      ));

      // Initially, the menu isn't shown at all.
331 332 333 334
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsNothing);
335 336 337 338 339
      expect(find.byType(IconButton), findsNothing);

      // Long press to show the menu.
      final Offset textOffset = textOffsetToPosition(tester, 1);
      await tester.longPressAt(textOffset);
340
      await tester.pumpAndSettle();
341 342

      // The last button is missing, and a more button is shown.
343 344 345 346
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsNothing);
347
      expect(find.byType(IconButton), findsOneWidget);
348
      final Offset cutOffset = tester.getTopLeft(find.text('Cut'));
349 350 351 352

      // Tapping the button shows the overflow menu.
      await tester.tap(find.byType(IconButton));
      await tester.pumpAndSettle();
353 354 355 356
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsOneWidget);
357 358 359
      expect(find.byType(IconButton), findsOneWidget);

      // The back button is at the top of the overflow menu.
360
      final Offset selectAllOffset = tester.getTopLeft(find.text('Select all'));
361 362 363 364 365 366 367 368 369
      final Offset moreOffset = tester.getTopLeft(find.byType(IconButton));
      expect(moreOffset.dy, lessThan(selectAllOffset.dy));

      // The overflow menu grows downward.
      expect(selectAllOffset.dy, greaterThan(cutOffset.dy));

      // Tapping the back button shows the selection menu again.
      await tester.tap(find.byType(IconButton));
      await tester.pumpAndSettle();
370 371 372 373
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsNothing);
374
      expect(find.byType(IconButton), findsOneWidget);
375
    },
376
      skip: isBrowser, // [intended] We do not use Flutter-rendered context menu on the Web.
377 378
      variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
    );
379

380
    testWidgets('When the menu items change, the menu is closed and _closedWidth reset.', (WidgetTester tester) async {
381
      // Set the screen size to more narrow, so that Select all can't fit.
382 383
      tester.view.physicalSize = const Size(1000, 800);
      addTearDown(tester.view.reset);
384 385

      final TextEditingController controller = TextEditingController(text: 'abc def ghi');
386
      addTearDown(controller.dispose);
387
      await tester.pumpWidget(MaterialApp(
388
        theme: ThemeData(platform: TargetPlatform.android, useMaterial3: false),
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: const MediaQueryData(size: Size(800.0, 600.0)),
            child: Align(
              alignment: Alignment.topLeft,
              child: Material(
                child: TextField(
                  controller: controller,
                ),
              ),
            ),
          ),
        ),
      ));

      // Initially, the menu isn't shown at all.
406 407 408
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
409
      expect(find.text('Share'), findsNothing);
410
      expect(find.text('Select all'), findsNothing);
411
      expect(find.byType(IconButton), findsNothing); // 'More' button.
412 413 414 415 416 417 418 419 420 421 422 423 424 425

      // Tap to place the cursor and tap again to show the menu without a
      // selection.
      await tester.tapAt(textOffsetToPosition(tester, 0));
      await tester.pumpAndSettle();
      final RenderEditable renderEditable = findRenderEditable(tester);
      final List<TextSelectionPoint> endpoints = globalize(
        renderEditable.getEndpointsForSelection(controller.selection),
        renderEditable,
      );
      expect(endpoints.length, 1);
      final Offset handlePos = endpoints[0].point + const Offset(0.0, 1.0);
      await tester.tapAt(handlePos, pointer: 7);
      await tester.pumpAndSettle();
426 427 428
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsOneWidget);
429
      expect(find.text('Share'), findsNothing);
430
      expect(find.text('Select all'), findsOneWidget);
431 432
      expect(find.byType(IconButton), findsNothing);

433
      // Tap Select all and measure the usual position of Cut, without
434
      // _closedWidth having been used yet.
435
      await tester.tap(find.text('Select all'));
436
      await tester.pumpAndSettle();
437 438 439
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
440
      expect(find.text('Share'), findsNothing);
441
      expect(find.text('Select all'), findsNothing);
442
      expect(find.byType(IconButton), findsOneWidget); // 'More' button.
443
      final Offset cutOffset = tester.getTopLeft(find.text('Cut'));
444 445 446 447

      // Tap to clear the selection.
      await tester.tapAt(textOffsetToPosition(tester, 0));
      await tester.pumpAndSettle();
448 449 450 451
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsNothing);
452
      expect(find.byType(IconButton), findsNothing); // 'More' button.
453 454 455

      // Long press to show the menu.
      await tester.longPressAt(textOffsetToPosition(tester, 1));
456
      await tester.pumpAndSettle();
457

458
      // The last buttons (share and select all) are missing, and a more button is shown.
459 460 461
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
462
      expect(find.text('Share'), findsNothing);
463
      expect(find.text('Select all'), findsNothing);
464
      expect(find.byType(IconButton), findsOneWidget); // 'More' button.
465

466
      // Tapping the more button shows the overflow menu.
467 468
      await tester.tap(find.byType(IconButton));
      await tester.pumpAndSettle();
469 470 471
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
472
      expect(find.text('Share'), findsOneWidget);
473
      expect(find.text('Select all'), findsOneWidget);
474
      expect(find.byType(IconButton), findsOneWidget); // Back button.
475

476
      // Tapping 'Select all' closes the overflow menu.
477
      await tester.tap(find.text('Select all'));
478
      await tester.pumpAndSettle();
479 480 481
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
482
      expect(find.text('Share'), findsNothing);
483
      expect(find.text('Select all'), findsNothing);
484
      expect(find.byType(IconButton), findsOneWidget); // 'More' button.
485
      final Offset newCutOffset = tester.getTopLeft(find.text('Cut'));
486
      expect(newCutOffset, equals(cutOffset));
487
    },
488
      skip: isBrowser, // [intended] We do not use Flutter-rendered context menu on the Web.
489 490
      variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
    );
491 492 493
  });

  group('menu position', () {
494
    testWidgets('When renders below a block of text, menu appears below bottom endpoint', (WidgetTester tester) async {
495
      final TextEditingController controller = TextEditingController(text: 'abc\ndef\nghi\njkl\nmno\npqr');
496
      addTearDown(controller.dispose);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: const MediaQueryData(size: Size(800.0, 600.0)),
            child: Align(
              alignment: Alignment.topLeft,
              child: Material(
                child: TextField(
                  controller: controller,
                ),
              ),
            ),
          ),
        ),
      ));

      // Initially, the menu isn't shown at all.
516 517 518 519
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsNothing);
      expect(find.text('Select all'), findsNothing);
520 521 522 523 524 525 526 527 528 529 530 531 532 533
      expect(find.byType(IconButton), findsNothing);

      // Tap to place the cursor in the field, then tap the handle to show the
      // selection menu.
      await tester.tap(find.byType(TextField));
      await tester.pumpAndSettle();
      RenderEditable renderEditable = findRenderEditable(tester);
      List<TextSelectionPoint> endpoints = globalize(
        renderEditable.getEndpointsForSelection(controller.selection),
        renderEditable,
      );
      expect(endpoints.length, 1);
      final Offset handlePos = endpoints[0].point + const Offset(0.0, 1.0);
      await tester.tapAt(handlePos, pointer: 7);
534
      await tester.pumpAndSettle();
535 536 537 538
      expect(find.text('Cut'), findsNothing);
      expect(find.text('Copy'), findsNothing);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsOneWidget);
539 540 541
      expect(find.byType(IconButton), findsNothing);

      // Tap to select all.
542
      await tester.tap(find.text('Select all'));
543 544
      await tester.pumpAndSettle();

545 546 547 548 549
      // Only Cut, Copy, and Paste are shown.
      expect(find.text('Cut'), findsOneWidget);
      expect(find.text('Copy'), findsOneWidget);
      expect(find.text('Paste'), findsOneWidget);
      expect(find.text('Select all'), findsNothing);
550 551 552 553 554 555 556 557 558 559
      expect(find.byType(IconButton), findsNothing);

      // The menu appears below the bottom handle.
      renderEditable = findRenderEditable(tester);
      endpoints = globalize(
        renderEditable.getEndpointsForSelection(controller.selection),
        renderEditable,
      );
      expect(endpoints.length, 2);
      final Offset bottomHandlePos = endpoints[1].point;
560
      final Offset cutOffset = tester.getTopLeft(find.text('Cut'));
561
      expect(cutOffset.dy, greaterThan(bottomHandlePos.dy));
562
    },
563
      skip: isBrowser, // [intended] We do not use Flutter-rendered context menu on the Web.
564 565
      variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
    );
566

567
    testWidgets(
568 569 570 571
      'When selecting multiple lines over max lines',
      (WidgetTester tester) async {
        final TextEditingController controller =
            TextEditingController(text: 'abc\ndef\nghi\njkl\nmno\npqr');
572
        addTearDown(controller.dispose);
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
        await tester.pumpWidget(MaterialApp(
          theme: ThemeData(platform: TargetPlatform.android),
          home: Directionality(
            textDirection: TextDirection.ltr,
            child: MediaQuery(
              data: const MediaQueryData(size: Size(800.0, 600.0)),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Material(
                  child: TextField(
                    decoration: const InputDecoration(contentPadding: EdgeInsets.all(8.0)),
                    style: const TextStyle(fontSize: 32, height: 1),
                    maxLines: 2,
                    controller: controller,
                  ),
                ),
              ),
            ),
          ),
        ));

        // Initially, the menu isn't shown at all.
        expect(find.text('Cut'), findsNothing);
        expect(find.text('Copy'), findsNothing);
        expect(find.text('Paste'), findsNothing);
        expect(find.text('Select all'), findsNothing);
        expect(find.byType(IconButton), findsNothing);

        // Tap to place the cursor in the field, then tap the handle to show the
        // selection menu.
        await tester.tap(find.byType(TextField));
        await tester.pumpAndSettle();
        final RenderEditable renderEditable = findRenderEditable(tester);
        final List<TextSelectionPoint> endpoints = globalize(
          renderEditable.getEndpointsForSelection(controller.selection),
          renderEditable,
        );
        expect(endpoints.length, 1);
        final Offset handlePos = endpoints[0].point + const Offset(0.0, 1.0);
        await tester.tapAt(handlePos, pointer: 7);
        await tester.pumpAndSettle();
        expect(find.text('Cut'), findsNothing);
        expect(find.text('Copy'), findsNothing);
        expect(find.text('Paste'), findsOneWidget);
        expect(find.text('Select all'), findsOneWidget);
        expect(find.byType(IconButton), findsNothing);

        // Tap to select all.
        await tester.tap(find.text('Select all'));
        await tester.pumpAndSettle();

        // Only Cut, Copy, and Paste are shown.
        expect(find.text('Cut'), findsOneWidget);
        expect(find.text('Copy'), findsOneWidget);
        expect(find.text('Paste'), findsOneWidget);
        expect(find.text('Select all'), findsNothing);
        expect(find.byType(IconButton), findsNothing);

        // The menu appears at the top of the visible selection.
        final Offset selectionOffset = tester
            .getTopLeft(find.byType(TextSelectionToolbarTextButton).first);
        final Offset textFieldOffset =
            tester.getTopLeft(find.byType(TextField));

        // 44.0 + 8.0 - 8.0 = _kToolbarHeight + _kToolbarContentDistance - contentPadding
        expect(selectionOffset.dy + 44.0 + 8.0 - 8.0, equals(textFieldOffset.dy));
      },
      skip: isBrowser, // [intended] the selection menu isn't required by web
      variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }),
    );
643 644
  });

645
  group('material handles', () {
646
    testWidgets('draws transparent handle correctly', (WidgetTester tester) async {
647 648 649
      await tester.pumpWidget(RepaintBoundary(
        child: Theme(
          data: ThemeData(
650 651 652
            textSelectionTheme: const TextSelectionThemeData(
              selectionHandleColor: Color(0x550000AA),
            ),
653 654 655 656 657 658 659 660 661 662 663
          ),
          child: Builder(
            builder: (BuildContext context) {
              return Container(
                color: Colors.white,
                height: 800,
                width: 800,
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 250),
                  child: FittedBox(
                    child: materialTextSelectionControls.buildHandle(
664
                      context, TextSelectionHandleType.right, 10.0,
665 666 667 668 669 670 671 672 673 674 675 676 677 678
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      ));

      await expectLater(
        find.byType(RepaintBoundary),
        matchesGoldenFile('transparent_handle.png'),
      );
    });
679

680
    testWidgets('works with 3 positional parameters', (WidgetTester tester) async {
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
      await tester.pumpWidget(Theme(
        data: ThemeData(
          textSelectionTheme: const TextSelectionThemeData(
            selectionHandleColor: Color(0x550000AA),
          ),
        ),
        child: Builder(
          builder: (BuildContext context) {
            return Container(
              color: Colors.white,
              height: 800,
              width: 800,
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 250),
                child: FittedBox(
                  child: materialTextSelectionControls.buildHandle(
                    context, TextSelectionHandleType.right, 10.0,
                  ),
                ),
              ),
            );
          },
        ),
      ));

      // No expect here as this should simply compile / not throw any
      // exceptions while building. The test will fail if this either does
      // not compile or if the tester catches an exception, which we do
      // not take here.
    });
711
  });
712

713
  testWidgets('Paste only appears when clipboard has contents', (WidgetTester tester) async {
714 715 716
    final TextEditingController controller = TextEditingController(
      text: 'Atwater Peel Sherbrooke Bonaventure',
    );
717
    addTearDown(controller.dispose);
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Column(
            children: <Widget>[
              TextField(
                controller: controller,
              ),
            ],
          ),
        ),
      ),
    );

    // Make sure the clipboard is empty to start.
    await Clipboard.setData(const ClipboardData(text: ''));

735
    // Double tap to select the first word.
736 737 738 739 740 741
    const int index = 4;
    await tester.tapAt(textOffsetToPosition(tester, index));
    await tester.pump(const Duration(milliseconds: 50));
    await tester.tapAt(textOffsetToPosition(tester, index));
    await tester.pumpAndSettle();

742 743
    // No Paste yet, because nothing has been copied.
    expect(find.text('Paste'), findsNothing);
744 745 746
    expect(find.text('Copy'), findsOneWidget);
    expect(find.text('Cut'), findsOneWidget);
    expect(find.text('Select all'), findsOneWidget);
747 748

    // Tap copy to add something to the clipboard and close the menu.
749
    await tester.tapAt(tester.getCenter(find.text('Copy')));
750
    await tester.pumpAndSettle();
751 752 753
    expect(find.text('Copy'), findsNothing);
    expect(find.text('Cut'), findsNothing);
    expect(find.text('Select all'), findsNothing);
754 755 756 757 758 759 760 761

    // Double tap to show the menu again.
    await tester.tapAt(textOffsetToPosition(tester, index));
    await tester.pump(const Duration(milliseconds: 50));
    await tester.tapAt(textOffsetToPosition(tester, index));
    await tester.pumpAndSettle();

    // Paste now shows.
762 763 764 765
    expect(find.text('Copy'), findsOneWidget);
    expect(find.text('Cut'), findsOneWidget);
    expect(find.text('Paste'), findsOneWidget);
    expect(find.text('Select all'), findsOneWidget);
766 767 768 769
  },
    skip: isBrowser, // [intended] we don't supply the cut/copy/paste buttons on the web.
    variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android })
  );
770
}