debug_test.dart 8.67 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 15
  TestRenderingFlutterBinding.ensureInitialized();

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
    expect(b.debugPaint, isNot(paints..path()));
100 101 102 103 104 105 106 107 108 109
    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
110 111 112 113 114 115 116
    expect(s.debugPaint, isNot(paints..rect()));
    debugPaintSizeEnabled = false;
  });

  test('debugPaintPadding from render objects', () {
    debugPaintSizeEnabled = true;
    RenderSliver s;
117
    final RenderBox b = RenderPadding(
118
      padding: const EdgeInsets.all(10.0),
119
      child: RenderViewport(
120
        crossAxisDirection: AxisDirection.right,
121
        offset: ViewportOffset.zero(),
Ian Hickson's avatar
Ian Hickson committed
122
        children: <RenderSliver>[
123
          s = RenderSliverPadding(
124
            padding: const EdgeInsets.all(10.0),
Ian Hickson's avatar
Ian Hickson committed
125 126 127 128 129
          ),
        ],
      ),
    );
    layout(b);
130
    expect(s.debugPaint, paints..rect(color: const Color(0x90909090)));
131 132 133 134 135 136 137 138 139 140 141 142
    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)),
      ),
    );
143 144
    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
145 146
    debugPaintSizeEnabled = false;
  });
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;
168
    final PaintingContext context = PaintingContext(ContainerLayer(), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0));
169 170
    try {
      s.debugPaint(
171
        context,
172
        const Offset(0.0, 500),
173
      );
174
    } catch (e) {
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 200
      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;
201
    final PaintingContext context = PaintingContext(ContainerLayer(), const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0));
202 203
    try {
      s.debugPaint(
204
        context,
205
        const Offset(0.0, 500),
206
      );
207
    } catch (e) {
208 209 210 211 212
      error = e;
    }
    expect(error, isNull);
    debugPaintSizeEnabled = false;
  });
Dan Field's avatar
Dan Field committed
213 214 215 216 217 218 219 220 221 222 223 224

  test('debugDisableOpacity keeps things in the right spot', () {
    debugDisableOpacityLayers = true;

    final RenderDecoratedBox blackBox = RenderDecoratedBox(
      decoration: const BoxDecoration(color: Color(0xff000000)),
      child: RenderConstrainedBox(
        additionalConstraints: BoxConstraints.tight(const Size.square(20.0)),
      ),
    );
    final RenderOpacity root = RenderOpacity(
      opacity: .5,
225
      child: RenderRepaintBoundary(child: blackBox),
Dan Field's avatar
Dan Field committed
226 227 228
    );
    layout(root, phase: EnginePhase.compositingBits);

229
    final OffsetLayer rootLayer = OffsetLayer();
Dan Field's avatar
Dan Field committed
230 231 232 233
    final PaintingContext context = PaintingContext(
      rootLayer,
      const Rect.fromLTWH(0, 0, 500, 500),
    );
234 235
    context.paintChild(root, const Offset(40, 40));

Dan Field's avatar
Dan Field committed
236 237 238 239
    final OpacityLayer opacityLayer = rootLayer.firstChild! as OpacityLayer;
    expect(opacityLayer.offset, const Offset(40, 40));
    debugDisableOpacityLayers = false;
  });
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

  test('debugAssertAllRenderVarsUnset warns when debugProfileLayoutsEnabled set', () {
    debugProfileLayoutsEnabled = true;
    expect(() => debugAssertAllRenderVarsUnset('ERROR'), throwsFlutterError);
    debugProfileLayoutsEnabled = false;
  });

  test('debugAssertAllRenderVarsUnset warns when debugDisableClipLayers set', () {
    debugDisableClipLayers = true;
    expect(() => debugAssertAllRenderVarsUnset('ERROR'), throwsFlutterError);
    debugDisableClipLayers = false;
  });

  test('debugAssertAllRenderVarsUnset warns when debugDisablePhysicalShapeLayers set', () {
    debugDisablePhysicalShapeLayers = true;
    expect(() => debugAssertAllRenderVarsUnset('ERROR'), throwsFlutterError);
    debugDisablePhysicalShapeLayers = false;
  });

  test('debugAssertAllRenderVarsUnset warns when debugDisableOpacityLayers set', () {
    debugDisableOpacityLayers = true;
    expect(() => debugAssertAllRenderVarsUnset('ERROR'), throwsFlutterError);
    debugDisableOpacityLayers = false;
  });
264
}