slivers_test.dart 46.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/gestures.dart';
9
import 'package:flutter/rendering.dart';
10
import 'package:flutter/widgets.dart';
11
import 'package:flutter_test/flutter_test.dart';
12
import 'package:vector_math/vector_math_64.dart';
13 14 15 16

import 'rendering_tester.dart';

void main() {
Adam Barth's avatar
Adam Barth committed
17
  test('RenderViewport basic test - no children', () {
18
    final RenderViewport root = RenderViewport(
19
      crossAxisDirection: AxisDirection.right,
20
      offset: ViewportOffset.zero(),
Ian Hickson's avatar
Ian Hickson committed
21
    );
22
    expect(root, hasAGoodToStringDeep);
23
    expect(
24
      root.toStringDeep(minLevel: DiagnosticLevel.info),
25 26
      equalsIgnoringHashCodes(
        'RenderViewport#00000 NEEDS-LAYOUT NEEDS-PAINT DETACHED\n'
27
        '   needs compositing\n'
Ian Hickson's avatar
Ian Hickson committed
28 29
        '   parentData: MISSING\n'
        '   constraints: MISSING\n'
30 31
        '   size: MISSING\n'
        '   axisDirection: down\n'
32
        '   crossAxisDirection: right\n'
33 34
        '   offset: _FixedViewportOffset#00000(offset: 0.0)\n'
        '   anchor: 0.0\n'
35 36
      ),
    );
37
    layout(root);
38
    root.offset = ViewportOffset.fixed(900.0);
39
    expect(root, hasAGoodToStringDeep);
40
    expect(
41
      root.toStringDeep(minLevel: DiagnosticLevel.info),
42 43
      equalsIgnoringHashCodes(
        'RenderViewport#00000 NEEDS-LAYOUT NEEDS-PAINT\n'
44
        '   needs compositing\n'
45 46 47 48
        '   parentData: <none>\n'
        '   constraints: BoxConstraints(w=800.0, h=600.0)\n'
        '   size: Size(800.0, 600.0)\n'
        '   axisDirection: down\n'
49
        '   crossAxisDirection: right\n'
50 51
        '   offset: _FixedViewportOffset#00000(offset: 900.0)\n'
        '   anchor: 0.0\n',
52 53 54
      ),
    );

55 56 57
    pumpFrame();
  });

Adam Barth's avatar
Adam Barth committed
58
  test('RenderViewport basic test - down', () {
59
    RenderBox a, b, c, d, e;
60
    final RenderViewport root = RenderViewport(
61
      crossAxisDirection: AxisDirection.right,
62
      offset: ViewportOffset.zero(),
63
      children: <RenderSliver>[
64 65 66 67 68
        RenderSliverToBoxAdapter(child: a = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: b = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: c = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: d = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: e = RenderSizedBox(const Size(100.0, 400.0))),
69 70
      ],
    );
71
    expect(root, hasAGoodToStringDeep);
72 73 74 75 76
    layout(root);

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

77 78
    expect(root, hasAGoodToStringDeep);
    expect(
79
      root.toStringDeep(minLevel: DiagnosticLevel.info),
80
      equalsIgnoringHashCodes(
81 82
        'RenderViewport#00000 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'
        ' │ needs compositing\n'
83 84 85
        ' │ parentData: <none>\n'
        ' │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        ' │ size: Size(800.0, 600.0)\n'
86
        ' │ axisDirection: down\n'
87
        ' │ crossAxisDirection: right\n'
88 89 90
        ' │ offset: _FixedViewportOffset#00000(offset: 0.0)\n'
        ' │ anchor: 0.0\n'
        ' │\n'
91
        ' ├─center child: RenderSliverToBoxAdapter#00000 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'
92 93 94 95
        ' │ │ parentData: paintOffset=Offset(0.0, 0.0) (can use size)\n'
        ' │ │ constraints: SliverConstraints(AxisDirection.down,\n'
        ' │ │   GrowthDirection.forward, ScrollDirection.idle, scrollOffset:\n'
        ' │ │   0.0, remainingPaintExtent: 600.0, crossAxisExtent: 800.0,\n'
96
        ' │ │   crossAxisDirection: AxisDirection.right,\n'
97 98
        ' │ │   viewportMainAxisExtent: 600.0, remainingCacheExtent: 850.0,\n'
        ' │ │   cacheOrigin: 0.0)\n'
99
        ' │ │ geometry: SliverGeometry(scrollExtent: 400.0, paintExtent: 400.0,\n'
100
        ' │ │   maxPaintExtent: 400.0, cacheExtent: 400.0)\n'
101 102 103 104 105 106
        ' │ │\n'
        ' │ └─child: RenderSizedBox#00000 NEEDS-PAINT\n'
        ' │     parentData: paintOffset=Offset(0.0, -0.0) (can use size)\n'
        ' │     constraints: BoxConstraints(w=800.0, 0.0<=h<=Infinity)\n'
        ' │     size: Size(800.0, 400.0)\n'
        ' │\n'
107
        ' ├─child 1: RenderSliverToBoxAdapter#00000 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'
108 109 110 111
        ' │ │ parentData: paintOffset=Offset(0.0, 400.0) (can use size)\n'
        ' │ │ constraints: SliverConstraints(AxisDirection.down,\n'
        ' │ │   GrowthDirection.forward, ScrollDirection.idle, scrollOffset:\n'
        ' │ │   0.0, remainingPaintExtent: 200.0, crossAxisExtent: 800.0,\n'
112
        ' │ │   crossAxisDirection: AxisDirection.right,\n'
113 114
        ' │ │   viewportMainAxisExtent: 600.0, remainingCacheExtent: 450.0,\n'
        ' │ │   cacheOrigin: 0.0)\n'
115
        ' │ │ geometry: SliverGeometry(scrollExtent: 400.0, paintExtent: 200.0,\n'
116 117
        ' │ │   maxPaintExtent: 400.0, hasVisualOverflow: true, cacheExtent:\n'
        ' │ │   400.0)\n'
118 119 120 121 122 123
        ' │ │\n'
        ' │ └─child: RenderSizedBox#00000 NEEDS-PAINT\n'
        ' │     parentData: paintOffset=Offset(0.0, -0.0) (can use size)\n'
        ' │     constraints: BoxConstraints(w=800.0, 0.0<=h<=Infinity)\n'
        ' │     size: Size(800.0, 400.0)\n'
        ' │\n'
124
        ' ├─child 2: RenderSliverToBoxAdapter#00000 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'
125
        ' │ │ parentData: paintOffset=Offset(0.0, 800.0) (can use size)\n'
126 127 128
        ' │ │ constraints: SliverConstraints(AxisDirection.down,\n'
        ' │ │   GrowthDirection.forward, ScrollDirection.idle, scrollOffset:\n'
        ' │ │   0.0, remainingPaintExtent: 0.0, crossAxisExtent: 800.0,\n'
129
        ' │ │   crossAxisDirection: AxisDirection.right,\n'
130 131
        ' │ │   viewportMainAxisExtent: 600.0, remainingCacheExtent: 50.0,\n'
        ' │ │   cacheOrigin: 0.0)\n'
132
        ' │ │ geometry: SliverGeometry(scrollExtent: 400.0, hidden,\n'
133 134
        ' │ │   maxPaintExtent: 400.0, hasVisualOverflow: true, cacheExtent:\n'
        ' │ │   50.0)\n'
135 136 137 138 139 140
        ' │ │\n'
        ' │ └─child: RenderSizedBox#00000 NEEDS-PAINT\n'
        ' │     parentData: paintOffset=Offset(0.0, -0.0) (can use size)\n'
        ' │     constraints: BoxConstraints(w=800.0, 0.0<=h<=Infinity)\n'
        ' │     size: Size(800.0, 400.0)\n'
        ' │\n'
141
        ' ├─child 3: RenderSliverToBoxAdapter#00000 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'
142
        ' │ │ parentData: paintOffset=Offset(0.0, 1200.0) (can use size)\n'
143 144 145
        ' │ │ constraints: SliverConstraints(AxisDirection.down,\n'
        ' │ │   GrowthDirection.forward, ScrollDirection.idle, scrollOffset:\n'
        ' │ │   0.0, remainingPaintExtent: 0.0, crossAxisExtent: 800.0,\n'
146
        ' │ │   crossAxisDirection: AxisDirection.right,\n'
147 148
        ' │ │   viewportMainAxisExtent: 600.0, remainingCacheExtent: 0.0,\n'
        ' │ │   cacheOrigin: 0.0)\n'
149
        ' │ │ geometry: SliverGeometry(scrollExtent: 400.0, hidden,\n'
150
        ' │ │   maxPaintExtent: 400.0, hasVisualOverflow: true)\n'
151 152 153 154 155 156
        ' │ │\n'
        ' │ └─child: RenderSizedBox#00000 NEEDS-PAINT\n'
        ' │     parentData: paintOffset=Offset(0.0, -0.0) (can use size)\n'
        ' │     constraints: BoxConstraints(w=800.0, 0.0<=h<=Infinity)\n'
        ' │     size: Size(800.0, 400.0)\n'
        ' │\n'
157
        ' └─child 4: RenderSliverToBoxAdapter#00000 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'
158
        '   │ parentData: paintOffset=Offset(0.0, 1600.0) (can use size)\n'
159 160 161
        '   │ constraints: SliverConstraints(AxisDirection.down,\n'
        '   │   GrowthDirection.forward, ScrollDirection.idle, scrollOffset:\n'
        '   │   0.0, remainingPaintExtent: 0.0, crossAxisExtent: 800.0,\n'
162
        '   │   crossAxisDirection: AxisDirection.right,\n'
163 164
        '   │   viewportMainAxisExtent: 600.0, remainingCacheExtent: 0.0,\n'
        '   │   cacheOrigin: 0.0)\n'
165
        '   │ geometry: SliverGeometry(scrollExtent: 400.0, hidden,\n'
166
        '   │   maxPaintExtent: 400.0, hasVisualOverflow: true)\n'
167 168 169 170
        '   │\n'
        '   └─child: RenderSizedBox#00000 NEEDS-PAINT\n'
        '       parentData: paintOffset=Offset(0.0, -0.0) (can use size)\n'
        '       constraints: BoxConstraints(w=800.0, 0.0<=h<=Infinity)\n'
171
        '       size: Size(800.0, 400.0)\n'
172 173
      ),
    );
174 175
    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));
176 177 178
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 800.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1200.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1600.0));
179 180 181

    expect(a.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 400.0));
    expect(b.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 800.0));
