image_test.dart 5.88 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
import 'dart:ui' as ui show Image;
6

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

10
import 'rendering_tester.dart';
11

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

  @override
17
  int get height => 10;
18

19 20 21
  @override
  String toString() => '[$width\u00D7$height]';

22
  @override
Hixie's avatar
Hixie committed
23
  void dispose() { }
24 25
}

26
class WideImage implements ui.Image {
27
  @override
28
  int get width => 20;
29 30

  @override
31
  int get height => 10;
32

33 34 35
  @override
  String toString() => '[$width\u00D7$height]';

36
  @override
Hixie's avatar
Hixie committed
37
  void dispose() { }
38 39
}

40
class TallImage implements ui.Image {
41
  @override
42
  int get width => 10;
43 44

  @override
45
  int get height => 20;
46

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

50
  @override
Hixie's avatar
Hixie committed
51
  void dispose() { }
52 53 54 55 56 57 58 59
}

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

    image = new RenderImage(image: new SquareImage());
    layout(image,
60
          constraints: const BoxConstraints(
61 62 63 64 65 66 67
              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));

68 69
    expect(image, hasAGoodToStringDeep);
    expect(
70
      image.toStringDeep(minLevel: DiagnosticLevel.info),
71 72 73 74 75 76
      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'
77
        '   alignment: center\n'
78 79 80
      ),
    );

81 82
    image = new RenderImage(image: new WideImage());
    layout(image,
83
           constraints: const BoxConstraints(
84 85 86 87 88 89 90 91 92
              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,
93
           constraints: const BoxConstraints(
94 95 96 97 98 99 100 101 102
              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,
103
           constraints: const BoxConstraints(
104 105 106 107 108 109 110 111 112
              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,
113
           constraints: const BoxConstraints(
114 115 116 117 118 119 120 121 122
              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,
123
           constraints: const BoxConstraints(
124 125 126 127 128 129 130 131 132
              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,
133
           constraints: const BoxConstraints(
134 135 136 137 138 139 140 141 142
              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,
143
           constraints: const BoxConstraints(
144 145 146 147 148 149 150 151 152
              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,
153
           constraints: const BoxConstraints(
154 155 156 157 158 159 160 161 162 163 164 165 166
              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,
167
           constraints: const BoxConstraints(
168 169 170 171 172 173 174 175 176
             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,
177
           constraints: const BoxConstraints(
178 179 180 181 182 183 184 185 186
             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,
187
           constraints: const BoxConstraints(
188 189 190 191 192 193 194 195 196
             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,
197
           constraints: const BoxConstraints(
198 199 200 201 202 203 204
             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));
  });
205 206 207 208 209 210 211

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