proxy_box_test.dart 41.2 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 'dart:ui' as ui show Gradient, Image, ImageFilter;
6

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/material.dart';
9
import 'package:flutter/rendering.dart';
10
import 'package:flutter_test/flutter_test.dart';
11 12 13 14

import 'rendering_tester.dart';

void main() {
15
  TestRenderingFlutterBinding.ensureInitialized();
16 17 18 19 20 21 22 23 24
  test('RenderFittedBox handles applying paint transform and hit-testing with empty size', () {
    final RenderFittedBox fittedBox = RenderFittedBox(
      child: RenderCustomPaint(
        painter: TestCallbackPainter(onPaint: () {}),
      ),
    );

    layout(fittedBox, phase: EnginePhase.flushSemantics);
    final Matrix4 transform = Matrix4.identity();
25
    fittedBox.applyPaintTransform(fittedBox.child!, transform);
26 27 28
    expect(transform, Matrix4.zero());

    final BoxHitTestResult hitTestResult = BoxHitTestResult();
29
    expect(fittedBox.hitTestChildren(hitTestResult, position: Offset.zero), isFalse);
30 31
  });

32
  test('RenderFittedBox does not paint with empty sizes', () {
33
    bool painted;
34
    RenderFittedBox makeFittedBox(Size size) {
35 36
      return RenderFittedBox(
        child: RenderCustomPaint(
37
          preferredSize: size,
38
          painter: TestCallbackPainter(onPaint: () {
39 40
            painted = true;
          }),
41 42 43 44
        ),
      );
    }

45
    // The RenderFittedBox paints if both its size and its child's size are nonempty.
46
    painted = false;
47
    layout(makeFittedBox(const Size(1, 1)), phase: EnginePhase.paint);
48 49
    expect(painted, equals(true));

50 51 52 53 54
    // The RenderFittedBox should not paint if its child is empty-sized.
    painted = false;
    layout(makeFittedBox(Size.zero), phase: EnginePhase.paint);
    expect(painted, equals(false));

55 56
    // The RenderFittedBox should not paint if it is empty.
    painted = false;
57
    layout(makeFittedBox(const Size(1, 1)), constraints: BoxConstraints.tight(Size.zero), phase: EnginePhase.paint);
58 59
    expect(painted, equals(false));
  });
60

61
  test('RenderPhysicalModel compositing', () {
62
    final RenderPhysicalModel root = RenderPhysicalModel(color: const Color(0xffff00ff));
63
    layout(root, phase: EnginePhase.composite);
64
    expect(root.needsCompositing, isFalse);
65 66 67 68 69

    // On Fuchsia, the system compositor is responsible for drawing shadows
    // for physical model layers with non-zero elevation.
    root.elevation = 1.0;
    pumpFrame(phase: EnginePhase.composite);
70
    expect(root.needsCompositing, isFalse);
71 72 73

    root.elevation = 0.0;
    pumpFrame(phase: EnginePhase.composite);
74
    expect(root.needsCompositing, isFalse);
75
  });
76 77

  test('RenderSemanticsGestureHandler adds/removes correct semantic actions', () {
78
    final RenderSemanticsGestureHandler renderObj = RenderSemanticsGestureHandler(
79 80
      onTap: () { },
      onHorizontalDragUpdate: (DragUpdateDetails details) { },
81 82
    );

83
    SemanticsConfiguration config = SemanticsConfiguration();
84 85 86 87
    renderObj.describeSemanticsConfiguration(config);
    expect(config.getActionHandler(SemanticsAction.tap), isNotNull);
    expect(config.getActionHandler(SemanticsAction.scrollLeft), isNotNull);
    expect(config.getActionHandler(SemanticsAction.scrollRight), isNotNull);
88

89
    config = SemanticsConfiguration();
90
    renderObj.validActions = <SemanticsAction>{SemanticsAction.tap, SemanticsAction.scrollLeft};
91

92 93 94 95
    renderObj.describeSemanticsConfiguration(config);
    expect(config.getActionHandler(SemanticsAction.tap), isNotNull);
    expect(config.getActionHandler(SemanticsAction.scrollLeft), isNotNull);
    expect(config.getActionHandler(SemanticsAction.scrollRight), isNull);
96
  });
97 98 99

  group('RenderPhysicalShape', () {
    test('shape change triggers repaint', () {
Dan Field's avatar
Dan Field committed
100 101 102 103 104 105 106 107 108
      for (final TargetPlatform platform in TargetPlatform.values) {
        debugDefaultTargetPlatformOverride = platform;

        final RenderPhysicalShape root = RenderPhysicalShape(
          color: const Color(0xffff00ff),
          clipper: const ShapeBorderClipper(shape: CircleBorder()),
        );
        layout(root, phase: EnginePhase.composite);
        expect(root.debugNeedsPaint, isFalse);
109

Dan Field's avatar
Dan Field committed
110 111 112
        // Same shape, no repaint.
        root.clipper = const ShapeBorderClipper(shape: CircleBorder());
        expect(root.debugNeedsPaint, isFalse);
113

Dan Field's avatar
Dan Field committed
114 115 116 117 118
        // Different shape triggers repaint.
        root.clipper = const ShapeBorderClipper(shape: StadiumBorder());
        expect(root.debugNeedsPaint, isTrue);
      }
      debugDefaultTargetPlatformOverride = null;
119 120
    });

121
    test('compositing', () {
Dan Field's avatar
Dan Field committed
122 123 124 125 126 127 128
      for (final TargetPlatform platform in TargetPlatform.values) {
        debugDefaultTargetPlatformOverride = platform;
        final RenderPhysicalShape root = RenderPhysicalShape(
          color: const Color(0xffff00ff),
          clipper: const ShapeBorderClipper(shape: CircleBorder()),
        );
        layout(root, phase: EnginePhase.composite);
129
        expect(root.needsCompositing, isFalse);
Dan Field's avatar
Dan Field committed
130 131 132 133

        // On non-Fuchsia platforms, we composite physical shape layers
        root.elevation = 1.0;
        pumpFrame(phase: EnginePhase.composite);
134
        expect(root.needsCompositing, isFalse);
Dan Field's avatar
Dan Field committed
135 136 137

        root.elevation = 0.0;
        pumpFrame(phase: EnginePhase.composite);
138
        expect(root.needsCompositing, isFalse);
Dan Field's avatar
Dan Field committed
139
      }
140 141 142
      debugDefaultTargetPlatformOverride = null;
    });
  });
143 144

  test('RenderRepaintBoundary can capture images of itself', () async {
145 146
    RenderRepaintBoundary boundary = RenderRepaintBoundary();
    layout(boundary, constraints: BoxConstraints.tight(const Size(100.0, 200.0)));
147 148 149 150 151 152
    pumpFrame(phase: EnginePhase.composite);
    ui.Image image = await boundary.toImage();
    expect(image.width, equals(100));
    expect(image.height, equals(200));

    // Now with pixel ratio set to something other than 1.0.
153 154
    boundary = RenderRepaintBoundary();
    layout(boundary, constraints: BoxConstraints.tight(const Size(100.0, 200.0)));
155 156 157 158 159 160
    pumpFrame(phase: EnginePhase.composite);
    image = await boundary.toImage(pixelRatio: 2.0);
    expect(image.width, equals(200));
    expect(image.height, equals(400));

    // Try building one with two child layers and make sure it renders them both.
161 162 163
    boundary = RenderRepaintBoundary();
    final RenderStack stack = RenderStack()..alignment = Alignment.topLeft;
    final RenderDecoratedBox blackBox = RenderDecoratedBox(
164 165 166 167 168 169 170 171 172 173
      decoration: const BoxDecoration(color: Color(0xff000000)),
      child: RenderConstrainedBox(
        additionalConstraints: BoxConstraints.tight(const Size.square(20.0)),
      ),
    );
    stack.add(
      RenderOpacity()
        ..opacity = 0.5
        ..child = blackBox,
    );
174
    final RenderDecoratedBox whiteBox = RenderDecoratedBox(
175 176 177 178 179
      decoration: const BoxDecoration(color: Color(0xffffffff)),
      child: RenderConstrainedBox(
        additionalConstraints: BoxConstraints.tight(const Size.square(10.0)),
      ),
    );
180
    final RenderPositionedBox positioned = RenderPositionedBox(
181 182 183 184 185 186 187
      widthFactor: 2.0,
      heightFactor: 2.0,
      alignment: Alignment.topRight,
      child: whiteBox,
    );
    stack.add(positioned);
    boundary.child = stack;
188
    layout(boundary, constraints: BoxConstraints.tight(const Size(20.0, 20.0)));
189 190 191 192
    pumpFrame(phase: EnginePhase.composite);
    image = await boundary.toImage();
    expect(image.width, equals(20));
    expect(image.height, equals(20));
193
    ByteData data = (await image.toByteData())!;
194 195 196

    int getPixel(int x, int y) => data.getUint32((x + y * image.width) * 4);

197 198
    expect(data.lengthInBytes, equals(20 * 20 * 4));
    expect(data.elementSizeInBytes, equals(1));
199
    expect(getPixel(0, 0), equals(0x00000080));
200 201
    expect(getPixel(image.width - 1, 0 ), equals(0xffffffff));

202
    final OffsetLayer layer = boundary.debugLayer! as OffsetLayer;
203 204 205 206

    image = await layer.toImage(Offset.zero & const Size(20.0, 20.0));
    expect(image.width, equals(20));
    expect(image.height, equals(20));
207
    data = (await image.toByteData())!;
208
    expect(getPixel(0, 0), equals(0x00000080));
209 210 211 212 213 214
    expect(getPixel(image.width - 1, 0 ), equals(0xffffffff));

    // non-zero offsets.
    image = await layer.toImage(const Offset(-10.0, -10.0) & const Size(30.0, 30.0));
    expect(image.width, equals(30));
    expect(image.height, equals(30));
215
    data = (await image.toByteData())!;
216
    expect(getPixel(0, 0), equals(0x00000000));
217
    expect(getPixel(10, 10), equals(0x00000080));
218 219 220 221 222 223 224
    expect(getPixel(image.width - 1, 0), equals(0x00000000));
    expect(getPixel(image.width - 1, 10), equals(0xffffffff));

    // offset combined with a custom pixel ratio.
    image = await layer.toImage(const Offset(-10.0, -10.0) & const Size(30.0, 30.0), pixelRatio: 2.0);
    expect(image.width, equals(60));
    expect(image.height, equals(60));
225
    data = (await image.toByteData())!;
226
    expect(getPixel(0, 0), equals(0x00000000));
227
    expect(getPixel(20, 20), equals(0x00000080));
228 229
    expect(getPixel(image.width - 1, 0), equals(0x00000000));
    expect(getPixel(image.width - 1, 20), equals(0xffffffff));
230
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/49857
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  test('RenderRepaintBoundary can capture images of itself synchronously', () async {
    RenderRepaintBoundary boundary = RenderRepaintBoundary();
    layout(boundary, constraints: BoxConstraints.tight(const Size(100.0, 200.0)));
    pumpFrame(phase: EnginePhase.composite);
    ui.Image image = boundary.toImageSync();
    expect(image.width, equals(100));
    expect(image.height, equals(200));

    // Now with pixel ratio set to something other than 1.0.
    boundary = RenderRepaintBoundary();
    layout(boundary, constraints: BoxConstraints.tight(const Size(100.0, 200.0)));
    pumpFrame(phase: EnginePhase.composite);
    image = boundary.toImageSync(pixelRatio: 2.0);
    expect(image.width, equals(200));
    expect(image.height, equals(400));

    // Try building one with two child layers and make sure it renders them both.
    boundary = RenderRepaintBoundary();
    final RenderStack stack = RenderStack()..alignment = Alignment.topLeft;
    final RenderDecoratedBox blackBox = RenderDecoratedBox(
      decoration: const BoxDecoration(color: Color(0xff000000)),
      child: RenderConstrainedBox(
        additionalConstraints: BoxConstraints.tight(const Size.square(20.0)),
      ),
    );
    stack.add(
      RenderOpacity()
        ..opacity = 0.5
        ..child = blackBox,
    );
    final RenderDecoratedBox whiteBox = RenderDecoratedBox(
      decoration: const BoxDecoration(color: Color(0xffffffff)),
      child: RenderConstrainedBox(
        additionalConstraints: BoxConstraints.tight(const Size.square(10.0)),
      ),
    );
    final RenderPositionedBox positioned = RenderPositionedBox(
      widthFactor: 2.0,
      heightFactor: 2.0,
      alignment: Alignment.topRight,
      child: whiteBox,
    );
    stack.add(positioned);
    boundary.child = stack;
    layout(boundary, constraints: BoxConstraints.tight(const Size(20.0, 20.0)));
    pumpFrame(phase: EnginePhase.composite);
    image = boundary.toImageSync();
    expect(image.width, equals(20));
    expect(image.height, equals(20));
    ByteData data = (await image.toByteData())!;

    int getPixel(int x, int y) => data.getUint32((x + y * image.width) * 4);

    expect(data.lengthInBytes, equals(20 * 20 * 4));
    expect(data.elementSizeInBytes, equals(1));
    expect(getPixel(0, 0), equals(0x00000080));
    expect(getPixel(image.width - 1, 0 ), equals(0xffffffff));

    final OffsetLayer layer = boundary.debugLayer! as OffsetLayer;

    image = layer.toImageSync(Offset.zero & const Size(20.0, 20.0));
    expect(image.width, equals(20));
    expect(image.height, equals(20));
    data = (await image.toByteData())!;
    expect(getPixel(0, 0), equals(0x00000080));
    expect(getPixel(image.width - 1, 0 ), equals(0xffffffff));

    // non-zero offsets.
    image = layer.toImageSync(const Offset(-10.0, -10.0) & const Size(30.0, 30.0));
    expect(image.width, equals(30));
    expect(image.height, equals(30));
    data = (await image.toByteData())!;
    expect(getPixel(0, 0), equals(0x00000000));
    expect(getPixel(10, 10), equals(0x00000080));
    expect(getPixel(image.width - 1, 0), equals(0x00000000));
    expect(getPixel(image.width - 1, 10), equals(0xffffffff));

    // offset combined with a custom pixel ratio.
    image = layer.toImageSync(const Offset(-10.0, -10.0) & const Size(30.0, 30.0), pixelRatio: 2.0);
    expect(image.width, equals(60));
    expect(image.height, equals(60));
    data = (await image.toByteData())!;
    expect(getPixel(0, 0), equals(0x00000000));
    expect(getPixel(20, 20), equals(0x00000080));
    expect(getPixel(image.width - 1, 0), equals(0x00000000));
    expect(getPixel(image.width - 1, 20), equals(0xffffffff));
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/49857

320
  test('RenderOpacity does not composite if it is transparent', () {
321
    final RenderOpacity renderOpacity = RenderOpacity(
322
      opacity: 0.0,
323
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
324 325 326 327 328 329
    );

    layout(renderOpacity, phase: EnginePhase.composite);
    expect(renderOpacity.needsCompositing, false);
  });

330
  test('RenderOpacity does composite if it is opaque', () {
331 332
    final RenderOpacity renderOpacity = RenderOpacity(
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
333 334
    );

335
    layout(renderOpacity, phase: EnginePhase.composite);
336
    expect(renderOpacity.needsCompositing, true);
337 338 339 340 341 342 343 344
  });

  test('RenderOpacity does composite if it is partially opaque', () {
    final RenderOpacity renderOpacity = RenderOpacity(
      opacity: 0.1,
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
    );

345
    layout(renderOpacity, phase: EnginePhase.composite);
346
    expect(renderOpacity.needsCompositing, true);
347 348
  });

349 350 351
  test('RenderOpacity reuses its layer', () {
    _testLayerReuse<OpacityLayer>(RenderOpacity(
      opacity: 0.5,  // must not be 0 or 1.0. Otherwise, it won't create a layer
352 353 354
      child: RenderRepaintBoundary(
        child: RenderSizedBox(const Size(1.0, 1.0)),
      ), // size doesn't matter
355 356 357
    ));
  });

358
  test('RenderAnimatedOpacity does not composite if it is transparent', () async {
359
    final Animation<double> opacityAnimation = AnimationController(
360
      vsync: FakeTickerProvider(),
361 362
    )..value = 0.0;

363
    final RenderAnimatedOpacity renderAnimatedOpacity = RenderAnimatedOpacity(
364
      opacity: opacityAnimation,
365
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
366 367 368 369 370 371
    );

    layout(renderAnimatedOpacity, phase: EnginePhase.composite);
    expect(renderAnimatedOpacity.needsCompositing, false);
  });

372
  test('RenderAnimatedOpacity does composite if it is opaque', () {
373
    final Animation<double> opacityAnimation = AnimationController(
374
      vsync: FakeTickerProvider(),
375 376
    )..value = 1.0;

377
    final RenderAnimatedOpacity renderAnimatedOpacity = RenderAnimatedOpacity(
378
      opacity: opacityAnimation,
379
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
380 381
    );

382
    layout(renderAnimatedOpacity, phase: EnginePhase.composite);
383
    expect(renderAnimatedOpacity.needsCompositing, true);
384 385 386 387 388 389 390 391 392 393 394 395
  });

  test('RenderAnimatedOpacity does composite if it is partially opaque', () {
    final Animation<double> opacityAnimation = AnimationController(
      vsync: FakeTickerProvider(),
    )..value = 0.5;

    final RenderAnimatedOpacity renderAnimatedOpacity = RenderAnimatedOpacity(
      opacity: opacityAnimation,
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
    );

396
    layout(renderAnimatedOpacity, phase: EnginePhase.composite);
Dan Field's avatar
Dan Field committed
397
    expect(renderAnimatedOpacity.needsCompositing, true);
398
  });
399 400 401

  test('RenderAnimatedOpacity reuses its layer', () {
    final Animation<double> opacityAnimation = AnimationController(
402
      vsync: FakeTickerProvider(),
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    )..value = 0.5;  // must not be 0 or 1.0. Otherwise, it won't create a layer

    _testLayerReuse<OpacityLayer>(RenderAnimatedOpacity(
      opacity: opacityAnimation,
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
    ));
  });

  test('RenderShaderMask reuses its layer', () {
    _testLayerReuse<ShaderMaskLayer>(RenderShaderMask(
      shaderCallback: (Rect rect) {
        return ui.Gradient.radial(
          rect.center,
          rect.shortestSide / 2.0,
          const <Color>[Color.fromRGBO(0, 0, 0, 1.0), Color.fromRGBO(255, 255, 255, 1.0)],
        );
      },
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
    ));
  });

  test('RenderBackdropFilter reuses its layer', () {
    _testLayerReuse<BackdropFilterLayer>(RenderBackdropFilter(
      filter: ui.ImageFilter.blur(),
      child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
    ));
  });

  test('RenderClipRect reuses its layer', () {
    _testLayerReuse<ClipRectLayer>(RenderClipRect(
      clipper: _TestRectClipper(),
434
      child: RenderRepaintBoundary(
435 436 437 438 439 440 441 442
        child: RenderSizedBox(const Size(1.0, 1.0)),
      ), // size doesn't matter
    ));
  });

  test('RenderClipRRect reuses its layer', () {
    _testLayerReuse<ClipRRectLayer>(RenderClipRRect(
      clipper: _TestRRectClipper(),
443
      child: RenderRepaintBoundary(
444 445 446 447 448 449 450 451
        child: RenderSizedBox(const Size(1.0, 1.0)),
      ), // size doesn't matter
    ));
  });

  test('RenderClipOval reuses its layer', () {
    _testLayerReuse<ClipPathLayer>(RenderClipOval(
      clipper: _TestRectClipper(),
452
      child: RenderRepaintBoundary(
453 454 455 456 457 458 459 460
        child: RenderSizedBox(const Size(1.0, 1.0)),
      ), // size doesn't matter
    ));
  });

  test('RenderClipPath reuses its layer', () {
    _testLayerReuse<ClipPathLayer>(RenderClipPath(
      clipper: _TestPathClipper(),
461
      child: RenderRepaintBoundary(
462 463 464 465 466 467
        child: RenderSizedBox(const Size(1.0, 1.0)),
      ), // size doesn't matter
    ));
  });

  test('RenderPhysicalModel reuses its layer', () {
468 469
    _testLayerReuse<ClipRRectLayer>(RenderPhysicalModel(
      clipBehavior: Clip.hardEdge,
470
      color: const Color.fromRGBO(0, 0, 0, 1.0),
471
      child: RenderRepaintBoundary(
472 473 474 475 476 477
        child: RenderSizedBox(const Size(1.0, 1.0)),
      ), // size doesn't matter
    ));
  });

  test('RenderPhysicalShape reuses its layer', () {
478
    _testLayerReuse<ClipPathLayer>(RenderPhysicalShape(
479
      clipper: _TestPathClipper(),
480
      clipBehavior: Clip.hardEdge,
481
      color: const Color.fromRGBO(0, 0, 0, 1.0),
482
      child: RenderRepaintBoundary(
483 484 485 486 487 488 489 490 491
        child: RenderSizedBox(const Size(1.0, 1.0)),
      ), // size doesn't matter
    ));
  });

  test('RenderTransform reuses its layer', () {
    _testLayerReuse<TransformLayer>(RenderTransform(
      // Use a 3D transform to force compositing.
      transform: Matrix4.rotationX(0.1),
492
      child: RenderRepaintBoundary(
493 494 495 496 497
        child: RenderSizedBox(const Size(1.0, 1.0)),
      ), // size doesn't matter
    ));
  });

498
  void testFittedBoxWithClipRectLayer() {
499 500
    _testLayerReuse<ClipRectLayer>(RenderFittedBox(
      fit: BoxFit.cover,
501
      clipBehavior: Clip.hardEdge,
502
      // Inject opacity under the clip to force compositing.
503
      child: RenderRepaintBoundary(
504 505 506 507 508
        child: RenderSizedBox(const Size(100.0, 200.0)),
      ), // size doesn't matter
    ));
  }

509
  void testFittedBoxWithTransformLayer() {
510 511 512
    _testLayerReuse<TransformLayer>(RenderFittedBox(
      fit: BoxFit.fill,
      // Inject opacity under the clip to force compositing.
513
      child: RenderRepaintBoundary(
514 515 516 517 518 519
        child: RenderSizedBox(const Size(1, 1)),
      ), // size doesn't matter
    ));
  }

  test('RenderFittedBox reuses ClipRectLayer', () {
520
    testFittedBoxWithClipRectLayer();
521 522 523
  });

  test('RenderFittedBox reuses TransformLayer', () {
524
    testFittedBoxWithTransformLayer();
525 526 527
  });

  test('RenderFittedBox switches between ClipRectLayer and TransformLayer, and reuses them', () {
528
    testFittedBoxWithClipRectLayer();
529 530

    // clip -> transform
531
    testFittedBoxWithTransformLayer();
532
    // transform -> clip
533
    testFittedBoxWithClipRectLayer();
534
  });
535

536 537
  test('RenderFittedBox respects clipBehavior', () {
    const BoxConstraints viewport = BoxConstraints(maxHeight: 100.0, maxWidth: 100.0);
538 539 540 541 542 543 544 545 546 547 548 549 550
    for (final Clip? clip in <Clip?>[null, ...Clip.values]) {
      final TestClipPaintingContext context = TestClipPaintingContext();
      final RenderFittedBox box;
      switch (clip) {
        case Clip.none:
        case Clip.hardEdge:
        case Clip.antiAlias:
        case Clip.antiAliasWithSaveLayer:
          box = RenderFittedBox(child: box200x200, fit: BoxFit.none, clipBehavior: clip!);
        case null:
          box = RenderFittedBox(child: box200x200, fit: BoxFit.none);
      }
      layout(box, constraints: viewport, phase: EnginePhase.composite, onErrors: expectNoFlutterErrors);
551
      box.paint(context, Offset.zero);
552 553
      // By default, clipBehavior should be Clip.none
      expect(context.clipBehavior, equals(clip ?? Clip.none));
554 555 556
    }
  });

557 558 559 560 561 562 563 564 565
  test('RenderMouseRegion can change properties when detached', () {
    final RenderMouseRegion object = RenderMouseRegion();
    object
      ..opaque = false
      ..onEnter = (_) {}
      ..onExit = (_) {}
      ..onHover = (_) {};
    // Passes if no error is thrown
  });
566 567 568 569 570 571 572 573 574 575 576 577

  test('RenderFractionalTranslation updates its semantics after its translation value is set', () {
    final _TestSemanticsUpdateRenderFractionalTranslation box = _TestSemanticsUpdateRenderFractionalTranslation(
      translation: const Offset(0.5, 0.5),
    );
    layout(box, constraints: BoxConstraints.tight(const Size(200.0, 200.0)));
    expect(box.markNeedsSemanticsUpdateCallCount, 1);
    box.translation = const Offset(0.4, 0.4);
    expect(box.markNeedsSemanticsUpdateCallCount, 2);
    box.translation = const Offset(0.3, 0.3);
    expect(box.markNeedsSemanticsUpdateCallCount, 3);
  });
578 579 580 581 582 583 584 585

  test('RenderFollowerLayer hit test without a leader layer and the showWhenUnlinked is true', () {
    final RenderFollowerLayer follower = RenderFollowerLayer(
      link: LayerLink(),
      child: RenderSizedBox(const Size(1.0, 1.0)),
    );
    layout(follower, constraints: BoxConstraints.tight(const Size(200.0, 200.0)));
    final BoxHitTestResult hitTestResult = BoxHitTestResult();
586
    expect(follower.hitTest(hitTestResult, position: Offset.zero), isTrue);
587 588 589 590 591 592 593 594 595 596
  });

  test('RenderFollowerLayer hit test without a leader layer and the showWhenUnlinked is false', () {
    final RenderFollowerLayer follower = RenderFollowerLayer(
      link: LayerLink(),
      showWhenUnlinked: false,
      child: RenderSizedBox(const Size(1.0, 1.0)),
    );
    layout(follower, constraints: BoxConstraints.tight(const Size(200.0, 200.0)));
    final BoxHitTestResult hitTestResult = BoxHitTestResult();
597
    expect(follower.hitTest(hitTestResult, position: Offset.zero), isFalse);
598 599 600 601 602 603 604 605 606 607 608 609 610 611
  });

  test('RenderFollowerLayer hit test with a leader layer and the showWhenUnlinked is true', () {
    // Creates a layer link with a leader.
    final LayerLink link = LayerLink();
    final LeaderLayer leader = LeaderLayer(link: link);
    leader.attach(Object());

    final RenderFollowerLayer follower = RenderFollowerLayer(
      link: link,
      child: RenderSizedBox(const Size(1.0, 1.0)),
    );
    layout(follower, constraints: BoxConstraints.tight(const Size(200.0, 200.0)));
    final BoxHitTestResult hitTestResult = BoxHitTestResult();
612
    expect(follower.hitTest(hitTestResult, position: Offset.zero), isTrue);
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
  });

  test('RenderFollowerLayer hit test with a leader layer and the showWhenUnlinked is false', () {
    // Creates a layer link with a leader.
    final LayerLink link = LayerLink();
    final LeaderLayer leader = LeaderLayer(link: link);
    leader.attach(Object());

    final RenderFollowerLayer follower = RenderFollowerLayer(
      link: link,
      showWhenUnlinked: false,
      child: RenderSizedBox(const Size(1.0, 1.0)),
    );
    layout(follower, constraints: BoxConstraints.tight(const Size(200.0, 200.0)));
    final BoxHitTestResult hitTestResult = BoxHitTestResult();
    // The follower is still hit testable because there is a leader layer.
629
    expect(follower.hitTest(hitTestResult, position: Offset.zero), isTrue);
630
  });
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 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

  test('RenderObject can become a repaint boundary', () {
    final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary();
    final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);

    layout(renderBox, phase: EnginePhase.composite);

    expect(childBox.paintCount, 1);
    expect(renderBox.paintCount, 1);

    renderBox.isRepaintBoundary = true;
    renderBox.markNeedsCompositingBitsUpdate();
    renderBox.markNeedsCompositedLayerUpdate();

    pumpFrame(phase: EnginePhase.composite);

    // The first time the render object becomes a repaint boundary
    // we must repaint from the parent to allow the layer to be
    // created.
    expect(childBox.paintCount, 2);
    expect(renderBox.paintCount, 2);
    expect(renderBox.debugLayer, isA<OffsetLayer>());

    renderBox.markNeedsCompositedLayerUpdate();
    expect(renderBox.debugNeedsPaint, false);
    expect(renderBox.debugNeedsCompositedLayerUpdate, true);

    pumpFrame(phase: EnginePhase.composite);

    // The second time the layer exists and we can skip paint.
    expect(childBox.paintCount, 2);
    expect(renderBox.paintCount, 2);
    expect(renderBox.debugLayer, isA<OffsetLayer>());

    renderBox.isRepaintBoundary = false;
    renderBox.markNeedsCompositingBitsUpdate();

    pumpFrame(phase: EnginePhase.composite);

    // Once it stops being a repaint boundary we must repaint to
    // remove the layer. its required that the render object
    // perform this action in paint.
    expect(childBox.paintCount, 3);
    expect(renderBox.paintCount, 3);
    expect(renderBox.debugLayer, null);

    // When the render object is not a repaint boundary, calling
    // markNeedsLayerPropertyUpdate is the same as calling
    // markNeedsPaint.

    renderBox.markNeedsCompositedLayerUpdate();
    expect(renderBox.debugNeedsPaint, true);
    expect(renderBox.debugNeedsCompositedLayerUpdate, true);
  });

  test('RenderObject with repaint boundary asserts when a composited layer is replaced during layer property update', () {
    final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
    final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);

    // Ignore old layer.
    childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
      return TestOffsetLayerA();
    };

    layout(renderBox, phase: EnginePhase.composite);

    expect(childBox.paintCount, 1);
    expect(renderBox.paintCount, 1);

    renderBox.markNeedsCompositedLayerUpdate();

    pumpFrame(phase: EnginePhase.composite, onErrors: expectAssertionError);
  }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/102086

  test('RenderObject with repaint boundary asserts when a composited layer is replaced during painting', () {
    final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
    final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);

    // Ignore old layer.
    childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
      return TestOffsetLayerA();
    };

    layout(renderBox, phase: EnginePhase.composite);

    expect(childBox.paintCount, 1);
    expect(renderBox.paintCount, 1);
    renderBox.markNeedsPaint();

    pumpFrame(phase: EnginePhase.composite, onErrors: expectAssertionError);
  }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/102086

  test('RenderObject with repaint boundary asserts when a composited layer tries to update its own offset', () {
    final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
    final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);

    // Ignore old layer.
    childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
      return (oldLayer ?? TestOffsetLayerA())..offset = const Offset(2133, 4422);
    };

    layout(renderBox, phase: EnginePhase.composite);

    expect(childBox.paintCount, 1);
    expect(renderBox.paintCount, 1);
    renderBox.markNeedsPaint();

    pumpFrame(phase: EnginePhase.composite, onErrors: expectAssertionError);
  }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/102086

  test('RenderObject markNeedsPaint while repaint boundary, and then updated to no longer be a repaint boundary with '
    'calling markNeedsCompositingBitsUpdate 1', () {
    final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
    final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
    // Ignore old layer.
    childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
      return oldLayer ?? TestOffsetLayerA();
    };

    layout(renderBox, phase: EnginePhase.composite);

    expect(childBox.paintCount, 1);
    expect(renderBox.paintCount, 1);

    childBox.markNeedsPaint();
    childBox.isRepaintBoundary = false;
    childBox.markNeedsCompositingBitsUpdate();

    expect(() => pumpFrame(phase: EnginePhase.composite), returnsNormally);
  });

  test('RenderObject markNeedsPaint while repaint boundary, and then updated to no longer be a repaint boundary with '
    'calling markNeedsCompositingBitsUpdate 2', () {
    final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
    final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
    // Ignore old layer.
    childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
      return oldLayer ?? TestOffsetLayerA();
    };

    layout(renderBox, phase: EnginePhase.composite);

    expect(childBox.paintCount, 1);
    expect(renderBox.paintCount, 1);

    childBox.isRepaintBoundary = false;
    childBox.markNeedsCompositingBitsUpdate();
    childBox.markNeedsPaint();

    expect(() => pumpFrame(phase: EnginePhase.composite), returnsNormally);
  });

  test('RenderObject markNeedsPaint while repaint boundary, and then updated to no longer be a repaint boundary with '
    'calling markNeedsCompositingBitsUpdate 3', () {
    final ConditionalRepaintBoundary childBox = ConditionalRepaintBoundary(isRepaintBoundary: true);
    final ConditionalRepaintBoundary renderBox = ConditionalRepaintBoundary(child: childBox);
    // Ignore old layer.
    childBox.offsetLayerFactory = (OffsetLayer? oldLayer) {
      return oldLayer ?? TestOffsetLayerA();
    };

    layout(renderBox, phase: EnginePhase.composite);

    expect(childBox.paintCount, 1);
    expect(renderBox.paintCount, 1);

    childBox.isRepaintBoundary = false;
    childBox.markNeedsCompositedLayerUpdate();
    childBox.markNeedsCompositingBitsUpdate();

    expect(() => pumpFrame(phase: EnginePhase.composite), returnsNormally);
  });
