Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -14,22 +14,17 @@

package com.google.maps.android.compose

import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -42,8 +37,6 @@ class GoogleMapViewTests {

private val startingZoom = 10f
private val startingPosition = LatLng(1.23, 4.56)
private val assertRoundingError = 0.01

private lateinit var cameraPositionState: CameraPositionState

private fun initMap(content: @Composable () -> Unit = {}) {
Expand Down Expand Up @@ -97,7 +90,7 @@ class GoogleMapViewTests {
composeTestRule.waitUntil(1000) {
cameraPositionState.isMoving
}
composeTestRule.waitUntil(3000) {
composeTestRule.waitUntil(5000) {
!cameraPositionState.isMoving
}
assertFalse(cameraPositionState.isMoving)
Expand Down Expand Up @@ -238,9 +231,4 @@ class GoogleMapViewTests {

assertionBlock()
}

private fun LatLng.assertEquals(other: LatLng) {
assertEquals(latitude, other.latitude, assertRoundingError)
assertEquals(longitude, other.longitude, assertRoundingError)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.maps.android.compose

import android.util.Log
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createComposeRule
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

private const val TAG = "MapInColumnTests"

class MapInColumnTests {
@get:Rule
val composeTestRule = createComposeRule()

private val startingZoom = 10f
private val startingPosition = LatLng(1.23, 4.56)
private lateinit var cameraPositionState: CameraPositionState

private fun initMap(content: @Composable () -> Unit = {}) {
val countDownLatch = CountDownLatch(1)
composeTestRule.setContent {
var scrollingEnabled by remember { mutableStateOf(true) }

LaunchedEffect(cameraPositionState.isMoving) {
if (!cameraPositionState.isMoving) {
scrollingEnabled = true
Log.d(TAG, "Map camera stopped moving - Enabling column scrolling...")
}
}

MapInColumn(
modifier = Modifier.fillMaxSize(),
cameraPositionState,
columnScrollingEnabled = scrollingEnabled,
onMapTouched = {
scrollingEnabled = false
Log.d(
TAG,
"User touched map - Disabling column scrolling after user touched this Box..."
)
},
onMapLoaded = {
countDownLatch.countDown()
}
)
}
val mapLoaded = countDownLatch.await(30, TimeUnit.SECONDS)
assertTrue("Map loaded", mapLoaded)
}

@Before
fun setUp() {
cameraPositionState = CameraPositionState(
position = CameraPosition.fromLatLngZoom(
startingPosition,
startingZoom
)
)
}

@Test
fun testStartingCameraPosition() {
initMap()
startingPosition.assertEquals(cameraPositionState.position.target)
}

@Test
fun testLatLngInVisibleRegion() {
initMap()
composeTestRule.runOnUiThread {
val projection = cameraPositionState.projection
assertNotNull(projection)
assertTrue(
projection!!.visibleRegion.latLngBounds.contains(startingPosition)
)
}
}

@Test
fun testLatLngNotInVisibleRegion() {
initMap()
composeTestRule.runOnUiThread {
val projection = cameraPositionState.projection
assertNotNull(projection)
val latLng = LatLng(23.4, 25.6)
assertFalse(
projection!!.visibleRegion.latLngBounds.contains(latLng)
)
}
}

@Test
fun testScrollColumn_MapCameraRemainsSame() {
initMap()
// Check that the column scrolls to the last item
composeTestRule.onRoot().performTouchInput { swipeUp() }
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag("Item 1").assertIsNotDisplayed()

// Check that the map didn't change
startingPosition.assertEquals(cameraPositionState.position.target)
}

// @Test
// fun testPanMapUp_MapCameraChangesColumnDoesNotScroll() {
// initMap()
// // Swipe the map up
// // FIXME - for some reason this scrolls the entire column instead of just the map

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arriolac @wangela I still need to work on this unit test further (for some reason it fails when it should pass), but I'd like to go ahead and get this PR merged first and add this test later (IMHO it's a "nice-to-have"). Otherwise, due to the refactoring in this PR I keep getting merge conflicts every time the main branch is changed.

So it should be ready for review.

// composeTestRule.onNodeWithTag("Map").performTouchInput { swipeUp() }
// composeTestRule.waitForIdle()
//
// // Make sure that the map changed (i.e., we can scroll the map in the column)
// startingPosition.assertNotEquals(cameraPositionState.position.target)
//
// // Check to make sure column didn't scroll
// composeTestRule.onNodeWithTag("Item 1").assertIsDisplayed()
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.google.maps.android.compose

import com.google.android.gms.maps.model.LatLng
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals

const val assertRoundingError: Double = 0.01

fun LatLng.assertEquals(other: LatLng) {
assertEquals(latitude, other.latitude, assertRoundingError)
assertEquals(longitude, other.longitude, assertRoundingError)
}

fun LatLng.assertNotEquals(other: LatLng) {
assertNotEquals(latitude, other.latitude, assertRoundingError)
assertNotEquals(longitude, other.longitude, assertRoundingError)
}
9 changes: 7 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@
android:value="${MAPS_API_KEY}" />

<activity
android:name=".MapSampleActivity"
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BasicMapActivity"
android:exported="true" />
<activity
android:name=".MapInColumnActivity"
android:exported="true"/>

<!-- Used by createComponentActivity() for unit testing -->
<activity android:name="androidx.activity.ComponentActivity" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,9 @@ import androidx.compose.animation.fadeOut
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
Expand All @@ -57,14 +38,14 @@ import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import kotlinx.coroutines.launch

private const val TAG = "MapSampleActivity"
private const val TAG = "BasicMapActivity"

val singapore = LatLng(1.35, 103.87)
val singapore2 = LatLng(1.40, 103.77)
val singapore3 = LatLng(1.45, 103.77)
val defaultCameraPosition = CameraPosition.fromLatLngZoom(singapore, 11f)
private val singapore = LatLng(1.35, 103.87)
private val singapore2 = LatLng(1.40, 103.77)
private val singapore3 = LatLng(1.45, 103.77)
private val defaultCameraPosition = CameraPosition.fromLatLngZoom(singapore, 11f)

class MapSampleActivity : ComponentActivity() {
class BasicMapActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
74 changes: 74 additions & 0 deletions app/src/main/java/com/google/maps/android/compose/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.maps.android.compose

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

private const val TAG = "MapSampleActivity"

class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val context = LocalContext.current
Column(
Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.padding(10.dp))
Text(
text = getString(R.string.main_activity_title),
style = MaterialTheme.typography.h5
)
Spacer(modifier = Modifier.padding(10.dp))
Button(
onClick = {
context.startActivity(Intent(context, BasicMapActivity::class.java))
}) {
Text(getString(R.string.basic_map_activity))
}
Spacer(modifier = Modifier.padding(5.dp))
Button(
onClick = {
context.startActivity(Intent(context, MapInColumnActivity::class.java))
}) {
Text(getString(R.string.map_in_column_activity))
}
}
}
}
}
}
Loading