slivers_test.dart 7.05 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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';
import 'package:flutter/rendering.dart';

Future<Null> test(WidgetTester tester, double offset, { double anchor: 0.0 }) {
10 11 12 13 14 15
  return tester.pumpWidget(
    new Directionality(
      textDirection: TextDirection.ltr,
      child: new Viewport(
        anchor: anchor / 600.0,
        offset: new ViewportOffset.fixed(offset),
16
        slivers: const <Widget>[
17 18 19 20 21 22 23 24 25
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
        ],
      ),
    ),
  );
26 27
}

28 29 30
void verify(WidgetTester tester, List<Offset> idealPositions, List<bool> idealVisibles) {
  final List<Offset> actualPositions = tester.renderObjectList<RenderBox>(find.byType(SizedBox)).map<Offset>(
    (RenderBox target) => target.localToGlobal(const Offset(0.0, 0.0))
31
  ).toList();
32
  final List<bool> actualVisibles = tester.renderObjectList<RenderSliverToBoxAdapter>(find.byType(SliverToBoxAdapter)).map<bool>(
33 34 35 36 37 38 39
    (RenderSliverToBoxAdapter target) => target.geometry.visible
  ).toList();
  expect(actualPositions, equals(idealPositions));
  expect(actualVisibles, equals(idealVisibles));
}

void main() {
Adam Barth's avatar
Adam Barth committed
40
  testWidgets('Viewport basic test', (WidgetTester tester) async {
41
    await test(tester, 0.0);
Adam Barth's avatar
Adam Barth committed
42
    expect(tester.renderObject<RenderBox>(find.byType(Viewport)).size, equals(const Size(800.0, 600.0)));
43 44 45 46 47 48
    verify(tester, <Offset>[
      const Offset(0.0, 0.0),
      const Offset(0.0, 400.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
49 50 51
    ], <bool>[true, true, false, false, false]);

    await test(tester, 200.0);
52 53 54 55 56 57
    verify(tester, <Offset>[
      const Offset(0.0, -200.0),
      const Offset(0.0, 200.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
58 59 60
    ], <bool>[true, true, false, false, false]);

    await test(tester, 600.0);
61 62 63 64 65 66
    verify(tester, <Offset>[
      const Offset(0.0, -600.0),
      const Offset(0.0, -200.0),
      const Offset(0.0, 200.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
67 68 69
    ], <bool>[false, true, true, false, false]);

    await test(tester, 900.0);
70 71 72 73 74 75
    verify(tester, <Offset>[
      const Offset(0.0, -900.0),
      const Offset(0.0, -500.0),
      const Offset(0.0, -100.0),
      const Offset(0.0, 300.0),
      const Offset(0.0, 600.0),
76 77 78
    ], <bool>[false, false, true, true, false]);
  });

Adam Barth's avatar
Adam Barth committed
79
  testWidgets('Viewport anchor test', (WidgetTester tester) async {
80
    await test(tester, 0.0, anchor: 100.0);
Adam Barth's avatar
Adam Barth committed
81
    expect(tester.renderObject<RenderBox>(find.byType(Viewport)).size, equals(const Size(800.0, 600.0)));
82 83 84 85 86 87
    verify(tester, <Offset>[
      const Offset(0.0, 100.0),
      const Offset(0.0, 500.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
88 89 90
    ], <bool>[true, true, false, false, false]);

    await test(tester, 200.0, anchor: 100.0);
91 92 93 94 95 96
    verify(tester, <Offset>[
      const Offset(0.0, -100.0),
      const Offset(0.0, 300.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
97 98 99
    ], <bool>[true, true, false, false, false]);

    await test(tester, 600.0, anchor: 100.0);
100 101 102 103 104 105
    verify(tester, <Offset>[
      const Offset(0.0, -500.0),
      const Offset(0.0, -100.0),
      const Offset(0.0, 300.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
106 107 108
    ], <bool>[false, true, true, false, false]);

    await test(tester, 900.0, anchor: 100.0);
109 110 111 112 113 114
    verify(tester, <Offset>[
      const Offset(0.0, -800.0),
      const Offset(0.0, -400.0),
      const Offset(0.0, 0.0),
      const Offset(0.0, 400.0),
      const Offset(0.0, 600.0),
115 116
    ], <bool>[false, false, true, true, false]);
  });
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

  testWidgets('Multiple grids and lists', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new SizedBox(
          width: 44.4,
          height: 60.0,
          child: new Directionality(
            textDirection: TextDirection.ltr,
            child: new CustomScrollView(
              slivers: <Widget>[
                new SliverList(
                  delegate: new SliverChildListDelegate(
                    <Widget>[
                      new Container(height: 22.2, child: const Text('TOP')),
                      new Container(height: 22.2),
                      new Container(height: 22.2),
                    ],
                  ),
                ),
                new SliverFixedExtentList(
                  itemExtent: 22.2,
                  delegate: new SliverChildListDelegate(
                    <Widget>[
                      new Container(),
                      new Container(child: const Text('A')),
                      new Container(),
                    ],
                  ),
                ),
                new SliverGrid(
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                  ),
                  delegate: new SliverChildListDelegate(
                    <Widget>[
                      new Container(),
                      new Container(child: const Text('B')),
                      new Container(),
                    ],
                  ),
                ),
                new SliverList(
                  delegate: new SliverChildListDelegate(
                    <Widget>[
                      new Container(height: 22.2),
                      new Container(height: 22.2),
                      new Container(height: 22.2, child: const Text('BOTTOM')),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
    final TestGesture gesture = await tester.startGesture(const Offset(400.0, 300.0));
    expect(find.text('TOP'), findsOneWidget);
    expect(find.text('A'), findsNothing);
    expect(find.text('B'), findsNothing);
    expect(find.text('BOTTOM'), findsNothing);
    await gesture.moveBy(const Offset(0.0, -70.0));
    await tester.pump();
    expect(find.text('TOP'), findsNothing);
    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsNothing);
    expect(find.text('BOTTOM'), findsNothing);
    await gesture.moveBy(const Offset(0.0, -70.0));
    await tester.pump();
    expect(find.text('TOP'), findsNothing);
    expect(find.text('A'), findsNothing);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('BOTTOM'), findsNothing);
    await gesture.moveBy(const Offset(0.0, -70.0));
    await tester.pump();
    expect(find.text('TOP'), findsNothing);
    expect(find.text('A'), findsNothing);
    expect(find.text('B'), findsNothing);
    expect(find.text('BOTTOM'), findsOneWidget);
  });
198
}