slider_theme_test.dart 49.5 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
import 'dart:ui' show window;

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

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

void main() {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  test('SliderThemeData copyWith, ==, hashCode basics', () {
    expect(const SliderThemeData(), const SliderThemeData().copyWith());
    expect(const SliderThemeData().hashCode, const SliderThemeData().copyWith().hashCode);
  });

  testWidgets('Default SliderThemeData debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const SliderThemeData().debugFillProperties(builder);

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

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


  testWidgets('SliderThemeData implements debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const SliderThemeData(
      trackHeight: 7.0,
      activeTrackColor: Color(0xFF000001),
      inactiveTrackColor: Color(0xFF000002),
      disabledActiveTrackColor: Color(0xFF000003),
      disabledInactiveTrackColor: Color(0xFF000004),
      activeTickMarkColor: Color(0xFF000005),
      inactiveTickMarkColor: Color(0xFF000006),
      disabledActiveTickMarkColor: Color(0xFF000007),
      disabledInactiveTickMarkColor: Color(0xFF000008),
      thumbColor: Color(0xFF000009),
      overlappingShapeStrokeColor: Color(0xFF000010),
      disabledThumbColor: Color(0xFF000011),
      overlayColor: Color(0xFF000012),
      valueIndicatorColor: Color(0xFF000013),
      overlayShape: RoundSliderOverlayShape(),
      tickMarkShape: RoundSliderTickMarkShape(),
      thumbShape: RoundSliderThumbShape(),
      trackShape: RoundedRectSliderTrackShape(),
      valueIndicatorShape: PaddleSliderValueIndicatorShape(),
      rangeTickMarkShape: RoundRangeSliderTickMarkShape(),
      rangeThumbShape: RoundRangeSliderThumbShape(),
      rangeTrackShape: RoundedRectRangeSliderTrackShape(),
      rangeValueIndicatorShape: PaddleRangeSliderValueIndicatorShape(),
      showValueIndicator: ShowValueIndicator.always,
      valueIndicatorTextStyle: TextStyle(color: Colors.black),
    ).debugFillProperties(builder);

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

    expect(description, <String>[
      'trackHeight: 7.0',
      'activeTrackColor: Color(0xff000001)',
      'inactiveTrackColor: Color(0xff000002)',
      'disabledActiveTrackColor: Color(0xff000003)',
      'disabledInactiveTrackColor: Color(0xff000004)',
      'activeTickMarkColor: Color(0xff000005)',
      'inactiveTickMarkColor: Color(0xff000006)',
      'disabledActiveTickMarkColor: Color(0xff000007)',
      'disabledInactiveTickMarkColor: Color(0xff000008)',
      'thumbColor: Color(0xff000009)',
      'overlappingShapeStrokeColor: Color(0xff000010)',
      'disabledThumbColor: Color(0xff000011)',
      'overlayColor: Color(0xff000012)',
      'valueIndicatorColor: Color(0xff000013)',
83 84 85 86 87 88 89 90 91
      "overlayShape: Instance of 'RoundSliderOverlayShape'",
      "tickMarkShape: Instance of 'RoundSliderTickMarkShape'",
      "thumbShape: Instance of 'RoundSliderThumbShape'",
      "trackShape: Instance of 'RoundedRectSliderTrackShape'",
      "valueIndicatorShape: Instance of 'PaddleSliderValueIndicatorShape'",
      "rangeTickMarkShape: Instance of 'RoundRangeSliderTickMarkShape'",
      "rangeThumbShape: Instance of 'RoundRangeSliderThumbShape'",
      "rangeTrackShape: Instance of 'RoundedRectRangeSliderTrackShape'",
      "rangeValueIndicatorShape: Instance of 'PaddleRangeSliderValueIndicatorShape'",
92
      'showValueIndicator: always',
93
      'valueIndicatorTextStyle: TextStyle(inherit: true, color: Color(0xff000000))',
94 95 96
    ]);
  });

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  testWidgets('Slider V2 uses ThemeData slider theme if present', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5, enabled: false, useV2Slider: true));
    final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));

    expect(
      sliderBox,
      paints
        ..rrect(color: sliderTheme.disabledActiveTrackColor)
        ..rrect(color: sliderTheme.disabledInactiveTrackColor),
    );
  });

115
  testWidgets('Slider uses ThemeData slider theme if present', (WidgetTester tester) async {
116
    final ThemeData theme = ThemeData(
117 118 119 120 121
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;

122
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5, enabled: false));
123
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
124

125
    expect(
126
      material,
127
      paints
128 129
        ..rect(color: sliderTheme.disabledActiveTrackColor)
        ..rect(color: sliderTheme.disabledInactiveTrackColor),
130
    );
131 132
  });

133 134 135 136 137 138 139 140 141 142 143 144
  testWidgets('Slider V2 overrides ThemeData theme if SliderTheme present', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;
    final SliderThemeData customTheme = sliderTheme.copyWith(
      activeTrackColor: Colors.purple,
      inactiveTrackColor: Colors.purple.withAlpha(0x3d),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5, enabled: false, useV2Slider: true));
145
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
146 147

    expect(
148
      material,
149 150 151 152 153 154
      paints
        ..rrect(color: customTheme.disabledActiveTrackColor)
        ..rrect(color: customTheme.disabledInactiveTrackColor),
    );
  });

