icon_button_test.dart 25.1 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
import 'dart:ui';

7
import 'package:flutter/material.dart';
8
import 'package:flutter/rendering.dart';
9 10
import 'package:flutter_test/flutter_test.dart';

11
import '../rendering/mock_canvas.dart';
12
import '../widgets/semantics_tester.dart';
13
import 'feedback_tester.dart';
14

15
class MockOnPressedFunction {
16 17
  int called = 0;

18
  void handler() {
19 20 21 22
    called++;
  }
}

23
void main() {
24
  late MockOnPressedFunction mockOnPressedFunction;
25 26

  setUp(() {
27
    mockOnPressedFunction = MockOnPressedFunction();
28 29 30 31
  });

  testWidgets('test default icon buttons are sized up to 48', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
32
      wrap(
33
          child: IconButton(
34
            onPressed: mockOnPressedFunction.handler,
35
            icon: const Icon(Icons.link),
36 37 38 39
          ),
      ),
    );

40
    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));
41
    expect(iconButton.size, const Size(48.0, 48.0));
42

43 44 45 46 47 48
    await tester.tap(find.byType(IconButton));
    expect(mockOnPressedFunction.called, 1);
  });

  testWidgets('test small icons are sized up to 48dp', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
49
      wrap(
50
          child: IconButton(
51
            iconSize: 10.0,
52
            onPressed: mockOnPressedFunction.handler,
53
            icon: const Icon(Icons.link),
54 55 56 57
          ),
      ),
    );

58
    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));
59
    expect(iconButton.size, const Size(48.0, 48.0));
60 61 62 63
  });

  testWidgets('test icons can be small when total size is >48dp', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
64
      wrap(
65
          child: IconButton(
66
            iconSize: 10.0,
67
            padding: const EdgeInsets.all(30.0),
68
            onPressed: mockOnPressedFunction.handler,
69
            icon: const Icon(Icons.link),
70 71 72 73
          ),
      ),
    );

74
    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));
75
    expect(iconButton.size, const Size(70.0, 70.0));
76 77
  });

78 79 80 81 82
  testWidgets('when both iconSize and IconTheme.of(context).size are null, size falls back to 24.0', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'Ink Focus');
    await tester.pumpWidget(
      wrap(
          child: IconTheme(
83
            data: const IconThemeData(),
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            child: IconButton(
              focusNode: focusNode,
              onPressed: mockOnPressedFunction.handler,
              icon: const Icon(Icons.link),
            ),
          )
      ),
    );

    final RenderBox icon = tester.renderObject(find.byType(Icon));
    expect(icon.size, const Size(24.0, 24.0));
  });

  testWidgets('when null, iconSize is overridden by closest IconTheme', (WidgetTester tester) async {
    RenderBox icon;

    await tester.pumpWidget(
      wrap(
        child: IconTheme(
          data: const IconThemeData(size: 10),
          child: IconButton(
            onPressed: mockOnPressedFunction.handler,
            icon: const Icon(Icons.link),
          ),
        )
      ),
    );

    icon = tester.renderObject(find.byType(Icon));
    expect(icon.size, const Size(10.0, 10.0));

    await tester.pumpWidget(
      wrap(
          child: Theme(
            data: ThemeData(
              iconTheme: const IconThemeData(size: 10),
            ),
            child: IconButton(
              onPressed: mockOnPressedFunction.handler,
              icon: const Icon(Icons.link),
            ),
          )
      ),
    );

    icon = tester.renderObject(find.byType(Icon));
    expect(icon.size, const Size(10.0, 10.0));

    await tester.pumpWidget(
      wrap(
          child: Theme(
            data: ThemeData(
              iconTheme: const IconThemeData(size: 20),
            ),
            child: IconTheme(
              data: const IconThemeData(size: 10),
              child: IconButton(
                onPressed: mockOnPressedFunction.handler,
                icon: const Icon(Icons.link),
              ),
            ),
          )
      ),
    );

    icon = tester.renderObject(find.byType(Icon));
    expect(icon.size, const Size(10.0, 10.0));

    await tester.pumpWidget(
      wrap(
          child: IconTheme(
            data: const IconThemeData(size: 20),
            child: Theme(
              data: ThemeData(
                iconTheme: const IconThemeData(size: 10),
              ),
              child: IconButton(
                onPressed: mockOnPressedFunction.handler,
                icon: const Icon(Icons.link),
              ),
            ),
          )
      ),
    );

    icon = tester.renderObject(find.byType(Icon));
    expect(icon.size, const Size(10.0, 10.0));
  });

  testWidgets('when non-null, iconSize precedes IconTheme.of(context).size', (WidgetTester tester) async {
    await tester.pumpWidget(
      wrap(
          child: IconTheme(
            data: const IconThemeData(size: 30.0),
            child: IconButton(
              iconSize: 10.0,
              onPressed: mockOnPressedFunction.handler,
              icon: const Icon(Icons.link),
            ),
          )
      ),
    );

    final RenderBox icon = tester.renderObject(find.byType(Icon));
    expect(icon.size, const Size(10.0, 10.0));
  });

