date_picker_test.dart 45.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
// @dart = 2.8

7 8
import 'dart:ui';

9
import 'package:flutter/cupertino.dart';
10
import 'package:flutter/rendering.dart';
11
import 'package:flutter/semantics.dart';
12 13
import 'package:flutter_test/flutter_test.dart';

14
// scrolling by this offset will move the picker to the next item
15
const Offset _kRowOffset = Offset(0.0, -50.0);
16

17 18 19 20 21
void main() {
  group('Countdown timer picker', () {
    testWidgets('onTimerDurationChanged is not null', (WidgetTester tester) async {
      expect(
        () {
22
          CupertinoTimerPicker(onTimerDurationChanged: null);
23 24 25 26 27 28 29 30
        },
        throwsAssertionError,
      );
    });

    testWidgets('initialTimerDuration falls within limit', (WidgetTester tester) async {
      expect(
        () {
31
          CupertinoTimerPicker(
32
            onTimerDurationChanged: (_) { },
33 34 35 36 37 38 39 40
            initialTimerDuration: const Duration(days: 1),
          );
        },
        throwsAssertionError,
      );

      expect(
        () {
41
          CupertinoTimerPicker(
42
            onTimerDurationChanged: (_) { },
43 44 45 46 47 48 49 50 51 52
            initialTimerDuration: const Duration(seconds: -1),
          );
        },
        throwsAssertionError,
      );
    });

    testWidgets('minuteInterval is positive and is a factor of 60', (WidgetTester tester) async {
      expect(
        () {
53
          CupertinoTimerPicker(
54
            onTimerDurationChanged: (_) { },
55 56 57 58 59 60 61
            minuteInterval: 0,
          );
        },
        throwsAssertionError,
      );
      expect(
        () {
62
          CupertinoTimerPicker(
63
            onTimerDurationChanged: (_) { },
64 65 66 67 68 69 70
            minuteInterval: -1,
          );
        },
        throwsAssertionError,
      );
      expect(
        () {
71
          CupertinoTimerPicker(
72
            onTimerDurationChanged: (_) { },
73 74 75 76 77 78 79 80 81 82
            minuteInterval: 7,
          );
        },
        throwsAssertionError,
      );
    });

    testWidgets('secondInterval is positive and is a factor of 60', (WidgetTester tester) async {
      expect(
        () {
83
          CupertinoTimerPicker(
84
            onTimerDurationChanged: (_) { },
85 86 87 88 89 90 91
            secondInterval: 0,
          );
        },
        throwsAssertionError,
      );
      expect(
        () {
92
          CupertinoTimerPicker(
93
            onTimerDurationChanged: (_) { },
94 95 96 97 98 99 100
            secondInterval: -1,
          );
        },
        throwsAssertionError,
      );
      expect(
        () {
101
          CupertinoTimerPicker(
102
            onTimerDurationChanged: (_) { },
103 104 105 106 107 108 109
            secondInterval: 7,
          );
        },
        throwsAssertionError,
      );
    });

110 111 112 113 114 115 116 117 118 119
    testWidgets('background color default value', (WidgetTester tester) async {
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTimerPicker(
            onTimerDurationChanged: (_) { },
          ),
        ),
      );

      final Iterable<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
120
      expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != null), false);
121 122
    });

123 124 125 126
    testWidgets('background color can be null', (WidgetTester tester) async {
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTimerPicker(
127 128
            onTimerDurationChanged: (_) { },
            backgroundColor: null,
129 130
          ),
        ),
131
      );
132 133

      expect(tester.takeException(), isNull);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    });

    testWidgets('specified background color is applied', (WidgetTester tester) async {
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoTimerPicker(
            onTimerDurationChanged: (_) { },
            backgroundColor: CupertinoColors.black,
          ),
        ),
      );

      final Iterable<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
      expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false);
    });