155
  testWidgets('Slider overrides ThemeData theme if SliderTheme present', (WidgetTester tester) async {
156
    final ThemeData theme = ThemeData(
157 158 159 160 161
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme;
    final SliderThemeData customTheme = sliderTheme.copyWith(
162 163
      activeTrackColor: Colors.purple,
      inactiveTrackColor: Colors.purple.withAlpha(0x3d),
164 165
    );

166
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5, enabled: false));
167
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
168

169
    expect(
170
      material,
171
      paints
172 173
        ..rect(color: customTheme.disabledActiveTrackColor)
        ..rect(color: customTheme.disabledInactiveTrackColor),
174
    );
175 176
  });

177
  testWidgets('SliderThemeData generates correct opacities for fromPrimaryColors', (WidgetTester tester) async {
178 179 180 181
    const Color customColor1 = Color(0xcafefeed);
    const Color customColor2 = Color(0xdeadbeef);
    const Color customColor3 = Color(0xdecaface);
    const Color customColor4 = Color(0xfeedcafe);
182

183
    final SliderThemeData sliderTheme = SliderThemeData.fromPrimaryColors(
184 185 186
      primaryColor: customColor1,
      primaryColorDark: customColor2,
      primaryColorLight: customColor3,
187
      valueIndicatorTextStyle: ThemeData.fallback().textTheme.bodyText1.copyWith(color: customColor4),
188 189
    );

190 191 192 193
    expect(sliderTheme.activeTrackColor, equals(customColor1.withAlpha(0xff)));
    expect(sliderTheme.inactiveTrackColor, equals(customColor1.withAlpha(0x3d)));
    expect(sliderTheme.disabledActiveTrackColor, equals(customColor2.withAlpha(0x52)));
    expect(sliderTheme.disabledInactiveTrackColor, equals(customColor2.withAlpha(0x1f)));
194 195 196 197 198 199
    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)));
200
    expect(sliderTheme.overlayColor, equals(customColor1.withAlpha(0x1f)));
201
    expect(sliderTheme.valueIndicatorColor, equals(customColor1.withAlpha(0xff)));
202
    expect(sliderTheme.valueIndicatorTextStyle.color, equals(customColor4));
203 204
  });

205 206 207 208 209 210 211 212 213 214
  testWidgets('SliderThemeData generates correct shapes for fromPrimaryColors', (WidgetTester tester) async {
    const Color customColor1 = Color(0xcafefeed);
    const Color customColor2 = Color(0xdeadbeef);
    const Color customColor3 = Color(0xdecaface);
    const Color customColor4 = Color(0xfeedcafe);

    final SliderThemeData sliderTheme = SliderThemeData.fromPrimaryColors(
      primaryColor: customColor1,
      primaryColorDark: customColor2,
      primaryColorLight: customColor3,
215
      valueIndicatorTextStyle: ThemeData.fallback().textTheme.bodyText1.copyWith(color: customColor4),
216 217 218 219 220 221 222 223 224 225 226 227 228
    );

    expect(sliderTheme.overlayShape, const RoundSliderOverlayShape());
    expect(sliderTheme.tickMarkShape, const RoundSliderTickMarkShape());
    expect(sliderTheme.thumbShape, const RoundSliderThumbShape());
    expect(sliderTheme.trackShape, const RoundedRectSliderTrackShape());
    expect(sliderTheme.valueIndicatorShape, const PaddleSliderValueIndicatorShape());
    expect(sliderTheme.rangeTickMarkShape, const RoundRangeSliderTickMarkShape());
    expect(sliderTheme.rangeThumbShape, const RoundRangeSliderThumbShape());
    expect(sliderTheme.rangeTrackShape, const RoundedRectRangeSliderTrackShape());
    expect(sliderTheme.rangeValueIndicatorShape, const PaddleRangeSliderValueIndicatorShape());
  });

229
  testWidgets('SliderThemeData lerps correctly', (WidgetTester tester) async {
230
    final SliderThemeData sliderThemeBlack = SliderThemeData.fromPrimaryColors(
231 232 233
      primaryColor: Colors.black,
      primaryColorDark: Colors.black,
      primaryColorLight: Colors.black,
234
      valueIndicatorTextStyle: ThemeData.fallback().textTheme.bodyText1.copyWith(color: Colors.black),
235
    ).copyWith(trackHeight: 2.0);
236
    final SliderThemeData sliderThemeWhite = SliderThemeData.fromPrimaryColors(
237 238 239
      primaryColor: Colors.white,
      primaryColorDark: Colors.white,
      primaryColorLight: Colors.white,
240
      valueIndicatorTextStyle: ThemeData.fallback().textTheme.bodyText1.copyWith(color: Colors.white),
241
    ).copyWith(trackHeight: 6.0);
242
    final SliderThemeData lerp = SliderThemeData.lerp(sliderThemeBlack, sliderThemeWhite, 0.5);
243
    const Color middleGrey = Color(0xff7f7f7f);
244 245

    expect(lerp.trackHeight, equals(4.0));
246 247 248 249
    expect(lerp.activeTrackColor, equals(middleGrey.withAlpha(0xff)));
    expect(lerp.inactiveTrackColor, equals(middleGrey.withAlpha(0x3d)));
    expect(lerp.disabledActiveTrackColor, equals(middleGrey.withAlpha(0x52)));
    expect(lerp.disabledInactiveTrackColor, equals(middleGrey.withAlpha(0x1f)));
250 251 252 253 254 255
    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)));
256
    expect(lerp.overlayColor, equals(middleGrey.withAlpha(0x1f)));
257
    expect(lerp.valueIndicatorColor, equals(middleGrey.withAlpha(0xff)));
258
    expect(lerp.valueIndicatorTextStyle.color, equals(middleGrey.withAlpha(0xff)));
259 260
  });

