remember_scroll_position_test.dart 4.09 KB
Newer Older
Ian Hickson's avatar
Ian Hickson 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/material.dart';
Ian Hickson's avatar
Ian Hickson committed
7

8
class ThePositiveNumbers extends StatelessWidget {
9
  @override
10 11 12 13 14 15 16 17 18 19
  Widget build(BuildContext context) {
    return new ScrollableLazyList(
      itemExtent: 100.0,
      itemBuilder: (BuildContext context, int start, int count) {
        List<Widget> result = new List<Widget>();
        for (int index = start; index < start + count; index += 1)
          result.add(new Text('$index', key: new ValueKey<int>(index)));
        return result;
      }
    );
Ian Hickson's avatar
Ian Hickson committed
20 21 22 23
  }
}

void main() {
24
  testWidgets('whether we remember our scroll position', (WidgetTester tester) async {
25
    GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
26
    await tester.pumpWidget(new Navigator(
27 28 29 30 31 32 33 34 35
      key: navigatorKey,
      onGenerateRoute: (RouteSettings settings) {
        if (settings.name == '/')
          return new MaterialPageRoute<Null>(builder: (_) => new Container(child: new ThePositiveNumbers()));
        else if (settings.name == '/second')
          return new MaterialPageRoute<Null>(builder: (_) => new Container(child: new ThePositiveNumbers()));
        return null;
      }
    ));
Ian Hickson's avatar
Ian Hickson committed
36

37 38 39 40 41 42 43 44 45 46 47
    // we're 600 pixels high, each item is 100 pixels high, scroll position is
    // zero, so we should have exactly 6 items, 0..5.
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsOneWidget);
    expect(find.text('4'), findsOneWidget);
    expect(find.text('5'), findsOneWidget);
    expect(find.text('6'), findsNothing);
    expect(find.text('10'), findsNothing);
    expect(find.text('100'), findsNothing);
Ian Hickson's avatar
Ian Hickson committed
48

49
    ScrollableState targetState = tester.state(find.byType(Scrollable));
50
    targetState.scrollTo(1000.0);
51
    await tester.pump(new Duration(seconds: 1));
Ian Hickson's avatar
Ian Hickson committed
52

53 54
    // we're 600 pixels high, each item is 100 pixels high, scroll position is
    // 1000, so we should have exactly 6 items, 10..15.
Ian Hickson's avatar
Ian Hickson committed
55

56 57 58 59 60 61 62 63 64 65 66
    expect(find.text('0'), findsNothing);
    expect(find.text('8'), findsNothing);
    expect(find.text('9'), findsNothing);
    expect(find.text('10'), findsOneWidget);
    expect(find.text('11'), findsOneWidget);
    expect(find.text('12'), findsOneWidget);
    expect(find.text('13'), findsOneWidget);
    expect(find.text('14'), findsOneWidget);
    expect(find.text('15'), findsOneWidget);
    expect(find.text('16'), findsNothing);
    expect(find.text('100'), findsNothing);
Ian Hickson's avatar
Ian Hickson committed
67

68 69 70
    navigatorKey.currentState.openTransaction(
      (NavigatorTransaction transaction) => transaction.pushNamed('/second')
    );
71 72
    await tester.pump(); // navigating always takes two frames
    await tester.pump(new Duration(seconds: 1));
Ian Hickson's avatar
Ian Hickson committed
73

74 75 76 77 78 79 80 81 82 83
    // same as the first list again
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsOneWidget);
    expect(find.text('4'), findsOneWidget);
    expect(find.text('5'), findsOneWidget);
    expect(find.text('6'), findsNothing);
    expect(find.text('10'), findsNothing);
    expect(find.text('100'), findsNothing);
Ian Hickson's avatar
Ian Hickson committed
84

85 86 87
    navigatorKey.currentState.openTransaction(
      (NavigatorTransaction transaction) => transaction.pop()
    );
88 89
    await tester.pump(); // navigating always takes two frames
    await tester.pump(new Duration(seconds: 1));
Ian Hickson's avatar
Ian Hickson committed
90

91 92
    // we're 600 pixels high, each item is 100 pixels high, scroll position is
    // 1000, so we should have exactly 6 items, 10..15.
Ian Hickson's avatar
Ian Hickson committed
93

94 95 96 97 98 99 100 101 102 103 104
    expect(find.text('0'), findsNothing);
    expect(find.text('8'), findsNothing);
    expect(find.text('9'), findsNothing);
    expect(find.text('10'), findsOneWidget);
    expect(find.text('11'), findsOneWidget);
    expect(find.text('12'), findsOneWidget);
    expect(find.text('13'), findsOneWidget);
    expect(find.text('14'), findsOneWidget);
    expect(find.text('15'), findsOneWidget);
    expect(find.text('16'), findsNothing);
    expect(find.text('100'), findsNothing);
Adam Barth's avatar
Adam Barth committed
105

Ian Hickson's avatar
Ian Hickson committed
106 107
  });
}