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

import 'dart:ui' as ui;

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/material.dart';
9
import 'package:flutter_test/flutter_test.dart';
10 11 12 13

class TestCanvas implements Canvas {
  final List<Invocation> invocations = <Invocation>[];

14
  @override
15 16 17 18 19 20
  void noSuchMethod(Invocation invocation) {
    invocations.add(invocation);
  }
}

void main() {
21 22
  late ui.Image image300x300;
  late ui.Image image300x200;
23

24 25 26 27 28
  setUpAll(() async {
    image300x300 = await createTestImage(width: 300, height: 300, cache: false);
    image300x200 = await createTestImage(width: 300, height: 200, cache: false);
  });

29 30 31 32
  setUp(() {
    debugFlushLastFrameImageSizeInfo();
  });

33
  test('Cover and align', () async {
34
    final TestCanvas canvas = TestCanvas();
35 36
    paintImage(
      canvas: canvas,
Dan Field's avatar
Dan Field committed
37
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
38
      image: image300x300,
39
      fit: BoxFit.cover,
40
      alignment: Alignment.centerLeft,
41 42
    );

43
    final Invocation command = canvas.invocations.firstWhere((Invocation invocation) {
44 45 46 47
      return invocation.memberName == #drawImageRect;
    });

    expect(command, isNotNull);
48
    expect(command.positionalArguments[0], equals(image300x300));
Dan Field's avatar
Dan Field committed
49 50
    expect(command.positionalArguments[1], equals(const Rect.fromLTWH(0.0, 75.0, 300.0, 150.0)));
    expect(command.positionalArguments[2], equals(const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0)));
51
  });
52