261 262 263 264 265 266 267 268
  testWidgets('Slider V2 track draws correctly', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme.copyWith(thumbColor: Colors.red.shade500);

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25, useV2Slider: true));
269
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
270 271 272 273 274 275 276

    const Radius radius = Radius.circular(2);
    const Radius activatedRadius = Radius.circular(3);

    // The enabled slider thumb has track segments that extend to and from
    // the center of the thumb.
    expect(
277
      material,
278 279 280 281 282 283 284 285 286 287
      paints
        ..rrect(rrect: RRect.fromLTRBAndCorners(24.0, 297.0, 212.0, 303.0, topLeft: activatedRadius, bottomLeft: activatedRadius), color: sliderTheme.activeTrackColor)
        ..rrect(rrect: RRect.fromLTRBAndCorners(212.0, 298.0, 776.0, 302.0, topRight: radius, bottomRight: radius), color: sliderTheme.inactiveTrackColor),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25, enabled: false, useV2Slider: true));
    await tester.pumpAndSettle(); // wait for disable animation

    // The disabled slider thumb is the same size as the enabled thumb.
    expect(
288
      material,
289 290 291 292 293 294
      paints
        ..rrect(rrect: RRect.fromLTRBAndCorners(24.0, 297.0, 212.0, 303.0, topLeft: activatedRadius, bottomLeft: activatedRadius), color: sliderTheme.disabledActiveTrackColor)
        ..rrect(rrect: RRect.fromLTRBAndCorners(212.0, 298.0, 776.0, 302.0, topRight: radius, bottomRight: radius), color: sliderTheme.disabledInactiveTrackColor),
    );
  });

295 296 297 298 299 300 301
  testWidgets('Default slider track draws correctly', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme.copyWith(thumbColor: Colors.red.shade500);

302
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25));
303
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
304

305 306
    // The enabled slider thumb has track segments that extend to and from
    // the center of the thumb.
307
    expect(
308
      material,
309
      paints
310 311
        ..rect(rect: const Rect.fromLTRB(25.0, 299.0, 202.0, 301.0), color: sliderTheme.activeTrackColor)
        ..rect(rect: const Rect.fromLTRB(222.0, 299.0, 776.0, 301.0), color: sliderTheme.inactiveTrackColor),
312 313
    );

314
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25, enabled: false));
315
    await tester.pumpAndSettle(); // wait for disable animation
316

317
    // The disabled slider thumb is the same size as the enabled thumb.
318
    expect(
319
      material,
320
      paints
321 322
        ..rect(rect: const Rect.fromLTRB(25.0, 299.0, 202.0, 301.0), color: sliderTheme.disabledActiveTrackColor)
        ..rect(rect: const Rect.fromLTRB(222.0, 299.0, 776.0, 301.0), color: sliderTheme.disabledInactiveTrackColor),
323 324 325 326 327 328 329 330 331 332
    );
  });

  testWidgets('Default slider overlay draws correctly', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme.copyWith(thumbColor: Colors.red.shade500);

333
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25));
334
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
335 336 337

    // With no touch, paints only the thumb.
    expect(
338
      material,
339 340 341
      paints
        ..circle(
          color: sliderTheme.thumbColor,
342
          x: 212.0,
343
          y: 300.0,
344
          radius: 10.0,
345
        ),
346 347 348 349 350 351 352 353 354
    );

    final Offset center = tester.getCenter(find.byType(Slider));
    final TestGesture gesture = await tester.startGesture(center);
    // Wait for overlay animation to finish.
    await tester.pumpAndSettle();

    // After touch, paints thumb and overlay.
    expect(
355
      material,
356 357 358
      paints
        ..circle(
          color: sliderTheme.overlayColor,
359
          x: 212.0,
360
          y: 300.0,
361
          radius: 24.0,
362 363 364
        )
        ..circle(
          color: sliderTheme.thumbColor,
365
          x: 212.0,
366
          y: 300.0,
367
          radius: 10.0,
368
        ),
369 370 371 372
    );

    await gesture.up();
    await tester.pumpAndSettle();
373

374 375
    // After the gesture is up and complete, it again paints only the thumb.
    expect(
376
      material,
377 378 379
      paints
        ..circle(
          color: sliderTheme.thumbColor,
380
          x: 212.0,
381
          y: 300.0,
382
          radius: 10.0,
383
        ),
384 385 386 387
    );
  });

  testWidgets('Default slider ticker and thumb shape draw correctly', (WidgetTester tester) async {
388
    final ThemeData theme = ThemeData(
389 390 391 392 393
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme.copyWith(thumbColor: Colors.red.shade500);

394
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.45));
395
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
396

397
    expect(material, paints..circle(color: sliderTheme.thumbColor, radius: 10.0));
398

399
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.45, enabled: false));
400
    await tester.pumpAndSettle(); // wait for disable animation
401

402
    expect(material, paints..circle(color: sliderTheme.disabledThumbColor, radius: 10.0));
403

404 405 406
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.45, divisions: 3));
    await tester.pumpAndSettle(); // wait for enable animation

407
    expect(
408
      material,
409 410 411 412 413
      paints
        ..circle(color: sliderTheme.activeTickMarkColor)
        ..circle(color: sliderTheme.activeTickMarkColor)
        ..circle(color: sliderTheme.inactiveTickMarkColor)
        ..circle(color: sliderTheme.inactiveTickMarkColor)
414
        ..circle(color: sliderTheme.thumbColor, radius: 10.0),
415
    );
416

417
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.45, divisions: 3, enabled: false));
418
    await tester.pumpAndSettle(); // wait for disable animation
419

420
    expect(
421
      material,
422 423 424 425 426
      paints
        ..circle(color: sliderTheme.disabledActiveTickMarkColor)
        ..circle(color: sliderTheme.disabledInactiveTickMarkColor)
        ..circle(color: sliderTheme.disabledInactiveTickMarkColor)
        ..circle(color: sliderTheme.disabledInactiveTickMarkColor)
427
        ..circle(color: sliderTheme.disabledThumbColor, radius: 10.0),
428
    );
