single_child_scroll_view_test.dart 5.35 KB
Newer Older
1 2 3 4 5 6 7
// 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';

8
class TestScrollPosition extends ScrollPositionWithSingleContext {
9 10
  TestScrollPosition({
    ScrollPhysics physics,
11
    ScrollContext state,
12 13 14 15
    double initialPixels: 0.0,
    ScrollPosition oldPosition,
  }) : super(
    physics: physics,
16
    context: state,
17 18 19 20 21 22 23
    initialPixels: initialPixels,
    oldPosition: oldPosition,
  );
}

class TestScrollController extends ScrollController {
  @override
24
  ScrollPosition createScrollPosition(ScrollPhysics physics, ScrollContext context, ScrollPosition oldPosition) {
25 26
    return new TestScrollPosition(
      physics: physics,
27
      state: context,
28 29 30 31 32 33
      initialPixels: initialScrollOffset,
      oldPosition: oldPosition,
    );
  }
}

34 35 36 37 38
void main() {
  testWidgets('SingleChildScrollView control test', (WidgetTester tester) async {
    await tester.pumpWidget(new SingleChildScrollView(
      child: new Container(
        height: 2000.0,
39
        color: const Color(0xFF00FF00),
40 41 42
      ),
    ));

43
    final RenderBox box = tester.renderObject(find.byType(Container));
44
    expect(box.localToGlobal(Offset.zero), equals(Offset.zero));
45

46
    await tester.drag(find.byType(SingleChildScrollView), const Offset(-200.0, -200.0));
47

48
    expect(box.localToGlobal(Offset.zero), equals(const Offset(0.0, -200.0)));
49
  });
50 51

  testWidgets('Changing controllers changes scroll position', (WidgetTester tester) async {
52
    final TestScrollController controller = new TestScrollController();
53 54 55 56

    await tester.pumpWidget(new SingleChildScrollView(
      child: new Container(
        height: 2000.0,
57
        color: const Color(0xFF00FF00),
58 59 60 61 62 63 64
      ),
    ));

    await tester.pumpWidget(new SingleChildScrollView(
      controller: controller,
      child: new Container(
        height: 2000.0,
65
        color: const Color(0xFF00FF00),
66 67 68
      ),
    ));

69
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
70 71 72
    expect(scrollable.position, const isInstanceOf<TestScrollPosition>());
  });

73
  testWidgets('Sets PrimaryScrollController when primary', (WidgetTester tester) async {
74
    final ScrollController primaryScrollController = new ScrollController();
75 76 77 78 79 80
    await tester.pumpWidget(new PrimaryScrollController(
      controller: primaryScrollController,
      child: new SingleChildScrollView(
        primary: true,
        child: new Container(
          height: 2000.0,
81
          color: const Color(0xFF00FF00),
82 83 84 85
        ),
      ),
    ));

86
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
87 88 89 90
    expect(scrollable.controller, primaryScrollController);
  });


91
  testWidgets('Changing scroll controller inside dirty layout builder does not assert', (WidgetTester tester) async {
92
    final ScrollController controller = new ScrollController();
93 94 95 96 97 98 99 100 101

    await tester.pumpWidget(new Center(
      child: new SizedBox(
        width: 750.0,
        child: new LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            return new SingleChildScrollView(
              child: new Container(
                height: 2000.0,
102
                color: const Color(0xFF00FF00),
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
              ),
            );
          },
        ),
      ),
    ));

    await tester.pumpWidget(new Center(
      child: new SizedBox(
        width: 700.0,
        child: new LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            return new SingleChildScrollView(
              controller: controller,
              child: new Container(
                height: 2000.0,
119
                color: const Color(0xFF00FF00),
120 121 122 123 124 125 126
              ),
            );
          },
        ),
      ),
    ));
  });
127 128

  testWidgets('Vertical SingleChildScrollViews are primary by default', (WidgetTester tester) async {
129
    final SingleChildScrollView view = new SingleChildScrollView(scrollDirection: Axis.vertical);
130 131 132 133
    expect(view.primary, isTrue);
  });

  testWidgets('Horizontal SingleChildScrollViews are non-primary by default', (WidgetTester tester) async {
134
    final SingleChildScrollView view = new SingleChildScrollView(scrollDirection: Axis.horizontal);
135 136 137 138
    expect(view.primary, isFalse);
  });

  testWidgets('SingleChildScrollViews with controllers are non-primary by default', (WidgetTester tester) async {
139
    final SingleChildScrollView view = new SingleChildScrollView(
140 141 142 143 144 145 146 147
      controller: new ScrollController(),
      scrollDirection: Axis.vertical,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('Nested scrollables have a null PrimaryScrollController', (WidgetTester tester) async {
    const Key innerKey = const Key('inner');
148
    final ScrollController primaryScrollController = new ScrollController();
149 150 151 152 153
    await tester.pumpWidget(new PrimaryScrollController(
      controller: primaryScrollController,
      child: new SingleChildScrollView(
        primary: true,
        child: new Container(
154
          constraints: const BoxConstraints(maxHeight: 200.0),
155 156 157 158 159
          child: new ListView(key: innerKey, primary: true),
        ),
      ),
    ));

160
    final Scrollable innerScrollable = tester.widget(
161 162 163 164 165 166 167
      find.descendant(
        of: find.byKey(innerKey),
        matching: find.byType(Scrollable),
      ),
    );
    expect(innerScrollable.controller, isNull);
  });
168
}