icon_button_test.dart 14.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
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
  });

  testWidgets('test default icon buttons are constrained', (WidgetTester tester) async {
79
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
80
      wrap(
81
          child: IconButton(
82
            padding: EdgeInsets.zero,
83
            onPressed: mockOnPressedFunction,
84
            icon: const Icon(Icons.ac_unit),
85 86 87 88 89
            iconSize: 80.0,
          ),
      ),
    );

90
    final RenderBox box = tester.renderObject(find.byType(IconButton));
91
    expect(box.size, const Size(80.0, 80.0));
92 93
  });

94
  testWidgets('test default icon buttons can be stretched if specified', (WidgetTester tester) async {
95
    await tester.pumpWidget(
96
      Directionality(
97
        textDirection: TextDirection.ltr,
98 99
        child: Material(
          child: Row(
100 101
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget> [
102
              IconButton(
103 104 105 106 107
                onPressed: mockOnPressedFunction,
                icon: const Icon(Icons.ac_unit),
              ),
            ],
          ),
108 109 110 111
        ),
      ),
    );

112
    final RenderBox box = tester.renderObject(find.byType(IconButton));
113
    expect(box.size, const Size(48.0, 600.0));
114 115 116 117
  });

  testWidgets('test default padding', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
118
      wrap(
119
          child: IconButton(
120
            onPressed: mockOnPressedFunction,
121
            icon: const Icon(Icons.ac_unit),
122 123 124
            iconSize: 80.0,
          ),
      ),
125 126
    );

127
    final RenderBox box = tester.renderObject(find.byType(IconButton));
128
    expect(box.size, const Size(96.0, 96.0));
129 130 131 132
  });

  testWidgets('test tooltip', (WidgetTester tester) async {
    await tester.pumpWidget(
133 134 135 136
      MaterialApp(
        home: Material(
          child: Center(
            child: IconButton(
137
              onPressed: mockOnPressedFunction,
138
              icon: const Icon(Icons.ac_unit),
139 140 141 142 143 144 145 146 147
            ),
          ),
        ),
      ),
    );

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

    // Clear the widget tree.
148
    await tester.pumpWidget(Container(key: UniqueKey()));
149 150

    await tester.pumpWidget(
151 152 153 154
      MaterialApp(
        home: Material(
          child: Center(
            child: IconButton(
155
              onPressed: mockOnPressedFunction,
156
              icon: const Icon(Icons.ac_unit),
157 158 159 160 161 162 163 164 165 166 167 168
              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);
169 170 171 172
  });

  testWidgets('IconButton AppBar size', (WidgetTester tester) async {
    await tester.pumpWidget(
173 174 175
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
176
            actions: <Widget>[
177
              IconButton(
178 179 180 181 182 183
                padding: EdgeInsets.zero,
                onPressed: mockOnPressedFunction,
                icon: const Icon(Icons.ac_unit),
              ),
            ],
          ),
184 185
        ),
      ),
186 187
    );

188 189
    final RenderBox barBox = tester.renderObject(find.byType(AppBar));
    final RenderBox iconBox = tester.renderObject(find.byType(IconButton));
190 191
    expect(iconBox.size.height, equals(barBox.size.height));
  });
192 193 194 195

  // 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 {
196 197
    const Color directSplashColor = Color(0xFF00000F);
    const Color directHighlightColor = Color(0xFF0000F0);
198

Ian Hickson's avatar
Ian Hickson committed
199
    Widget buttonWidget = wrap(
200
        child: IconButton(
201 202 203 204 205 206 207 208
          icon: const Icon(Icons.android),
          splashColor: directSplashColor,
          highlightColor: directHighlightColor,
          onPressed: () { /* enable the button */ },
        ),
    );

    await tester.pumpWidget(
209 210
      Theme(
        data: ThemeData(),
211 212 213 214 215 216 217 218 219 220 221 222 223
        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)
224
        ..circle(color: directHighlightColor),
225 226
    );

227 228
    const Color themeSplashColor1 = Color(0xFF000F00);
    const Color themeHighlightColor1 = Color(0xFF00FF00);
229

Ian Hickson's avatar
Ian Hickson committed
230
    buttonWidget = wrap(
231
        child: IconButton(
232 233 234 235 236 237
          icon: const Icon(Icons.android),
          onPressed: () { /* enable the button */ },
        ),
    );

    await tester.pumpWidget(
238 239
      Theme(
        data: ThemeData(
240 241 242 243 244 245 246 247 248 249 250
          highlightColor: themeHighlightColor1,
          splashColor: themeSplashColor1,
        ),
        child: buttonWidget,
      ),
    );

    expect(
      Material.of(tester.element(find.byType(IconButton))),
      paints
        ..circle(color: themeSplashColor1)
251
        ..circle(color: themeHighlightColor1),
252 253
    );

254 255
    const Color themeSplashColor2 = Color(0xFF002200);
    const Color themeHighlightColor2 = Color(0xFF001100);
256 257

    await tester.pumpWidget(
258 259
      Theme(
        data: ThemeData(
260 261 262 263 264 265 266 267 268 269 270
          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)
271
        ..circle(color: themeHighlightColor2),
272 273 274 275
    );

    await gesture.up();
  });
