rendering_tester.dart 2.79 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

Hixie's avatar
Hixie committed
46
void layout(RenderBox box, { BoxConstraints constraints, EnginePhase phase: EnginePhase.layout }) {
47 48
  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
49

50
  renderer.renderView.child = null;
51 52 53 54 55 56 57 58
  if (constraints != null) {
    box = new RenderPositionedBox(
      child: new RenderConstrainedBox(
        additionalConstraints: constraints,
        child: box
      )
    );
  }
Ian Hickson's avatar
Ian Hickson committed
59
  renderer.renderView.child = box;
Hixie's avatar
Hixie committed
60 61 62 63 64

  pumpFrame(phase: phase);
}

void pumpFrame({ EnginePhase phase: EnginePhase.layout }) {
Ian Hickson's avatar
Ian Hickson committed
65 66 67
  assert(renderer != null);
  renderer.phase = phase;
  renderer.beginFrame();
68
}
69 70 71 72 73 74

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

  final VoidCallback onPaint;

75
  @override
76 77 78 79
  void paint(Canvas canvas, Size size) {
    onPaint();
  }

80
  @override
81 82
  bool shouldRepaint(TestCallbackPainter oldPainter) => true;
}
83 84 85 86 87 88 89 90


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

  final Size _size;

  @override
91
  double computeMinIntrinsicWidth(double height) {
92 93 94 95
    return _size.width;
  }

  @override
96
  double computeMaxIntrinsicWidth(double height) {
97 98 99 100
    return _size.width;
  }

  @override
101
  double computeMinIntrinsicHeight(double width) {
102 103 104 105
    return _size.height;
  }

  @override
106
  double computeMaxIntrinsicHeight(double width) {
107 108 109 110 111 112 113 114
    return _size.height;
  }

  @override
  bool get sizedByParent => true;

  @override
  void performResize() {
115 116
    size = constraints.constrain(_size);
  }
117 118 119

  @override
  void performLayout() { }
120
}