Skip to content

Commit 0432554

Browse files
committed
Improve tests
1 parent 29a0c8a commit 0432554

File tree

3 files changed

+210
-6
lines changed

3 files changed

+210
-6
lines changed

packages/google_maps_flutter/google_maps_flutter_ios/ios/Classes/GoogleMapMarkerController.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ - (UIImage *)scaleImage:(UIImage *)image by:(id)scaleParam {
257257
- (UIImage *)scaleImage:(UIImage *)image scale:(CGFloat)scale {
258258
if (fabs(scale - image.scale) > 1e-3) {
259259
return [UIImage imageWithCGImage:[image CGImage]
260-
scale:scale orientation:(image.imageOrientation)];
260+
scale:scale
261+
orientation:(image.imageOrientation)];
261262
}
262263
return image;
263264
}

packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ class BitmapDescriptor {
7272
assert((jsonList[3] as List<dynamic>).length == 2);
7373
}
7474
break;
75+
case _asset:
76+
assert(jsonList.length == 4 || jsonList.length == 5);
77+
assert(jsonList[1] != null && jsonList[1] is String);
78+
assert((jsonList[1] as String).isNotEmpty);
79+
assert(jsonList[2] != null && jsonList[2] is String);
80+
assert(jsonList[3] != null && jsonList[3] is double);
81+
if (jsonList.length == 5) {
82+
assert(jsonList[4] != null && jsonList[4] is List<dynamic>);
83+
assert((jsonList[4] as List<dynamic>).length == 2);
84+
}
85+
break;
86+
case _bytes:
87+
assert(jsonList.length == 4 || jsonList.length == 5);
88+
assert(jsonList[1] != null && jsonList[1] is List<int>);
89+
assert(jsonList[2] != null && jsonList[2] is String);
90+
assert(jsonList[3] != null && jsonList[3] is double);
91+
if (jsonList.length == 5) {
92+
assert(jsonList[4] != null && jsonList[4] is List<dynamic>);
93+
assert((jsonList[4] as List<dynamic>).length == 2);
94+
}
95+
assert(
96+
(jsonList[2] as String) != bitmapNoScaling || jsonList.length == 4);
97+
assert((jsonList[2] as String) != bitmapNoScaling ||
98+
(jsonList[3] as double) == 1.0);
99+
break;
75100
default:
76101
break;
77102
}
@@ -81,11 +106,8 @@ class BitmapDescriptor {
81106
static const String _asset = 'asset';
82107
static const String _bytes = 'bytes';
83108

84-
@Deprecated('No longer supported')
85109
static const String _fromAsset = 'fromAsset';
86-
@Deprecated('No longer supported')
87110
static const String _fromAssetImage = 'fromAssetImage';
88-
@Deprecated('No longer supported')
89111
static const String _fromBytes = 'fromBytes';
90112

91113
/// Value representing auto scaling parameter.
@@ -152,7 +174,7 @@ class BitmapDescriptor {
152174
/// This method takes into consideration various asset resolutions
153175
/// and scales the images to the right resolution depending on the dpi.
154176
/// Set `mipmaps` to false to load the exact dpi version of the image, `mipmap` is true by default.
155-
@Deprecated('No longer supported')
177+
@Deprecated('Switch to using createFromAsset instead')
156178
static Future<BitmapDescriptor> fromAssetImage(
157179
ImageConfiguration configuration,
158180
String assetName, {
@@ -191,7 +213,7 @@ class BitmapDescriptor {
191213
/// bitmap, regardless of the actual resolution of the encoded PNG.
192214
/// This helps the browser to render High-DPI images at the correct size.
193215
/// `size` is not required (and ignored, if passed) in other platforms.
194-
@Deprecated('No longer supported')
216+
@Deprecated('Switch to using createFromBytes instead')
195217
static BitmapDescriptor fromBytes(Uint8List byteData, {Size? size}) {
196218
assert(byteData.isNotEmpty,
197219
'Cannot create BitmapDescriptor with empty byteData');

packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/bitmap_test.dart

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void main() {
3838
1.0
3939
]));
4040
});
41+
4142
test('with mipmaps', () async {
4243
final BitmapDescriptor descriptor =
4344
await BitmapDescriptor.createFromAsset(
@@ -54,6 +55,7 @@ void main() {
5455
1.0
5556
]));
5657
});
58+
5759
test('with size and without mipmaps', () async {
5860
final double devicePixelRatio = WidgetsBinding
5961
.instance.platformDispatcher.views.first.devicePixelRatio;
@@ -142,12 +144,63 @@ void main() {
142144
}, skip: !kIsWeb);
143145
});
144146

