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

5 6
import 'dart:async';
import 'dart:typed_data';
7
import 'dart:ui' as ui show Image, ImageByteFormat;
8

9
import 'package:flutter/rendering.dart';
10
import 'package:flutter_test/flutter_test.dart';
11

12
import 'rendering_tester.dart';
13

14
class SquareImage implements ui.Image {
15
  @override
16
  int get width => 10;
17 18

  @override
19
  int get height => 10;
20

21
  @override
22
  Future<ByteData> toByteData({ ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba }) async {
23
    throw UnsupportedError('Cannot encode test image');
24 25
  }

26 27 28
  @override
  String toString() => '[$width\u00D7$height]';

29
  @override
Hixie's avatar
Hixie committed
30
  void dispose() { }
31 32
}

33
class WideImage implements ui.Image {
34
  @override
35
  int get width => 20;
36 37

  @override
38
  int get height => 10;
39

40
  @override
41
  Future<ByteData> toByteData({ ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba }) async {
42
    throw UnsupportedError('Cannot encode test image');
43 44
  }

45 46 47
  @override
  String toString() => '[$width\u00D7$height]';

48
  @override
Hixie's avatar
Hixie committed
49
  void dispose() { }
50 51
}

52
class TallImage implements ui.Image {
53
  @override
54
  int get width => 10;
55 56

  @override
57
  int get height => 20;
58

59
  @override
60
  Future<ByteData> toByteData({ ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba }) async {
61
    throw UnsupportedError('Cannot encode test image');
62 63
  }

64 65 66
  @override
  String toString() => '[$width\u00D7$height]';

67
  @override
Hixie's avatar
Hixie committed
68
  void dispose() { }
69 70 71 72 73 74
}

void main() {
  test('Image sizing', () {
    RenderImage image;

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

85 86
    expect(image, hasAGoodToStringDeep);
    expect(
87
      image.toStringDeep(minLevel: DiagnosticLevel.info),
88 89 90 91 92 93
      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'
        '   image: [10×10]\n'
94
        '   alignment: center\n'
95
        '   invertColors: false\n'
96
        '   filterQuality: low\n'
97 98 99
      ),
    );

100
    image = RenderImage(image: WideImage());
101
    layout(image,
102
           constraints: const BoxConstraints(
103 104 105 106 107 108 109
              minWidth: 5.0,
              minHeight: 30.0,
              maxWidth: 100.0,
              maxHeight: 100.0));
    expect(image.size.width, equals(60.0));
    expect(image.size.height, equals(30.0));

110
    image = RenderImage(image: TallImage());
111
    layout(image,
112
           constraints: const BoxConstraints(
113 114 115 116 117 118 119
              minWidth: 50.0,
              minHeight: 5.0,
              maxWidth: 75.0,
              maxHeight: 75.0));
    expect(image.size.width, equals(50.0));
    expect(image.size.height, equals(75.0));

120
    image = RenderImage(image: WideImage());
121
    layout(image,
122
           constraints: const BoxConstraints(
123 124 125 126 127 128 129
              minWidth: 5.0,
              minHeight: 5.0,
              maxWidth: 100.0,
              maxHeight: 100.0));
    expect(image.size.width, equals(20.0));
    expect(image.size.height, equals(10.0));

130
    image = RenderImage(image: WideImage());
131
    layout(image,
132
           constraints: const BoxConstraints(
133 134 135 136 137 138 139
              minWidth: 5.0,
              minHeight: 5.0,
              maxWidth: 16.0,
              maxHeight: 16.0));
    expect(image.size.width, equals(16.0));
    expect(image.size.height, equals(8.0));

140
    image = RenderImage(image: TallImage());
141
    layout(image,
142
           constraints: const BoxConstraints(
143 144 145 146 147 148 149
              minWidth: 5.0,
              minHeight: 5.0,
              maxWidth: 16.0,
              maxHeight: 16.0));
    expect(image.size.width, equals(8.0));
    expect(image.size.height, equals(16.0));

150
    image = RenderImage(image: SquareImage());
151
    layout(image,
152
           constraints: const BoxConstraints(
153 154 155 156 157 158 159
              minWidth: 4.0,
              minHeight: 4.0,
              maxWidth: 8.0,
              maxHeight: 8.0));
    expect(image.size.width, equals(8.0));
    expect(image.size.height, equals(8.0));

160
    image = RenderImage(image: WideImage());
161
    layout(image,
162
           constraints: const BoxConstraints(
163 164 165 166 167 168 169
              minWidth: 20.0,
              minHeight: 20.0,
              maxWidth: 30.0,
              maxHeight: 30.0));
    expect(image.size.width, equals(30.0));
    expect(image.size.height, equals(20.0));

170
    image = RenderImage(image: TallImage());
171
    layout(image,
172
           constraints: const BoxConstraints(
173 174 175 176 177 178 179 180 181 182 183
              minWidth: 20.0,
              minHeight: 20.0,
              maxWidth: 30.0,
              maxHeight: 30.0));
    expect(image.size.width, equals(20.0));
    expect(image.size.height, equals(30.0));
  });

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

184
    image = RenderImage();
185
    layout(image,
186
           constraints: const BoxConstraints(
187 188 189 190 191 192 193
             minWidth: 25.0,
             minHeight: 25.0,
             maxWidth: 100.0,
             maxHeight: 100.0));
    expect(image.size.width, equals(25.0));
    expect(image.size.height, equals(25.0));

194
    image = RenderImage(width: 50.0);
195
    layout(image,
196
           constraints: const BoxConstraints(
197 198 199 200 201 202 203
             minWidth: 25.0,
             minHeight: 25.0,
             maxWidth: 100.0,
             maxHeight: 100.0));
    expect(image.size.width, equals(50.0));
    expect(image.size.height, equals(25.0));

204
    image = RenderImage(height: 50.0);
205
    layout(image,
206
           constraints: const BoxConstraints(
207 208 209 210 211 212 213
             minWidth: 25.0,
             minHeight: 25.0,
             maxWidth: 100.0,
             maxHeight: 100.0));
    expect(image.size.width, equals(25.0));
    expect(image.size.height, equals(50.0));

214
    image = RenderImage(width: 100.0, height: 100.0);
215
    layout(image,
216
           constraints: const BoxConstraints(
217 218 219 220 221 222 223
             minWidth: 25.0,
             minHeight: 25.0,
             maxWidth: 75.0,
             maxHeight: 75.0));
    expect(image.size.width, equals(75.0));
    expect(image.size.height, equals(75.0));
  });
224 225

  test('update image colorBlendMode', () {
226
    final RenderImage image = RenderImage();
227 228 229 230
    expect(image.colorBlendMode, isNull);
    image.colorBlendMode = BlendMode.color;
    expect(image.colorBlendMode, BlendMode.color);
  });
231
}