image_test.dart 7.08 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 14 15 16
Future<void> main() async {
  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);
17 18 19
  test('Image sizing', () {
    RenderImage image;

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

30
    expect(image, hasAGoodToStringDeep);
31
    expect(
32
      image.toStringDeep(minLevel: DiagnosticLevel.info),
33 34 35 36 37
      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'
38
        '   image: $squareImage\n'
39
        '   alignment: Alignment.center\n'
40
        '   invertColors: false\n'
41
        '   filterQuality: low\n',
42 43 44
      ),
    );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    renderImage.image = image.clone();
185
    expect(image.debugGetOpenHandleStackTraces()!.length, 2);
186 187

    renderImage.image = null;
188
    expect(image.debugGetOpenHandleStackTraces()!.length, 1);
189 190

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

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

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

    renderImage.image = renderImage.image;
202
    expect(image.debugGetOpenHandleStackTraces()!.length, 2);
203 204

    renderImage.image = null;
205
    expect(image.debugGetOpenHandleStackTraces()!.length, 1);
206 207

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

  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);
224
  }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87442
225
}