slivers_block_test.dart 9.3 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 58 59 60

  @override
  void didAdoptChild(RenderBox child) {
    assert(_currentlyUpdatingChildIndex != null);
    final SliverMultiBoxAdaptorParentData childParentData = child.parentData;
    childParentData.index = _currentlyUpdatingChildIndex;
  }
61 62 63

  @override
  void setDidUnderflow(bool value) { }
Ian Hickson's avatar
Ian Hickson committed
64 65 66
}

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

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

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

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

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

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

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

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

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

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

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

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

217 218 219 220 221 222 223 224 225 226 227 228 229 230
  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,
231
      crossAxisDirection: AxisDirection.right,
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
      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);
  });
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

  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
269
}