150 151
    testWidgets('columns are ordered correctly when text direction is ltr', (WidgetTester tester) async {
      await tester.pumpWidget(
152 153
        CupertinoApp(
          home: CupertinoTimerPicker(
154
            onTimerDurationChanged: (_) { },
155 156 157 158 159 160 161 162 163 164 165 166 167
            initialTimerDuration: const Duration(hours: 12, minutes: 30, seconds: 59),
          ),
        ),
      );

      Offset lastOffset = tester.getTopLeft(find.text('12'));

      expect(tester.getTopLeft(find.text('hours')).dx > lastOffset.dx, true);
      lastOffset = tester.getTopLeft(find.text('hours'));

      expect(tester.getTopLeft(find.text('30')).dx > lastOffset.dx, true);
      lastOffset = tester.getTopLeft(find.text('30'));

168 169
      expect(tester.getTopLeft(find.text('min.')).dx > lastOffset.dx, true);
      lastOffset = tester.getTopLeft(find.text('min.'));
170 171 172 173

      expect(tester.getTopLeft(find.text('59')).dx > lastOffset.dx, true);
      lastOffset = tester.getTopLeft(find.text('59'));

174
      expect(tester.getTopLeft(find.text('sec.')).dx > lastOffset.dx, true);
175 176 177 178
    });

    testWidgets('columns are ordered correctly when text direction is rtl', (WidgetTester tester) async {
      await tester.pumpWidget(
179 180 181 182
        CupertinoApp(
          home: Directionality(
            textDirection: TextDirection.rtl,
            child: CupertinoTimerPicker(
183
              onTimerDurationChanged: (_) { },
184 185
              initialTimerDuration: const Duration(hours: 12, minutes: 30, seconds: 59),
            ),
186 187 188 189 190 191 192 193 194 195 196 197
          ),
        ),
      );

      Offset lastOffset = tester.getTopLeft(find.text('12'));

      expect(tester.getTopLeft(find.text('hours')).dx > lastOffset.dx, false);
      lastOffset = tester.getTopLeft(find.text('hours'));

      expect(tester.getTopLeft(find.text('30')).dx > lastOffset.dx, false);
      lastOffset = tester.getTopLeft(find.text('30'));

198 199
      expect(tester.getTopLeft(find.text('min.')).dx > lastOffset.dx, false);
      lastOffset = tester.getTopLeft(find.text('min.'));
200 201 202 203

      expect(tester.getTopLeft(find.text('59')).dx > lastOffset.dx, false);
      lastOffset = tester.getTopLeft(find.text('59'));

204
      expect(tester.getTopLeft(find.text('sec.')).dx > lastOffset.dx, false);
205 206 207 208
    });

    testWidgets('width of picker is consistent', (WidgetTester tester) async {
      await tester.pumpWidget(
209 210 211 212
        CupertinoApp(
          home: SizedBox(
            height: 400.0,
            width: 400.0,
213
            child: CupertinoTimerPicker(
214
              onTimerDurationChanged: (_) { },
215 216 217 218 219 220 221
              initialTimerDuration: const Duration(hours: 12, minutes: 30, seconds: 59),
            ),
          ),
        ),
      );

      // Distance between the first column and the last column.
222 223 224
      final double distance = tester.getCenter(
        find.text('sec.')).dx - tester.getCenter(find.text('12'),
      ).dx;
225 226

      await tester.pumpWidget(
227 228 229 230
        CupertinoApp(
          home: SizedBox(
            height: 400.0,
            width: 800.0,
231
            child: CupertinoTimerPicker(
232
              onTimerDurationChanged: (_) { },
233 234 235 236 237 238 239 240
              initialTimerDuration: const Duration(hours: 12, minutes: 30, seconds: 59),
            ),
          ),
        ),
      );

      // Distance between the first and the last column should be the same.
      expect(
241
        tester.getCenter(find.text('sec.')).dx - tester.getCenter(find.text('12')).dx,
242 243 244 245
        distance,
      );
    });
  });
246 247 248 249 250 251 252 253 254 255

  testWidgets('picker honors minuteInterval and secondInterval', (WidgetTester tester) async {
    Duration duration;
    await tester.pumpWidget(
      CupertinoApp(
        home: SizedBox(
          height: 400.0,
          width: 400.0,
          child: CupertinoTimerPicker(
            minuteInterval: 10,
256 257
            secondInterval: 12,
            initialTimerDuration: const Duration(hours: 10, minutes: 40, seconds: 48),
258 259 260 261 262 263 264 265 266 267 268
            mode: CupertinoTimerPickerMode.hms,
            onTimerDurationChanged: (Duration d) {
              duration = d;
            },
          ),
        ),
      ),
    );

    await tester.drag(find.text('40'), _kRowOffset);
    await tester.pump();
269
    await tester.drag(find.text('48'), -_kRowOffset);
270 271 272 273 274
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

    expect(
      duration,
275
      const Duration(hours: 10, minutes: 50, seconds: 36),
276 277 278
    );
  });

279 280 281 282 283 284
  group('Date picker', () {
    testWidgets('mode is not null', (WidgetTester tester) async {
      expect(
        () {
          CupertinoDatePicker(
            mode: null,
285
            onDateTimeChanged: (_) { },
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            initialDateTime: DateTime.now(),
          );
        },
        throwsAssertionError,
      );
    });

    testWidgets('onDateTimeChanged is not null', (WidgetTester tester) async {
      expect(
        () {
          CupertinoDatePicker(
            onDateTimeChanged: null,
            initialDateTime: DateTime.now(),
          );
        },
        throwsAssertionError,
      );
    });

305 306
    testWidgets('initial date is set to default value', (WidgetTester tester) async {
      final CupertinoDatePicker picker = CupertinoDatePicker(
307
        onDateTimeChanged: (_) { },
308
      );
309
      expect(picker.initialDateTime, isNotNull);
310 311
    });

312 313 314 315 316 317 318 319 320 321
    testWidgets('background color default value', (WidgetTester tester) async {
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoDatePicker(
            onDateTimeChanged: (_) { },
          ),
        ),
      );

      final Iterable<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
322
      expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != null), false);
323 324
    });

325 326 327 328
    testWidgets('background color can be null', (WidgetTester tester) async {
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoDatePicker(
329 330
            onDateTimeChanged: (_) { },
            backgroundColor: null,
331 332
          ),
        ),
333
      );
334 335

      expect(tester.takeException(), isNull);
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    });

    testWidgets('specified background color is applied', (WidgetTester tester) async {
      await tester.pumpWidget(
        CupertinoApp(
          home: CupertinoDatePicker(
            onDateTimeChanged: (_) { },
            backgroundColor: CupertinoColors.black,
          ),
        ),
      );

      final Iterable<CupertinoPicker> pickers = tester.allWidgets.whereType<CupertinoPicker>();
      expect(pickers.any((CupertinoPicker picker) => picker.backgroundColor != CupertinoColors.black), false);
    });

