box_test.dart 4.17 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
import 'package:flutter_test/flutter_test.dart';
8

9
import 'rendering_tester.dart';
10 11 12

void main() {
  test("should size to render view", () {
13
    final RenderBox root = new RenderDecoratedBox(
14
      decoration: new BoxDecoration(
15
        color: const Color(0xFF00FF00),
16
        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
        color: 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
        color: const Color(0xFF0000FF),
50
      ),
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 73 74 75 76 77 78 79 80 81 82

    expect(coloredBox, hasAGoodToStringDeep);
    expect(coloredBox.toStringDeep(), equalsIgnoringHashCodes(
        'RenderDecoratedBox#00000 NEEDS-LAYOUT NEEDS-PAINT DETACHED\n'
        '   parentData: null\n'
        '   constraints: null\n'
        '   size: MISSING\n'
        '   decoration:\n'
        '     <no decorations specified>\n'
        '   configuration: ImageConfiguration()\n'));

83
    final RenderBox paddingBox = new RenderPadding(
84 85
      padding: const EdgeInsets.all(10.0),
      child: coloredBox,
86
    );
87
    final RenderBox root = new RenderDecoratedBox(
88
      decoration: const BoxDecoration(),
89
      child: paddingBox,
90 91 92 93
    );
    layout(root);
    expect(coloredBox.size.width, equals(780.0));
    expect(coloredBox.size.height, equals(580.0));
94 95 96 97 98 99 100 101 102 103 104 105 106 107

    expect(coloredBox, hasAGoodToStringDeep);
    expect(
      coloredBox.toStringDeep(),
      equalsIgnoringHashCodes(
        'RenderDecoratedBox#00000 NEEDS-PAINT\n'
        '   parentData: offset=Offset(10.0, 10.0) (can use size)\n'
        '   constraints: BoxConstraints(w=780.0, h=580.0)\n'
        '   size: Size(780.0, 580.0)\n'
        '   decoration:\n'
        '     <no decorations specified>\n'
        '   configuration: ImageConfiguration()\n',
      ),
    );
108
  });
109 110

  test("reparenting should clear position", () {
111
    final RenderDecoratedBox coloredBox = new RenderDecoratedBox(
112
      decoration: const BoxDecoration(),
113
    );
114

115
    final RenderPadding paddedBox = new RenderPadding(
116 117 118
      child: coloredBox,
      padding: const EdgeInsets.all(10.0),
    );
119
    layout(paddedBox);
120
    final BoxParentData parentData = coloredBox.parentData;
121
    expect(parentData.offset.dx, isNot(equals(0.0)));
122 123
    paddedBox.child = null;

124
    final RenderConstrainedBox constraintedBox = new RenderConstrainedBox(
125 126 127
      child: coloredBox,
      additionalConstraints: const BoxConstraints(),
    );
128
    layout(constraintedBox);
129
    expect(coloredBox.parentData?.runtimeType, ParentData);
130
  });
131
}