1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2015 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/rendering.dart';
import 'package:flutter/animation.dart';
import 'package:meta/meta.dart';
import '../flutter_test_alternative.dart';
import 'rendering_tester.dart';
class TestRenderSliverBoxChildManager extends RenderSliverBoxChildManager {
TestRenderSliverBoxChildManager({
this.children,
});
RenderSliverList _renderObject;
List<RenderBox> children;
RenderSliverList createRenderObject() {
assert(_renderObject == null);
_renderObject = RenderSliverList(childManager: this);
return _renderObject;
}
int _currentlyUpdatingChildIndex;
@override
void createChild(int index, { @required RenderBox after }) {
if (index < 0 || index >= children.length)
return;
try {
_currentlyUpdatingChildIndex = index;
_renderObject.insert(children[index], after: after);
} finally {
_currentlyUpdatingChildIndex = null;
}
}
@override
void removeChild(RenderBox child) {
_renderObject.remove(child);
}
@override
double estimateMaxScrollOffset(
SliverConstraints constraints, {
int firstIndex,
int lastIndex,
double leadingScrollOffset,
double trailingScrollOffset,
}) {
assert(lastIndex >= firstIndex);
return children.length * (trailingScrollOffset - leadingScrollOffset) / (lastIndex - firstIndex + 1);
}
@override
int get childCount => children.length;
@override
void didAdoptChild(RenderBox child) {
assert(_currentlyUpdatingChildIndex != null);
final SliverMultiBoxAdaptorParentData childParentData = child.parentData;
childParentData.index = _currentlyUpdatingChildIndex;
}
@override
void setDidUnderflow(bool value) { }
}
class ViewportOffsetSpy extends ViewportOffset {
ViewportOffsetSpy(this._pixels);
double _pixels;
@override
double get pixels => _pixels;
bool corrected = false;
@override
bool applyViewportDimension(double viewportDimension) => true;
@override
bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) => true;
@override
void correctBy(double correction) {
_pixels += correction;
corrected = true;
}
@override
void jumpTo(double pixels) {
// Do nothing, not required in test.
}
@override
Future<void> animateTo(
double to, {
@required Duration duration,
@required Curve curve,
}) async {
// Do nothing, not required in test.
}
@override
ScrollDirection get userScrollDirection => ScrollDirection.idle;
@override
bool get allowImplicitScrolling => false;
}
void main() {
test('RenderSliverList basic test - down', () {
RenderObject inner;
RenderBox a, b, c, d, e;
final TestRenderSliverBoxChildManager childManager = TestRenderSliverBoxChildManager(
children: <RenderBox>[
a = RenderSizedBox(const Size(100.0, 400.0)),
b = RenderSizedBox(const Size(100.0, 400.0)),
c = RenderSizedBox(const Size(100.0, 400.0)),
d = RenderSizedBox(const Size(100.0, 400.0)),
e = RenderSizedBox(const Size(100.0, 400.0)),
],
);
final RenderViewport root = RenderViewport(
axisDirection: AxisDirection.down,
crossAxisDirection: AxisDirection.right,
offset: ViewportOffset.zero(),
cacheExtent: 0.0,
children: <RenderSliver>[
inner = childManager.createRenderObject(),
],
);
layout(root);
expect(root.size.width, equals(800.0));
expect(root.size.height, equals(600.0));
expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 0.0));
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 400.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
// make sure that layout is stable by laying out again
inner.markNeedsLayout();
pumpFrame();
expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 0.0));
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 400.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
// now try various scroll offsets
root.offset = ViewportOffset.fixed(200.0);
pumpFrame();
expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -200.0));
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 200.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
root.offset = ViewportOffset.fixed(600.0);
pumpFrame();
expect(a.attached, false);
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -200.0));
expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 200.0));
expect(d.attached, false);
expect(e.attached, false);
root.offset = ViewportOffset.fixed(900.0);
pumpFrame();
expect(a.attached, false);
expect(b.attached, false);
expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -100.0));
expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 300.0));
expect(e.attached, false);
// try going back up
root.offset = ViewportOffset.fixed(200.0);
pumpFrame();
expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -200.0));
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 200.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
});
test('RenderSliverList basic test - up', () {
RenderObject inner;
RenderBox a, b, c, d, e;
final TestRenderSliverBoxChildManager childManager = TestRenderSliverBoxChildManager(
children: <RenderBox>[
a = RenderSizedBox(const Size(100.0, 400.0)),
b = RenderSizedBox(const Size(100.0, 400.0)),
c = RenderSizedBox(const Size(100.0, 400.0)),
d = RenderSizedBox(const Size(100.0, 400.0)),
e = RenderSizedBox(const Size(100.0, 400.0)),
],
);
final RenderViewport root = RenderViewport(
axisDirection: AxisDirection.up,
crossAxisDirection: AxisDirection.right,
offset: ViewportOffset.zero(),
children: <RenderSliver>[
inner = childManager.createRenderObject(),
],
cacheExtent: 0.0,
);
layout(root);
expect(root.size.width, equals(800.0));
expect(root.size.height, equals(600.0));
expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 200.0));
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -200.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
// make sure that layout is stable by laying out again
inner.markNeedsLayout();
pumpFrame();
expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 200.0));
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -200.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
// now try various scroll offsets
root.offset = ViewportOffset.fixed(200.0);
pumpFrame();
expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 400.0));
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 0.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
root.offset = ViewportOffset.fixed(600.0);
pumpFrame();
expect(a.attached, false);
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 400.0));
expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 0.0));
expect(d.attached, false);
expect(e.attached, false);
root.offset = ViewportOffset.fixed(900.0);
pumpFrame();
expect(a.attached, false);
expect(b.attached, false);
expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 300.0));
expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -100.0));
expect(e.attached, false);
// try going back up
root.offset = ViewportOffset.fixed(200.0);
pumpFrame();
expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 400.0));
expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 0.0));
expect(c.attached, false);
expect(d.attached, false);
expect(e.attached, false);
});
test('SliverList - no zero scroll offset correction', () {
RenderSliverList inner;
RenderBox a;
final TestRenderSliverBoxChildManager childManager = TestRenderSliverBoxChildManager(
children: <RenderBox>[
a = RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
],
);
final RenderViewport root = RenderViewport(
axisDirection: AxisDirection.down,
crossAxisDirection: AxisDirection.right,
offset: ViewportOffset.zero(),
children: <RenderSliver>[
inner = childManager.createRenderObject(),
],
);
layout(root);
final SliverMultiBoxAdaptorParentData parentData = a.parentData;
parentData.layoutOffset = 0.001;
root.offset = ViewportOffset.fixed(900.0);
pumpFrame();
root.offset = ViewportOffset.fixed(0.0);
pumpFrame();
expect(inner.geometry.scrollOffsetCorrection, isNull);
});
test('SliverList - no correction when tiny double precision error', () {
RenderSliverList inner;
RenderBox a;
final TestRenderSliverBoxChildManager childManager = TestRenderSliverBoxChildManager(
children: <RenderBox>[
a = RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
RenderSizedBox(const Size(100.0, 400.0)),
],
);
inner = childManager.createRenderObject();
final RenderViewport root = RenderViewport(
axisDirection: AxisDirection.down,
crossAxisDirection: AxisDirection.right,
offset: ViewportOffset.zero(),
children: <RenderSliver>[
inner,
],
);
layout(root);
final SliverMultiBoxAdaptorParentData parentData = a.parentData;
// Simulate double precision error.
parentData.layoutOffset = -0.0000000000001;
root.offset = ViewportOffset.fixed(900.0);
pumpFrame();
final ViewportOffsetSpy spy = ViewportOffsetSpy(0.0);
root.offset = spy;
pumpFrame();
expect(spy.corrected, false);
});
test('SliverMultiBoxAdaptorParentData.toString', () {
final SliverMultiBoxAdaptorParentData candidate = SliverMultiBoxAdaptorParentData();
expect(candidate.keepAlive, isFalse);
expect(candidate.index, isNull);
expect(candidate.toString(), 'index=null; layoutOffset=0.0');
candidate.keepAlive = null;
expect(candidate.toString(), 'index=null; layoutOffset=0.0');
candidate.keepAlive = true;
expect(candidate.toString(), 'index=null; keepAlive; layoutOffset=0.0');
candidate.keepAlive = false;
expect(candidate.toString(), 'index=null; layoutOffset=0.0');
candidate.index = 0;
expect(candidate.toString(), 'index=0; layoutOffset=0.0');
candidate.index = 1;
expect(candidate.toString(), 'index=1; layoutOffset=0.0');
candidate.index = -1;
expect(candidate.toString(), 'index=-1; layoutOffset=0.0');
});
}