352
    testWidgets('initial date honors minuteInterval', (WidgetTester tester) async {
353 354 355 356 357 358 359 360 361 362 363
      DateTime newDateTime;
      await tester.pumpWidget(
        CupertinoApp(
          home: Center(
            child: SizedBox(
              width: 400,
              height: 400,
              child: CupertinoDatePicker(
                onDateTimeChanged: (DateTime d) => newDateTime = d,
                initialDateTime: DateTime(2018, 10, 10, 10, 3),
                minuteInterval: 3,
364
              ),
365
            ),
366 367
          ),
        ),
368
      );
369

370 371 372 373 374
      // Drag the minute picker to the next slot (03 -> 06).
      // The `initialDateTime` and the `minuteInterval` values are specifically chosen
      // so that `find.text` finds exactly one widget.
      await tester.drag(find.text('03'), _kRowOffset);
      await tester.pump();
375

376
      expect(newDateTime.minute, 6);
377 378
    });

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    test('initial date honors minimumDate & maximumDate', () {
      expect(() {
          CupertinoDatePicker(
            onDateTimeChanged: (DateTime d) { },
            initialDateTime: DateTime(2018, 10, 10),
            minimumDate: DateTime(2018, 10, 11),
          );
        },
        throwsAssertionError,
      );

      expect(() {
          CupertinoDatePicker(
            onDateTimeChanged: (DateTime d) { },
            initialDateTime: DateTime(2018, 10, 10),
            maximumDate: DateTime(2018, 10, 9),
          );
        },
        throwsAssertionError,
      );
    });

401 402 403
    testWidgets('changing initialDateTime after first build does not do anything', (WidgetTester tester) async {
      DateTime selectedDateTime;
      await tester.pumpWidget(
404
        CupertinoApp(
405 406 407 408 409 410 411 412 413
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.dateAndTime,
                onDateTimeChanged: (DateTime dateTime) => selectedDateTime = dateTime,
                initialDateTime: DateTime(2018, 1, 1, 10, 30),
              ),
414 415 416 417 418
            ),
          ),
        ),
      );

419
      await tester.drag(find.text('10'), const Offset(0.0, 32.0), touchSlopY: 0);
420 421 422 423 424 425
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(selectedDateTime, DateTime(2018, 1, 1, 9, 30));

      await tester.pumpWidget(
426
        CupertinoApp(
427 428 429 430 431 432 433 434 435 436
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.dateAndTime,
                onDateTimeChanged: (DateTime dateTime) => selectedDateTime = dateTime,
                // Change the initial date, but it shouldn't affect the present state.
                initialDateTime: DateTime(2016, 4, 5, 15, 00),
              ),
437 438 439 440 441
            ),
          ),
        ),
      );

442
      await tester.drag(find.text('9'), const Offset(0.0, 32.0), touchSlopY: 0);
443 444 445 446 447 448 449
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      // Moving up an hour is still based on the original initial date time.
      expect(selectedDateTime, DateTime(2018, 1, 1, 8, 30));
    });

450 451
    testWidgets('date picker has expected string', (WidgetTester tester) async {
      await tester.pumpWidget(
452
        CupertinoApp(
453 454 455 456 457 458 459 460 461
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.date,
                onDateTimeChanged: (_) { },
                initialDateTime: DateTime(2018, 9, 15, 0, 0),
              ),
462 463 464 465 466 467 468 469 470 471 472 473
            ),
          ),
        ),
      );

      expect(find.text('September'), findsOneWidget);
      expect(find.text('9'), findsOneWidget);
      expect(find.text('2018'), findsOneWidget);
    });

    testWidgets('datetime picker has expected string', (WidgetTester tester) async {
      await tester.pumpWidget(
474
        CupertinoApp(
475 476 477 478 479 480 481 482 483
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.dateAndTime,
                onDateTimeChanged: (_) { },
                initialDateTime: DateTime(2018, 9, 15, 3, 14),
              ),
484 485 486 487 488 489 490 491 492 493 494
            ),
          ),
        ),
      );

      expect(find.text('Sat Sep 15'), findsOneWidget);
      expect(find.text('3'), findsOneWidget);
      expect(find.text('14'), findsOneWidget);
      expect(find.text('AM'), findsOneWidget);
    });