429 430
  });

431
  testWidgets('Slider V2 value indicator shape draws correctly', (WidgetTester tester) async {
Jose Alba's avatar
Jose Alba committed
432 433 434 435
    final ThemeData theme = ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
436 437 438 439
    final SliderThemeData sliderTheme = theme.sliderTheme.copyWith(
      thumbColor: Colors.red.shade500,
      showValueIndicator: ShowValueIndicator.always,
    );
Jose Alba's avatar
Jose Alba committed
440
    Widget buildApp(String value, { double sliderValue = 0.5, double textScale = 1.0 }) {
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
      return MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window).copyWith(textScaleFactor: textScale),
            child: Material(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: SliderTheme(
                      data: sliderTheme,
                      child: Slider(
                        value: sliderValue,
                        label: value,
                        divisions: 3,
                        onChanged: (double d) { },
                        useV2Slider: true,
                      ),
Jose Alba's avatar
Jose Alba committed
459 460
                    ),
                  ),
461 462
                ],
              ),
Jose Alba's avatar
Jose Alba committed
463 464 465 466 467 468 469 470
            ),
          ),
        ),
      );
    }

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

471
    final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay));
Jose Alba's avatar
Jose Alba committed
472 473 474 475 476 477

    Offset center = tester.getCenter(find.byType(Slider));
    TestGesture gesture = await tester.startGesture(center);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
      valueIndicatorBox,
      paints
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
            Offset(-20.0, -12.0),
            Offset(20.0, -34.0),
            Offset(0.0, -38.0),
          ],
          color: const Color(0xf55f5f5f),
        ),
    );

    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);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
      valueIndicatorBox,
      paints
        ..rrect()
        ..rrect()
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
            Offset(-30.0, -12.0),
            Offset(30.0, -34.0),
            Offset(0.0, -38.0),
          ],
          color: const Color(0xf55f5f5f),
        ),
    );
    await gesture.up();

    // 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);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
      valueIndicatorBox,
      paints
        ..rrect()
        ..rrect()
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
            Offset(-12.0, -12.0),
            Offset(110.0, -34.0),
            Offset(0.0, -38.0),
          ],
          color: const Color(0xf55f5f5f),
        )
    );
    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);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
      valueIndicatorBox,
      paints
        ..rrect()
        ..rrect()
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
            Offset(-110.0, -12.0),
            Offset(12.0, -34.0),
            Offset(0.0, -38.0),
          ],
          color: const Color(0xf55f5f5f),
        )
    );
    await gesture.up();

    // Test that the box decreases in height when the text scale gets smaller.
    await tester.pumpWidget(buildApp('1000000', sliderValue: 0.0, textScale: 0.5));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
      valueIndicatorBox,
      paints
        ..rrect()
        ..rrect()
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
            Offset(-12.0, -12.0),
            Offset(61.0, -16.0),
            Offset(0.0, -20.0),
          ],
          excludes: const <Offset>[
            Offset(0.0, -38.0)
          ],
          color: const Color(0xf55f5f5f),
        )
    );
    await gesture.up();

    // Test that the box increases in height when the text scale gets bigger.
    await tester.pumpWidget(buildApp('1000000', sliderValue: 0.0, textScale: 2.0));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
      valueIndicatorBox,
      paints
        ..rrect()
        ..rrect()
        ..path(
          includes: const <Offset>[
            Offset(0.0, 0.0),
            Offset(-12.0, -16.0),
            Offset(208.0, -40.0),
            Offset(0.0, -50.0),
          ],
          color: const Color(0xf55f5f5f),
        )
    );
    await gesture.up();
  }, skip: isBrowser);

  testWidgets('Default paddle slider value indicator shape draws correctly', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
    final SliderThemeData sliderTheme = theme.sliderTheme.copyWith(
      thumbColor: Colors.red.shade500,
      showValueIndicator: ShowValueIndicator.always,
      valueIndicatorShape: const PaddleSliderValueIndicatorShape(),
    );
    Widget buildApp(String value, { double sliderValue = 0.5, double textScale = 1.0 }) {
      return MaterialApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData.fromWindow(window).copyWith(textScaleFactor: textScale),
            child: Material(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: SliderTheme(
                      data: sliderTheme,
                      child: Slider(
                        value: sliderValue,
                        label: value,
                        divisions: 3,
                        onChanged: (double d) { },
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    }

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

    final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay));

    Offset center = tester.getCenter(find.byType(Slider));
    TestGesture gesture = await tester.startGesture(center);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
      valueIndicatorBox,
660 661 662 663 664 665 666 667 668
      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)],
669
        ),
670
    );
671 672 673 674 675 676 677 678

    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);
    // Wait for value indicator animation to finish.
679
    await tester.pumpAndSettle();
680
    expect(
681
      valueIndicatorBox,
682 683 684 685 686 687 688 689 690
      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)],
691
        ),
692
    );
693
    await gesture.up();
694 695 696 697 698 699

    // 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);
    // Wait for value indicator animation to finish.
700
    await tester.pumpAndSettle();
701
    expect(
702
      valueIndicatorBox,
703 704 705 706 707
      paints
        ..path(
          color: sliderTheme.valueIndicatorColor,
          includes: <Offset>[
            const Offset(0.0, -40.0),
708
            const Offset(92.0, -40.0),
709 710
            const Offset(-16.0, -40.0),
          ],
711
          excludes: <Offset>[const Offset(98.1, -40.0), const Offset(-20.1, -40.0)],
712
        ),
713
    );
