Skip to content

Commit

Permalink
Add hasRegisteredMapBitmap method to maps inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
aednlaxer committed Feb 14, 2025
1 parent 9799b6a commit dcf3876
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1100,4 +1100,10 @@ public Boolean isLiteModeEnabled() {
}
return data;
}

@NonNull
@Override
public Boolean hasRegisteredMapBitmap(@NonNull Long id) {
return imageRegistry.getBitmap(id) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public void clearBitmapCache() {
registry.clear();
}


/**
* Retrieves a BitmapDescriptor from the registry using the provided ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7440,6 +7440,9 @@ public interface MapsInspectorApi {
@NonNull
List<PlatformCluster> getClusters(@NonNull String clusterManagerId);

@NonNull
Boolean hasRegisteredMapBitmap(@NonNull Long id);

/** The codec used by MapsInspectorApi. */
static @NonNull MessageCodec<Object> getCodec() {
return PigeonCodec.INSTANCE;
Expand Down Expand Up @@ -7782,6 +7785,31 @@ static void setUp(
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger,
"dev.flutter.pigeon.google_maps_flutter_android.MapsInspectorApi.hasRegisteredMapBitmap"
+ messageChannelSuffix,
getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<>();
ArrayList<Object> args = (ArrayList<Object>) message;
Long idArg = (Long) args.get(0);
try {
Boolean output = api.hasRegisteredMapBitmap(idArg);
wrapped.add(0, output);
} catch (Throwable exception) {
wrapped = wrapError(exception);
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:google_maps_flutter_example/example_google_map.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';

void bitmapRegistryTests() {
Expand All @@ -10,7 +14,15 @@ void bitmapRegistryTests() {
imagePixelRatio: 1.0,
);

final int mapId = await _pumpMap(tester);
await GoogleMapsFlutterPlatform.instance.registerBitmap(1, bitmap);
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isTrue,
);
});

testWidgets('Remove bitmap from cache', (WidgetTester tester) async {
Expand All @@ -19,8 +31,24 @@ void bitmapRegistryTests() {
imagePixelRatio: 1.0,
);

final int mapId = await _pumpMap(tester);
await GoogleMapsFlutterPlatform.instance.registerBitmap(1, bitmap);
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isTrue,
);

await GoogleMapsFlutterPlatform.instance.unregisterBitmap(1);
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isFalse,
);
});

testWidgets('Clear bitmap cache', (WidgetTester tester) async {
Expand All @@ -29,8 +57,44 @@ void bitmapRegistryTests() {
imagePixelRatio: 1.0,
);

final int mapId = await _pumpMap(tester);
await GoogleMapsFlutterPlatform.instance.clearBitmapCache();
await GoogleMapsFlutterPlatform.instance.registerBitmap(1, bitmap);
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isTrue,
);

await GoogleMapsFlutterPlatform.instance.clearBitmapCache();
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isFalse,
);
});
}

// Pump a map and return the map ID.
Future<int> _pumpMap(WidgetTester tester) async {
final Completer<ExampleGoogleMapController> controllerCompleter =
Completer<ExampleGoogleMapController>();

await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: ExampleGoogleMap(
initialCameraPosition: const CameraPosition(target: LatLng(0, 0)),
onMapCreated: (ExampleGoogleMapController googleMapController) {
controllerCompleter.complete(googleMapController);
},
),
));

final ExampleGoogleMapController controller =
await controllerCompleter.future;
return controller.mapId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,12 @@ class GoogleMapsInspectorAndroid extends GoogleMapsInspectorPlatform {
GoogleMapsFlutterAndroid.clusterFromPlatformCluster(cluster!))
.toList();
}

@override
Future<bool> hasRegisteredMapBitmap({
required int mapId,
required int bitmapId,
}) {
return _inspectorProvider(mapId)!.hasRegisteredMapBitmap(bitmapId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3440,6 +3440,35 @@ class MapsInspectorApi {
.cast<PlatformCluster>();
}
}

Future<bool> hasRegisteredMapBitmap(int id) async {
final String pigeonVar_channelName =
'dev.flutter.pigeon.google_maps_flutter_android.MapsInspectorApi.hasRegisteredMapBitmap$pigeonVar_messageChannelSuffix';
final BasicMessageChannel<Object?> pigeonVar_channel =
BasicMessageChannel<Object?>(
pigeonVar_channelName,
pigeonChannelCodec,
binaryMessenger: pigeonVar_binaryMessenger,
);
final List<Object?>? pigeonVar_replyList =
await pigeonVar_channel.send(<Object?>[id]) as List<Object?>?;
if (pigeonVar_replyList == null) {
throw _createConnectionError(pigeonVar_channelName);
} else if (pigeonVar_replyList.length > 1) {
throw PlatformException(
code: pigeonVar_replyList[0]! as String,
message: pigeonVar_replyList[1] as String?,
details: pigeonVar_replyList[2],
);
} else if (pigeonVar_replyList[0] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (pigeonVar_replyList[0] as bool?)!;
}
}
}

/// API for interacting with the image registry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ abstract class MapsInspectorApi {
PlatformTileLayer? getTileOverlayInfo(String tileOverlayId);
PlatformZoomRange getZoomRange();
List<PlatformCluster> getClusters(String clusterManagerId);
bool hasRegisteredMapBitmap(int id);
}

