slivers_test.dart 44.7 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
import 'package:flutter/gestures.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:vector_math/vector_math_64.dart';
9 10 11 12

import 'rendering_tester.dart';

void main() {
13 14
  TestRenderingFlutterBinding.ensureInitialized();

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

53 54 55
    pumpFrame();
  });

Adam Barth's avatar
Adam Barth committed
56
  test('RenderViewport basic test - down', () {
57
    RenderBox a, b, c, d, e;
58
    final RenderViewport root = RenderViewport(
59
      crossAxisDirection: AxisDirection.right,
60
      offset: ViewportOffset.zero(),
61
      children: <RenderSliver>[
62 63 64 65 66
        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))),
67 68
      ],
    );
69
    expect(root, hasAGoodToStringDeep);
70 71 72 73 74
    layout(root);

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

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

    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));
180 181 182
    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));
183

184
    root.offset = ViewportOffset.fixed(200.0);
185
    pumpFrame();
186 187 188 189 190
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 600.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, 1000.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, 1400.0));
191

192
    root.offset = ViewportOffset.fixed(600.0);
193
    pumpFrame();
194 195 196 197 198
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, -600.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, 600.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, 1000.0));
199

200
    root.offset = ViewportOffset.fixed(900.0);
201
    pumpFrame();
202 203 204 205 206
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, -900.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -500.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, -100.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, 300.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, 700.0));
207

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

Adam Barth's avatar
Adam Barth committed
213
  test('RenderViewport basic test - up', () {
214
    RenderBox a, b, c, d, e;
215
    final RenderViewport root = RenderViewport(
216
      axisDirection: AxisDirection.up,
217
      crossAxisDirection: AxisDirection.right,
218
      offset: ViewportOffset.zero(),
219
      children: <RenderSliver>[
220 221 222 223 224
        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))),
225 226 227 228 229 230 231
      ],
    );
    layout(root);

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

232 233 234 235 236
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, -600.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -1000.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, -1400.0));
237

238
    root.offset = ViewportOffset.fixed(200.0);
239
    pumpFrame();
240 241 242 243 244
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
    expect(b.localToGlobal(Offset.zero), Offset.zero);
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, -400.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -800.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, -1200.0));
245

246
    root.offset = ViewportOffset.fixed(600.0);
247
    pumpFrame();
248 249 250 251 252
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 800.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
    expect(c.localToGlobal(Offset.zero), Offset.zero);
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -400.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, -800.0));
253

254
    root.offset = ViewportOffset.fixed(900.0);
255
    pumpFrame();
256 257 258 259 260
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 1100.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 700.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 300.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -100.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, -500.0));
261

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

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

Adam Barth's avatar
Adam Barth committed
272
  test('RenderViewport basic test - right', () {
273
    RenderBox a, b, c, d, e;
274
    final RenderViewport root = RenderViewport(
275
      axisDirection: AxisDirection.right,
276
      crossAxisDirection: AxisDirection.down,
277
      offset: ViewportOffset.zero(),
278
      children: <RenderSliver>[
279 280 281 282 283
        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))),
284 285 286 287 288 289 290
      ],
    );
    layout(root);

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

291 292 293 294 295
    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;
296

297 298 299 300 301
    expect(a.localToGlobal(Offset.zero), Offset.zero);
    expect(b.localToGlobal(Offset.zero), const Offset(400.0, 0.0));
    expect(c.localToGlobal(Offset.zero), const Offset(800.0, 0.0));
    expect(d.localToGlobal(Offset.zero), const Offset(1200.0, 0.0));
    expect(e.localToGlobal(Offset.zero), const Offset(1600.0, 0.0));
302

303 304 305 306 307
    expect(getPaintOrigin(sliverA), Offset.zero);
    expect(getPaintOrigin(sliverB), const Offset(400.0, 0.0));
    expect(getPaintOrigin(sliverC), const Offset(800.0, 0.0));
    expect(getPaintOrigin(sliverD), const Offset(1200.0, 0.0));
    expect(getPaintOrigin(sliverE), const Offset(1600.0, 0.0));
308

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

317 318 319 320 321
    expect(getPaintOrigin(sliverA), Offset.zero);
    expect(getPaintOrigin(sliverB), const Offset(200.0, 0.0));
    expect(getPaintOrigin(sliverC), const Offset(600.0, 0.0));
    expect(getPaintOrigin(sliverD), const Offset(1000.0, 0.0));
    expect(getPaintOrigin(sliverE), const Offset(1400.0, 0.0));
322

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

331 332 333 334 335
    expect(getPaintOrigin(sliverA), Offset.zero);
    expect(getPaintOrigin(sliverB), Offset.zero);
    expect(getPaintOrigin(sliverC), const Offset(200.0, 0.0));
    expect(getPaintOrigin(sliverD), const Offset(600.0, 0.0));
    expect(getPaintOrigin(sliverE), const Offset(1000.0, 0.0));
