image_test.dart 7.13 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
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 'dart:ui' as ui show Image;
6

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter_test/flutter_test.dart';
10

11
import 'rendering_tester.dart';
12

13
Future<void> main() async {
14 15
  TestRenderingFlutterBinding.ensureInitialized();

16 17 18
  final ui.Image squareImage = await createTestImage(width: 10, height: 10);
  final ui.Image wideImage =   await createTestImage(width: 20, height: 10);
  final ui.Image tallImage =   await createTestImage(width: 10, height: 20);
19 20 21
  test('Image sizing', () {
    RenderImage image;

22
    image = RenderImage(image: squareImage);
23 24 25 26 27 28
    layout(image, constraints: const BoxConstraints(
      minWidth: 25.0,
      minHeight: 25.0,
      maxWidth: 100.0,
      maxHeight: 100.0,
    ));
29 30 31
    expect(image.size.width, equals(25.0));
    expect(image.size.height, equals(25.0));

32
    expect(image, hasAGoodToStringDeep);
33
    expect(
34
      image.toStringDeep(minLevel: DiagnosticLevel.info),
35 36 37 38 39
      equalsIgnoringHashCodes(
        'RenderImage#00000 relayoutBoundary=up2 NEEDS-PAINT\n'
        '   parentData: <none> (can use size)\n'
        '   constraints: BoxConstraints(25.0<=w<=100.0, 25.0<=h<=100.0)\n'
        '   size: Size(25.0, 25.0)\n'
40
        '   image: $squareImage\n'
41
        '   alignment: Alignment.center\n'
42
        '   invertColors: false\n'
43
        '   filterQuality: low\n',
44 45 46
      ),
    );

47
    image = RenderImage(image: wideImage);
48 49 50 51 52 53
    layout(image, constraints: const BoxConstraints(
      minWidth: 5.0,
      minHeight: 30.0,
      maxWidth: 100.0,
      maxHeight: 100.0,
    ));
54 55 56
    expect(image.size.width, equals(60.0));
    expect(image.size.height, equals(30.0));

57
    image = RenderImage(image: tallImage);
58 59 60 61 62 63
    layout(image, constraints: const BoxConstraints(
      minWidth: 50.0,
      minHeight: 5.0,
      maxWidth: 75.0,
      maxHeight: 75.0,
    ));
64 65 66
    expect(image.size.width, equals(50.0));
    expect(image.size.height, equals(75.0));

67
    image = RenderImage(image: wideImage);
68 69 70 71 72 73
    layout(image, constraints: const BoxConstraints(
      minWidth: 5.0,
      minHeight: 5.0,
      maxWidth: 100.0,
      maxHeight: 100.0,
    ));
74 75 76
    expect(image.size.width, equals(20.0));
    expect(image.size.height, equals(10.0));

77
    image = RenderImage(image: wideImage);
78 79 80 81 82 83
    layout(image, constraints: const BoxConstraints(
      minWidth: 5.0,
      minHeight: 5.0,
      maxWidth: 16.0,
      maxHeight: 16.0,
    ));
84 85 86
    expect(image.size.width, equals(16.0));
    expect(image.size.height, equals(8.0));

87
    image = RenderImage(image: tallImage);
88 89 90 91 92 93
    layout(image, constraints: const BoxConstraints(
      minWidth: 5.0,
      minHeight: 5.0,
      maxWidth: 16.0,
      maxHeight: 16.0,
    ));
94 95 96
    expect(image.size.width, equals(8.0));
    expect(image.size.height, equals(16.0));

97
    image = RenderImage(image: squareImage);
98 99 100 101 102 103
    layout(image, constraints: const BoxConstraints(
      minWidth: 4.0,
      minHeight: 4.0,
      maxWidth: 8.0,
      maxHeight: 8.0,
    ));
104 105 106
    expect(image.size.width, equals(8.0));
    expect(image.size.height, equals(8.0));

107
    image = RenderImage(image: wideImage);
108 109 110 111 112 113
    layout(image, constraints: const BoxConstraints(
      minWidth: 20.0,
      minHeight: 20.0,
      maxWidth: 30.0,
      maxHeight: 30.0,
    ));
114 115 116
    expect(image.size.width, equals(30.0));
    expect(image.size.height, equals(20.0));

117
    image = RenderImage(image: tallImage);
118 119 120 121 122 123
    layout(image, constraints: const BoxConstraints(
      minWidth: 20.0,
      minHeight: 20.0,
      maxWidth: 30.0,
      maxHeight: 30.0,
    ));
124 125 126 127 128 129 130
    expect(image.size.width, equals(20.0));
    expect(image.size.height, equals(30.0));
  });

  test('Null image sizing', () {
    RenderImage image;

131
    image = RenderImage();
132 133 134 135 136 137
    layout(image, constraints: const BoxConstraints(
      minWidth: 25.0,
      minHeight: 25.0,
      maxWidth: 100.0,
      maxHeight: 100.0,
    ));
138 139 140
    expect(image.size.width, equals(25.0));
    expect(image.size.height, equals(25.0));

141
    image = RenderImage(width: 50.0);
142 143 144 145 146 147
    layout(image, constraints: const BoxConstraints(
      minWidth: 25.0,
      minHeight: 25.0,
      maxWidth: 100.0,
      maxHeight: 100.0,
    ));
148 149 150
    expect(image.size.width, equals(50.0));
    expect(image.size.height, equals(25.0));

151
    image = RenderImage(height: 50.0);
152 153 154 155 156 157
    layout(image, constraints: const BoxConstraints(
      minWidth: 25.0,
      minHeight: 25.0,
      maxWidth: 100.0,
      maxHeight: 100.0,
    ));
158 159 160
    expect(image.size.width, equals(25.0));
    expect(image.size.height, equals(50.0));

161
    image = RenderImage(width: 100.0, height: 100.0);
162 163 164 165 166 167
    layout(image, constraints: const BoxConstraints(
      minWidth: 25.0,
      minHeight: 25.0,
      maxWidth: 75.0,
      maxHeight: 75.0,
    ));
168 169 170
    expect(image.size.width, equals(75.0));
    expect(image.size.height, equals(75.0));
  });
171 172

  test('update image colorBlendMode', () {
173
    final RenderImage image = RenderImage();
174 175 176 177
    expect(image.colorBlendMode, isNull);
    image.colorBlendMode = BlendMode.color;
    expect(image.colorBlendMode, BlendMode.color);
  });
178

179
  test('RenderImage disposes its image', () async {
180
    final ui.Image image = await createTestImage(width: 10, height: 10, cache: false);
181
    expect(image.debugGetOpenHandleStackTraces()!.length, 1);
182 183

    final RenderImage renderImage = RenderImage(image: image.clone());
184
    expect(image.debugGetOpenHandleStackTraces()!.length, 2);
185 186

    renderImage.image = image.clone();
187
    expect(image.debugGetOpenHandleStackTraces()!.length, 2);
188 189

    renderImage.image = null;
190
    expect(image.debugGetOpenHandleStackTraces()!.length, 1);
191 192

    image.dispose();
193
    expect(image.debugGetOpenHandleStackTraces()!.length, 0);
194
  }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87442
195

196
  test('RenderImage does not dispose its image if setting the same image twice', () async {
197
    final ui.Image image = await createTestImage(width: 10, height: 10, cache: false);
198
    expect(image.debugGetOpenHandleStackTraces()!.length, 1);
199 200

    final RenderImage renderImage = RenderImage(image: image.clone());
201
    expect(image.debugGetOpenHandleStackTraces()!.length, 2);
202 203

    renderImage.image = renderImage.image;
204
    expect(image.debugGetOpenHandleStackTraces()!.length, 2);
205 206

    renderImage.image = null;
207
    expect(image.debugGetOpenHandleStackTraces()!.length, 1);
208 209

    image.dispose();
210
    expect(image.debugGetOpenHandleStackTraces()!.length, 0);
211
  }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87442
212 213 214 215 216 217 218 219 220 221 222 223 224 225

  test('Render image disposes its image when it is disposed', () async {
    final ui.Image image = await createTestImage(width: 10, height: 10, cache: false);
    expect(image.debugGetOpenHandleStackTraces()!.length, 1);

    final RenderImage renderImage = RenderImage(image: image.clone());
    expect(image.debugGetOpenHandleStackTraces()!.length, 2);

    renderImage.dispose();
    expect(image.debugGetOpenHandleStackTraces()!.length, 1);
    expect(renderImage.image, null);

    image.dispose();
    expect(image.debugGetOpenHandleStackTraces()!.length, 0);
226
  }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87442
227
}