button_style_test.dart 10.4 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 The Flutter 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/rendering.dart';

import 'package:flutter_test/flutter_test.dart';
9

10 11 12 13 14 15 16
void main() {
  test('ButtonStyle copyWith, merge, ==, hashCode basics', () {
    expect(const ButtonStyle(), const ButtonStyle().copyWith());
    expect(const ButtonStyle().merge(const ButtonStyle()), const ButtonStyle());
    expect(const ButtonStyle().hashCode, const ButtonStyle().copyWith().hashCode);
  });

17 18 19 20 21 22
  test('ButtonStyle lerp special cases', () {
    expect(ButtonStyle.lerp(null, null, 0), null);
    const ButtonStyle data = ButtonStyle();
    expect(identical(ButtonStyle.lerp(data, data, 0.5), data), true);
  });

23 24 25 26 27 28
  test('ButtonStyle defaults', () {
    const ButtonStyle style = ButtonStyle();
    expect(style.textStyle, null);
    expect(style.backgroundColor, null);
    expect(style.foregroundColor, null);
    expect(style.overlayColor, null);
29 30
    expect(style.shadowColor, null);
    expect(style.surfaceTintColor, null);
31 32 33
    expect(style.elevation, null);
    expect(style.padding, null);
    expect(style.minimumSize, null);
34
    expect(style.fixedSize, null);
35
    expect(style.maximumSize, null);
36
    expect(style.iconColor, null);
37
    expect(style.iconSize, null);
38 39 40 41 42 43 44 45 46
    expect(style.side, null);
    expect(style.shape, null);
    expect(style.mouseCursor, null);
    expect(style.visualDensity, null);
    expect(style.tapTargetSize, null);
    expect(style.animationDuration, null);
    expect(style.enableFeedback, null);
  });

47
  testWidgets('Default ButtonStyle debugFillProperties', (WidgetTester tester) async {
48 49 50 51 52 53 54 55 56 57 58
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const ButtonStyle().debugFillProperties(builder);

    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString())
      .toList();

    expect(description, <String>[]);
  });

59
  testWidgets('ButtonStyle debugFillProperties', (WidgetTester tester) async {
60
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
61 62 63 64 65 66 67 68 69 70 71 72
    const ButtonStyle(
      textStyle: MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 10.0)),
      backgroundColor: MaterialStatePropertyAll<Color>(Color(0xfffffff1)),
      foregroundColor: MaterialStatePropertyAll<Color>(Color(0xfffffff2)),
      overlayColor: MaterialStatePropertyAll<Color>(Color(0xfffffff3)),
      shadowColor: MaterialStatePropertyAll<Color>(Color(0xfffffff4)),
      surfaceTintColor: MaterialStatePropertyAll<Color>(Color(0xfffffff5)),
      elevation: MaterialStatePropertyAll<double>(1.5),
      padding: MaterialStatePropertyAll<EdgeInsets>(EdgeInsets.all(1.0)),
      minimumSize: MaterialStatePropertyAll<Size>(Size(1.0, 2.0)),
      side: MaterialStatePropertyAll<BorderSide>(BorderSide(width: 4.0, color: Color(0xfffffff6))),
      maximumSize: MaterialStatePropertyAll<Size>(Size(100.0, 200.0)),
73
      iconColor: MaterialStatePropertyAll<Color>(Color(0xfffffff6)),
74
      iconSize: MaterialStatePropertyAll<double>(48.1),
75 76
      shape: MaterialStatePropertyAll<OutlinedBorder>(StadiumBorder()),
      mouseCursor: MaterialStatePropertyAll<MouseCursor>(SystemMouseCursors.forbidden),
77
      tapTargetSize: MaterialTapTargetSize.shrinkWrap,
78
      animationDuration: Duration(seconds: 1),
79 80 81 82 83 84 85 86 87
      enableFeedback: true,
    ).debugFillProperties(builder);

    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString())
      .toList();

    expect(description, <String>[
88 89 90 91 92 93 94 95 96 97
      'textStyle: MaterialStatePropertyAll(TextStyle(inherit: true, size: 10.0))',
      'backgroundColor: MaterialStatePropertyAll(Color(0xfffffff1))',
      'foregroundColor: MaterialStatePropertyAll(Color(0xfffffff2))',
      'overlayColor: MaterialStatePropertyAll(Color(0xfffffff3))',
      'shadowColor: MaterialStatePropertyAll(Color(0xfffffff4))',
      'surfaceTintColor: MaterialStatePropertyAll(Color(0xfffffff5))',
      'elevation: MaterialStatePropertyAll(1.5)',
      'padding: MaterialStatePropertyAll(EdgeInsets.all(1.0))',
      'minimumSize: MaterialStatePropertyAll(Size(1.0, 2.0))',
      'maximumSize: MaterialStatePropertyAll(Size(100.0, 200.0))',
98
      'iconColor: MaterialStatePropertyAll(Color(0xfffffff6))',
99
      'iconSize: MaterialStatePropertyAll(48.1)',
100 101
      'side: MaterialStatePropertyAll(BorderSide(color: Color(0xfffffff6), width: 4.0))',
      'shape: MaterialStatePropertyAll(StadiumBorder(BorderSide(width: 0.0, style: none)))',
102
      'mouseCursor: MaterialStatePropertyAll(SystemMouseCursor(forbidden))',
103 104 105 106 107 108
      'tapTargetSize: shrinkWrap',
      'animationDuration: 0:00:01.000000',
      'enableFeedback: true',
    ]);
  });

