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

5 6
// @dart = 2.8

7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/gestures.dart';
9
import 'package:flutter/material.dart';
10
import 'package:flutter/widgets.dart';
11 12

void main() {
13
  testWidgets('Scroll notification basics', (WidgetTester tester) async {
Adam Barth's avatar
Adam Barth committed
14
    ScrollNotification notification;
15

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

27
    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
28
    await tester.pump(const Duration(seconds: 1));
Dan Field's avatar
Dan Field committed
29
    expect(notification, isA<ScrollStartNotification>());
30
    expect(notification.depth, equals(0));
31
    final ScrollStartNotification start = notification as ScrollStartNotification;
32
    expect(start.dragDetails, isNotNull);
33
    expect(start.dragDetails.globalPosition, equals(const Offset(100.0, 100.0)));
34

35
    await gesture.moveBy(const Offset(-10.0, -10.0));
36
    await tester.pump(const Duration(seconds: 1));
Dan Field's avatar
Dan Field committed
37
    expect(notification, isA<ScrollUpdateNotification>());
38
    expect(notification.depth, equals(0));
39
    final ScrollUpdateNotification update = notification as ScrollUpdateNotification;
40
    expect(update.dragDetails, isNotNull);
41
    expect(update.dragDetails.globalPosition, equals(const Offset(90.0, 90.0)));
42
    expect(update.dragDetails.delta, equals(const Offset(0.0, -10.0)));
43 44 45

    await gesture.up();
    await tester.pump(const Duration(seconds: 1));
Dan Field's avatar
Dan Field committed
46
    expect(notification, isA<ScrollEndNotification>());
47
    expect(notification.depth, equals(0));
48
    final ScrollEndNotification end = notification as ScrollEndNotification;
49 50
    expect(end.dragDetails, isNotNull);
    expect(end.dragDetails.velocity, equals(Velocity.zero));
51 52
  });

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

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

87
    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
88
    await tester.pump(const Duration(seconds: 1));
89
    await gesture.moveBy(const Offset(-10.0, -40.0));
90 91 92 93
    await tester.pump(const Duration(seconds: 1));
    await gesture.up();
    await tester.pump(const Duration(seconds: 1));

94 95 96 97 98 99
    final List<Type> types = <Type>[
      ScrollStartNotification,
      UserScrollNotification,
      ScrollUpdateNotification,
      ScrollEndNotification,
      UserScrollNotification,
100
    ];
101 102
    expect(depth0Types, equals(types));
    expect(depth1Types, equals(types));
103

104 105
    expect(depth0Values, equals(<int>[0, 0, 0, 0, 0]));
    expect(depth1Values, equals(<int>[1, 1, 1, 1, 1]));
106
  });
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

  testWidgets('ScrollNotifications bubble past Scaffold Material', (WidgetTester tester) async {
    final List<Type> notificationTypes = <Type>[];

    await tester.pumpWidget(
      MaterialApp(
        home: NotificationListener<ScrollNotification>(
          onNotification: (ScrollNotification value) {
            notificationTypes.add(value.runtimeType);
            return false;
          },
          child: Scaffold(
            body: SizedBox.expand(
              child: SingleChildScrollView(
                dragStartBehavior: DragStartBehavior.down,
                child: SizedBox(
                  height: 1200.0,
                  child: Container(
                    padding: const EdgeInsets.all(50.0),
                    child: const SingleChildScrollView(
                      child: SizedBox(height: 1200.0),
                      dragStartBehavior: DragStartBehavior.down,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );

    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
    await tester.pump(const Duration(seconds: 1));
    await gesture.moveBy(const Offset(-10.0, -40.0));
    await tester.pump(const Duration(seconds: 1));
    await gesture.up();
    await tester.pump(const Duration(seconds: 1));

    final List<Type> types = <Type>[
      ScrollStartNotification,
      UserScrollNotification,
      ScrollUpdateNotification,
      ScrollEndNotification,
      UserScrollNotification,
    ];
    expect(notificationTypes, equals(types));
  });

156
}