191 192 193 194 195
  testWidgets('Small icons with non-null constraints can be <48dp', (WidgetTester tester) async {
    await tester.pumpWidget(
      wrap(
        child: IconButton(
          iconSize: 10.0,
196
          onPressed: mockOnPressedFunction.handler,
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
          icon: const Icon(Icons.link),
          constraints: const BoxConstraints(),
        ),
      ),
    );

    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));

    // By default IconButton has a padding of 8.0 on all sides, so both
    // width and height are 10.0 + 2 * 8.0 = 26.0
    expect(iconButton.size, const Size(26.0, 26.0));
  });

  testWidgets('Small icons with non-null constraints and custom padding can be <48dp', (WidgetTester tester) async {
    await tester.pumpWidget(
      wrap(
        child: IconButton(
          iconSize: 10.0,
          padding: const EdgeInsets.all(3.0),
216
          onPressed: mockOnPressedFunction.handler,
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
          icon: const Icon(Icons.link),
          constraints: const BoxConstraints(),
        ),
      ),
    );

    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));

    // This IconButton has a padding of 3.0 on all sides, so both
    // width and height are 10.0 + 2 * 3.0 = 16.0
    expect(iconButton.size, const Size(16.0, 16.0));
  });

  testWidgets('Small icons comply with VisualDensity requirements', (WidgetTester tester) async {
    await tester.pumpWidget(
      wrap(
        child: Theme(
          data: ThemeData(visualDensity: const VisualDensity(horizontal: 1, vertical: -1)),
          child: IconButton(
            iconSize: 10.0,
237
            onPressed: mockOnPressedFunction.handler,
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
            icon: const Icon(Icons.link),
            constraints: const BoxConstraints(minWidth: 32.0, minHeight: 32.0),
          ),
        ),
      ),
    );

    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));

    // VisualDensity(horizontal: 1, vertical: -1) increases the icon's
    // width by 4 pixels and decreases its height by 4 pixels, giving
    // final width 32.0 + 4.0 = 36.0 and
    // final height 32.0 - 4.0 = 28.0
    expect(iconButton.size, const Size(36.0, 28.0));
  });

254
  testWidgets('test default icon buttons are constrained', (WidgetTester tester) async {
255
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
256
      wrap(
257
          child: IconButton(
258
            padding: EdgeInsets.zero,
259
            onPressed: mockOnPressedFunction.handler,
260
            icon: const Icon(Icons.ac_unit),
261 262 263 264 265
            iconSize: 80.0,
          ),
      ),
    );

266
    final RenderBox box = tester.renderObject(find.byType(IconButton));
267
    expect(box.size, const Size(80.0, 80.0));
268 269
  });

