slider_test.dart 19.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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
8 9 10
import 'package:flutter/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';

xster's avatar
xster committed
11
import '../rendering/mock_canvas.dart';
12 13
import '../widgets/semantics_tester.dart';

14 15 16 17 18 19 20 21 22 23 24
const CupertinoDynamicColor _kSystemFill = CupertinoDynamicColor(
  color: Color.fromARGB(51, 120, 120, 128),
  darkColor: Color.fromARGB(91, 120, 120, 128),
  highContrastColor: Color.fromARGB(71, 120, 120, 128),
  darkHighContrastColor: Color.fromARGB(112, 120, 120, 128),
  elevatedColor: Color.fromARGB(51, 120, 120, 128),
  darkElevatedColor: Color.fromARGB(91, 120, 120, 128),
  highContrastElevatedColor: Color.fromARGB(71, 120, 120, 128),
  darkHighContrastElevatedColor: Color.fromARGB(112, 120, 120, 128),
);

25
void main() {
26

27
  Future<void> _dragSlider(WidgetTester tester, Key sliderKey) {
28 29 30 31 32 33
    final Offset topLeft = tester.getTopLeft(find.byKey(sliderKey));
    const double unit = CupertinoThumbPainter.radius;
    const double delta = 3.0 * unit;
    return tester.dragFrom(topLeft + const Offset(unit, unit), const Offset(delta, 0.0));
  }

34
  testWidgets('Slider does not move when tapped (LTR)', (WidgetTester tester) async {
35
    final Key sliderKey = UniqueKey();
36 37
    double value = 0.0;

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    await tester.pumpWidget(
      CupertinoApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Material(
                child: Center(
                  child: CupertinoSlider(
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() { value = newValue; });
                    },
                  ),
                ),
              );
            },
          ),
        ),
58
      ),
59
    );
60 61

    expect(value, equals(0.0));
62
    await tester.tap(find.byKey(sliderKey), warnIfMissed: false);
63 64 65 66
    expect(value, equals(0.0));
    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
67
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
68 69 70
  });

  testWidgets('Slider does not move when tapped (RTL)', (WidgetTester tester) async {
71
    final Key sliderKey = UniqueKey();
72 73
    double value = 0.0;

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    await tester.pumpWidget(
      CupertinoApp(
        home: Directionality(
          textDirection: TextDirection.rtl,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Material(
                child: Center(
                  child: CupertinoSlider(
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() { value = newValue; });
                    },
                  ),
                ),
              );
            },
          ),
        ),
94
      ),
95
    );
96 97

    expect(value, equals(0.0));
98
    await tester.tap(find.byKey(sliderKey), warnIfMissed: false);
99 100 101 102
    expect(value, equals(0.0));
    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
103
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
104 105
  });

106
  testWidgets('Slider calls onChangeStart once when interaction begins', (WidgetTester tester) async {
107
    final Key sliderKey = UniqueKey();
108 109 110
    double value = 0.0;
    int numberOfTimesOnChangeStartIsCalled = 0;

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    await tester.pumpWidget(
      CupertinoApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Material(
                child: Center(
                  child: CupertinoSlider(
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() { value = newValue; });
                    },
                    onChangeStart: (double value) {
                      numberOfTimesOnChangeStartIsCalled++;
                    },
                  ),
                ),
              );
            },
          ),
        ),
134
      ),
135
    );
136 137

    await _dragSlider(tester, sliderKey);
138

139 140 141 142 143
    expect(numberOfTimesOnChangeStartIsCalled, equals(1));

    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
144
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
145 146 147
  });

  testWidgets('Slider calls onChangeEnd once after interaction has ended', (WidgetTester tester) async {
148
    final Key sliderKey = UniqueKey();
149 150 151
    double value = 0.0;
    int numberOfTimesOnChangeEndIsCalled = 0;

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    await tester.pumpWidget(
      CupertinoApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Material(
                child: Center(
                  child: CupertinoSlider(
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() { value = newValue; });
                    },
                    onChangeEnd: (double value) {
                      numberOfTimesOnChangeEndIsCalled++;
                    },
                  ),
                ),
              );
            },
          ),
        ),
175
      ),
176
    );
177

178
    await _dragSlider(tester, sliderKey);
179

180 181 182 183 184
    expect(numberOfTimesOnChangeEndIsCalled, equals(1));

    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
