block_test.dart 3.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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/material.dart';
import 'package:flutter/rendering.dart';
import 'package:test/test.dart';

import 'rendering_tester.dart';

class TestBlockPainter extends Painter {
  void paint(PaintingContext context, Offset offset) { }
}

void main() {
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  test('block intrinsics', () {
    RenderParagraph paragraph = new RenderParagraph(
      new StyledTextSpan(
        new TextStyle(
          height: 1.0
        ),
        <TextSpan>[new PlainTextSpan('Hello World')]
      )
    );
    const BoxConstraints unconstrained = const BoxConstraints();
    double textWidth = paragraph.getMaxIntrinsicWidth(unconstrained);
    double oneLineTextHeight = paragraph.getMinIntrinsicHeight(unconstrained);
    final BoxConstraints constrained = new BoxConstraints(maxWidth: textWidth * 0.9);
    double wrappedTextWidth = paragraph.getMinIntrinsicWidth(unconstrained);
    double twoLinesTextHeight = paragraph.getMinIntrinsicHeight(constrained);

    // controls
    expect(wrappedTextWidth, lessThan(textWidth));
    expect(paragraph.getMinIntrinsicWidth(unconstrained), equals(wrappedTextWidth));
    expect(paragraph.getMaxIntrinsicWidth(constrained), equals(constrained.maxWidth));

    expect(oneLineTextHeight, lessThan(twoLinesTextHeight));
    expect(twoLinesTextHeight, lessThan(oneLineTextHeight * 3.0));
    expect(paragraph.getMaxIntrinsicHeight(unconstrained), equals(oneLineTextHeight));
    expect(paragraph.getMaxIntrinsicHeight(constrained), equals(twoLinesTextHeight));

    RenderBox testBlock = new RenderBlock(
      children: <RenderBox>[
        paragraph,
      ]
    );
    expect(testBlock.getMinIntrinsicWidth(unconstrained), equals(wrappedTextWidth));
    expect(testBlock.getMinIntrinsicWidth(constrained), equals(wrappedTextWidth));
    expect(testBlock.getMaxIntrinsicWidth(unconstrained), equals(textWidth));
    expect(testBlock.getMaxIntrinsicWidth(constrained), equals(constrained.maxWidth));
    expect(testBlock.getMinIntrinsicHeight(unconstrained), equals(oneLineTextHeight));
    expect(testBlock.getMinIntrinsicHeight(constrained), equals(twoLinesTextHeight));
    expect(testBlock.getMaxIntrinsicHeight(unconstrained), equals(oneLineTextHeight));
    expect(testBlock.getMaxIntrinsicHeight(constrained), equals(twoLinesTextHeight));

    final BoxConstraints empty = new BoxConstraints.tight(Size.zero);
    expect(testBlock.getMinIntrinsicWidth(empty), equals(0.0));
    expect(testBlock.getMaxIntrinsicWidth(empty), equals(0.0));
    expect(testBlock.getMinIntrinsicHeight(empty), equals(0.0));
    expect(testBlock.getMaxIntrinsicHeight(empty), equals(0.0));
  });


64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  test('overlay painters can attach and detach', () {
    TestBlockPainter first = new TestBlockPainter();
    TestBlockPainter second = new TestBlockPainter();
    RenderBlockViewport block = new RenderBlockViewport(overlayPainter: first);

    // The first painter isn't attached because we haven't attached block.
    expect(first.renderObject, isNull);
    expect(second.renderObject, isNull);

    block.overlayPainter = second;

    expect(first.renderObject, isNull);
    expect(second.renderObject, isNull);

    layout(block);

    expect(first.renderObject, isNull);
    expect(second.renderObject, equals(block));

    block.overlayPainter = first;

    expect(first.renderObject, equals(block));
    expect(second.renderObject, isNull);
  });
}