714 715 716 717 718 719 720
    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);
    // Wait for value indicator animation to finish.
721
    await tester.pumpAndSettle();
722
    expect(
723
      valueIndicatorBox,
724 725 726 727 728 729
      paints
        ..path(
          color: sliderTheme.valueIndicatorColor,
          includes: <Offset>[
            const Offset(0.0, -40.0),
            const Offset(16.0, -40.0),
730
            const Offset(-92.0, -40.0),
731
          ],
732
          excludes: <Offset>[const Offset(20.1, -40.0), const Offset(-98.1, -40.0)],
733
        ),
734
    );
735
    await gesture.up();
736 737 738 739 740 741 742 743

    // Test that the neck stretches when the text scale gets smaller.
    await tester.pumpWidget(buildApp('1000000', sliderValue: 0.0, textScale: 0.5));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
744
      valueIndicatorBox,
745 746 747 748 749
      paints
        ..path(
          color: sliderTheme.valueIndicatorColor,
          includes: <Offset>[
            const Offset(0.0, -49.0),
750
            const Offset(68.0, -49.0),
751 752 753 754
            const Offset(-24.0, -49.0),
          ],
          excludes: <Offset>[
            const Offset(98.0, -32.0),  // inside full size, outside small
755
            const Offset(-40.0, -32.0),  // inside full size, outside small
756
            const Offset(90.1, -49.0),
757
            const Offset(-40.1, -49.0),
758
          ],
759
        ),
760
    );
761 762 763 764 765 766 767 768 769
    await gesture.up();

    // Test that the neck shrinks when the text scale gets larger.
    await tester.pumpWidget(buildApp('1000000', sliderValue: 0.0, textScale: 2.5));
    center = tester.getCenter(find.byType(Slider));
    gesture = await tester.startGesture(center);
    // Wait for value indicator animation to finish.
    await tester.pumpAndSettle();
    expect(
770
      valueIndicatorBox,
771 772 773 774 775
      paints
        ..path(
          color: sliderTheme.valueIndicatorColor,
          includes: <Offset>[
            const Offset(0.0, -38.8),
776 777 778
            const Offset(92.0, -38.8),
            const Offset(8.0, -23.0), // Inside large, outside scale=1.0
            const Offset(-2.0, -23.0), // Inside large, outside scale=1.0
779 780 781 782 783
          ],
          excludes: <Offset>[
            const Offset(98.5, -38.8),
            const Offset(-16.1, -38.8),
          ],
784
        ),
785
    );
786
    await gesture.up();
787
  }, skip: isBrowser);
788 789 790 791 792 793

  testWidgets('The slider track height can be overridden', (WidgetTester tester) async {
    final SliderThemeData sliderTheme = ThemeData().sliderTheme.copyWith(trackHeight: 16);

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25));

794
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
795 796 797

    // Top and bottom are centerY (300) + and - trackRadius (8).
    expect(
798
      material,
799
      paints
800 801
        ..rect(rect: const Rect.fromLTRB(32.0, 292.0, 202.0, 308.0), color: sliderTheme.activeTrackColor)
        ..rect(rect: const Rect.fromLTRB(222.0, 292.0, 776.0, 308.0), color: sliderTheme.inactiveTrackColor),
802 803 804 805 806 807 808 809
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25, enabled: false));
    await tester.pumpAndSettle(); // wait for disable animation

    // The disabled thumb is smaller so the active track has to paint longer to
    // get to the edge.
    expect(
810
      material,
811
      paints
812 813
        ..rect(rect: const Rect.fromLTRB(32.0, 292.0, 202.0, 308.0), color: sliderTheme.disabledActiveTrackColor)
        ..rect(rect: const Rect.fromLTRB(222.0, 292.0, 776.0, 308.0), color: sliderTheme.disabledInactiveTrackColor),
814 815 816
    );
  });

817 818 819 820 821 822 823
  testWidgets('The slider V2 track height can be overridden', (WidgetTester tester) async {
    final SliderThemeData sliderTheme = ThemeData().sliderTheme.copyWith(trackHeight: 16);
    const Radius radius = Radius.circular(8);
    const Radius activatedRadius = Radius.circular(9);

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25, useV2Slider: true));

824
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
825 826 827

    // Top and bottom are centerY (300) + and - trackRadius (8).
    expect(
828
      material,
829 830 831 832 833 834 835 836 837 838 839
      paints
        ..rrect(rrect: RRect.fromLTRBAndCorners(24.0, 291.0, 212.0, 309.0, topLeft: activatedRadius, bottomLeft: activatedRadius), color: sliderTheme.activeTrackColor)
        ..rrect(rrect: RRect.fromLTRBAndCorners(212.0, 292.0, 776.0, 308.0, topRight: radius, bottomRight: radius), color: sliderTheme.inactiveTrackColor),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25, enabled: false, useV2Slider: true));
    await tester.pumpAndSettle(); // wait for disable animation

    // The disabled thumb is smaller so the active track has to paint longer to
    // get to the edge.
    expect(
840
      material,
841 842 843 844 845 846
      paints
        ..rrect(rrect: RRect.fromLTRBAndCorners(24.0, 291.0, 212.0, 309.0, topLeft: activatedRadius, bottomLeft: activatedRadius), color: sliderTheme.disabledActiveTrackColor)
        ..rrect(rrect: RRect.fromLTRBAndCorners(212.0, 292.0, 776.0, 308.0, topRight: radius, bottomRight: radius), color: sliderTheme.disabledInactiveTrackColor),
    );
  });