147+
group('fromBytes constructor', () {
148+
test('with empty byte array, throws assertion error', () {
149+
expect(() {
150+
BitmapDescriptor.fromBytes(Uint8List.fromList(<int>[]));
151+
}, throwsAssertionError);
152+
});
153+
154+
test('with bytes', () {
155+
final BitmapDescriptor descriptor = BitmapDescriptor.fromBytes(
156+
Uint8List.fromList(<int>[1, 2, 3]),
157+
);
158+
expect(descriptor, isA<BitmapDescriptor>());
159+
expect(
160+
descriptor.toJson(),
161+
equals(<Object>[
162+
'fromBytes',
163+
<int>[1, 2, 3],
164+
]));
165+
});
166+
167+
test('with size, not on the web, size is ignored', () {
168+
final BitmapDescriptor descriptor = BitmapDescriptor.fromBytes(
169+
Uint8List.fromList(<int>[1, 2, 3]),
170+
size: const Size(40, 20),
171+
);
172+
173+
expect(
174+
descriptor.toJson(),
175+
equals(<Object>[
176+
'fromBytes',
177+
<int>[1, 2, 3],
178+
]));
179+
}, skip: kIsWeb);
180+
181+
test('with size, on the web, size is preserved', () {
182+
final BitmapDescriptor descriptor = BitmapDescriptor.fromBytes(
183+
Uint8List.fromList(<int>[1, 2, 3]),
184+
size: const Size(40, 20),
185+
);
186+
187+
expect(
188+
descriptor.toJson(),
189+
equals(<Object>[
190+
'fromBytes',
191+
<int>[1, 2, 3],
192+
<int>[40, 20],
193+
]));
194+
}, skip: !kIsWeb);
195+
});
196+
145197
group('fromJson validation', () {
146198
group('type validation', () {
147199
test('correct type', () {
148200
expect(BitmapDescriptor.fromJson(<dynamic>['defaultMarker']),
149201
isA<BitmapDescriptor>());
150202
});
203+
151204
test('wrong type', () {
152205
expect(() {
153206
BitmapDescriptor.fromJson(<dynamic>['bogusType']);
@@ -159,15 +212,18 @@ void main() {
159212
expect(BitmapDescriptor.fromJson(<dynamic>['defaultMarker']),
160213
isA<BitmapDescriptor>());
161214
});
215+
162216
test('hue is number', () {
163217
expect(BitmapDescriptor.fromJson(<dynamic>['defaultMarker', 158]),
164218
isA<BitmapDescriptor>());
165219
});
220+
166221
test('hue is not number', () {
167222
expect(() {
168223
BitmapDescriptor.fromJson(<dynamic>['defaultMarker', 'nope']);
169224
}, throwsAssertionError);
170225
});
226+
171227
test('hue is out of range', () {
172228
expect(() {
173229
BitmapDescriptor.fromJson(<dynamic>['defaultMarker', -1]);
@@ -186,6 +242,7 @@ void main() {
186242
]),
187243
isA<BitmapDescriptor>());
188244
});
245+
189246
test('without bytes', () {
190247
expect(() {
191248
BitmapDescriptor.fromJson(<dynamic>['fromBytes', null]);
@@ -202,6 +259,7 @@ void main() {
202259
<dynamic>['fromAsset', 'some/path.png']),
203260
isA<BitmapDescriptor>());
204261
});
262+
205263
test('name cannot be null or empty', () {
206264
expect(() {
207265
BitmapDescriptor.fromJson(<dynamic>['fromAsset', null]);
@@ -210,12 +268,14 @@ void main() {
210268
BitmapDescriptor.fromJson(<dynamic>['fromAsset', '']);
211269
}, throwsAssertionError);
212270
});
271+
213272
test('package is passed', () {
214273
expect(
215274
BitmapDescriptor.fromJson(
216275
<dynamic>['fromAsset', 'some/path.png', 'some_package']),
217276
isA<BitmapDescriptor>());
218277
});
278+
219279
test('package cannot be null or empty', () {
220280
expect(() {
221281
BitmapDescriptor.fromJson(
@@ -262,6 +322,7 @@ void main() {
262322
BitmapDescriptor.fromJson(<dynamic>['fromAssetImage', '', 1.0]);
263323
}, throwsAssertionError);
264324
});
325+
265326
test('dpi must be number', () {
266327
expect(() {
267328
BitmapDescriptor.fromJson(
@@ -272,6 +333,7 @@ void main() {
272333
<dynamic>['fromAssetImage', 'some/path.png', 'one']);
273334
}, throwsAssertionError);
274335
});
336+
275337
test('with optional [width, height] List', () {
276338
expect(
277339
BitmapDescriptor.fromJson(<dynamic>[
@@ -282,6 +344,7 @@ void main() {
282344
]),
283345
isA<BitmapDescriptor>());
284346
});
347+
285348
test(
286349
'optional [width, height] List cannot be null or not contain 2 elements',
287350
() {
@@ -303,6 +366,124 @@ void main() {
303366
}, throwsAssertionError);
304367
});
305368
});
369+
370+
group('bytes', () {
371+
test('with bytes', () {
372+
expect(
373+
BitmapDescriptor.fromJson(<dynamic>[
374+
'bytes',
375+
Uint8List.fromList(<int>[1, 2, 3]),
376+
'auto',
377+
3.0,
378+
]),
379+
isA<BitmapDescriptor>());
380+
});
381+
382+
test('without bytes', () {
383+
expect(() {
384+
BitmapDescriptor.fromJson(<dynamic>['bytes', null, 'auto', 3.0]);
385+
}, throwsAssertionError);
386+
expect(() {
387+
BitmapDescriptor.fromJson(
388+
<dynamic>['bytes', <dynamic>[], 'auto', 3.0]);
389+
}, throwsAssertionError);
390+
});
391+
392+
test(
393+
'optional [width, height] List cannot be null or not contain 2 elements',
394+
() {
395+
expect(() {
396+
BitmapDescriptor.fromJson(
397+
<dynamic>['bytes', <dynamic>[], 'auto', 3.0, null]);
398+
}, throwsAssertionError);
399+
expect(() {
400+
BitmapDescriptor.fromJson(
401+
<dynamic>['bytes', <dynamic>[], 'auto', 3.0, <dynamic>[]]);
402+
}, throwsAssertionError);
403+
expect(() {
404+
BitmapDescriptor.fromJson(<dynamic>[
405+
'bytes',
406+
<dynamic>[],
407+
'auto',
408+
3.0,
409+
<dynamic>[640, 480, 1024]
410+
]);
411+
}, throwsAssertionError);
412+
});
413+
});
414+
415+
group('asset', () {
416+
test('name and dpi passed', () {
417+
expect(
418+
BitmapDescriptor.fromJson(
419+
<dynamic>['asset', 'some/path.png', 'auto', 1.0]),
420+
isA<BitmapDescriptor>());
421+
});
422+
423+
test('name cannot be null or empty', () {
424+
expect(() {
425+
BitmapDescriptor.fromJson(<dynamic>['asset', null, 'auto', 1.0]);
426+
}, throwsAssertionError);
427+
expect(() {
428+
BitmapDescriptor.fromJson(<dynamic>['asset', '', 'auto', 1.0]);
429+
}, throwsAssertionError);
430+
});
431+
432+
test('dpi must be number', () {
433+
expect(() {
434+
BitmapDescriptor.fromJson(
435+
<dynamic>['asset', 'auto', 'some/path.png', null]);
436+
}, throwsAssertionError);
437+
expect(() {
438+
BitmapDescriptor.fromJson(
439+
<dynamic>['asset', 'auto', 'some/path.png', 'one']);
440+
}, throwsAssertionError);
441+
});
442+
443+
test('with optional [width, height] List', () {
444+
expect(
445+
BitmapDescriptor.fromJson(<dynamic>[
446+
'asset',
447+
'some/path.png',
448+
'auto',
449+
1.0,
450+
<dynamic>[640, 480]
451+
]),
452+
isA<BitmapDescriptor>());
453+
});
454+
455+
test(
456+
'optional [width, height] List cannot be null or not contain 2 elements',
457+
() {
458+
expect(() {
459+
BitmapDescriptor.fromJson(<dynamic>[
460+
'fromAssetImage',
461+
'some/path.png',
462+
'auto',
463+
1.0,
464+
null
465+
]);
466+
}, throwsAssertionError);
467+
expect(() {
468+
BitmapDescriptor.fromJson(<dynamic>[
469+
'fromAssetImage',
470+
'some/path.png',
471+
'auto',
472+
1.0,
473+
<dynamic>[]
474+
]);
475+
}, throwsAssertionError);
476+
expect(() {
477+
BitmapDescriptor.fromJson(<dynamic>[
478+
'asset',
479+
'some/path.png',
480+
'auto',
481+
1.0,
482+
<dynamic>[640, 480, 1024]
483+
]);
484+
}, throwsAssertionError);
485+
});
486+
});
306487
});
307488
});
308489
}

0 commit comments

Comments
 (0)