276

277
  testWidgets('IconButton Semantics (enabled)', (WidgetTester tester) async {
278
    final SemanticsTester semantics = SemanticsTester(tester);
279 280 281

    await tester.pumpWidget(
      wrap(
282
        child: IconButton(
283 284 285 286 287 288
          onPressed: mockOnPressedFunction,
          icon: const Icon(Icons.link, semanticLabel: 'link'),
        ),
      ),
    );

289
    expect(semantics, hasSemantics(TestSemantics.root(
290
      children: <TestSemantics>[
291
        TestSemantics.rootChild(
Dan Field's avatar
Dan Field committed
292
          rect: const Rect.fromLTRB(0.0, 0.0, 48.0, 48.0),
293
          actions: <SemanticsAction>[
294
            SemanticsAction.tap,
295 296 297
          ],
          flags: <SemanticsFlag>[
            SemanticsFlag.hasEnabledState,
298
            SemanticsFlag.isButton,
299 300
            SemanticsFlag.isEnabled,
            SemanticsFlag.isFocusable,
301
          ],
302
          label: 'link',
303
        ),
304
      ],
305 306 307 308
    ), ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });
309 310

  testWidgets('IconButton Semantics (disabled)', (WidgetTester tester) async {
311
    final SemanticsTester semantics = SemanticsTester(tester);
312 313 314 315 316

    await tester.pumpWidget(
      wrap(
        child: const IconButton(
          onPressed: null,
317
          icon: Icon(Icons.link, semanticLabel: 'link'),
318 319 320 321
        ),
      ),
    );

322
    expect(semantics, hasSemantics(TestSemantics.root(
323
        children: <TestSemantics>[
324
          TestSemantics.rootChild(
Dan Field's avatar
Dan Field committed
325
            rect: const Rect.fromLTRB(0.0, 0.0, 48.0, 48.0),
326 327
            flags: <SemanticsFlag>[
              SemanticsFlag.hasEnabledState,
328
              SemanticsFlag.isButton,
329 330
            ],
            label: 'link',
331
          ),
332
        ],
333 334 335 336
    ), ignoreId: true, ignoreTransform: true));

    semantics.dispose();
  });
337 338 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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

  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);
  });
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 465 466 467 468 469

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

  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)));
  });
507
}
Ian Hickson's avatar
Ian Hickson committed
508 509

Widget wrap({ Widget child }) {
510 511 512 513 514 515 516
  return DefaultFocusTraversal(
    policy: ReadingOrderTraversalPolicy(),
    child: Directionality(
      textDirection: TextDirection.ltr,
      child: Material(
        child: Center(child: child),
      ),
Ian Hickson's avatar
Ian Hickson committed
517 518 519
    ),
  );
}