|
| 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 <Flutter/Flutter.h> |
| 11 | +#import <OCMock/OCMock.h> |
| 12 | +#import "PartiallyMockedMapView.h" |
| 13 | + |
| 14 | +@interface FGMClusterManagersControllerTests : XCTestCase |
| 15 | +@end |
| 16 | + |
| 17 | +@implementation FGMClusterManagersControllerTests |
| 18 | + |
| 19 | +- (void)testClustering { |
| 20 | + NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar)); |
| 21 | + CGRect frame = CGRectMake(0, 0, 100, 100); |
| 22 | + |
| 23 | + GMSMapViewOptions *mapViewOptions = [[GMSMapViewOptions alloc] init]; |
| 24 | + mapViewOptions.frame = frame; |
| 25 | + mapViewOptions.camera = [[GMSCameraPosition alloc] initWithLatitude:0 longitude:0 zoom:0]; |
| 26 | + |
| 27 | + PartiallyMockedMapView *mapView = [[PartiallyMockedMapView alloc] initWithOptions:mapViewOptions]; |
| 28 | + |
| 29 | + id handler = OCMClassMock([FGMMapsCallbackApi class]); |
| 30 | + |
| 31 | + FGMClusterManagersController *clusterManagersController = |
| 32 | + [[FGMClusterManagersController alloc] initWithMapView:mapView callbackHandler:handler]; |
| 33 | + |
| 34 | + FLTMarkersController *markersController = |
| 35 | + [[FLTMarkersController alloc] initWithMapView:mapView |
| 36 | + callbackHandler:handler |
| 37 | + clusterManagersController:clusterManagersController |
| 38 | + registrar:registrar]; |
| 39 | + |
| 40 | + // Add cluster managers. |
| 41 | + NSString *clusterManagerId = @"cm"; |
| 42 | + FGMPlatformClusterManager *clusterManagerToAdd = |
| 43 | + [FGMPlatformClusterManager makeWithIdentifier:clusterManagerId]; |
| 44 | + [clusterManagersController addClusterManagers:@[ clusterManagerToAdd ]]; |
| 45 | + |
| 46 | + // Add cluster managers in JSON format. |
| 47 | + NSString *JSONClusterManagerId = @"json_cm"; |
| 48 | + NSDictionary *JSONclusterManagerToAdd = @{@"clusterManagerId" : JSONClusterManagerId}; |
| 49 | + [clusterManagersController addJSONClusterManagers:@[ JSONclusterManagerToAdd ]]; |
| 50 | + |
| 51 | + // Verify that cluster managers are available |
| 52 | + GMUClusterManager *clusterManager = |
| 53 | + [clusterManagersController clusterManagerWithIdentifier:clusterManagerId]; |
| 54 | + XCTAssertNotNil(clusterManager, @"Cluster Manager should not be nil"); |
| 55 | + GMUClusterManager *JSONClusterManager = |
| 56 | + [clusterManagersController clusterManagerWithIdentifier:JSONClusterManagerId]; |
| 57 | + XCTAssertNotNil(JSONClusterManager, @"Cluster Manager should not be nil"); |
| 58 | + |
| 59 | + // Add markers |
| 60 | + NSString *markerId1 = @"m1"; |
| 61 | + NSString *markerId2 = @"m2"; |
| 62 | + |
| 63 | + FGMPlatformMarker *marker1 = [FGMPlatformMarker makeWithJson:@{ |
| 64 | + @"markerId" : markerId1, |
| 65 | + @"position" : @[ @0, @0 ], |
| 66 | + @"clusterManagerId" : clusterManagerId |
| 67 | + }]; |
| 68 | + NSDictionary *marker2 = |
| 69 | + @{@"markerId" : markerId2, @"position" : @[ @0, @0 ], @"clusterManagerId" : clusterManagerId}; |
| 70 | + |
| 71 | + [markersController addMarkers:@[ marker1 ]]; |
| 72 | + [markersController addJSONMarkers:@[ marker2 ]]; |
| 73 | + |
| 74 | + FlutterError *error = nil; |
| 75 | + |
| 76 | + // Invoke clustering |
| 77 | + [clusterManagersController invokeClusteringForEachClusterManager]; |
| 78 | + |
| 79 | + // Verify that the markers were added to the cluster manager |
| 80 | + NSArray<FGMPlatformCluster *> *clusters1 = |
| 81 | + [clusterManagersController clustersWithIdentifier:clusterManagerId error:&error]; |
| 82 | + XCTAssertNil(error, @"Error should be nil"); |
| 83 | + for (FGMPlatformCluster *cluster in clusters1) { |
| 84 | + NSString *cmId = cluster.clusterManagerId; |
| 85 | + XCTAssertNotNil(cmId, @"Cluster Manager Identifier should not be nil"); |
| 86 | + if ([cmId isEqualToString:clusterManagerId]) { |
| 87 | + NSArray *markerIds = cluster.markerIds; |
| 88 | + XCTAssertEqual(markerIds.count, 2, @"Cluster should contain two marker"); |
| 89 | + XCTAssertTrue([markerIds containsObject:markerId1], @"Cluster should contain markerId1"); |
| 90 | + XCTAssertTrue([markerIds containsObject:markerId2], @"Cluster should contain markerId2"); |
| 91 | + return; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + [markersController removeMarkersWithIdentifiers:@[ markerId2 ]]; |
| 96 | + |
| 97 | + // Verify that the marker2 is removed from the clusterManager |
| 98 | + NSArray<FGMPlatformCluster *> *clusters2 = |
| 99 | + [clusterManagersController clustersWithIdentifier:clusterManagerId error:&error]; |
| 100 | + XCTAssertNil(error, @"Error should be nil"); |
| 101 | + |
| 102 | + for (FGMPlatformCluster *cluster in clusters2) { |
| 103 | + NSString *cmId = cluster.clusterManagerId; |
| 104 | + XCTAssertNotNil(cmId, @"Cluster Manager ID should not be nil"); |
| 105 | + if ([cmId isEqualToString:clusterManagerId]) { |
| 106 | + NSArray *markerIds = cluster.markerIds; |
| 107 | + XCTAssertEqual(markerIds.count, 1, @"Cluster should contain one marker"); |
| 108 | + XCTAssertTrue([markerIds containsObject:markerId1], @"Cluster should contain markerId1"); |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + [markersController removeMarkersWithIdentifiers:@[ markerId1 ]]; |
| 114 | + |
| 115 | + // Verify that all markers are removed from clusterManager |
| 116 | + NSArray<FGMPlatformCluster *> *clusters3 = |
| 117 | + [clusterManagersController clustersWithIdentifier:clusterManagerId error:&error]; |
| 118 | + XCTAssertNil(error, @"Error should be nil"); |
| 119 | + XCTAssertEqual(clusters3.count, 0, @"Cluster Manager should not contain any clusters"); |
| 120 | + |
| 121 | + // Remove cluster manager |
| 122 | + [clusterManagersController removeClusterManagersWithIdentifiers:@[ clusterManagerId ]]; |
| 123 | + |
| 124 | + // Verify that the cluster manager is removed |
| 125 | + clusterManager = [clusterManagersController clusterManagerWithIdentifier:clusterManagerId]; |
| 126 | + XCTAssertNil(clusterManager, @"Cluster Manager should be nil"); |
| 127 | +} |
| 128 | + |
| 129 | +@end |
0 commit comments