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
1 change: 1 addition & 0 deletions code/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ dependencies {
implementation(libs.coil.network.okhttp)
implementation(libs.libphonenumber)
implementation(libs.maps.compose)
implementation(libs.maplibre.android)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.junit)
testImplementation(libs.mockk)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ class AdminProfileDetailsScreenUITest {

composeTestRule.onNodeWithTag("adminPasswordInput")
.assert(SemanticsMatcher.keyIsDefined(SemanticsProperties.Password))
.assertTextContains("••••••••")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ class RegistrationStatusScreenTest {
@Test
fun showsMessageForEachStatus() {
val expected = mapOf(
WaitlistStatus.WON to "Lottery Won",
WaitlistStatus.LOST to "Lottery Lost",
WaitlistStatus.WON to "Won the Lottery",
WaitlistStatus.LOST to "Lost the Lottery",
WaitlistStatus.ACCEPTED to "Reservation Accepted",
WaitlistStatus.DECLINED to "Reservation Declined"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,130 +1,101 @@
package com.example.lambda_bbb_events.ui.campsites

import androidx.compose.foundation.layout.size
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.click
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.performTouchInput
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.lambda_bbb_events.model.campsite.Campsite
import com.example.lambda_bbb_events.view.ui.components.map.MapCommand
import com.example.lambda_bbb_events.view.ui.components.map.MapsJavaScriptView
import com.example.lambda_bbb_events.view.ui.components.map.osmCampsiteMapScript
import com.example.lambda_bbb_events.view.ui.components.map.osmClickableMapScript
import com.example.lambda_bbb_events.view.ui.components.map.osmPageHtml
import com.example.lambda_bbb_events.view.ui.components.map.OpenStreetCampsiteMap
import com.example.lambda_bbb_events.view.ui.components.map.OpenStreetClickableMap
import com.google.android.gms.maps.model.LatLng
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Tests the OpenStreetMap pages in a real WebView with the bundled Leaflet. The map images are
* left out, so nothing is loaded from OpenStreetMap.
* Tests the native OpenStreetMap maps drawn by MapLibre.
*/
@RunWith(AndroidJUnit4::class)
class OpenStreetMapUITest {

@get:Rule
val composeTestRule = createComposeRule()

private val events = mutableListOf<Pair<String, JSONObject>>()
private var command by mutableStateOf<MapCommand?>(null)

private fun named(name: String) = events.filter { it.first == name }.map { it.second }

private fun show(script: String) {
val page = osmPageHtml(script).replace("https://tile.openstreetmap.org/{z}/{x}/{y}.png", "data:,")
composeTestRule.setContent {
MapsJavaScriptView(
html = page,
modifier = Modifier.size(300.dp).testTag("map"),
command = command,
onEvent = { name, data -> events.add(name to data) }
)
private val start = LatLng(51.1869, -115.5323)

private val campsites = listOf(
Campsite(campsiteId = "a", campsiteName = "Lake", address = "1 Lake Rd", latitude = 51.18, longitude = -115.53),
Campsite(campsiteId = "b", campsiteName = "Ridge", address = "2 Ridge Rd", latitude = 51.25, longitude = -115.6)
)

/**
* Taps the picker beside its marker until the map is ready and reports the tap, then returns
* where it was reported.
*/
private fun tapPickerUntilSelected(selected: () -> LatLng?): LatLng? {
repeat(20) {
composeTestRule.onNodeWithTag("osm_picker").performTouchInput { click(centerLeft + (center - centerLeft) / 2f) }
composeTestRule.waitForIdle()
selected()?.let { return it }
Thread.sleep(500)
}
}

private fun waitFor(name: String) {
composeTestRule.waitUntil(15_000) { named(name).isNotEmpty() }
}

private fun run(script: String) {
composeTestRule.runOnIdle { command = MapCommand(script) }
}

private fun showLocationMap() {
show(osmClickableMapScript(LatLng(51.0, -115.0)))
waitFor("mapReady")
return null
}

@Test
fun tappingTheMapSelectsThatLocation() {
showLocationMap()
composeTestRule.onNodeWithTag("map").performTouchInput { click(percentOffset(0.75f, 0.75f)) }
waitFor("locationSelected")
fun campsiteMapShowsOpenStreetMapCredit() {
composeTestRule.setContent {
OpenStreetCampsiteMap(campsites, Modifier.size(300.dp), onCampsiteClick = null)
}

// Down and to the right of where the map starts
val location = named("locationSelected").last()
assertTrue("$location", location.getDouble("lat") in 50.9..50.9999)
assertTrue("$location", location.getDouble("lng") in -114.9999..-114.9)
composeTestRule.onNodeWithText("© OpenStreetMap contributors").assertIsDisplayed()
}

@Test
fun movingTheMapDoesNotSelectTheLocation() {
showLocationMap()
run("showLocation(52.9, -118.1); send('moved', { html: document.querySelectorAll('.leaflet-marker-icon').length });")
waitFor("moved")
assertFalse(events.any { it.first == "locationSelected" })

// The map is now centred on the new location, so tapping the middle area selects near it
composeTestRule.onNodeWithTag("map").performTouchInput { click(percentOffset(0.6f, 0.6f)) }
waitFor("locationSelected")
val location = named("locationSelected").last()
assertEquals(52.9, location.getDouble("lat"), 0.05)
assertEquals(-118.1, location.getDouble("lng"), 0.05)
}
fun tappingACampsiteShowsItsNameAndAddress() {
var opened: Campsite? = null
composeTestRule.setContent {
OpenStreetCampsiteMap(campsites.take(1), Modifier.size(300.dp).testTag("osm_campsites")) { opened = it }
}

@Test
fun openStreetMapIsCredited() {
showLocationMap()
run("send('credit', { text: document.querySelector('.leaflet-control-attribution').textContent });")
waitFor("credit")
repeat(20) {
composeTestRule.onNodeWithTag("osm_campsites").performTouchInput { click(center) }
composeTestRule.waitForIdle()
if (composeTestRule.onAllNodesWithTag("osm_campsite_card").fetchSemanticsNodes().isNotEmpty()) {
return@repeat
}
Thread.sleep(500)
}
composeTestRule.onNodeWithText("1 Lake Rd").assertIsDisplayed()
composeTestRule.onNodeWithText("View campsite").performClick()

assertTrue(named("credit").last().getString("text").contains("OpenStreetMap contributors"))
assertEquals("a", opened?.campsiteId)
}

@Test
fun campsiteMarkersOpenTheCampsite() {
val sneaky = "Bob's \"Camp\" </script><script>send('hacked')</script>"
show(
osmCampsiteMapScript(
listOf(
Campsite(campsiteId = "a", campsiteName = sneaky, address = "1 Lake Rd", latitude = 53.5, longitude = -113.5),
Campsite(campsiteId = "b", campsiteName = "Forest", address = "9 Pine St", latitude = 51.2, longitude = -115.6)
)
) + "\nconst drawn = initMap; initMap = function () { drawn(); send('drawn'); };"
)
waitFor("drawn")
run(
"const pins = document.querySelectorAll('.leaflet-marker-icon'); pins[0].click();" +
"send('pins', { count: pins.length, name: document.querySelector('.info b').textContent });" +
"document.querySelector('.info a').click();"
)
waitFor("campsiteClick")
fun tappingThePickerSelectsWhereItWasTapped() {
var selected: LatLng? = null
composeTestRule.setContent {
OpenStreetClickableMap(start, null, Modifier.size(300.dp).testTag("osm_picker")) { selected = it }
}

val tapped = tapPickerUntilSelected { selected }

assertEquals(2, named("pins").last().getInt("count"))
assertEquals(sneaky, named("pins").last().getString("name"))
assertEquals("a", named("campsiteClick").last().getString("id"))
assertFalse(events.any { it.first == "hacked" })
assertNotNull(tapped)
assertEquals(start.latitude, tapped!!.latitude, 0.01)
assertTrue(tapped.longitude < start.longitude)
assertTrue(tapped.longitude > start.longitude - 0.05)
}
}
26 changes: 0 additions & 26 deletions code/app/src/main/assets/leaflet/LICENSE

This file was deleted.

Loading
Loading