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

51 52 53
    pumpFrame();
  });

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

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

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

    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));
178 179 180
    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));
181

182
    root.offset = ViewportOffset.fixed(200.0);
183
    pumpFrame();
184 185 186 187 188
    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));
189

190
    root.offset = ViewportOffset.fixed(600.0);
191
    pumpFrame();
192 193 194 195 196
    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));
197

198
    root.offset = ViewportOffset.fixed(900.0);
199
    pumpFrame();
200 201 202 203 204
    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));
205

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

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

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

230 231 232 233 234
    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));
235

236
    root.offset = ViewportOffset.fixed(200.0);
237
    pumpFrame();
238 239 240 241 242
    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));
243

244
    root.offset = ViewportOffset.fixed(600.0);
245
    pumpFrame();
246 247 248 249 250
    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));
251

252
    root.offset = ViewportOffset.fixed(900.0);
253
    pumpFrame();
254 255 256 257 258
    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));
259

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

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

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

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

289 290 291 292 293
    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;
294

295 296 297 298 299
    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));
300

301
    expect(_getPaintOrigin(sliverA), Offset.zero);
302 303
    expect(_getPaintOrigin(sliverB), const Offset(400.0, 0.0));
    expect(_getPaintOrigin(sliverC), const Offset(800.0, 0.0));
304 305
    expect(_getPaintOrigin(sliverD), const Offset(1200.0, 0.0));
    expect(_getPaintOrigin(sliverE), const Offset(1600.0, 0.0));
306

307
    root.offset = ViewportOffset.fixed(200.0);
308
    pumpFrame();
309 310 311 312 313
    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));
314

315
    expect(_getPaintOrigin(sliverA), Offset.zero);
316 317
    expect(_getPaintOrigin(sliverB), const Offset(200.0, 0.0));
    expect(_getPaintOrigin(sliverC), const Offset(600.0, 0.0));
318 319
    expect(_getPaintOrigin(sliverD), const Offset(1000.0, 0.0));
    expect(_getPaintOrigin(sliverE), const Offset(1400.0, 0.0));
320

321
    root.offset = ViewportOffset.fixed(600.0);
322
    pumpFrame();
323 324 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));

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

335
    root.offset = ViewportOffset.fixed(900.0);
336
    pumpFrame();
337 338 339 340 341 342 343 344 345
    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));

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

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

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

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

373 374 375 376 377
    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));
378

379
    root.offset = ViewportOffset.fixed(200.0);
380
    pumpFrame();
381 382 383 384 385
    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));
386

387
    root.offset = ViewportOffset.fixed(600.0);
388
    pumpFrame();
389 390 391 392 393
    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));
394

395
    root.offset = ViewportOffset.fixed(900.0);
396
    pumpFrame();
397 398 399 400 401
    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));
402

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

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

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

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

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

441 442 443 444 445
    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));
446

447 448
    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));
449 450 451
    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));
452

453
    root.offset = ViewportOffset.fixed(200.0);
454
    pumpFrame();
455 456 457 458 459
    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));
460

461
    root.offset = ViewportOffset.fixed(600.0);
462
    pumpFrame();
463 464 465 466 467
    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));
468

469
    root.offset = ViewportOffset.fixed(900.0);
470
    pumpFrame();
471 472 473 474 475
    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));
476

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

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

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

501 502 503 504 505
    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));
506

507
    root.offset = ViewportOffset.fixed(200.0);
508
    pumpFrame();
509 510 511 512 513
    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));
514

515
    root.offset = ViewportOffset.fixed(600.0);
516
    pumpFrame();
517 518 519 520 521
    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));
522

523
    root.offset = ViewportOffset.fixed(900.0);
524
    pumpFrame();
525 526 527 528 529
    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));
530

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

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

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

555 556 557 558 559
    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));
560

561
    root.offset = ViewportOffset.fixed(200.0);
562
    pumpFrame();
563 564 565 566 567
    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));
568

569
    root.offset = ViewportOffset.fixed(600.0);
570
    pumpFrame();
571 572 573 574 575
    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));
576

577
    root.offset = ViewportOffset.fixed(900.0);
578
    pumpFrame();
579 580 581 582 583
    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));
584

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

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

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

609 610 611 612 613
    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));
614

615
    root.offset = ViewportOffset.fixed(200.0);
616
    pumpFrame();
617 618 619 620 621
    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));
622

623
    root.offset = ViewportOffset.fixed(600.0);
624
    pumpFrame();
625 626 627 628 629
    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));
630

631
    root.offset = ViewportOffset.fixed(900.0);
632
    pumpFrame();
633 634 635 636 637
    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));
638

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

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

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

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

716 717
    final RenderSliver sliver = RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(400.0, height)),
718
    );
719
    final RenderViewport root = RenderViewport(
720
      axisDirection: AxisDirection.down,
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 827 828 829

  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);
  });
830 831 832 833 834 835

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

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

      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]));
851
      expect(entry2.transform, transform);
852 853 854 855

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

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

    test('addWithAxisOffset with non zero paintOffset', () {
      final SliverHitTestResult result = SliverHitTestResult();
922 923
      late double recordedMainAxisPosition;
      late double recordedCrossAxisPosition;
LongCatIsLooong's avatar
LongCatIsLooong committed
924 925 926 927 928 929 930 931 932
      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,
933
        hitTest: (SliverHitTestResult result, { required double mainAxisPosition, required double crossAxisPosition }) {
LongCatIsLooong's avatar
LongCatIsLooong committed
934 935 936 937 938 939 940 941 942 943
          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(
944
        entry.transform!..translate(paintOffset.dx, paintOffset.dy),
LongCatIsLooong's avatar
LongCatIsLooong committed
945 946 947
        Matrix4.identity(),
      );
    });
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 980 981 982

  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'
983
        '  SliverConstraints(AxisDirection.down, GrowthDirection.forward, ScrollDirection.idle, scrollOffset: NaN, remainingPaintExtent: NaN, overlap: NaN, crossAxisExtent: NaN, crossAxisDirection: AxisDirection.left, viewportMainAxisExtent: NaN, remainingCacheExtent: NaN, cacheOrigin: NaN)',
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 1016 1017 1018
      );
      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'
1019
        '  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)',
1020 1021 1022 1023 1024
      );
      threw = true;
    }
    expect(threw, true);
  });
1025 1026 1027 1028 1029 1030 1031
}

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

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