debug_test.dart 6.56 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 6
// @dart = 2.8

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

11
import '../flutter_test_alternative.dart';
12
import 'mock_canvas.dart';
13
import 'rendering_tester.dart';
14

15
void main() {
16
  test('Describe transform control test', () {
17
    final Matrix4 identity = Matrix4.identity();
18
    final List<String> description = debugDescribeTransform(identity);
19
    expect(description, <String>[
20 21 22 23
      '[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',
24
    ]);
25
  });
26

27
  test('transform property test', () {
28 29
    final Matrix4 transform = Matrix4.diagonal3(Vector3.all(2.0));
    final TransformProperty simple = TransformProperty(
30 31 32 33
      'transform',
      transform,
    );
    expect(simple.name, equals('transform'));
34
    expect(simple.value, same(transform));
35
    expect(
36
      simple.toString(parentConfiguration: sparseTextConfiguration),
37 38
      equals(
        'transform:\n'
39 40 41 42
        '  [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',
43 44
      ),
    );
45 46 47 48
    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]'),
    );
49

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

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

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

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

  test('debugPaintPadding from render objects', () {
    debugPaintSizeEnabled = true;
    RenderSliver s;
109
    final RenderBox b = RenderPadding(
110
      padding: const EdgeInsets.all(10.0),
111
      child: RenderViewport(
112
        crossAxisDirection: AxisDirection.right,
113
        offset: ViewportOffset.zero(),
Ian Hickson's avatar
Ian Hickson committed
114
        children: <RenderSliver>[
115
          s = RenderSliverPadding(
116
            padding: const EdgeInsets.all(10.0),
Ian Hickson's avatar
Ian Hickson committed
117 118 119 120 121
          ),
        ],
      ),
    );
    layout(b);
122
    expect(s.debugPaint, paints..rect(color: const Color(0x90909090)));
Ian Hickson's avatar
Ian Hickson committed
123
    expect(s.debugPaint, isNot(paints..circle(hasMaskFilter: true)..line(hasMaskFilter: true)..path(hasMaskFilter: true)..path(hasMaskFilter: true)
124 125 126
                                     ..path(color: const Color(0x900090FF))..path(color: const Color(0xFF0090FF))));
    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
127 128
    debugPaintSizeEnabled = false;
  });
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  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(
        PaintingContext(
          ContainerLayer(), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0)),
154
        const Offset(0.0, 500),
155
      );
156
    } catch (e) {
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
      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(
        PaintingContext(
          ContainerLayer(), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0)),
187
        const Offset(0.0, 500),
188
      );
189
    } catch (e) {
190 191 192 193 194
      error = e;
    }
    expect(error, isNull);
    debugPaintSizeEnabled = false;
  });
195
}