182 183 184
    expect(c.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 1200.0));
    expect(d.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 1600.0));
    expect(e.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 2000.0));
185

186
    root.offset = ViewportOffset.fixed(200.0);
187
    pumpFrame();
188 189 190
    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));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 600.0));
191 192
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1000.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1400.0));
193

194
    root.offset = ViewportOffset.fixed(600.0);
195
    pumpFrame();
196 197 198 199
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -600.0));
    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));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 600.0));
200
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1000.0));
201

202
    root.offset = ViewportOffset.fixed(900.0);
203
    pumpFrame();
204 205 206 207
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -900.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -500.0));
    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));
208
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 700.0));
209

210
    final BoxHitTestResult result = BoxHitTestResult();
211
    root.hitTest(result, position: const Offset(130.0, 150.0));
212
    expect(result.path.first.target, equals(c));
213 214
  });

Adam Barth's avatar
Adam Barth committed
215
  test('RenderViewport basic test - up', () {
216
    RenderBox a, b, c, d, e;
217
    final RenderViewport root = RenderViewport(
218
      axisDirection: AxisDirection.up,
219
      crossAxisDirection: AxisDirection.right,
220
      offset: ViewportOffset.zero(),
221
      children: <RenderSliver>[
222 223 224 225 226
        RenderSliverToBoxAdapter(child: a = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: b = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: c = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: d = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: e = RenderSizedBox(const Size(100.0, 400.0))),
227 228 229 230 231 232 233
      ],
    );
    layout(root);

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

234 235
    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));
