box_test.dart 3.32 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 6
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
7 8
import 'package:test/test.dart';

9
import 'rendering_tester.dart';
10 11 12

void main() {
  test("should size to render view", () {
13
    final RenderBox root = new RenderDecoratedBox(
14 15 16
      decoration: new BoxDecoration(
        backgroundColor: const Color(0xFF00FF00),
        gradient: new RadialGradient(
17
          center: FractionalOffset.topLeft, radius: 1.8,
18 19 20 21
          colors: <Color>[Colors.yellow[500], Colors.blue[500]],
        ),
        boxShadow: kElevationToShadow[3],
      ),
22 23 24 25 26 27 28
    );
    layout(root);
    expect(root.size.width, equals(800.0));
    expect(root.size.height, equals(600.0));
  });

  test('Flex and padding', () {
29
    final RenderBox size = new RenderConstrainedBox(
30
      additionalConstraints: const BoxConstraints().tighten(height: 100.0),
31
    );
32
    final RenderBox inner = new RenderDecoratedBox(
33
      decoration: const BoxDecoration(
34
        backgroundColor: const Color(0xFF00FF00),
35
      ),
36
      child: size,
37
    );
38
    final RenderBox padding = new RenderPadding(
39
      padding: const EdgeInsets.all(50.0),
40
      child: inner,
41
    );
42
    final RenderBox flex = new RenderFlex(
43
      children: <RenderBox>[padding],
44
      direction: Axis.vertical,
45
      crossAxisAlignment: CrossAxisAlignment.stretch,
46
    );
47
    final RenderBox outer = new RenderDecoratedBox(
48
      decoration: const BoxDecoration(
49 50
        backgroundColor: const Color(0xFF0000FF)
      ),
51
      child: flex,
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    );

    layout(outer);

    expect(size.size.width, equals(700.0));
    expect(size.size.height, equals(100.0));
    expect(inner.size.width, equals(700.0));
    expect(inner.size.height, equals(100.0));
    expect(padding.size.width, equals(800.0));
    expect(padding.size.height, equals(200.0));
    expect(flex.size.width, equals(800.0));
    expect(flex.size.height, equals(600.0));
    expect(outer.size.width, equals(800.0));
    expect(outer.size.height, equals(600.0));
  });

  test("should not have a 0 sized colored Box", () {
69
    final RenderBox coloredBox = new RenderDecoratedBox(
70
      decoration: const BoxDecoration(),
71
    );
72
    final RenderBox paddingBox = new RenderPadding(
73 74
      padding: const EdgeInsets.all(10.0),
      child: coloredBox,
75
    );
76
    final RenderBox root = new RenderDecoratedBox(
77
      decoration: const BoxDecoration(),
78
      child: paddingBox,
79 80 81 82 83
    );
    layout(root);
    expect(coloredBox.size.width, equals(780.0));
    expect(coloredBox.size.height, equals(580.0));
  });
84 85

  test("reparenting should clear position", () {
86
    final RenderDecoratedBox coloredBox = new RenderDecoratedBox(
87
      decoration: const BoxDecoration(),
88
    );
89

90
    final RenderPadding paddedBox = new RenderPadding(
91 92 93
      child: coloredBox,
      padding: const EdgeInsets.all(10.0),
    );
94
    layout(paddedBox);
95
    final BoxParentData parentData = coloredBox.parentData;
96
    expect(parentData.offset.dx, isNot(equals(0.0)));
97 98
    paddedBox.child = null;

99
    final RenderConstrainedBox constraintedBox = new RenderConstrainedBox(
100 101 102
      child: coloredBox,
      additionalConstraints: const BoxConstraints(),
    );
103
    layout(constraintedBox);
104
    expect(coloredBox.parentData?.runtimeType, ParentData);
105
  });
106
}