scroll_notification_test.dart 3.78 KB
Newer Older
1 2 3 4 5
// 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';
6
import 'package:flutter/gestures.dart';
7 8 9
import 'package:flutter/widgets.dart';

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

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

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

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

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

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

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

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

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

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