drop_down_test.dart 5.28 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2015 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_test/flutter_test.dart';
import 'package:flutter/material.dart';

void main() {
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
  testWidgets('Drop down button control test', (WidgetTester tester) async {
    List<String> items = <String>['one', 'two', 'three', 'four'];
    String value = items.first;

    void didChangeValue(String newValue) {
      value = newValue;
    }

    Widget build() {
      return new MaterialApp(
        home: new Material(
          child: new Center(
            child: new DropdownButton<String>(
              value: value,
              items: items.map((String item) {
                return new DropdownMenuItem<String>(
                  value: item,
                  child: new Text(item),
                );
              }).toList(),
              onChanged: didChangeValue,
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(build());

    await tester.tap(find.text('one'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('one'));

    await tester.tap(find.text('three').last);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('three'));

    await tester.tap(find.text('three'));
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 116 117
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('three'));

    await tester.pumpWidget(build());

    await tester.tap(find.text('two').last);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('two'));
  });

  testWidgets('Drop down button with no app', (WidgetTester tester) async {
    List<String> items = <String>['one', 'two', 'three', 'four'];
    String value = items.first;

    void didChangeValue(String newValue) {
      value = newValue;
    }

    Widget build() {
      return new Navigator(
        initialRoute: '/',
        onGenerateRoute: (RouteSettings settings) {
          return new MaterialPageRoute<Null>(
            settings: settings,
            builder: (BuildContext context) {
              return new Material(
                child: new Center(
                  child: new DropdownButton<String>(
                    value: value,
                    items: items.map((String item) {
                      return new DropdownMenuItem<String>(
                        value: item,
                        child: new Text(item),
                      );
                    }).toList(),
                    onChanged: didChangeValue,
                  ),
                )
              );
            },
          );
        }
      );
    }

    await tester.pumpWidget(build());

    await tester.tap(find.text('one'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('one'));

    await tester.tap(find.text('three').last);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('three'));

    await tester.tap(find.text('three'));
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('three'));

    await tester.pumpWidget(build());

    await tester.tap(find.text('two').last);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('two'));
  });

133
  testWidgets('Drop down screen edges', (WidgetTester tester) async {
134
    int value = 4;
135
    List<DropdownMenuItem<int>> items = <DropdownMenuItem<int>>[];
136
    for (int i = 0; i < 20; ++i)
137
      items.add(new DropdownMenuItem<int>(value: i, child: new Text('$i')));
138 139 140 141 142

    void handleChanged(int newValue) {
      value = newValue;
    }

143
    DropdownButton<int> button = new DropdownButton<int>(
144 145
      value: value,
      onChanged: handleChanged,
146
      items: items,
147 148
    );

149
    await tester.pumpWidget(
150 151 152 153
      new MaterialApp(
        home: new Material(
          child: new Align(
            alignment: FractionalOffset.topCenter,
154 155 156 157
            child: button,
          ),
        ),
      ),
158 159
    );

160 161 162
    await tester.tap(find.text('4'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation
163

164 165
    // We should have two copies of item 5, one in the menu and one in the
    // button itself.
166
    expect(tester.elementList(find.text('5')), hasLength(2));
167 168 169

    // We should only have one copy of item 19, which is in the button itself.
    // The copy in the menu shouldn't be in the tree because it's off-screen.
170
    expect(tester.elementList(find.text('19')), hasLength(1));
171

172
    expect(value, 4);
173
    await tester.tap(find.byConfig(button));
174
    expect(value, 4);
175 176
    // this waits for the route's completer to complete, which calls handleChanged
    await tester.idle();
177
    expect(value, 4);
178 179 180

    // TODO(abarth): Remove these calls to pump once navigator cleans up its
    // pop transitions.
181 182
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation
183 184
  });
}