slivers_block_test.dart 9.78 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8
// 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/rendering.dart';
import 'package:flutter/widgets.dart';

9 10
import '../rendering/mock_canvas.dart';

Ian Hickson's avatar
Ian Hickson committed
11
Future<Null> test(WidgetTester tester, double offset) {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
  return tester.pumpWidget(
    new Directionality(
      textDirection: TextDirection.ltr,
      child: new Viewport(
        offset: new ViewportOffset.fixed(offset),
        slivers: <Widget>[
          new SliverList(
            delegate: new SliverChildListDelegate(<Widget>[
              const SizedBox(height: 400.0, child: const Text('a')),
              const SizedBox(height: 400.0, child: const Text('b')),
              const SizedBox(height: 400.0, child: const Text('c')),
              const SizedBox(height: 400.0, child: const Text('d')),
              const SizedBox(height: 400.0, child: const Text('e')),
            ]),
          ),
        ],
Ian Hickson's avatar
Ian Hickson committed
28
      ),
29 30
    ),
  );
Ian Hickson's avatar
Ian Hickson committed
31 32
}

33 34 35
void verify(WidgetTester tester, List<Offset> answerKey, String text) {
  final List<Offset> testAnswers = tester.renderObjectList<RenderBox>(find.byType(SizedBox)).map<Offset>(
    (RenderBox target) => target.localToGlobal(const Offset(0.0, 0.0))
Ian Hickson's avatar
Ian Hickson committed
36 37 38
  ).toList();
  expect(testAnswers, equals(answerKey));
  final String foundText =
Ian Hickson's avatar
Ian Hickson committed
39 40
    tester.widgetList<Text>(find.byType(Text))
    .map<String>((Text widget) => widget.data)
Ian Hickson's avatar
Ian Hickson committed
41 42 43 44 45
    .reduce((String value, String element) => value + element);
  expect(foundText, equals(text));
}

void main() {
Adam Barth's avatar
Adam Barth committed
46
  testWidgets('Viewport+SliverBlock basic test', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
47
    await test(tester, 0.0);
Adam Barth's avatar
Adam Barth committed
48
    expect(tester.renderObject<RenderBox>(find.byType(Viewport)).size, equals(const Size(800.0, 600.0)));
49 50 51
    verify(tester, <Offset>[
      const Offset(0.0, 0.0),
      const Offset(0.0, 400.0),
Ian Hickson's avatar
Ian Hickson committed
52 53 54
    ], 'ab');

    await test(tester, 200.0);
55 56 57
    verify(tester, <Offset>[
      const Offset(0.0, -200.0),
      const Offset(0.0, 200.0),
Ian Hickson's avatar
Ian Hickson committed
58 59 60
    ], 'ab');

    await test(tester, 600.0);
61 62 63
    verify(tester, <Offset>[
      const Offset(0.0, -200.0),
      const Offset(0.0, 200.0),
Ian Hickson's avatar
Ian Hickson committed
64 65 66
    ], 'bc');

    await test(tester, 900.0);
67 68 69
    verify(tester, <Offset>[
      const Offset(0.0, -100.0),
      const Offset(0.0, 300.0),
Ian Hickson's avatar
Ian Hickson committed
70 71 72
    ], 'cd');

    await test(tester, 200.0);
73 74 75
    verify(tester, <Offset>[
      const Offset(0.0, -200.0),
      const Offset(0.0, 200.0),
Ian Hickson's avatar
Ian Hickson committed
76 77 78
    ], 'ab');
  });

Adam Barth's avatar
Adam Barth committed
79
  testWidgets('Viewport with GlobalKey reparenting', (WidgetTester tester) async {
80 81
    final Key key1 = new GlobalKey();
    final ViewportOffset offset = new ViewportOffset.zero();
82 83 84 85 86 87 88 89 90 91 92 93 94 95
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                const SizedBox(height: 251.0, child: const Text('a')),
                const SizedBox(height: 252.0, child: const Text('b')),
                new SizedBox(key: key1, height: 253.0, child: const Text('c')),
              ]),
            ),
          ],
Ian Hickson's avatar
Ian Hickson committed
96
        ),
97 98
      ),
    );
99 100 101 102
    verify(tester, <Offset>[
      const Offset(0.0, 0.0),
      const Offset(0.0, 251.0),
      const Offset(0.0, 503.0),
Ian Hickson's avatar
Ian Hickson committed
103
    ], 'abc');
104 105 106 107 108 109 110 111 112 113 114 115 116 117
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                new SizedBox(key: key1, height: 253.0, child: const Text('c')),
                const SizedBox(height: 251.0, child: const Text('a')),
                const SizedBox(height: 252.0, child: const Text('b')),
              ]),
            ),
          ],
Ian Hickson's avatar
Ian Hickson committed
118
        ),
119 120
      ),
    );
121 122 123 124
    verify(tester, <Offset>[
      const Offset(0.0, 0.0),
      const Offset(0.0, 253.0),
      const Offset(0.0, 504.0),
Ian Hickson's avatar
Ian Hickson committed
125
    ], 'cab');
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                const SizedBox(height: 251.0, child: const Text('a')),
                new SizedBox(key: key1, height: 253.0, child: const Text('c')),
                const SizedBox(height: 252.0, child: const Text('b')),
              ]),
            ),
          ],
