Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 5753e7a

Browse files
author
Chris Yang
committed
format, crash, more tests
1 parent 71c1eda commit 5753e7a

File tree

4 files changed

+209
-21
lines changed

4 files changed

+209
-21
lines changed

packages/google_maps_flutter/google_maps_flutter/example/ios/RunnerTests/FLTGoogleMapJSONConversionsConversionTests.m

Lines changed: 200 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@import google_maps_flutter.Test;
77
@import XCTest;
88
@import MapKit;
9+
@import GoogleMaps;
910

1011
#import <OCMock/OCMock.h>
1112
#import "PartiallyMockedMapView.h"
@@ -75,29 +76,216 @@ - (void)testHolesFromPointsArray {
7576
XCTAssertEqual(holes[1][1].coordinate.longitude, 8);
7677
}
7778

78-
- (void)testDictionaryFromPosition{
79+
- (void)testDictionaryFromPosition {
7980
id mockPosition = OCMClassMock([GMSCameraPosition class]);
8081
NSValue *locationValue = [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(1, 2)];
8182
[(GMSCameraPosition *)[[mockPosition stub] andReturnValue:locationValue] target];
8283
[[[mockPosition stub] andReturnValue:@(2.0)] zoom];
8384
[[[mockPosition stub] andReturnValue:@(3.0)] bearing];
8485
[[[mockPosition stub] andReturnValue:@(75.0)] viewingAngle];
8586
NSDictionary *dictionary = [FLTGoogleMapJSONConversions dictionaryFromPosition:mockPosition];
86-
NSArray *targetArray = @[@1, @2];
87+
NSArray *targetArray = @[ @1, @2 ];
8788
XCTAssertEqualObjects(dictionary[@"target"], targetArray);
8889
XCTAssertEqualObjects(dictionary[@"zoom"], @2.0);
8990
XCTAssertEqualObjects(dictionary[@"bearing"], @3.0);
9091
XCTAssertEqualObjects(dictionary[@"tilt"], @75.0);
9192
}
9293

93-
@end
94+
- (void)testDictionaryFromPoint {
95+
CGPoint point = CGPointMake(10, 20);
96+
NSDictionary *dictionary = [FLTGoogleMapJSONConversions dictionaryFromPoint:point];
97+
const CGFloat accuracy = 0.0001;
98+
XCTAssertEqualWithAccuracy([dictionary[@"x"] floatValue], point.x, accuracy);
99+
XCTAssertEqualWithAccuracy([dictionary[@"y"] floatValue], point.y, accuracy);
100+
}
101+
102+
- (void)testDictionaryFromCoordinateBounds {
103+
XCTAssertNil([FLTGoogleMapJSONConversions dictionaryFromCoordinateBounds:nil]);
104+
105+
GMSCoordinateBounds *bounds =
106+
[[GMSCoordinateBounds alloc] initWithCoordinate:CLLocationCoordinate2DMake(10, 20)
107+
coordinate:CLLocationCoordinate2DMake(30, 40)];
108+
NSDictionary *dictionary = [FLTGoogleMapJSONConversions dictionaryFromCoordinateBounds:bounds];
109+
NSArray *southwest = @[ @10, @20 ];
110+
NSArray *northeast = @[ @30, @40 ];
111+
XCTAssertEqualObjects(dictionary[@"southwest"], southwest);
112+
XCTAssertEqualObjects(dictionary[@"northeast"], northeast);
113+
}
114+
115+
- (void)testCameraPostionFromDictionary {
116+
XCTAssertNil([FLTGoogleMapJSONConversions cameraPostionFromDictionary:nil]);
117+
118+
NSDictionary *channelValue =
119+
@{@"target" : @[ @(1), @(2) ], @"zoom" : @3, @"bearing" : @4, @"tilt" : @5};
120+
121+
GMSCameraPosition *cameraPosition =
122+
[FLTGoogleMapJSONConversions cameraPostionFromDictionary:channelValue];
123+
124+
const CGFloat accuracy = 0.001;
125+
XCTAssertEqualWithAccuracy(cameraPosition.target.latitude, 1, accuracy);
126+
XCTAssertEqualWithAccuracy(cameraPosition.target.longitude, 2, accuracy);
127+
XCTAssertEqualWithAccuracy(cameraPosition.zoom, 3, accuracy);
128+
XCTAssertEqualWithAccuracy(cameraPosition.bearing, 4, accuracy);
129+
XCTAssertEqualWithAccuracy(cameraPosition.viewingAngle, 5, accuracy);
130+
}
131+
132+
- (void)testPointFromDictionary {
133+
XCTAssertNil([FLTGoogleMapJSONConversions cameraPostionFromDictionary:nil]);
134+
135+
NSDictionary *dictionary = @{
136+
@"x" : @1,
137+
@"y" : @2,
138+
};
139+
140+
CGPoint point = [FLTGoogleMapJSONConversions pointFromDictionary:dictionary];
141+
142+
const CGFloat accuracy = 0.001;
143+
XCTAssertEqualWithAccuracy(point.x, 1, accuracy);
144+
XCTAssertEqualWithAccuracy(point.y, 2, accuracy);
145+
}
146+
147+
- (void)testCoordinateBoundsFromLatlongs {
148+
NSArray<NSNumber *> *latlong1 = @[ @(1), @(2) ];
149+
NSArray<NSNumber *> *latlong2 = @[ @(3), @(4) ];
150+
151+
GMSCoordinateBounds *bounds =
152+
[FLTGoogleMapJSONConversions coordinateBoundsFromLatlongs:@[ latlong1, latlong2 ]];
153+
154+
const CGFloat accuracy = 0.001;
155+
XCTAssertEqualWithAccuracy(bounds.southWest.latitude, 1, accuracy);
156+
XCTAssertEqualWithAccuracy(bounds.southWest.longitude, 2, accuracy);
157+
XCTAssertEqualWithAccuracy(bounds.northEast.latitude, 3, accuracy);
158+
XCTAssertEqualWithAccuracy(bounds.northEast.longitude, 4, accuracy);
159+
}
160+
161+
- (void)testMapViewTypeFromTypeValue {
162+
XCTAssertEqual(kGMSTypeNormal, [FLTGoogleMapJSONConversions mapViewTypeFromTypeValue:@1]);
163+
XCTAssertEqual(kGMSTypeSatellite, [FLTGoogleMapJSONConversions mapViewTypeFromTypeValue:@2]);
164+
XCTAssertEqual(kGMSTypeTerrain, [FLTGoogleMapJSONConversions mapViewTypeFromTypeValue:@3]);
165+
XCTAssertEqual(kGMSTypeHybrid, [FLTGoogleMapJSONConversions mapViewTypeFromTypeValue:@4]);
166+
XCTAssertEqual(kGMSTypeNone, [FLTGoogleMapJSONConversions mapViewTypeFromTypeValue:@5]);
167+
}
168+
169+
- (void)testCameraUpdateFromChannelValueNewCameraPosition {
170+
NSArray *channelValue = @[
171+
@"newCameraPosition",
172+
@{@"target" : @[ @(1), @(2) ], @"zoom" : @3, @"bearing" : @4, @"tilt" : @5}
173+
];
174+
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
175+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
176+
[[classMockCameraUpdate expect]
177+
setCamera:[FLTGoogleMapJSONConversions cameraPostionFromDictionary:channelValue[1]]];
178+
[classMockCameraUpdate stopMocking];
179+
}
180+
181+
// TODO(cyanglaz): Fix the test for CameraUpdateFromChannelValue with the "NewLatlng" key.
182+
// 2 approaches have been tried and neither worked for the tests.
183+
//
184+
// 1. Use OCMock to vefiry that [GMSCameraUpdate setTarget:] is triggered with the correct value.
185+
// This class method conflicts with certain category method in OCMock, causing OCMock not able to
186+
// disambigious them.
187+
//
188+
// 2. Directly verify the GMSCameraUpdate object returned by the method.
189+
// The GMSCameraUpdate object returned from the method doesn't have any accessors to the "target"
190+
// property. It can be used to update the "camera" property in GMSMapView. However, [GMSMapView
191+
// moveCamera:] doesn't update the camera immediately. Thus the GMSCameraUpdate object cannot be
192+
// verified.
193+
//
194+
// The code in below test uses the 2nd approach.
195+
- (void)skip_testCameraUpdateFromChannelValueNewLatlong {
196+
NSArray *channelValue = @[ @"newLatLng", @[ @1, @2 ] ];
197+
198+
GMSCameraUpdate *update = [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
199+
200+
GMSMapView *mapView = [[GMSMapView alloc]
201+
initWithFrame:CGRectZero
202+
camera:[GMSCameraPosition cameraWithTarget:CLLocationCoordinate2DMake(5, 6) zoom:1]];
203+
[mapView moveCamera:update];
204+
const CGFloat accuracy = 0.001;
205+
XCTAssertEqualWithAccuracy(mapView.camera.target.latitude, 1,
206+
accuracy); // mapView.camera.target.latitude is still 5.
207+
XCTAssertEqualWithAccuracy(mapView.camera.target.longitude, 2,
208+
accuracy); // mapView.camera.target.longitude is still 6.
209+
}
210+
211+
- (void)testCameraUpdateFromChannelValueNewLatLngBounds {
212+
NSArray<NSNumber *> *latlong1 = @[ @(1), @(2) ];
213+
NSArray<NSNumber *> *latlong2 = @[ @(3), @(4) ];
214+
GMSCoordinateBounds *bounds =
215+
[FLTGoogleMapJSONConversions coordinateBoundsFromLatlongs:@[ latlong1, latlong2 ]];
216+
217+
NSArray *channelValue = @[ @"newLatLngBounds", @[ latlong1, latlong2 ], @20 ];
218+
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
219+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
94220

95-
//+ (nullable NSDictionary<NSString *, id> *)dictionaryFromPosition:
96-
// (nullable GMSCameraPosition *)position;
97-
//+ (NSDictionary<NSString *, NSNumber *> *)dictionaryFromPoint:(CGPoint)point;
98-
//+ (nullable NSDictionary *)dictionaryFromCoordinateBounds:(nullable GMSCoordinateBounds *)bounds;
99-
//+ (nullable GMSCameraPosition *)cameraPostionFromDictionary:(nullable NSDictionary *)channelValue;
100-
//+ (CGPoint)pointFromDictionary:(NSDictionary *)dictionary;
101-
//+ (GMSCoordinateBounds *)coordinateBoundsFromLatlong:(NSArray *)latlong;
102-
//+ (GMSMapViewType)mapViewTypeFromTypeValue:(NSNumber *)value;
103-
//+ (nullable GMSCameraUpdate *)cameraUpdateFromChannelValue:(NSArray *)channelValue;
221+
[[classMockCameraUpdate expect] fitBounds:bounds withPadding:20];
222+
[classMockCameraUpdate stopMocking];
223+
}
224+
225+
- (void)testCameraUpdateFromChannelValueNewLatLngZoom {
226+
NSArray *channelValue = @[ @"newLatLngZoom", @[ @1, @2 ], @3 ];
227+
228+
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
229+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
230+
231+
[[classMockCameraUpdate expect] setTarget:CLLocationCoordinate2DMake(1, 2) zoom:3];
232+
[classMockCameraUpdate stopMocking];
233+
}
234+
235+
- (void)testCameraUpdateFromChannelValueScrollBy {
236+
NSArray *channelValue = @[ @"scrollBy", @1, @2 ];
237+
238+
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
239+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue];
240+
241+
[[classMockCameraUpdate expect] scrollByX:1 Y:2];
242+
[classMockCameraUpdate stopMocking];
243+
}
244+
245+
- (void)testCameraUpdateFromChannelValueZoomBy {
246+
NSArray *channelValueNoPoint = @[ @"zoomBy", @1 ];
247+
248+
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
249+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint];
250+
251+
[[classMockCameraUpdate expect] zoomBy:1];
252+
253+
NSArray *channelValueWithPoint = @[ @"zoomBy", @1, @[ @2, @3 ] ];
254+
255+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueWithPoint];
256+
257+
[[classMockCameraUpdate expect] zoomBy:1 atPoint:CGPointMake(2, 3)];
258+
[classMockCameraUpdate stopMocking];
259+
}
260+
261+
- (void)testCameraUpdateFromChannelValueZoomIn {
262+
NSArray *channelValueNoPoint = @[ @"zoomIn" ];
263+
264+
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
265+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint];
266+
267+
[[classMockCameraUpdate expect] zoomIn];
268+
[classMockCameraUpdate stopMocking];
269+
}
270+
271+
- (void)testCameraUpdateFromChannelValueZoomOut {
272+
NSArray *channelValueNoPoint = @[ @"zoomOut" ];
273+
274+
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
275+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint];
276+
277+
[[classMockCameraUpdate expect] zoomOut];
278+
[classMockCameraUpdate stopMocking];
279+
}
280+
281+
- (void)testCameraUpdateFromChannelValueZoomTo {
282+
NSArray *channelValueNoPoint = @[ @"zoomTo", @1 ];
283+
284+
id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]);
285+
[FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint];
286+
287+
[[classMockCameraUpdate expect] zoomTo:1];
288+
[classMockCameraUpdate stopMocking];
289+
}
290+
291+
@end