847 848 849 850 851 852 853 854 855
  testWidgets('The default slider thumb shape sizes can be overridden', (WidgetTester tester) async {
    final SliderThemeData sliderTheme = ThemeData().sliderTheme.copyWith(
        thumbShape: const RoundSliderThumbShape(
          enabledThumbRadius: 7,
          disabledThumbRadius: 11,
        ),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25));
856
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
857 858

    expect(
859
      material,
860
      paints..circle(x: 212, y: 300, radius: 7, color: sliderTheme.thumbColor),
861 862 863 864 865 866
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25, enabled: false));
    await tester.pumpAndSettle(); // wait for disable animation

    expect(
867
      material,
868
      paints..circle(x: 212, y: 300, radius: 11, color: sliderTheme.disabledThumbColor),
869 870 871 872 873 874 875 876 877 878 879
    );
  });

  testWidgets('The default slider thumb shape disabled size can be inferred from the enabled size', (WidgetTester tester) async {
    final SliderThemeData sliderTheme = ThemeData().sliderTheme.copyWith(
      thumbShape: const RoundSliderThumbShape(
        enabledThumbRadius: 9,
      ),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25));
880
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
881 882

    expect(
883
      material,
884
      paints..circle(x: 212, y: 300, radius: 9, color: sliderTheme.thumbColor),
885 886 887 888 889
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.25, enabled: false));
    await tester.pumpAndSettle(); // wait for disable animation
    expect(
890
      material,
891
      paints..circle(x: 212, y: 300, radius: 9, color: sliderTheme.disabledThumbColor),
892 893 894 895 896
    );
  });

  testWidgets('The default slider tick mark shape size can be overridden', (WidgetTester tester) async {
    final SliderThemeData sliderTheme = ThemeData().sliderTheme.copyWith(
897
      tickMarkShape: const RoundSliderTickMarkShape(tickMarkRadius: 5),
898 899 900 901 902 903 904 905
      activeTickMarkColor: const Color(0xfadedead),
      inactiveTickMarkColor: const Color(0xfadebeef),
      disabledActiveTickMarkColor: const Color(0xfadecafe),
      disabledInactiveTickMarkColor: const Color(0xfadeface),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5, divisions: 2));

906
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
907 908

    expect(
909
      material,
910
      paints
911
        ..circle(x: 29, y: 300, radius: 5, color: sliderTheme.activeTickMarkColor)
912
        ..circle(x: 400, y: 300, radius: 5, color: sliderTheme.activeTickMarkColor)
913
        ..circle(x: 771, y: 300, radius: 5, color: sliderTheme.inactiveTickMarkColor),
914 915
    );

916
    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5, divisions: 2, enabled: false));
917 918 919
    await tester.pumpAndSettle();

    expect(
920
      material,
921
      paints
922
        ..circle(x: 29, y: 300, radius: 5, color: sliderTheme.disabledActiveTickMarkColor)
923
        ..circle(x: 400, y: 300, radius: 5, color: sliderTheme.disabledActiveTickMarkColor)
924
        ..circle(x: 771, y: 300, radius: 5, color: sliderTheme.disabledInactiveTickMarkColor),
925 926 927
    );
  });

928 929 930 931 932 933 934 935 936 937 938
  testWidgets('The default slider V2 tick mark shape size can be overridden', (WidgetTester tester) async {
    final SliderThemeData sliderTheme = ThemeData().sliderTheme.copyWith(
      tickMarkShape: const RoundSliderTickMarkShape(tickMarkRadius: 5, useV2Slider: true),
      activeTickMarkColor: const Color(0xfadedead),
      inactiveTickMarkColor: const Color(0xfadebeef),
      disabledActiveTickMarkColor: const Color(0xfadecafe),
      disabledInactiveTickMarkColor: const Color(0xfadeface),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5, divisions: 2, useV2Slider: true));

939
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
940 941

    expect(
942
      material,
943 944 945 946 947 948 949 950 951 952
      paints
        ..circle(x: 26, y: 300, radius: 5, color: sliderTheme.activeTickMarkColor)
        ..circle(x: 400, y: 300, radius: 5, color: sliderTheme.activeTickMarkColor)
        ..circle(x: 774, y: 300, radius: 5, color: sliderTheme.inactiveTickMarkColor),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5, divisions: 2,  enabled: false, useV2Slider: true));
    await tester.pumpAndSettle();

    expect(
953
      material,
954 955 956 957 958 959 960
      paints
        ..circle(x: 26, y: 300, radius: 5, color: sliderTheme.disabledActiveTickMarkColor)
        ..circle(x: 400, y: 300, radius: 5, color: sliderTheme.disabledActiveTickMarkColor)
        ..circle(x: 774, y: 300, radius: 5, color: sliderTheme.disabledInactiveTickMarkColor),
    );
  });

961 962 963 964 965 966 967 968 969 970 971 972 973 974
  testWidgets('The default slider overlay shape size can be overridden', (WidgetTester tester) async {
    const double uniqueOverlayRadius = 23;
    final SliderThemeData sliderTheme = ThemeData().sliderTheme.copyWith(
      overlayShape: const RoundSliderOverlayShape(
        overlayRadius: uniqueOverlayRadius,
      ),
    );

    await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5));
    // Tap center and wait for animation.
    final Offset center = tester.getCenter(find.byType(Slider));
    await tester.startGesture(center);
    await tester.pumpAndSettle();

