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

5 6
import 'dart:ui';

7
import 'package:flutter/rendering.dart';
Ian Hickson's avatar
Ian Hickson committed
8
import 'package:flutter/material.dart';
9
import 'package:flutter/widgets.dart';
Ian Hickson's avatar
Ian Hickson committed
10 11
import 'package:flutter_test/flutter_test.dart';

12
import '../widgets/semantics_tester.dart';
13 14
import 'feedback_tester.dart';

Ian Hickson's avatar
Ian Hickson committed
15
void main() {
16 17 18 19 20 21
  group('showDatePicker', () {
    _tests();
  });
}

void _tests() {
22 23 24
  DateTime firstDate;
  DateTime lastDate;
  DateTime initialDate;
25
  SelectableDayPredicate selectableDayPredicate;
26
  DatePickerMode initialDatePickerMode;
27 28
  final Finder nextMonthIcon = find.byWidgetPredicate((Widget w) => w is IconButton && (w.tooltip?.startsWith('Next month') ?? false));
  final Finder previousMonthIcon = find.byWidgetPredicate((Widget w) => w is IconButton && (w.tooltip?.startsWith('Previous month') ?? false));
29 30

  setUp(() {
31 32 33
    firstDate = DateTime(2001, DateTime.january, 1);
    lastDate = DateTime(2031, DateTime.december, 31);
    initialDate = DateTime(2016, DateTime.january, 15);
34 35
    selectableDayPredicate = null;
    initialDatePickerMode = null;
36 37
  });

38
  testWidgets('tap-select a day', (WidgetTester tester) async {
39 40
    final Key _datePickerKey = UniqueKey();
    DateTime _selectedDate = DateTime(2016, DateTime.july, 26);
Ian Hickson's avatar
Ian Hickson committed
41 42

    await tester.pumpWidget(
43 44
      MaterialApp(
        home: StatefulBuilder(
45
          builder: (BuildContext context, StateSetter setState) {
46
            return Container(
47
              width: 400.0,
48 49 50 51 52
              child: SingleChildScrollView(
                child: Material(
                  child: MonthPicker(
                    firstDate: DateTime(0),
                    lastDate: DateTime(9999),
53 54 55 56 57 58 59
                    key: _datePickerKey,
                    selectedDate: _selectedDate,
                    onChanged: (DateTime value) {
                      setState(() {
                        _selectedDate = value;
                      });
                    },
60
                  ),
61 62 63 64 65 66
                ),
              ),
            );
          },
        ),
      )
Ian Hickson's avatar
Ian Hickson committed
67
    );
68

69
    expect(_selectedDate, equals(DateTime(2016, DateTime.july, 26)));
Ian Hickson's avatar
Ian Hickson committed
70

71
    await tester.tapAt(const Offset(50.0, 100.0));
72
    await tester.pumpAndSettle();
Ian Hickson's avatar
Ian Hickson committed
73 74
    await tester.pump(const Duration(seconds: 2));

75 76
    await tester.tap(find.text('1'));
    await tester.pumpAndSettle();
77
    expect(_selectedDate, equals(DateTime(2016, DateTime.july, 1)));
Ian Hickson's avatar
Ian Hickson committed
78

79
    await tester.tap(nextMonthIcon);
80
    await tester.pumpAndSettle();
81
    expect(_selectedDate, equals(DateTime(2016, DateTime.july, 1)));
Ian Hickson's avatar
Ian Hickson committed
82

83 84
    await tester.tap(find.text('5'));
    await tester.pumpAndSettle();
85
    expect(_selectedDate, equals(DateTime(2016, DateTime.august, 5)));
Ian Hickson's avatar
Ian Hickson committed
86

87 88
    await tester.drag(find.byKey(_datePickerKey), const Offset(-400.0, 0.0));
    await tester.pumpAndSettle();
89
    expect(_selectedDate, equals(DateTime(2016, DateTime.august, 5)));
Ian Hickson's avatar
Ian Hickson committed
90

91 92
    await tester.tap(find.text('25'));
    await tester.pumpAndSettle();
93
    expect(_selectedDate, equals(DateTime(2016, DateTime.september, 25)));
Ian Hickson's avatar
Ian Hickson committed
94

95 96
    await tester.drag(find.byKey(_datePickerKey), const Offset(800.0, 0.0));
    await tester.pumpAndSettle();
97
    expect(_selectedDate, equals(DateTime(2016, DateTime.september, 25)));
Ian Hickson's avatar
Ian Hickson committed
98

99 100
    await tester.tap(find.text('17'));
    await tester.pumpAndSettle();
101
    expect(_selectedDate, equals(DateTime(2016, DateTime.august, 17)));
Ian Hickson's avatar
Ian Hickson committed
102 103 104 105
  });

  testWidgets('render picker with intrinsic dimensions', (WidgetTester tester) async {
    await tester.pumpWidget(
106 107
      MaterialApp(
        home: StatefulBuilder(
108
          builder: (BuildContext context, StateSetter setState) {
109 110 111 112 113 114 115
            return IntrinsicWidth(
              child: IntrinsicHeight(
                child: Material(
                  child: SingleChildScrollView(
                    child: MonthPicker(
                      firstDate: DateTime(0),
                      lastDate: DateTime(9999),
116
                      onChanged: (DateTime value) { },
117
                      selectedDate: DateTime(2000, DateTime.january, 1),
118 119
                    ),
                  ),
120 121 122 123 124
                ),
              ),
            );
          },
        ),
125
      ),
Ian Hickson's avatar
Ian Hickson committed
126 127 128 129
    );
    await tester.pump(const Duration(seconds: 5));
  });

130
  Future<void> preparePicker(WidgetTester tester, Future<void> callback(Future<DateTime> date)) async {
131
    BuildContext buttonContext;
132 133 134
    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: Builder(
135
          builder: (BuildContext context) {
136
            return RaisedButton(
137 138 139
              onPressed: () {
                buttonContext = context;
              },
140
              child: const Text('Go'),
141 142 143 144 145 146 147 148 149
            );
          },
        ),
      ),
    ));

    await tester.tap(find.text('Go'));
    expect(buttonContext, isNotNull);

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    final Future<DateTime> date = initialDatePickerMode == null
        // Exercise the argument default for initialDatePickerMode.
        ?
            showDatePicker(
              context: buttonContext,
              initialDate: initialDate,
              firstDate: firstDate,
              lastDate: lastDate,
              selectableDayPredicate: selectableDayPredicate,
            )
        :
            showDatePicker(
              context: buttonContext,
              initialDate: initialDate,
              firstDate: firstDate,
              lastDate: lastDate,
              selectableDayPredicate: selectableDayPredicate,
              initialDatePickerMode: initialDatePickerMode,
            );
