slivers_block_test.dart 9.35 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';

11
Future<void> test(WidgetTester tester, double offset) {
12
  return tester.pumpWidget(
13
    Directionality(
14
      textDirection: TextDirection.ltr,
15 16
      child: Viewport(
        offset: ViewportOffset.fixed(offset),
17
        slivers: const <Widget>[
18 19 20 21 22 23 24
          SliverList(
            delegate: SliverChildListDelegate(<Widget>[
              SizedBox(height: 400.0, child: Text('a')),
              SizedBox(height: 400.0, child: Text('b')),
              SizedBox(height: 400.0, child: Text('c')),
              SizedBox(height: 400.0, child: Text('d')),
              SizedBox(height: 400.0, child: 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 = GlobalKey();
    final ViewportOffset offset = ViewportOffset.zero();
82
    await tester.pumpWidget(
83
      Directionality(
84
        textDirection: TextDirection.ltr,
85
        child: Viewport(
86 87
          offset: offset,
          slivers: <Widget>[
88 89
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
90 91
                const SizedBox(height: 251.0, child: Text('a')),
                const SizedBox(height: 252.0, child: Text('b')),
92
                SizedBox(key: key1, height: 253.0, child: const Text('c')),
93 94 95
              ]),
            ),
          ],
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
    await tester.pumpWidget(
105
      Directionality(
106
        textDirection: TextDirection.ltr,
107
        child: Viewport(
108 109
          offset: offset,
          slivers: <Widget>[
110 111 112
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
                SizedBox(key: key1, height: 253.0, child: const Text('c')),
113 114
                const SizedBox(height: 251.0, child: Text('a')),
                const SizedBox(height: 252.0, child: 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
    await tester.pumpWidget(
127
      Directionality(
128
        textDirection: TextDirection.ltr,
129
        child: Viewport(
130 131
          offset: offset,
          slivers: <Widget>[
132 133
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
134
                const SizedBox(height: 251.0, child: Text('a')),
135
                SizedBox(key: key1, height: 253.0, child: const Text('c')),
136
                const SizedBox(height: 252.0, child: 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
    await tester.pumpWidget(
149
      Directionality(
150
        textDirection: TextDirection.ltr,
151
        child: Viewport(
152
          offset: offset,
153
          slivers: const <Widget>[
154 155 156 157
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
                SizedBox(height: 251.0, child: Text('a')),
                SizedBox(height: 252.0, child: 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
    await tester.pumpWidget(
169
      Directionality(
170
        textDirection: TextDirection.ltr,
171
        child: Viewport(
172 173
          offset: offset,
          slivers: <Widget>[
174 175
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
176
                const SizedBox(height: 251.0, child: Text('a')),
177
                SizedBox(key: key1, height: 253.0, child: const Text('c')),
178
                const SizedBox(height: 252.0, child: 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
    await tester.pumpWidget(
194
      Directionality(
195
        textDirection: TextDirection.ltr,
196 197
        child: Viewport(
          offset: ViewportOffset.zero(),
198
          slivers: const <Widget>[
199 200
            SliverToBoxAdapter(
              child: SizedBox(height: 400.0, child: 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
    await tester.pumpWidget(
210
      Directionality(
211
        textDirection: TextDirection.ltr,
212 213
        child: Viewport(
          offset: ViewportOffset.fixed(100.0),
214
          slivers: const <Widget>[
215 216
            SliverToBoxAdapter(
              child: SizedBox(height: 400.0, child: Text('a')),
217 218
            ),
          ],
219
        ),
220 221
      ),
    );
222

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

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

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

241
    await tester.pumpWidget(
242
      Directionality(
243
        textDirection: TextDirection.ltr,
244 245
        child: Viewport(
          offset: ViewportOffset.zero(),
246
          slivers: const <Widget>[
247 248
            SliverToBoxAdapter(
              child: SizedBox(height: 4000.0, child: 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
    await tester.pumpWidget(
260
      Directionality(
261
        textDirection: TextDirection.ltr,
262 263
        child: Viewport(
          offset: ViewportOffset.zero(),
264
          slivers: const <Widget>[
265 266 267
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
                SizedBox(height: 400.0, child: 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
    await tester.pumpWidget(
278
      Directionality(
279
        textDirection: TextDirection.ltr,
280 281
        child: Viewport(
          offset: ViewportOffset.fixed(100.0),
282
          slivers: const <Widget>[
283 284 285
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
                SizedBox(height: 400.0, child: 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
    await tester.pumpWidget(
296
      Directionality(
297
        textDirection: TextDirection.ltr,
298 299
        child: Viewport(
          offset: ViewportOffset.fixed(100.0),
300
          slivers: const <Widget>[
301 302 303
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
                SizedBox(height: 4000.0, child: 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
    await tester.pumpWidget(
314
      Directionality(
315
        textDirection: TextDirection.ltr,
316 317
        child: Viewport(
          offset: ViewportOffset.zero(),
318
          slivers: const <Widget>[
319 320 321
            SliverList(
              delegate: SliverChildListDelegate(<Widget>[
                SizedBox(height: 4000.0, child: 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
}