270
  testWidgets('test default icon buttons can be stretched if specified', (WidgetTester tester) async {
271
    await tester.pumpWidget(
272
      Directionality(
273
        textDirection: TextDirection.ltr,
274 275
        child: Material(
          child: Row(
276 277
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget> [
278
              IconButton(
279
                onPressed: mockOnPressedFunction.handler,
280 281 282 283
                icon: const Icon(Icons.ac_unit),
              ),
            ],
          ),
284 285 286 287
        ),
      ),
    );

288
    final RenderBox box = tester.renderObject(find.byType(IconButton));
289
    expect(box.size, const Size(48.0, 600.0));
290 291 292 293
  });

  testWidgets('test default padding', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
294
      wrap(
295
          child: IconButton(
296
            onPressed: mockOnPressedFunction.handler,
297
            icon: const Icon(Icons.ac_unit),
298 299 300
            iconSize: 80.0,
          ),
      ),
301 302
    );

303
    final RenderBox box = tester.renderObject(find.byType(IconButton));
304
    expect(box.size, const Size(96.0, 96.0));
305 306 307 308
  });

  testWidgets('test tooltip', (WidgetTester tester) async {
    await tester.pumpWidget(
309 310 311 312
      MaterialApp(
        home: Material(
          child: Center(
            child: IconButton(
313
              onPressed: mockOnPressedFunction.handler,
314
              icon: const Icon(Icons.ac_unit),
315 316 317 318 319 320 321 322 323
            ),
          ),
        ),
      ),
    );

    expect(find.byType(Tooltip), findsNothing);

    // Clear the widget tree.
324
    await tester.pumpWidget(Container(key: UniqueKey()));
325 326

    await tester.pumpWidget(
327 328 329 330
      MaterialApp(
        home: Material(
          child: Center(
            child: IconButton(
331
              onPressed: mockOnPressedFunction.handler,
332
              icon: const Icon(Icons.ac_unit),
333 334 335 336 337 338 339 340 341 342 343 344
              tooltip: 'Test tooltip',
            ),
          ),
        ),
      ),
    );

    expect(find.byType(Tooltip), findsOneWidget);
    expect(find.byTooltip('Test tooltip'), findsOneWidget);

    await tester.tap(find.byTooltip('Test tooltip'));
    expect(mockOnPressedFunction.called, 1);
345 346 347 348
  });

  testWidgets('IconButton AppBar size', (WidgetTester tester) async {
    await tester.pumpWidget(
349 350 351
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
352
            actions: <Widget>[
353
              IconButton(
354
                padding: EdgeInsets.zero,
355
                onPressed: mockOnPressedFunction.handler,
356 357 358 359
                icon: const Icon(Icons.ac_unit),
              ),
            ],
          ),
360 361
        ),
      ),
362 363
    );

364 365
    final RenderBox barBox = tester.renderObject(find.byType(AppBar));
    final RenderBox iconBox = tester.renderObject(find.byType(IconButton));
366 367
    expect(iconBox.size.height, equals(barBox.size.height));
  });