169

170
    await tester.pumpAndSettle(const Duration(seconds: 1));
171 172 173 174 175 176
    await callback(date);
  }

  testWidgets('Initial date is the default', (WidgetTester tester) async {
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.tap(find.text('OK'));
177
      expect(await date, equals(DateTime(2016, DateTime.january, 15)));
178 179 180 181 182 183 184 185 186 187 188 189 190 191
    });
  });

  testWidgets('Can cancel', (WidgetTester tester) async {
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.tap(find.text('CANCEL'));
      expect(await date, isNull);
    });
  });

  testWidgets('Can select a day', (WidgetTester tester) async {
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.tap(find.text('12'));
      await tester.tap(find.text('OK'));
192
      expect(await date, equals(DateTime(2016, DateTime.january, 12)));
193 194 195 196 197
    });
  });

  testWidgets('Can select a month', (WidgetTester tester) async {
    await preparePicker(tester, (Future<DateTime> date) async {
198
      await tester.tap(previousMonthIcon);
199
      await tester.pumpAndSettle(const Duration(seconds: 1));
200 201
      await tester.tap(find.text('25'));
      await tester.tap(find.text('OK'));
202
      expect(await date, equals(DateTime(2015, DateTime.december, 25)));
203 204 205 206 207 208 209
    });
  });

  testWidgets('Can select a year', (WidgetTester tester) async {
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.tap(find.text('2016'));
      await tester.pump();
210
      await tester.tap(find.text('2018'));
211
      await tester.tap(find.text('OK'));
212
      expect(await date, equals(DateTime(2018, DateTime.january, 15)));
213 214 215 216 217 218 219
    });
  });

  testWidgets('Can select a year and then a day', (WidgetTester tester) async {
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.tap(find.text('2016'));
      await tester.pump();
220
      await tester.tap(find.text('2017'));
221
      await tester.pump();
Yegor's avatar
Yegor committed
222 223 224
      final MaterialLocalizations localizations = MaterialLocalizations.of(
        tester.element(find.byType(DayPicker))
      );
225
      final String dayLabel = localizations.formatMediumDate(DateTime(2017, DateTime.january, 15));
226 227 228 229
      await tester.tap(find.text(dayLabel));
      await tester.pump();
      await tester.tap(find.text('19'));
      await tester.tap(find.text('OK'));
230
      expect(await date, equals(DateTime(2017, DateTime.january, 19)));
231 232 233 234
    });
  });

  testWidgets('Current year is initially visible in year picker', (WidgetTester tester) async {
235 236 237
    initialDate = DateTime(2000);
    firstDate = DateTime(1900);
    lastDate = DateTime(2100);
238 239 240 241
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.tap(find.text('2000'));
      await tester.pump();
      expect(find.text('2000'), findsNWidgets(2));
242 243
    });
  });
