pageable_list_test.dart 6.13 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

Adam Barth's avatar
Adam Barth committed
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter/widgets.dart';
10
import 'package:meta/meta.dart';
11

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

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

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

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

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

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

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

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

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

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

102
    await pageRight(tester);
103
    expect(currentPage, equals(0));
104
  });
105

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

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

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

129
    await pageLeft(tester);
130
    expect(currentPage, equals(0));
131

132
    expect(find.text('0'), findsOneWidget);
133 134 135 136
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
137
    expect(find.text('5'), findsNothing);
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 199 200

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