scroll_notification_test.dart 3.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// 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() {
  testWidgets('Scroll notifcation basics', (WidgetTester tester) async {
    ScrollNotification notification;

    await tester.pumpWidget(new NotificationListener<ScrollNotification>(
      onNotification: (ScrollNotification value) {
        notification = value;
        return false;
      },
      child: new ScrollableViewport(
        child: new SizedBox(height: 1200.0)
      )
    ));

    TestGesture gesture = await tester.startGesture(new Point(100.0, 100.0));
    await tester.pump(const Duration(seconds: 1));
    expect(notification.kind, equals(ScrollNotificationKind.started));
    expect(notification.depth, equals(0));
26 27 28 29
    expect(notification.dragStartDetails, isNotNull);
    expect(notification.dragStartDetails.globalPosition, equals(new Point(100.0, 100.0)));
    expect(notification.dragUpdateDetails, isNull);
    expect(notification.dragEndDetails, isNull);
30 31 32 33 34

    await gesture.moveBy(new Offset(-10.0, -10.0));
    await tester.pump(const Duration(seconds: 1));
    expect(notification.kind, equals(ScrollNotificationKind.updated));
    expect(notification.depth, equals(0));
35 36 37 38 39
    expect(notification.dragStartDetails, isNull);
    expect(notification.dragUpdateDetails, isNotNull);
    expect(notification.dragUpdateDetails.globalPosition, equals(new Point(90.0, 90.0)));
    expect(notification.dragUpdateDetails.delta, equals(new Offset(0.0, -10.0)));
    expect(notification.dragEndDetails, isNull);
40 41 42 43 44

    await gesture.up();
    await tester.pump(const Duration(seconds: 1));
    expect(notification.kind, equals(ScrollNotificationKind.ended));
    expect(notification.depth, equals(0));
45 46 47 48
    expect(notification.dragStartDetails, isNull);
    expect(notification.dragUpdateDetails, isNull);
    expect(notification.dragEndDetails, isNotNull);
    expect(notification.dragEndDetails.velocity, equals(Velocity.zero));
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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
  });

  testWidgets('Scroll notifcation depth', (WidgetTester tester) async {
    final List<ScrollNotificationKind> depth0Kinds = <ScrollNotificationKind>[];
    final List<ScrollNotificationKind> depth1Kinds = <ScrollNotificationKind>[];
    final List<int> depth0Values = <int>[];
    final List<int> depth1Values = <int>[];

    await tester.pumpWidget(new NotificationListener<ScrollNotification>(
      onNotification: (ScrollNotification value) {
        depth1Kinds.add(value.kind);
        depth1Values.add(value.depth);
        return false;
      },
      child: new ScrollableViewport(
        child: new SizedBox(
          height: 1200.0,
          child: new NotificationListener<ScrollNotification>(
            onNotification: (ScrollNotification value) {
              depth0Kinds.add(value.kind);
              depth0Values.add(value.depth);
              return false;
            },
            child: new Container(
              padding: const EdgeInsets.all(50.0),
              child: new ScrollableViewport(child: new SizedBox(height: 1200.0))
            )
          )
        )
      )
    ));

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

    final List<ScrollNotificationKind> kinds = <ScrollNotificationKind>[
      ScrollNotificationKind.started,
      ScrollNotificationKind.updated,
      ScrollNotificationKind.ended
    ];
    expect(depth0Kinds, equals(kinds));
    expect(depth1Kinds, equals(kinds));

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