clamp_overscrolls_test.dart 3.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2016 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';

// Assuming that the test container is 800x600. The height of the
// viewport's contents is 650.0, the top and bottom text children
// are 100 pixels high and top/left edge of both widgets are visible.
// The top of the bottom widget is at 550 (the top of the top widget
// is at 0). The top of the bottom widget is 500 when it has been
// scrolled completely into view.
14 15 16 17 18 19 20 21
Widget buildFrame(ScrollPhysics physics) {
  return new SingleChildScrollView(
    key: new UniqueKey(),
    physics: physics,
    child: new SizedBox(
      height: 650.0,
      child: new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
22
        textDirection: TextDirection.ltr,
23
        children: <Widget>[
Ian Hickson's avatar
Ian Hickson committed
24
          const SizedBox(height: 100.0, child: const Text('top', textDirection: TextDirection.ltr)),
25
          new Expanded(child: new Container()),
Ian Hickson's avatar
Ian Hickson committed
26
          const SizedBox(height: 100.0, child: const Text('bottom', textDirection: TextDirection.ltr)),
27 28 29
        ],
      ),
    ),
30 31 32 33
  );
}

void main() {
34
  testWidgets('ClampingScrollPhysics', (WidgetTester tester) async {
35 36 37

    // Scroll the target text widget by offset and then return its origin
    // in global coordinates.
38
    Future<Offset> locationAfterScroll(String target, Offset offset) async {
39
      await tester.dragFrom(tester.getTopLeft(find.text(target)), offset);
40 41
      await tester.pump();
      final RenderBox textBox = tester.renderObject(find.text(target));
42
      final Offset widgetOrigin = textBox.localToGlobal(Offset.zero);
43
      await tester.pump(const Duration(seconds: 1)); // Allow overscroll to settle
44
      return new Future<Offset>.value(widgetOrigin);
45 46
    }

47
    await tester.pumpWidget(buildFrame(const BouncingScrollPhysics()));
48 49
    Offset origin = await locationAfterScroll('top', const Offset(0.0, 400.0));
    expect(origin.dy, greaterThan(0.0));
50
    origin = await locationAfterScroll('bottom', const Offset(0.0, -400.0));
51
    expect(origin.dy, lessThan(500.0));
52 53


54
    await tester.pumpWidget(buildFrame(const ClampingScrollPhysics()));
55
    origin = await locationAfterScroll('top', const Offset(0.0, 400.0));
56
    expect(origin.dy, equals(0.0));
57
    origin = await locationAfterScroll('bottom', const Offset(0.0, -400.0));
58
    expect(origin.dy, equals(500.0));
59
  });
60

61
  testWidgets('ClampingScrollPhysics affects ScrollPosition', (WidgetTester tester) async {
62

63
    // BouncingScrollPhysics
64

65
    await tester.pumpWidget(buildFrame(const BouncingScrollPhysics()));
Adam Barth's avatar
Adam Barth committed
66
    ScrollableState scrollable = tester.state(find.byType(Scrollable));
67

68
    await tester.dragFrom(tester.getTopLeft(find.text('top')), const Offset(0.0, 400.0));
69
    await tester.pump();
70
    expect(scrollable.position.pixels, lessThan(0.0));
71 72
    await tester.pump(const Duration(seconds: 1)); // Allow overscroll to settle

73
    await tester.dragFrom(tester.getTopLeft(find.text('bottom')), const Offset(0.0, -400.0));
74
    await tester.pump();
75
    expect(scrollable.position.pixels, greaterThan(0.0));
76 77
    await tester.pump(const Duration(seconds: 1)); // Allow overscroll to settle

78
    // ClampingScrollPhysics
79

80
    await tester.pumpWidget(buildFrame(const ClampingScrollPhysics()));
Adam Barth's avatar
Adam Barth committed
81
    scrollable = scrollable = tester.state(find.byType(Scrollable));
82

83
    await tester.dragFrom(tester.getTopLeft(find.text('top')), const Offset(0.0, 400.0));
84
    await tester.pump();
85
    expect(scrollable.position.pixels, equals(0.0));
86 87
    await tester.pump(const Duration(seconds: 1)); // Allow overscroll to settle

88
    await tester.dragFrom(tester.getTopLeft(find.text('bottom')), const Offset(0.0, -400.0));
89
    await tester.pump();
90
    expect(scrollable.position.pixels, equals(50.0));
91
  });
92
}