pageable_list_test.dart 6.04 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.

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

8
Size pageSize = const Size(600.0, 300.0);
9
const List<int> defaultPages = <int>[0, 1, 2, 3, 4, 5];
10
final List<GlobalKey> globalKeys = defaultPages.map<GlobalKey>((_) => GlobalKey()).toList();
11
int? currentPage;
12

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

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

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

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

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

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

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

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

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

98
    await pageRight(tester);
99
    expect(currentPage, equals(0));
100
  });
101

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

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

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

125
    await pageLeft(tester);
126
    expect(currentPage, equals(0));
127

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

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