336

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

345 346 347 348 349
    expect(getPaintOrigin(sliverA), Offset.zero);
    expect(getPaintOrigin(sliverB), Offset.zero);
    expect(getPaintOrigin(sliverC), Offset.zero);
    expect(getPaintOrigin(sliverD), const Offset(300.0, 0.0));
    expect(getPaintOrigin(sliverE), const Offset(700.0, 0.0));
350

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

Adam Barth's avatar
Adam Barth committed
356
  test('RenderViewport basic test - left', () {
357
    RenderBox a, b, c, d, e;
358
    final RenderViewport root = RenderViewport(
359
      axisDirection: AxisDirection.left,
360
      crossAxisDirection: AxisDirection.down,
361
      offset: ViewportOffset.zero(),
362
      children: <RenderSliver>[
363 364 365 366 367
        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))),
368 369 370 371 372 373 374
      ],
    );
    layout(root);

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

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

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

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

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

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

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

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

  test('RenderShrinkWrappingViewport basic test - down', () {
    RenderBox a, b, c, d, e;
427
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
428
      crossAxisDirection: AxisDirection.right,
429
      offset: ViewportOffset.zero(),
430
      children: <RenderSliver>[
431 432 433 434 435
        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))),
436 437 438 439 440 441 442
      ],
    );
    layout(root);

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

443 444 445 446 447
    expect(a.localToGlobal(Offset.zero), Offset.zero);
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 800.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, 1200.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, 1600.0));
448

449 450
    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));
451 452 453
    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));
454

455
    root.offset = ViewportOffset.fixed(200.0);
456
    pumpFrame();
457 458 459 460 461
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 600.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, 1000.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, 1400.0));
462

463
    root.offset = ViewportOffset.fixed(600.0);
464
    pumpFrame();
465 466 467 468 469
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, -600.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, 600.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, 1000.0));
470

471
    root.offset = ViewportOffset.fixed(900.0);
472
    pumpFrame();
473 474 475 476 477
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, -900.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -500.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, -100.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, 300.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, 700.0));
478

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

  test('RenderShrinkWrappingViewport basic test - up', () {
    RenderBox a, b, c, d, e;
486
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
487
      axisDirection: AxisDirection.up,
488
      crossAxisDirection: AxisDirection.right,
489
      offset: ViewportOffset.zero(),
490
      children: <RenderSliver>[
491 492 493 494 495
        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))),
496 497 498 499 500 501 502
      ],
    );
    layout(root);

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

503 504 505 506 507
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 200.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, -200.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, -600.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -1000.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, -1400.0));
508

509
    root.offset = ViewportOffset.fixed(200.0);
510
    pumpFrame();
511 512 513 514 515
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
    expect(b.localToGlobal(Offset.zero), Offset.zero);
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, -400.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -800.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, -1200.0));
516

517
    root.offset = ViewportOffset.fixed(600.0);
518
    pumpFrame();
519 520 521 522 523
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 800.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 400.0));
    expect(c.localToGlobal(Offset.zero), Offset.zero);
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -400.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, -800.0));
524

525
    root.offset = ViewportOffset.fixed(900.0);
526
    pumpFrame();
527 528 529 530 531
    expect(a.localToGlobal(Offset.zero), const Offset(0.0, 1100.0));
    expect(b.localToGlobal(Offset.zero), const Offset(0.0, 700.0));
    expect(c.localToGlobal(Offset.zero), const Offset(0.0, 300.0));
    expect(d.localToGlobal(Offset.zero), const Offset(0.0, -100.0));
    expect(e.localToGlobal(Offset.zero), const Offset(0.0, -500.0));
532

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

  test('RenderShrinkWrappingViewport basic test - right', () {
    RenderBox a, b, c, d, e;
540
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
541
      axisDirection: AxisDirection.right,
542
      crossAxisDirection: AxisDirection.down,
543
      offset: ViewportOffset.zero(),
544
      children: <RenderSliver>[
545 546 547 548 549
        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))),
550 551 552 553 554 555 556
      ],
    );
    layout(root);

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

557 558 559 560 561
    expect(a.localToGlobal(Offset.zero), Offset.zero);
    expect(b.localToGlobal(Offset.zero), const Offset(400.0, 0.0));
    expect(c.localToGlobal(Offset.zero), const Offset(800.0, 0.0));
    expect(d.localToGlobal(Offset.zero), const Offset(1200.0, 0.0));
    expect(e.localToGlobal(Offset.zero), const Offset(1600.0, 0.0));
