mirror of
https://github.com/googlemaps/android-samples.git
synced 2025-12-08 18:02:20 +00:00
feat(GroundOverlay): Refactor onClick handlers and add tests
This commit refactors the GroundOverlayDemoActivity to remove onClick handlers from the layout XML files, setting them programmatically in the activity instead. This improves code organization and maintainability. Additionally, this change introduces Espresso tests for the GroundOverlayDemoActivity, verifying the functionality of the transparency slider and the toggle buttons.
This commit is contained in:
parent
9115c9fc4f
commit
bc6a5f6dde
@ -23,6 +23,7 @@ recyclerview = "1.4.0"
|
||||
secretsGradlePlugin = "2.0.1"
|
||||
volley = "1.2.1"
|
||||
truth = "1.1.3"
|
||||
uiautomator = "2.3.0"
|
||||
|
||||
[libraries]
|
||||
activity = { module = "androidx.activity:activity", version.ref = "activity" }
|
||||
@ -46,3 +47,4 @@ recyclerview = { group = "androidx.recyclerview", name = "recyclerview", version
|
||||
secretsGradlePlugin = { module = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin", version.ref = "secretsGradlePlugin" }
|
||||
volley = { group = "com.android.volley", name = "volley", version.ref = "volley" }
|
||||
truth = { group = "com.google.truth", name = "truth", version.ref = "truth" }
|
||||
uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "uiautomator" }
|
||||
|
||||
@ -67,6 +67,7 @@ dependencies {
|
||||
implementation(libs.material)
|
||||
implementation(libs.activity)
|
||||
implementation(project(":common-ui"))
|
||||
implementation(libs.uiautomator)
|
||||
|
||||
// Tests
|
||||
testImplementation(libs.junit)
|
||||
|
||||
@ -15,6 +15,14 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
import androidx.test.uiautomator.UiDevice;
|
||||
import android.graphics.Point;
|
||||
|
||||
import com.google.android.gms.maps.model.LatLng;
|
||||
|
||||
import com.example.common_ui.R;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@ -24,6 +32,9 @@ public class GroundOverlayDemoActivityTest {
|
||||
private ActivityScenario<GroundOverlayDemoActivity> scenario;
|
||||
private GroundOverlay groundOverlay;
|
||||
|
||||
// A coordinate known to be on the rotated overlay
|
||||
private final LatLng OVERLAY_COORDINATE = new LatLng(40.714904490859396, -74.23857238143682);
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
scenario = ActivityScenario.launch(GroundOverlayDemoActivity.class);
|
||||
@ -40,49 +51,19 @@ public class GroundOverlayDemoActivityTest {
|
||||
if (activity.mMap == null) {
|
||||
throw new RuntimeException("Map did not become available within 5 seconds.");
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
idlingResource = new MapIdlingResource(activity.mMap);
|
||||
IdlingRegistry.getInstance().register(idlingResource);
|
||||
groundOverlay = activity.groundOverlayRotated;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToggleClickability() {
|
||||
// By default, the overlay is clickable.
|
||||
final float[] initialTransparency = new float[1];
|
||||
scenario.onActivity(activity -> {
|
||||
initialTransparency[0] = groundOverlay.getTransparency();
|
||||
});
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.map)).perform(ViewActions.click());
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
idlingResource.waitForIdle();
|
||||
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(groundOverlay.getTransparency()).isNotEqualTo(initialTransparency[0]);
|
||||
});
|
||||
|
||||
// Disable clickability.
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.toggleClickability)).perform(ViewActions.click());
|
||||
|
||||
// Now, clicking the map should not change the transparency.
|
||||
final float[] transparencyAfterToggle = new float[1];
|
||||
scenario.onActivity(activity -> {
|
||||
transparencyAfterToggle[0] = groundOverlay.getTransparency();
|
||||
});
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.map)).perform(ViewActions.click());
|
||||
idlingResource.waitForIdle();
|
||||
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(groundOverlay.getTransparency()).isEqualTo(transparencyAfterToggle[0]);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransparencySeekBar() {
|
||||
final float[] initialTransparency = new float[1];
|
||||
@ -90,7 +71,7 @@ public class GroundOverlayDemoActivityTest {
|
||||
initialTransparency[0] = activity.groundOverlay.getTransparency();
|
||||
});
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.transparencySeekBar)).perform(ViewActions.swipeRight());
|
||||
Espresso.onView(ViewMatchers.withId(R.id.transparencySeekBar)).perform(ViewActions.swipeRight());
|
||||
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(activity.groundOverlay.getTransparency()).isNotEqualTo(initialTransparency[0]);
|
||||
@ -105,76 +86,110 @@ public class GroundOverlayDemoActivityTest {
|
||||
initialBitmapDescriptor[0] = (BitmapDescriptor) activity.groundOverlay.getTag();
|
||||
});
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.switchImage)).perform(ViewActions.click());
|
||||
Espresso.onView(ViewMatchers.withId(R.id.switchImage)).perform(ViewActions.click());
|
||||
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(activity.groundOverlay.getTag()).isNotEqualTo(initialBitmapDescriptor[0]);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToggleClickability_DisablesClicks() {
|
||||
// Verify the overlay is initially clickable.
|
||||
final float[] initialTransparency = new float[1];
|
||||
scenario.onActivity(activity -> {
|
||||
initialTransparency[0] = activity.groundOverlayRotated.getTransparency();
|
||||
});
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.map)).perform(ViewActions.click());
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
idlingResource.waitForIdle();
|
||||
private Point getPointAtCoordinate(LatLng latLng) {
|
||||
final Point[] screenPoint = new Point[1];
|
||||
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(activity.groundOverlayRotated.getTransparency()).isNotEqualTo(initialTransparency[0]);
|
||||
// Get the top-left screen coordinate of the MapView
|
||||
int[] mapViewLocation = new int[2];
|
||||
activity.mMap.getUiSettings().setScrollGesturesEnabled(false); // Disable gestures to prevent map movement during test
|
||||
activity.findViewById(R.id.map).getLocationOnScreen(mapViewLocation);
|
||||
int mapViewLeft = mapViewLocation[0];
|
||||
int mapViewTop = mapViewLocation[1];
|
||||
|
||||
// Convert the LatLng to screen pixels relative to the MapView
|
||||
screenPoint[0] = activity.mMap.getProjection().toScreenLocation(latLng);
|
||||
|
||||
// Now calculate the overall location
|
||||
screenPoint[0].x += mapViewLeft;
|
||||
screenPoint[0].y += mapViewTop;
|
||||
});
|
||||
|
||||
// Disable clickability.
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.toggleClickability)).perform(ViewActions.click());
|
||||
return screenPoint[0];
|
||||
}
|
||||
|
||||
// Now, clicking the map should not change the transparency.
|
||||
final float[] transparencyAfterToggle = new float[1];
|
||||
scenario.onActivity(activity -> {
|
||||
transparencyAfterToggle[0] = activity.groundOverlayRotated.getTransparency();
|
||||
});
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.map)).perform(ViewActions.click());
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
idlingResource.waitForIdle();
|
||||
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(activity.groundOverlayRotated.getTransparency()).isEqualTo(transparencyAfterToggle[0]);
|
||||
});
|
||||
private boolean clickOnMapAt(UiDevice device, LatLng latLng) {
|
||||
Point screenPoint = getPointAtCoordinate(latLng);
|
||||
return device.click(screenPoint.x, screenPoint.y);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToggleClickability_ReEnablesClicks() {
|
||||
// Disable and then re-enable clickability.
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.toggleClickability)).perform(ViewActions.click());
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.toggleClickability)).perform(ViewActions.click());
|
||||
public void testToggleClickability_DisablesClicks() {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
final float[] initialTransparency = new float[1];
|
||||
final UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
|
||||
|
||||
// 1. VERIFY CLICKABLE (WHEN ENABLED)
|
||||
scenario.onActivity(activity -> {
|
||||
initialTransparency[0] = activity.groundOverlayRotated.getTransparency();
|
||||
// Ensure it's clickable for the first part of the test
|
||||
activity.groundOverlayRotated.setClickable(true);
|
||||
});
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.map)).perform(ViewActions.click());
|
||||
clickOnMapAt(device, OVERLAY_COORDINATE);
|
||||
|
||||
idlingResource.waitForIdle(); // Wait for listener to fire
|
||||
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
idlingResource.waitForIdle();
|
||||
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(activity.groundOverlayRotated.getTransparency()).isNotEqualTo(initialTransparency[0]);
|
||||
assertThat(activity.groundOverlayRotatedClickCount).isEqualTo(1);
|
||||
});
|
||||
|
||||
// 2. DISABLE CLICKABILITY
|
||||
Espresso.onView(ViewMatchers.withId(R.id.toggleClickability))
|
||||
.perform(ViewActions.click());
|
||||
|
||||
// 3. VERIFY NOT CLICKABLE (WHEN DISABLED)
|
||||
|
||||
// Click the *exact same spot* again
|
||||
clickOnMapAt(device, OVERLAY_COORDINATE);
|
||||
|
||||
idlingResource.waitForIdle(); // Give it time (listener shouldn't fire)
|
||||
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// Assert the count did not change
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(activity.groundOverlayRotatedClickCount).isEqualTo(1);
|
||||
});
|
||||
|
||||
// And back to clickability
|
||||
Espresso.onView(ViewMatchers.withId(R.id.toggleClickability))
|
||||
.perform(ViewActions.click());
|
||||
|
||||
// Click the *exact same spot* again
|
||||
clickOnMapAt(device, OVERLAY_COORDINATE);
|
||||
|
||||
idlingResource.waitForIdle(); // Give it time (listener shouldn't fire)
|
||||
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// Assert the count did not change
|
||||
scenario.onActivity(activity -> {
|
||||
assertThat(activity.groundOverlayRotatedClickCount).isEqualTo(2);
|
||||
});
|
||||
}
|
||||
|
||||
@ -185,7 +200,7 @@ public class GroundOverlayDemoActivityTest {
|
||||
initialTransparency[0] = activity.groundOverlay.getTransparency();
|
||||
});
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(com.example.common_ui.R.id.map)).perform(ViewActions.click());
|
||||
Espresso.onView(ViewMatchers.withId(R.id.map)).perform(ViewActions.click());
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@ -24,6 +24,7 @@ import com.google.android.gms.maps.model.GroundOverlay;
|
||||
import com.google.android.gms.maps.model.GroundOverlayOptions;
|
||||
import com.google.android.gms.maps.model.LatLng;
|
||||
|
||||
import android.graphics.Point;
|
||||
import android.os.Bundle;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.SeekBar.OnSeekBarChangeListener;
|
||||
@ -42,6 +43,8 @@ public class GroundOverlayDemoActivity extends SamplesBaseActivity
|
||||
implements OnSeekBarChangeListener, OnMapReadyCallback,
|
||||
GoogleMap.OnGroundOverlayClickListener {
|
||||
|
||||
private static final String TAG = GroundOverlayDemoActivity.class.getName();
|
||||
|
||||
private static final int TRANSPARENCY_MAX = 100;
|
||||
private static final LatLng NEWARK = new LatLng(40.714086, -74.228697);
|
||||
private static final LatLng NEAR_NEWARK =
|
||||
@ -54,9 +57,14 @@ public class GroundOverlayDemoActivity extends SamplesBaseActivity
|
||||
public GroundOverlay groundOverlay;
|
||||
public GroundOverlay groundOverlayRotated;
|
||||
|
||||
public int groundOverlayRotatedClickCount = 0;
|
||||
public int mapClickCount = 0;
|
||||
|
||||
private com.example.common_ui.databinding.GroundOverlayDemoBinding binding;
|
||||
private int currentEntry = 0;
|
||||
|
||||
public boolean mapReady = false;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -89,6 +97,13 @@ public class GroundOverlayDemoActivity extends SamplesBaseActivity
|
||||
// Move the camera to the Newark area.
|
||||
map.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));
|
||||
|
||||
map.moveCamera(CameraUpdateFactory.scrollBy(100f, 100f));
|
||||
|
||||
map.setOnMapClickListener(ll -> {
|
||||
Point point = mMap.getProjection().toScreenLocation(ll);
|
||||
mapClickCount += 1;
|
||||
});
|
||||
|
||||
// Prepare the BitmapDescriptor objects. Using a BitmapDescriptorFactory is the most
|
||||
// memory-efficient way to create the images that will be used for the overlays.
|
||||
images.clear();
|
||||
@ -112,16 +127,18 @@ public class GroundOverlayDemoActivity extends SamplesBaseActivity
|
||||
.clickable(binding.toggleClickability.isChecked()));
|
||||
|
||||
// Add a large overlay at Newark on top of the smaller overlay.
|
||||
groundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
|
||||
.image(images.get(currentEntry)).anchor(0, 1)
|
||||
.position(NEWARK, 8600f, 6500f));
|
||||
groundOverlay.setTag(images.get(currentEntry));
|
||||
groundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
|
||||
.image(images.get(currentEntry)).anchor(0, 1)
|
||||
.position(NEWARK, 8600f, 6500f));
|
||||
groundOverlay.setTag(images.get(currentEntry));
|
||||
|
||||
binding.transparencySeekBar.setOnSeekBarChangeListener(this);
|
||||
|
||||
// Override the default content description on the view for accessibility mode.
|
||||
// Ideally this string would be localized.
|
||||
map.setContentDescription("Google Map with ground overlay.");
|
||||
|
||||
mapReady = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -155,6 +172,7 @@ public class GroundOverlayDemoActivity extends SamplesBaseActivity
|
||||
*/
|
||||
@Override
|
||||
public void onGroundOverlayClick(GroundOverlay groundOverlay) {
|
||||
groundOverlayRotatedClickCount += 1;
|
||||
// In this demo, we only toggle the transparency of the smaller, rotated overlay.
|
||||
// The transparency is toggled between 0.0f (opaque) and 0.5f (semi-transparent).
|
||||
groundOverlayRotated.setTransparency(0.5f - groundOverlayRotated.getTransparency());
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.example.kotlindemos
|
||||
|
||||
import android.view.View
|
||||
import androidx.test.core.app.ActivityScenario
|
||||
import androidx.test.espresso.Espresso.onView
|
||||
import androidx.test.espresso.IdlingRegistry
|
||||
@ -14,6 +15,7 @@ import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import com.example.common_ui.R
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class GroundOverlayDemoActivityTest {
|
||||
@ -44,6 +46,7 @@ class GroundOverlayDemoActivityTest {
|
||||
IdlingRegistry.getInstance().register(idlingResource)
|
||||
groundOverlay = activity.groundOverlayRotated!!
|
||||
}
|
||||
Thread.sleep(2000)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -53,6 +56,7 @@ class GroundOverlayDemoActivityTest {
|
||||
// Get the initial transparency and position of the overlay on the UI thread.
|
||||
var initialTransparency = 0f
|
||||
scenario.onActivity { activity ->
|
||||
assertThat(activity.groundOverlayRotatedClickCount).isEqualTo(0)
|
||||
initialTransparency = groundOverlay.transparency
|
||||
}
|
||||
|
||||
@ -63,14 +67,16 @@ class GroundOverlayDemoActivityTest {
|
||||
|
||||
// This is not ideal, but it's a simple way to make the test reliable.
|
||||
Thread.sleep(200)
|
||||
idlingResource.waitForIdle()
|
||||
|
||||
// Verify that the transparency has changed, indicating the click was successful.
|
||||
scenario.onActivity {
|
||||
scenario.onActivity { activity ->
|
||||
assertThat(activity.groundOverlayRotatedClickCount).isEqualTo(1)
|
||||
assertThat(groundOverlay.transparency).isNotEqualTo(initialTransparency)
|
||||
}
|
||||
|
||||
// Disable clickability by clicking the checkbox.
|
||||
onView(withId(com.example.common_ui.R.id.toggleClickability)).perform(click())
|
||||
onView(withId(R.id.toggleClickability)).perform(click())
|
||||
|
||||
// Get the transparency after the first click.
|
||||
var transparencyAfterToggle = 0f
|
||||
@ -93,10 +99,14 @@ class GroundOverlayDemoActivityTest {
|
||||
private fun clickOnMapAt(latLng: LatLng) {
|
||||
val coordinates = FloatArray(2)
|
||||
scenario.onActivity { activity ->
|
||||
val mapLocation = IntArray(2)
|
||||
activity.findViewById<View>(R.id.map).getLocationOnScreen(mapLocation)
|
||||
|
||||
val projection = activity.map.projection
|
||||
val screenPosition = projection.toScreenLocation(latLng)
|
||||
coordinates[0] = screenPosition.x.toFloat()
|
||||
coordinates[1] = screenPosition.y.toFloat()
|
||||
|
||||
coordinates[0] = screenPosition.x.toFloat() + mapLocation[0]
|
||||
coordinates[1] = screenPosition.y.toFloat() + mapLocation[1]
|
||||
}
|
||||
|
||||
val clickAction = androidx.test.espresso.action.GeneralClickAction(
|
||||
@ -106,7 +116,7 @@ class GroundOverlayDemoActivityTest {
|
||||
android.view.InputDevice.SOURCE_UNKNOWN,
|
||||
android.view.MotionEvent.BUTTON_PRIMARY
|
||||
)
|
||||
onView(withId(com.example.common_ui.R.id.map)).perform(clickAction)
|
||||
onView(withId(R.id.map)).perform(clickAction)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -116,7 +126,7 @@ class GroundOverlayDemoActivityTest {
|
||||
initialTransparency = activity.groundOverlay!!.transparency
|
||||
}
|
||||
|
||||
onView(withId(com.example.common_ui.R.id.transparencySeekBar)).perform(click())
|
||||
onView(withId(R.id.transparencySeekBar)).perform(click())
|
||||
|
||||
scenario.onActivity { activity ->
|
||||
assertThat(activity.groundOverlay!!.transparency).isNotEqualTo(initialTransparency)
|
||||
@ -131,13 +141,68 @@ class GroundOverlayDemoActivityTest {
|
||||
initialBitmapDescriptor = activity.groundOverlay!!.tag as BitmapDescriptor
|
||||
}
|
||||
|
||||
onView(withId(com.example.common_ui.R.id.switchImage)).perform(click())
|
||||
onView(withId(R.id.switchImage)).perform(click())
|
||||
|
||||
scenario.onActivity { activity ->
|
||||
assertThat(activity.groundOverlay!!.tag as BitmapDescriptor).isNotEqualTo(initialBitmapDescriptor)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testToggleClickability_DisablesClicks() {
|
||||
// Verify the overlay is initially clickable.
|
||||
var initialTransparency = 0f
|
||||
scenario.onActivity {
|
||||
initialTransparency = groundOverlay.transparency
|
||||
}
|
||||
|
||||
val clickPosition = LatLng(40.71398657613997, -74.24413025379181)
|
||||
|
||||
clickOnMapAt(clickPosition)
|
||||
|
||||
Thread.sleep(2000)
|
||||
|
||||
scenario.onActivity {
|
||||
assertThat(groundOverlay.transparency).isNotEqualTo(initialTransparency)
|
||||
}
|
||||
|
||||
// Disable clickability.
|
||||
onView(withId(R.id.toggleClickability)).perform(click())
|
||||
|
||||
// Now, clicking the map should not change the transparency.
|
||||
var transparencyAfterToggle = 0f
|
||||
scenario.onActivity {
|
||||
transparencyAfterToggle = groundOverlay.transparency
|
||||
}
|
||||
|
||||
clickOnMapAt(clickPosition)
|
||||
Thread.sleep(200)
|
||||
|
||||
scenario.onActivity {
|
||||
assertThat(groundOverlay.transparency).isEqualTo(transparencyAfterToggle)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testToggleClickability_ReEnablesClicks() {
|
||||
// Disable and then re-enable clickability.
|
||||
onView(withId(R.id.toggleClickability)).perform(click())
|
||||
onView(withId(R.id.toggleClickability)).perform(click())
|
||||
|
||||
var initialTransparency = 0f
|
||||
scenario.onActivity {
|
||||
initialTransparency = groundOverlay.transparency
|
||||
}
|
||||
|
||||
val clickPosition = LatLng(40.71398657613997, -74.24413025379181)
|
||||
clickOnMapAt(clickPosition)
|
||||
Thread.sleep(200)
|
||||
|
||||
scenario.onActivity {
|
||||
assertThat(groundOverlay.transparency).isNotEqualTo(initialTransparency)
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
IdlingRegistry.getInstance().unregister(idlingResource)
|
||||
|
||||
@ -17,7 +17,6 @@ package com.example.kotlindemos
|
||||
import android.os.Bundle
|
||||
import android.widget.SeekBar
|
||||
import android.widget.SeekBar.OnSeekBarChangeListener
|
||||
import android.widget.Toast
|
||||
import com.example.common_ui.R
|
||||
import com.google.android.gms.maps.CameraUpdateFactory
|
||||
import com.google.android.gms.maps.GoogleMap
|
||||
@ -40,8 +39,7 @@ import com.google.android.gms.maps.model.LatLng
|
||||
class GroundOverlayDemoActivity : SamplesBaseActivity(),
|
||||
OnSeekBarChangeListener,
|
||||
OnMapReadyCallback,
|
||||
OnGroundOverlayClickListener,
|
||||
GoogleMap.OnMapClickListener
|
||||
OnGroundOverlayClickListener
|
||||
{
|
||||
|
||||
private val images: MutableList<BitmapDescriptor> = ArrayList()
|
||||
@ -49,6 +47,9 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
|
||||
|
||||
// These are internal for testing purposes only.
|
||||
internal var groundOverlayRotated: GroundOverlay? = null
|
||||
|
||||
internal var groundOverlayRotatedClickCount = 0
|
||||
|
||||
internal lateinit var map: GoogleMap
|
||||
internal var mapReady = false
|
||||
|
||||
@ -66,8 +67,12 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
|
||||
// Set up programmatic click listeners for the buttons.
|
||||
// This is a better practice than using the android:onClick XML attribute, as it keeps
|
||||
// all the view logic in the Activity code.
|
||||
binding.switchImage.setOnClickListener { switchImage() }
|
||||
binding.toggleClickability.setOnClickListener { toggleClickability() }
|
||||
binding.switchImage.setOnClickListener {
|
||||
switchImage()
|
||||
}
|
||||
binding.toggleClickability.setOnClickListener {
|
||||
toggleClickability()
|
||||
}
|
||||
|
||||
val mapFragment =
|
||||
supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
|
||||
@ -84,11 +89,11 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
|
||||
// Register a listener to respond to clicks on GroundOverlays.
|
||||
map.setOnGroundOverlayClickListener(this)
|
||||
|
||||
map.setOnMapClickListener(this)
|
||||
|
||||
// Move the camera to the Newark area.
|
||||
map.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11f))
|
||||
|
||||
map.moveCamera(CameraUpdateFactory.scrollBy(100f, 100f))
|
||||
|
||||
// Prepare the BitmapDescriptor objects. Using a BitmapDescriptorFactory is the most
|
||||
// memory-efficient way to create the images that will be used for the overlays.
|
||||
images.clear()
|
||||
@ -159,6 +164,7 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
|
||||
override fun onGroundOverlayClick(groundOverlay: GroundOverlay) {
|
||||
// In this demo, we only toggle the transparency of the smaller, rotated overlay.
|
||||
// The transparency is toggled between 0.0f (opaque) and 0.5f (semi-transparent).
|
||||
groundOverlayRotatedClickCount += 1
|
||||
groundOverlayRotated?.let {
|
||||
if (it.transparency < 0.25f) {
|
||||
it.transparency = 0.5f
|
||||
@ -173,24 +179,9 @@ class GroundOverlayDemoActivity : SamplesBaseActivity(),
|
||||
*/
|
||||
private fun toggleClickability() {
|
||||
// The clickability of an overlay can be changed at any time.
|
||||
android.util.Log.d(TAG, "Clickability toggled!")
|
||||
groundOverlayRotated?.isClickable = binding.toggleClickability.isChecked
|
||||
}
|
||||
|
||||
override fun onMapClick(p0: LatLng) {
|
||||
Toast.makeText(
|
||||
this,
|
||||
"Clicked on ${p0.latitude}, ${p0.longitude}",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
|
||||
val clickedGroundOverlay = groundOverlayRotated
|
||||
if (clickedGroundOverlay != null && clickedGroundOverlay.bounds.contains(p0)) {
|
||||
clickedGroundOverlay.transparency = 0.5f - clickedGroundOverlay.transparency
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = GroundOverlayDemoActivity::class.java.simpleName
|
||||
private const val TRANSPARENCY_MAX = 100
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user