236 237 238
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -600.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -1000.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -1400.0));
239

240
    root.offset = ViewportOffset.fixed(200.0);
241
    pumpFrame();
242 243 244
    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));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -400.0));
245 246
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -800.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -1200.0));
247

248
    root.offset = ViewportOffset.fixed(600.0);
249
    pumpFrame();
250 251 252 253
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 800.0));
    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));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -400.0));
254
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -800.0));
255

256
    root.offset = ViewportOffset.fixed(900.0);
257
    pumpFrame();
258 259 260 261
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1100.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 700.0));
    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));
262
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -500.0));
263

264
    final BoxHitTestResult result = BoxHitTestResult();
265
    root.hitTest(result, position: const Offset(150.0, 350.0));
266
    expect(result.path.first.target, equals(c));
267 268
  });

269
  Offset _getPaintOrigin(RenderObject render) {
270 271
    final Vector3 transformed3 = render.getTransformTo(null).perspectiveTransform(Vector3(0.0, 0.0, 0.0));
    return Offset(transformed3.x, transformed3.y);
272 273
  }

Adam Barth's avatar
Adam Barth committed
274
  test('RenderViewport basic test - right', () {
275
    RenderBox a, b, c, d, e;
276
    final RenderViewport root = RenderViewport(
277
      axisDirection: AxisDirection.right,
278
      crossAxisDirection: AxisDirection.down,
279
      offset: ViewportOffset.zero(),
280
      children: <RenderSliver>[
281 282 283 284 285
        RenderSliverToBoxAdapter(child: a = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: b = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: c = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: d = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: e = RenderSizedBox(const Size(400.0, 100.0))),
286 287 288 289 290 291 292
      ],
    );
    layout(root);

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

293 294 295 296 297
    final RenderSliver sliverA = a.parent as RenderSliver;
    final RenderSliver sliverB = b.parent as RenderSliver;
    final RenderSliver sliverC = c.parent as RenderSliver;
    final RenderSliver sliverD = d.parent as RenderSliver;
    final RenderSliver sliverE = e.parent as RenderSliver;
298

299 300 301
    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(400.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(800.0, 0.0));
302 303
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(1200.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(1600.0, 0.0));
304

305 306 307
    expect(_getPaintOrigin(sliverA), const Offset(0.0, 0.0));
    expect(_getPaintOrigin(sliverB), const Offset(400.0, 0.0));
    expect(_getPaintOrigin(sliverC), const Offset(800.0, 0.0));
308 309
    expect(_getPaintOrigin(sliverD), const Offset(1200.0, 0.0));
    expect(_getPaintOrigin(sliverE), const Offset(1600.0, 0.0));
310

311
    root.offset = ViewportOffset.fixed(200.0);
312
    pumpFrame();
313 314 315
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(-200.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(200.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(600.0, 0.0));
316 317
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(1000.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(1400.0, 0.0));
318

319 320 321
    expect(_getPaintOrigin(sliverA), const Offset(000.0, 0.0));
    expect(_getPaintOrigin(sliverB), const Offset(200.0, 0.0));
    expect(_getPaintOrigin(sliverC), const Offset(600.0, 0.0));
322 323
    expect(_getPaintOrigin(sliverD), const Offset(1000.0, 0.0));
    expect(_getPaintOrigin(sliverE), const Offset(1400.0, 0.0));
324

325
    root.offset = ViewportOffset.fixed(600.0);
326
    pumpFrame();
327 328 329 330
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(-600.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(-200.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(200.0, 0.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(600.0, 0.0));
331
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(1000.0, 0.0));
332

333 334 335 336
    expect(_getPaintOrigin(sliverA), const Offset(000.0, 0.0));
    expect(_getPaintOrigin(sliverB), const Offset(000.0, 0.0));
    expect(_getPaintOrigin(sliverC), const Offset(200.0, 0.0));
    expect(_getPaintOrigin(sliverD), const Offset(600.0, 0.0));
337
    expect(_getPaintOrigin(sliverE), const Offset(1000.0, 0.0));
338

339
    root.offset = ViewportOffset.fixed(900.0);
340
    pumpFrame();
341 342 343 344 345
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(-900.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(-500.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(-100.0, 0.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(300.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(700.0, 0.0));
346

347 348 349 350 351 352
    expect(_getPaintOrigin(sliverA), const Offset(000.0, 0.0));
    expect(_getPaintOrigin(sliverB), const Offset(000.0, 0.0));
    expect(_getPaintOrigin(sliverC), const Offset(000.0, 0.0));
    expect(_getPaintOrigin(sliverD), const Offset(300.0, 0.0));
    expect(_getPaintOrigin(sliverE), const Offset(700.0, 0.0));

353
    final BoxHitTestResult result = BoxHitTestResult();
354
    root.hitTest(result, position: const Offset(150.0, 450.0));
355
    expect(result.path.first.target, equals(c));
356 357
  });

Adam Barth's avatar
Adam Barth committed
358
  test('RenderViewport basic test - left', () {
359
    RenderBox a, b, c, d, e;
360
    final RenderViewport root = RenderViewport(
361
      axisDirection: AxisDirection.left,
362
      crossAxisDirection: AxisDirection.down,
363
      offset: ViewportOffset.zero(),
364
      children: <RenderSliver>[
365 366 367 368 369
        RenderSliverToBoxAdapter(child: a = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: b = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: c = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: d = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: e = RenderSizedBox(const Size(400.0, 100.0))),
370 371 372 373 374 375 376
      ],
    );
    layout(root);

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

377 378 379
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(400.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(-400.0, 0.0));
380 381
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(-800.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(-1200.0, 0.0));
382

383
    root.offset = ViewportOffset.fixed(200.0);
384
    pumpFrame();
385 386 387
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(600.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(200.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(-200.0, 0.0));
388 389
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(-600.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(-1000.0, 0.0));
390

391
    root.offset = ViewportOffset.fixed(600.0);
392
    pumpFrame();
393 394 395 396
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(1000.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(600.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(200.0, 0.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(-200.0, 0.0));
397
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(-600.0, 0.0));
398

399
    root.offset = ViewportOffset.fixed(900.0);
400
    pumpFrame();
401 402 403 404 405
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(1300.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(900.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(500.0, 0.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(100.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(-300.0, 0.0));
406

407
    final BoxHitTestResult result = BoxHitTestResult();
408
    root.hitTest(result, position: const Offset(550.0, 150.0));
409
    expect(result.path.first.target, equals(c));
410 411 412 413 414 415
  });

  // TODO(ianh): test anchor
  // TODO(ianh): test center
  // TODO(ianh): test semantics

416
  test('RenderShrinkWrappingViewport basic test - no children', () {
417
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
418
      crossAxisDirection: AxisDirection.right,
419
      offset: ViewportOffset.zero(),
420
    );
421
    expect(root, hasAGoodToStringDeep);
422
    layout(root);
423
    root.offset = ViewportOffset.fixed(900.0);
424 425 426 427 428
    pumpFrame();
  });

  test('RenderShrinkWrappingViewport basic test - down', () {
    RenderBox a, b, c, d, e;
429
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
430
      crossAxisDirection: AxisDirection.right,
431
      offset: ViewportOffset.zero(),
432
      children: <RenderSliver>[
433 434 435 436 437
        RenderSliverToBoxAdapter(child: a = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: b = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: c = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: d = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: e = RenderSizedBox(const Size(100.0, 400.0))),
438 439 440 441 442 443 444
      ],
    );
    layout(root);

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

445 446
    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));
447 448 449
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 800.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1200.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1600.0));
450

451 452
    expect(a.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 400.0));
    expect(b.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 800.0));
453 454 455
    expect(c.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 1200.0));
    expect(d.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 1600.0));
    expect(e.localToGlobal(const Offset(800.0, 400.0)), const Offset(800.0, 2000.0));
456

457
    root.offset = ViewportOffset.fixed(200.0);
458
    pumpFrame();
459 460 461
    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));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 600.0));
