icon_button_test.dart 16.6 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 16 17 18 19 20 21 22
class MockOnPressedFunction implements Function {
  int called = 0;

  void call() {
    called++;
  }
}

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

  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,
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 52
            iconSize: 10.0,
            onPressed: mockOnPressedFunction,
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,
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 83 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
  testWidgets('Small icons with non-null constraints can be <48dp', (WidgetTester tester) async {
    await tester.pumpWidget(
      wrap(
        child: IconButton(
          iconSize: 10.0,
          onPressed: mockOnPressedFunction,
          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),
          onPressed: mockOnPressedFunction,
          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,
            onPressed: mockOnPressedFunction,
            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,
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 167 168 169 170
                onPressed: mockOnPressedFunction,
                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,
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,
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,
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 242 243 244 245 246
                padding: EdgeInsets.zero,
                onPressed: mockOnPressedFunction,
                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
  testWidgets('IconButton Semantics (enabled)', (WidgetTester tester) async {
341
    final SemanticsTester semantics = SemanticsTester(tester);
342 343 344

    await tester.pumpWidget(
      wrap(
345
        child: IconButton(
346 347 348 349 350 351
          onPressed: mockOnPressedFunction,
          icon: const Icon(Icons.link, semanticLabel: 'link'),
        ),
      ),
    );

352
    expect(semantics, hasSemantics(TestSemantics.root(
353
      children: <TestSemantics>[
354
        TestSemantics.rootChild(
Dan Field's avatar
Dan Field committed
355
          rect: const Rect.fromLTRB(0.0, 0.0, 48.0, 48.0),
356
          actions: <SemanticsAction>[
357
            SemanticsAction.tap,
358 359 360
          ],
          flags: <SemanticsFlag>[
            SemanticsFlag.hasEnabledState,
361
            SemanticsFlag.isButton,
362 363
            SemanticsFlag.isEnabled,
            SemanticsFlag.isFocusable,
364
          ],
365
          label: 'link',
366
        ),
367
      ],
368 369 370 371
    ), ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });
372 373

  testWidgets('IconButton Semantics (disabled)', (WidgetTester tester) async {
374
    final SemanticsTester semantics = SemanticsTester(tester);
375 376 377 378 379

    await tester.pumpWidget(
      wrap(
        child: const IconButton(
          onPressed: null,
380
          icon: Icon(Icons.link, semanticLabel: 'link'),
381 382 383 384
        ),
      ),
    );

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

    semantics.dispose();
  });
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 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 462 463 464

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

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

  group('feedback', () {
    FeedbackTester feedback;

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

    tearDown(() {
      feedback?.dispose();
    });

    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);
    });
  });
533 534 535 536 537 538 539 540 541 542 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

  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)));
  });
570
}
Ian Hickson's avatar
Ian Hickson committed
571 572

Widget wrap({ Widget child }) {
573
  return FocusTraversalGroup(
574 575 576 577 578 579
    policy: ReadingOrderTraversalPolicy(),
    child: Directionality(
      textDirection: TextDirection.ltr,
      child: Material(
        child: Center(child: child),
      ),
Ian Hickson's avatar
Ian Hickson committed
580 581 582
    ),
  );
}