185
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
186
  });
187 188

  testWidgets('Slider moves when dragged (LTR)', (WidgetTester tester) async {
189
    final Key sliderKey = UniqueKey();
190
    double value = 0.0;
191 192
    late double startValue;
    late double endValue;
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
    await tester.pumpWidget(
      CupertinoApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Material(
                child: Center(
                  child: CupertinoSlider(
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() { value = newValue; });
                    },
                    onChangeStart: (double value) {
                      startValue = value;
                    },
                    onChangeEnd: (double value) {
                      endValue = value;
                    },
                  ),
                ),
              );
            },
          ),
        ),
220
      ),
221
    );
222 223

    expect(value, equals(0.0));
224

225
    final Offset topLeft = tester.getTopLeft(find.byKey(sliderKey));
226 227
    const double unit = CupertinoThumbPainter.radius;
    const double delta = 3.0 * unit;
228
    await tester.dragFrom(topLeft + const Offset(unit, unit), const Offset(delta, 0.0));
229

230
    final Size size = tester.getSize(find.byKey(sliderKey));
231 232 233 234
    final double finalValue = delta / (size.width - 2.0 * (8.0 + CupertinoThumbPainter.radius));
    expect(startValue, equals(0.0));
    expect(value, equals(finalValue));
    expect(endValue, equals(finalValue));
235

236 237 238
    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
239
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
240
  });
241

242
  testWidgets('Slider moves when dragged (RTL)', (WidgetTester tester) async {
243
    final Key sliderKey = UniqueKey();
244
    double value = 0.0;
245 246
    late double startValue;
    late double endValue;
247

248 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
    await tester.pumpWidget(
      CupertinoApp(
        home: Directionality(
          textDirection: TextDirection.rtl,
          child: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Material(
                child: Center(
                  child: CupertinoSlider(
                    key: sliderKey,
                    value: value,
                    onChanged: (double newValue) {
                      setState(() { value = newValue; });
                    },
                    onChangeStart: (double value) {
                      setState(() { startValue = value; });
                    },
                    onChangeEnd: (double value) {
                      setState(() { endValue = value; });
                    },
                  ),
                ),
              );
            },
          ),
        ),
274
      ),
275
    );
276 277

    expect(value, equals(0.0));
278

279 280 281 282
    final Offset bottomRight = tester.getBottomRight(find.byKey(sliderKey));
    const double unit = CupertinoThumbPainter.radius;
    const double delta = 3.0 * unit;
    await tester.dragFrom(bottomRight - const Offset(unit, unit), const Offset(-delta, 0.0));
283

284
    final Size size = tester.getSize(find.byKey(sliderKey));
285 286 287 288
    final double finalValue = delta / (size.width - 2.0 * (8.0 + CupertinoThumbPainter.radius));
    expect(startValue, equals(0.0));
    expect(value, equals(finalValue));
    expect(endValue, equals(finalValue));
289

290 291 292
    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
293
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(0));
294 295
  });

296
  testWidgets('Slider Semantics', (WidgetTester tester) async {
297
    final SemanticsTester semantics = SemanticsTester(tester);
298

299 300 301 302 303 304 305 306 307 308
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(),
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: CupertinoSlider(
            value: 0.5,
            onChanged: (double v) { },
          ),
        ),
309
      ),
310
    );
311 312

    expect(semantics, hasSemantics(
313
      TestSemantics.root(
314
        children: <TestSemantics>[
315
          TestSemantics.rootChild(
316
            id: 1,
317 318 319 320
            value: '50%',
            increasedValue: '60%',
            decreasedValue: '40%',
            textDirection: TextDirection.ltr,
321
            flags: <SemanticsFlag>[SemanticsFlag.isSlider],
322 323
            actions: SemanticsAction.decrease.index | SemanticsAction.increase.index,
          ),
324
        ],
325 326 327 328 329 330
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    // Disable slider
331 332 333 334 335 336 337 338 339 340
    await tester.pumpWidget(
      const MediaQuery(
        data: MediaQueryData(),
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: CupertinoSlider(
            value: 0.5,
            onChanged: null,
          ),
        ),
341
      ),
342
    );
343 344

    expect(semantics, hasSemantics(
345 346 347 348 349 350 351 352
      TestSemantics.root(
        children: <TestSemantics>[
          TestSemantics(
            id: 1,
            flags: <SemanticsFlag>[SemanticsFlag.isSlider],
          )
        ],
      ),
353 354 355 356 357 358
      ignoreRect: true,
      ignoreTransform: true,
    ));

    semantics.dispose();
  });
