icon_button_test.dart 19.9 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('Small icons with non-null constraints can be <48dp', (WidgetTester tester) async {
    await tester.pumpWidget(
      wrap(
        child: IconButton(
          iconSize: 10.0,
83
          onPressed: mockOnPressedFunction.handler,
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
          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),
103
          onPressed: mockOnPressedFunction.handler,
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
          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,
124
            onPressed: mockOnPressedFunction.handler,
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            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));
  });

141
  testWidgets('test default icon buttons are constrained', (WidgetTester tester) async {
142
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
143
      wrap(
144
          child: IconButton(
145
            padding: EdgeInsets.zero,
146
            onPressed: mockOnPressedFunction.handler,
147
            icon: const Icon(Icons.ac_unit),
148 149 150 151 152
            iconSize: 80.0,
          ),
      ),
    );

153
    final RenderBox box = tester.renderObject(find.byType(IconButton));
154
    expect(box.size, const Size(80.0, 80.0));
155 156
  });

157
  testWidgets('test default icon buttons can be stretched if specified', (WidgetTester tester) async {
158
    await tester.pumpWidget(
159
      Directionality(
160
        textDirection: TextDirection.ltr,
161 162
        child: Material(
          child: Row(
163 164
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget> [
165
              IconButton(
166
                onPressed: mockOnPressedFunction.handler,
167 168 169 170
                icon: const Icon(Icons.ac_unit),
              ),
            ],
          ),
171 172 173 174
        ),
      ),
    );

175
    final RenderBox box = tester.renderObject(find.byType(IconButton));
176
    expect(box.size, const Size(48.0, 600.0));
177 178 179 180
  });

  testWidgets('test default padding', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
181
      wrap(
182
          child: IconButton(
183
            onPressed: mockOnPressedFunction.handler,
184
            icon: const Icon(Icons.ac_unit),
185 186 187
            iconSize: 80.0,
          ),
      ),
188 189
    );

190
    final RenderBox box = tester.renderObject(find.byType(IconButton));
191
    expect(box.size, const Size(96.0, 96.0));
192 193 194 195
  });

  testWidgets('test tooltip', (WidgetTester tester) async {
    await tester.pumpWidget(
196 197 198 199
      MaterialApp(
        home: Material(
          child: Center(
            child: IconButton(
200
              onPressed: mockOnPressedFunction.handler,
201
              icon: const Icon(Icons.ac_unit),
202 203 204 205 206 207 208 209 210
            ),
          ),
        ),
      ),
    );

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

    // Clear the widget tree.
211
    await tester.pumpWidget(Container(key: UniqueKey()));
212 213

    await tester.pumpWidget(
214 215 216 217
      MaterialApp(
        home: Material(
          child: Center(
            child: IconButton(
218
              onPressed: mockOnPressedFunction.handler,
219
              icon: const Icon(Icons.ac_unit),
220 221 222 223 224 225 226 227 228 229 230 231
              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);
232 233 234 235
  });

  testWidgets('IconButton AppBar size', (WidgetTester tester) async {
    await tester.pumpWidget(
236 237 238
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
239
            actions: <Widget>[
240
              IconButton(
241
                padding: EdgeInsets.zero,
242
                onPressed: mockOnPressedFunction.handler,
243 244 245 246
                icon: const Icon(Icons.ac_unit),
              ),
            ],
          ),
247 248
        ),
      ),
249 250
    );

251 252
    final RenderBox barBox = tester.renderObject(find.byType(AppBar));
    final RenderBox iconBox = tester.renderObject(find.byType(IconButton));
253 254
    expect(iconBox.size.height, equals(barBox.size.height));
  });
255 256 257 258

  // 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 {
259 260
    const Color directSplashColor = Color(0xFF00000F);
    const Color directHighlightColor = Color(0xFF0000F0);
261

Ian Hickson's avatar
Ian Hickson committed
262
    Widget buttonWidget = wrap(
263
        child: IconButton(
264 265 266 267 268 269 270 271
          icon: const Icon(Icons.android),
          splashColor: directSplashColor,
          highlightColor: directHighlightColor,
          onPressed: () { /* enable the button */ },
        ),
    );

    await tester.pumpWidget(
272 273
      Theme(
        data: ThemeData(),
274 275 276 277 278 279 280 281 282 283 284 285 286
        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)
287
        ..circle(color: directHighlightColor),
288 289
    );

290 291
    const Color themeSplashColor1 = Color(0xFF000F00);
    const Color themeHighlightColor1 = Color(0xFF00FF00);
292

Ian Hickson's avatar
Ian Hickson committed
293
    buttonWidget = wrap(
294
        child: IconButton(
295 296 297 298 299 300
          icon: const Icon(Icons.android),
          onPressed: () { /* enable the button */ },
        ),
    );

    await tester.pumpWidget(
301 302
      Theme(
        data: ThemeData(
303 304 305 306 307 308 309 310 311 312 313
          highlightColor: themeHighlightColor1,
          splashColor: themeSplashColor1,
        ),
        child: buttonWidget,
      ),
    );

    expect(
      Material.of(tester.element(find.byType(IconButton))),
      paints
        ..circle(color: themeSplashColor1)
314
        ..circle(color: themeHighlightColor1),
315 316
    );

317 318
    const Color themeSplashColor2 = Color(0xFF002200);
    const Color themeHighlightColor2 = Color(0xFF001100);
319 320

    await tester.pumpWidget(
321 322
      Theme(
        data: ThemeData(
323 324 325 326 327 328 329 330 331 332 333
          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)
334
        ..circle(color: themeHighlightColor2),
335 336 337 338
    );

    await gesture.up();
  });
