date_picker_test.dart 4.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
// 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.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('tap-select an day', (WidgetTester tester) async {
    Key _datePickerKey = new UniqueKey();
    DateTime _selectedDate = new DateTime(2016, DateTime.JULY, 26);

    await tester.pumpWidget(
      new Overlay(
        initialEntries: <OverlayEntry>[
          new OverlayEntry(
            builder: (BuildContext context) => new StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return new Positioned(
                  width: 400.0,
                  child: new Block(
                    children: <Widget>[
                      new Material(
                        child: new DatePicker(
                          firstDate: new DateTime(0),
                          lastDate: new DateTime(9999),
                          key: _datePickerKey,
                          selectedDate: _selectedDate,
                          onChanged: (DateTime value) {
                            setState(() {
                              _selectedDate = value;
                            });
                          }
                        )
                      )
                    ]
                  )
                );
              }
            )
          )
        ]
      )
    );

    await tester.tapAt(const Point(50.0, 200.0));
    expect(_selectedDate, equals(new DateTime(2016, DateTime.JULY, 26)));
    await tester.pump(const Duration(seconds: 2));

    await tester.tapAt(const Point(300.0, 200.0));
    expect(_selectedDate, equals(new DateTime(2016, DateTime.JULY, 1)));
    await tester.pump(const Duration(seconds: 2));

    await tester.tapAt(const Point(380.0, 120.0));
    await tester.pump();
    await tester.pump(const Duration(seconds: 2));
    expect(_selectedDate, equals(new DateTime(2016, DateTime.JULY, 1)));

    await tester.tapAt(const Point(300.0, 200.0));
    expect(_selectedDate, equals(new DateTime(2016, DateTime.AUGUST, 5)));
    await tester.pump(const Duration(seconds: 2));

    await tester.scroll(find.byKey(_datePickerKey), const Offset(-300.0, 0.0));
    await tester.pump();
    await tester.pump(const Duration(seconds: 2));
    expect(_selectedDate, equals(new DateTime(2016, DateTime.AUGUST, 5)));

    await tester.tapAt(const Point(45.0, 370.0));
    expect(_selectedDate, equals(new DateTime(2016, DateTime.SEPTEMBER, 25)));
    await tester.pump(const Duration(seconds: 2));

    await tester.scroll(find.byKey(_datePickerKey), const Offset(300.0, 10.0));
    await tester.pump();
    await tester.pump(const Duration(seconds: 2));
    expect(_selectedDate, equals(new DateTime(2016, DateTime.SEPTEMBER, 25)));

    await tester.tapAt(const Point(210.0, 280.0));
    expect(_selectedDate, equals(new DateTime(2016, DateTime.AUGUST, 17)));
    await tester.pump(const Duration(seconds: 2));

  });

  testWidgets('render picker with intrinsic dimensions', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Overlay(
        initialEntries: <OverlayEntry>[
          new OverlayEntry(
            builder: (BuildContext context) => new StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return new IntrinsicWidth(
                  child: new IntrinsicHeight(
                    child: new Material(
                      child: new Block(
                        children: <Widget>[
                          new DatePicker(
                            firstDate: new DateTime(0),
                            lastDate: new DateTime(9999),
                            onChanged: null,
                            selectedDate: new DateTime(2000, DateTime.JANUARY, 1)
                          )
                        ]
                      )
                    )
                  )
                );
              }
            )
          )
        ]
      )
    );
    await tester.pump(const Duration(seconds: 5));
  });

}