custom_paint_test.dart 7.7 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/rendering.dart';
8
import 'package:flutter/widgets.dart';
9 10
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
11 12 13 14

class TestCustomPainter extends CustomPainter {
  TestCustomPainter({ this.log, this.name });

15 16
  final List<String> log;
  final String name;
17

18
  @override
19 20 21 22
  void paint(Canvas canvas, Size size) {
    log.add(name);
  }

23
  @override
24 25 26
  bool shouldRepaint(TestCustomPainter oldPainter) => true;
}

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
class TestCustomPainterWithCustomSemanticsBuilder extends TestCustomPainter {
  TestCustomPainterWithCustomSemanticsBuilder() : super(log: <String>[]);

  @override
  SemanticsBuilderCallback get semanticsBuilder => (Size size) {
    const Key key = Key('0');
    const Rect rect = Rect.fromLTRB(0, 0, 0, 0);
    const SemanticsProperties semanticsProperties = SemanticsProperties();
    return <CustomPainterSemantics>[
      const CustomPainterSemantics(key: key, rect: rect, properties: semanticsProperties),
      const CustomPainterSemantics(key: key, rect: rect, properties: semanticsProperties),
    ];
  };
}

class MockCanvas extends Mock implements Canvas {}

class MockPaintingContext extends Mock implements PaintingContext {}

46
void main() {
47
  testWidgets('Control test for custom painting', (WidgetTester tester) async {
48
    final List<String> log = <String>[];
49 50
    await tester.pumpWidget(CustomPaint(
      painter: TestCustomPainter(
51
        log: log,
52
        name: 'background',
53
      ),
54
      foregroundPainter: TestCustomPainter(
55
        log: log,
56
        name: 'foreground',
57
      ),
58 59
      child: CustomPaint(
        painter: TestCustomPainter(
60
          log: log,
61 62 63
          name: 'child',
        ),
      ),
64
    ));
65

66
    expect(log, equals(<String>['background', 'child', 'foreground']));
67
  });
Ian Hickson's avatar
Ian Hickson committed
68

69 70 71 72 73 74 75 76 77
  testWidgets('Throws FlutterError on custom painter incorrect restore/save calls', (
      WidgetTester tester) async {
    final GlobalKey target = GlobalKey();
    final List<String> log = <String>[];
    await tester.pumpWidget(CustomPaint(
      key: target,
      isComplex: true,
      painter: TestCustomPainter(log: log),
    ));
78
    final RenderCustomPaint renderCustom = target.currentContext.findRenderObject() as RenderCustomPaint;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    final Canvas canvas = MockCanvas();
    int saveCount = 0;
    when(canvas.getSaveCount()).thenAnswer((_) => saveCount++);
    final PaintingContext paintingContext = MockPaintingContext();
    when(paintingContext.canvas).thenReturn(canvas);

    FlutterError getError() {
      FlutterError error;
      try {
        renderCustom.paint(paintingContext, const Offset(0, 0));
      } on FlutterError catch (e) {
        error = e;
      }
      return error;
    }

    FlutterError error = getError();
    expect(error.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   The TestCustomPainter#00000() custom painter called canvas.save()\n'
      '   or canvas.saveLayer() at least 1 more time than it called\n'
      '   canvas.restore().\n'
      '   This leaves the canvas in an inconsistent state and will probably\n'
      '   result in a broken display.\n'
      '   You must pair each call to save()/saveLayer() with a later\n'
      '   matching call to restore().\n'
    ));

    when(canvas.getSaveCount()).thenAnswer((_) => saveCount--);
    error = getError();
    expect(error.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   The TestCustomPainter#00000() custom painter called\n'
      '   canvas.restore() 1 more time than it called canvas.save() or\n'
      '   canvas.saveLayer().\n'
      '   This leaves the canvas in an inconsistent state and will result\n'
      '   in a broken display.\n'
      '   You should only call restore() if you first called save() or\n'
      '   saveLayer().\n'
    ));

    when(canvas.getSaveCount()).thenAnswer((_) => saveCount += 2);
    error = getError();
    expect(error.toStringDeep(), contains('2 more times'));

    when(canvas.getSaveCount()).thenAnswer((_) => saveCount -= 2);
    error = getError();
    expect(error.toStringDeep(), contains('2 more times'));
  });

  testWidgets('assembleSemanticsNode throws FlutterError', (WidgetTester tester) async {
    final List<String> log = <String>[];
    final GlobalKey target = GlobalKey();
    await tester.pumpWidget(CustomPaint(
      key: target,
      isComplex: true,
      painter: TestCustomPainter(log: log),
    ));
137 138
    final RenderCustomPaint renderCustom = target.currentContext.findRenderObject() as RenderCustomPaint;
    dynamic error;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    try {
      renderCustom.assembleSemanticsNode(
        null,
        null,
        <SemanticsNode>[SemanticsNode()],
      );
    } on FlutterError catch (e) {
      error = e;
    }
    expect(error, isNotNull);
    expect(error.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   RenderCustomPaint does not have a child widget but received a\n'
      '   non-empty list of child SemanticsNode:\n'
      '   SemanticsNode#1(Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible)\n'
    ));

    await tester.pumpWidget(CustomPaint(
      key: target,
      isComplex: true,
      painter: TestCustomPainterWithCustomSemanticsBuilder(),
    ));
    final dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
    error = exception;
    expect(error.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   Failed to update the list of CustomPainterSemantics:\n'
167
      "   - duplicate key [<'0'>] found at position 1\n"
168 169 170
    ));
  });