803

804
  test('Offstage implements paintsChild correctly', () {
805 806
    final RenderConstrainedBox box = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 20));
    final RenderConstrainedBox parent = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 20));
807
    final RenderOffstage offstage = RenderOffstage(offstage: false, child: box);
808
    parent.child = offstage;
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828

    expect(offstage.paintsChild(box), true);

    offstage.offstage = true;

    expect(offstage.paintsChild(box), false);
  });

  test('Opacity implements paintsChild correctly', () {
    final RenderBox box = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 20));
    final RenderOpacity opacity = RenderOpacity(child: box);

    expect(opacity.paintsChild(box), true);

    opacity.opacity = 0;

    expect(opacity.paintsChild(box), false);
  });

  test('AnimatedOpacity sets paint matrix to zero when alpha == 0', () {
829
    final RenderBox box = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 20));
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
    final AnimationController opacityAnimation = AnimationController(value: 1, vsync: FakeTickerProvider());
    final RenderAnimatedOpacity opacity = RenderAnimatedOpacity(opacity: opacityAnimation, child: box);

    // Make it listen to the animation.
    opacity.attach(PipelineOwner());

    expect(opacity.paintsChild(box), true);

    opacityAnimation.value = 0;

    expect(opacity.paintsChild(box), false);
  });

  test('AnimatedOpacity sets paint matrix to zero when alpha == 0 (sliver)', () {
    final RenderSliver sliver = RenderSliverToBoxAdapter(child: RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 20)));
    final AnimationController opacityAnimation = AnimationController(value: 1, vsync: FakeTickerProvider());
    final RenderSliverAnimatedOpacity opacity = RenderSliverAnimatedOpacity(opacity: opacityAnimation, sliver: sliver);

    // Make it listen to the animation.
    opacity.attach(PipelineOwner());

    expect(opacity.paintsChild(sliver), true);

    opacityAnimation.value = 0;

    expect(opacity.paintsChild(sliver), false);
  });

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
  test('RenderCustomClip extenders respect clipBehavior when asked to describeApproximateClip', () {
    final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
    final RenderClipRect renderClipRect = RenderClipRect(clipBehavior: Clip.none, child: child);
    layout(renderClipRect);
    expect(
      renderClipRect.describeApproximatePaintClip(child),
      null,
    );
    renderClipRect.clipBehavior = Clip.hardEdge;
    expect(
      renderClipRect.describeApproximatePaintClip(child),
      Offset.zero & renderClipRect.size,
    );
    renderClipRect.clipBehavior = Clip.antiAlias;
    expect(
      renderClipRect.describeApproximatePaintClip(child),
      Offset.zero & renderClipRect.size,
    );
    renderClipRect.clipBehavior = Clip.antiAliasWithSaveLayer;
    expect(
      renderClipRect.describeApproximatePaintClip(child),
      Offset.zero & renderClipRect.size,
    );
  });
