reattach_test.dart 6.86 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 11

import 'rendering_tester.dart';

class TestTree {
  TestTree() {
12
    // incoming constraints are tight 800x600
13 14
    root = RenderPositionedBox(
      child: RenderConstrainedBox(
15 16 17 18 19
        additionalConstraints: const BoxConstraints.tightFor(width: 800.0),
        // Place the child to be evaluated within both a repaint boundary and a
        // layout-root element (in this case a tightly constrained box). Otherwise
        // the act of transplanting the root into a new container will cause the
        // relayout/repaint of the new parent node to satisfy the test.
20 21
        child: RenderRepaintBoundary(
          child: RenderConstrainedBox(
22
            additionalConstraints: const BoxConstraints.tightFor(height: 20.0, width: 20.0),
23 24 25
            child: RenderRepaintBoundary(
              child: RenderCustomPaint(
                painter: TestCallbackPainter(
26 27
                  onPaint: () { painted = true; },
                ),
28 29
                child: RenderPositionedBox(
                  child: child = RenderConstrainedBox(
30
                    additionalConstraints: const BoxConstraints.tightFor(height: 20.0, width: 20.0),
31
                    child: RenderSemanticsAnnotations(attributedLabel: AttributedString('Hello there foo'), textDirection: TextDirection.ltr),
32 33
                  ),
                ),
34
              ),
35 36 37 38
            ),
          ),
        ),
      ),
39 40
    );
  }
41 42
  late RenderBox root;
  late RenderConstrainedBox child;
43 44 45 46
  bool painted = false;
}

class MutableCompositor extends RenderProxyBox {
47
  MutableCompositor({ required RenderBox child }) : super(child);
48 49 50 51 52 53 54
  bool _alwaysComposite = false;
  @override
  bool get alwaysNeedsCompositing => _alwaysComposite;
}

class TestCompositingBitsTree {
  TestCompositingBitsTree() {
55
    // incoming constraints are tight 800x600
56 57
    root = RenderPositionedBox(
      child: RenderConstrainedBox(
58 59 60 61
        additionalConstraints: const BoxConstraints.tightFor(width: 800.0),
        // Place the child to be evaluated within a repaint boundary. Otherwise
        // the act of transplanting the root into a new container will cause the
        // repaint of the new parent node to satisfy the test.
62 63 64 65
        child: RenderRepaintBoundary(
          child: compositor = MutableCompositor(
            child: RenderCustomPaint(
              painter: TestCallbackPainter(
66 67
                onPaint: () { painted = true; },
              ),
68
              child: child = RenderConstrainedBox(
69
                additionalConstraints: const BoxConstraints.tightFor(height: 20.0, width: 20.0),
70
              ),
71
            ),
72 73 74
          ),
        ),
      ),
75 76
    );
  }
77 78 79
  late RenderBox root;
  late MutableCompositor compositor;
  late RenderConstrainedBox child;
80 81 82 83 84
  bool painted = false;
}

void main() {
  test('objects can be detached and re-attached: layout', () {
85
    final TestTree testTree = TestTree();
86 87 88 89 90 91 92 93
    // Lay out
    layout(testTree.root, phase: EnginePhase.layout);
    expect(testTree.child.size, equals(const Size(20.0, 20.0)));
    // Remove testTree from the custom render view
    renderer.renderView.child = null;
    expect(testTree.child.owner, isNull);
    // Dirty one of the elements
    testTree.child.additionalConstraints =
94
      const BoxConstraints.tightFor(height: 5.0, width: 5.0);
95 96 97 98 99
    // Lay out again
    layout(testTree.root, phase: EnginePhase.layout);
    expect(testTree.child.size, equals(const Size(5.0, 5.0)));
  });
  test('objects can be detached and re-attached: compositingBits', () {
100
    final TestCompositingBitsTree testTree = TestCompositingBitsTree();
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    // Lay out, composite, and paint
    layout(testTree.root, phase: EnginePhase.paint);
    expect(testTree.painted, isTrue);
    // Remove testTree from the custom render view
    renderer.renderView.child = null;
    expect(testTree.child.owner, isNull);
    // Dirty one of the elements
    testTree.compositor._alwaysComposite = true;
    testTree.child.markNeedsCompositingBitsUpdate();
    testTree.painted = false;
    // Lay out, composite, and paint again
    layout(testTree.root, phase: EnginePhase.paint);
    expect(testTree.painted, isTrue);
  });
  test('objects can be detached and re-attached: paint', () {
116
    final TestTree testTree = TestTree();
117 118 119 120 121 122 123 124 125 126 127 128 129
    // Lay out, composite, and paint
    layout(testTree.root, phase: EnginePhase.paint);
    expect(testTree.painted, isTrue);
    // Remove testTree from the custom render view
    renderer.renderView.child = null;
    expect(testTree.child.owner, isNull);
    // Dirty one of the elements
    testTree.child.markNeedsPaint();
    testTree.painted = false;
    // Lay out, composite, and paint again
    layout(testTree.root, phase: EnginePhase.paint);
    expect(testTree.painted, isTrue);
  });
130
  test('objects can be detached and re-attached: semantics (no change)', () {
131
    final TestTree testTree = TestTree();
132
    int semanticsUpdateCount = 0;
133
    final SemanticsHandle semanticsHandle = renderer.pipelineOwner.ensureSemantics(
134 135
      listener: () {
        ++semanticsUpdateCount;
136
      },
137
    );
138
    // Lay out, composite, paint, and update semantics
139
    layout(testTree.root, phase: EnginePhase.flushSemantics);
140
    expect(semanticsUpdateCount, 1);
141 142 143 144
    // Remove testTree from the custom render view
    renderer.renderView.child = null;
    expect(testTree.child.owner, isNull);
    // Dirty one of the elements
145
    semanticsUpdateCount = 0;
146
    testTree.child.markNeedsSemanticsUpdate();
147
    expect(semanticsUpdateCount, 0);
148
    // Lay out, composite, paint, and update semantics again
149
    layout(testTree.root, phase: EnginePhase.flushSemantics);
150
    expect(semanticsUpdateCount, 0); // no semantics have changed.
151 152 153
    semanticsHandle.dispose();
  });
  test('objects can be detached and re-attached: semantics (with change)', () {
154
    final TestTree testTree = TestTree();
155 156
    int semanticsUpdateCount = 0;
    final SemanticsHandle semanticsHandle = renderer.pipelineOwner.ensureSemantics(
157 158 159
      listener: () {
        ++semanticsUpdateCount;
      },
160 161 162
    );
    // Lay out, composite, paint, and update semantics
    layout(testTree.root, phase: EnginePhase.flushSemantics);
163
    expect(semanticsUpdateCount, 1);
164 165 166 167 168 169 170 171 172 173
    // Remove testTree from the custom render view
    renderer.renderView.child = null;
    expect(testTree.child.owner, isNull);
    // Dirty one of the elements
    semanticsUpdateCount = 0;
    testTree.child.additionalConstraints = const BoxConstraints.tightFor(height: 20.0, width: 30.0);
    testTree.child.markNeedsSemanticsUpdate();
    expect(semanticsUpdateCount, 0);
    // Lay out, composite, paint, and update semantics again
    layout(testTree.root, phase: EnginePhase.flushSemantics);
174
    expect(semanticsUpdateCount, 1); // semantics have changed.
175
    semanticsHandle.dispose();
176 177
  });
}