button_theme_test.dart 14 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
// @dart = 2.8

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

void main() {
  test('ButtonThemeData defaults', () {
14
    const ButtonThemeData theme = ButtonThemeData();
15 16 17 18
    expect(theme.textTheme, ButtonTextTheme.normal);
    expect(theme.constraints, const BoxConstraints(minWidth: 88.0, minHeight: 36.0));
    expect(theme.padding, const EdgeInsets.symmetric(horizontal: 16.0));
    expect(theme.shape, const RoundedRectangleBorder(
19
      borderRadius: BorderRadius.all(Radius.circular(2.0)),
20
    ));
21
    expect(theme.alignedDropdown, false);
22
    expect(theme.layoutBehavior, ButtonBarLayoutBehavior.padded);
23 24 25
  });

  test('ButtonThemeData default overrides', () {
26
    const ButtonThemeData theme = ButtonThemeData(
27 28 29 30
      textTheme: ButtonTextTheme.primary,
      minWidth: 100.0,
      height: 200.0,
      padding: EdgeInsets.zero,
31
      shape: RoundedRectangleBorder(),
32
      alignedDropdown: true,
33 34 35 36 37
    );
    expect(theme.textTheme, ButtonTextTheme.primary);
    expect(theme.constraints, const BoxConstraints(minWidth: 100.0, minHeight: 200.0));
    expect(theme.padding, EdgeInsets.zero);
    expect(theme.shape, const RoundedRectangleBorder());
38
    expect(theme.alignedDropdown, true);
39 40 41 42
  });

  testWidgets('ButtonTheme defaults', (WidgetTester tester) async {
    ButtonTextTheme textTheme;
43
    ButtonBarLayoutBehavior layoutBehavior;
44 45 46
    BoxConstraints constraints;
    EdgeInsets padding;
    ShapeBorder shape;
47 48
    bool alignedDropdown;
    ColorScheme colorScheme;
49 50

    await tester.pumpWidget(
51 52
      ButtonTheme(
        child: Builder(
53 54 55 56
          builder: (BuildContext context) {
            final ButtonThemeData theme = ButtonTheme.of(context);
            textTheme = theme.textTheme;
            constraints = theme.constraints;
57
            padding = theme.padding as EdgeInsets;
58
            shape = theme.shape;
59 60 61
            layoutBehavior = theme.layoutBehavior;
            colorScheme = theme.colorScheme;
            alignedDropdown = theme.alignedDropdown;
62
            return Container(
63
              alignment: Alignment.topLeft,
64
              child: Directionality(
65
                textDirection: TextDirection.ltr,
66
                child: FlatButton(
67 68
                  onPressed: () { },
                  child: const Text('b'), // intrinsic width < minimum width
69 70 71 72 73 74 75 76 77
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(textTheme, ButtonTextTheme.normal);
78
    expect(layoutBehavior, ButtonBarLayoutBehavior.padded);
79 80 81
    expect(constraints, const BoxConstraints(minWidth: 88.0, minHeight: 36.0));
    expect(padding, const EdgeInsets.symmetric(horizontal: 16.0));
    expect(shape, const RoundedRectangleBorder(
82
      borderRadius: BorderRadius.all(Radius.circular(2.0)),
83
    ));
84
    expect(alignedDropdown, false);
85
    expect(colorScheme, ThemeData.light().colorScheme);
86 87 88 89
    expect(tester.widget<Material>(find.byType(Material)).shape, shape);
    expect(tester.getSize(find.byType(Material)), const Size(88.0, 36.0));
  });

90 91 92
  test('ButtonThemeData.copyWith', () {
    ButtonThemeData theme = const ButtonThemeData().copyWith();
    expect(theme.textTheme, ButtonTextTheme.normal);
93
    expect(theme.layoutBehavior, ButtonBarLayoutBehavior.padded);
94 95 96
    expect(theme.constraints, const BoxConstraints(minWidth: 88.0, minHeight: 36.0));
    expect(theme.padding, const EdgeInsets.symmetric(horizontal: 16.0));
    expect(theme.shape, const RoundedRectangleBorder(
97
      borderRadius: BorderRadius.all(Radius.circular(2.0)),
98 99
    ));
    expect(theme.alignedDropdown, false);
100
    expect(theme.colorScheme, null);
101 102 103

    theme = const ButtonThemeData().copyWith(
      textTheme: ButtonTextTheme.primary,
104
      layoutBehavior: ButtonBarLayoutBehavior.constrained,
105 106 107 108 109
      minWidth: 100.0,
      height: 200.0,
      padding: EdgeInsets.zero,
      shape: const StadiumBorder(),
      alignedDropdown: true,
110 111
      colorScheme: const ColorScheme.dark(),
      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
112 113
    );
    expect(theme.textTheme, ButtonTextTheme.primary);
114
    expect(theme.layoutBehavior, ButtonBarLayoutBehavior.constrained);
115 116 117 118
    expect(theme.constraints, const BoxConstraints(minWidth: 100.0, minHeight: 200.0));
    expect(theme.padding, EdgeInsets.zero);
    expect(theme.shape, const StadiumBorder());
    expect(theme.alignedDropdown, true);
119
    expect(theme.colorScheme, const ColorScheme.dark());
120 121
  });

122
  testWidgets('Theme buttonTheme defaults', (WidgetTester tester) async {
123
    final ThemeData lightTheme = ThemeData.light();
124 125 126 127 128
    ButtonTextTheme textTheme;
    BoxConstraints constraints;
    EdgeInsets padding;
    ShapeBorder shape;

129
    const Color disabledColor = Color(0xFF00FF00);
130
    await tester.pumpWidget(
131
      Theme(
132
        data: lightTheme.copyWith(
133 134
          disabledColor: disabledColor, // disabled RaisedButton fill color
          buttonTheme: const ButtonThemeData(disabledColor: disabledColor),
135 136 137 138 139 140 141 142
          textTheme: lightTheme.textTheme.copyWith(
            button: lightTheme.textTheme.button.copyWith(
              // The button's height will match because there's no
              // vertical padding by default
              fontSize: 48.0,
            ),
          ),
        ),
143
        child: Builder(
144 145 146 147
          builder: (BuildContext context) {
            final ButtonThemeData theme = ButtonTheme.of(context);
            textTheme = theme.textTheme;
            constraints = theme.constraints;
148
            padding = theme.padding as EdgeInsets;
149
            shape = theme.shape;
150
            return Container(
151 152 153
              alignment: Alignment.topLeft,
              child: const Directionality(
                textDirection: TextDirection.ltr,
154
                child: RaisedButton(
155
                  onPressed: null,
156
                  child: Text('b'), // intrinsic width < minimum width
157 158 159 160 161 162 163 164 165 166 167 168
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(textTheme, ButtonTextTheme.normal);
    expect(constraints, const BoxConstraints(minWidth: 88.0, minHeight: 36.0));
    expect(padding, const EdgeInsets.symmetric(horizontal: 16.0));
    expect(shape, const RoundedRectangleBorder(
169
      borderRadius: BorderRadius.all(Radius.circular(2.0)),
170 171 172
    ));

    expect(tester.widget<Material>(find.byType(Material)).shape, shape);
173
    expect(tester.widget<Material>(find.byType(Material)).color, disabledColor);
174
    expect(tester.getSize(find.byType(Material)), const Size(88.0, 48.0));
175
  });
176 177 178 179 180 181 182 183

  testWidgets('Theme buttonTheme ButtonTheme overrides', (WidgetTester tester) async {
    ButtonTextTheme textTheme;
    BoxConstraints constraints;
    EdgeInsets padding;
    ShapeBorder shape;

    await tester.pumpWidget(
184 185
      Theme(
        data: ThemeData.light().copyWith(
186 187
          buttonColor: const Color(0xFF00FF00), // enabled RaisedButton fill color
        ),
188
        child: ButtonTheme(
189 190 191 192
          textTheme: ButtonTextTheme.primary,
          minWidth: 100.0,
          height: 200.0,
          padding: EdgeInsets.zero,
193
          buttonColor: const Color(0xFF00FF00), // enabled RaisedButton fill color
194
          shape: const RoundedRectangleBorder(),
195
          child: Builder(
196 197 198 199
            builder: (BuildContext context) {
              final ButtonThemeData theme = ButtonTheme.of(context);
              textTheme = theme.textTheme;
              constraints = theme.constraints;
200
              padding = theme.padding as EdgeInsets;
201
              shape = theme.shape;
202
              return Container(
203
                alignment: Alignment.topLeft,
204
                child: Directionality(
205
                  textDirection: TextDirection.ltr,
206
                  child: RaisedButton(
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
                    onPressed: () { },
                    child: const Text('b'), // intrinsic width < minimum width
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );

    expect(textTheme, ButtonTextTheme.primary);
    expect(constraints, const BoxConstraints(minWidth: 100.0, minHeight: 200.0));
    expect(padding, EdgeInsets.zero);
    expect(shape, const RoundedRectangleBorder());

    expect(tester.widget<Material>(find.byType(Material)).shape, shape);
    expect(tester.widget<Material>(find.byType(Material)).color, const Color(0xFF00FF00));
    expect(tester.getSize(find.byType(Material)), const Size(100.0, 200.0));
  });
227 228

  testWidgets('ButtonTheme alignedDropdown', (WidgetTester tester) async {
229
    final Key dropdownKey = UniqueKey();
230 231

    Widget buildFrame({ bool alignedDropdown, TextDirection textDirection }) {
232
      return MaterialApp(
233
        builder: (BuildContext context, Widget child) {
234
          return Directionality(
235 236 237 238
            textDirection: textDirection,
            child: child,
          );
        },
239
        home: ButtonTheme(
240
          alignedDropdown: alignedDropdown,
241 242
          child: Material(
            child: Builder(
243
              builder: (BuildContext context) {
244
                return Container(
245
                  alignment: Alignment.center,
246 247
                  child: DropdownButtonHideUnderline(
                    child: Container(
248
                      width: 200.0,
249
                      child: DropdownButton<String>(
250 251 252 253
                        key: dropdownKey,
                        onChanged: (String value) { },
                        value: 'foo',
                        items: const <DropdownMenuItem<String>>[
254
                          DropdownMenuItem<String>(
255
                            value: 'foo',
256
                            child: Text('foo'),
257
                          ),
258
                          DropdownMenuItem<String>(
259
                            value: 'bar',
260
                            child: Text('bar'),
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ),
      );
    }

    final Finder button = find.byKey(dropdownKey);
    final Finder menu = find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_DropdownMenu<String>');

    await tester.pumpWidget(
      buildFrame(
        alignedDropdown: false,
        textDirection: TextDirection.ltr,
      ),
    );
    await tester.tap(button);
    await tester.pumpAndSettle();

    // 240 = 200.0 (button width) + _kUnalignedMenuMargin (20.0 left and right)
    expect(tester.getSize(button).width, 200.0);
    expect(tester.getSize(menu).width, 240.0);

    // Dismiss the menu.
    await tester.tapAt(Offset.zero);
    await tester.pumpAndSettle();
    expect(menu, findsNothing);

    await tester.pumpWidget(
      buildFrame(
        alignedDropdown: true,
        textDirection: TextDirection.ltr,
      ),
    );
    await tester.tap(button);
    await tester.pumpAndSettle();

    // Aligneddropdown: true means the button and menu widths match
    expect(tester.getSize(button).width, 200.0);
    expect(tester.getSize(menu).width, 200.0);

    // There are two 'foo' widgets: the selected menu item's label and the drop
    // down button's label. The should both appear at the same location.
    final Finder fooText = find.text('foo');
    expect(fooText, findsNWidgets(2));
    expect(tester.getRect(fooText.at(0)), tester.getRect(fooText.at(1)));

    // Dismiss the menu.
    await tester.tapAt(Offset.zero);
    await tester.pumpAndSettle();
    expect(menu, findsNothing);

319
    // Same test as above except RTL
320 321 322 323 324 325 326 327 328 329 330 331
    await tester.pumpWidget(
      buildFrame(
        alignedDropdown: true,
        textDirection: TextDirection.rtl,
      ),
    );
    await tester.tap(button);
    await tester.pumpAndSettle();

    expect(fooText, findsNWidgets(2));
    expect(tester.getRect(fooText.at(0)), tester.getRect(fooText.at(1)));
  });
332 333 334 335

  testWidgets('button theme with stateful color changes color in states', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode();

336 337 338 339
    const Color pressedColor = Color(0x00000001);
    const Color hoverColor = Color(0x00000002);
    const Color focusedColor = Color(0x00000003);
    const Color defaultColor = Color(0x00000004);
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

    Color getTextColor(Set<MaterialState> states) {
      if (states.contains(MaterialState.pressed)) {
        return pressedColor;
      }
      if (states.contains(MaterialState.hovered)) {
        return hoverColor;
      }
      if (states.contains(MaterialState.focused)) {
        return focusedColor;
      }
      return defaultColor;
    }

    const ColorScheme colorScheme = ColorScheme.light();

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: ButtonTheme(
              colorScheme: colorScheme.copyWith(
                primary: MaterialStateColor.resolveWith(getTextColor),
              ),
              textTheme: ButtonTextTheme.primary,
              child: FlatButton(
                child: const Text('FlatButton'),
                onPressed: () {},
                focusNode: focusNode,
              ),
            ),
          ),
        ),
      ),
    );

    Color textColor() {
      return tester.renderObject<RenderParagraph>(find.text('FlatButton')).text.style.color;
    }

    // Default, not disabled.
    expect(textColor(), equals(defaultColor));

    // Focused.
    focusNode.requestFocus();
    await tester.pumpAndSettle();
    expect(textColor(), focusedColor);

    // Hovered.
    final Offset center = tester.getCenter(find.byType(FlatButton));
    final TestGesture gesture = await tester.createGesture(
      kind: PointerDeviceKind.mouse,
    );
393
    await gesture.addPointer(location: Offset.zero);
394
    addTearDown(gesture.removePointer);
395 396 397 398 399 400 401 402 403 404 405 406
    await gesture.moveTo(center);
    await tester.pumpAndSettle();
    expect(textColor(), hoverColor);

    // Highlighted (pressed).
    await gesture.down(center);
    await tester.pump(); // Start the splash and highlight animations.
    await tester.pump(const Duration(milliseconds: 800)); // Wait for splash and highlight to be well under way.
    expect(textColor(), pressedColor);
  },
    semanticsEnabled: true,
  );
407
}