rendering_tester.dart 2.98 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5
// 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.

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

10 11
enum EnginePhase {
  layout,
12
  compositingBits,
13
  paint,
14
  composite,
15
  flushSemantics
16 17
}

18
class TestRenderingFlutterBinding extends BindingBase with SchedulerBinding, ServicesBinding, RendererBinding, GestureBinding {
Ian Hickson's avatar
Ian Hickson committed
19 20
  EnginePhase phase = EnginePhase.composite;

21
  @override
Ian Hickson's avatar
Ian Hickson committed
22
  void beginFrame() {
23
    pipelineOwner.flushLayout();
Ian Hickson's avatar
Ian Hickson committed
24 25
    if (phase == EnginePhase.layout)
      return;
26
    pipelineOwner.flushCompositingBits();
27 28
    if (phase == EnginePhase.compositingBits)
      return;
29
    pipelineOwner.flushPaint();
Ian Hickson's avatar
Ian Hickson committed
30 31
    if (phase == EnginePhase.paint)
      return;
32
    renderView.compositeFrame();
33 34
    if (phase == EnginePhase.composite)
      return;
35 36
    pipelineOwner.flushSemantics();
    assert(phase == EnginePhase.flushSemantics);
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 48 49 50 51 52
/// Place the box in the render tree, at the given size and with the given
/// alignment on the screen.
void layout(RenderBox box, {
  BoxConstraints constraints,
  FractionalOffset alignment: FractionalOffset.center,
  EnginePhase phase: EnginePhase.layout
}) {
53 54
  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
55

56
  renderer.renderView.child = null;
57 58
  if (constraints != null) {
    box = new RenderPositionedBox(
59
      alignment: alignment,
60 61 62 63 64 65
      child: new RenderConstrainedBox(
        additionalConstraints: constraints,
        child: box
      )
    );
  }
Ian Hickson's avatar
Ian Hickson committed
66
  renderer.renderView.child = box;
Hixie's avatar
Hixie committed
67 68 69 70 71

  pumpFrame(phase: phase);
}

void pumpFrame({ EnginePhase phase: EnginePhase.layout }) {
Ian Hickson's avatar
Ian Hickson committed
72 73 74
  assert(renderer != null);
  renderer.phase = phase;
  renderer.beginFrame();
75
}
76 77 78 79 80 81

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

  final VoidCallback onPaint;

82
  @override
83 84 85 86
  void paint(Canvas canvas, Size size) {
    onPaint();
  }

87
  @override
88 89
  bool shouldRepaint(TestCallbackPainter oldPainter) => true;
}
90 91 92 93 94 95 96 97


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

  final Size _size;

  @override
98
  double computeMinIntrinsicWidth(double height) {
99 100 101 102
    return _size.width;
  }

  @override
103
  double computeMaxIntrinsicWidth(double height) {
104 105 106 107
    return _size.width;
  }

  @override
108
  double computeMinIntrinsicHeight(double width) {
109 110 111 112
    return _size.height;
  }

  @override
113
  double computeMaxIntrinsicHeight(double width) {
114 115 116 117 118 119 120 121
    return _size.height;
  }

  @override
  bool get sizedByParent => true;

  @override
  void performResize() {
122 123
    size = constraints.constrain(_size);
  }
124 125 126

  @override
  void performLayout() { }
127
}