368 369 370 371

  // This test is very similar to the '...explicit splashColor and highlightColor' test
  // in buttons_test.dart. If you change this one, you may want to also change that one.
  testWidgets('IconButton with explicit splashColor and highlightColor', (WidgetTester tester) async {
372 373
    const Color directSplashColor = Color(0xFF00000F);
    const Color directHighlightColor = Color(0xFF0000F0);
374

Ian Hickson's avatar
Ian Hickson committed
375
    Widget buttonWidget = wrap(
376
        child: IconButton(
377 378 379 380 381 382 383 384
          icon: const Icon(Icons.android),
          splashColor: directSplashColor,
          highlightColor: directHighlightColor,
          onPressed: () { /* enable the button */ },
        ),
    );

    await tester.pumpWidget(
385 386
      Theme(
        data: ThemeData(),
387 388 389 390 391 392 393 394 395 396 397 398 399
        child: buttonWidget,
      ),
    );

    final Offset center = tester.getCenter(find.byType(IconButton));
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pump(); // start gesture
    await tester.pump(const Duration(milliseconds: 200)); // wait for splash to be well under way

    expect(
      Material.of(tester.element(find.byType(IconButton))),
      paints
        ..circle(color: directSplashColor)
400
        ..circle(color: directHighlightColor),
401 402
    );

403 404
    const Color themeSplashColor1 = Color(0xFF000F00);
    const Color themeHighlightColor1 = Color(0xFF00FF00);
405

Ian Hickson's avatar
Ian Hickson committed
406
    buttonWidget = wrap(
407
        child: IconButton(
408 409 410 411 412 413
          icon: const Icon(Icons.android),
          onPressed: () { /* enable the button */ },
        ),
    );

    await tester.pumpWidget(
414 415
      Theme(
        data: ThemeData(
416 417 418 419 420 421 422 423 424 425 426
          highlightColor: themeHighlightColor1,
          splashColor: themeSplashColor1,
        ),
        child: buttonWidget,
      ),
    );

    expect(
      Material.of(tester.element(find.byType(IconButton))),
      paints
        ..circle(color: themeSplashColor1)
427
        ..circle(color: themeHighlightColor1),
428 429
    );

430 431
    const Color themeSplashColor2 = Color(0xFF002200);
    const Color themeHighlightColor2 = Color(0xFF001100);
432 433

    await tester.pumpWidget(
434 435
      Theme(
        data: ThemeData(
436 437 438 439 440 441 442 443 444 445 446
          highlightColor: themeHighlightColor2,
          splashColor: themeSplashColor2,
        ),
        child: buttonWidget, // same widget, so does not get updated because of us
      ),
    );

    expect(
      Material.of(tester.element(find.byType(IconButton))),
      paints
        ..circle(color: themeSplashColor2)
447
        ..circle(color: themeHighlightColor2),
448 449 450 451
    );

    await gesture.up();
  });
452

453
  testWidgets('IconButton with explicit splash radius', (WidgetTester tester) async {
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
    const double splashRadius = 30.0;
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Center(
            child: IconButton(
              icon: const Icon(Icons.android),
              splashRadius: splashRadius,
              onPressed: () { /* enable the button */ },
            ),
          ),
        ),
      ),
    );

    final Offset center = tester.getCenter(find.byType(IconButton));
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pump(); // Start gesture.
    await tester.pump(const Duration(milliseconds: 1000)); // Wait for splash to be well under way.

    expect(
      Material.of(tester.element(find.byType(IconButton))),
      paints
477
        ..circle(radius: splashRadius),
478 479 480 481 482
    );

    await gesture.up();
  });