359 360 361 362

  testWidgets('Slider Semantics can be updated', (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
    double value = 0.5;
363 364 365 366 367 368 369 370 371
    await tester.pumpWidget(
      CupertinoApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: CupertinoSlider(
            value: value,
            onChanged: (double v) { },
          ),
        ),
372
      ),
373
    );
374

375
    expect(tester.getSemantics(find.byType(CupertinoSlider)), matchesSemantics(
376
      isSlider: true,
377 378 379 380 381 382 383 384 385
      hasIncreaseAction: true,
      hasDecreaseAction: true,
      value: '50%',
      increasedValue: '60%',
      decreasedValue: '40%',
      textDirection: TextDirection.ltr,
    ));

    value = 0.6;
386 387 388 389 390 391 392 393 394
    await tester.pumpWidget(
      CupertinoApp(
        home: Directionality(
          textDirection: TextDirection.ltr,
          child: CupertinoSlider(
            value: value,
            onChanged: (double v) { },
          ),
        ),
395
      ),
396
    );
397

398
    expect(tester.getSemantics(find.byType(CupertinoSlider)), matchesSemantics(
399
      isSlider: true,
400 401 402 403 404 405 406 407 408 409
      hasIncreaseAction: true,
      hasDecreaseAction: true,
      value: '60%',
      increasedValue: '70%',
      decreasedValue: '50%',
      textDirection: TextDirection.ltr,
    ));

    handle.dispose();
  });
xster's avatar
xster committed
410 411 412 413 414 415

  testWidgets('Slider respects themes', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: CupertinoSlider(
416
            onChanged: (double value) { },
xster's avatar
xster committed
417 418 419 420 421 422 423 424
            value: 0.5,
          ),
        ),
      ),
    );
    expect(
      find.byType(CupertinoSlider),
      // First line it paints is blue.
425
      paints..rrect(color: CupertinoColors.systemBlue.color),
xster's avatar
xster committed
426 427 428 429 430 431 432
    );

    await tester.pumpWidget(
      CupertinoApp(
        theme: const CupertinoThemeData(brightness: Brightness.dark),
        home: Center(
          child: CupertinoSlider(
433
            onChanged: (double value) { },
xster's avatar
xster committed
434 435 436 437 438
            value: 0.5,
          ),
        ),
      ),
    );
439

xster's avatar
xster committed
440 441
    expect(
      find.byType(CupertinoSlider),
442
      paints..rrect(color: CupertinoColors.systemBlue.darkColor),
xster's avatar
xster committed
443 444 445 446 447 448 449 450 451 452
    );
  });

  testWidgets('Themes can be overridden', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        theme: const CupertinoThemeData(brightness: Brightness.dark),
        home: Center(
          child: CupertinoSlider(
            activeColor: CupertinoColors.activeGreen,
453
            onChanged: (double value) { },
xster's avatar
xster committed
454 455 456 457 458 459 460
            value: 0.5,
          ),
        ),
      ),
    );
    expect(
      find.byType(CupertinoSlider),
461
      paints..rrect(color: CupertinoColors.systemGreen.darkColor),
xster's avatar
xster committed
462 463
    );
  });
