|
| 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_ios; |
| 6 | +@import google_maps_flutter_ios.Test; |
| 7 | +@import XCTest; |
| 8 | +@import GoogleMaps; |
| 9 | + |
| 10 | +#import <OCMock/OCMock.h> |
| 11 | +#import <google_maps_flutter_ios/messages.g.h> |
| 12 | +#import "PartiallyMockedMapView.h" |
| 13 | + |
| 14 | +@interface GoogleMapsMarkerControllerTests : XCTestCase |
| 15 | +@end |
| 16 | + |
| 17 | +@implementation GoogleMapsMarkerControllerTests |
| 18 | + |
| 19 | +/// Returns a mocked map view for use with marker controllers. |
| 20 | +- (GMSMapView *)mockedMapView { |
| 21 | + GMSMapViewOptions *mapViewOptions = [[GMSMapViewOptions alloc] init]; |
| 22 | + mapViewOptions.frame = CGRectMake(0, 0, 100, 100); |
| 23 | + mapViewOptions.camera = [[GMSCameraPosition alloc] initWithLatitude:0 longitude:0 zoom:0]; |
| 24 | + return [[PartiallyMockedMapView alloc] initWithOptions:mapViewOptions]; |
| 25 | +} |
| 26 | + |
| 27 | +/// Returns a FLTMarkersController instance instantiated with the given map view. |
| 28 | +/// |
| 29 | +/// The mapView should outlive the controller, as the controller keeps a weak reference to it. |
| 30 | +- (FLTMarkersController *)markersControllerWithMapView:(GMSMapView *)mapView { |
| 31 | + NSObject<FlutterPluginRegistrar> *mockRegistrar = |
| 32 | + OCMStrictProtocolMock(@protocol(FlutterPluginRegistrar)); |
| 33 | + return [[FLTMarkersController alloc] initWithMapView:mapView |
| 34 | + callbackHandler:[[FGMMapsCallbackApi alloc] init] |
| 35 | + clusterManagersController:nil |
| 36 | + registrar:mockRegistrar]; |
| 37 | +} |
| 38 | + |
| 39 | +- (FGMPlatformBitmap *)placeholderBitmap { |
| 40 | + return [FGMPlatformBitmap makeWithBitmap:[FGMPlatformBitmapDefaultMarker makeWithHue:@0]]; |
| 41 | +} |
| 42 | + |
| 43 | +- (void)testSetsMarkerNumericProperties { |
| 44 | + GMSMapView *mapView = [self mockedMapView]; |
| 45 | + FLTMarkersController *controller = [self markersControllerWithMapView:mapView]; |
| 46 | + |
| 47 | + NSString *markerIdentifier = @"marker"; |
| 48 | + double anchorX = 3.14; |
| 49 | + double anchorY = 2.718; |
| 50 | + double alpha = 0.4; |
| 51 | + double rotation = 90.0; |
| 52 | + double zIndex = 3.0; |
| 53 | + double latitutde = 10.0; |
| 54 | + double longitude = 20.0; |
| 55 | + [controller addMarkers:@[ [FGMPlatformMarker |
| 56 | + makeWithAlpha:alpha |
| 57 | + anchor:[FGMPlatformPoint makeWithX:anchorX y:anchorY] |
| 58 | + consumeTapEvents:YES |
| 59 | + draggable:YES |
| 60 | + flat:YES |
| 61 | + icon:[self placeholderBitmap] |
| 62 | + infoWindow:[FGMPlatformInfoWindow |
| 63 | + makeWithTitle:@"info title" |
| 64 | + snippet:@"info snippet" |
| 65 | + anchor:[FGMPlatformPoint makeWithX:0 y:0]] |
| 66 | + position:[FGMPlatformLatLng makeWithLatitude:latitutde |
| 67 | + longitude:longitude] |
| 68 | + rotation:rotation |
| 69 | + visible:YES |
| 70 | + zIndex:zIndex |
| 71 | + markerId:markerIdentifier |
| 72 | + clusterManagerId:nil] ]]; |
| 73 | + |
| 74 | + FLTGoogleMapMarkerController *markerController = |
| 75 | + controller.markerIdentifierToController[markerIdentifier]; |
| 76 | + GMSMarker *marker = markerController.marker; |
| 77 | + |
| 78 | + const double delta = 0.0001; |
| 79 | + XCTAssertEqualWithAccuracy(marker.opacity, alpha, delta); |
| 80 | + XCTAssertEqualWithAccuracy(marker.rotation, rotation, delta); |
| 81 | + XCTAssertEqualWithAccuracy(marker.zIndex, zIndex, delta); |
| 82 | + XCTAssertEqualWithAccuracy(marker.groundAnchor.x, anchorX, delta); |
| 83 | + XCTAssertEqualWithAccuracy(marker.groundAnchor.y, anchorY, delta); |
| 84 | + XCTAssertEqualWithAccuracy(marker.position.latitude, latitutde, delta); |
| 85 | + XCTAssertEqualWithAccuracy(marker.position.longitude, longitude, delta); |
| 86 | +} |
| 87 | + |
| 88 | +// Boolean properties are tested individually to ensure they aren't accidentally cross-assigned from |
| 89 | +// another property. |
| 90 | +- (void)testSetsDraggable { |
| 91 | + GMSMapView *mapView = [self mockedMapView]; |
| 92 | + FLTMarkersController *controller = [self markersControllerWithMapView:mapView]; |
| 93 | + |
| 94 | + NSString *markerIdentifier = @"marker"; |
| 95 | + [controller addMarkers:@[ [FGMPlatformMarker |
| 96 | + makeWithAlpha:1.0 |
| 97 | + anchor:[FGMPlatformPoint makeWithX:0 y:0] |
| 98 | + consumeTapEvents:NO |
| 99 | + draggable:YES |
| 100 | + flat:NO |
| 101 | + icon:[self placeholderBitmap] |
| 102 | + infoWindow:[FGMPlatformInfoWindow |
| 103 | + makeWithTitle:@"info title" |
| 104 | + snippet:@"info snippet" |
| 105 | + anchor:[FGMPlatformPoint makeWithX:0 y:0]] |
| 106 | + position:[FGMPlatformLatLng makeWithLatitude:0.0 longitude:0.0] |
| 107 | + rotation:0 |
| 108 | + visible:NO |
| 109 | + zIndex:0 |
| 110 | + markerId:markerIdentifier |
| 111 | + clusterManagerId:nil] ]]; |
| 112 | + |
| 113 | + FLTGoogleMapMarkerController *markerController = |
| 114 | + controller.markerIdentifierToController[markerIdentifier]; |
| 115 | + GMSMarker *marker = markerController.marker; |
| 116 | + |
| 117 | + XCTAssertTrue(marker.draggable); |
| 118 | +} |
| 119 | + |
| 120 | +// Boolean properties are tested individually to ensure they aren't accidentally cross-assigned from |
| 121 | +// another property. |
| 122 | +- (void)testSetsFlat { |
| 123 | + GMSMapView *mapView = [self mockedMapView]; |
| 124 | + FLTMarkersController *controller = [self markersControllerWithMapView:mapView]; |
| 125 | + |
| 126 | + NSString *markerIdentifier = @"marker"; |
| 127 | + [controller addMarkers:@[ [FGMPlatformMarker |
| 128 | + makeWithAlpha:1.0 |
| 129 | + anchor:[FGMPlatformPoint makeWithX:0 y:0] |
| 130 | + consumeTapEvents:NO |
| 131 | + draggable:NO |
| 132 | + flat:YES |
| 133 | + icon:[self placeholderBitmap] |
| 134 | + infoWindow:[FGMPlatformInfoWindow |
| 135 | + makeWithTitle:@"info title" |
| 136 | + snippet:@"info snippet" |
| 137 | + anchor:[FGMPlatformPoint makeWithX:0 y:0]] |
| 138 | + position:[FGMPlatformLatLng makeWithLatitude:0.0 longitude:0.0] |
| 139 | + rotation:0 |
| 140 | + visible:NO |
| 141 | + zIndex:0 |
| 142 | + markerId:markerIdentifier |
| 143 | + clusterManagerId:nil] ]]; |
| 144 | + |
| 145 | + FLTGoogleMapMarkerController *markerController = |
| 146 | + controller.markerIdentifierToController[markerIdentifier]; |
| 147 | + GMSMarker *marker = markerController.marker; |
| 148 | + |
| 149 | + XCTAssertTrue(marker.flat); |
| 150 | +} |
| 151 | + |
| 152 | +// Boolean properties are tested individually to ensure they aren't accidentally cross-assigned from |
| 153 | +// another property. |
| 154 | +- (void)testSetsVisible { |
| 155 | + GMSMapView *mapView = [self mockedMapView]; |
| 156 | + FLTMarkersController *controller = [self markersControllerWithMapView:mapView]; |
| 157 | + |
| 158 | + NSString *markerIdentifier = @"marker"; |
| 159 | + [controller addMarkers:@[ [FGMPlatformMarker |
| 160 | + makeWithAlpha:1.0 |
| 161 | + anchor:[FGMPlatformPoint makeWithX:0 y:0] |
| 162 | + consumeTapEvents:NO |
| 163 | + draggable:NO |
| 164 | + flat:NO |
| 165 | + icon:[self placeholderBitmap] |
| 166 | + infoWindow:[FGMPlatformInfoWindow |
| 167 | + makeWithTitle:@"info title" |
| 168 | + snippet:@"info snippet" |
| 169 | + anchor:[FGMPlatformPoint makeWithX:0 y:0]] |
| 170 | + position:[FGMPlatformLatLng makeWithLatitude:0.0 longitude:0.0] |
| 171 | + rotation:0 |
| 172 | + visible:YES |
| 173 | + zIndex:0 |
| 174 | + markerId:markerIdentifier |
| 175 | + clusterManagerId:nil] ]]; |
| 176 | + |
| 177 | + FLTGoogleMapMarkerController *markerController = |
| 178 | + controller.markerIdentifierToController[markerIdentifier]; |
| 179 | + GMSMarker *marker = markerController.marker; |
| 180 | + |
| 181 | + // Visibility is controlled by being set to a map. |
| 182 | + XCTAssertNotNil(marker.map); |
| 183 | +} |
| 184 | + |
| 185 | +- (void)testSetsMarkerInfoWindowProperties { |
| 186 | + GMSMapView *mapView = [self mockedMapView]; |
| 187 | + FLTMarkersController *controller = [self markersControllerWithMapView:mapView]; |
| 188 | + |
| 189 | + NSString *markerIdentifier = @"marker"; |
| 190 | + NSString *title = @"info title"; |
| 191 | + NSString *snippet = @"info snippet"; |
| 192 | + double anchorX = 3.14; |
| 193 | + double anchorY = 2.718; |
| 194 | + [controller |
| 195 | + addMarkers:@[ [FGMPlatformMarker |
| 196 | + makeWithAlpha:1.0 |
| 197 | + anchor:[FGMPlatformPoint makeWithX:0 y:0] |
| 198 | + consumeTapEvents:YES |
| 199 | + draggable:YES |
| 200 | + flat:YES |
| 201 | + icon:[self placeholderBitmap] |
| 202 | + infoWindow:[FGMPlatformInfoWindow |
| 203 | + makeWithTitle:title |
| 204 | + snippet:snippet |
| 205 | + anchor:[FGMPlatformPoint makeWithX:anchorX |
| 206 | + y:anchorY]] |
| 207 | + position:[FGMPlatformLatLng makeWithLatitude:0 longitude:0] |
| 208 | + rotation:0 |
| 209 | + visible:YES |
| 210 | + zIndex:0 |
| 211 | + markerId:markerIdentifier |
| 212 | + clusterManagerId:nil] ]]; |
| 213 | + |
| 214 | + FLTGoogleMapMarkerController *markerController = |
| 215 | + controller.markerIdentifierToController[markerIdentifier]; |
| 216 | + GMSMarker *marker = markerController.marker; |
| 217 | + |
| 218 | + const double delta = 0.0001; |
| 219 | + XCTAssertEqualWithAccuracy(marker.infoWindowAnchor.x, anchorX, delta); |
| 220 | + XCTAssertEqualWithAccuracy(marker.infoWindowAnchor.y, anchorY, delta); |
| 221 | + XCTAssertEqual(marker.title, title); |
| 222 | + XCTAssertEqual(marker.snippet, snippet); |
| 223 | +} |
| 224 | + |
| 225 | +@end |
0 commit comments