pageable_list_test.dart 6 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
import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.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 29
    reverse: reverse,
    onPageChanged: (int page) { currentPage = page; },
30
    children: pages.map<Widget>(buildPage).toList(),
31
  );
32 33 34

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

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

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

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

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

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

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

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

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

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

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

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

127
    expect(find.text('0'), findsOneWidget);
128 129 130 131
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
132
    expect(find.text('5'), findsNothing);
133
  });
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

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