|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +@import google_maps_flutter; |
| 6 | +@import google_maps_flutter.Test; |
| 7 | +@import XCTest; |
| 8 | +@import MapKit; |
| 9 | +@import GoogleMaps; |
| 10 | + |
| 11 | +#import <OCMock/OCMock.h> |
| 12 | +#import "PartiallyMockedMapView.h" |
| 13 | + |
| 14 | +@interface FLTGoogleMapJSONConversionsTests : XCTestCase |
| 15 | +@end |
| 16 | + |
| 17 | +@implementation FLTGoogleMapJSONConversionsTests |
| 18 | + |
| 19 | +- (void)testLocationFromLatLong { |
| 20 | + NSArray<NSNumber *> *latlong = @[ @1, @2 ]; |
| 21 | + CLLocationCoordinate2D location = [FLTGoogleMapJSONConversions locationFromLatLong:latlong]; |
| 22 | + XCTAssertEqual(location.latitude, 1); |
| 23 | + XCTAssertEqual(location.longitude, 2); |
| 24 | +} |
| 25 | + |
| 26 | +- (void)testPointFromArray { |
| 27 | + NSArray<NSNumber *> *array = @[ @1, @2 ]; |
| 28 | + CGPoint point = [FLTGoogleMapJSONConversions pointFromArray:array]; |
| 29 | + XCTAssertEqual(point.x, 1); |
| 30 | + XCTAssertEqual(point.y, 2); |
| 31 | +} |
| 32 | + |
| 33 | +- (void)testArrayFromLocation { |
| 34 | + CLLocationCoordinate2D location = CLLocationCoordinate2DMake(1, 2); |
| 35 | + NSArray<NSNumber *> *array = [FLTGoogleMapJSONConversions arrayFromLocation:location]; |
| 36 | + XCTAssertEqual([array[0] integerValue], 1); |
| 37 | + XCTAssertEqual([array[1] integerValue], 2); |
| 38 | +} |
| 39 | + |
| 40 | +- (void)testColorFromRGBA { |
| 41 | + NSNumber *rgba = @(0x01020304); |
| 42 | + UIColor *color = [FLTGoogleMapJSONConversions colorFromRGBA:rgba]; |
| 43 | + CGFloat red, green, blue, alpha; |
| 44 | + BOOL success = [color getRed:&red green:&green blue:&blue alpha:&alpha]; |
| 45 | + XCTAssertTrue(success); |
| 46 | + const CGFloat accuracy = 0.0001; |
| 47 | + XCTAssertEqualWithAccuracy(red, 2 / 255.0, accuracy); |
| 48 | + XCTAssertEqualWithAccuracy(green, 3 / 255.0, accuracy); |
| 49 | + XCTAssertEqualWithAccuracy(blue, 4 / 255.0, accuracy); |
| 50 | + XCTAssertEqualWithAccuracy(alpha, 1 / 255.0, accuracy); |
| 51 | +} |
| 52 | + |
| 53 | +- (void)testPointsFromLatLongs { |
| 54 | + NSArray<NSArray *> *latlongs = @[ @[ @1, @2 ], @[ @(3), @(4) ] ]; |
| 55 | + NSArray<CLLocation *> *locations = [FLTGoogleMapJSONConversions pointsFromLatLongs:latlongs]; |
| 56 | + XCTAssertEqual(locations.count, 2); |
| 57 | + XCTAssertEqual(locations[0].coordinate.latitude, 1); |
| 58 | + XCTAssertEqual(locations[0].coordinate.longitude, 2); |
| 59 | + XCTAssertEqual(locations[1].coordinate.latitude, 3); |
| 60 | + XCTAssertEqual(locations[1].coordinate.longitude, 4); |
| 61 | +} |
| 62 | + |
| 63 | +- (void)testHolesFromPointsArray { |
| 64 | + NSArray<NSArray *> *pointsArray = |
| 65 | + @[ @[ @[ @1, @2 ], @[ @(3), @(4) ] ], @[ @[ @(5), @(6) ], @[ @(7), @(8) ] ] ]; |
| 66 | + NSArray<NSArray<CLLocation *> *> *holes = |
| 67 | + [FLTGoogleMapJSONConversions holesFromPointsArray:pointsArray]; |
| 68 | + XCTAssertEqual(holes.count, 2); |
| 69 | + XCTAssertEqual(holes[0][0].coordinate.latitude, 1); |
| 70 | + XCTAssertEqual(holes[0][0].coordinate.longitude, 2); |
| 71 | + XCTAssertEqual(holes[0][1].coordinate.latitude, 3); |
| 72 | + XCTAssertEqual(holes[0][1].coordinate.longitude, 4); |
| 73 | + XCTAssertEqual(holes[1][0].coordinate.latitude, 5); |
| 74 | + XCTAssertEqual(holes[1][0].coordinate.longitude, 6); |
| 75 | + XCTAssertEqual(holes[1][1].coordinate.latitude, 7); |
| 76 | + XCTAssertEqual(holes[1][1].coordinate.longitude, 8); |
| 77 | +} |
| 78 | + |
| 79 | +- (void)testDictionaryFromPosition { |
| 80 | + id mockPosition = OCMClassMock([GMSCameraPosition class]); |
| 81 | + NSValue *locationValue = [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(1, 2)]; |
| 82 | + [(GMSCameraPosition *)[[mockPosition stub] andReturnValue:locationValue] target]; |
| 83 | + [[[mockPosition stub] andReturnValue:@(2.0)] zoom]; |
| 84 | + [[[mockPosition stub] andReturnValue:@(3.0)] bearing]; |
| 85 | + [[[mockPosition stub] andReturnValue:@(75.0)] viewingAngle]; |
| 86 | + NSDictionary *dictionary = [FLTGoogleMapJSONConversions dictionaryFromPosition:mockPosition]; |
| 87 | + NSArray *targetArray = @[ @1, @2 ]; |
| 88 | + XCTAssertEqualObjects(dictionary[@"target"], targetArray); |
| 89 | + XCTAssertEqualObjects(dictionary[@"zoom"], @2.0); |
| 90 | + XCTAssertEqualObjects(dictionary[@"bearing"], @3.0); |
| 91 | + XCTAssertEqualObjects(dictionary[@"tilt"], @75.0); |
| 92 | +} |
| 93 | + |
| 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", @{@"target" : @[ @1, @2 ], @"zoom" : @3, @"bearing" : @4, @"tilt" : @5} |
| 172 | + ]; |
| 173 | + id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]); |
| 174 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue]; |
| 175 | + [[classMockCameraUpdate expect] |
| 176 | + setCamera:[FLTGoogleMapJSONConversions cameraPostionFromDictionary:channelValue[1]]]; |
| 177 | + [classMockCameraUpdate stopMocking]; |
| 178 | +} |
| 179 | + |
| 180 | +// TODO(cyanglaz): Fix the test for CameraUpdateFromChannelValue with the "NewLatlng" key. |
| 181 | +// 2 approaches have been tried and neither worked for the tests. |
| 182 | +// |
| 183 | +// 1. Use OCMock to vefiry that [GMSCameraUpdate setTarget:] is triggered with the correct value. |
| 184 | +// This class method conflicts with certain category method in OCMock, causing OCMock not able to |
| 185 | +// disambigious them. |
| 186 | +// |
| 187 | +// 2. Directly verify the GMSCameraUpdate object returned by the method. |
| 188 | +// The GMSCameraUpdate object returned from the method doesn't have any accessors to the "target" |
| 189 | +// property. It can be used to update the "camera" property in GMSMapView. However, [GMSMapView |
| 190 | +// moveCamera:] doesn't update the camera immediately. Thus the GMSCameraUpdate object cannot be |
| 191 | +// verified. |
| 192 | +// |
| 193 | +// The code in below test uses the 2nd approach. |
| 194 | +- (void)skip_testCameraUpdateFromChannelValueNewLatLong { |
| 195 | + NSArray *channelValue = @[ @"newLatLng", @[ @1, @2 ] ]; |
| 196 | + |
| 197 | + GMSCameraUpdate *update = [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue]; |
| 198 | + |
| 199 | + GMSMapView *mapView = [[GMSMapView alloc] |
| 200 | + initWithFrame:CGRectZero |
| 201 | + camera:[GMSCameraPosition cameraWithTarget:CLLocationCoordinate2DMake(5, 6) zoom:1]]; |
| 202 | + [mapView moveCamera:update]; |
| 203 | + const CGFloat accuracy = 0.001; |
| 204 | + XCTAssertEqualWithAccuracy(mapView.camera.target.latitude, 1, |
| 205 | + accuracy); // mapView.camera.target.latitude is still 5. |
| 206 | + XCTAssertEqualWithAccuracy(mapView.camera.target.longitude, 2, |
| 207 | + accuracy); // mapView.camera.target.longitude is still 6. |
| 208 | +} |
| 209 | + |
| 210 | +- (void)testCameraUpdateFromChannelValueNewLatLngBounds { |
| 211 | + NSArray<NSNumber *> *latlong1 = @[ @1, @2 ]; |
| 212 | + NSArray<NSNumber *> *latlong2 = @[ @(3), @(4) ]; |
| 213 | + GMSCoordinateBounds *bounds = |
| 214 | + [FLTGoogleMapJSONConversions coordinateBoundsFromLatLongs:@[ latlong1, latlong2 ]]; |
| 215 | + |
| 216 | + NSArray *channelValue = @[ @"newLatLngBounds", @[ latlong1, latlong2 ], @20 ]; |
| 217 | + id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]); |
| 218 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue]; |
| 219 | + |
| 220 | + [[classMockCameraUpdate expect] fitBounds:bounds withPadding:20]; |
| 221 | + [classMockCameraUpdate stopMocking]; |
| 222 | +} |
| 223 | + |
| 224 | +- (void)testCameraUpdateFromChannelValueNewLatLngZoom { |
| 225 | + NSArray *channelValue = @[ @"newLatLngZoom", @[ @1, @2 ], @3 ]; |
| 226 | + |
| 227 | + id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]); |
| 228 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue]; |
| 229 | + |
| 230 | + [[classMockCameraUpdate expect] setTarget:CLLocationCoordinate2DMake(1, 2) zoom:3]; |
| 231 | + [classMockCameraUpdate stopMocking]; |
| 232 | +} |
| 233 | + |
| 234 | +- (void)testCameraUpdateFromChannelValueScrollBy { |
| 235 | + NSArray *channelValue = @[ @"scrollBy", @1, @2 ]; |
| 236 | + |
| 237 | + id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]); |
| 238 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValue]; |
| 239 | + |
| 240 | + [[classMockCameraUpdate expect] scrollByX:1 Y:2]; |
| 241 | + [classMockCameraUpdate stopMocking]; |
| 242 | +} |
| 243 | + |
| 244 | +- (void)testCameraUpdateFromChannelValueZoomBy { |
| 245 | + NSArray *channelValueNoPoint = @[ @"zoomBy", @1 ]; |
| 246 | + |
| 247 | + id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]); |
| 248 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint]; |
| 249 | + |
| 250 | + [[classMockCameraUpdate expect] zoomBy:1]; |
| 251 | + |
| 252 | + NSArray *channelValueWithPoint = @[ @"zoomBy", @1, @[ @2, @3 ] ]; |
| 253 | + |
| 254 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueWithPoint]; |
| 255 | + |
| 256 | + [[classMockCameraUpdate expect] zoomBy:1 atPoint:CGPointMake(2, 3)]; |
| 257 | + [classMockCameraUpdate stopMocking]; |
| 258 | +} |
| 259 | + |
| 260 | +- (void)testCameraUpdateFromChannelValueZoomIn { |
| 261 | + NSArray *channelValueNoPoint = @[ @"zoomIn" ]; |
| 262 | + |
| 263 | + id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]); |
| 264 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint]; |
| 265 | + |
| 266 | + [[classMockCameraUpdate expect] zoomIn]; |
| 267 | + [classMockCameraUpdate stopMocking]; |
| 268 | +} |
| 269 | + |
| 270 | +- (void)testCameraUpdateFromChannelValueZoomOut { |
| 271 | + NSArray *channelValueNoPoint = @[ @"zoomOut" ]; |
| 272 | + |
| 273 | + id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]); |
| 274 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint]; |
| 275 | + |
| 276 | + [[classMockCameraUpdate expect] zoomOut]; |
| 277 | + [classMockCameraUpdate stopMocking]; |
| 278 | +} |
| 279 | + |
| 280 | +- (void)testCameraUpdateFromChannelValueZoomTo { |
| 281 | + NSArray *channelValueNoPoint = @[ @"zoomTo", @1 ]; |
| 282 | + |
| 283 | + id classMockCameraUpdate = OCMClassMock([GMSCameraUpdate class]); |
| 284 | + [FLTGoogleMapJSONConversions cameraUpdateFromChannelValue:channelValueNoPoint]; |
| 285 | + |
| 286 | + [[classMockCameraUpdate expect] zoomTo:1]; |
| 287 | + [classMockCameraUpdate stopMocking]; |
| 288 | +} |
| 289 | + |
| 290 | +@end |
0 commit comments