pageable_list_test.dart 7.4 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
import 'package:test/test.dart';

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

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

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

  // 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)
  );
43 44 45 46
}

void page(WidgetTester tester, Offset offset) {
  String itemText = currentPage != null ? currentPage.toString() : '0';
47
  tester.scroll(find.text(itemText), offset);
48 49 50
  // One frame to start the animation, a second to complete it.
  tester.pump();
  tester.pump(const Duration(seconds: 1));
51 52 53 54 55 56 57 58 59 60
}

void pageLeft(WidgetTester tester) {
  page(tester, new Offset(-pageSize.width, 0.0));
}

void pageRight(WidgetTester tester) {
  page(tester, new Offset(pageSize.width, 0.0));
}

61
void main() {
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
  testWidgets('PageableList with itemsWrap: false', (WidgetTester tester) {
    currentPage = null;
    tester.pumpWidget(buildFrame());
    expect(currentPage, isNull);
    pageLeft(tester);
    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);

    pageRight(tester);
    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);

    pageRight(tester);
    expect(currentPage, equals(0));
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 118 119 120 121
  testWidgets('PageableList with end scroll anchor', (WidgetTester tester) {
    currentPage = 5;
    tester.pumpWidget(buildFrame(scrollAnchor: ViewportAnchor.end));
    pageRight(tester);
    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);

    pageLeft(tester);
    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);

    pageLeft(tester);
    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);
122 123
  });

124 125 126 127 128 129 130 131 132 133
  testWidgets('PageableList with itemsWrap: true', (WidgetTester tester) {
    currentPage = null;
    tester.pumpWidget(buildFrame(itemsWrap: true));
    expect(currentPage, isNull);
    pageLeft(tester);
    expect(currentPage, equals(1));
    pageRight(tester);
    expect(currentPage, equals(0));
    pageRight(tester);
    expect(currentPage, equals(5));
134
  });
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  testWidgets('PageableList with end and itemsWrap: true', (WidgetTester tester) {
    currentPage = 5;
    tester.pumpWidget(buildFrame(itemsWrap: true, scrollAnchor: ViewportAnchor.end));
    pageRight(tester);
    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);

    pageLeft(tester);
    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);

    pageLeft(tester);
    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);

    pageLeft(tester);
    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);
178 179
  });

180 181 182 183 184 185 186 187 188 189
  testWidgets('PageableList with two items', (WidgetTester tester) {
    currentPage = null;
    tester.pumpWidget(buildFrame(itemsWrap: true, pages: <int>[0, 1]));
    expect(currentPage, isNull);
    pageLeft(tester);
    expect(currentPage, equals(1));
    pageRight(tester);
    expect(currentPage, equals(0));
    pageRight(tester);
    expect(currentPage, equals(1));
190 191
  });

192 193 194 195 196 197 198 199 200 201
  testWidgets('PageableList with one item', (WidgetTester tester) {
    currentPage = null;
    tester.pumpWidget(buildFrame(itemsWrap: true, pages: <int>[0]));
    expect(currentPage, isNull);
    pageLeft(tester);
    expect(currentPage, equals(0));
    pageRight(tester);
    expect(currentPage, equals(0));
    pageRight(tester);
    expect(currentPage, equals(0));
202 203
  });

204 205 206 207
  testWidgets('PageableList with no items', (WidgetTester tester) {
    currentPage = null;
    tester.pumpWidget(buildFrame(itemsWrap: true, pages: <int>[]));
    expect(currentPage, isNull);
208 209
  });

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  testWidgets('PageableList resize parent', (WidgetTester tester) {
    tester.pumpWidget(new Container());
    currentPage = null;

    tester.pumpWidget(buildFrame(itemsWrap: true));
    expect(currentPage, isNull);
    pageRight(tester);
    expect(currentPage, equals(5));

    RenderBox box = globalKeys[5].currentContext.findRenderObject();
    expect(box.size.width, equals(pageSize.width));
    expect(box.size.height, equals(pageSize.height));

    pageSize = new Size(pageSize.height, pageSize.width);
    tester.pumpWidget(buildFrame(itemsWrap: true));

    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);

    box = globalKeys[5].currentContext.findRenderObject();
    expect(box.size.width, equals(pageSize.width));
    expect(box.size.height, equals(pageSize.height));
236
  });
237
}