image_test.dart 6.36 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}) async {
23 24 25
    throw new UnsupportedError('Cannot encode test image');
  }

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}) async {
42 43 44
    throw new UnsupportedError('Cannot encode test image');
  }

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}) async {
61 62 63
    throw new UnsupportedError('Cannot encode test image');
  }

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 75 76
}

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

    image = new RenderImage(image: new SquareImage());
    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 96 97
      ),
    );

98 99
    image = new RenderImage(image: new WideImage());
    layout(image,
100
           constraints: const BoxConstraints(
101 102 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));

    image = new RenderImage(image: new TallImage());
    layout(image,
110
           constraints: const BoxConstraints(
111 112 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));

    image = new RenderImage(image: new WideImage());
    layout(image,
120
           constraints: const BoxConstraints(
121 122 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));

    image = new RenderImage(image: new WideImage());
    layout(image,
130
           constraints: const BoxConstraints(
131 132 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));

    image = new RenderImage(image: new TallImage());
    layout(image,
140
           constraints: const BoxConstraints(
141 142 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));

    image = new RenderImage(image: new SquareImage());
    layout(image,
150
           constraints: const BoxConstraints(
151 152 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));

    image = new RenderImage(image: new WideImage());
    layout(image,
160
           constraints: const BoxConstraints(
161 162 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));

    image = new RenderImage(image: new TallImage());
    layout(image,
170
           constraints: const BoxConstraints(
171 172 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;

    image = new RenderImage();
    layout(image,
184
           constraints: const BoxConstraints(
185 186 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));

    image = new RenderImage(width: 50.0);
    layout(image,
194
           constraints: const BoxConstraints(
195 196 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));

    image = new RenderImage(height: 50.0);
    layout(image,
204
           constraints: const BoxConstraints(
205 206 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));

    image = new RenderImage(width: 100.0, height: 100.0);
    layout(image,
214
           constraints: const BoxConstraints(
215 216 217 218 219 220 221
             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));
  });
222 223 224 225 226 227 228

  test('update image colorBlendMode', () {
    final RenderImage image = new RenderImage();
    expect(image.colorBlendMode, isNull);
    image.colorBlendMode = BlendMode.color;
    expect(image.colorBlendMode, BlendMode.color);
  });
229
}