244 245

  testWidgets('Cannot select a day outside bounds', (WidgetTester tester) async {
246
    initialDate = DateTime(2017, DateTime.january, 15);
247 248 249 250 251 252
    firstDate = initialDate;
    lastDate = initialDate;
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.tap(find.text('10')); // Earlier than firstDate. Should be ignored.
      await tester.tap(find.text('20')); // Later than lastDate. Should be ignored.
      await tester.tap(find.text('OK'));
253
      // We should still be on the initial date.
254 255 256 257 258
      expect(await date, equals(initialDate));
    });
  });

  testWidgets('Cannot select a month past last date', (WidgetTester tester) async {
259
    initialDate = DateTime(2017, DateTime.january, 15);
260
    firstDate = initialDate;
261
    lastDate = DateTime(2017, DateTime.february, 20);
262
    await preparePicker(tester, (Future<DateTime> date) async {
263
      await tester.tap(nextMonthIcon);
264
      await tester.pumpAndSettle(const Duration(seconds: 1));
265
      // Shouldn't be possible to keep going into March.
266
      expect(nextMonthIcon, findsNothing);
267 268 269 270
    });
  });

  testWidgets('Cannot select a month before first date', (WidgetTester tester) async {
271 272
    initialDate = DateTime(2017, DateTime.january, 15);
    firstDate = DateTime(2016, DateTime.december, 10);
273 274
    lastDate = initialDate;
    await preparePicker(tester, (Future<DateTime> date) async {
275
      await tester.tap(previousMonthIcon);
276
      await tester.pumpAndSettle(const Duration(seconds: 1));
277
      // Shouldn't be possible to keep going into November.
278
      expect(previousMonthIcon, findsNothing);
279 280
    });
  });
281 282

  testWidgets('Only predicate days are selectable', (WidgetTester tester) async {
283 284 285
    initialDate = DateTime(2017, DateTime.january, 16);
    firstDate = DateTime(2017, DateTime.january, 10);
    lastDate = DateTime(2017, DateTime.january, 20);
286 287 288 289 290 291
    selectableDayPredicate = (DateTime day) => day.day.isEven;
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.tap(find.text('10')); // Even, works.
      await tester.tap(find.text('13')); // Odd, doesn't work.
      await tester.tap(find.text('17')); // Odd, doesn't work.
      await tester.tap(find.text('OK'));
292
      expect(await date, equals(DateTime(2017, DateTime.january, 10)));
293 294
    });
  });
295

296
  testWidgets('Can select initial date picker mode', (WidgetTester tester) async {
297
    initialDate = DateTime(2014, DateTime.january, 15);
298 299 300 301 302 303 304
    initialDatePickerMode = DatePickerMode.year;
    await preparePicker(tester, (Future<DateTime> date) async {
      await tester.pump();
      // 2018 wouldn't be available if the year picker wasn't showing.
      // The initial current year is 2014.
      await tester.tap(find.text('2018'));
      await tester.tap(find.text('OK'));
305
      expect(await date, equals(DateTime(2018, DateTime.january, 15)));
306 307 308
    });
  });

