paint_image_test.dart 8.16 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/painting.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
    final FlutterExceptionHandler? oldFlutterError = FlutterError.onError;
56 57 58 59 60 61 62 63 64 65 66 67

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

    final TestCanvas canvas = TestCanvas();
    const Rect rect = Rect.fromLTWH(50.0, 50.0, 200.0, 100.0);

    paintImage(
      canvas: canvas,
      rect: rect,
68
      image: image300x300,
69 70 71 72 73 74 75 76 77 78 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
      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);
    expect(commands[1].positionalArguments[1], 100.0);

    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);
    expect(commands[3].positionalArguments[1], -100.0);

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

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

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 137 138 139 140 141
  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;
    }
  });

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  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)));
  });

162
  testWidgets('Reports Image painting', (WidgetTester tester) async {
163
    late ImageSizeInfo imageSizeInfo;
164 165 166 167 168 169 170 171 172 173
    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),
174
      image: image300x300,
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
      debugImageLabel: 'test.png',
    );

    expect(count, 1);
    expect(imageSizeInfo, isNotNull);
    expect(imageSizeInfo.source, 'test.png');
    expect(imageSizeInfo.imageSize, const Size(300, 300));
    expect(imageSizeInfo.displaySize, const Size(200, 100));

    // 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),
192
      image: image300x300,
193 194 195 196 197 198 199 200 201
      debugImageLabel: 'test.png',
    );

    expect(count, 1);

    debugOnPaintImage = null;
  });

  testWidgets('Reports Image painting - change per frame', (WidgetTester tester) async {
202
    late ImageSizeInfo imageSizeInfo;
203 204 205 206 207 208 209 210 211 212
    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),
213
      image: image300x300,
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      debugImageLabel: 'test.png',
    );

    expect(count, 1);
    expect(imageSizeInfo, isNotNull);
    expect(imageSizeInfo.source, 'test.png');
    expect(imageSizeInfo.imageSize, const Size(300, 300));
    expect(imageSizeInfo.displaySize, const Size(200, 100));

    // 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),
231
      image: image300x300,
232 233 234 235 236 237 238 239 240 241 242 243 244
      debugImageLabel: 'test.png',
    );

    expect(count, 2);
    expect(imageSizeInfo, isNotNull);
    expect(imageSizeInfo.source, 'test.png');
    expect(imageSizeInfo.imageSize, const Size(300, 300));
    expect(imageSizeInfo.displaySize, const Size(200, 150));

    debugOnPaintImage = null;
  });

  testWidgets('Reports Image painting - no debug label', (WidgetTester tester) async {
245
    late ImageSizeInfo imageSizeInfo;
246 247 248 249 250 251 252 253 254 255
    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),
256
      image: image300x200,
257 258 259 260 261 262 263 264 265 266 267
    );

    expect(count, 1);
    expect(imageSizeInfo, isNotNull);
    expect(imageSizeInfo.source, '<Unknown Image(300×200)>');
    expect(imageSizeInfo.imageSize, const Size(300, 200));
    expect(imageSizeInfo.displaySize, const Size(200, 100));

    debugOnPaintImage = null;
  });

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