53
  test('debugInvertOversizedImages', () async {
54
    debugInvertOversizedImages = true;
55
    expect(PaintingBinding.instance.window.devicePixelRatio != 1.0, true);
56
    final FlutterExceptionHandler? oldFlutterError = FlutterError.onError;
57 58 59 60 61 62 63

    final List<String> messages = <String>[];
    FlutterError.onError = (FlutterErrorDetails details) {
      messages.add(details.exceptionAsString());
    };

    final TestCanvas canvas = TestCanvas();
64
    const Rect rect = Rect.fromLTWH(50.0, 50.0, 100.0, 50.0);
65 66 67 68

    paintImage(
      canvas: canvas,
      rect: rect,
69
      image: image300x300,
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      debugImageLabel: 'TestImage',
      fit: BoxFit.fill,
    );

    final List<Invocation> commands = canvas.invocations
      .skipWhile((Invocation invocation) => invocation.memberName != #saveLayer)
      .take(4)
      .toList();

    expect(commands[0].positionalArguments[0], rect);
    final Paint paint = commands[0].positionalArguments[1] as Paint;
    expect(
      paint.colorFilter,
      const ColorFilter.matrix(<double>[
        -1,  0,  0, 0, 255,
         0, -1,  0, 0, 255,
         0,  0, -1, 0, 255,
         0,  0,  0, 1,   0,
      ]),
    );
    expect(commands[1].memberName, #translate);
    expect(commands[1].positionalArguments[0], 0.0);
92
    expect(commands[1].positionalArguments[1], 75.0);
93 94 95 96 97 98 99 100

    expect(commands[2].memberName, #scale);
    expect(commands[2].positionalArguments[0], 1.0);
    expect(commands[2].positionalArguments[1], -1.0);


    expect(commands[3].memberName, #translate);
    expect(commands[3].positionalArguments[0], 0.0);
101
    expect(commands[3].positionalArguments[1], -75.0);
102 103 104

    expect(
      messages.single,
105 106
      'Image TestImage has a display size of 300×150 but a decode size of 300×300, which uses an additional 234KB.\n\n'
      'Consider resizing the asset ahead of time, supplying a cacheWidth parameter of 300, a cacheHeight parameter of 150, or using a ResizeImage.',
107 108 109 110 111 112
    );

    debugInvertOversizedImages = false;
    FlutterError.onError = oldFlutterError;
  });

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  test('debugInvertOversizedImages smaller than overhead allowance', () async {
    debugInvertOversizedImages = true;
    final FlutterExceptionHandler? oldFlutterError = FlutterError.onError;

    final List<String> messages = <String>[];
    FlutterError.onError = (FlutterErrorDetails details) {
      messages.add(details.exceptionAsString());
    };

    try {
      // Create a 290x290 sized image, which is ~24kb less than the allocated size,
      // and below the default debugImageOverheadAllowance size of 128kb.
      const Rect rect = Rect.fromLTWH(50.0, 50.0, 290.0, 290.0);
      final TestCanvas canvas = TestCanvas();

      paintImage(
        canvas: canvas,
        rect: rect,
        image: image300x300,
        debugImageLabel: 'TestImage',
        fit: BoxFit.fill,
      );

      expect(messages, isEmpty);
    } finally {
      debugInvertOversizedImages = false;
      FlutterError.onError = oldFlutterError;
    }
  });

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  test('centerSlice with scale ≠ 1', () async {
    final TestCanvas canvas = TestCanvas();
    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTRB(10, 20, 430, 420),
      image: image300x300,
      scale: 2.0,
      centerSlice: const Rect.fromLTRB(50, 40, 250, 260),
    );

    final Invocation command = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #drawImageNine;
    });

    expect(command, isNotNull);
    expect(command.positionalArguments[0], equals(image300x300));
    expect(command.positionalArguments[1], equals(const Rect.fromLTRB(100.0, 80.0, 500.0, 520.0)));
    expect(command.positionalArguments[2], equals(const Rect.fromLTRB(20.0, 40.0, 860.0, 840.0)));
  });

163
  testWidgets('Reports Image painting', (WidgetTester tester) async {
164
    late ImageSizeInfo imageSizeInfo;
165 166 167 168 169 170 171 172 173 174
    int count = 0;
    debugOnPaintImage = (ImageSizeInfo info) {
      count += 1;
      imageSizeInfo = info;
    };

    final TestCanvas canvas = TestCanvas();
    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
175
      image: image300x300,
176 177 178 179 180 181 182
      debugImageLabel: 'test.png',
    );

    expect(count, 1);
    expect(imageSizeInfo, isNotNull);
    expect(imageSizeInfo.source, 'test.png');
    expect(imageSizeInfo.imageSize, const Size(300, 300));
183
    expect(imageSizeInfo.displaySize, const Size(200, 100) * PaintingBinding.instance.window.devicePixelRatio);
184 185 186 187 188 189 190 191 192

    // Make sure that we don't report an identical image size info if we
    // redraw in the next frame.
    tester.binding.scheduleForcedFrame();
    await tester.pump();

    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
193
      image: image300x300,
194 195 196 197 198 199 200 201 202
      debugImageLabel: 'test.png',
    );

    expect(count, 1);

    debugOnPaintImage = null;
  });

  testWidgets('Reports Image painting - change per frame', (WidgetTester tester) async {
203
    late ImageSizeInfo imageSizeInfo;
204 205 206 207 208 209 210 211 212 213
    int count = 0;
    debugOnPaintImage = (ImageSizeInfo info) {
      count += 1;
      imageSizeInfo = info;
    };

    final TestCanvas canvas = TestCanvas();
    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
214
      image: image300x300,
215 216 217 218 219 220 221
      debugImageLabel: 'test.png',
    );

    expect(count, 1);
    expect(imageSizeInfo, isNotNull);
    expect(imageSizeInfo.source, 'test.png');
    expect(imageSizeInfo.imageSize, const Size(300, 300));
222
    expect(imageSizeInfo.displaySize, const Size(200, 100) * PaintingBinding.instance.window.devicePixelRatio);
223 224 225 226 227 228 229 230 231

    // Make sure that we don't report an identical image size info if we
    // redraw in the next frame.
    tester.binding.scheduleForcedFrame();
    await tester.pump();

    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 150.0),
232
      image: image300x300,
233 234 235 236 237 238 239
      debugImageLabel: 'test.png',
    );

    expect(count, 2);
    expect(imageSizeInfo, isNotNull);
    expect(imageSizeInfo.source, 'test.png');
    expect(imageSizeInfo.imageSize, const Size(300, 300));
240
    expect(imageSizeInfo.displaySize, const Size(200, 150) * PaintingBinding.instance.window.devicePixelRatio);
241 242 243 244 245

    debugOnPaintImage = null;
  });

  testWidgets('Reports Image painting - no debug label', (WidgetTester tester) async {
246
    late ImageSizeInfo imageSizeInfo;
247 248 249 250 251 252 253 254 255 256
    int count = 0;
    debugOnPaintImage = (ImageSizeInfo info) {
      count += 1;
      imageSizeInfo = info;
    };

    final TestCanvas canvas = TestCanvas();
    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
257
      image: image300x200,
258 259 260 261 262 263
    );

    expect(count, 1);
    expect(imageSizeInfo, isNotNull);
    expect(imageSizeInfo.source, '<Unknown Image(300×200)>');
    expect(imageSizeInfo.imageSize, const Size(300, 200));
264
    expect(imageSizeInfo.displaySize, const Size(200, 100) * PaintingBinding.instance.window.devicePixelRatio);
265 266 267 268

    debugOnPaintImage = null;
  });

269
  // See also the DecorationImage tests in: decoration_test.dart
270
}