462 463
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1000.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1400.0));
464

465
    root.offset = ViewportOffset.fixed(600.0);
466
    pumpFrame();
467 468 469 470
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -600.0));
    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));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 600.0));
471
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1000.0));
472

473
    root.offset = ViewportOffset.fixed(900.0);
474
    pumpFrame();
475 476 477 478
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -900.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -500.0));
    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));
479
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 700.0));
480

481
    final BoxHitTestResult result = BoxHitTestResult();
482
    root.hitTest(result, position: const Offset(130.0, 150.0));
483 484 485 486 487
    expect(result.path.first.target, equals(c));
  });

  test('RenderShrinkWrappingViewport basic test - up', () {
    RenderBox a, b, c, d, e;
488
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
489
      axisDirection: AxisDirection.up,
490
      crossAxisDirection: AxisDirection.right,
491
      offset: ViewportOffset.zero(),
492
      children: <RenderSliver>[
493 494 495 496 497
        RenderSliverToBoxAdapter(child: a = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: b = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: c = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: d = RenderSizedBox(const Size(100.0, 400.0))),
        RenderSliverToBoxAdapter(child: e = RenderSizedBox(const Size(100.0, 400.0))),
498 499 500 501 502 503 504
      ],
    );
    layout(root);

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

505 506
    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));
