slider_theme_test.dart 13.1 KB
Newer Older
1 2 3 4
// 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.

5 6
import 'dart:ui' show window;

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/painting.dart';

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

void main() {
  testWidgets('Slider theme is built by ThemeData', (WidgetTester tester) async {
    final ThemeData theme = new ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;

    expect(sliderTheme.activeRailColor.value, equals(Colors.red.value));
    expect(sliderTheme.inactiveRailColor.value, equals(Colors.red.withAlpha(0x3d).value));
  });

  testWidgets('Slider uses ThemeData slider theme if present', (WidgetTester tester) async {
    final ThemeData theme = new ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;

    Widget buildSlider(SliderThemeData data) {
      return new Directionality(
        textDirection: TextDirection.ltr,
36 37 38 39 40 41 42 43 44 45 46
        child: new MediaQuery(
          data: new MediaQueryData.fromWindow(window),
          child: new Material(
            child: new Center(
              child: new Theme(
                data: theme,
                child: const Slider(
                  value: 0.5,
                  label: '0.5',
                  onChanged: null,
                ),
47 48 49 50 51 52 53 54 55 56 57
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildSlider(sliderTheme));

    final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));

58
    expect(sliderBox, paints..rect(color: sliderTheme.disabledActiveRailColor)..rect(color: sliderTheme.disabledInactiveRailColor));
59 60
  });

61
  testWidgets('Slider overrides ThemeData theme if SliderTheme present', (WidgetTester tester) async {
62 63 64 65 66 67 68 69 70 71 72 73 74
    final ThemeData theme = new ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;
    final SliderThemeData customTheme = sliderTheme.copyWith(
      activeRailColor: Colors.purple,
      inactiveRailColor: Colors.purple.withAlpha(0x3d),
    );

    Widget buildSlider(SliderThemeData data) {
      return new Directionality(
        textDirection: TextDirection.ltr,
75 76 77 78 79 80 81 82 83 84 85 86 87
        child: new MediaQuery(
          data: new MediaQueryData.fromWindow(window),
          child: new Material(
            child: new Center(
              child: new Theme(
                data: theme,
                child: new SliderTheme(
                  data: customTheme,
                  child: const Slider(
                    value: 0.5,
                    label: '0.5',
                    onChanged: null,
                  ),
88 89 90 91 92 93 94 95 96 97 98 99
                ),
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildSlider(sliderTheme));

    final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));

100
    expect(sliderBox, paints..rect(color: customTheme.disabledActiveRailColor)..rect(color: customTheme.disabledInactiveRailColor));
101 102
  });

103
  testWidgets('SliderThemeData generates correct opacities for materialDefaults', (WidgetTester tester) async {
104 105 106 107
    const Color customColor1 = const Color(0xcafefeed);
    const Color customColor2 = const Color(0xdeadbeef);
    const Color customColor3 = const Color(0xdecaface);

108
    final SliderThemeData sliderTheme = new SliderThemeData.fromPrimaryColors(
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
      primaryColor: customColor1,
      primaryColorDark: customColor2,
      primaryColorLight: customColor3,
    );

    expect(sliderTheme.activeRailColor, equals(customColor1.withAlpha(0xff)));
    expect(sliderTheme.inactiveRailColor, equals(customColor1.withAlpha(0x3d)));
    expect(sliderTheme.disabledActiveRailColor, equals(customColor2.withAlpha(0x52)));
    expect(sliderTheme.disabledInactiveRailColor, equals(customColor2.withAlpha(0x1f)));
    expect(sliderTheme.activeTickMarkColor, equals(customColor3.withAlpha(0x8a)));
    expect(sliderTheme.inactiveTickMarkColor, equals(customColor1.withAlpha(0x8a)));
    expect(sliderTheme.disabledActiveTickMarkColor, equals(customColor3.withAlpha(0x1f)));
    expect(sliderTheme.disabledInactiveTickMarkColor, equals(customColor2.withAlpha(0x1f)));
    expect(sliderTheme.thumbColor, equals(customColor1.withAlpha(0xff)));
    expect(sliderTheme.disabledThumbColor, equals(customColor2.withAlpha(0x52)));
    expect(sliderTheme.overlayColor, equals(customColor1.withAlpha(0x29)));
    expect(sliderTheme.valueIndicatorColor, equals(customColor1.withAlpha(0xff)));
    expect(sliderTheme.thumbShape, equals(const isInstanceOf<RoundSliderThumbShape>()));
127
    expect(sliderTheme.valueIndicatorShape, equals(const isInstanceOf<PaddleSliderValueIndicatorShape>()));
128 129 130 131
    expect(sliderTheme.showValueIndicator, equals(ShowValueIndicator.onlyForDiscrete));
  });

  testWidgets('SliderThemeData lerps correctly', (WidgetTester tester) async {
132
    final SliderThemeData sliderThemeBlack = new SliderThemeData.fromPrimaryColors(
133 134 135 136
      primaryColor: Colors.black,
      primaryColorDark: Colors.black,
      primaryColorLight: Colors.black,
    );
137
    final SliderThemeData sliderThemeWhite = new SliderThemeData.fromPrimaryColors(
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
      primaryColor: Colors.white,
      primaryColorDark: Colors.white,
      primaryColorLight: Colors.white,
    );
    final SliderThemeData lerp = SliderThemeData.lerp(sliderThemeBlack, sliderThemeWhite, 0.5);
    const Color middleGrey = const Color(0xff7f7f7f);
    expect(lerp.activeRailColor, equals(middleGrey.withAlpha(0xff)));
    expect(lerp.inactiveRailColor, equals(middleGrey.withAlpha(0x3d)));
    expect(lerp.disabledActiveRailColor, equals(middleGrey.withAlpha(0x52)));
    expect(lerp.disabledInactiveRailColor, equals(middleGrey.withAlpha(0x1f)));
    expect(lerp.activeTickMarkColor, equals(middleGrey.withAlpha(0x8a)));
    expect(lerp.inactiveTickMarkColor, equals(middleGrey.withAlpha(0x8a)));
    expect(lerp.disabledActiveTickMarkColor, equals(middleGrey.withAlpha(0x1f)));
    expect(lerp.disabledInactiveTickMarkColor, equals(middleGrey.withAlpha(0x1f)));
    expect(lerp.thumbColor, equals(middleGrey.withAlpha(0xff)));
    expect(lerp.disabledThumbColor, equals(middleGrey.withAlpha(0x52)));
    expect(lerp.overlayColor, equals(middleGrey.withAlpha(0x29)));
    expect(lerp.valueIndicatorColor, equals(middleGrey.withAlpha(0xff)));
  });

  testWidgets('Default slider thumb shape draws correctly', (WidgetTester tester) async {
    final ThemeData theme = new ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme.copyWith(thumbColor: Colors.red.shade500);
    double value = 0.45;
    Widget buildApp({
      int divisions,
      bool enabled: true,
    }) {
      final ValueChanged<double> onChanged = enabled ? (double d) => value = d : null;
      return new Directionality(
        textDirection: TextDirection.ltr,
172 173 174 175 176 177 178 179 180 181 182 183
        child: new MediaQuery(
          data: new MediaQueryData.fromWindow(window),
          child: new Material(
            child: new Center(
              child: new SliderTheme(
                data: sliderTheme,
                child: new Slider(
                  value: value,
                  label: '$value',
                  divisions: divisions,
                  onChanged: onChanged,
                ),
184 185 186 187 188 189 190 191 192 193 194 195 196 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 227
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp());

    final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));

    expect(sliderBox, paints..circle(color: sliderTheme.thumbColor, radius: 6.0));

    await tester.pumpWidget(buildApp(enabled: false));
    await tester.pump(const Duration(milliseconds: 500)); // wait for disable animation
    expect(sliderBox, paints..circle(color: sliderTheme.disabledThumbColor, radius: 4.0));

    await tester.pumpWidget(buildApp(divisions: 3));
    await tester.pump(const Duration(milliseconds: 500)); // wait for disable animation
    expect(
        sliderBox,
        paints
          ..circle(color: sliderTheme.activeTickMarkColor)
          ..circle(color: sliderTheme.activeTickMarkColor)
          ..circle(color: sliderTheme.inactiveTickMarkColor)
          ..circle(color: sliderTheme.inactiveTickMarkColor)
          ..circle(color: sliderTheme.thumbColor, radius: 6.0));

    await tester.pumpWidget(buildApp(divisions: 3, enabled: false));
    await tester.pump(const Duration(milliseconds: 500)); // wait for disable animation
    expect(
        sliderBox,
        paints
          ..circle(color: sliderTheme.disabledActiveTickMarkColor)
          ..circle(color: sliderTheme.disabledInactiveTickMarkColor)
          ..circle(color: sliderTheme.disabledInactiveTickMarkColor)
          ..circle(color: sliderTheme.disabledThumbColor, radius: 4.0));
  });

  testWidgets('Default slider value indicator shape draws correctly', (WidgetTester tester) async {
    final ThemeData theme = new ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
228 229
    final SliderThemeData sliderTheme = theme.sliderTheme.copyWith(thumbColor: Colors.red.shade500, showValueIndicator: ShowValueIndicator.always);
    Widget buildApp(String value, {double sliderValue = 0.5}) {
230 231
      return new Directionality(
        textDirection: TextDirection.ltr,
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        child: new MediaQuery(
          data: new MediaQueryData.fromWindow(window),
          child: new Material(
            child: new Row(
              children: <Widget>[
                new Expanded(
                  child: new SliderTheme(
                    data: sliderTheme,
                    child: new Slider(
                      value: sliderValue,
                      label: '$value',
                      divisions: 3,
                      onChanged: (double d) {},
                    ),
                  ),
                ),
              ],
249 250 251 252 253 254 255 256 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
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp('1'));

    final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));

    Offset center = tester.getCenter(find.byType(Slider));
    TestGesture gesture = await tester.startGesture(center);
    await tester.pump();
    // Wait for value indicator animation to finish.
    await tester.pump(const Duration(milliseconds: 500));
    expect(
        sliderBox,
        paints
          ..path(
            color: sliderTheme.valueIndicatorColor,
            includes: <Offset>[
              const Offset(0.0, -40.0),
              const Offset(15.9, -40.0),
              const Offset(-15.9, -40.0),
            ],
            excludes: <Offset>[const Offset(16.1, -40.0), const Offset(-16.1, -40.0)],
          ));

    await gesture.up();

    // Test that it expands with a larger label.
    await tester.pumpWidget(buildApp('1000'));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
    await tester.pump();
    // Wait for value indicator animation to finish.
    await tester.pump(const Duration(milliseconds: 500));
    expect(
        sliderBox,
        paints
          ..path(
            color: sliderTheme.valueIndicatorColor,
            includes: <Offset>[
              const Offset(0.0, -40.0),
              const Offset(35.9, -40.0),
              const Offset(-35.9, -40.0),
            ],
            excludes: <Offset>[const Offset(36.1, -40.0), const Offset(-36.1, -40.0)],
          ));
    await gesture.up();
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 328 329 330 331 332 333 334 335 336 337 338 339 340

    // Test that it avoids the left edge of the screen.
    await tester.pumpWidget(buildApp('1000000', sliderValue: 0.0));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
    await tester.pump();
    // Wait for value indicator animation to finish.
    await tester.pump(const Duration(milliseconds: 500));
    expect(
        sliderBox,
        paints
          ..path(
            color: sliderTheme.valueIndicatorColor,
            includes: <Offset>[
              const Offset(0.0, -40.0),
              const Offset(98.0, -40.0),
              const Offset(-16.0, -40.0),
            ],
            excludes: <Offset>[const Offset(98.1, -40.0), const Offset(-16.1, -40.0)],
          ));
    await gesture.up();

    // Test that it avoids the right edge of the screen.
    await tester.pumpWidget(buildApp('1000000', sliderValue: 1.0));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
    await tester.pump();
    // Wait for value indicator animation to finish.
    await tester.pump(const Duration(milliseconds: 500));
    expect(
        sliderBox,
        paints
          ..path(
            color: sliderTheme.valueIndicatorColor,
            includes: <Offset>[
              const Offset(0.0, -40.0),
              const Offset(16.0, -40.0),
              const Offset(-98.0, -40.0),
            ],
            excludes: <Offset>[const Offset(16.1, -40.0), const Offset(-98.1, -40.0)],
          ));
    await gesture.up();
341 342
  });
}