slivers_block_test.dart 9.36 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8 9 10
// 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:meta/meta.dart';
import 'package:test/test.dart';

import 'rendering_tester.dart';

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

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

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

  int _currentlyUpdatingChildIndex;

Ian Hickson's avatar
Ian Hickson committed
27 28 29 30
  @override
  void createChild(int index, { @required RenderBox after }) {
    if (index < 0 || index >= children.length)
      return null;
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
  double estimateMaxScrollOffset(SliverConstraints constraints, {
Ian Hickson's avatar
Ian Hickson committed
46 47 48 49 50 51 52 53
    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
54

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

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

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

void main() {
70
  test('RenderSliverList basic test - down', () {
Ian Hickson's avatar
Ian Hickson committed
71 72
    RenderObject inner;
    RenderBox a, b, c, d, e;
73
    final TestRenderSliverBoxChildManager childManager = new TestRenderSliverBoxChildManager(
Adam Barth's avatar
Adam Barth committed
74 75 76 77 78 79 80 81
      children: <RenderBox>[
        a = new RenderSizedBox(const Size(100.0, 400.0)),
        b = new RenderSizedBox(const Size(100.0, 400.0)),
        c = new RenderSizedBox(const Size(100.0, 400.0)),
        d = new RenderSizedBox(const Size(100.0, 400.0)),
        e = new RenderSizedBox(const Size(100.0, 400.0)),
      ],
    );
82
    final RenderViewport root = new RenderViewport(
Ian Hickson's avatar
Ian Hickson committed
83
      axisDirection: AxisDirection.down,
84
      crossAxisDirection: AxisDirection.right,
Ian Hickson's avatar
Ian Hickson committed
85 86
      offset: new ViewportOffset.zero(),
      children: <RenderSliver>[
Adam Barth's avatar
Adam Barth committed
87
        inner = childManager.createRenderObject(),
Ian Hickson's avatar
Ian Hickson committed
88 89 90 91 92 93 94
      ],
    );
    layout(root);

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

95 96
    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
97 98 99 100 101 102 103
    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();
104 105
    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
106 107 108 109 110 111 112
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

    // now try various scroll offsets
    root.offset = new ViewportOffset.fixed(200.0);
    pumpFrame();
113 114
    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
115 116 117 118 119 120 121
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

    root.offset = new ViewportOffset.fixed(600.0);
    pumpFrame();
    expect(a.attached, false);
122 123
    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
124 125 126 127 128 129 130
    expect(d.attached, false);
    expect(e.attached, false);

    root.offset = new ViewportOffset.fixed(900.0);
    pumpFrame();
    expect(a.attached, false);
    expect(b.attached, false);
131 132
    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
133 134 135 136 137
    expect(e.attached, false);

    // try going back up
    root.offset = new ViewportOffset.fixed(200.0);
    pumpFrame();
138 139
    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
140 141 142 143 144
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);
  });

145
  test('RenderSliverList basic test - up', () {
Ian Hickson's avatar
Ian Hickson committed
146 147
    RenderObject inner;
    RenderBox a, b, c, d, e;
148
    final TestRenderSliverBoxChildManager childManager = new TestRenderSliverBoxChildManager(
Adam Barth's avatar
Adam Barth committed
149 150 151 152 153 154 155 156
      children: <RenderBox>[
        a = new RenderSizedBox(const Size(100.0, 400.0)),
        b = new RenderSizedBox(const Size(100.0, 400.0)),
        c = new RenderSizedBox(const Size(100.0, 400.0)),
        d = new RenderSizedBox(const Size(100.0, 400.0)),
        e = new RenderSizedBox(const Size(100.0, 400.0)),
      ],
    );
157
    final RenderViewport root = new RenderViewport(
Ian Hickson's avatar
Ian Hickson committed
158
      axisDirection: AxisDirection.up,
159
      crossAxisDirection: AxisDirection.right,
Ian Hickson's avatar
Ian Hickson committed
160 161
      offset: new ViewportOffset.zero(),
      children: <RenderSliver>[
Adam Barth's avatar
Adam Barth committed
162
        inner = childManager.createRenderObject(),
Ian Hickson's avatar
Ian Hickson committed
163 164 165 166 167 168 169
      ],
    );
    layout(root);

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

170 171
    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
172 173 174 175 176 177 178
    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();
179 180
    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
181 182 183 184 185 186 187
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

    // now try various scroll offsets
    root.offset = new ViewportOffset.fixed(200.0);
    pumpFrame();
188 189
    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
190 191 192 193 194 195 196
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);

    root.offset = new ViewportOffset.fixed(600.0);
    pumpFrame();
    expect(a.attached, false);
197 198
    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
199 200 201 202 203 204 205
    expect(d.attached, false);
    expect(e.attached, false);

    root.offset = new ViewportOffset.fixed(900.0);
    pumpFrame();
    expect(a.attached, false);
    expect(b.attached, false);
206 207
    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
208 209 210 211 212
    expect(e.attached, false);

    // try going back up
    root.offset = new ViewportOffset.fixed(200.0);
    pumpFrame();
213 214
    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
215 216 217 218 219
    expect(c.attached, false);
    expect(d.attached, false);
    expect(e.attached, false);
  });

220 221 222 223 224 225 226 227 228 229 230 231 232 233
  test('SliverList - no zero scroll offset correction', () {
    RenderSliverList inner;
    RenderBox a;
    final TestRenderSliverBoxChildManager childManager = new TestRenderSliverBoxChildManager(
      children: <RenderBox>[
        a = new RenderSizedBox(const Size(100.0, 400.0)),
        new RenderSizedBox(const Size(100.0, 400.0)),
        new RenderSizedBox(const Size(100.0, 400.0)),
        new RenderSizedBox(const Size(100.0, 400.0)),
        new RenderSizedBox(const Size(100.0, 400.0)),
      ],
    );
    final RenderViewport root = new RenderViewport(
      axisDirection: AxisDirection.down,
234
      crossAxisDirection: AxisDirection.right,
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      offset: new ViewportOffset.zero(),
      children: <RenderSliver>[
        inner = childManager.createRenderObject(),
      ],
    );
    layout(root);

    final SliverMultiBoxAdaptorParentData parentData = a.parentData;
    parentData.layoutOffset = 0.001;

    root.offset = new ViewportOffset.fixed(900.0);
    pumpFrame();

    root.offset = new ViewportOffset.fixed(0.0);
    pumpFrame();

    expect(inner.geometry.scrollOffsetCorrection, isNull);
  });
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

  test('SliverMultiBoxAdaptorParentData.toString', () {
    final SliverMultiBoxAdaptorParentData candidate = new 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');
  });
Ian Hickson's avatar
Ian Hickson committed
272
}