882 883

  // Simulate painting a RenderBox as if 'debugPaintSizeEnabled == true'
884
  DebugPaintCallback debugPaint(RenderBox renderBox) {
885 886 887 888 889 890 891 892 893
    layout(renderBox);
    pumpFrame(phase: EnginePhase.compositingBits);
    return (PaintingContext context, Offset offset) {
      renderBox.paint(context, offset);
      renderBox.debugPaintSize(context, offset);
    };
  }

  test('RenderClipPath.debugPaintSize draws a path and a debug text when clipBehavior is not Clip.none', () {
894
    DebugPaintCallback debugPaintClipRect(Clip clip) {
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
      final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
      final RenderClipPath renderClipPath = RenderClipPath(clipBehavior: clip, child: child);
      return debugPaint(renderClipPath);
    }

    // RenderClipPath.debugPaintSize draws when clipBehavior is not Clip.none
    expect(debugPaintClipRect(Clip.hardEdge), paintsExactlyCountTimes(#drawPath, 1));
    expect(debugPaintClipRect(Clip.hardEdge), paintsExactlyCountTimes(#drawParagraph, 1));

    // RenderClipPath.debugPaintSize does not draw when clipBehavior is Clip.none
    // Regression test for https://github.com/flutter/flutter/issues/105969
    expect(debugPaintClipRect(Clip.none), paintsExactlyCountTimes(#drawPath, 0));
    expect(debugPaintClipRect(Clip.none), paintsExactlyCountTimes(#drawParagraph, 0));
  });

  test('RenderClipRect.debugPaintSize draws a rect and a debug text when clipBehavior is not Clip.none', () {
911
    DebugPaintCallback debugPaintClipRect(Clip clip) {
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
      final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
      final RenderClipRect renderClipRect = RenderClipRect(clipBehavior: clip, child: child);
      return debugPaint(renderClipRect);
    }

    // RenderClipRect.debugPaintSize draws when clipBehavior is not Clip.none
    expect(debugPaintClipRect(Clip.hardEdge), paintsExactlyCountTimes(#drawRect, 1));
    expect(debugPaintClipRect(Clip.hardEdge), paintsExactlyCountTimes(#drawParagraph, 1));

    // RenderClipRect.debugPaintSize does not draw when clipBehavior is Clip.none
    expect(debugPaintClipRect(Clip.none), paintsExactlyCountTimes(#drawRect, 0));
    expect(debugPaintClipRect(Clip.none), paintsExactlyCountTimes(#drawParagraph, 0));
  });

  test('RenderClipRRect.debugPaintSize draws a rounded rect and a debug text when clipBehavior is not Clip.none', () {
927
    DebugPaintCallback debugPaintClipRRect(Clip clip) {
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
      final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
      final RenderClipRRect renderClipRRect = RenderClipRRect(clipBehavior: clip, child: child);
      return debugPaint(renderClipRRect);
    }

    // RenderClipRRect.debugPaintSize draws when clipBehavior is not Clip.none
    expect(debugPaintClipRRect(Clip.hardEdge), paintsExactlyCountTimes(#drawRRect, 1));
    expect(debugPaintClipRRect(Clip.hardEdge), paintsExactlyCountTimes(#drawParagraph, 1));

    // RenderClipRRect.debugPaintSize does not draw when clipBehavior is Clip.none
    expect(debugPaintClipRRect(Clip.none), paintsExactlyCountTimes(#drawRRect, 0));
    expect(debugPaintClipRRect(Clip.none), paintsExactlyCountTimes(#drawParagraph, 0));
  });

  test('RenderClipOval.debugPaintSize draws a path and a debug text when clipBehavior is not Clip.none', () {
943
    DebugPaintCallback debugPaintClipOval(Clip clip) {
944 945 946 947 948 949 950 951 952 953 954 955 956
      final RenderBox child = RenderConstrainedBox(additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200));
      final RenderClipOval renderClipOval = RenderClipOval(clipBehavior: clip, child: child);
      return debugPaint(renderClipOval);
    }

    // RenderClipOval.debugPaintSize draws when clipBehavior is not Clip.none
    expect(debugPaintClipOval(Clip.hardEdge), paintsExactlyCountTimes(#drawPath, 1));
    expect(debugPaintClipOval(Clip.hardEdge), paintsExactlyCountTimes(#drawParagraph, 1));

    // RenderClipOval.debugPaintSize does not draw when clipBehavior is Clip.none
    expect(debugPaintClipOval(Clip.none), paintsExactlyCountTimes(#drawPath, 0));
    expect(debugPaintClipOval(Clip.none), paintsExactlyCountTimes(#drawParagraph, 0));
  });
957 958 959 960 961 962 963 964 965 966 967

  test('RenderProxyBox behavior can be mixed in along with another base class', () {
    final RenderFancyProxyBox fancyProxyBox = RenderFancyProxyBox(fancy: 6);
    // Box has behavior from its base class:
    expect(fancyProxyBox.fancyMethod(), 36);
    // Box has behavior from RenderProxyBox:
    expect(
      fancyProxyBox.computeDryLayout(const BoxConstraints(minHeight: 8)),
      const Size(0, 8),
    );
  });
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
}

class _TestRectClipper extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return Rect.zero;
  }

  @override
  Rect getApproximateClipRect(Size size) => getClip(size);

  @override
  bool shouldReclip(_TestRectClipper oldClipper) => true;
}

class _TestRRectClipper extends CustomClipper<RRect> {
  @override
  RRect getClip(Size size) {
    return RRect.zero;
  }

  @override
  Rect getApproximateClipRect(Size size) => getClip(size).outerRect;

  @override
  bool shouldReclip(_TestRRectClipper oldClipper) => true;
994 995
}

996 997 998
// Forces two frames and checks that:
// - a layer is created on the first frame
// - the layer is reused on the second frame
999
void _testLayerReuse<L extends Layer>(RenderBox renderObject) {
1000 1001 1002
  expect(L, isNot(Layer));
  expect(renderObject.debugLayer, null);
  layout(renderObject, phase: EnginePhase.paint, constraints: BoxConstraints.tight(const Size(10, 10)));
1003
  final Layer? layer = renderObject.debugLayer;
Dan Field's avatar
Dan Field committed
1004
  expect(layer, isA<L>());
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  expect(layer, isNotNull);

  // Mark for repaint otherwise pumpFrame is a noop.
  renderObject.markNeedsPaint();
  expect(renderObject.debugNeedsPaint, true);
  pumpFrame(phase: EnginePhase.paint);
  expect(renderObject.debugNeedsPaint, false);
  expect(renderObject.debugLayer, same(layer));
}

class _TestPathClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    return Path()
      ..addRect(const Rect.fromLTWH(50.0, 50.0, 100.0, 100.0));
  }
  @override
  bool shouldReclip(_TestPathClipper oldClipper) => false;
}
1024 1025 1026

class _TestSemanticsUpdateRenderFractionalTranslation extends RenderFractionalTranslation {
  _TestSemanticsUpdateRenderFractionalTranslation({
1027 1028
    required super.translation,
  });
1029 1030 1031 1032 1033 1034 1035 1036 1037

  int markNeedsSemanticsUpdateCallCount = 0;

  @override
  void markNeedsSemanticsUpdate() {
    markNeedsSemanticsUpdateCallCount++;
    super.markNeedsSemanticsUpdate();
  }
}
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

class ConditionalRepaintBoundary extends RenderProxyBox {
  ConditionalRepaintBoundary({this.isRepaintBoundary = false, RenderBox? child}) : super(child);

  @override
  bool isRepaintBoundary = false;

  OffsetLayer Function(OffsetLayer?)? offsetLayerFactory;

  int paintCount = 0;

  @override
  OffsetLayer updateCompositedLayer({required covariant OffsetLayer? oldLayer}) {
    if (offsetLayerFactory != null) {
      return offsetLayerFactory!.call(oldLayer);
    }
    return super.updateCompositedLayer(oldLayer: oldLayer);
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    paintCount += 1;
    super.paint(context, offset);
  }
}

class TestOffsetLayerA extends OffsetLayer {}

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
class RenderFancyBox extends RenderBox {
  RenderFancyBox({required this.fancy}) : super();

  late int fancy;

  int fancyMethod() {
    return fancy * fancy;
  }
}

class RenderFancyProxyBox extends RenderFancyBox
    with RenderObjectWithChildMixin<RenderBox>, RenderProxyBoxMixin<RenderBox> {
  RenderFancyProxyBox({required super.fancy});
}

1081 1082 1083 1084 1085 1086 1087
void expectAssertionError() {
  final FlutterErrorDetails errorDetails = TestRenderingFlutterBinding.instance.takeFlutterErrorDetails()!;
  final bool asserted = errorDetails.toString().contains('Failed assertion');
  if (!asserted) {
    FlutterError.reportError(errorDetails);
  }
}
1088 1089

typedef DebugPaintCallback = void Function(PaintingContext context, Offset offset);