109
  testWidgets('ButtonStyle copyWith, merge', (WidgetTester tester) async {
110 111 112 113 114 115 116 117 118 119 120
    const MaterialStateProperty<TextStyle> textStyle = MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 10));
    const MaterialStateProperty<Color> backgroundColor = MaterialStatePropertyAll<Color>(Color(0xfffffff1));
    const MaterialStateProperty<Color> foregroundColor = MaterialStatePropertyAll<Color>(Color(0xfffffff2));
    const MaterialStateProperty<Color> overlayColor = MaterialStatePropertyAll<Color>(Color(0xfffffff3));
    const MaterialStateProperty<Color> shadowColor =  MaterialStatePropertyAll<Color>(Color(0xfffffff4));
    const MaterialStateProperty<Color> surfaceTintColor = MaterialStatePropertyAll<Color>(Color(0xfffffff5));
    const MaterialStateProperty<double> elevation = MaterialStatePropertyAll<double>(1);
    const MaterialStateProperty<EdgeInsets> padding = MaterialStatePropertyAll<EdgeInsets>(EdgeInsets.all(1));
    const MaterialStateProperty<Size> minimumSize = MaterialStatePropertyAll<Size>(Size(1, 2));
    const MaterialStateProperty<Size> fixedSize = MaterialStatePropertyAll<Size>(Size(3, 4));
    const MaterialStateProperty<Size> maximumSize = MaterialStatePropertyAll<Size>(Size(5, 6));
121
    const MaterialStateProperty<Color> iconColor = MaterialStatePropertyAll<Color>(Color(0xfffffff6));
122
    const MaterialStateProperty<double> iconSize = MaterialStatePropertyAll<double>(48.0);
123 124 125
    const MaterialStateProperty<BorderSide> side = MaterialStatePropertyAll<BorderSide>(BorderSide());
    const MaterialStateProperty<OutlinedBorder> shape = MaterialStatePropertyAll<OutlinedBorder>(StadiumBorder());
    const MaterialStateProperty<MouseCursor> mouseCursor = MaterialStatePropertyAll<MouseCursor>(SystemMouseCursors.forbidden);
126 127 128 129 130
    const VisualDensity visualDensity = VisualDensity.compact;
    const MaterialTapTargetSize tapTargetSize = MaterialTapTargetSize.shrinkWrap;
    const Duration animationDuration = Duration(seconds: 1);
    const bool enableFeedback = true;