507 508 509
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -600.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -1000.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -1400.0));
510

511
    root.offset = ViewportOffset.fixed(200.0);
512
    pumpFrame();
513 514 515
    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));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -400.0));
516 517
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -800.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -1200.0));
518

519
    root.offset = ViewportOffset.fixed(600.0);
520
    pumpFrame();
521 522 523 524
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 800.0));
    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));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -400.0));
525
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -800.0));
526

527
    root.offset = ViewportOffset.fixed(900.0);
528
    pumpFrame();
529 530 531 532
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 1100.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 700.0));
    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));
533
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, -500.0));
534

535
    final BoxHitTestResult result = BoxHitTestResult();
536
    root.hitTest(result, position: const Offset(150.0, 350.0));
537 538 539 540 541
    expect(result.path.first.target, equals(c));
  });

  test('RenderShrinkWrappingViewport basic test - right', () {
    RenderBox a, b, c, d, e;
542
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
543
      axisDirection: AxisDirection.right,
544
      crossAxisDirection: AxisDirection.down,
545
      offset: ViewportOffset.zero(),
546
      children: <RenderSliver>[
547 548 549 550 551
        RenderSliverToBoxAdapter(child: a = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: b = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: c = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: d = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: e = RenderSizedBox(const Size(400.0, 100.0))),
552 553 554 555 556 557 558
      ],
    );
    layout(root);

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

559 560 561
    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(400.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(800.0, 0.0));
562 563
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(1200.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(1600.0, 0.0));
564

565
    root.offset = ViewportOffset.fixed(200.0);
566
    pumpFrame();
567 568 569
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(-200.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(200.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(600.0, 0.0));
570 571
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(1000.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(1400.0, 0.0));
572

573
    root.offset = ViewportOffset.fixed(600.0);
574
    pumpFrame();
575 576 577 578
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(-600.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(-200.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(200.0, 0.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(600.0, 0.0));
579
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(1000.0, 0.0));
580

581
    root.offset = ViewportOffset.fixed(900.0);
582
    pumpFrame();
583 584 585 586 587
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(-900.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(-500.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(-100.0, 0.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(300.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(700.0, 0.0));
588

589
    final BoxHitTestResult result = BoxHitTestResult();
590
    root.hitTest(result, position: const Offset(150.0, 450.0));
591 592 593 594 595
    expect(result.path.first.target, equals(c));
  });

  test('RenderShrinkWrappingViewport basic test - left', () {
    RenderBox a, b, c, d, e;
596
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
597
      axisDirection: AxisDirection.left,
598
      crossAxisDirection: AxisDirection.down,
599
      offset: ViewportOffset.zero(),
600
      children: <RenderSliver>[
601 602 603 604 605
        RenderSliverToBoxAdapter(child: a = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: b = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: c = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: d = RenderSizedBox(const Size(400.0, 100.0))),
        RenderSliverToBoxAdapter(child: e = RenderSizedBox(const Size(400.0, 100.0))),
606 607 608 609 610 611 612
      ],
    );
    layout(root);

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

613 614 615
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(400.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(0.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(-400.0, 0.0));
616 617
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(-800.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(-1200.0, 0.0));
618

619
    root.offset = ViewportOffset.fixed(200.0);
620
    pumpFrame();
621 622 623
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(600.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(200.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(-200.0, 0.0));
624 625
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(-600.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(-1000.0, 0.0));
626

627
    root.offset = ViewportOffset.fixed(600.0);
628
    pumpFrame();
629 630 631 632
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(1000.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(600.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(200.0, 0.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(-200.0, 0.0));
633
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(-600.0, 0.0));
634

635
    root.offset = ViewportOffset.fixed(900.0);
636
    pumpFrame();
637 638 639 640 641
    expect(a.localToGlobal(const Offset(0.0, 0.0)), const Offset(1300.0, 0.0));
    expect(b.localToGlobal(const Offset(0.0, 0.0)), const Offset(900.0, 0.0));
    expect(c.localToGlobal(const Offset(0.0, 0.0)), const Offset(500.0, 0.0));
    expect(d.localToGlobal(const Offset(0.0, 0.0)), const Offset(100.0, 0.0));
    expect(e.localToGlobal(const Offset(0.0, 0.0)), const Offset(-300.0, 0.0));
642

643
    final BoxHitTestResult result = BoxHitTestResult();
644
    root.hitTest(result, position: const Offset(550.0, 150.0));
645 646 647 648 649
    expect(result.path.first.target, equals(c));
  });

  test('RenderShrinkWrappingViewport shrinkwrap test - 1 child', () {
    RenderBox child;
650 651
    final RenderBox root = RenderPositionedBox(
      child: child = RenderShrinkWrappingViewport(
652
        axisDirection: AxisDirection.left,
653
        crossAxisDirection: AxisDirection.down,
654
        offset: ViewportOffset.fixed(200.0),
655
        children: <RenderSliver>[
656
          RenderSliverToBoxAdapter(child: RenderSizedBox(const Size(400.0, 100.0))),
657 658 659 660 661 662 663 664 665 666 667 668 669
        ],
      ),
    );
    layout(root);

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

  test('RenderShrinkWrappingViewport shrinkwrap test - 2 children', () {
    RenderBox child;
670 671
    final RenderBox root = RenderPositionedBox(
      child: child = RenderShrinkWrappingViewport(
672
        axisDirection: AxisDirection.right,
673
        crossAxisDirection: AxisDirection.down,
674
        offset: ViewportOffset.fixed(200.0),
675
        children: <RenderSliver>[
676 677
          RenderSliverToBoxAdapter(child: RenderSizedBox(const Size(300.0, 100.0))),
          RenderSliverToBoxAdapter(child: RenderSizedBox(const Size(150.0, 100.0))),
678 679 680 681 682 683 684 685 686 687
        ],
      ),
    );
    layout(root);

    expect(root.size.width, equals(800.0));
    expect(root.size.height, equals(600.0));
    expect(child.size.width, equals(450.0));
    expect(child.size.height, equals(600.0));
  });
688 689 690

  test('SliverGeometry toString', () {
    expect(
691 692 693
      const SliverGeometry().toString(),
      equals('SliverGeometry(scrollExtent: 0.0, hidden, maxPaintExtent: 0.0)'),
    );
694
    expect(
695 696 697 698 699 700 701
      const SliverGeometry(
        scrollExtent: 100.0,
        paintExtent: 50.0,
        layoutExtent: 20.0,
        visible: true,
      ).toString(),
      equals(
702
        'SliverGeometry(scrollExtent: 100.0, paintExtent: 50.0, layoutExtent: 20.0, maxPaintExtent: 0.0, cacheExtent: 20.0)',
703 704 705 706 707 708 709 710 711
      ),
    );
    expect(
      const SliverGeometry(
        scrollExtent: 100.0,
        paintExtent: 0.0,
        layoutExtent: 20.0,
      ).toString(),
      equals(
712
        'SliverGeometry(scrollExtent: 100.0, hidden, layoutExtent: 20.0, maxPaintExtent: 0.0, cacheExtent: 20.0)',
713 714
      ),
    );
715
  });
716 717 718 719

  test('Sliver paintBounds and semanticBounds - vertical', () {
    const double height = 150.0;

720 721
    final RenderSliver sliver = RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(400.0, height)),
722
    );
723
    final RenderViewport root = RenderViewport(
724
      axisDirection: AxisDirection.down,
725
      crossAxisDirection: AxisDirection.right,
726
      offset: ViewportOffset.zero(),
727 728 729 730 731 732
      children: <RenderSliver>[
        sliver,
      ],
    );
    layout(root);

733
    final Rect expectedRect = Rect.fromLTWH(0.0, 0.0, root.size.width, height);
734 735 736 737 738 739 740 741

    expect(sliver.paintBounds, expectedRect);
    expect(sliver.semanticBounds, expectedRect);
  });

  test('Sliver paintBounds and semanticBounds - horizontal', () {
    const double width = 150.0;

742 743
    final RenderSliver sliver = RenderSliverToBoxAdapter(
      child: RenderSizedBox(const Size(width, 400.0)),
744
    );
745
    final RenderViewport root = RenderViewport(
746
      axisDirection: AxisDirection.right,
747
      crossAxisDirection: AxisDirection.down,
748
      offset: ViewportOffset.zero(),
749 750 751 752 753 754
      children: <RenderSliver>[
        sliver,
      ],
    );
    layout(root);

755
    final Rect expectedRect = Rect.fromLTWH(0.0, 0.0, width, root.size.height);
756 757 758 759

    expect(sliver.paintBounds, expectedRect);
    expect(sliver.semanticBounds, expectedRect);
  });
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

  test('precedingScrollExtent is 0.0 for first Sliver in list', () {
    const double viewportWidth = 800.0;
    final RenderSliver sliver = RenderSliverToBoxAdapter(
      child: RenderSizedBox(const Size(viewportWidth, 150.0)),
    );
    final RenderViewport root = RenderViewport(
      axisDirection: AxisDirection.down,
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      children: <RenderSliver>[
        sliver,
      ],
    );
    layout(root);

    expect(sliver.constraints.precedingScrollExtent, 0.0);
  });

  test('precedingScrollExtent accumulates over multiple Slivers', () {
    const double viewportWidth = 800.0;
    final RenderSliver sliver1 = RenderSliverToBoxAdapter(
      child: RenderSizedBox(const Size(viewportWidth, 150.0)),
    );
    final RenderSliver sliver2 = RenderSliverToBoxAdapter(
      child: RenderSizedBox(const Size(viewportWidth, 150.0)),
    );
    final RenderSliver sliver3 = RenderSliverToBoxAdapter(
      child: RenderSizedBox(const Size(viewportWidth, 150.0)),
    );
    final RenderViewport root = RenderViewport(
      axisDirection: AxisDirection.down,
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      children: <RenderSliver>[
        sliver1,
        sliver2,
        sliver3,
      ],
    );
    layout(root);

    // The 3rd Sliver comes after 300.0px of preceding scroll extent by first 2 Slivers.
    expect(sliver3.constraints.precedingScrollExtent, 300.0);
  });

  test('precedingScrollExtent is not impacted by scrollOffset', () {
    const double viewportWidth = 800.0;
    final RenderSliver sliver1 = RenderSliverToBoxAdapter(
      child: RenderSizedBox(const Size(viewportWidth, 150.0)),
    );
    final RenderSliver sliver2 = RenderSliverToBoxAdapter(
      child: RenderSizedBox(const Size(viewportWidth, 150.0)),
    );
    final RenderSliver sliver3 = RenderSliverToBoxAdapter(
      child: RenderSizedBox(const Size(viewportWidth, 150.0)),
    );
    final RenderViewport root = RenderViewport(
      axisDirection: AxisDirection.down,
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.fixed(100.0),
      children: <RenderSliver>[
        sliver1,
        sliver2,
        sliver3,
      ],
    );
    layout(root);

    // The 3rd Sliver comes after 300.0px of preceding scroll extent by first 2 Slivers.
    // In this test a ViewportOffset is applied to simulate a scrollOffset. That
    // offset is not expected to impact the precedingScrollExtent.
    expect(sliver3.constraints.precedingScrollExtent, 300.0);
  });
834 835 836 837 838 839

  group('hit testing', () {
    test('SliverHitTestResult wrapping HitTestResult', () {
      final HitTestEntry entry1 = HitTestEntry(_DummyHitTestTarget());
      final HitTestEntry entry2 = HitTestEntry(_DummyHitTestTarget());
      final HitTestEntry entry3 = HitTestEntry(_DummyHitTestTarget());
840
      final Matrix4 transform = Matrix4.translationValues(40.0, 150.0, 0.0);
841

842 843
      final HitTestResult wrapped = MyHitTestResult()
        ..publicPushTransform(transform);
844 845
      wrapped.add(entry1);
      expect(wrapped.path, equals(<HitTestEntry>[entry1]));
846
      expect(entry1.transform, transform);
847 848 849 850 851 852 853 854

      final SliverHitTestResult wrapping = SliverHitTestResult.wrap(wrapped);
      expect(wrapping.path, equals(<HitTestEntry>[entry1]));
      expect(wrapping.path, same(wrapped.path));

      wrapping.add(entry2);
      expect(wrapping.path, equals(<HitTestEntry>[entry1, entry2]));
      expect(wrapped.path, equals(<HitTestEntry>[entry1, entry2]));
855
      expect(entry2.transform, transform);
856 857 858 859

      wrapped.add(entry3);
      expect(wrapping.path, equals(<HitTestEntry>[entry1, entry2, entry3]));
      expect(wrapped.path, equals(<HitTestEntry>[entry1, entry2, entry3]));
860
      expect(entry3.transform, transform);
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
    });

    test('addWithAxisOffset', () {
      final SliverHitTestResult result = SliverHitTestResult();
      final List<double> mainAxisPositions = <double>[];
      final List<double> crossAxisPositions = <double>[];
      const Offset paintOffsetDummy = Offset.zero;

      bool isHit = result.addWithAxisOffset(
        paintOffset: paintOffsetDummy,
        mainAxisOffset: 0.0,
        crossAxisOffset: 0.0,
        mainAxisPosition: 0.0,
        crossAxisPosition: 0.0,
        hitTest: (SliverHitTestResult result, { double mainAxisPosition, double crossAxisPosition }) {
          expect(result, isNotNull);
          mainAxisPositions.add(mainAxisPosition);
          crossAxisPositions.add(crossAxisPosition);
          return true;
        },
      );
      expect(isHit, isTrue);
      expect(mainAxisPositions.single, 0.0);
      expect(crossAxisPositions.single, 0.0);
      mainAxisPositions.clear();
      crossAxisPositions.clear();

      isHit = result.addWithAxisOffset(
        paintOffset: paintOffsetDummy,
        mainAxisOffset: 5.0,
        crossAxisOffset: 6.0,
        mainAxisPosition: 10.0,
        crossAxisPosition: 20.0,
        hitTest: (SliverHitTestResult result, { double mainAxisPosition, double crossAxisPosition }) {
          expect(result, isNotNull);
          mainAxisPositions.add(mainAxisPosition);
          crossAxisPositions.add(crossAxisPosition);
          return false;
        },
      );
      expect(isHit, isFalse);
      expect(mainAxisPositions.single, 10.0 - 5.0);
      expect(crossAxisPositions.single, 20.0 - 6.0);
      mainAxisPositions.clear();
      crossAxisPositions.clear();

      isHit = result.addWithAxisOffset(
        paintOffset: paintOffsetDummy,
        mainAxisOffset: -5.0,
        crossAxisOffset: -6.0,
        mainAxisPosition: 10.0,
        crossAxisPosition: 20.0,
        hitTest: (SliverHitTestResult result, { double mainAxisPosition, double crossAxisPosition }) {
          expect(result, isNotNull);
          mainAxisPositions.add(mainAxisPosition);
          crossAxisPositions.add(crossAxisPosition);
          return false;
        },
      );
      expect(isHit, isFalse);
      expect(mainAxisPositions.single, 10.0 + 5.0);
      expect(crossAxisPositions.single, 20.0 + 6.0);
      mainAxisPositions.clear();
      crossAxisPositions.clear();
    });
LongCatIsLooong's avatar
LongCatIsLooong committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955

    test('addWithAxisOffset with non zero paintOffset', () {
      final SliverHitTestResult result = SliverHitTestResult();
      double recordedMainAxisPosition;
      double recordedCrossAxisPosition;
      final HitTestEntry entry = HitTestEntry(_DummyHitTestTarget());
      const Offset paintOffset = Offset(7, 11);

      final bool isHit = result.addWithAxisOffset(
        paintOffset: paintOffset,
        mainAxisOffset: 5.0,
        crossAxisOffset: 6.0,
        mainAxisPosition: 10.0,
        crossAxisPosition: 20.0,
        hitTest: (SliverHitTestResult result, { double mainAxisPosition, double crossAxisPosition }) {
          expect(result, isNotNull);
          recordedMainAxisPosition = mainAxisPosition;
          recordedCrossAxisPosition = crossAxisPosition;
          result.add(entry);
          return true;
        },
      );
      expect(isHit, isTrue);
      expect(recordedMainAxisPosition, 10.0 - 5.0);
      expect(recordedCrossAxisPosition, 20.0 - 6.0);
      expect(
        entry.transform..translate(paintOffset.dx, paintOffset.dy),
        Matrix4.identity(),
      );
    });
956
  });
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

  test('SliverConstraints check for NaN on all double properties', () {
    const SliverConstraints constraints = SliverConstraints(
      axisDirection: AxisDirection.down,
      cacheOrigin: double.nan,
      crossAxisDirection: AxisDirection.left,
      crossAxisExtent: double.nan,
      growthDirection: GrowthDirection.forward,
      overlap: double.nan,
      precedingScrollExtent: double.nan,
      remainingCacheExtent: double.nan,
      remainingPaintExtent: double.nan,
      scrollOffset: double.nan,
      userScrollDirection: ScrollDirection.idle,
      viewportMainAxisExtent: double.nan,
    );
    bool threw = false;
    try {
      constraints.debugAssertIsValid();
    } on FlutterError catch (error) {
      expect(
        error.message,
        'SliverConstraints is not valid:\n'
        '  The "scrollOffset" is NaN.\n'
        '  The "overlap" is NaN.\n'
        '  The "crossAxisExtent" is NaN.\n'
        '  The "scrollOffset" is NaN, expected greater than or equal to zero.\n'
        '  The "viewportMainAxisExtent" is NaN, expected greater than or equal to zero.\n'
        '  The "remainingPaintExtent" is NaN, expected greater than or equal to zero.\n'
        '  The "remainingCacheExtent" is NaN, expected greater than or equal to zero.\n'
        '  The "cacheOrigin" is NaN, expected less than or equal to zero.\n'
        '  The "precedingScrollExtent" is NaN, expected greater than or equal to zero.\n'
        '  The constraints are not normalized.\n'
        'The offending constraints were:\n'
991
        '  SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: NaN, remainingPaintExtent: NaN, overlap: NaN, crossAxisExtent: NaN, crossAxisDirection: AxisDirection.left, viewportMainAxisExtent: NaN, remainingCacheExtent: NaN, cacheOrigin: NaN)',
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
      );
      threw = true;
    }
    expect(threw, true);
  });

  test('SliverConstraints check for sign on relevant double properties', () {
    const SliverConstraints constraints = SliverConstraints(
      axisDirection: AxisDirection.down,
      cacheOrigin: 1.0,
      crossAxisDirection: AxisDirection.left,
      crossAxisExtent: 0.0,
      growthDirection: GrowthDirection.forward,
      overlap: 0.0,
      precedingScrollExtent: -1.0,
      remainingCacheExtent: -1.0,
      remainingPaintExtent: -1.0,
      scrollOffset: -1.0,
      userScrollDirection: ScrollDirection.idle,
      viewportMainAxisExtent: 0.0,
    );
    bool threw = false;
    try {
      constraints.debugAssertIsValid();
    } on FlutterError catch (error) {
      expect(
        error.message,
        'SliverConstraints is not valid:\n'
        '  The "scrollOffset" is negative.\n'
        '  The "remainingPaintExtent" is negative.\n'
        '  The "remainingCacheExtent" is negative.\n'
        '  The "cacheOrigin" is positive.\n'
        '  The "precedingScrollExtent" is negative.\n'
        '  The constraints are not normalized.\n'
        'The offending constraints were:\n'
1027
        '  SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: -1.0, remainingPaintExtent: -1.0, crossAxisExtent: 0.0, crossAxisDirection: AxisDirection.left, viewportMainAxisExtent: 0.0, remainingCacheExtent: -1.0, cacheOrigin: 1.0)',
1028 1029 1030 1031 1032
      );
      threw = true;
    }
    expect(threw, true);
  });
1033 1034 1035 1036 1037 1038 1039
}

class _DummyHitTestTarget implements HitTestTarget {
  @override
  void handleEvent(PointerEvent event, HitTestEntry entry) {
    // Nothing to do.
  }
1040
}
1041 1042 1043 1044

class MyHitTestResult extends HitTestResult {
  void publicPushTransform(Matrix4 transform) => pushTransform(transform);
}