slivers_block_test.dart 9.9 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
  return tester.pumpWidget(
    new Directionality(
      textDirection: TextDirection.ltr,
      child: new Viewport(
        offset: new ViewportOffset.fixed(offset),
17
        slivers: const <Widget>[
18 19 20 21 22 23 24
          const SliverList(
            delegate: const SliverChildListDelegate(const <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')),
25 26 27
            ]),
          ),
        ],
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
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
90 91
                const SizedBox(height: 251.0, child: const Text('a')),
                const SizedBox(height: 252.0, child: const Text('b')),
92 93 94 95
                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
    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')),
113 114
                const SizedBox(height: 251.0, child: const Text('a')),
                const SizedBox(height: 252.0, child: const Text('b')),
115 116 117
              ]),
            ),
          ],
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
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
134
                const SizedBox(height: 251.0, child: const Text('a')),
135
                new SizedBox(key: key1, height: 253.0, child: const Text('c')),
136
                const SizedBox(height: 252.0, child: const Text('b')),
137 138 139
              ]),
            ),
          ],
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
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
153
          slivers: const <Widget>[
154 155 156 157
            const SliverList(
              delegate: const SliverChildListDelegate(const <Widget>[
                const SizedBox(height: 251.0, child: const Text('a')),
                const SizedBox(height: 252.0, child: const Text('b')),
158 159 160
              ]),
            ),
          ],
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
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: offset,
          slivers: <Widget>[
            new SliverList(
              delegate: new SliverChildListDelegate(<Widget>[
176
                const SizedBox(height: 251.0, child: const Text('a')),
177
                new SizedBox(key: key1, height: 253.0, child: const Text('c')),
178
                const SizedBox(height: 252.0, child: const Text('b')),
179 180 181
              ]),
            ),
          ],
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
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.zero(),
198
          slivers: const <Widget>[
199 200
            const SliverToBoxAdapter(
              child: const SizedBox(height: 400.0, child: const Text('a')),
201 202
            ),
          ],
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
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.fixed(100.0),
214
          slivers: const <Widget>[
215 216
            const SliverToBoxAdapter(
              child: const SizedBox(height: 400.0, child: const Text('a')),
217 218
            ),
          ],
219
        ),
220 221
      ),
    );
222

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

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

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

241 242 243 244 245
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.zero(),
246
          slivers: const <Widget>[
247 248
            const SliverToBoxAdapter(
              child: const SizedBox(height: 4000.0, child: const Text('a')),
249 250
            ),
          ],
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
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.zero(),
264
          slivers: const <Widget>[
265 266 267
            const SliverList(
              delegate: const SliverChildListDelegate(const <Widget>[
                const SizedBox(height: 400.0, child: const Text('a')),
268 269 270
              ]),
            ),
          ],
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
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.fixed(100.0),
282
          slivers: const <Widget>[
283 284 285
            const SliverList(
              delegate: const SliverChildListDelegate(const <Widget>[
                const SizedBox(height: 400.0, child: const Text('a')),
286 287 288
              ]),
            ),
          ],
289
        ),
290 291
      ),
    );
292

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

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

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

313 314 315 316 317
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Viewport(
          offset: new ViewportOffset.zero(),
318
          slivers: const <Widget>[
319 320 321
            const SliverList(
              delegate: const SliverChildListDelegate(const <Widget>[
                const SizedBox(height: 4000.0, child: const Text('a')),
322 323 324
              ]),
            ),
          ],
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
}