339

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
  testWidgets('IconButton with explicit splash radius',
      (WidgetTester tester) async {
    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
        ..circle(radius: splashRadius)
    );

    await gesture.up();
  });

371
  testWidgets('IconButton Semantics (enabled)', (WidgetTester tester) async {
372
    final SemanticsTester semantics = SemanticsTester(tester);
373 374 375

    await tester.pumpWidget(
      wrap(
376
        child: IconButton(
377
          onPressed: mockOnPressedFunction.handler,
378 379 380 381 382
          icon: const Icon(Icons.link, semanticLabel: 'link'),
        ),
      ),
    );

383
    expect(semantics, hasSemantics(TestSemantics.root(
384
      children: <TestSemantics>[
385
        TestSemantics.rootChild(
Dan Field's avatar
Dan Field committed
386
          rect: const Rect.fromLTRB(0.0, 0.0, 48.0, 48.0),
387
          actions: <SemanticsAction>[
388
            SemanticsAction.tap,
389 390 391
          ],
          flags: <SemanticsFlag>[
            SemanticsFlag.hasEnabledState,
392
            SemanticsFlag.isButton,
393 394
            SemanticsFlag.isEnabled,
            SemanticsFlag.isFocusable,
395
          ],
396
          label: 'link',
397
        ),
398
      ],
399 400 401 402
    ), ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });
403 404

  testWidgets('IconButton Semantics (disabled)', (WidgetTester tester) async {
405
    final SemanticsTester semantics = SemanticsTester(tester);
406 407 408 409 410

    await tester.pumpWidget(
      wrap(
        child: const IconButton(
          onPressed: null,
411
          icon: Icon(Icons.link, semanticLabel: 'link'),
412 413 414 415
        ),
      ),
    );

416
    expect(semantics, hasSemantics(TestSemantics.root(
417
        children: <TestSemantics>[
418
          TestSemantics.rootChild(
Dan Field's avatar
Dan Field committed
419
            rect: const Rect.fromLTRB(0.0, 0.0, 48.0, 48.0),
420 421
            flags: <SemanticsFlag>[
              SemanticsFlag.hasEnabledState,
422
              SemanticsFlag.isButton,
423 424
            ],
            label: 'link',
425
          ),
426
        ],
427 428 429 430
    ), ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

  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);
  });

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
  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);
  });

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
  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);
  });
536 537

  group('feedback', () {
538
    late FeedbackTester feedback;
539 540 541 542 543 544

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

    tearDown(() {
545
      feedback.dispose();
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 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
    });

    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: () {},
              enableFeedback: true,
              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);
    });
  });
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

  testWidgets('IconButton responds to density changes.', (WidgetTester tester) async {
    const Key key = Key('test');
    Future<void> buildTest(VisualDensity visualDensity) async {
      return await tester.pumpWidget(
        MaterialApp(
          home: Material(
            child: Center(
              child: IconButton(
                visualDensity: visualDensity,
                key: key,
                onPressed: () {},
                icon: const Icon(Icons.play_arrow),
              ),
            ),
          ),
        ),
      );
    }

    await buildTest(const VisualDensity());
    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)));
  });
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

  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();

665
    expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.forbidden);
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

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

682
    expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
683
  });
684
}
Ian Hickson's avatar
Ian Hickson committed
685

686
Widget wrap({ required Widget child }) {
687
  return FocusTraversalGroup(
688 689 690 691 692 693
    policy: ReadingOrderTraversalPolicy(),
    child: Directionality(
      textDirection: TextDirection.ltr,
      child: Material(
        child: Center(child: child),
      ),
Ian Hickson's avatar
Ian Hickson committed
694 695 696
    ),
  );
}