pageable_list_test.dart 7.82 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter/widgets.dart';
8

9
Size pageSize = new Size(600.0, 300.0);
10
const List<int> defaultPages = const <int>[0, 1, 2, 3, 4, 5];
11
final List<GlobalKey> globalKeys = defaultPages.map((_) => new GlobalKey()).toList();
Ian Hickson's avatar
Ian Hickson committed
12
int currentPage;
13

14
Widget buildPage(int page) {
15
  return new Container(
16
    key: globalKeys[page],
17 18 19 20 21 22
    width: pageSize.width,
    height: pageSize.height,
    child: new Text(page.toString())
  );
}

23 24 25 26 27 28
Widget buildFrame({
  bool itemsWrap: false,
  ViewportAnchor scrollAnchor: ViewportAnchor.start,
  List<int> pages: defaultPages
}) {
  final PageableList list = new PageableList(
29
    children: pages.map(buildPage),
30
    itemsWrap: itemsWrap,
31
    scrollDirection: Axis.horizontal,
32
    scrollAnchor: scrollAnchor,
33
    onPageChanged: (int page) { currentPage = page; }
34
  );
35 36 37 38 39 40 41

  // The test framework forces the frame to be 800x600, so we need to create
  // an outer container where we can change the size.
  return new Center(
    child: new Container(
      width: pageSize.width, height: pageSize.height, child: list)
  );
42 43
}

44 45 46 47 48 49 50 51
Future<Null> page(WidgetTester tester, Offset offset) {
  return TestAsyncUtils.guard(() async {
    String itemText = currentPage != null ? currentPage.toString() : '0';
    await tester.scroll(find.text(itemText), offset);
    // One frame to start the animation, a second to complete it.
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
  });
52 53
}

54 55
Future<Null> pageLeft(WidgetTester tester) {
  return page(tester, new Offset(-pageSize.width, 0.0));
56 57
}

58 59
Future<Null> pageRight(WidgetTester tester) {
  return page(tester, new Offset(pageSize.width, 0.0));
60 61
}

62
void main() {
63 64 65 66
  testWidgets('PageableList default control', (WidgetTester tester) async {
    await tester.pumpWidget(new Center(child: new PageableList()));
  });

67
  testWidgets('PageableList with itemsWrap: false', (WidgetTester tester) async {
68
    currentPage = null;
69
    await tester.pumpWidget(buildFrame());
70
    expect(currentPage, isNull);
71
    await pageLeft(tester);
72 73 74 75 76 77 78 79 80
    expect(currentPage, equals(1));

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsNothing);

81
    await pageRight(tester);
82 83 84 85 86 87 88 89 90
    expect(currentPage, equals(0));

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsNothing);

91
    await pageRight(tester);
92
    expect(currentPage, equals(0));
93
  });
94

95
  testWidgets('PageableList with end scroll anchor', (WidgetTester tester) async {
96
    currentPage = 5;
97 98
    await tester.pumpWidget(buildFrame(scrollAnchor: ViewportAnchor.end));
    await pageRight(tester);
99 100 101 102 103 104 105 106 107
    expect(currentPage, equals(4));

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsOneWidget);
    expect(find.text('5'), findsNothing);

108
    await pageLeft(tester);
109 110 111 112 113 114 115 116 117
    expect(currentPage, equals(5));

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsOneWidget);

118
    await pageLeft(tester);
119 120 121 122 123 124 125 126
    expect(currentPage, equals(5));

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsOneWidget);
127 128
  });

129
  testWidgets('PageableList with itemsWrap: true', (WidgetTester tester) async {
130
    currentPage = null;
131
    await tester.pumpWidget(buildFrame(itemsWrap: true));
132
    expect(currentPage, isNull);
133
    await pageLeft(tester);
134
    expect(currentPage, equals(1));
135
    await pageRight(tester);
136
    expect(currentPage, equals(0));
137
    await pageRight(tester);
138
    expect(currentPage, equals(5));
139
  });
140

141
  testWidgets('PageableList with end and itemsWrap: true', (WidgetTester tester) async {
142
    currentPage = 5;
143 144
    await tester.pumpWidget(buildFrame(itemsWrap: true, scrollAnchor: ViewportAnchor.end));
    await pageRight(tester);
145 146 147 148 149 150 151 152 153
    expect(currentPage, equals(4));

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsOneWidget);
    expect(find.text('5'), findsNothing);

154
    await pageLeft(tester);
155 156 157 158 159 160 161 162 163
    expect(currentPage, equals(5));

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsOneWidget);

164
    await pageLeft(tester);
165 166 167 168 169 170 171 172 173
    expect(currentPage, equals(0));

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsNothing);

174
    await pageLeft(tester);
175 176 177 178 179 180 181 182
    expect(currentPage, equals(1));

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsNothing);
183 184
  });

185
  testWidgets('PageableList with two items', (WidgetTester tester) async {
186
    currentPage = null;
187
    await tester.pumpWidget(buildFrame(itemsWrap: true, pages: <int>[0, 1]));
188
    expect(currentPage, isNull);
189
    await pageLeft(tester);
190
    expect(currentPage, equals(1));
191
    await pageRight(tester);
192
    expect(currentPage, equals(0));
193
    await pageRight(tester);
194
    expect(currentPage, equals(1));
195 196
  });

197
  testWidgets('PageableList with one item', (WidgetTester tester) async {
198
    currentPage = null;
199
    await tester.pumpWidget(buildFrame(itemsWrap: true, pages: <int>[0]));
200
    expect(currentPage, isNull);
201
    await pageLeft(tester);
202
    expect(currentPage, equals(0));
203
    await pageRight(tester);
204
    expect(currentPage, equals(0));
205
    await pageRight(tester);
206
    expect(currentPage, equals(0));
207 208
  });

209
  testWidgets('PageableList with no items', (WidgetTester tester) async {
210
    currentPage = null;
211
    await tester.pumpWidget(buildFrame(itemsWrap: true, pages: <int>[]));
212
    expect(currentPage, isNull);
213 214
  });

215 216
  testWidgets('PageableList resize parent', (WidgetTester tester) async {
    await tester.pumpWidget(new Container());
217 218
    currentPage = null;

219
    await tester.pumpWidget(buildFrame(itemsWrap: true));
220
    expect(currentPage, isNull);
221
    await pageRight(tester);
222 223
    expect(currentPage, equals(5));

224 225 226
    Size boxSize = globalKeys[5].currentContext.size;
    expect(boxSize.width, equals(pageSize.width));
    expect(boxSize.height, equals(pageSize.height));
227 228

    pageSize = new Size(pageSize.height, pageSize.width);
229
    await tester.pumpWidget(buildFrame(itemsWrap: true));
230 231 232 233 234 235 236 237

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsOneWidget);

238 239 240
    boxSize = globalKeys[5].currentContext.size;
    expect(boxSize.width, equals(pageSize.width));
    expect(boxSize.height, equals(pageSize.height));
241
  });
242
}