1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2016 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.
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui show Image;
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
class TestImage implements ui.Image {
TestImage(this.scale);
final double scale;
@override
int get width => (48*scale).floor();
@override
int get height => (48*scale).floor();
@override
void dispose() { }
}
class TestByteData implements ByteData {
TestByteData(this.scale);
final double scale;
@override
dynamic noSuchMethod(Invocation invocation) => null;
}
const String testManifest = '''
{
"assets/image.png" : [
"assets/image.png",
"assets/1.5x/image.png",
"assets/2.0x/image.png",
"assets/3.0x/image.png",
"assets/4.0x/image.png"
]
}
''';
class TestAssetBundle extends CachingAssetBundle {
TestAssetBundle({ this.manifest: testManifest });
final String manifest;
@override
Future<ByteData> load(String key) {
ByteData data;
switch (key) {
case 'assets/image.png':
data = new TestByteData(1.0);
break;
case 'assets/1.0x/image.png':
data = new TestByteData(10.0); // see "...with a main asset and a 1.0x asset"
break;
case 'assets/1.5x/image.png':
data = new TestByteData(1.5);
break;
case 'assets/2.0x/image.png':
data = new TestByteData(2.0);
break;
case 'assets/3.0x/image.png':
data = new TestByteData(3.0);
break;
case 'assets/4.0x/image.png':
data = new TestByteData(4.0);
break;
}
return new SynchronousFuture<ByteData>(data);
}
@override
Future<String> loadString(String key, { bool cache: true }) {
if (key == 'AssetManifest.json')
return new SynchronousFuture<String>(manifest);
return null;
}
@override
String toString() => '${describeIdentity(this)}()';
}
class FakeImageStreamCompleter extends ImageStreamCompleter {
FakeImageStreamCompleter(Future<ImageInfo> image) {
image.then<void>(setImage);
}
}
class TestAssetImage extends AssetImage {
TestAssetImage(String name) : super(name);
@override
ImageStreamCompleter load(AssetBundleImageKey key) {
ImageInfo imageInfo;
key.bundle.load(key.name).then<void>((ByteData data) {
final TestByteData testData = data;
final ui.Image image = new TestImage(testData.scale);
imageInfo = new ImageInfo(image: image, scale: key.scale);
});
assert(imageInfo != null);
return new FakeImageStreamCompleter(
new SynchronousFuture<ImageInfo>(imageInfo)
);
}
}
Widget buildImageAtRatio(String image, Key key, double ratio, bool inferSize, [AssetBundle bundle]) {
const double windowSize = 500.0; // 500 logical pixels
const double imageSize = 200.0; // 200 logical pixels
return new MediaQuery(
data: new MediaQueryData(
size: const Size(windowSize, windowSize),
devicePixelRatio: ratio,
padding: const EdgeInsets.all(0.0)
),
child: new DefaultAssetBundle(
bundle: bundle ?? new TestAssetBundle(),
child: new Center(
child: inferSize ?
new Image(
key: key,
image: new TestAssetImage(image)
) :
new Image(
key: key,
image: new TestAssetImage(image),
height: imageSize,
width: imageSize,
fit: BoxFit.fill
)
)
)
);
}
RenderImage getRenderImage(WidgetTester tester, Key key) {
return tester.renderObject<RenderImage>(find.byKey(key));
}
TestImage getTestImage(WidgetTester tester, Key key) {
return tester.renderObject<RenderImage>(find.byKey(key)).image;
}
Future<Null> pumpTreeToLayout(WidgetTester tester, Widget widget) {
const Duration pumpDuration = const Duration(milliseconds: 0);
const EnginePhase pumpPhase = EnginePhase.layout;
return tester.pumpWidget(widget, pumpDuration, pumpPhase);
}
void main() {
const String image = 'assets/image.png';
testWidgets('Image for device pixel ratio 1.0', (WidgetTester tester) async {
const double ratio = 1.0;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 1.0);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true));
expect(getRenderImage(tester, key).size, const Size(48.0, 48.0));
expect(getTestImage(tester, key).scale, 1.0);
});
testWidgets('Image for device pixel ratio 0.5', (WidgetTester tester) async {
const double ratio = 0.5;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 1.0);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true));
expect(getRenderImage(tester, key).size, const Size(48.0, 48.0));
expect(getTestImage(tester, key).scale, 1.0);
});
testWidgets('Image for device pixel ratio 1.5', (WidgetTester tester) async {
const double ratio = 1.5;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 1.5);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true));
expect(getRenderImage(tester, key).size, const Size(48.0, 48.0));
expect(getTestImage(tester, key).scale, 1.5);
});
testWidgets('Image for device pixel ratio 1.75', (WidgetTester tester) async {
const double ratio = 1.75;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 1.5);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true));
expect(getRenderImage(tester, key).size, const Size(48.0, 48.0));
expect(getTestImage(tester, key).scale, 1.5);
});
testWidgets('Image for device pixel ratio 2.3', (WidgetTester tester) async {
const double ratio = 2.3;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 2.0);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true));
expect(getRenderImage(tester, key).size, const Size(48.0, 48.0));
expect(getTestImage(tester, key).scale, 2.0);
});
testWidgets('Image for device pixel ratio 3.7', (WidgetTester tester) async {
const double ratio = 3.7;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 4.0);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true));
expect(getRenderImage(tester, key).size, const Size(48.0, 48.0));
expect(getTestImage(tester, key).scale, 4.0);
});
testWidgets('Image for device pixel ratio 5.1', (WidgetTester tester) async {
const double ratio = 5.1;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 4.0);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true));
expect(getRenderImage(tester, key).size, const Size(48.0, 48.0));
expect(getTestImage(tester, key).scale, 4.0);
});
testWidgets('Image for device pixel ratio 1.0, with no main asset', (WidgetTester tester) async {
const String manifest = '''
{
"assets/image.png" : [
"assets/1.5x/image.png",
"assets/2.0x/image.png",
"assets/3.0x/image.png",
"assets/4.0x/image.png"
]
}
''';
final AssetBundle bundle = new TestAssetBundle(manifest: manifest);
const double ratio = 1.0;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false, bundle));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 1.5);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true, bundle));
expect(getRenderImage(tester, key).size, const Size(48.0, 48.0));
expect(getTestImage(tester, key).scale, 1.5);
});
testWidgets('Image for device pixel ratio 1.0, with a main asset and a 1.0x asset', (WidgetTester tester) async {
// If both a main asset and a 1.0x asset are specified, then prefer
// the 1.0x asset.
const String manifest = '''
{
"assets/image.png" : [
"assets/image.png",
"assets/1.0x/image.png",
"assets/1.5x/image.png",
"assets/2.0x/image.png",
"assets/3.0x/image.png",
"assets/4.0x/image.png"
]
}
''';
final AssetBundle bundle = new TestAssetBundle(manifest: manifest);
const double ratio = 1.0;
Key key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, false, bundle));
expect(getRenderImage(tester, key).size, const Size(200.0, 200.0));
expect(getTestImage(tester, key).scale, 10.0);
key = new GlobalKey();
await pumpTreeToLayout(tester, buildImageAtRatio(image, key, ratio, true, bundle));
expect(getRenderImage(tester, key).size, const Size(480.0, 480.0));
expect(getTestImage(tester, key).scale, 10.0);
});
}