theme_defaults_test.dart 11.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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';

const ShapeBorder defaultButtonShape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(2.0)));
const EdgeInsets defaultButtonPadding = EdgeInsets.only(left: 16.0, right: 16.0);
const BoxConstraints defaultButtonConstraints = BoxConstraints(minWidth: 88.0, minHeight: 36.0);
const Duration defaultButtonDuration = Duration(milliseconds: 200);

void main() {
  group('RaisedButton', () {
    testWidgets('theme: ThemeData.light(), enabled: true', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: Center(
            child: RaisedButton(
              onPressed: () { }, // button.enabled == true
              child: const Text('button'),
23
            ),
24 25 26 27 28
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
29
      expect(raw.textStyle!.color, const Color(0xdd000000));
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      expect(raw.fillColor, const Color(0xffe0e0e0));
      expect(raw.highlightColor, const Color(0x29000000)); // Was Color(0x66bcbcbc)
      expect(raw.splashColor, const Color(0x1f000000)); // Was Color(0x66c8c8c8)
      expect(raw.elevation, 2.0);
      expect(raw.highlightElevation, 8.0);
      expect(raw.disabledElevation, 0.0);
      expect(raw.constraints, defaultButtonConstraints);
      expect(raw.padding, defaultButtonPadding);
      expect(raw.shape, defaultButtonShape);
      expect(raw.animationDuration, defaultButtonDuration);
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });

    testWidgets('theme: ThemeData.light(), enabled: false', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: const Center(
            child: RaisedButton(
              onPressed: null, // button.enabled == false
              child: Text('button'),
51
            ),
52 53 54 55 56
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
57
      expect(raw.textStyle!.color, const Color(0x61000000));
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      expect(raw.fillColor, const Color(0x61000000));
      // highlightColor, disabled button can't be pressed
      // splashColor, disabled button doesn't splash
      expect(raw.elevation, 2.0);
      expect(raw.highlightElevation, 8.0);
      expect(raw.disabledElevation, 0.0);
      expect(raw.constraints, defaultButtonConstraints);
      expect(raw.padding, defaultButtonPadding);
      expect(raw.shape, defaultButtonShape);
      expect(raw.animationDuration, defaultButtonDuration);
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });
  });

  group('FlatButton', () {
    testWidgets('theme: ThemeData.light(), enabled: true', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: Center(
            child: FlatButton(
              onPressed: () { }, // button.enabled == true
              child: const Text('button'),
81
            ),
82 83 84 85 86
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
87
      expect(raw.textStyle!.color, const Color(0xdd000000));
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      expect(raw.fillColor, null);
      expect(raw.highlightColor, const Color(0x29000000)); // Was Color(0x66bcbcbc)
      expect(raw.splashColor, const Color(0x1f000000)); // Was Color(0x66c8c8c8)
      expect(raw.elevation, 0.0);
      expect(raw.highlightElevation, 0.0);
      expect(raw.disabledElevation, 0.0);
      expect(raw.constraints, defaultButtonConstraints);
      expect(raw.padding, defaultButtonPadding);
      expect(raw.shape, defaultButtonShape);
      expect(raw.animationDuration, defaultButtonDuration);
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });

    testWidgets('theme: ThemeData.light(), enabled: false', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: const Center(
            child: FlatButton(
              onPressed: null, // button.enabled == false
              child: Text('button'),
109
            ),
110 111 112 113 114
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
115
      expect(raw.textStyle!.color, const Color(0x61000000));
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      expect(raw.fillColor, null);
      // highlightColor, disabled button can't be pressed
      // splashColor, disabled button doesn't splash
      expect(raw.elevation, 0.0);
      expect(raw.highlightElevation, 0.0);
      expect(raw.disabledElevation, 0.0);
      expect(raw.constraints, defaultButtonConstraints);
      expect(raw.padding, defaultButtonPadding);
      expect(raw.shape, defaultButtonShape);
      expect(raw.animationDuration, const Duration(milliseconds: 200));
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });
  });

  group('OutlineButton', () {
131
    testWidgets('theme: ThemeData.light(), enabled: true, highlightElevation: 2.0', (WidgetTester tester) async {
132 133 134 135 136 137
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: Center(
            child: OutlineButton(
              onPressed: () { }, // button.enabled == true
138 139 140 141
              // Causes the button to be filled with the theme's canvasColor
              // instead of Colors.transparent before the button material's
              // elevation is animated to 2.0.
              highlightElevation: 2.0,
142
              child: const Text('button'),
143
            ),
144 145 146 147 148
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
149
      expect(raw.textStyle!.color, const Color(0xdd000000));
150
      expect(raw.fillColor, const Color(0x00fafafa));
151 152
      expect(raw.highlightColor, const Color(0x29000000)); // Was Color(0x66bcbcbc)
      expect(raw.splashColor, const Color(0x1f000000)); // Was Color(0x66c8c8c8)
153 154 155 156 157
      expect(raw.elevation, 0.0);
      expect(raw.highlightElevation, 0.0);
      expect(raw.disabledElevation, 0.0);
      expect(raw.constraints, defaultButtonConstraints);
      expect(raw.padding, defaultButtonPadding);
158
      // animationDuration can't be configured by the theme/constructor
159 160 161 162 163 164 165 166 167 168 169
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });

    testWidgets('theme: ThemeData.light(), enabled: true', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: Center(
            child: OutlineButton(
              onPressed: () { }, // button.enabled == true
              child: const Text('button'),
170
            ),
171 172 173 174 175
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
176
      expect(raw.textStyle!.color, const Color(0xdd000000));
177 178 179
      expect(raw.fillColor, Colors.transparent);
      expect(raw.highlightColor, const Color(0x29000000)); // Was Color(0x66bcbcbc)
      expect(raw.splashColor, const Color(0x1f000000)); // Was Color(0x66c8c8c8)
180 181 182 183 184
      expect(raw.elevation, 0.0);
      expect(raw.highlightElevation, 0.0);
      expect(raw.disabledElevation, 0.0);
      expect(raw.constraints, defaultButtonConstraints);
      expect(raw.padding, defaultButtonPadding);
185
      // animationDuration can't be configured by the theme/constructor
186 187 188 189 190 191 192 193 194 195 196
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });

    testWidgets('theme: ThemeData.light(), enabled: false', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: const Center(
            child: OutlineButton(
              onPressed: null, // button.enabled == false
              child: Text('button'),
197
            ),
198 199 200 201 202
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
203
      expect(raw.textStyle!.color, const Color(0x61000000));
204 205 206 207 208 209 210 211
      expect(raw.fillColor, const Color(0x00000000));
      // highlightColor, disabled button can't be pressed
      // splashColor, disabled button doesn't splash
      expect(raw.elevation, 0.0);
      expect(raw.highlightElevation, 0.0);
      expect(raw.disabledElevation, 0.0);
      expect(raw.constraints, defaultButtonConstraints);
      expect(raw.padding, defaultButtonPadding);
212
      // animationDuration can't be configured by the theme/constructor
213 214 215 216
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });
  });

217 218
  group('FloatingActionButton', () {
    const BoxConstraints defaultFABConstraints = BoxConstraints.tightFor(width: 56.0, height: 56.0);
219
    const ShapeBorder defaultFABShape = CircleBorder(side: BorderSide.none);
220 221 222 223 224 225 226 227 228 229
    const EdgeInsets defaultFABPadding = EdgeInsets.zero;

    testWidgets('theme: ThemeData.light(), enabled: true', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: Center(
              child: FloatingActionButton(
                onPressed: () { }, // button.enabled == true
                child: const Icon(Icons.add),
230
              ),
231 232 233 234 235 236
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
      expect(raw.enabled, true);
237
      expect(raw.textStyle!.color, const Color(0xffffffff));
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
      expect(raw.fillColor, const Color(0xff2196f3));
      expect(raw.elevation, 6.0);
      expect(raw.highlightElevation, 12.0);
      expect(raw.disabledElevation, 6.0);
      expect(raw.constraints, defaultFABConstraints);
      expect(raw.padding, defaultFABPadding);
      expect(raw.shape, defaultFABShape);
      expect(raw.animationDuration, defaultButtonDuration);
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });

    testWidgets('theme: ThemeData.light(), enabled: false', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData.light(),
          home: const Center(
              child: FloatingActionButton(
                onPressed: null, // button.enabled == false
                child: Icon(Icons.add),
257
              ),
258 259 260 261 262 263
          ),
        ),
      );

      final RawMaterialButton raw = tester.widget<RawMaterialButton>(find.byType(RawMaterialButton));
      expect(raw.enabled, false);
264
      expect(raw.textStyle!.color, const Color(0xffffffff));
265 266 267 268 269 270 271 272 273 274 275 276 277
      expect(raw.fillColor, const Color(0xff2196f3));
      // highlightColor, disabled button can't be pressed
      // splashColor, disabled button doesn't splash
      expect(raw.elevation, 6.0);
      expect(raw.highlightElevation, 12.0);
      expect(raw.disabledElevation, 6.0);
      expect(raw.constraints, defaultFABConstraints);
      expect(raw.padding, defaultFABPadding);
      expect(raw.shape, defaultFABShape);
      expect(raw.animationDuration, defaultButtonDuration);
      expect(raw.materialTapTargetSize, MaterialTapTargetSize.padded);
    });
  });
278
}