scroll_behavior_test.dart 2.15 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/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7

Adam Barth's avatar
Adam Barth committed
8
class TestScrollBehavior extends ScrollBehavior {
9
  const TestScrollBehavior(this.flag);
10 11 12 13

  final bool flag;

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

  @override
21
  bool shouldNotify(TestScrollBehavior old) => flag != old.flag;
22
}
23 24

void main() {
25
  testWidgets('Inherited ScrollConfiguration changed', (WidgetTester tester) async {
26
    final GlobalKey key = GlobalKey(debugLabel: 'scrollable');
27
    TestScrollBehavior behavior;
28
    ScrollPositionWithSingleContext position;
29

30
    final Widget scrollView = SingleChildScrollView(
31
      key: key,
32
      child: Builder(
33
        builder: (BuildContext context) {
34 35
          behavior = ScrollConfiguration.of(context) as TestScrollBehavior;
          position = Scrollable.of(context).position as ScrollPositionWithSingleContext;
36
          return Container(height: 1000.0);
37 38 39
        },
      ),
    );
40 41

    await tester.pumpWidget(
42
      ScrollConfiguration(
43
        behavior: const TestScrollBehavior(true),
44 45
        child: scrollView,
      ),
46 47
    );

48 49
    expect(behavior, isNotNull);
    expect(behavior.flag, isTrue);
Dan Field's avatar
Dan Field committed
50
    expect(position.physics, isA<ClampingScrollPhysics>());
51
    ScrollMetrics metrics = position.copyWith();
52 53
    expect(metrics.extentAfter, equals(400.0));
    expect(metrics.viewportDimension, equals(600.0));
54 55 56

    // Same Scrollable, different ScrollConfiguration
    await tester.pumpWidget(
57
      ScrollConfiguration(
58
        behavior: const TestScrollBehavior(false),
59 60
        child: scrollView,
      ),
61 62
    );

63 64
    expect(behavior, isNotNull);
    expect(behavior.flag, isFalse);
Dan Field's avatar
Dan Field committed
65
    expect(position.physics, isA<BouncingScrollPhysics>());
66
    // Regression test for https://github.com/flutter/flutter/issues/5856
67
    metrics = position.copyWith();
68 69
    expect(metrics.extentAfter, equals(400.0));
    expect(metrics.viewportDimension, equals(600.0));
70
  });
71
}