495 496
    testWidgets('width of picker in date and time mode is consistent', (WidgetTester tester) async {
      await tester.pumpWidget(
497 498
        CupertinoApp(
          home: Directionality(
499 500 501
            textDirection: TextDirection.ltr,
            child: CupertinoDatePicker(
              mode: CupertinoDatePickerMode.dateAndTime,
502
              onDateTimeChanged: (_) { },
503 504 505 506 507 508 509 510
              initialDateTime: DateTime(2018, 1, 1, 10, 30),
            ),
          ),
        ),
      );

      // Distance between the first column and the last column.
      final double distance =
511
          tester.getCenter(find.text('Mon Jan 1 ')).dx - tester.getCenter(find.text('AM')).dx;
512 513

      await tester.pumpWidget(
514
        CupertinoApp(
515 516 517 518 519 520 521 522 523
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 800.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.dateAndTime,
                onDateTimeChanged: (_) { },
                initialDateTime: DateTime(2018, 1, 1, 10, 30),
              ),
524 525 526 527 528 529 530
            ),
          ),
        ),
      );

      // Distance between the first and the last column should be the same.
      expect(
531
        tester.getCenter(find.text('Mon Jan 1 ')).dx - tester.getCenter(find.text('AM')).dx,
532 533 534 535 536 537
        distance,
      );
    });

    testWidgets('width of picker in date mode is consistent', (WidgetTester tester) async {
      await tester.pumpWidget(
538
        CupertinoApp(
539 540 541 542 543 544 545 546 547
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.date,
                onDateTimeChanged: (_) { },
                initialDateTime: DateTime(2018, 1, 1, 10, 30),
              ),
548 549 550 551 552 553 554 555 556 557
            ),
          ),
        ),
      );

      // Distance between the first column and the last column.
      final double distance =
          tester.getCenter(find.text('January')).dx - tester.getCenter(find.text('2018')).dx;

      await tester.pumpWidget(
558
        CupertinoApp(
559 560 561 562 563 564 565 566 567
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 800.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.date,
                onDateTimeChanged: (_) { },
                initialDateTime: DateTime(2018, 1, 1, 10, 30),
              ),
568 569 570 571 572 573 574 575 576 577 578 579 580 581
            ),
          ),
        ),
      );

      // Distance between the first and the last column should be the same.
      expect(
        tester.getCenter(find.text('January')).dx - tester.getCenter(find.text('2018')).dx,
        distance,
      );
    });

    testWidgets('width of picker in time mode is consistent', (WidgetTester tester) async {
      await tester.pumpWidget(
582
        CupertinoApp(
583 584 585 586 587 588 589 590 591
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.time,
                onDateTimeChanged: (_) { },
                initialDateTime: DateTime(2018, 1, 1, 10, 30),
              ),
592 593 594 595 596 597 598 599 600 601
            ),
          ),
        ),
      );

      // Distance between the first column and the last column.
      final double distance =
          tester.getCenter(find.text('10')).dx - tester.getCenter(find.text('AM')).dx;

      await tester.pumpWidget(
602
        CupertinoApp(
603 604 605 606 607 608 609 610 611
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 800.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.time,
                onDateTimeChanged: (_) { },
                initialDateTime: DateTime(2018, 1, 1, 10, 30),
              ),
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
            ),
          ),
        ),
      );

      // Distance between the first and the last column should be the same.
      expect(
        tester.getCenter(find.text('10')).dx - tester.getCenter(find.text('AM')).dx,
        distance,
      );
    });

    testWidgets('picker automatically scrolls away from invalid date on month change', (WidgetTester tester) async {
      DateTime date;
      await tester.pumpWidget(
627
        CupertinoApp(
628 629 630 631 632 633 634 635 636 637 638
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.date,
                onDateTimeChanged: (DateTime newDate) {
                  date = newDate;
                },
                initialDateTime: DateTime(2018, 3, 30),
              ),
639 640 641 642 643
            ),
          ),
        ),
      );

644 645
      await tester.drag(find.text('March'), const Offset(0, 32.0), touchSlopY: 0.0);

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
      // Momentarily, the 2018 and the incorrect 30 of February is aligned.
      expect(
        tester.getTopLeft(find.text('2018')).dy,
        tester.getTopLeft(find.text('30')).dy,
      );
      await tester.pump(); // Once to trigger the post frame animate call.
      await tester.pump(); // Once to start the DrivenScrollActivity.
      await tester.pump(const Duration(milliseconds: 500));

      expect(
        date,
        DateTime(2018, 2, 28),
      );
      expect(
        tester.getTopLeft(find.text('2018')).dy,
        tester.getTopLeft(find.text('28')).dy,
      );
    });