309
  group('haptic feedback', () {
310
    const Duration kHapticFeedbackInterval = Duration(milliseconds: 10);
311
    FeedbackTester feedback;
312 313

    setUp(() {
314 315 316 317
      feedback = FeedbackTester();
      initialDate = DateTime(2017, DateTime.january, 16);
      firstDate = DateTime(2017, DateTime.january, 10);
      lastDate = DateTime(2018, DateTime.january, 20);
318 319 320
      selectableDayPredicate = (DateTime date) => date.day.isEven;
    });

321 322 323 324
    tearDown(() {
      feedback?.dispose();
    });

325 326 327 328
    testWidgets('tap-select date vibrates', (WidgetTester tester) async {
      await preparePicker(tester, (Future<DateTime> date) async {
        await tester.tap(find.text('10'));
        await tester.pump(kHapticFeedbackInterval);
329
        expect(feedback.hapticCount, 1);
330 331
        await tester.tap(find.text('12'));
        await tester.pump(kHapticFeedbackInterval);
332
        expect(feedback.hapticCount, 2);
333 334
        await tester.tap(find.text('14'));
        await tester.pump(kHapticFeedbackInterval);
335
        expect(feedback.hapticCount, 3);
336 337 338 339 340 341 342
      });
    });

    testWidgets('tap-select unselectable date does not vibrate', (WidgetTester tester) async {
      await preparePicker(tester, (Future<DateTime> date) async {
        await tester.tap(find.text('11'));
        await tester.pump(kHapticFeedbackInterval);
343
        expect(feedback.hapticCount, 0);
344 345
        await tester.tap(find.text('13'));
        await tester.pump(kHapticFeedbackInterval);
346
        expect(feedback.hapticCount, 0);
347 348
        await tester.tap(find.text('15'));
        await tester.pump(kHapticFeedbackInterval);
349
        expect(feedback.hapticCount, 0);
350 351 352 353 354 355 356
      });
    });

    testWidgets('mode, year change vibrates', (WidgetTester tester) async {
      await preparePicker(tester, (Future<DateTime> date) async {
        await tester.tap(find.text('2017'));
        await tester.pump(kHapticFeedbackInterval);
357
        expect(feedback.hapticCount, 1);
358 359
        await tester.tap(find.text('2018'));
        await tester.pump(kHapticFeedbackInterval);
360
        expect(feedback.hapticCount, 2);
361 362
      });
    });
363

364
  });
365 366 367 368 369 370 371 372 373

  test('days in month', () {
    expect(DayPicker.getDaysInMonth(2017, 10), 31);
    expect(DayPicker.getDaysInMonth(2017, 6), 30);
    expect(DayPicker.getDaysInMonth(2017, 2), 28);
    expect(DayPicker.getDaysInMonth(2016, 2), 29);
    expect(DayPicker.getDaysInMonth(2000, 2), 29);
    expect(DayPicker.getDaysInMonth(1900, 2), 28);
  });
374 375 376 377 378 379 380 381 382 383 384 385 386

  testWidgets('month header tap', (WidgetTester tester) async {
    selectableDayPredicate = null;
    await preparePicker(tester, (Future<DateTime> date) async {
      // Switch into the year selector.
      await tester.tap(find.text('January 2016'));
      await tester.pump();
      expect(find.text('2020'), isNotNull);

      await tester.tap(find.text('CANCEL'));
      expect(await date, isNull);
    });
  });