464 465

  testWidgets('Themes can be overridden by dynamic colors', (WidgetTester tester) async {
466 467 468 469 470 471 472 473 474
    const CupertinoDynamicColor activeColor = CupertinoDynamicColor(
      color: Color(0x00000001),
      darkColor: Color(0x00000002),
      elevatedColor: Color(0x00000003),
      highContrastColor: Color(0x00000004),
      darkElevatedColor: Color(0x00000005),
      darkHighContrastColor: Color(0x00000006),
      highContrastElevatedColor: Color(0x00000007),
      darkHighContrastElevatedColor: Color(0x00000008),
475 476 477 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
    );

    Widget withTraits(Brightness brightness, CupertinoUserInterfaceLevelData level, bool highContrast) {
      return CupertinoTheme(
        data: CupertinoThemeData(brightness: brightness),
        child: CupertinoUserInterfaceLevel(
          data: level,
          child: MediaQuery(
            data: MediaQueryData(highContrast: highContrast),
            child: Center(
              child: CupertinoSlider(
                activeColor: activeColor,
                onChanged: (double value) { },
                value: 0.5,
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(CupertinoApp(home: withTraits(Brightness.light, CupertinoUserInterfaceLevelData.base, false)));
    expect(find.byType(CupertinoSlider), paints..rrect(color: activeColor.color));

    await tester.pumpWidget(CupertinoApp(home: withTraits(Brightness.dark, CupertinoUserInterfaceLevelData.base, false)));
    expect(find.byType(CupertinoSlider), paints..rrect(color: activeColor.darkColor));

    await tester.pumpWidget(CupertinoApp(home: withTraits(Brightness.dark, CupertinoUserInterfaceLevelData.elevated, false)));
    expect(find.byType(CupertinoSlider), paints..rrect(color: activeColor.darkElevatedColor));

    await tester.pumpWidget(CupertinoApp(home: withTraits(Brightness.dark, CupertinoUserInterfaceLevelData.base, true)));
    expect(find.byType(CupertinoSlider), paints..rrect(color: activeColor.darkHighContrastColor));

    await tester.pumpWidget(CupertinoApp(home: withTraits(Brightness.dark, CupertinoUserInterfaceLevelData.elevated, true)));
    expect(find.byType(CupertinoSlider), paints..rrect(color: activeColor.darkHighContrastElevatedColor));

    await tester.pumpWidget(CupertinoApp(home: withTraits(Brightness.light, CupertinoUserInterfaceLevelData.base, true)));
    expect(find.byType(CupertinoSlider), paints..rrect(color: activeColor.highContrastColor));

    await tester.pumpWidget(CupertinoApp(home: withTraits(Brightness.light, CupertinoUserInterfaceLevelData.elevated, false)));
    expect(find.byType(CupertinoSlider), paints..rrect(color: activeColor.elevatedColor));

    await tester.pumpWidget(CupertinoApp(home: withTraits(Brightness.light, CupertinoUserInterfaceLevelData.elevated, true)));
    expect(find.byType(CupertinoSlider), paints..rrect(color: activeColor.highContrastElevatedColor));
  });

  testWidgets('track color is dynamic', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        theme: const CupertinoThemeData(brightness: Brightness.light),
        home: Center(
          child: CupertinoSlider(
            activeColor: CupertinoColors.activeGreen,
            onChanged: (double value) { },
            value: 0,
          ),
        ),
      ),
    );

    expect(
      find.byType(CupertinoSlider),
537
      paints..rrect(color: _kSystemFill.color),
538 539 540 541
    );

    expect(
      find.byType(CupertinoSlider),
542
      isNot(paints..rrect(color: _kSystemFill.darkColor)),
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    );

    await tester.pumpWidget(
      CupertinoApp(
        theme: const CupertinoThemeData(brightness: Brightness.dark),
        home: Center(
          child: CupertinoSlider(
            activeColor: CupertinoColors.activeGreen,
            onChanged: (double value) { },
            value: 0,
          ),
        ),
      ),
    );

    expect(
      find.byType(CupertinoSlider),
560
      paints..rrect(color: _kSystemFill.darkColor),
561 562 563 564
    );

    expect(
      find.byType(CupertinoSlider),
565
      isNot(paints..rrect(color: _kSystemFill.color)),
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

  testWidgets('Thumb color can be overridden', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: CupertinoSlider(
            thumbColor: CupertinoColors.systemPurple,
            onChanged: (double value) { },
            value: 0,
          ),
        ),
      ),
    );

    expect(
      find.byType(CupertinoSlider),
      paints
      ..rrect()
      ..rrect()
      ..rrect()
      ..rrect()
      ..rrect()
      ..rrect(color: CupertinoColors.systemPurple.color)
    );

    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: CupertinoSlider(
            thumbColor: CupertinoColors.activeOrange,
            onChanged: (double value) { },
            value: 0,
          ),
        ),
      ),
    );

    expect(
        find.byType(CupertinoSlider),
        paints
          ..rrect()
          ..rrect()
          ..rrect()
          ..rrect()
          ..rrect()
          ..rrect(color: CupertinoColors.activeOrange.color)
    );
  });
616
}