scroll_behavior_test.dart 2.73 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/gestures.dart';
8
import 'package:flutter/widgets.dart';
9
import 'package:flutter_test/flutter_test.dart';
10

11
GestureVelocityTrackerBuilder lastCreatedBuilder;
Adam Barth's avatar
Adam Barth committed
12
class TestScrollBehavior extends ScrollBehavior {
13
  const TestScrollBehavior(this.flag);
14 15 16 17

  final bool flag;

  @override
18
  ScrollPhysics getScrollPhysics(BuildContext context) {
19
    return flag
20 21
      ? const ClampingScrollPhysics()
      : const BouncingScrollPhysics();
22 23 24
  }

  @override
25
  bool shouldNotify(TestScrollBehavior old) => flag != old.flag;
26 27 28 29

  @override
  GestureVelocityTrackerBuilder velocityTrackerBuilder(BuildContext context) {
      lastCreatedBuilder = flag
30
        ? (PointerEvent ev) => VelocityTracker.withKind(ev.kind)
31
        : (PointerEvent ev) => IOSScrollViewFlingVelocityTracker(ev.kind);
32 33
      return lastCreatedBuilder;
  }
34
}
35 36

void main() {
37
  testWidgets('Inherited ScrollConfiguration changed', (WidgetTester tester) async {
38
    final GlobalKey key = GlobalKey(debugLabel: 'scrollable');
39
    TestScrollBehavior behavior;
40
    ScrollPositionWithSingleContext position;
41

42
    final Widget scrollView = SingleChildScrollView(
43
      key: key,
44
      child: Builder(
45
        builder: (BuildContext context) {
46 47
          behavior = ScrollConfiguration.of(context) as TestScrollBehavior;
          position = Scrollable.of(context).position as ScrollPositionWithSingleContext;
48
          return Container(height: 1000.0);
49 50 51
        },
      ),
    );
52 53

    await tester.pumpWidget(
54
      ScrollConfiguration(
55
        behavior: const TestScrollBehavior(true),
56 57
        child: scrollView,
      ),
58 59
    );

60 61
    expect(behavior, isNotNull);
    expect(behavior.flag, isTrue);
Dan Field's avatar
Dan Field committed
62
    expect(position.physics, isA<ClampingScrollPhysics>());
63
    expect(lastCreatedBuilder(const PointerDownEvent()), isA<VelocityTracker>());
64
    ScrollMetrics metrics = position.copyWith();
65 66
    expect(metrics.extentAfter, equals(400.0));
    expect(metrics.viewportDimension, equals(600.0));
67 68 69

    // Same Scrollable, different ScrollConfiguration
    await tester.pumpWidget(
70
      ScrollConfiguration(
71
        behavior: const TestScrollBehavior(false),
72 73
        child: scrollView,
      ),
74 75
    );

76 77
    expect(behavior, isNotNull);
    expect(behavior.flag, isFalse);
Dan Field's avatar
Dan Field committed
78
    expect(position.physics, isA<BouncingScrollPhysics>());
79
    expect(lastCreatedBuilder(const PointerDownEvent()), isA<IOSScrollViewFlingVelocityTracker>());
80
    // Regression test for https://github.com/flutter/flutter/issues/5856
81
    metrics = position.copyWith();
82 83
    expect(metrics.extentAfter, equals(400.0));
    expect(metrics.viewportDimension, equals(600.0));
84
  });
85
}