483
  testWidgets('IconButton Semantics (enabled)', (WidgetTester tester) async {
484
    final SemanticsTester semantics = SemanticsTester(tester);
485 486 487

    await tester.pumpWidget(
      wrap(
488
        child: IconButton(
489
          onPressed: mockOnPressedFunction.handler,
490 491 492 493 494
          icon: const Icon(Icons.link, semanticLabel: 'link'),
        ),
      ),
    );

495
    expect(semantics, hasSemantics(TestSemantics.root(
496
      children: <TestSemantics>[
497
        TestSemantics.rootChild(
Dan Field's avatar
Dan Field committed
498
          rect: const Rect.fromLTRB(0.0, 0.0, 48.0, 48.0),
499
          actions: <SemanticsAction>[
500
            SemanticsAction.tap,
501 502 503
          ],
          flags: <SemanticsFlag>[
            SemanticsFlag.hasEnabledState,
504
            SemanticsFlag.isButton,
505 506
            SemanticsFlag.isEnabled,
            SemanticsFlag.isFocusable,
507
          ],
508
          label: 'link',
509
        ),
510
      ],
511 512 513 514
    ), ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });
515 516

  testWidgets('IconButton Semantics (disabled)', (WidgetTester tester) async {
517
    final SemanticsTester semantics = SemanticsTester(tester);
518 519 520 521 522

    await tester.pumpWidget(
      wrap(
        child: const IconButton(
          onPressed: null,
523
          icon: Icon(Icons.link, semanticLabel: 'link'),
524 525 526 527
        ),
      ),
    );

528
    expect(semantics, hasSemantics(TestSemantics.root(
529
        children: <TestSemantics>[
530
          TestSemantics.rootChild(
Dan Field's avatar
Dan Field committed
531
            rect: const Rect.fromLTRB(0.0, 0.0, 48.0, 48.0),
532 533
            flags: <SemanticsFlag>[
              SemanticsFlag.hasEnabledState,
534
              SemanticsFlag.isButton,
535 536
            ],
            label: 'link',
537
          ),
538
        ],
539 540 541 542
    ), ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

  testWidgets('IconButton loses focus when disabled.', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'IconButton');
    await tester.pumpWidget(
      wrap(
        child: IconButton(
          focusNode: focusNode,
          autofocus: true,
          onPressed: () {},
          icon: const Icon(Icons.link),
        ),
      ),
    );

    await tester.pump();
    expect(focusNode.hasPrimaryFocus, isTrue);

    await tester.pumpWidget(
      wrap(
        child: IconButton(
          focusNode: focusNode,
          autofocus: true,
          onPressed: null,
          icon: const Icon(Icons.link),
        ),
      ),
    );
    await tester.pump();
    expect(focusNode.hasPrimaryFocus, isFalse);
  });

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
  testWidgets('IconButton keeps focus when disabled in directional navigation mode.', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'IconButton');
    await tester.pumpWidget(
      wrap(
        child: MediaQuery(
          data: const MediaQueryData(
            navigationMode: NavigationMode.directional,
          ),
          child: IconButton(
            focusNode: focusNode,
            autofocus: true,
            onPressed: () {},
            icon: const Icon(Icons.link),
          ),
        ),
      ),
    );

    await tester.pump();
    expect(focusNode.hasPrimaryFocus, isTrue);

    await tester.pumpWidget(
      wrap(
        child: MediaQuery(
          data: const MediaQueryData(
            navigationMode: NavigationMode.directional,
          ),
          child: IconButton(
            focusNode: focusNode,
            autofocus: true,
            onPressed: null,
            icon: const Icon(Icons.link),
          ),
        ),
      ),
    );
    await tester.pump();
    expect(focusNode.hasPrimaryFocus, isTrue);
  });

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 643 644 645 646 647
  testWidgets("Disabled IconButton can't be traversed to when disabled.", (WidgetTester tester) async {
    final FocusNode focusNode1 = FocusNode(debugLabel: 'IconButton 1');
    final FocusNode focusNode2 = FocusNode(debugLabel: 'IconButton 2');

    await tester.pumpWidget(
      wrap(
        child: Column(
          children: <Widget>[
            IconButton(
              focusNode: focusNode1,
              autofocus: true,
              onPressed: () {},
              icon: const Icon(Icons.link),
            ),
            IconButton(
              focusNode: focusNode2,
              onPressed: null,
              icon: const Icon(Icons.link),
            ),
          ],
        ),
      ),
    );
    await tester.pump();

    expect(focusNode1.hasPrimaryFocus, isTrue);
    expect(focusNode2.hasPrimaryFocus, isFalse);

    expect(focusNode1.nextFocus(), isTrue);
    await tester.pump();

    expect(focusNode1.hasPrimaryFocus, isTrue);
    expect(focusNode2.hasPrimaryFocus, isFalse);
  });
648 649

  group('feedback', () {
650
    late FeedbackTester feedback;
651 652 653 654 655 656

    setUp(() {
      feedback = FeedbackTester();
    });

    tearDown(() {
657
      feedback.dispose();
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 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 711 712 713 714
    });

    testWidgets('IconButton with disabled feedback', (WidgetTester tester) async {
      await tester.pumpWidget(Material(
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: IconButton(
              onPressed: () {},
              enableFeedback: false,
              icon: const Icon(Icons.link),
            ),
          ),
        ),
      ));
      await tester.tap(find.byType(IconButton), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 0);
      expect(feedback.hapticCount, 0);
    });

    testWidgets('IconButton with enabled feedback', (WidgetTester tester) async {
      await tester.pumpWidget(Material(
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: IconButton(
              onPressed: () {},
              icon: const Icon(Icons.link),
            ),
          ),
        ),
      ));
      await tester.tap(find.byType(IconButton), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 1);
      expect(feedback.hapticCount, 0);
    });

    testWidgets('IconButton with enabled feedback by default', (WidgetTester tester) async {
      await tester.pumpWidget(Material(
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: IconButton(
              onPressed: () {},
              icon: const Icon(Icons.link),
            ),
          ),
        ),
      ));
      await tester.tap(find.byType(IconButton), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 1);
      expect(feedback.hapticCount, 0);
    });
  });
