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

import 'package:flutter/rendering.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8 9 10

import 'rendering_tester.dart';

void main() {
11 12
  TestRenderingFlutterBinding.ensureInitialized();

Ian Hickson's avatar
Ian Hickson committed
13
  test('nested repaint boundaries - smoke test', () {
14
    RenderOpacity a, b, c;
15 16 17 18
    a = RenderOpacity(
      child: RenderRepaintBoundary(
        child: b = RenderOpacity(
          child: RenderRepaintBoundary(
19
            child: c = RenderOpacity(),
20
          ),
21
        ),
22
      ),
23 24 25
    );
    layout(a, phase: EnginePhase.flushSemantics);
    c.opacity = 0.9;
26
    pumpFrame(phase: EnginePhase.flushSemantics);
27 28
    a.opacity = 0.8;
    c.opacity = 0.8;
29
    pumpFrame(phase: EnginePhase.flushSemantics);
30 31 32
    a.opacity = 0.7;
    b.opacity = 0.7;
    c.opacity = 0.7;
33
    pumpFrame(phase: EnginePhase.flushSemantics);
34
  });
35 36 37 38 39 40 41 42 43

  test('Repaint boundary can get new parent after markNeedsCompositingBitsUpdate', () {
    // Regression test for https://github.com/flutter/flutter/issues/24029.

    final RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();
    layout(repaintBoundary, phase: EnginePhase.flushSemantics);

    repaintBoundary.markNeedsCompositingBitsUpdate();

44
    TestRenderingFlutterBinding.instance.renderView.child = null;
45 46 47
    final RenderPadding padding = RenderPadding(
      padding: const EdgeInsets.all(50),
    );
48
    TestRenderingFlutterBinding.instance.renderView.child = padding;
49 50 51
    padding.child = repaintBoundary;
    pumpFrame(phase: EnginePhase.flushSemantics);
  });
52 53 54 55 56 57 58

  test('Framework creates an OffsetLayer for a repaint boundary', () {
    final _TestRepaintBoundary repaintBoundary = _TestRepaintBoundary();
    final RenderOpacity opacity = RenderOpacity(
      child: repaintBoundary,
    );
    layout(opacity, phase: EnginePhase.flushSemantics);
Dan Field's avatar
Dan Field committed
59
    expect(repaintBoundary.debugLayer, isA<OffsetLayer>());
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  });

  test('Framework does not create an OffsetLayer for a non-repaint boundary', () {
    final _TestNonCompositedBox nonCompositedBox = _TestNonCompositedBox();
    final RenderOpacity opacity = RenderOpacity(
      child: nonCompositedBox,
    );
    layout(opacity, phase: EnginePhase.flushSemantics);
    expect(nonCompositedBox.debugLayer, null);
  });

  test('Framework allows a non-repaint boundary to create own layer', () {
    final _TestCompositedBox compositedBox = _TestCompositedBox();
    final RenderOpacity opacity = RenderOpacity(
      child: compositedBox,
    );
    layout(opacity, phase: EnginePhase.flushSemantics);
Dan Field's avatar
Dan Field committed
77
    expect(compositedBox.debugLayer, isA<OpacityLayer>());
78 79 80 81 82 83 84 85
  });

  test('Framework ensures repaint boundary layer is not overwritten', () {
    final _TestRepaintBoundaryThatOverwritesItsLayer faultyRenderObject = _TestRepaintBoundaryThatOverwritesItsLayer();
    final RenderOpacity opacity = RenderOpacity(
      child: faultyRenderObject,
    );

86
    late FlutterErrorDetails error;
87
    layout(opacity, phase: EnginePhase.flushSemantics, onErrors: () {
88
      error = TestRenderingFlutterBinding.instance.takeFlutterErrorDetails()!;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    });
    expect('${error.exception}', contains('Attempted to set a layer to a repaint boundary render object.'));
  });
}

// A plain render object that's a repaint boundary.
class _TestRepaintBoundary extends RenderBox {
  @override
  bool get isRepaintBoundary => true;

  @override
  void performLayout() {
    size = constraints.smallest;
  }
}

// A render object that's a repaint boundary and (incorrectly) creates its own layer.
class _TestRepaintBoundaryThatOverwritesItsLayer extends RenderBox {
  @override
  bool get isRepaintBoundary => true;

  @override
  void performLayout() {
    size = constraints.smallest;
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    layer = OpacityLayer(alpha: 50);
  }
}

// A render object that's neither a repaint boundary nor creates its own layer.
class _TestNonCompositedBox extends RenderBox {
  @override
  bool get isRepaintBoundary => false;

  @override
  void performLayout() {
    size = constraints.smallest;
  }
}

// A render object that's not a repaint boundary but creates its own layer.
class _TestCompositedBox extends RenderBox {
  @override
  bool get isRepaintBoundary => false;

  @override
  void performLayout() {
    size = constraints.smallest;
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    layer = OpacityLayer(alpha: 50);
  }
146
}