131
    const ButtonStyle style = ButtonStyle(
132 133 134 135
      textStyle: textStyle,
      backgroundColor: backgroundColor,
      foregroundColor: foregroundColor,
      overlayColor: overlayColor,
136 137
      shadowColor: shadowColor,
      surfaceTintColor: surfaceTintColor,
138 139 140
      elevation: elevation,
      padding: padding,
      minimumSize: minimumSize,
141
      fixedSize: fixedSize,
142
      maximumSize: maximumSize,
143
      iconColor: iconColor,
144
      iconSize: iconSize,
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
      side: side,
      shape: shape,
      mouseCursor: mouseCursor,
      visualDensity: visualDensity,
      tapTargetSize: tapTargetSize,
      animationDuration: animationDuration,
      enableFeedback: enableFeedback,
    );

    expect(
      style,
      const ButtonStyle().copyWith(
        textStyle: textStyle,
        backgroundColor: backgroundColor,
        foregroundColor: foregroundColor,
        overlayColor: overlayColor,
161 162
        shadowColor: shadowColor,
        surfaceTintColor: surfaceTintColor,
163 164 165
        elevation: elevation,
        padding: padding,
        minimumSize: minimumSize,
166
        fixedSize: fixedSize,
167
        maximumSize: maximumSize,
168
        iconColor: iconColor,
169
        iconSize: iconSize,
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        side: side,
        shape: shape,
        mouseCursor: mouseCursor,
        visualDensity: visualDensity,
        tapTargetSize: tapTargetSize,
        animationDuration: animationDuration,
        enableFeedback: enableFeedback,
      ),
    );

    expect(
      style,
      const ButtonStyle().merge(style),
    );

    expect(
      style.copyWith(),
187
      style.merge(const ButtonStyle()),
188 189
    );
  });
190 191 192 193 194 195 196

  test('ButtonStyle.lerp BorderSide', () {
    // This is regression test for https://github.com/flutter/flutter/pull/78051
    expect(ButtonStyle.lerp(null, null, 0), null);
    expect(ButtonStyle.lerp(null, null, 0.5), null);
    expect(ButtonStyle.lerp(null, null, 1), null);

197 198
    const BorderSide blackSide = BorderSide();
    const BorderSide whiteSide = BorderSide(color: Color(0xFFFFFFFF));
199 200
    const BorderSide emptyBlackSide = BorderSide(width: 0, color: Color(0x00000000));

201 202
    const ButtonStyle blackStyle = ButtonStyle(side: MaterialStatePropertyAll<BorderSide>(blackSide));
    const ButtonStyle whiteStyle = ButtonStyle(side: MaterialStatePropertyAll<BorderSide>(whiteSide));
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

    // MaterialState.all<Foo>(value) properties resolve to value
    // for any set of MaterialStates.
    const Set<MaterialState> states = <MaterialState>{ };

    expect(ButtonStyle.lerp(blackStyle, blackStyle, 0)?.side?.resolve(states), blackSide);
    expect(ButtonStyle.lerp(blackStyle, blackStyle, 0.5)?.side?.resolve(states), blackSide);
    expect(ButtonStyle.lerp(blackStyle, blackStyle, 1)?.side?.resolve(states), blackSide);

    expect(ButtonStyle.lerp(blackStyle, null, 0)?.side?.resolve(states), blackSide);
    expect(ButtonStyle.lerp(blackStyle, null, 0.5)?.side?.resolve(states), BorderSide.lerp(blackSide, emptyBlackSide, 0.5));
    expect(ButtonStyle.lerp(blackStyle, null, 1)?.side?.resolve(states), emptyBlackSide);

    expect(ButtonStyle.lerp(null, blackStyle, 0)?.side?.resolve(states), emptyBlackSide);
    expect(ButtonStyle.lerp(null, blackStyle, 0.5)?.side?.resolve(states), BorderSide.lerp(emptyBlackSide, blackSide, 0.5));
    expect(ButtonStyle.lerp(null, blackStyle, 1)?.side?.resolve(states), blackSide);

    expect(ButtonStyle.lerp(blackStyle, whiteStyle, 0)?.side?.resolve(states), blackSide);
    expect(ButtonStyle.lerp(blackStyle, whiteStyle, 0.5)?.side?.resolve(states), BorderSide.lerp(blackSide, whiteSide, 0.5));
    expect(ButtonStyle.lerp(blackStyle, whiteStyle, 1)?.side?.resolve(states), whiteSide);
  });
224
}