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

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

  final bool flag;

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

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

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

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

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

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

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

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

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