icon_button_test.dart 7.87 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

8 9
import '../rendering/mock_canvas.dart';

10 11 12 13 14 15 16 17
class MockOnPressedFunction implements Function {
  int called = 0;

  void call() {
    called++;
  }
}

18
void main() {
19 20 21 22 23 24 25 26
  MockOnPressedFunction mockOnPressedFunction;

  setUp(() {
    mockOnPressedFunction = new MockOnPressedFunction();
  });

  testWidgets('test default icon buttons are sized up to 48', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
27
      wrap(
28 29
          child: new IconButton(
            onPressed: mockOnPressedFunction,
30
            icon: const Icon(Icons.link),
31 32 33 34
          ),
      ),
    );

35
    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));
36
    expect(iconButton.size, const Size(48.0, 48.0));
37

38 39 40 41 42 43
    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
44
      wrap(
45 46 47
          child: new IconButton(
            iconSize: 10.0,
            onPressed: mockOnPressedFunction,
48
            icon: const Icon(Icons.link),
49 50 51 52
          ),
      ),
    );

53
    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));
54
    expect(iconButton.size, const Size(48.0, 48.0));
55 56 57 58
  });

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

69
    final RenderBox iconButton = tester.renderObject(find.byType(IconButton));
70
    expect(iconButton.size, const Size(70.0, 70.0));
71 72 73
  });

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

85
    final RenderBox box = tester.renderObject(find.byType(IconButton));
86
    expect(box.size, const Size(80.0, 80.0));
87 88 89 90 91 92
  });

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

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

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

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

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

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

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

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

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

185 186
    final RenderBox barBox = tester.renderObject(find.byType(AppBar));
    final RenderBox iconBox = tester.renderObject(find.byType(IconButton));
187 188
    expect(iconBox.size.height, equals(barBox.size.height));
  });
189 190 191 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 {
    final Color directSplashColor = const Color(0xFF00000F);
    final Color directHighlightColor = const Color(0xFF0000F0);

Ian Hickson's avatar
Ian Hickson committed
196
    Widget buttonWidget = wrap(
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        child: new IconButton(
          icon: const Icon(Icons.android),
          splashColor: directSplashColor,
          highlightColor: directHighlightColor,
          onPressed: () { /* enable the button */ },
        ),
    );

    await tester.pumpWidget(
      new Theme(
        data: new ThemeData(),
        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)
        ..circle(color: directHighlightColor)
    );

    final Color themeSplashColor1 = const Color(0xFF000F00);
    final Color themeHighlightColor1 = const Color(0xFF00FF00);

Ian Hickson's avatar
Ian Hickson committed
227
    buttonWidget = wrap(
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        child: new IconButton(
          icon: const Icon(Icons.android),
          onPressed: () { /* enable the button */ },
        ),
    );

    await tester.pumpWidget(
      new Theme(
        data: new ThemeData(
          highlightColor: themeHighlightColor1,
          splashColor: themeSplashColor1,
        ),
        child: buttonWidget,
      ),
    );

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

    final Color themeSplashColor2 = const Color(0xFF002200);
    final Color themeHighlightColor2 = const Color(0xFF001100);

    await tester.pumpWidget(
      new Theme(
        data: new ThemeData(
          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)
        ..circle(color: themeHighlightColor2)
    );

    await gesture.up();
  });
273
}
Ian Hickson's avatar
Ian Hickson committed
274 275 276 277 278 279 280 281 282

Widget wrap({ Widget child }) {
  return new Directionality(
    textDirection: TextDirection.ltr,
    child: new Material(
      child: new Center(child: child),
    ),
  );
}