button_theme_test.dart 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2018 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';

void main() {
  test('ButtonThemeData defaults', () {
10
    const ButtonThemeData theme = ButtonThemeData();
11 12 13 14
    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(
15
      borderRadius: BorderRadius.all(Radius.circular(2.0)),
16
    ));
17
    expect(theme.alignedDropdown, false);
18
    expect(theme.layoutBehavior, ButtonBarLayoutBehavior.padded);
19 20 21
  });

  test('ButtonThemeData default overrides', () {
22
    const ButtonThemeData theme = ButtonThemeData(
23 24 25 26
      textTheme: ButtonTextTheme.primary,
      minWidth: 100.0,
      height: 200.0,
      padding: EdgeInsets.zero,
27
      shape: RoundedRectangleBorder(),
28
      alignedDropdown: true,
29 30 31 32 33
    );
    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());
34
    expect(theme.alignedDropdown, true);
35 36 37 38
  });

  testWidgets('ButtonTheme defaults', (WidgetTester tester) async {
    ButtonTextTheme textTheme;
39
    ButtonBarLayoutBehavior layoutBehavior;
40 41 42
    BoxConstraints constraints;
    EdgeInsets padding;
    ShapeBorder shape;
43 44
    bool alignedDropdown;
    ColorScheme colorScheme;
45 46

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

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

86 87 88
  test('ButtonThemeData.copyWith', () {
    ButtonThemeData theme = const ButtonThemeData().copyWith();
    expect(theme.textTheme, ButtonTextTheme.normal);
89
    expect(theme.layoutBehavior, ButtonBarLayoutBehavior.padded);
90 91 92
    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(
93
      borderRadius: BorderRadius.all(Radius.circular(2.0)),
94 95
    ));
    expect(theme.alignedDropdown, false);
96
    expect(theme.colorScheme, null);
97 98 99

    theme = const ButtonThemeData().copyWith(
      textTheme: ButtonTextTheme.primary,
100
      layoutBehavior: ButtonBarLayoutBehavior.constrained,
101 102 103 104 105
      minWidth: 100.0,
      height: 200.0,
      padding: EdgeInsets.zero,
      shape: const StadiumBorder(),
      alignedDropdown: true,
106 107
      colorScheme: const ColorScheme.dark(),
      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
108 109
    );
    expect(theme.textTheme, ButtonTextTheme.primary);
110
    expect(theme.layoutBehavior, ButtonBarLayoutBehavior.constrained);
111 112 113 114
    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);
115
    expect(theme.colorScheme, const ColorScheme.dark());
116 117
  });

118
  testWidgets('Theme buttonTheme defaults', (WidgetTester tester) async {
119
    final ThemeData lightTheme = ThemeData.light();
120 121 122 123 124
    ButtonTextTheme textTheme;
    BoxConstraints constraints;
    EdgeInsets padding;
    ShapeBorder shape;

125
    const Color disabledColor = Color(0xFF00FF00);
126
    await tester.pumpWidget(
127
      Theme(
128
        data: lightTheme.copyWith(
129 130
          disabledColor: disabledColor, // disabled RaisedButton fill color
          buttonTheme: const ButtonThemeData(disabledColor: disabledColor),
131 132 133 134 135 136 137 138
          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,
            ),
          ),
        ),
139
        child: Builder(
140 141 142 143 144 145
          builder: (BuildContext context) {
            final ButtonThemeData theme = ButtonTheme.of(context);
            textTheme = theme.textTheme;
            constraints = theme.constraints;
            padding = theme.padding;
            shape = theme.shape;
146
            return Container(
147 148 149
              alignment: Alignment.topLeft,
              child: const Directionality(
                textDirection: TextDirection.ltr,
150
                child: RaisedButton(
151
                  onPressed: null,
152
                  child: Text('b'), // intrinsic width < minimum width
153 154 155 156 157 158 159 160 161 162 163 164
                ),
              ),
            );
          },
        ),
      ),
    );

    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(
165
      borderRadius: BorderRadius.all(Radius.circular(2.0)),
166 167 168
    ));

    expect(tester.widget<Material>(find.byType(Material)).shape, shape);
169
    expect(tester.widget<Material>(find.byType(Material)).color, disabledColor);
170 171 172 173 174 175 176 177 178 179
    expect(tester.getSize(find.byType(Material)), const Size(88.0, 48.0));
  });

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

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

  testWidgets('ButtonTheme alignedDropdown', (WidgetTester tester) async {
225
    final Key dropdownKey = UniqueKey();
226 227

    Widget buildFrame({ bool alignedDropdown, TextDirection textDirection }) {
228
      return MaterialApp(
229
        builder: (BuildContext context, Widget child) {
230
          return Directionality(
231 232 233 234
            textDirection: textDirection,
            child: child,
          );
        },
235
        home: ButtonTheme(
236
          alignedDropdown: alignedDropdown,
237 238
          child: Material(
            child: Builder(
239
              builder: (BuildContext context) {
240
                return Container(
241
                  alignment: Alignment.center,
242 243
                  child: DropdownButtonHideUnderline(
                    child: Container(
244
                      width: 200.0,
245
                      child: DropdownButton<String>(
246 247 248 249
                        key: dropdownKey,
                        onChanged: (String value) { },
                        value: 'foo',
                        items: const <DropdownMenuItem<String>>[
250
                          DropdownMenuItem<String>(
251
                            value: 'foo',
252
                            child: Text('foo'),
253
                          ),
254
                          DropdownMenuItem<String>(
255
                            value: 'bar',
256
                            child: Text('bar'),
257 258 259 260 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 319 320 321 322 323 324 325 326 327
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ),
      );
    }

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

    // Same test as above execpt RTL
    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)));
  });
328
}