paint_image_test.dart 6.49 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 8
import 'dart:ui' as ui;

9
import 'package:flutter/foundation.dart';
10
import 'package:flutter_test/flutter_test.dart';
11 12 13 14 15
import 'package:flutter/painting.dart';

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

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

void main() {
23 24 25 26 27 28 29
  ui.Image image300x300;
  ui.Image image300x200;
  setUpAll(() async {
    image300x300 = await createTestImage(width: 300, height: 300, cache: false);
    image300x200 = await createTestImage(width: 300, height: 200, cache: false);
  });

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

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

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

    expect(command, isNotNull);
49
    expect(command.positionalArguments[0], equals(image300x300));
Dan Field's avatar
Dan Field committed
50 51
    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)));
52
  });
53

54
  test('debugInvertOversizedImages', () async {
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    debugInvertOversizedImages = true;
    final FlutterExceptionHandler oldFlutterError = FlutterError.onError;

    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,
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      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,
      'Image TestImage has a display size of 200×100 but a decode size of 300×300, which uses an additional 364kb.\n\n'
      '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;
  });

113 114 115 116 117 118 119 120 121 122 123 124
  testWidgets('Reports Image painting', (WidgetTester tester) async {
    ImageSizeInfo imageSizeInfo;
    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),
125
      image: image300x300,
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      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),
143
      image: image300x300,
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      debugImageLabel: 'test.png',
    );

    expect(count, 1);

    debugOnPaintImage = null;
  });

  testWidgets('Reports Image painting - change per frame', (WidgetTester tester) async {
    ImageSizeInfo imageSizeInfo;
    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),
164
      image: image300x300,
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      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),
182
      image: image300x300,
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
      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 {
    ImageSizeInfo imageSizeInfo;
    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),
207
      image: image300x200,
208 209 210 211 212 213 214 215 216 217 218
    );

    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;
  });

219
  // See also the DecorationImage tests in: decoration_test.dart
220
}