715 716 717 718

  testWidgets('IconButton responds to density changes.', (WidgetTester tester) async {
    const Key key = Key('test');
    Future<void> buildTest(VisualDensity visualDensity) async {
719
      return tester.pumpWidget(
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
        MaterialApp(
          home: Material(
            child: Center(
              child: IconButton(
                visualDensity: visualDensity,
                key: key,
                onPressed: () {},
                icon: const Icon(Icons.play_arrow),
              ),
            ),
          ),
        ),
      );
    }

735
    await buildTest(VisualDensity.standard);
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    final RenderBox box = tester.renderObject(find.byKey(key));
    await tester.pumpAndSettle();
    expect(box.size, equals(const Size(48, 48)));

    await buildTest(const VisualDensity(horizontal: 3.0, vertical: 3.0));
    await tester.pumpAndSettle();
    expect(box.size, equals(const Size(60, 60)));

    await buildTest(const VisualDensity(horizontal: -3.0, vertical: -3.0));
    await tester.pumpAndSettle();
    expect(box.size, equals(const Size(40, 40)));

    await buildTest(const VisualDensity(horizontal: 3.0, vertical: -3.0));
    await tester.pumpAndSettle();
    expect(box.size, equals(const Size(60, 40)));
  });
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

  testWidgets('IconButton.mouseCursor changes cursor on hover', (WidgetTester tester) async {
    // Test argument works
    await tester.pumpWidget(
      Material(
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: IconButton(
              onPressed: () {},
              mouseCursor: SystemMouseCursors.forbidden,
              icon: const Icon(Icons.play_arrow),
            ),
          ),
        ),
      ),
    );

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
    await gesture.addPointer(location: tester.getCenter(find.byType(IconButton)));
    addTearDown(gesture.removePointer);

    await tester.pump();

776
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.forbidden);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

    // Test default is click
    await tester.pumpWidget(
      Material(
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: IconButton(
              onPressed: () {},
              icon: const Icon(Icons.play_arrow),
            ),
          ),
        ),
      ),
    );

793
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
794
  });
795

796
  testWidgets('disabled IconButton has basic mouse cursor', (WidgetTester tester) async {
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
    await tester.pumpWidget(
      const Material(
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: IconButton(
              onPressed: null, // null value indicates IconButton is disabled
              icon: Icon(Icons.play_arrow),
            ),
          ),
        ),
      ),
    );

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
    await gesture.addPointer(location: tester.getCenter(find.byType(IconButton)));
    addTearDown(gesture.removePointer);

    await tester.pump();

817
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
  });

  testWidgets('IconButton.mouseCursor overrides implicit setting of mouse cursor', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Material(
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: IconButton(
              onPressed: null,
              mouseCursor: SystemMouseCursors.none,
              icon: Icon(Icons.play_arrow),
            ),
          ),
        ),
      ),
    );

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
    await gesture.addPointer(location: tester.getCenter(find.byType(IconButton)));
    addTearDown(gesture.removePointer);

    await tester.pump();

842
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.none);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

    await tester.pumpWidget(
      Material(
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: IconButton(
              onPressed: () {},
              mouseCursor: SystemMouseCursors.none,
              icon: const Icon(Icons.play_arrow),
            ),
          ),
        ),
      ),
    );

859
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.none);
860
  });
861
}
Ian Hickson's avatar
Ian Hickson committed
862

863
Widget wrap({ required Widget child }) {
864
  return FocusTraversalGroup(
865 866 867 868 869 870
    policy: ReadingOrderTraversalPolicy(),
    child: Directionality(
      textDirection: TextDirection.ltr,
      child: Material(
        child: Center(child: child),
      ),
Ian Hickson's avatar
Ian Hickson committed
871 872 873
    ),
  );
}