665
    testWidgets(
666
      'date picker automatically scrolls away from invalid date, '
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
      "and onDateTimeChanged doesn't report these dates",
      (WidgetTester tester) async {
        DateTime date;
        // 2016 is a leap year.
        final DateTime minimum = DateTime(2016, 2, 29);
        final DateTime maximum = DateTime(2018, 12, 31);
        await tester.pumpWidget(
          CupertinoApp(
            home: Center(
              child: SizedBox(
                height: 400.0,
                width: 400.0,
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.date,
                  minimumDate: minimum,
                  maximumDate: maximum,
                  onDateTimeChanged: (DateTime newDate) {
                    date = newDate;
                    // Callback doesn't transiently go into invalid dates.
                    expect(newDate.isAtSameMomentAs(minimum) || newDate.isAfter(minimum), isTrue);
                    expect(newDate.isAtSameMomentAs(maximum) || newDate.isBefore(maximum), isTrue);
                  },
                  initialDateTime: DateTime(2017, 2, 28),
                ),
              ),
            ),
          ),
        );

        // 2017 has 28 days in Feb so 29 is greyed out.
        expect(
          tester.widget<Text>(find.text('29')).style.color,
699
          isSameColorAs(CupertinoColors.inactiveGray.color),
700 701 702 703 704 705 706 707 708 709 710 711 712 713
        );

        await tester.drag(find.text('2017'), const Offset(0.0, 32.0), touchSlopY: 0.0);
        await tester.pump();
        await tester.pumpAndSettle(); // Now the autoscrolling should happen.

        expect(
          date,
          DateTime(2016, 2, 29),
        );

        // 2016 has 29 days in Feb so 29 is not greyed out.
        expect(
          tester.widget<Text>(find.text('29')).style.color,
714
          isNot(isSameColorAs(CupertinoColors.inactiveGray.color)),
715 716 717 718 719 720 721 722 723 724 725 726 727
        );

        await tester.drag(find.text('2016'), const Offset(0.0, -32.0), touchSlopY: 0.0);
        await tester.pump(); // Once to trigger the post frame animate call.
        await tester.pumpAndSettle();

        expect(
          date,
          DateTime(2017, 2, 28),
        );

        expect(
          tester.widget<Text>(find.text('29')).style.color,
728
          isSameColorAs(CupertinoColors.inactiveGray.color),
729 730 731
        );
    });

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
    testWidgets(
      'dateTime picker automatically scrolls away from invalid date, '
      "and onDateTimeChanged doesn't report these dates",
      (WidgetTester tester) async {
        DateTime date;
        final DateTime minimum = DateTime(2019, 11, 11, 3, 30);
        final DateTime maximum = DateTime(2019, 11, 11, 14, 59, 59);
        await tester.pumpWidget(
          CupertinoApp(
            home: Center(
              child: SizedBox(
                height: 400.0,
                width: 400.0,
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.dateAndTime,
                  minimumDate: minimum,
                  maximumDate: maximum,
                  onDateTimeChanged: (DateTime newDate) {
                    date = newDate;
                    // Callback doesn't transiently go into invalid dates.
                    expect(minimum.isAfter(newDate), isFalse);
                    expect(maximum.isBefore(newDate), isFalse);
                  },
                  initialDateTime: DateTime(2019, 11, 11, 4),
                ),
              ),
            ),
          ),
        );

        // 3:00 is valid but 2:00 should be invalid.
        expect(
          tester.widget<Text>(find.text('3')).style.color,
          isNot(isSameColorAs(CupertinoColors.inactiveGray.color)),
        );

        expect(
          tester.widget<Text>(find.text('2')).style.color,
          isSameColorAs(CupertinoColors.inactiveGray.color),
        );

        // 'PM' is greyed out.
        expect(
          tester.widget<Text>(find.text('PM')).style.color,
          isSameColorAs(CupertinoColors.inactiveGray.color),
        );

        await tester.drag(find.text('AM'), const Offset(0.0, -32.0), touchSlopY: 0.0);
        await tester.pump();
        await tester.pumpAndSettle(); // Now the autoscrolling should happen.

        expect(
          date,
          DateTime(2019, 11, 11, 14, 59),
        );

        // 3'o clock and 'AM' are now greyed out.
        expect(
          tester.widget<Text>(find.text('AM')).style.color,
          isSameColorAs(CupertinoColors.inactiveGray.color),
        );
        expect(
          tester.widget<Text>(find.text('3')).style.color,
          isSameColorAs(CupertinoColors.inactiveGray.color),
        );

        await tester.drag(find.text('PM'), const Offset(0.0, 32.0), touchSlopY: 0.0);
        await tester.pump(); // Once to trigger the post frame animate call.
        await tester.pumpAndSettle();

        // Returns to min date.
        expect(
          date,
          DateTime(2019, 11, 11, 3, 30),
        );
    });

    testWidgets(
      'time picker automatically scrolls away from invalid date, '
      "and onDateTimeChanged doesn't report these dates",
      (WidgetTester tester) async {
        DateTime date;
        final DateTime minimum = DateTime(2019, 11, 11, 3, 30);
        final DateTime maximum = DateTime(2019, 11, 11, 14, 59, 59);
        await tester.pumpWidget(
          CupertinoApp(
            home: Center(
              child: SizedBox(
                height: 400.0,
                width: 400.0,
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.time,
                  minimumDate: minimum,
                  maximumDate: maximum,
                  onDateTimeChanged: (DateTime newDate) {
                    date = newDate;
                    // Callback doesn't transiently go into invalid dates.
                    expect(minimum.isAfter(newDate), isFalse);
                    expect(maximum.isBefore(newDate), isFalse);
                  },
                  initialDateTime: DateTime(2019, 11, 11, 4),
                ),
              ),
            ),
          ),
        );

        // 3:00 is valid but 2:00 should be invalid.
        expect(
          tester.widget<Text>(find.text('3')).style.color,
          isNot(isSameColorAs(CupertinoColors.inactiveGray.color)),
        );

        expect(
          tester.widget<Text>(find.text('2')).style.color,
          isSameColorAs(CupertinoColors.inactiveGray.color),
        );

        // 'PM' is greyed out.
        expect(
          tester.widget<Text>(find.text('PM')).style.color,
          isSameColorAs(CupertinoColors.inactiveGray.color),
        );

        await tester.drag(find.text('AM'), const Offset(0.0, -32.0), touchSlopY: 0.0);
        await tester.pump();
        await tester.pumpAndSettle(); // Now the autoscrolling should happen.

        expect(
          date,
          DateTime(2019, 11, 11, 14, 59),
        );

        // 3'o clock and 'AM' are now greyed out.
        expect(
          tester.widget<Text>(find.text('AM')).style.color,
          isSameColorAs(CupertinoColors.inactiveGray.color),
        );
        expect(
          tester.widget<Text>(find.text('3')).style.color,
          isSameColorAs(CupertinoColors.inactiveGray.color),
        );

        await tester.drag(find.text('PM'), const Offset(0.0, 32.0), touchSlopY: 0.0);
        await tester.pump(); // Once to trigger the post frame animate call.
        await tester.pumpAndSettle();

        // Returns to min date.
        expect(
          date,
          DateTime(2019, 11, 11, 3, 30),
        );
    });

886 887 888
    testWidgets('picker automatically scrolls away from invalid date on day change', (WidgetTester tester) async {
      DateTime date;
      await tester.pumpWidget(
889
        CupertinoApp(
890 891 892 893 894 895 896 897 898 899 900
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.date,
                onDateTimeChanged: (DateTime newDate) {
                  date = newDate;
                },
                initialDateTime: DateTime(2018, 2, 27), // 2018 has 28 days in Feb.
              ),
901 902 903 904 905
            ),
          ),
        ),
      );

906
      await tester.drag(find.text('27'), const Offset(0.0, -32.0), touchSlopY: 0.0);
907 908 909 910 911 912
      await tester.pump();
      expect(
        date,
        DateTime(2018, 2, 28),
      );