975
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
976
    expect(
977
      material,
978 979 980 981 982
      paints..circle(
        x: center.dx,
        y: center.dy,
        radius: uniqueOverlayRadius,
        color: sliderTheme.overlayColor,
983
      ),
984 985
    );
  });
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004

  // Only the thumb, overlay, and tick mark have special shortcuts to provide
  // no-op or empty shapes.
  //
  // The track can also be skipped by providing 0 height.
  //
  // The value indicator can be skipped by passing the appropriate
  // [ShowValueIndicator].
  testWidgets('The slider can skip all of its comoponent painting', (WidgetTester tester) async {
    // Pump a slider with all shapes skipped.
    await tester.pumpWidget(_buildApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        overlayShape: SliderComponentShape.noOverlay,
        thumbShape: SliderComponentShape.noThumb,
        tickMarkShape: SliderTickMarkShape.noTickMark,
        showValueIndicator: ShowValueIndicator.never,
      ),
      value: 0.5,
1005
      divisions: 4,
1006 1007
    ));

1008
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
1009

1010 1011 1012
    expect(material, paintsExactlyCountTimes(#drawRect, 0));
    expect(material, paintsExactlyCountTimes(#drawCircle, 0));
    expect(material, paintsExactlyCountTimes(#drawPath, 0));
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
  });

  testWidgets('The slider can skip all component painting except the track', (WidgetTester tester) async {
    // Pump a slider with just a track.
    await tester.pumpWidget(_buildApp(
      ThemeData().sliderTheme.copyWith(
        overlayShape: SliderComponentShape.noOverlay,
        thumbShape: SliderComponentShape.noThumb,
        tickMarkShape: SliderTickMarkShape.noTickMark,
        showValueIndicator: ShowValueIndicator.never,
      ),
      value: 0.5,
1025
      divisions: 4,
1026 1027
    ));

1028
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
1029 1030

    // Only 2 track segments.
1031 1032 1033
    expect(material, paintsExactlyCountTimes(#drawRect, 2));
    expect(material, paintsExactlyCountTimes(#drawCircle, 0));
    expect(material, paintsExactlyCountTimes(#drawPath, 0));
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  });

  testWidgets('The slider can skip all component painting except the tick marks', (WidgetTester tester) async {
    // Pump a slider with just tick marks.
    await tester.pumpWidget(_buildApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        overlayShape: SliderComponentShape.noOverlay,
        thumbShape: SliderComponentShape.noThumb,
        showValueIndicator: ShowValueIndicator.never,
1044 1045 1046
        // When the track is hidden to 0 height, a tick mark radius
        // must be provided to get a non-zero radius.
        tickMarkShape: const RoundSliderTickMarkShape(tickMarkRadius: 1),
1047 1048
      ),
      value: 0.5,
1049
      divisions: 4,
1050 1051
    ));

1052
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
1053 1054

    // Only 5 tick marks.
1055 1056 1057
    expect(material, paintsExactlyCountTimes(#drawRect, 0));
    expect(material, paintsExactlyCountTimes(#drawCircle, 5));
    expect(material, paintsExactlyCountTimes(#drawPath, 0));
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  });

  testWidgets('The slider can skip all component painting except the thumb', (WidgetTester tester) async {
    // Pump a slider with just a thumb.
    await tester.pumpWidget(_buildApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        overlayShape: SliderComponentShape.noOverlay,
        tickMarkShape: SliderTickMarkShape.noTickMark,
        showValueIndicator: ShowValueIndicator.never,
      ),
      value: 0.5,
1070
      divisions: 4,
1071 1072
    ));

1073
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
1074 1075

    // Only 1 thumb.
1076 1077 1078
    expect(material, paintsExactlyCountTimes(#drawRect, 0));
    expect(material, paintsExactlyCountTimes(#drawCircle, 1));
    expect(material, paintsExactlyCountTimes(#drawPath, 0));
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
  });

  testWidgets('The slider can skip all component painting except the overlay', (WidgetTester tester) async {
    // Pump a slider with just an overlay.
    await tester.pumpWidget(_buildApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        thumbShape: SliderComponentShape.noThumb,
        tickMarkShape: SliderTickMarkShape.noTickMark,
        showValueIndicator: ShowValueIndicator.never,
      ),
      value: 0.5,
1091
      divisions: 4,
1092 1093
    ));

1094
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
1095 1096 1097 1098 1099 1100 1101

    // Tap the center of the track and wait for animations to finish.
    final Offset center = tester.getCenter(find.byType(Slider));
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pumpAndSettle();

    // Only 1 overlay.
1102 1103 1104
    expect(material, paintsExactlyCountTimes(#drawRect, 0));
    expect(material, paintsExactlyCountTimes(#drawCircle, 1));
    expect(material, paintsExactlyCountTimes(#drawPath, 0));
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

    await gesture.up();
  });

  testWidgets('The slider can skip all component painting except the value indicator', (WidgetTester tester) async {
    // Pump a slider with just a value indicator.
    await tester.pumpWidget(_buildApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        overlayShape: SliderComponentShape.noOverlay,
        thumbShape: SliderComponentShape.noThumb,
        tickMarkShape: SliderTickMarkShape.noTickMark,
        showValueIndicator: ShowValueIndicator.always,
      ),
      value: 0.5,
1120
      divisions: 4,
1121 1122
    ));

1123
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
1124
    final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay));
1125 1126 1127 1128 1129 1130 1131

    // Tap the center of the track and wait for animations to finish.
    final Offset center = tester.getCenter(find.byType(Slider));
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pumpAndSettle();

    // Only 1 value indicator.
1132 1133
    expect(material, paintsExactlyCountTimes(#drawRect, 0));
    expect(material, paintsExactlyCountTimes(#drawCircle, 0));
1134
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1));
Jose Alba's avatar
Jose Alba committed
1135 1136 1137 1138

    await gesture.up();
  });

1139
  testWidgets('PaddleSliderValueIndicatorShape skips all painting at zero scale', (WidgetTester tester) async {
Jose Alba's avatar
Jose Alba committed
1140 1141 1142 1143 1144 1145 1146 1147
    // Pump a slider with just a value indicator.
    await tester.pumpWidget(_buildApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        overlayShape: SliderComponentShape.noOverlay,
        thumbShape: SliderComponentShape.noThumb,
        tickMarkShape: SliderTickMarkShape.noTickMark,
        showValueIndicator: ShowValueIndicator.always,
1148
        valueIndicatorShape: const PaddleSliderValueIndicatorShape(),
Jose Alba's avatar
Jose Alba committed
1149 1150 1151 1152 1153
      ),
      value: 0.5,
      divisions: 4,
    ));

1154
    final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
1155
    final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay));
Jose Alba's avatar
Jose Alba committed
1156 1157 1158 1159 1160 1161 1162

    // Tap the center of the track to kick off the animation of the value indicator.
    final Offset center = tester.getCenter(find.byType(Slider));
    final TestGesture gesture = await tester.startGesture(center);

    // Nothing to paint at scale 0.
    await tester.pump();
1163 1164
    expect(material, paintsExactlyCountTimes(#drawRect, 0));
    expect(material, paintsExactlyCountTimes(#drawCircle, 0));
1165
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 0));
Jose Alba's avatar
Jose Alba committed
1166 1167 1168

    // Painting a path for the value indicator.
    await tester.pump(const Duration(milliseconds: 16));
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1));

    await gesture.up();
  });

  testWidgets('Default slider value indicator shape skips all painting at zero scale', (WidgetTester tester) async {
    // Pump a slider with just a value indicator.
    await tester.pumpWidget(_buildApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        overlayShape: SliderComponentShape.noOverlay,
        thumbShape: SliderComponentShape.noThumb,
        tickMarkShape: SliderTickMarkShape.noTickMark,
        showValueIndicator: ShowValueIndicator.always,
      ),
      value: 0.5,
      divisions: 4,
    ));

    final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay));

    // Tap the center of the track to kick off the animation of the value indicator.
    final Offset center = tester.getCenter(find.byType(Slider));
    final TestGesture gesture = await tester.startGesture(center);

    // Nothing to paint at scale 0.
    await tester.pump();
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 0));

    // Painting a path for the value indicator.
    await tester.pump(const Duration(milliseconds: 16));
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1));

    await gesture.up();
  });

  testWidgets('PaddleRangeSliderValueIndicatorShape skips all painting at zero scale', (WidgetTester tester) async {
    // Pump a slider with just a value indicator.
    await tester.pumpWidget(_buildRangeApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        rangeValueIndicatorShape: const PaddleRangeSliderValueIndicatorShape(),
      ),
      values: const RangeValues(0, 0.5),
      divisions: 4,
    ));

//    final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(RangeSlider));
    final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay));

    // Tap the center of the track to kick off the animation of the value indicator.
    final Offset center = tester.getCenter(find.byType(RangeSlider));
    final TestGesture gesture = await tester.startGesture(center);

    // No value indicator path to paint at scale 0.
    await tester.pump();
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 0));

    // Painting a path for each value indicator.
    await tester.pump(const Duration(milliseconds: 16));
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 2));

    await gesture.up();
  });

  testWidgets('Default range indicator shape skips all painting at zero scale', (WidgetTester tester) async {
    // Pump a slider with just a value indicator.
    await tester.pumpWidget(_buildRangeApp(
      ThemeData().sliderTheme.copyWith(
        trackHeight: 0,
        overlayShape: SliderComponentShape.noOverlay,
        thumbShape: SliderComponentShape.noThumb,
        tickMarkShape: SliderTickMarkShape.noTickMark,
        showValueIndicator: ShowValueIndicator.always,
      ),
      values: const RangeValues(0, 0.5),
      divisions: 4,
    ));

    final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay));

    // Tap the center of the track to kick off the animation of the value indicator.
    final Offset center = tester.getCenter(find.byType(RangeSlider));
    final TestGesture gesture = await tester.startGesture(center);

    // No value indicator path to paint at scale 0.
    await tester.pump();
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 0));

    // Painting a path for each value indicator.
    await tester.pump(const Duration(milliseconds: 16));
    expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 2));
