scroll_behavior_test.dart 3.45 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:flutter/foundation.dart' show defaultTargetPlatform;
6
import 'package:flutter/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

class TestScrollConfigurationDelegate extends ScrollConfigurationDelegate {
  TestScrollConfigurationDelegate(this.flag);

  final bool flag;

  @override
  TargetPlatform get platform => defaultTargetPlatform;

  @override
  ExtentScrollBehavior createScrollBehavior() {
    return flag
      ? new BoundedBehavior(platform: platform)
      : new UnboundedBehavior(platform: platform);
  }

  @override
Adam Barth's avatar
Adam Barth committed
25
  bool updateShouldNotify(TestScrollConfigurationDelegate old) => flag != old.flag;
26
}
27 28 29

void main() {
  test('BoundedBehavior min scroll offset', () {
30 31 32 33 34 35 36
    BoundedBehavior behavior = new BoundedBehavior(
      contentExtent: 150.0,
      containerExtent: 75.0,
      minScrollOffset: -100.0,
      platform: TargetPlatform.iOS
    );

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    expect(behavior.minScrollOffset, equals(-100.0));
    expect(behavior.maxScrollOffset, equals(-25.0));

    double scrollOffset = behavior.updateExtents(
      contentExtent: 125.0,
      containerExtent: 50.0,
      scrollOffset: -80.0
    );

    expect(behavior.minScrollOffset, equals(-100.0));
    expect(behavior.maxScrollOffset, equals(-25.0));
    expect(scrollOffset, equals(-80.0));

    scrollOffset = behavior.updateExtents(
      minScrollOffset: 50.0,
      scrollOffset: scrollOffset
    );

    expect(behavior.minScrollOffset, equals(50.0));
    expect(behavior.maxScrollOffset, equals(125.0));
    expect(scrollOffset, equals(50.0));
  });
59 60

  testWidgets('Inherited ScrollConfiguration changed', (WidgetTester tester) async {
61
    final GlobalKey key = new GlobalKey(debugLabel: 'scrollable');
62 63 64 65 66 67 68
    TestScrollConfigurationDelegate delegate;
    ExtentScrollBehavior behavior;

    await tester.pumpWidget(
      new ScrollConfiguration(
        delegate: new TestScrollConfigurationDelegate(true),
        child: new ScrollableViewport(
69
          key: key,
70 71 72 73 74 75 76 77 78 79 80 81 82
          child: new Builder(
            builder: (BuildContext context) {
              delegate = ScrollConfiguration.of(context);
              behavior = Scrollable.of(context).scrollBehavior;
              return new Container(height: 1000.0);
            }
          )
        )
      )
    );

    expect(delegate, isNotNull);
    expect(delegate.flag, isTrue);
83
    expect(behavior, const isInstanceOf<BoundedBehavior>());
84 85
    expect(behavior.contentExtent, equals(1000.0));
    expect(behavior.containerExtent, equals(600.0));
86 87 88 89 90 91

    // Same Scrollable, different ScrollConfiguration
    await tester.pumpWidget(
      new ScrollConfiguration(
        delegate: new TestScrollConfigurationDelegate(false),
        child: new ScrollableViewport(
92
          key: key,
93 94 95 96 97 98 99 100 101 102 103 104 105
          child: new Builder(
            builder: (BuildContext context) {
              delegate = ScrollConfiguration.of(context);
              behavior = Scrollable.of(context).scrollBehavior;
              return new Container(height: 1000.0);
            }
          )
        )
      )
    );

    expect(delegate, isNotNull);
    expect(delegate.flag, isFalse);
106
    expect(behavior, const isInstanceOf<UnboundedBehavior>());
107 108 109
    // Regression test for https://github.com/flutter/flutter/issues/5856
    expect(behavior.contentExtent, equals(1000.0));
    expect(behavior.containerExtent, equals(600.0));
110
  });
111
}