913
      await tester.drag(find.text('28'), const Offset(0.0, -32.0), touchSlopY: 0.0);
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
      await tester.pump(); // Once to trigger the post frame animate call.

      // Callback doesn't transiently go into invalid dates.
      expect(
        date,
        DateTime(2018, 2, 28),
      );
      // Momentarily, the invalid 29th of Feb is dragged into the middle.
      expect(
        tester.getTopLeft(find.text('2018')).dy,
        tester.getTopLeft(find.text('29')).dy,
      );

      await tester.pump(); // Once to start the DrivenScrollActivity.
      await tester.pump(const Duration(milliseconds: 500));

      expect(
        date,
        DateTime(2018, 2, 28),
      );
      expect(
        tester.getTopLeft(find.text('2018')).dy,
        tester.getTopLeft(find.text('28')).dy,
      );
    });

940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    testWidgets(
      'date picker should only take into account the date part of minimumDate and maximumDate',
      (WidgetTester tester) async {
        // Regression test for https://github.com/flutter/flutter/issues/49606.
        DateTime date;
        final DateTime minDate = DateTime(2020, 1, 1, 12);
        await tester.pumpWidget(
          CupertinoApp(
            home: Center(
              child: SizedBox(
                height: 400.0,
                width: 400.0,
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.date,
                  minimumDate: minDate,
                  onDateTimeChanged: (DateTime newDate) { date = newDate; },
                  initialDateTime: DateTime(2020, 1, 12),
                ),
              ),
            ),
          ),
        );

        // Scroll to 2019.
        await tester.drag(find.text('2020'), const Offset(0.0, 32.0), touchSlopY: 0.0);
        await tester.pump();
        await tester.pumpAndSettle();
        expect(date.year, minDate.year);
        expect(date.month, minDate.month);
        expect(date.day, minDate.day);
    });


973 974 975 976 977
    group('Picker handles initial noon/midnight times', () {
      testWidgets('midnight', (WidgetTester tester) async {
        DateTime date;
        await tester.pumpWidget(
          CupertinoApp(
978 979 980 981 982 983 984 985 986 987 988
            home: Center(
              child: SizedBox(
                height: 400.0,
                width: 400.0,
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.time,
                  onDateTimeChanged: (DateTime newDate) {
                    date = newDate;
                  },
                  initialDateTime: DateTime(2019, 1, 1, 0, 15),
                ),
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
              ),
            ),
          ),
        );

        // 0:15 -> 0:16
        await tester.drag(find.text('15'), _kRowOffset);
        await tester.pump();
        await tester.pump(const Duration(milliseconds: 500));

        expect(date, DateTime(2019, 1, 1, 0, 16));
      });

      testWidgets('noon', (WidgetTester tester) async {
        DateTime date;
        await tester.pumpWidget(
          CupertinoApp(
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
            home: Center(
              child: SizedBox(
                height: 400.0,
                width: 400.0,
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.time,
                  onDateTimeChanged: (DateTime newDate) {
                    date = newDate;
                  },
                  initialDateTime: DateTime(2019, 1, 1, 12, 15),
                ),
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
              ),
            ),
          ),
        );

        // 12:15 -> 12:16
        await tester.drag(find.text('15'), _kRowOffset);
        await tester.pump();
        await tester.pump(const Duration(milliseconds: 500));

        expect(date, DateTime(2019, 1, 1, 12, 16));
      });

      testWidgets('noon in 24 hour time', (WidgetTester tester) async {
        DateTime date;
        await tester.pumpWidget(
          CupertinoApp(
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
            home: Center(
              child: SizedBox(
                height: 400.0,
                width: 400.0,
                child: CupertinoDatePicker(
                  use24hFormat: true,
                  mode: CupertinoDatePickerMode.time,
                  onDateTimeChanged: (DateTime newDate) {
                    date = newDate;
                  },
                  initialDateTime: DateTime(2019, 1, 1, 12, 25),
                ),
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
              ),
            ),
          ),
        );

        // 12:25 -> 12:26
        await tester.drag(find.text('25'), _kRowOffset);
        await tester.pump();
        await tester.pump(const Duration(milliseconds: 500));

        expect(date, DateTime(2019, 1, 1, 12, 26));
      });
    });

1060 1061 1062 1063
    testWidgets('picker persists am/pm value when scrolling hours', (WidgetTester tester) async {
      DateTime date;
      await tester.pumpWidget(
        CupertinoApp(
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.time,
                onDateTimeChanged: (DateTime newDate) {
                  date = newDate;
                },
                initialDateTime: DateTime(2019, 1, 1, 3),
              ),
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
            ),
          ),
        ),
      );

      // 3:00 -> 15:00
      await tester.drag(find.text('AM'), _kRowOffset);
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(date, DateTime(2019, 1, 1, 15));

      // 15:00 -> 16:00
      await tester.drag(find.text('3'), _kRowOffset);
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(date, DateTime(2019, 1, 1, 16));

      // 16:00 -> 4:00
      await tester.drag(find.text('PM'), -_kRowOffset);
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(date, DateTime(2019, 1, 1, 4));

      // 4:00 -> 3:00
      await tester.drag(find.text('4'), -_kRowOffset);
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(date, DateTime(2019, 1, 1, 3));
    });

