debug_test.dart 6.63 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:vector_math/vector_math_64.dart';
9

10
import 'mock_canvas.dart';
11
import 'rendering_tester.dart';
12

13
void main() {
14
  test('Describe transform control test', () {
15
    final Matrix4 identity = Matrix4.identity();
16
    final List<String> description = debugDescribeTransform(identity);
17
    expect(description, <String>[
18 19 20 21
      '[0] 1.0,0.0,0.0,0.0',
      '[1] 0.0,1.0,0.0,0.0',
      '[2] 0.0,0.0,1.0,0.0',
      '[3] 0.0,0.0,0.0,1.0',
22
    ]);
23
  });
24

25
  test('transform property test', () {
26 27
    final Matrix4 transform = Matrix4.diagonal3(Vector3.all(2.0));
    final TransformProperty simple = TransformProperty(
28 29 30 31
      'transform',
      transform,
    );
    expect(simple.name, equals('transform'));
32
    expect(simple.value, same(transform));
33
    expect(
34
      simple.toString(parentConfiguration: sparseTextConfiguration),
35 36
      equals(
        'transform:\n'
37 38 39 40
        '  [0] 2.0,0.0,0.0,0.0\n'
        '  [1] 0.0,2.0,0.0,0.0\n'
        '  [2] 0.0,0.0,2.0,0.0\n'
        '  [3] 0.0,0.0,0.0,1.0',
41 42
      ),
    );
43 44 45 46
    expect(
      simple.toString(parentConfiguration: singleLineTextConfiguration),
      equals('transform: [2.0,0.0,0.0,0.0; 0.0,2.0,0.0,0.0; 0.0,0.0,2.0,0.0; 0.0,0.0,0.0,1.0]'),
    );
47

48
    final TransformProperty nullProperty = TransformProperty(
49 50 51 52
      'transform',
      null,
    );
    expect(nullProperty.name, equals('transform'));
53
    expect(nullProperty.value, isNull);
54 55
    expect(nullProperty.toString(), equals('transform: null'));

56
    final TransformProperty hideNull = TransformProperty(
57 58 59 60
      'transform',
      null,
      defaultValue: null,
    );
61
    expect(hideNull.value, isNull);
62 63 64
    expect(hideNull.toString(), equals('transform: null'));
  });

65 66
  test('debugPaintPadding', () {
    expect((Canvas canvas) {
Dan Field's avatar
Dan Field committed
67
      debugPaintPadding(canvas, const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), null);
68
    }, paints..rect(color: const Color(0x90909090)));
69
    expect((Canvas canvas) {
Dan Field's avatar
Dan Field committed
70
      debugPaintPadding(canvas, const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), const Rect.fromLTRB(11.0, 11.0, 19.0, 19.0));
71
    }, paints..path(color: const Color(0x900090FF))..path(color: const Color(0xFF0090FF)));
72
    expect((Canvas canvas) {
Dan Field's avatar
Dan Field committed
73 74
      debugPaintPadding(canvas, const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), const Rect.fromLTRB(15.0, 15.0, 15.0, 15.0));
    }, paints..rect(rect: const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), color: const Color(0x90909090)));
75
  });