1261 1262 1263 1264

    await gesture.up();
  });
}
1265

1266 1267 1268 1269 1270
Widget _buildApp(
  SliderThemeData sliderTheme, {
  double value = 0.0,
  bool enabled = true,
  int divisions,
1271
  bool useV2Slider = false,
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
}) {
  final ValueChanged<double> onChanged = enabled ? (double d) => value = d : null;
  return MaterialApp(
    home: Scaffold(
      body: Center(
        child: SliderTheme(
          data: sliderTheme,
          child: Slider(
            value: value,
            label: '$value',
            onChanged: onChanged,
            divisions: divisions,
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
            useV2Slider: useV2Slider
          ),
        ),
      ),
    ),
  );
}

Widget _buildRangeApp(
    SliderThemeData sliderTheme, {
      RangeValues values = const RangeValues(0, 0),
      bool enabled = true,
      int divisions,
    }) {
  final ValueChanged<RangeValues> onChanged = enabled ? (RangeValues d) => values = d : null;
  return MaterialApp(
    home: Scaffold(
      body: Center(
        child: SliderTheme(
          data: sliderTheme,
          child: RangeSlider(
            values: values,
            labels: RangeLabels(values.start.toString(), values.end.toString()),
            onChanged: onChanged,
            divisions: divisions,
1309 1310 1311 1312 1313
          ),
        ),
      ),
    ),
  );
1314
}