scroll_notification_test.dart 5.49 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
import 'package:flutter/material.dart';
8
import 'package:flutter/widgets.dart';
9 10

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

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

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

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

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

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

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

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

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

102 103
    expect(depth0Values, equals(<int>[0, 0, 0, 0, 0]));
    expect(depth1Values, equals(<int>[1, 1, 1, 1, 1]));
104
  });
105 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

  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));
  });

154
}