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

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
9
  testWidgets('TrackingScrollController saves offset', (WidgetTester tester) async {
10
    final TrackingScrollController controller = TrackingScrollController();
11
    addTearDown(controller.dispose);
12
    const double listItemHeight = 100.0;
13 14

    await tester.pumpWidget(
15
      Directionality(
16
        textDirection: TextDirection.ltr,
17
        child: PageView.builder(
18
          itemBuilder: (BuildContext context, int index) {
19
            return ListView(
Ian Hickson's avatar
Ian Hickson committed
20
              controller: controller,
21
              children: List<Widget>.generate(
Ian Hickson's avatar
Ian Hickson committed
22
                10,
23
                (int i) => SizedBox(
Ian Hickson's avatar
Ian Hickson committed
24
                  height: listItemHeight,
25
                  child: Text('Page$index-Item$i'),
Ian Hickson's avatar
Ian Hickson committed
26 27 28
                ),
              ).toList(),
            );
29 30 31 32 33 34 35 36 37 38 39
          },
        ),
      ),
    );

    expect(find.text('Page0-Item1'), findsOneWidget);
    expect(find.text('Page1-Item1'), findsNothing);
    expect(find.text('Page2-Item0'), findsNothing);
    expect(find.text('Page2-Item1'), findsNothing);

    controller.jumpTo(listItemHeight + 10);
40
    await tester.pumpAndSettle();
41

Ian Hickson's avatar
Ian Hickson committed
42
    await tester.fling(find.text('Page0-Item1'), const Offset(-100.0, 0.0), 10000.0);
43
    await tester.pumpAndSettle();
44 45 46 47 48 49

    expect(find.text('Page0-Item1'), findsNothing);
    expect(find.text('Page1-Item1'), findsOneWidget);
    expect(find.text('Page2-Item0'), findsNothing);
    expect(find.text('Page2-Item1'), findsNothing);

Ian Hickson's avatar
Ian Hickson committed
50
    await tester.fling(find.text('Page1-Item1'), const Offset(-100.0, 0.0), 10000.0);
51
    await tester.pumpAndSettle();
52 53 54 55 56 57

    expect(find.text('Page0-Item1'), findsNothing);
    expect(find.text('Page1-Item1'), findsNothing);
    expect(find.text('Page2-Item0'), findsNothing);
    expect(find.text('Page2-Item1'), findsOneWidget);

Ian Hickson's avatar
Ian Hickson committed
58
    await tester.pumpWidget(const Text('Another page', textDirection: TextDirection.ltr));
59 60 61

    expect(controller.initialScrollOffset, 0.0);
  });
62

63
  testWidgets('TrackingScrollController saves offset', (WidgetTester tester) async {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    int attach = 0;
    int detach = 0;
    final TrackingScrollController controller = TrackingScrollController(
      onAttach: (_) { attach++; },
      onDetach: (_) { detach++; },
    );
    addTearDown(controller.dispose);
    const double listItemHeight = 100.0;

    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return ListView(
            controller: controller,
            children: List<Widget>.generate(
              10,
              (int i) => SizedBox(
                height: listItemHeight,
                child: Text('Page$index-Item$i'),
              ),
            ).toList(),
          );
        },
      ),
    ));
    await tester.pumpAndSettle();

    expect(attach, 1);
    expect(detach, 0);

    await tester.pumpWidget(Container());
    await tester.pumpAndSettle();

    expect(attach, 1);
    expect(detach, 1);
  });
101
}