slivers_block_test.dart 11.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
2 3 4 5
// 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';
6
import 'package:flutter/animation.dart';
Ian Hickson's avatar
Ian Hickson committed
7
import 'package:meta/meta.dart';
8

9
import '../flutter_test_alternative.dart';
Ian Hickson's avatar
Ian Hickson committed
10 11 12

import 'rendering_tester.dart';

Adam Barth's avatar
Adam Barth committed
13 14
class TestRenderSliverBoxChildManager extends RenderSliverBoxChildManager {
  TestRenderSliverBoxChildManager({
Ian Hickson's avatar
Ian Hickson committed
15 16 17
    this.children,
  });

18
  RenderSliverList _renderObject;
Ian Hickson's avatar
Ian Hickson committed
19 20
  List<RenderBox> children;

21
  RenderSliverList createRenderObject() {
Adam Barth's avatar
Adam Barth committed
22
    assert(_renderObject == null);
23
    _renderObject = RenderSliverList(childManager: this);
Adam Barth's avatar
Adam Barth committed
24 25 26 27 28
    return _renderObject;
  }

  int _currentlyUpdatingChildIndex;

Ian Hickson's avatar
Ian Hickson committed
29 30 31
  @override
  void createChild(int index, { @required RenderBox after }) {
    if (index < 0 || index >= children.length)
32
      return;
Adam Barth's avatar
Adam Barth committed
33 34 35 36 37 38 39 40 41 42 43
    try {
      _currentlyUpdatingChildIndex = index;
      _renderObject.insert(children[index], after: after);
    } finally {
      _currentlyUpdatingChildIndex = null;
    }
  }

  @override
  void removeChild(RenderBox child) {
    _renderObject.remove(child);
Ian Hickson's avatar
Ian Hickson committed
44 45 46
  }

  @override
47 48
  double estimateMaxScrollOffset(
    SliverConstraints constraints, {
Ian Hickson's avatar
Ian Hickson committed
49 50 51 52 53 54 55 56
    int firstIndex,
    int lastIndex,
    double leadingScrollOffset,
    double trailingScrollOffset,
  }) {
    assert(lastIndex >= firstIndex);
    return children.length * (trailingScrollOffset - leadingScrollOffset) / (lastIndex - firstIndex + 1);
  }
Adam Barth's avatar
Adam Barth committed
57

58 59 60
  @override
  int get childCount => children.length;

Adam Barth's avatar
Adam Barth committed
61 62 63
  @override
  void didAdoptChild(RenderBox child) {
    assert(_currentlyUpdatingChildIndex != null);
64
    final SliverMultiBoxAdaptorParentData childParentData = child.parentData as SliverMultiBoxAdaptorParentData;
Adam Barth's avatar
Adam Barth committed
65 66
    childParentData.index = _currentlyUpdatingChildIndex;
  }
67 68 69

  @override
  void setDidUnderflow(bool value) { }
Ian Hickson's avatar
Ian Hickson committed
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
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(
101 102 103 104
    double to, {
    @required Duration duration,
    @required Curve curve,
  }) async {
105 106 107 108 109 110 111 112 113 114
    // Do nothing, not required in test.
  }

  @override
  ScrollDirection get userScrollDirection => ScrollDirection.idle;

  @override
  bool get allowImplicitScrolling => false;
}

Ian Hickson's avatar
Ian Hickson committed
115
void main() {
116
  test('RenderSliverList basic test - down', () {
Ian Hickson's avatar
Ian Hickson committed
117 118
    RenderObject inner;
    RenderBox a, b, c, d, e;
119
    final TestRenderSliverBoxChildManager childManager = TestRenderSliverBoxChildManager(
Adam Barth's avatar
Adam Barth committed
120
      children: <RenderBox>[
121 122 123 124 125
        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)),
Adam Barth's avatar
Adam Barth committed
126 127
      ],
    );
128
    final RenderViewport root = RenderViewport(
Ian Hickson's avatar
Ian Hickson committed
129
      axisDirection: AxisDirection.down,
130
      crossAxisDirection: AxisDirection.right,
131
      offset: ViewportOffset.zero(),
132
      cacheExtent: 0.0,
Ian Hickson's avatar
Ian Hickson committed
133
      children: <RenderSliver>[
Adam Barth's avatar
Adam Barth committed
134
        inner = childManager.createRenderObject(),
Ian Hickson's avatar
Ian Hickson committed
135 136 137 138 139 140 141
      ],
    );
    layout(root);

    expect(root.size.width, equals(800.0));
    expect(root.size.height, equals(600.0));

142 143
    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));
Ian Hickson's avatar
Ian Hickson committed
144 145 146 147 148 149 150
    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();
151 152
    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));
Ian Hickson's avatar
Ian Hickson committed
153 154 155 156 157
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

    // now try various scroll offsets
158
    root.offset = ViewportOffset.fixed(200.0);
Ian Hickson's avatar
Ian Hickson committed
159
    pumpFrame();
160 161
    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));
Ian Hickson's avatar
Ian Hickson committed
162 163 164 165
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

166
    root.offset = ViewportOffset.fixed(600.0);
Ian Hickson's avatar
Ian Hickson committed
167 168
    pumpFrame();
    expect(a.attached, false);
169 170
    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));
Ian Hickson's avatar
Ian Hickson committed
171 172 173
    expect(d.attached, false);
    expect(e.attached, false);

174
    root.offset = ViewportOffset.fixed(900.0);
Ian Hickson's avatar
Ian Hickson committed
175 176 177
    pumpFrame();
    expect(a.attached, false);
    expect(b.attached, false);
178 179
    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));
