Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:typed_data';

import 'package:flutter_test/flutter_test.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';

import 'fake_google_maps_flutter_platform.dart';

void main() {
late FakeGoogleMapsFlutterPlatform platform;

setUp(() {
platform = FakeGoogleMapsFlutterPlatform();
GoogleMapsFlutterPlatform.instance = platform;
});

test('Adding bitmap to registry', () async {
final BytesMapBitmap bitmap = BytesMapBitmap(Uint8List(20));
final int id = await GoogleMapBitmapRegistry.instance.register(bitmap);
expect(id, 1);
expect(
platform.bitmapRegistryRecorder.bitmaps,
<String>['REGISTER $id $bitmap'],
);
});

test('Removing bitmap from registry', () async {
final BytesMapBitmap bitmap = BytesMapBitmap(Uint8List(20));
final int id = await GoogleMapBitmapRegistry.instance.register(bitmap);
await GoogleMapBitmapRegistry.instance.unregister(id);
expect(
platform.bitmapRegistryRecorder.bitmaps,
<String>['REGISTER $id $bitmap', 'UNREGISTER $id'],
);
});

test('Clearing bitmap registry', () async {
final BytesMapBitmap bitmap = BytesMapBitmap(Uint8List(20));
final int id1 = await GoogleMapBitmapRegistry.instance.register(bitmap);
final int id2 = await GoogleMapBitmapRegistry.instance.register(bitmap);
expect(
platform.bitmapRegistryRecorder.bitmaps,
<String>['REGISTER $id1 $bitmap', 'REGISTER $id2 $bitmap'],
);

await GoogleMapBitmapRegistry.instance.clear();
expect(
platform.bitmapRegistryRecorder.bitmaps,
<String>['REGISTER $id1 $bitmap', 'REGISTER $id2 $bitmap', 'CLEAR CACHE'],
);
});

test('Bitmap ID is incremental', () async {
final BytesMapBitmap bitmap = BytesMapBitmap(Uint8List(20));
final int id1 = await GoogleMapBitmapRegistry.instance.register(bitmap);
final int id2 = await GoogleMapBitmapRegistry.instance.register(bitmap);
final int id3 = await GoogleMapBitmapRegistry.instance.register(bitmap);
final int id4 = await GoogleMapBitmapRegistry.instance.register(bitmap);
expect(id2, id1 + 1);
expect(id3, id1 + 2);
expect(id4, id1 + 3);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class FakeGoogleMapsFlutterPlatform extends GoogleMapsFlutterPlatform {
Map<int, PlatformMapStateRecorder> mapInstances =
<int, PlatformMapStateRecorder>{};

/// A recorder for the bitmap registry calls.
PlatformBitmapRegistryRecorder bitmapRegistryRecorder =
PlatformBitmapRegistryRecorder();

PlatformMapStateRecorder get lastCreatedMap => mapInstances[createdIds.last]!;

/// Whether to add a small delay to async calls to simulate more realistic
Expand Down Expand Up @@ -264,6 +268,21 @@ class FakeGoogleMapsFlutterPlatform extends GoogleMapsFlutterPlatform {
return mapEventStreamController.stream.whereType<ClusterTapEvent>();
}

@override
Future<void> registerBitmap(int id, MapBitmap bitmap) async {
bitmapRegistryRecorder.bitmaps.add('REGISTER $id $bitmap');
}

@override
Future<void> unregisterBitmap(int id) async {
bitmapRegistryRecorder.bitmaps.add('UNREGISTER $id');
}

@override
Future<void> clearBitmapCache() async {
bitmapRegistryRecorder.bitmaps.add('CLEAR CACHE');
}

@override
void dispose({required int mapId}) {
disposed = true;
Expand Down Expand Up @@ -331,3 +350,11 @@ class PlatformMapStateRecorder {
final List<ClusterManagerUpdates> clusterManagerUpdates =
<ClusterManagerUpdates>[];
}

/// A fake implementation of native bitmap registry, which stores all the
/// updates for inspection in tests.
class PlatformBitmapRegistryRecorder {
PlatformBitmapRegistryRecorder();

final List<String> bitmaps = <String>[];
}
Comment thread
aednlaxer marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static BitmapDescriptor toBitmapDescriptor(
platformBitmap, assetManager, density, new BitmapDescriptorFactoryWrapper(), imageRegistry);
}

private static BitmapDescriptor toBitmapDescriptor(
public static BitmapDescriptor toBitmapDescriptor(
Comment thread
aednlaxer marked this conversation as resolved.
Messages.PlatformBitmap platformBitmap,
AssetManager assetManager,
float density,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.embedding.engine.plugins.lifecycle.FlutterLifecycleAdapter;
import io.flutter.plugins.googlemaps.Convert.BitmapDescriptorFactoryWrapper;
import io.flutter.plugins.googlemaps.Messages.ImageRegistryApi;

/**
Expand All @@ -37,7 +38,11 @@ public GoogleMapsPlugin() {}

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
Comment thread
jokerttu marked this conversation as resolved.
final ImageRegistry imageRegistry = new ImageRegistry(binding.getApplicationContext());
final ImageRegistry imageRegistry = new ImageRegistry(
binding.getApplicationContext().getAssets(),
new BitmapDescriptorFactoryWrapper(),
binding.getApplicationContext().getResources().getDisplayMetrics().density
);
ImageRegistryApi.setUp(binding.getBinaryMessenger(), imageRegistry);

binding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
package io.flutter.plugins.googlemaps;

import android.content.Context;
import android.content.res.AssetManager;
import androidx.annotation.NonNull;
import io.flutter.plugins.googlemaps.Convert.BitmapDescriptorFactoryWrapper;
import io.flutter.plugins.googlemaps.Messages.ImageRegistryApi;
import io.flutter.plugins.googlemaps.Messages.PlatformBitmap;
import com.google.android.gms.maps.model.BitmapDescriptor;
import java.util.HashMap;

class ImageRegistry implements ImageRegistryApi {
Comment thread
aednlaxer marked this conversation as resolved.
final Context context;
final AssetManager assetManager;
final BitmapDescriptorFactoryWrapper bitmapDescriptorFactoryWrapper;
final float density;

private final HashMap<Long, BitmapDescriptor> registry = new HashMap<>();

ImageRegistry(Context context) {
this.context = context;
this.assetManager = context.getAssets();
this.density = context.getResources().getDisplayMetrics().density;
ImageRegistry(
AssetManager assetManager,
BitmapDescriptorFactoryWrapper bitmapDescriptorFactoryWrapper,
float density
) {
this.assetManager = assetManager;
this.bitmapDescriptorFactoryWrapper = bitmapDescriptorFactoryWrapper;
this.density = density;
}

@Override
public void addBitmapToCache(Long id, PlatformBitmap bitmap) {
public void addBitmapToCache(@NonNull Long id, PlatformBitmap bitmap) {
if (!(bitmap.getBitmap() instanceof Messages.PlatformBitmapAsset) &&
!(bitmap.getBitmap() instanceof Messages.PlatformBitmapAssetMap) &&
!(bitmap.getBitmap() instanceof Messages.PlatformBitmapBytesMap)) {
throw new IllegalArgumentException("PlatformBitmap must contain a supported subtype.");
}

final BitmapDescriptor bitmapDescriptor =
Convert.toBitmapDescriptor(bitmap, assetManager, density, this);
Convert.toBitmapDescriptor(bitmap, assetManager, density, bitmapDescriptorFactoryWrapper, this);
registry.put(id, bitmapDescriptor);
}

@Override
public void removeBitmapFromCache(Long id) {
public void removeBitmapFromCache(@NonNull Long id) {
registry.remove(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@
import static io.flutter.plugins.googlemaps.Convert.HEATMAP_RADIUS_KEY;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.Base64;
import androidx.test.core.app.ApplicationProvider;
import androidx.annotation.NonNull;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.LatLng;
Expand All @@ -40,6 +44,7 @@
import com.google.maps.android.projection.SphericalMercatorProjection;
import io.flutter.plugins.googlemaps.Convert.BitmapDescriptorFactoryWrapper;
import io.flutter.plugins.googlemaps.Convert.FlutterInjectorWrapper;
import io.flutter.plugins.googlemaps.Messages.PlatformBitmap;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -335,6 +340,49 @@ public void GetBitmapFromBytesThrowsErrorIfInvalidImageData() {
fail("Expected an IllegalArgumentException to be thrown");
}

@Test
public void GetBitmapFromNotRegisteredBitmap() {
ImageRegistry imageRegistry =
new ImageRegistry(assetManager, bitmapDescriptorFactoryWrapper, 1L);

Messages.PlatformBitmapRegisteredMapBitmap bitmap =
new Messages.PlatformBitmapRegisteredMapBitmap.Builder()
.setId(0L)
.build();

BitmapDescriptor result = Convert.getBitmapFromRegisteredBitmap(imageRegistry, bitmap);
Assert.assertEquals(result, null);
}

@Test
public void GetPreviouslyRegisteredBitmap() {
ImageRegistry imageRegistry =
new ImageRegistry(assetManager, bitmapDescriptorFactoryWrapper, 1L);

byte[] bmpData = Base64.decode(base64Image, Base64.DEFAULT);
Messages.PlatformBitmapBytesMap bitmap =
new Messages.PlatformBitmapBytesMap.Builder()
.setBitmapScaling(Messages.PlatformMapBitmapScaling.NONE)
.setImagePixelRatio(2.0)
.setByteData(bmpData)
.build();
PlatformBitmap platformBitmap = new PlatformBitmap.Builder()
.setBitmap(bitmap)
.build();
when(bitmapDescriptorFactoryWrapper.fromBitmap(any())).thenReturn(mockBitmapDescriptor);
imageRegistry.addBitmapToCache(0L, platformBitmap);

Messages.PlatformBitmapRegisteredMapBitmap registeredBitmap =
new Messages.PlatformBitmapRegisteredMapBitmap.Builder()
.setId(0L)
.build();
when(bitmapDescriptorFactoryWrapper.fromBitmap(any())).thenReturn(mockBitmapDescriptor);
BitmapDescriptor result = Convert.getBitmapFromRegisteredBitmap(imageRegistry,
registeredBitmap);
BitmapDescriptor registryBitmapDescriptor = imageRegistry.getBitmap(0L);
Assert.assertEquals(result, registryBitmapDescriptor);
}

@Test
public void interpretMapConfiguration_handlesNulls() {
final Messages.PlatformMapConfiguration config =
Expand Down
Loading