1109 1110 1111
    testWidgets('picker automatically scrolls the am/pm column when the hour column changes enough', (WidgetTester tester) async {
      DateTime date;
      await tester.pumpWidget(
1112
        CupertinoApp(
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
          home: Center(
            child: SizedBox(
              height: 400.0,
              width: 400.0,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.time,
                onDateTimeChanged: (DateTime newDate) {
                  date = newDate;
                },
                initialDateTime: DateTime(2018, 1, 1, 11, 59),
              ),
1124 1125 1126 1127 1128
            ),
          ),
        ),
      );

1129 1130
      const Offset deltaOffset = Offset(0.0, -18.0);

1131 1132
      // 11:59 -> 12:59
      await tester.drag(find.text('11'), _kRowOffset);
1133 1134 1135 1136 1137
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(date, DateTime(2018, 1, 1, 12, 59));

1138 1139
      // 12:59 -> 11:59
      await tester.drag(find.text('12'), -_kRowOffset);
1140 1141 1142 1143 1144
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(date, DateTime(2018, 1, 1, 11, 59));

1145
      // 11:59 -> 9:59
1146
      await tester.drag(find.text('11'), -((_kRowOffset - deltaOffset) * 2 + deltaOffset));
1147 1148 1149 1150 1151
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(date, DateTime(2018, 1, 1, 9, 59));

1152
      // 9:59 -> 15:59
1153
      await tester.drag(find.text('9'), (_kRowOffset - deltaOffset) * 6 + deltaOffset);
1154 1155 1156 1157 1158
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      expect(date, DateTime(2018, 1, 1, 15, 59));
    });
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

    testWidgets('date picker given too narrow space horizontally shows message', (WidgetTester tester) async {
      await tester.pumpWidget(
        CupertinoApp(
          home: Center(
            child: SizedBox(
              // This is too small to draw the picker out fully.
              width: 100,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.dateAndTime,
                initialDateTime: DateTime(2019, 1, 1, 4),
                onDateTimeChanged: (_) {},
1171
              ),
1172
            ),
1173 1174
          ),
        ),
1175 1176 1177
      );

      final dynamic exception = tester.takeException();
1178
      expect(exception, isFlutterError);
1179 1180 1181 1182 1183 1184
      expect(
        exception.toString(),
        contains('Insufficient horizontal space to render the CupertinoDatePicker'),
      );
    });

1185 1186 1187 1188 1189
    testWidgets('DatePicker golden tests', (WidgetTester tester) async {
      await tester.pumpWidget(
        CupertinoApp(
          home: Center(
            child: SizedBox(
1190
              width: 500,
1191 1192 1193 1194 1195 1196 1197
              height: 400,
              child: RepaintBoundary(
                child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.dateAndTime,
                  initialDateTime: DateTime(2019, 1, 1, 4),
                  onDateTimeChanged: (_) {},
                ),
1198
              ),
1199
            ),
1200 1201
          ),
        ),
1202 1203 1204 1205
      );

      await expectLater(
        find.byType(CupertinoDatePicker),
1206
        matchesGoldenFile('date_picker_test.datetime.initial.png'),
1207 1208 1209 1210 1211 1212 1213 1214
      );

      // Slightly drag the hour component to make the current hour off-center.
      await tester.drag(find.text('4'), Offset(0, _kRowOffset.dy / 2));
      await tester.pump();

      await expectLater(
        find.byType(CupertinoDatePicker),
1215
        matchesGoldenFile('date_picker_test.datetime.drag.png'),
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
  testWidgets('TimerPicker golden tests', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        // Also check if the picker respects the theme.
        theme: const CupertinoThemeData(
          textTheme: CupertinoTextThemeData(
            pickerTextStyle: TextStyle(
              color: Color(0xFF663311),
            ),
          ),
        ),
        home: Center(
          child: SizedBox(
            width: 320,
            height: 216,
            child: RepaintBoundary(
              child: CupertinoTimerPicker(
                mode: CupertinoTimerPickerMode.hm,
                initialTimerDuration: const Duration(hours: 23, minutes: 59),
                onTimerDurationChanged: (_) {},
              ),
1241
            ),
1242 1243 1244 1245 1246 1247 1248
          ),
        ),
      ),
    );

    await expectLater(
      find.byType(CupertinoTimerPicker),
1249
      matchesGoldenFile('timer_picker_test.datetime.initial.png'),
1250 1251 1252 1253 1254 1255 1256 1257
    );

    // Slightly drag the minute component to make the current minute off-center.
    await tester.drag(find.text('59'), Offset(0, _kRowOffset.dy / 2));
    await tester.pump();

    await expectLater(
      find.byType(CupertinoTimerPicker),
1258
      matchesGoldenFile('timer_picker_test.datetime.drag.png'),
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 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 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
    );
  });

  testWidgets('TimerPicker only changes hour label after scrolling stops', (WidgetTester tester) async {
    Duration duration;
    await tester.pumpWidget(
      CupertinoApp(
        home: Center(
          child: SizedBox(
            width: 320,
            height: 216,
            child: CupertinoTimerPicker(
              mode: CupertinoTimerPickerMode.hm,
              initialTimerDuration: const Duration(hours: 2, minutes: 30),
              onTimerDurationChanged: (Duration d) { duration = d; },
            ),
          ),
        ),
      ),
    );

    expect(duration, isNull);
    expect(find.text('hour'), findsNothing);
    expect(find.text('hours'), findsOneWidget);

    await tester.drag(find.text('2'), Offset(0, -_kRowOffset.dy));
    // Duration should change but not the label.
    expect(duration?.inHours, 1);
    expect(find.text('hour'), findsNothing);
    expect(find.text('hours'), findsOneWidget);
    await tester.pumpAndSettle();

    // Now the label should change.
    expect(duration?.inHours, 1);
    expect(find.text('hours'), findsNothing);
    expect(find.text('hour'), findsOneWidget);
  });

  testWidgets('TimerPicker has intrinsic width and height', (WidgetTester tester) async {
    const Key key = Key('key');

    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTimerPicker(
          key: key,
          mode: CupertinoTimerPickerMode.hm,
          initialTimerDuration: const Duration(hours: 2, minutes: 30),
          onTimerDurationChanged: (Duration d) {},
        ),
      ),
    );

    expect(tester.getSize(find.descendant(of: find.byKey(key), matching: find.byType(Row))), const Size(320, 216));

    // Different modes shouldn't share state.
    await tester.pumpWidget(const Placeholder());
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTimerPicker(
          key: key,
          mode: CupertinoTimerPickerMode.ms,
          initialTimerDuration: const Duration(minutes: 30, seconds: 3),
          onTimerDurationChanged: (Duration d) {},
        ),
      ),
    );

    expect(tester.getSize(find.descendant(of: find.byKey(key), matching: find.byType(Row))), const Size(320, 216));

    // Different modes shouldn't share state.
    await tester.pumpWidget(const Placeholder());
    await tester.pumpWidget(
      CupertinoApp(
        home: CupertinoTimerPicker(
          key: key,
          mode: CupertinoTimerPickerMode.hms,
          initialTimerDuration: const Duration(hours: 5, minutes: 17, seconds: 19),
          onTimerDurationChanged: (Duration d) {},
        ),
      ),
    );

    expect(tester.getSize(find.descendant(of: find.byKey(key), matching: find.byType(Row))), const Size(330, 216));
  });

