scroll_notification_test.dart 3.74 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

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

void main() {
9
  testWidgets('Scroll notification basics', (WidgetTester tester) async {
Adam Barth's avatar
Adam Barth committed
10
    ScrollNotification notification;
11

Adam Barth's avatar
Adam Barth committed
12 13
    await tester.pumpWidget(new NotificationListener<ScrollNotification>(
      onNotification: (ScrollNotification value) {
14 15
        if (value is ScrollStartNotification || value is ScrollUpdateNotification || value is ScrollEndNotification)
          notification = value;
16 17
        return false;
      },
18
      child: new SingleChildScrollView(
19
        child: const SizedBox(height: 1200.0)
20 21 22
      )
    ));

23
    final TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0));
24
    await tester.pump(const Duration(seconds: 1));
25
    expect(notification, const isInstanceOf<ScrollStartNotification>());
26
    expect(notification.depth, equals(0));
27
    final ScrollStartNotification start = notification;
28 29
    expect(start.dragDetails, isNotNull);
    expect(start.dragDetails.globalPosition, equals(const Point(100.0, 100.0)));
30

31
    await gesture.moveBy(const Offset(-10.0, -10.0));
32
    await tester.pump(const Duration(seconds: 1));
33
    expect(notification, const isInstanceOf<ScrollUpdateNotification>());
34
    expect(notification.depth, equals(0));
35
    final ScrollUpdateNotification update = notification;
36 37 38
    expect(update.dragDetails, isNotNull);
    expect(update.dragDetails.globalPosition, equals(const Point(90.0, 90.0)));
    expect(update.dragDetails.delta, equals(const Offset(0.0, -10.0)));
39 40 41

    await gesture.up();
    await tester.pump(const Duration(seconds: 1));
42
    expect(notification, const isInstanceOf<ScrollEndNotification>());
43
    expect(notification.depth, equals(0));
44
    final ScrollEndNotification end = notification;
45 46
    expect(end.dragDetails, isNotNull);
    expect(end.dragDetails.velocity, equals(Velocity.zero));
47 48
  });

49
  testWidgets('Scroll notification depth', (WidgetTester tester) async {
50 51
    final List<Type> depth0Types = <Type>[];
    final List<Type> depth1Types = <Type>[];
52 53 54
    final List<int> depth0Values = <int>[];
    final List<int> depth1Values = <int>[];

Adam Barth's avatar
Adam Barth committed
55 56
    await tester.pumpWidget(new NotificationListener<ScrollNotification>(
      onNotification: (ScrollNotification value) {
57
        depth1Types.add(value.runtimeType);
58 59 60
        depth1Values.add(value.depth);
        return false;
      },
61
      child: new SingleChildScrollView(
62 63
        child: new SizedBox(
          height: 1200.0,
Adam Barth's avatar
Adam Barth committed
64 65
          child: new NotificationListener<ScrollNotification>(
            onNotification: (ScrollNotification value) {
66
              depth0Types.add(value.runtimeType);
67 68 69 70 71
              depth0Values.add(value.depth);
              return false;
            },
            child: new Container(
              padding: const EdgeInsets.all(50.0),
72
              child: new SingleChildScrollView(child: const SizedBox(height: 1200.0))
73 74 75 76 77 78
            )
          )
        )
      )
    ));

79
    final TestGesture gesture = await tester.startGesture(const Point(100.0, 100.0));
80
    await tester.pump(const Duration(seconds: 1));
81
    await gesture.moveBy(const Offset(-10.0, -10.0));
82 83 84 85
    await tester.pump(const Duration(seconds: 1));
    await gesture.up();
    await tester.pump(const Duration(seconds: 1));

86 87 88 89 90 91
    final List<Type> types = <Type>[
      ScrollStartNotification,
      UserScrollNotification,
      ScrollUpdateNotification,
      ScrollEndNotification,
      UserScrollNotification,
92
    ];
93 94
    expect(depth0Types, equals(types));
    expect(depth1Types, equals(types));
95

96 97
    expect(depth0Values, equals(<int>[0, 0, 0, 0, 0]));
    expect(depth1Values, equals(<int>[1, 1, 1, 1, 1]));
98 99
  });
}