pageable_list_test.dart 6.12 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
import 'package:meta/meta.dart';
9

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

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

24
Widget buildFrame({
25 26
  bool reverse = false,
  List<int> pages = defaultPages,
27
  @required TextDirection textDirection,
28
}) {
29
  final PageView child = PageView(
30
    scrollDirection: Axis.horizontal,
31 32
    reverse: reverse,
    onPageChanged: (int page) { currentPage = page; },
33
    children: pages.map<Widget>(buildPage).toList(),
34
  );
35 36 37

  // The test framework forces the frame to be 800x600, so we need to create
  // an outer container where we can change the size.
38
  return Directionality(
39
    textDirection: textDirection,
40 41
    child: Center(
      child: Container(
42 43
        width: pageSize.width, height: pageSize.height, child: child,
      ),
44
    ),
45
  );
46 47
}

48
Future<void> page(WidgetTester tester, Offset offset) {
49
  return TestAsyncUtils.guard(() async {
50
    final String itemText = currentPage != null ? currentPage.toString() : '0';
51 52
    await tester.drag(find.text(itemText), offset);
    await tester.pumpAndSettle();
53
  });
54 55
}

56
Future<void> pageLeft(WidgetTester tester) {
57
  return page(tester, Offset(-pageSize.width, 0.0));
58 59
}

60
Future<void> pageRight(WidgetTester tester) {
61
  return page(tester, Offset(pageSize.width, 0.0));
62 63
}

64
void main() {
65
  testWidgets('PageView default control', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
66
    await tester.pumpWidget(
67
      Directionality(
Ian Hickson's avatar
Ian Hickson committed
68
        textDirection: TextDirection.ltr,
69 70
        child: Center(
          child: PageView(),
Ian Hickson's avatar
Ian Hickson committed
71 72 73
        ),
      ),
    );
74 75
  });

76
  testWidgets('PageView control test (LTR)', (WidgetTester tester) async {
77
    currentPage = null;
78
    await tester.pumpWidget(buildFrame(textDirection: TextDirection.ltr));
79
    expect(currentPage, isNull);
80
    await pageLeft(tester);
81 82 83 84 85 86 87 88 89
    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);

90
    await pageRight(tester);
91 92 93 94 95 96 97 98 99
    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);

100
    await pageRight(tester);
101
    expect(currentPage, equals(0));
102
  });
103

104
  testWidgets('PageView with reverse (LTR)', (WidgetTester tester) async {
105
    currentPage = null;
106
    await tester.pumpWidget(buildFrame(reverse: true, textDirection: TextDirection.ltr));
107
    await pageRight(tester);
108
    expect(currentPage, equals(1));
109 110

    expect(find.text('0'), findsNothing);
111
    expect(find.text('1'), findsOneWidget);
112 113 114
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
115
    expect(find.text('5'), findsNothing);
116

117
    await pageLeft(tester);
118 119 120 121 122 123 124 125 126
    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);

127
    await pageLeft(tester);
128
    expect(currentPage, equals(0));
129

130
    expect(find.text('0'), findsOneWidget);
131 132 133 134
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
135
    expect(find.text('5'), findsNothing);
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

  testWidgets('PageView control test (RTL)', (WidgetTester tester) async {
    currentPage = null;
    await tester.pumpWidget(buildFrame(textDirection: TextDirection.rtl));
    await pageRight(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);

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

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

  testWidgets('PageView with reverse (RTL)', (WidgetTester tester) async {
    currentPage = null;
    await tester.pumpWidget(buildFrame(reverse: true, textDirection: TextDirection.rtl));
    expect(currentPage, isNull);
    await 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);

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

    await pageRight(tester);
    expect(currentPage, equals(0));
  });
199
}