reattach_test.dart 6.85 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 6
// @dart = 2.8

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

import 'rendering_tester.dart';

class TestTree {
  TestTree() {
15
    // incoming constraints are tight 800x600
16 17
    root = RenderPositionedBox(
      child: RenderConstrainedBox(
18 19 20 21 22
        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.
23 24
        child: RenderRepaintBoundary(
          child: RenderConstrainedBox(
25
            additionalConstraints: const BoxConstraints.tightFor(height: 20.0, width: 20.0),
26 27 28
            child: RenderRepaintBoundary(
              child: RenderCustomPaint(
                painter: TestCallbackPainter(
29 30
                  onPaint: () { painted = true; },
                ),
31 32
                child: RenderPositionedBox(
                  child: child = RenderConstrainedBox(
33
                    additionalConstraints: const BoxConstraints.tightFor(height: 20.0, width: 20.0),
34
                    child: RenderSemanticsAnnotations(label: 'Hello there foo', textDirection: TextDirection.ltr),
35 36
                  ),
                ),
37
              ),
38 39 40 41
            ),
          ),
        ),
      ),
42 43
    );
  }
44
  RenderBox root;
45 46 47 48 49 50 51 52 53 54 55 56 57
  RenderConstrainedBox child;
  bool painted = false;
}

class MutableCompositor extends RenderProxyBox {
  MutableCompositor({ RenderBox child }) : super(child);
  bool _alwaysComposite = false;
  @override
  bool get alwaysNeedsCompositing => _alwaysComposite;
}

class TestCompositingBitsTree {
  TestCompositingBitsTree() {
58
    // incoming constraints are tight 800x600
59 60
    root = RenderPositionedBox(
      child: RenderConstrainedBox(
61 62 63 64
        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.
65 66 67 68
        child: RenderRepaintBoundary(
          child: compositor = MutableCompositor(
            child: RenderCustomPaint(
              painter: TestCallbackPainter(
69 70
                onPaint: () { painted = true; },
              ),
71
              child: child = RenderConstrainedBox(
72 73
                additionalConstraints: const BoxConstraints.tightFor(height: 20.0, width: 20.0)
              ),
74
            ),
75 76 77
          ),
        ),
      ),
78 79
    );
  }
80
  RenderBox root;
81 82 83 84 85 86 87
  MutableCompositor compositor;
  RenderConstrainedBox child;
  bool painted = false;
}

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