387 388

  testWidgets('exports semantics', (WidgetTester tester) async {
389
    final SemanticsTester semantics = SemanticsTester(tester);
390
    await preparePicker(tester, (Future<DateTime> date) async {
391
      final TestSemantics expected = TestSemantics(
392 393 394
        flags: <SemanticsFlag>[
          SemanticsFlag.scopesRoute,
        ],
395
        children: <TestSemantics>[
396
          TestSemantics(
397
            actions: <SemanticsAction>[SemanticsAction.tap],
398
            label: '2016',
399 400
            textDirection: TextDirection.ltr,
          ),
401
          TestSemantics(
402
            flags: <SemanticsFlag>[SemanticsFlag.isSelected],
403
            actions: <SemanticsAction>[SemanticsAction.tap],
404
            label: 'Fri, Jan 15',
405 406
            textDirection: TextDirection.ltr,
          ),
407
          TestSemantics(
408
            children: <TestSemantics>[
409
              TestSemantics(
410
                children: <TestSemantics>[
411
                  TestSemantics(
412
                    actions: <SemanticsAction>[SemanticsAction.scrollLeft, SemanticsAction.scrollRight],
413
                    children: <TestSemantics>[
414
                      TestSemantics(
415
                        children: <TestSemantics>[
416
                          TestSemantics(
417
                            children: <TestSemantics>[
418
                              TestSemantics(
419 420 421 422
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '1, Friday, January 1, 2016',
                                textDirection: TextDirection.ltr,
                              ),
423
                              TestSemantics(
424 425 426 427
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '2, Saturday, January 2, 2016',
                                textDirection: TextDirection.ltr,
                              ),
428
                              TestSemantics(
429 430 431 432
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '3, Sunday, January 3, 2016',
                                textDirection: TextDirection.ltr,
                              ),
433
                              TestSemantics(
434 435 436 437
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '4, Monday, January 4, 2016',
                                textDirection: TextDirection.ltr,
                              ),
438
                              TestSemantics(
439 440 441 442
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '5, Tuesday, January 5, 2016',
                                textDirection: TextDirection.ltr,
                              ),
443
                              TestSemantics(
444 445 446 447
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '6, Wednesday, January 6, 2016',
                                textDirection: TextDirection.ltr,
                              ),
448
                              TestSemantics(
449 450 451 452
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '7, Thursday, January 7, 2016',
                                textDirection: TextDirection.ltr,
                              ),
453
                              TestSemantics(
454 455 456 457
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '8, Friday, January 8, 2016',
                                textDirection: TextDirection.ltr,
                              ),
458
                              TestSemantics(
459 460 461 462
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '9, Saturday, January 9, 2016',
                                textDirection: TextDirection.ltr,
                              ),
463
                              TestSemantics(
464 465 466 467
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '10, Sunday, January 10, 2016',
                                textDirection: TextDirection.ltr,
                              ),
468
                              TestSemantics(
469 470 471 472
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '11, Monday, January 11, 2016',
                                textDirection: TextDirection.ltr,
                              ),
473
                              TestSemantics(
474 475 476 477
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '12, Tuesday, January 12, 2016',
                                textDirection: TextDirection.ltr,
                              ),
478
                              TestSemantics(
479 480 481 482
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '13, Wednesday, January 13, 2016',
                                textDirection: TextDirection.ltr,
                              ),
483
                              TestSemantics(
484 485 486 487
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '14, Thursday, January 14, 2016',
                                textDirection: TextDirection.ltr,
                              ),
488
                              TestSemantics(
489 490 491 492 493
                                flags: <SemanticsFlag>[SemanticsFlag.isSelected],
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '15, Friday, January 15, 2016',
                                textDirection: TextDirection.ltr,
                              ),
494
                              TestSemantics(
495 496 497 498
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '16, Saturday, January 16, 2016',
                                textDirection: TextDirection.ltr,
                              ),
499
                              TestSemantics(
500 501 502 503
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '17, Sunday, January 17, 2016',
                                textDirection: TextDirection.ltr,
                              ),
504
                              TestSemantics(
505 506 507 508
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '18, Monday, January 18, 2016',
                                textDirection: TextDirection.ltr,
                              ),
509
                              TestSemantics(
510 511 512 513
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '19, Tuesday, January 19, 2016',
                                textDirection: TextDirection.ltr,
                              ),
514
                              TestSemantics(
515 516 517 518
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '20, Wednesday, January 20, 2016',
                                textDirection: TextDirection.ltr,
                              ),
519
                              TestSemantics(
520 521 522 523
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '21, Thursday, January 21, 2016',
                                textDirection: TextDirection.ltr,
                              ),
524
                              TestSemantics(
525 526 527 528
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '22, Friday, January 22, 2016',
                                textDirection: TextDirection.ltr,
                              ),
529
                              TestSemantics(
530 531 532 533
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '23, Saturday, January 23, 2016',
                                textDirection: TextDirection.ltr,
                              ),
534
                              TestSemantics(
535 536 537 538
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '24, Sunday, January 24, 2016',
                                textDirection: TextDirection.ltr,
                              ),
539
                              TestSemantics(
540 541 542 543
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '25, Monday, January 25, 2016',
                                textDirection: TextDirection.ltr,
                              ),
544
                              TestSemantics(
545 546 547 548
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '26, Tuesday, January 26, 2016',
                                textDirection: TextDirection.ltr,
                              ),
549
                              TestSemantics(
550 551 552 553
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '27, Wednesday, January 27, 2016',
                                textDirection: TextDirection.ltr,
                              ),
554
                              TestSemantics(
555 556 557 558
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '28, Thursday, January 28, 2016',
                                textDirection: TextDirection.ltr,
                              ),
559
                              TestSemantics(
560 561 562 563
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '29, Friday, January 29, 2016',
                                textDirection: TextDirection.ltr,
                              ),
564
                              TestSemantics(
565 566 567 568
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '30, Saturday, January 30, 2016',
                                textDirection: TextDirection.ltr,
                              ),
569
                              TestSemantics(
570 571 572 573 574
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                label: '31, Sunday, January 31, 2016',
                                textDirection: TextDirection.ltr,
                              ),
                            ],
575 576 577 578 579 580 581 582 583
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
584
          TestSemantics(
585
            flags: <SemanticsFlag>[SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled],
586
            actions: <SemanticsAction>[SemanticsAction.tap],
587
            label: 'Previous month December 2015',
588 589
            textDirection: TextDirection.ltr,
          ),
590
          TestSemantics(
591
            flags: <SemanticsFlag>[SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled],
592
            actions: <SemanticsAction>[SemanticsAction.tap],
593
            label: 'Next month February 2016',
594 595
            textDirection: TextDirection.ltr,
          ),
596
          TestSemantics(
597
            flags: <SemanticsFlag>[SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled],
598
            actions: <SemanticsAction>[SemanticsAction.tap],
599
            label: 'CANCEL',
600 601
            textDirection: TextDirection.ltr,
          ),
602
          TestSemantics(
603
            flags: <SemanticsFlag>[SemanticsFlag.isButton, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled],
604
            actions: <SemanticsAction>[SemanticsAction.tap],
605
            label: 'OK',
606 607 608 609 610 611
            textDirection: TextDirection.ltr,
          ),
        ],
      );

      expect(semantics, hasSemantics(
612 613
        TestSemantics.root(children: <TestSemantics>[
          TestSemantics(
614 615 616
            children: <TestSemantics>[expected],
          ),
        ]),
617 618 619 620 621
        ignoreId: true,
        ignoreTransform: true,
        ignoreRect: true,
      ));
    });
622 623

    semantics.dispose();
624
  });
625 626

  testWidgets('chervons animate when scrolling month picker', (WidgetTester tester) async {
627 628
    final Key _datePickerKey = UniqueKey();
    DateTime _selectedDate = DateTime(2016, DateTime.july, 26);
629 630

    await tester.pumpWidget(
631 632
      MaterialApp(
        home: StatefulBuilder(
633
          builder: (BuildContext context, StateSetter setState) {
634
            return Container(
635
              width: 400.0,
636 637 638 639 640
              child: SingleChildScrollView(
                child: Material(
                  child: MonthPicker(
                    firstDate: DateTime(0),
                    lastDate: DateTime(9999),
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
                    key: _datePickerKey,
                    selectedDate: _selectedDate,
                    onChanged: (DateTime value) {
                      setState(() {
                        _selectedDate = value;
                      });
                    },
                  ),
                ),
              ),
            );
          },
        ),
      )
    );

    final Finder chevronFinder = find.byType(IconButton);
    final List<RenderAnimatedOpacity> chevronRenderers = chevronFinder.evaluate().map(
      (Element element) => element.ancestorRenderObjectOfType(
        const TypeMatcher<RenderAnimatedOpacity>())).cast<RenderAnimatedOpacity>().toList();

    // Initial chevron animation state should be dismissed
    // An AlwaysStoppedAnimation is also found and is ignored
    for(RenderAnimatedOpacity renderer in chevronRenderers) {
      expect(renderer.opacity.value, equals(1.0));
      expect(renderer.opacity.status, equals(AnimationStatus.dismissed));
    }

    // Drag and hold the picker to test for the opacity change
    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
    await gesture.moveBy(const Offset(50.0, 100.0));
    await tester.pumpAndSettle();
    for(RenderAnimatedOpacity renderer in chevronRenderers) {
674
      expect(renderer.opacity.value, equals(0.0));
675 676 677 678 679 680 681 682 683 684 685
      expect(renderer.opacity.status, equals(AnimationStatus.completed));
    }

    // Release the drag and test for the opacity to return to original value
    await gesture.up();
    await tester.pumpAndSettle();
    for(RenderAnimatedOpacity renderer in chevronRenderers) {
      expect(renderer.opacity.value, equals(1.0));
      expect(renderer.opacity.status, equals(AnimationStatus.dismissed));
    }
  });
Ian Hickson's avatar
Ian Hickson committed
686
}