60 lines · 3.2 KB · KotlinRaw
package com.cottondb.android.ui.locationsimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.runtime.mutableStateOfimport androidx.compose.ui.Modifierimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.longClickimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.swipeimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.ScreenEvidenceimport com.cottondb.android.day.locations.LocationPinimport com.cottondb.android.day.locations.LocationPresentationFactsimport com.cottondb.android.day.locations.PinnedLocationimport com.cottondb.android.design.CottonThemeimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class LocationMapTest { @get:Rule val compose = createComposeRule() @Test fun paperLongPressChoosesFrameCoordinateAndLeavesPinControlled() { val picked = mutableListOf<LocationPin>() val pin = mutableStateOf<PinnedLocation?>(null) compose.setContent { CottonTheme { LocationMiniMap(pin.value, true, { picked += it }, Modifier.fillMaxWidth()) } } compose.onNodeWithContentDescription(MAP).performTouchInput { longClick(center, durationMillis = 600) } compose.runOnIdle { assertEquals(1, picked.size) assertEquals(LocationPresentationFacts.fallbackFrame.latitude, picked.single().lat, .0001) assertEquals(LocationPresentationFacts.fallbackFrame.longitude, picked.single().lng, .0001) } ScreenEvidence.capture("location-paper-map") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.runOnIdle { pin.value = PinnedLocation("place", "Synthetic", picked.single().lat, picked.single().lng) } compose.waitForIdle() ScreenEvidence.capture("location-paper-pin") { compose.onRoot().captureToImage().asAndroidBitmap() } } @Test fun disabledMapCannotSendPinAndDraggingDoesNotDropOne() { val enabled = mutableStateOf(false) val picked = mutableListOf<LocationPin>() compose.setContent { CottonTheme { LocationMiniMap(PinnedLocation("place", "Synthetic", 1.0, 2.0), enabled.value, { picked += it }, Modifier.fillMaxWidth()) } } compose.onNodeWithContentDescription("Paper location map").performTouchInput { longClick(center, durationMillis = 600) } compose.runOnIdle { assertEquals(0, picked.size); enabled.value = true } compose.onNodeWithContentDescription(MAP).performTouchInput { swipe(centerLeft, centerRight, durationMillis = 100) } compose.runOnIdle { assertEquals(0, picked.size) } compose.onNodeWithContentDescription(MAP).performTouchInput { down(center) moveTo(centerRight) advanceEventTime(600) up() } compose.runOnIdle { assertEquals(0, picked.size) } } private companion object { const val MAP = "Paper location map. Hold for 400 milliseconds to choose a location." }}