/// API for interacting with the image registry.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
import 'package:integration_test/integration_test.dart';
import 'package:maps_example_dart/example_google_map.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
Expand All @@ -12,7 +16,15 @@ void main() {
imagePixelRatio: 1.0,
);

final int mapId = await _pumpMap(tester);
await GoogleMapsFlutterPlatform.instance.registerBitmap(1, bitmap);
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isTrue,
);
});

testWidgets('Remove bitmap from cache', (WidgetTester tester) async {
Expand All @@ -21,8 +33,24 @@ void main() {
imagePixelRatio: 1.0,
);

final int mapId = await _pumpMap(tester);
await GoogleMapsFlutterPlatform.instance.registerBitmap(1, bitmap);
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isTrue,
);

await GoogleMapsFlutterPlatform.instance.unregisterBitmap(1);
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isFalse,
);
});

testWidgets('Clear bitmap cache', (WidgetTester tester) async {
Expand All @@ -31,8 +59,44 @@ void main() {
imagePixelRatio: 1.0,
);

final int mapId = await _pumpMap(tester);
await GoogleMapsFlutterPlatform.instance.clearBitmapCache();
await GoogleMapsFlutterPlatform.instance.registerBitmap(1, bitmap);
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isTrue,
);

await GoogleMapsFlutterPlatform.instance.clearBitmapCache();
expect(
await GoogleMapsInspectorPlatform.instance!.hasRegisteredMapBitmap(
mapId: mapId,
bitmapId: 1,
),
isFalse,
);
});
}

// Pump a map and return the map ID.
Future<int> _pumpMap(WidgetTester tester) async {
final Completer<ExampleGoogleMapController> controllerCompleter =
Completer<ExampleGoogleMapController>();

await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: ExampleGoogleMap(
initialCameraPosition: const CameraPosition(target: LatLng(0, 0)),
onMapCreated: (ExampleGoogleMapController googleMapController) {
controllerCompleter.complete(googleMapController);
},
),
));

final ExampleGoogleMapController controller =
await controllerCompleter.future;
return controller.mapId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ @interface FGMMapCallHandler ()
@interface FGMMapInspector : NSObject <FGMMapsInspectorApi>
- (instancetype)initWithMapController:(nonnull FLTGoogleMapController *)controller
messenger:(NSObject<FlutterBinaryMessenger> *)messenger
imageRegistry:(nonnull FGMImageRegistry *)imageRegistry
pigeonSuffix:(NSString *)suffix;
@end

Expand All @@ -113,6 +114,8 @@ @interface FGMMapInspector ()
@property(nonatomic, weak) FLTGoogleMapController *controller;
/// The messenger this instance was registered with by Pigeon.
@property(nonatomic, copy) NSObject<FlutterBinaryMessenger> *messenger;
/// ImageRegistry for registering bitmaps.
@property(nonatomic, weak) FGMImageRegistry *imageRegistry;
/// The suffix this instance was registered under with Pigeon.
@property(nonatomic, copy) NSString *pigeonSuffix;
@end
Expand Down Expand Up @@ -232,6 +235,7 @@ - (instancetype)initWithMapView:(GMSMapView *_Nonnull)mapView
SetUpFGMMapsApiWithSuffix(registrar.messenger, _callHandler, pigeonSuffix);
_inspector = [[FGMMapInspector alloc] initWithMapController:self
messenger:registrar.messenger
imageRegistry:imageRegistry
pigeonSuffix:pigeonSuffix];
SetUpFGMMapsInspectorApiWithSuffix(registrar.messenger, _inspector, pigeonSuffix);
}
Expand Down Expand Up @@ -743,12 +747,14 @@ @implementation FGMMapInspector

- (instancetype)initWithMapController:(nonnull FLTGoogleMapController *)controller
messenger:(NSObject<FlutterBinaryMessenger> *)messenger
imageRegistry:(nonnull FGMImageRegistry *)imageRegistry
pigeonSuffix:(NSString *)suffix {
self = [super init];
if (self) {
_controller = controller;
_messenger = messenger;
_pigeonSuffix = suffix;
_imageRegistry = imageRegistry;
}
return self;
}
Expand Down Expand Up @@ -831,4 +837,10 @@ - (nullable FGMPlatformZoomRange *)zoomRange:
max:@(self.controller.mapView.maxZoom)];
}

- (nullable NSNumber *)hasRegisteredMapBitmapId:(NSInteger)id
error:(FlutterError *_Nullable __autoreleasing *_Nonnull)
error {
return [self.imageRegistry getBitmap:@(id)] ? @(YES) : @(NO);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,9 @@ extern void SetUpFGMMapsPlatformViewApiWithSuffix(id<FlutterBinaryMessenger> bin
- (nullable NSArray<FGMPlatformCluster *> *)
clustersWithIdentifier:(NSString *)clusterManagerId
error:(FlutterError *_Nullable *_Nonnull)error;
/// @return `nil` only when `error != nil`.
- (nullable NSNumber *)hasRegisteredMapBitmapId:(NSInteger)id
error:(FlutterError *_Nullable *_Nonnull)error;
@end

extern void SetUpFGMMapsInspectorApi(id<FlutterBinaryMessenger> binaryMessenger,
Expand Down
Loading

0 comments on commit dcf3876

Please sign in to comment.