packages/google_maps_flutter/google_maps_flutter/ios/Classes/GoogleMapController.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ - (void)setTiltGesturesEnabled:(BOOL)enabled {
456456
}
457457

458458
- (void)setTrackCameraPosition:(BOOL)enabled {
459-
self.trackCameraPosition = enabled;
459+
_trackCameraPosition = enabled;
460460
}
461461

462462
- (void)setZoomGesturesEnabled:(BOOL)enabled {
@@ -554,11 +554,11 @@ - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordi
554554

555555
+ (void)interpretMapOptions:(NSDictionary *)data sink:(id<FLTGoogleMapOptionsSink>)sink {
556556
NSArray *cameraTargetBounds = data[@"cameraTargetBounds"];
557-
if (cameraTargetBounds) {
557+
if (cameraTargetBounds && cameraTargetBounds != (id)[NSNull null]) {
558558
[sink
559-
setCameraTargetBounds:cameraTargetBounds.count > 0
559+
setCameraTargetBounds:cameraTargetBounds.count > 0 && cameraTargetBounds[0] != [NSNull null]
560560
? [FLTGoogleMapJSONConversions
561-
coordinateBoundsFromLatlong:cameraTargetBounds.firstObject]
561+
coordinateBoundsFromLatlongs:cameraTargetBounds.firstObject]
562562
: nil];
563563
}
564564
NSNumber *compassEnabled = data[@"compassEnabled"];

packages/google_maps_flutter/google_maps_flutter/ios/Classes/JsonConversions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
2121
+ (nullable NSDictionary *)dictionaryFromCoordinateBounds:(nullable GMSCoordinateBounds *)bounds;
2222
+ (nullable GMSCameraPosition *)cameraPostionFromDictionary:(nullable NSDictionary *)channelValue;
2323
+ (CGPoint)pointFromDictionary:(NSDictionary *)dictionary;
24-
+ (GMSCoordinateBounds *)coordinateBoundsFromLatlong:(NSArray *)latlong;
24+
+ (GMSCoordinateBounds *)coordinateBoundsFromLatlongs:(NSArray *)latlongs;
2525
+ (GMSMapViewType)mapViewTypeFromTypeValue:(NSNumber *)value;
2626
+ (nullable GMSCameraUpdate *)cameraUpdateFromChannelValue:(NSArray *)channelValue;
2727

packages/google_maps_flutter/google_maps_flutter/ios/Classes/JsonConversions.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ + (CGPoint)pointFromDictionary:(NSDictionary *)dictionary {
9595
return CGPointMake(x, y);
9696
}
9797

98-
+ (GMSCoordinateBounds *)coordinateBoundsFromLatlong:(NSArray *)latlong {
98+
+ (GMSCoordinateBounds *)coordinateBoundsFromLatlongs:(NSArray *)latlongs {
9999
return [[GMSCoordinateBounds alloc]
100-
initWithCoordinate:[FLTGoogleMapJSONConversions locationFromLatlong:latlong[0]]
101-
coordinate:[FLTGoogleMapJSONConversions locationFromLatlong:latlong[1]]];
100+
initWithCoordinate:[FLTGoogleMapJSONConversions locationFromLatlong:latlongs[0]]
101+
coordinate:[FLTGoogleMapJSONConversions locationFromLatlong:latlongs[1]]];
102102
}
103103

104104
+ (GMSMapViewType)mapViewTypeFromTypeValue:(NSNumber *)typeValue {
@@ -116,7 +116,7 @@ + (nullable GMSCameraUpdate *)cameraUpdateFromChannelValue:(NSArray *)channelVal
116116
setTarget:[FLTGoogleMapJSONConversions locationFromLatlong:channelValue[1]]];
117117
} else if ([update isEqualToString:@"newLatLngBounds"]) {
118118
return [GMSCameraUpdate
119-
fitBounds:[FLTGoogleMapJSONConversions coordinateBoundsFromLatlong:channelValue[1]]
119+
fitBounds:[FLTGoogleMapJSONConversions coordinateBoundsFromLatlongs:channelValue[1]]
120120
withPadding:[channelValue[2] doubleValue]];
121121
} else if ([update isEqualToString:@"newLatLngZoom"]) {
122122
return

0 commit comments

Comments
 (0)