1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
  testWidgets('scrollController can be removed or added', (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
    int lastSelectedItem;
    void onSelectedItemChanged(int index) {
      lastSelectedItem = index;
    }
    await tester.pumpWidget(_buildPicker(
      controller: FixedExtentScrollController(),
      onSelectedItemChanged: onSelectedItemChanged,
    ));

    tester.binding.pipelineOwner.semanticsOwner.performAction(1, SemanticsAction.increase);
    await tester.pumpAndSettle();
    expect(lastSelectedItem, 1);

    await tester.pumpWidget(_buildPicker(
      onSelectedItemChanged: onSelectedItemChanged,
    ));

    tester.binding.pipelineOwner.semanticsOwner.performAction(1, SemanticsAction.increase);
    await tester.pumpAndSettle();
    expect(lastSelectedItem, 2);

    await tester.pumpWidget(_buildPicker(
      controller: FixedExtentScrollController(),
      onSelectedItemChanged: onSelectedItemChanged,
    ));

    tester.binding.pipelineOwner.semanticsOwner.performAction(1, SemanticsAction.increase);
    await tester.pumpAndSettle();
    expect(lastSelectedItem, 3);

    handle.dispose();
  });

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
  testWidgets('CupertinoDataPicker does not provide invalid MediaQuery', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/47989.
    Brightness brightness = Brightness.light;
    StateSetter setState;

    await tester.pumpWidget(
      CupertinoApp(
        theme: const CupertinoThemeData(
          textTheme: CupertinoTextThemeData(
            dateTimePickerTextStyle: TextStyle(
              color: CupertinoDynamicColor.withBrightness(
                color: Color(0xFFFFFFFF),
                darkColor: Color(0xFF000000),
              ),
            ),
          ),
        ),
        home: StatefulBuilder(builder: (BuildContext context, StateSetter stateSetter) {
          setState = stateSetter;
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(platformBrightness: brightness),
            child: CupertinoDatePicker(
              initialDateTime: DateTime(2019),
              mode: CupertinoDatePickerMode.date,
              onDateTimeChanged: (DateTime date) {},
            ),
          );
        }),
      ),
    );

    expect(
      tester.widget<Text>(find.text('2019')).style.color,
      isSameColorAs(const Color(0xFFFFFFFF)),
    );

    setState(() { brightness = Brightness.dark; });
    await tester.pump();

    expect(
      tester.widget<Text>(find.text('2019')).style.color,
      isSameColorAs(const Color(0xFF000000)),
    );
  });

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
  testWidgets('picker exports semantics', (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
    debugResetSemanticsIdCounter();
    int lastSelectedItem;
    await tester.pumpWidget(_buildPicker(onSelectedItemChanged: (int index) {
      lastSelectedItem = index;
    }));

    expect(tester.getSemantics(find.byType(CupertinoPicker)), matchesSemantics(
      children: <Matcher>[
        matchesSemantics(
          hasIncreaseAction: true,
          hasDecreaseAction: false,
          increasedValue: '1',
          value: '0',
          textDirection: TextDirection.ltr,
        ),
      ],
    ));

    tester.binding.pipelineOwner.semanticsOwner.performAction(1, SemanticsAction.increase);
    await tester.pumpAndSettle();

    expect(tester.getSemantics(find.byType(CupertinoPicker)), matchesSemantics(
      children: <Matcher>[
        matchesSemantics(
          hasIncreaseAction: true,
          hasDecreaseAction: true,
          increasedValue: '2',
          decreasedValue: '0',
          value: '1',
          textDirection: TextDirection.ltr,
        ),
      ],
    ));
    expect(lastSelectedItem, 1);
    handle.dispose();
  });
}

1464
Widget _buildPicker({ FixedExtentScrollController controller, ValueChanged<int> onSelectedItemChanged }) {
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
  return Directionality(
    textDirection: TextDirection.ltr,
    child: CupertinoPicker(
      scrollController: controller,
      itemExtent: 100.0,
      onSelectedItemChanged: onSelectedItemChanged,
      children: List<Widget>.generate(100, (int index) {
        return Center(
          child: Container(
            width: 400.0,
            height: 100.0,
            child: Text(index.toString()),
          ),
        );
      }),
    ),
  );
}