Ian Hickson's avatar
Ian Hickson committed
180 181 182
    expect(e.attached, false);

    // try going back up
183
    root.offset = ViewportOffset.fixed(200.0);
Ian Hickson's avatar
Ian Hickson committed
184
    pumpFrame();
185 186
    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));
Ian Hickson's avatar
Ian Hickson committed
187 188 189 190 191
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);
  });

192
  test('RenderSliverList basic test - up', () {
Ian Hickson's avatar
Ian Hickson committed
193 194
    RenderObject inner;
    RenderBox a, b, c, d, e;
195
    final TestRenderSliverBoxChildManager childManager = TestRenderSliverBoxChildManager(
Adam Barth's avatar
Adam Barth committed
196
      children: <RenderBox>[
197 198 199 200 201
        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)),
Adam Barth's avatar
Adam Barth committed
202 203
      ],
    );
204
    final RenderViewport root = RenderViewport(
Ian Hickson's avatar
Ian Hickson committed
205
      axisDirection: AxisDirection.up,
206
      crossAxisDirection: AxisDirection.right,
207
      offset: ViewportOffset.zero(),
Ian Hickson's avatar
Ian Hickson committed
208
      children: <RenderSliver>[
Adam Barth's avatar
Adam Barth committed
209
        inner = childManager.createRenderObject(),
Ian Hickson's avatar
Ian Hickson committed
210
      ],
211
      cacheExtent: 0.0,
Ian Hickson's avatar
Ian Hickson committed
212 213 214 215 216 217
    );
    layout(root);

    expect(root.size.width, equals(800.0));
    expect(root.size.height, equals(600.0));

218 219
    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));
Ian Hickson's avatar
Ian Hickson committed
220 221 222 223 224 225 226
    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();
227 228
    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));
Ian Hickson's avatar
Ian Hickson committed
229 230 231 232 233
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

    // now try various scroll offsets
234
    root.offset = ViewportOffset.fixed(200.0);
Ian Hickson's avatar
Ian Hickson committed
235
    pumpFrame();
236 237
    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));
Ian Hickson's avatar
Ian Hickson committed
238 239 240 241
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

242
    root.offset = ViewportOffset.fixed(600.0);
Ian Hickson's avatar
Ian Hickson committed
243 244
    pumpFrame();
    expect(a.attached, false);
245 246
    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));
Ian Hickson's avatar
Ian Hickson committed
247 248 249
    expect(d.attached, false);
    expect(e.attached, false);

250
    root.offset = ViewportOffset.fixed(900.0);
Ian Hickson's avatar
Ian Hickson committed
251 252 253
    pumpFrame();
    expect(a.attached, false);
    expect(b.attached, false);
254 255
    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));
Ian Hickson's avatar
Ian Hickson committed
256 257 258
    expect(e.attached, false);

    // try going back up
259
    root.offset = ViewportOffset.fixed(200.0);
Ian Hickson's avatar
Ian Hickson committed
260
    pumpFrame();
261 262
    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));
Ian Hickson's avatar
Ian Hickson committed
263 264 265 266 267
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);
  });

268 269 270
  test('SliverList - no zero scroll offset correction', () {
    RenderSliverList inner;
    RenderBox a;
271
    final TestRenderSliverBoxChildManager childManager = TestRenderSliverBoxChildManager(
272
      children: <RenderBox>[
273 274 275 276 277
        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)),
278 279
      ],
    );
280
    final RenderViewport root = RenderViewport(
281
      axisDirection: AxisDirection.down,
282
      crossAxisDirection: AxisDirection.right,
283
      offset: ViewportOffset.zero(),
284 285 286 287 288 289
      children: <RenderSliver>[
        inner = childManager.createRenderObject(),
      ],
    );
    layout(root);

290
    final SliverMultiBoxAdaptorParentData parentData = a.parentData as SliverMultiBoxAdaptorParentData;
291 292
    parentData.layoutOffset = 0.001;

293
    root.offset = ViewportOffset.fixed(900.0);
294 295
    pumpFrame();

296
    root.offset = ViewportOffset.fixed(0.0);
297 298 299 300
    pumpFrame();

    expect(inner.geometry.scrollOffsetCorrection, isNull);
  });
301

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  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);

325
    final SliverMultiBoxAdaptorParentData parentData = a.parentData as SliverMultiBoxAdaptorParentData;
326 327 328 329 330 331 332 333 334 335 336 337 338
    // 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);
  });

339
  test('SliverMultiBoxAdaptorParentData.toString', () {
340
    final SliverMultiBoxAdaptorParentData candidate = SliverMultiBoxAdaptorParentData();
341 342
    expect(candidate.keepAlive, isFalse);
    expect(candidate.index, isNull);
343
    expect(candidate.toString(), 'index=null; layoutOffset=None');
344
    candidate.keepAlive = null;
345
    expect(candidate.toString(), 'index=null; layoutOffset=None');
346
    candidate.keepAlive = true;
347
    expect(candidate.toString(), 'index=null; keepAlive; layoutOffset=None');
348
    candidate.keepAlive = false;
349
    expect(candidate.toString(), 'index=null; layoutOffset=None');
350
    candidate.index = 0;
351
    expect(candidate.toString(), 'index=0; layoutOffset=None');
352
    candidate.index = 1;
353
    expect(candidate.toString(), 'index=1; layoutOffset=None');
354
    candidate.index = -1;
355 356 357
    expect(candidate.toString(), 'index=-1; layoutOffset=None');
    candidate.layoutOffset = 100.0;
    expect(candidate.toString(), 'index=-1; layoutOffset=100.0');
358
  });
Ian Hickson's avatar
Ian Hickson committed
359
}