slivers_block_test.dart 11 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/animation.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter_test/flutter_test.dart';
Ian Hickson's avatar
Ian Hickson committed
8 9 10

import 'rendering_tester.dart';

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

16
  late RenderSliverList _renderObject;
Ian Hickson's avatar
Ian Hickson committed
17 18
  List<RenderBox> children;

19
  RenderSliverList createRenderObject() {
20
    _renderObject = RenderSliverList(childManager: this);
Adam Barth's avatar
Adam Barth committed
21 22 23
    return _renderObject;
  }

24
  int? _currentlyUpdatingChildIndex;
Adam Barth's avatar
Adam Barth committed
25

Ian Hickson's avatar
Ian Hickson committed
26
  @override
27
  void createChild(int index, { required RenderBox? after }) {
28
    if (index < 0 || index >= children.length) {
29
      return;
30
    }
Adam Barth's avatar
Adam Barth committed
31 32 33 34 35 36 37 38 39 40 41
    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
42 43 44
  }

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

56 57 58
  @override
  int get childCount => children.length;

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

  @override
  void setDidUnderflow(bool value) { }
Ian Hickson's avatar
Ian Hickson committed
68 69
}

70 71 72 73 74 75 76 77
class ViewportOffsetSpy extends ViewportOffset {
  ViewportOffsetSpy(this._pixels);

  double _pixels;

  @override
  double get pixels => _pixels;

78 79 80
  @override
  bool get hasPixels => true;

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  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(
102
    double to, {
103 104
    required Duration duration,
    required Curve curve,
105
  }) async {
106 107 108 109 110 111 112 113 114 115
    // Do nothing, not required in test.
  }

  @override
  ScrollDirection get userScrollDirection => ScrollDirection.idle;

  @override
  bool get allowImplicitScrolling => false;
}

Ian Hickson's avatar
Ian Hickson committed
116
void main() {
117 118
  TestRenderingFlutterBinding.ensureInitialized();

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

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

144 145
    expect(a.localToGlobal(Offset.zero), Offset.zero);
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
Ian Hickson's avatar
Ian Hickson committed
146 147 148 149 150 151 152
    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();
153 154
    expect(a.localToGlobal(Offset.zero), Offset.zero);
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
Ian Hickson's avatar
Ian Hickson committed
155 156 157 158 159
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

    // now try various scroll offsets
160
    root.offset = ViewportOffset.fixed(200.0);
Ian Hickson's avatar
Ian Hickson committed
161
    pumpFrame();
162 163
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
Ian Hickson's avatar
Ian Hickson committed
164 165 166 167
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

168
    root.offset = ViewportOffset.fixed(600.0);
Ian Hickson's avatar
Ian Hickson committed
169 170
    pumpFrame();
    expect(a.attached, false);
171 172
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
Ian Hickson's avatar
Ian Hickson committed
173 174 175
    expect(d.attached, false);
    expect(e.attached, false);

176
    root.offset = ViewportOffset.fixed(900.0);
Ian Hickson's avatar
Ian Hickson committed
177 178 179
    pumpFrame();
    expect(a.attached, false);
    expect(b.attached, false);
180 181
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, -100.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, 300.0));
Ian Hickson's avatar
Ian Hickson committed
182 183 184
    expect(e.attached, false);

    // try going back up
185
    root.offset = ViewportOffset.fixed(200.0);
Ian Hickson's avatar
Ian Hickson committed
186
    pumpFrame();
187 188
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
Ian Hickson's avatar
Ian Hickson committed
189 190 191 192 193
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);
  });

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

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

220 221
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
Ian Hickson's avatar
Ian Hickson committed
222 223 224 225 226 227 228
    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();
229 230
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
Ian Hickson's avatar
Ian Hickson committed
231 232 233 234 235
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

    // now try various scroll offsets
236
    root.offset = ViewportOffset.fixed(200.0);
Ian Hickson's avatar
Ian Hickson committed
237
    pumpFrame();
238 239
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
    expect(b.localToGlobal(Offset.zero), Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
240 241 242 243
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

244
    root.offset = ViewportOffset.fixed(600.0);
Ian Hickson's avatar
Ian Hickson committed
245 246
    pumpFrame();
    expect(a.attached, false);
247 248
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
    expect(c.localToGlobal(Offset.zero), Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
249 250 251
    expect(d.attached, false);
    expect(e.attached, false);

252
    root.offset = ViewportOffset.fixed(900.0);
Ian Hickson's avatar
Ian Hickson committed
253 254 255
    pumpFrame();
    expect(a.attached, false);
    expect(b.attached, false);
256 257
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 300.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -100.0));
Ian Hickson's avatar
Ian Hickson committed
258 259 260
    expect(e.attached, false);

    // try going back up
261
    root.offset = ViewportOffset.fixed(200.0);
Ian Hickson's avatar
Ian Hickson committed
262
    pumpFrame();
263 264
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
    expect(b.localToGlobal(Offset.zero), Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
265 266 267 268 269
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);
  });

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

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

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

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

300
    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(
      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 = true;
345
    expect(candidate.toString(), 'index=null; keepAlive; layoutOffset=None');
346
    candidate.keepAlive = false;
347
    expect(candidate.toString(), 'index=null; layoutOffset=None');
348
    candidate.index = 0;
349
    expect(candidate.toString(), 'index=0; layoutOffset=None');
350
    candidate.index = 1;
351
    expect(candidate.toString(), 'index=1; layoutOffset=None');
352
    candidate.index = -1;
353 354 355
    expect(candidate.toString(), 'index=-1; layoutOffset=None');
    candidate.layoutOffset = 100.0;
    expect(candidate.toString(), 'index=-1; layoutOffset=100.0');
356
  });
Ian Hickson's avatar
Ian Hickson committed
357
}