Ian Hickson's avatar
Ian Hickson committed
140
        ),
141 142
      ),
    );
143 144 145 146
    verify(tester, <Offset>[
      const Offset(0.0, 0.0),
      const Offset(0.0, 251.0),
      const Offset(0.0, 504.0),
Ian Hickson's avatar
Ian Hickson committed
147
    ], 'acb');
148 149 150 151 152 153 154 155 156 157 158 159 160
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                const SizedBox(height: 251.0, child: const Text('a')),
                const SizedBox(height: 252.0, child: const Text('b')),
              ]),
            ),
          ],
Ian Hickson's avatar
Ian Hickson committed
161
        ),
162 163
      ),
    );
164 165 166
    verify(tester, <Offset>[
      const Offset(0.0, 0.0),
      const Offset(0.0, 251.0),
Ian Hickson's avatar
Ian Hickson committed
167
    ], 'ab');
168 169 170 171 172 173 174 175 176 177 178 179 180 181
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                const SizedBox(height: 251.0, child: const Text('a')),
                new SizedBox(key: key1, height: 253.0, child: const Text('c')),
                const SizedBox(height: 252.0, child: const Text('b')),
              ]),
            ),
          ],
Ian Hickson's avatar
Ian Hickson committed
182
        ),
183 184
      ),
    );
185 186 187 188
    verify(tester, <Offset>[
      const Offset(0.0, 0.0),
      const Offset(0.0, 251.0),
      const Offset(0.0, 504.0),
Ian Hickson's avatar
Ian Hickson committed
189 190
    ], 'acb');
  });
191

Adam Barth's avatar
Adam Barth committed
192
  testWidgets('Viewport overflow clipping of SliverToBoxAdapter', (WidgetTester tester) async {
193 194 195 196 197 198 199 200 201 202
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.zero(),
          slivers: <Widget>[
            const SliverToBoxAdapter(
              child: const SizedBox(height: 400.0, child: const Text('a')),
            ),
          ],
203
        ),
204 205
      ),
    );
206

Adam Barth's avatar
Adam Barth committed
207
    expect(find.byType(Viewport), isNot(paints..clipRect()));
208

209 210 211 212 213 214 215 216 217 218
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.fixed(100.0),
          slivers: <Widget>[
            const SliverToBoxAdapter(
              child: const SizedBox(height: 400.0, child: const Text('a')),
            ),
          ],
219
        ),
220 221
      ),
    );
222

Adam Barth's avatar
Adam Barth committed
223
    expect(find.byType(Viewport), paints..clipRect());
224

225 226 227 228 229 230 231 232 233 234
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.fixed(100.0),
          slivers: <Widget>[
            const SliverToBoxAdapter(
              child: const SizedBox(height: 4000.0, child: const Text('a')),
            ),
          ],
235
        ),
236 237
      ),
    );
238

Adam Barth's avatar
Adam Barth committed
239
    expect(find.byType(Viewport), paints..clipRect());
240

241 242 243 244 245 246 247 248 249 250
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.zero(),
          slivers: <Widget>[
            const SliverToBoxAdapter(
              child: const SizedBox(height: 4000.0, child: const Text('a')),
            ),
          ],
251
        ),
252 253
      ),
    );
254

Adam Barth's avatar
Adam Barth committed
255
    expect(find.byType(Viewport), paints..clipRect());
256 257
  });

Adam Barth's avatar
Adam Barth committed
258
  testWidgets('Viewport overflow clipping of SliverBlock', (WidgetTester tester) async {
259 260 261 262 263 264 265 266 267 268 269 270
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.zero(),
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                const SizedBox(height: 400.0, child: const Text('a')),
              ]),
            ),
          ],
271
        ),
272 273
      ),
    );
274

Adam Barth's avatar
Adam Barth committed
275
    expect(find.byType(Viewport), isNot(paints..clipRect()));
276

277 278 279 280 281 282 283 284 285 286 287 288
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.fixed(100.0),
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                const SizedBox(height: 400.0, child: const Text('a')),
              ]),
            ),
          ],
289
        ),
290 291
      ),
    );
292

Adam Barth's avatar
Adam Barth committed
293
    expect(find.byType(Viewport), paints..clipRect());
294

295 296 297 298 299 300 301 302 303 304 305 306
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.fixed(100.0),
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                const SizedBox(height: 4000.0, child: const Text('a')),
              ]),
            ),
          ],
307
        ),
308 309
      ),
    );
310

Adam Barth's avatar
Adam Barth committed
311
    expect(find.byType(Viewport), paints..clipRect());
312

313 314 315 316 317 318 319 320 321 322 323 324
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.zero(),
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
                const SizedBox(height: 4000.0, child: const Text('a')),
              ]),
            ),
          ],
325
        ),
326 327
      ),
    );
328

Adam Barth's avatar
Adam Barth committed
329
    expect(find.byType(Viewport), paints..clipRect());
330
  });
Ian Hickson's avatar
Ian Hickson committed
331
}