Ian Hickson's avatar
Ian Hickson committed
171
  testWidgets('CustomPaint sizing', (WidgetTester tester) async {
172
    final GlobalKey target = GlobalKey();
Ian Hickson's avatar
Ian Hickson committed
173

174
    await tester.pumpWidget(Center(
175
      child: CustomPaint(key: target),
Ian Hickson's avatar
Ian Hickson committed
176 177 178
    ));
    expect(target.currentContext.size, Size.zero);

179
    await tester.pumpWidget(Center(
180
      child: CustomPaint(key: target, child: Container()),
Ian Hickson's avatar
Ian Hickson committed
181 182 183
    ));
    expect(target.currentContext.size, const Size(800.0, 600.0));

184
    await tester.pumpWidget(Center(
185
      child: CustomPaint(key: target, size: const Size(20.0, 20.0)),
Ian Hickson's avatar
Ian Hickson committed
186 187 188
    ));
    expect(target.currentContext.size, const Size(20.0, 20.0));

189
    await tester.pumpWidget(Center(
190
      child: CustomPaint(key: target, size: const Size(2000.0, 100.0)),
Ian Hickson's avatar
Ian Hickson committed
191 192 193
    ));
    expect(target.currentContext.size, const Size(800.0, 100.0));

194
    await tester.pumpWidget(Center(
195
      child: CustomPaint(key: target, size: Size.zero, child: Container()),
Ian Hickson's avatar
Ian Hickson committed
196 197 198
    ));
    expect(target.currentContext.size, const Size(800.0, 600.0));

199
    await tester.pumpWidget(Center(
200
      child: CustomPaint(key: target, child: const SizedBox(height: 0.0, width: 0.0)),
Ian Hickson's avatar
Ian Hickson committed
201 202 203 204
    ));
    expect(target.currentContext.size, Size.zero);

  });
205 206

  testWidgets('Raster cache hints', (WidgetTester tester) async {
207
    final GlobalKey target = GlobalKey();
208 209

    final List<String> log = <String>[];
210
    await tester.pumpWidget(CustomPaint(
211 212
      key: target,
      isComplex: true,
213
      painter: TestCustomPainter(log: log),
214
    ));
215
    RenderCustomPaint renderCustom = target.currentContext.findRenderObject() as RenderCustomPaint;
216 217 218
    expect(renderCustom.isComplex, true);
    expect(renderCustom.willChange, false);

219
    await tester.pumpWidget(CustomPaint(
220 221
      key: target,
      willChange: true,
222
      foregroundPainter: TestCustomPainter(log: log),
223
    ));
224
    renderCustom = target.currentContext.findRenderObject() as RenderCustomPaint;
225 226 227
    expect(renderCustom.isComplex, false);
    expect(renderCustom.willChange, true);
  });
228 229 230 231 232

  test('Raster cache hints cannot be set with null painters', () {
    expect(() => CustomPaint(isComplex: true), throwsAssertionError);
    expect(() => CustomPaint(willChange: true), throwsAssertionError);
  });
233
}