Ian Hickson's avatar
Ian Hickson committed
76 77 78 79 80

  test('debugPaintPadding from render objects', () {
    debugPaintSizeEnabled = true;
    RenderSliver s;
    RenderBox b;
81
    final RenderViewport root = RenderViewport(
82
      crossAxisDirection: AxisDirection.right,
83
      offset: ViewportOffset.zero(),
Ian Hickson's avatar
Ian Hickson committed
84
      children: <RenderSliver>[
85
        s = RenderSliverPadding(
86
          padding: const EdgeInsets.all(10.0),
87 88
          child: RenderSliverToBoxAdapter(
            child: b = RenderPadding(
89
              padding: const EdgeInsets.all(10.0),
Ian Hickson's avatar
Ian Hickson committed
90 91 92 93 94 95
            ),
          ),
        ),
      ],
    );
    layout(root);
96
    expect(b.debugPaint, paints..rect(color: const Color(0xFF00FFFF))..rect(color: const Color(0x90909090)));
Ian Hickson's avatar
Ian Hickson committed
97
    expect(b.debugPaint, isNot(paints..path()));
98 99 100 101 102 103 104 105 106 107
    expect(
      s.debugPaint,
      paints
        ..circle(hasMaskFilter: true)
        ..line(hasMaskFilter: true)
        ..path(hasMaskFilter: true)
        ..path(hasMaskFilter: true)
        ..path(color: const Color(0x900090FF))
        ..path(color: const Color(0xFF0090FF)),
    );
Ian Hickson's avatar
Ian Hickson committed
108 109 110 111 112 113 114
    expect(s.debugPaint, isNot(paints..rect()));
    debugPaintSizeEnabled = false;
  });

  test('debugPaintPadding from render objects', () {
    debugPaintSizeEnabled = true;
    RenderSliver s;
115
    final RenderBox b = RenderPadding(
116
      padding: const EdgeInsets.all(10.0),
117
      child: RenderViewport(
118
        crossAxisDirection: AxisDirection.right,
119
        offset: ViewportOffset.zero(),
Ian Hickson's avatar
Ian Hickson committed
120
        children: <RenderSliver>[
121
          s = RenderSliverPadding(
122
            padding: const EdgeInsets.all(10.0),
Ian Hickson's avatar
Ian Hickson committed
123 124 125 126 127
          ),
        ],
      ),
    );
    layout(b);
128
    expect(s.debugPaint, paints..rect(color: const Color(0x90909090)));
129 130 131 132 133 134 135 136 137 138 139 140
    expect(
      s.debugPaint,
      isNot(
        paints
          ..circle(hasMaskFilter: true)
          ..line(hasMaskFilter: true)
          ..path(hasMaskFilter: true)
          ..path(hasMaskFilter: true)
          ..path(color: const Color(0x900090FF))
          ..path(color: const Color(0xFF0090FF)),
      ),
    );
141 142
    expect(b.debugPaint, paints..rect(color: const Color(0xFF00FFFF))..path(color: const Color(0x900090FF))..path(color: const Color(0xFF0090FF)));
    expect(b.debugPaint, isNot(paints..rect(color: const Color(0x90909090))));
Ian Hickson's avatar
Ian Hickson committed
143 144
    debugPaintSizeEnabled = false;
  });
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

  test('debugPaintPadding from render objects with inverted direction vertical', () {
    debugPaintSizeEnabled = true;
    RenderSliver s;
    final RenderViewport root = RenderViewport(
      axisDirection: AxisDirection.up,
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      children: <RenderSliver>[
        s = RenderSliverPadding(
          padding: const EdgeInsets.all(10.0),
          child: RenderSliverToBoxAdapter(
            child: RenderPadding(
              padding: const EdgeInsets.all(10.0),
            ),
          ),
        ),
      ],
    );
    layout(root);
    dynamic error;
    try {
      s.debugPaint(
168
        PaintingContext(ContainerLayer(), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0)),
169
        const Offset(0.0, 500),
170
      );
171
    } catch (e) {
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      error = e;
    }
    expect(error, isNull);
    debugPaintSizeEnabled = false;
  });

  test('debugPaintPadding from render objects with inverted direction horizontal', () {
    debugPaintSizeEnabled = true;
    RenderSliver s;
    final RenderViewport root = RenderViewport(
      axisDirection: AxisDirection.left,
      crossAxisDirection: AxisDirection.down,
      offset: ViewportOffset.zero(),
      children: <RenderSliver>[
        s = RenderSliverPadding(
          padding: const EdgeInsets.all(10.0),
          child: RenderSliverToBoxAdapter(
            child: RenderPadding(
              padding: const EdgeInsets.all(10.0),
            ),
          ),
        ),
      ],
    );
    layout(root);
    dynamic error;
    try {
      s.debugPaint(
200
        PaintingContext(ContainerLayer(), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0)),
201
        const Offset(0.0, 500),
202
      );
203
    } catch (e) {
204 205 206 207 208
      error = e;
    }
    expect(error, isNull);
    debugPaintSizeEnabled = false;
  });
209
}