slivers_block_test.dart 9.41 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6
// 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';
7
import '../flutter_test_alternative.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({
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
      offset: new ViewportOffset.zero(),
86
      cacheExtent: 0.0,
Ian Hickson's avatar
Ian Hickson committed
87
      children: <RenderSliver>[
Adam Barth's avatar
Adam Barth committed
88
        inner = childManager.createRenderObject(),
Ian Hickson's avatar
Ian Hickson committed
89 90 91 92 93 94 95
      ],
    );
    layout(root);

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

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

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

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

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

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

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

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

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

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

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

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

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