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

5
import 'package:flutter/foundation.dart';
Ian Hickson's avatar
Ian Hickson committed
6
import 'package:flutter/gestures.dart';
7
import 'package:flutter/rendering.dart';
Ian Hickson's avatar
Ian Hickson committed
8 9
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
10

11 12
import 'package:flutter_test/flutter_test.dart' show EnginePhase;
export 'package:flutter_test/flutter_test.dart' show EnginePhase;
13

14
class TestRenderingFlutterBinding extends BindingBase with ServicesBinding, GestureBinding, SchedulerBinding, PaintingBinding, RendererBinding {
Ian Hickson's avatar
Ian Hickson committed
15 16
  EnginePhase phase = EnginePhase.composite;

17
  @override
18
  void drawFrame() {
19
    assert(phase != EnginePhase.build, 'rendering_tester does not support testing the build phase; use flutter_test instead');
20
    pipelineOwner.flushLayout();
Ian Hickson's avatar
Ian Hickson committed
21 22
    if (phase == EnginePhase.layout)
      return;
23
    pipelineOwner.flushCompositingBits();
24 25
    if (phase == EnginePhase.compositingBits)
      return;
26
    pipelineOwner.flushPaint();
Ian Hickson's avatar
Ian Hickson committed
27 28
    if (phase == EnginePhase.paint)
      return;
29
    renderView.compositeFrame();
30 31
    if (phase == EnginePhase.composite)
      return;
32
    pipelineOwner.flushSemantics();
33 34
    if (phase == EnginePhase.flushSemantics)
      return;
35 36
    assert(phase == EnginePhase.flushSemantics ||
           phase == EnginePhase.sendSemanticsUpdate);
Ian Hickson's avatar
Ian Hickson committed
37 38 39 40
  }
}

TestRenderingFlutterBinding _renderer;
41 42 43 44
TestRenderingFlutterBinding get renderer {
  _renderer ??= new TestRenderingFlutterBinding();
  return _renderer;
}
45

46 47
/// Place the box in the render tree, at the given size and with the given
/// alignment on the screen.
48 49 50 51 52 53 54
///
/// If you've updated `box` and want to lay it out again, use [pumpFrame].
///
/// Once a particular [RenderBox] has been passed to [layout], it cannot easily
/// be put in a different place in the tree or passed to [layout] again, because
/// [layout] places the given object into another [RenderBox] which you would
/// need to unparent it from (but that box isn't itself made available).
55 56 57
///
/// The EnginePhase must not be [EnginePhase.build], since the rendering layer
/// has no build phase.
58 59
void layout(RenderBox box, {
  BoxConstraints constraints,
60
  Alignment alignment: Alignment.center,
61
  EnginePhase phase: EnginePhase.layout,
62
}) {
63 64
  assert(box != null); // If you want to just repump the last box, call pumpFrame().
  assert(box.parent == null); // We stick the box in another, so you can't reuse it easily, sorry.
Hixie's avatar
Hixie committed
65

66
  renderer.renderView.child = null;
67 68
  if (constraints != null) {
    box = new RenderPositionedBox(
69
      alignment: alignment,
70 71 72 73 74 75
      child: new RenderConstrainedBox(
        additionalConstraints: constraints,
        child: box
      )
    );
  }
Ian Hickson's avatar
Ian Hickson committed
76
  renderer.renderView.child = box;
Hixie's avatar
Hixie committed
77 78 79 80 81

  pumpFrame(phase: phase);
}

void pumpFrame({ EnginePhase phase: EnginePhase.layout }) {
Ian Hickson's avatar
Ian Hickson committed
82
  assert(renderer != null);
83 84
  assert(renderer.renderView != null);
  assert(renderer.renderView.child != null); // call layout() first!
Ian Hickson's avatar
Ian Hickson committed
85
  renderer.phase = phase;
86
  renderer.drawFrame();
87
}
88 89 90 91 92 93

class TestCallbackPainter extends CustomPainter {
  const TestCallbackPainter({ this.onPaint });

  final VoidCallback onPaint;

94
  @override
95 96 97 98
  void paint(Canvas canvas, Size size) {
    onPaint();
  }

99
  @override
100 101
  bool shouldRepaint(TestCallbackPainter oldPainter) => true;
}
102 103 104 105 106 107 108 109


class RenderSizedBox extends RenderBox {
  RenderSizedBox(this._size);

  final Size _size;

  @override
110
  double computeMinIntrinsicWidth(double height) {
111 112 113 114
    return _size.width;
  }

  @override
115
  double computeMaxIntrinsicWidth(double height) {
116 117 118 119
    return _size.width;
  }

  @override
120
  double computeMinIntrinsicHeight(double width) {
121 122 123 124
    return _size.height;
  }

  @override
125
  double computeMaxIntrinsicHeight(double width) {
126 127 128 129 130 131 132 133
    return _size.height;
  }

  @override
  bool get sizedByParent => true;

  @override
  void performResize() {
134 135
    size = constraints.constrain(_size);
  }
136 137 138

  @override
  void performLayout() { }
139 140

  @override
141
  bool hitTestSelf(Offset position) => true;
142
}