562

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

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

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

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

  test('RenderShrinkWrappingViewport basic test - left', () {
    RenderBox a, b, c, d, e;
594
    final RenderShrinkWrappingViewport root = RenderShrinkWrappingViewport(
595
      axisDirection: AxisDirection.left,
596
      crossAxisDirection: AxisDirection.down,
597
      offset: ViewportOffset.zero(),
598
      children: <RenderSliver>[
599 600 601 602 603
        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))),
604 605 606 607 608 609 610
      ],
    );
    layout(root);

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

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

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

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

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

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

  test('RenderShrinkWrappingViewport shrinkwrap test - 1 child', () {
    RenderBox child;
648 649
    final RenderBox root = RenderPositionedBox(
      child: child = RenderShrinkWrappingViewport(
650
        axisDirection: AxisDirection.left,
651
        crossAxisDirection: AxisDirection.down,
652
        offset: ViewportOffset.fixed(200.0),
653
        children: <RenderSliver>[
654
          RenderSliverToBoxAdapter(child: RenderSizedBox(const Size(400.0, 100.0))),
655 656 657 658 659 660 661 662 663 664 665 666 667
        ],
      ),
    );
    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;
668 669
    final RenderBox root = RenderPositionedBox(
      child: child = RenderShrinkWrappingViewport(
670
        axisDirection: AxisDirection.right,
671
        crossAxisDirection: AxisDirection.down,
672
        offset: ViewportOffset.fixed(200.0),
673
        children: <RenderSliver>[
674 675
          RenderSliverToBoxAdapter(child: RenderSizedBox(const Size(300.0, 100.0))),
          RenderSliverToBoxAdapter(child: RenderSizedBox(const Size(150.0, 100.0))),
676 677 678 679 680 681 682 683 684 685
        ],
      ),
    );
    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));
  });
686 687 688

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

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

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

729
    final Rect expectedRect = Rect.fromLTWH(0.0, 0.0, root.size.width, height);
730 731 732 733 734 735 736 737

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

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

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

751
    final Rect expectedRect = Rect.fromLTWH(0.0, 0.0, width, root.size.height);
752 753 754 755

    expect(sliver.paintBounds, expectedRect);
    expect(sliver.semanticBounds, expectedRect);
  });
756 757 758 759 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

  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(
      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(
      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(
      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);
  });
827 828 829 830 831 832

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

835 836
      final HitTestResult wrapped = MyHitTestResult()
        ..publicPushTransform(transform);
837 838
      wrapped.add(entry1);
      expect(wrapped.path, equals(<HitTestEntry>[entry1]));
839
      expect(entry1.transform, transform);
840 841 842 843 844 845 846 847

      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]));
848
      expect(entry2.transform, transform);
849 850 851 852

      wrapped.add(entry3);
      expect(wrapping.path, equals(<HitTestEntry>[entry1, entry2, entry3]));
      expect(wrapped.path, equals(<HitTestEntry>[entry1, entry2, entry3]));
853
      expect(entry3.transform, transform);
854 855 856 857 858 859 860 861 862 863 864 865 866 867
    });

    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,
868
        hitTest: (SliverHitTestResult result, { required double mainAxisPosition, required double crossAxisPosition }) {
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
          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,
886
        hitTest: (SliverHitTestResult result, { required double mainAxisPosition, required double crossAxisPosition }) {
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
          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,
904
        hitTest: (SliverHitTestResult result, { required double mainAxisPosition, required double crossAxisPosition }) {
905 906 907 908 909 910 911 912 913 914 915
          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
916 917 918

    test('addWithAxisOffset with non zero paintOffset', () {
      final SliverHitTestResult result = SliverHitTestResult();
919 920
      late double recordedMainAxisPosition;
      late double recordedCrossAxisPosition;
LongCatIsLooong's avatar
LongCatIsLooong committed
921 922 923 924 925 926 927 928 929
      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,
930
        hitTest: (SliverHitTestResult result, { required double mainAxisPosition, required double crossAxisPosition }) {
LongCatIsLooong's avatar
LongCatIsLooong committed
931 932 933 934 935 936 937 938 939 940
          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(
941
        entry.transform!..translate(paintOffset.dx, paintOffset.dy),
LongCatIsLooong's avatar
LongCatIsLooong committed
942 943 944
        Matrix4.identity(),
      );
    });
945
  });
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979

  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'
980
        '  SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: NaN, remainingPaintExtent: NaN, overlap: NaN, crossAxisExtent: NaN, crossAxisDirection: AxisDirection.left, viewportMainAxisExtent: NaN, remainingCacheExtent: NaN, cacheOrigin: NaN)',
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
      );
      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'
1016
        '  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)',
1017 1018 1019 1020 1021
      );
      threw = true;
    }
    expect(threw, true);
  });
1022 1023 1024 1025 1026 1027 1028
}

class _DummyHitTestTarget implements HitTestTarget {
  @override
  void handleEvent(PointerEvent event, HitTestEntry entry) {
    // Nothing to do.
  }
1029
}
1030 1031 1032 1033

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