From b2bc43c4fb48b18caa7e2477d73c45a831975993 Mon Sep 17 00:00:00 2001 From: Marian Jureczko Date: Fri, 4 Oct 2024 20:45:02 +0200 Subject: [PATCH] #17 in progress - espresso test for updating location --- app/build.gradle | 1 + .../poszukiwacz/SearchingScreenTest.kt | 84 +++++++++++++++++++ .../pl/marianjureczko/poszukiwacz/UiTest.kt | 18 ++++ app/src/main/AndroidManifest.xml | 3 +- .../activity/searching/n/SearchingScreen.kt | 5 +- 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/SearchingScreenTest.kt diff --git a/app/build.gradle b/app/build.gradle index 72a83ab..1ec452f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,6 +16,7 @@ android { versionCode 9 versionName "0.9" testInstrumentationRunner "pl.marianjureczko.poszukiwacz.CustomTestRunner" + testInstrumentationRunnerArguments["grantPermissions"] = "true" manifestPlaceholders = [facebookToken: FACEBOOK_TOKEN] javaCompileOptions { annotationProcessorOptions { diff --git a/app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/SearchingScreenTest.kt b/app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/SearchingScreenTest.kt new file mode 100644 index 0000000..9b294dd --- /dev/null +++ b/app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/SearchingScreenTest.kt @@ -0,0 +1,84 @@ +package pl.marianjureczko.poszukiwacz + +import android.content.Context +import android.location.Location +import android.location.LocationManager +import android.location.provider.ProviderProperties +import android.os.Build +import android.os.SystemClock +import androidx.annotation.RequiresApi +import androidx.compose.ui.test.SemanticsNodeInteraction +import androidx.compose.ui.test.assertTextEquals +import androidx.test.InstrumentationRegistry +import androidx.test.core.app.ApplicationProvider +import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation +import dagger.hilt.android.testing.HiltAndroidTest +import org.junit.Test +import pl.marianjureczko.poszukiwacz.activity.searching.n.STEPS_TO_TREASURE +import pl.marianjureczko.poszukiwacz.screen.main.CustomInitializerForRoute +import pl.marianjureczko.poszukiwacz.shared.StorageHelper + +@HiltAndroidTest +class SearchingScreenTest : UiTest() { + + @RequiresApi(Build.VERSION_CODES.S) + @Test + fun shouldUpdateNavigationWidgets_whenLocationIsUpdated() { + //given + val context = ApplicationProvider.getApplicationContext() + val locationManager: LocationManager = createLocationManager(context) + goToSearching() + val route = StorageHelper(context).loadRoute(CustomInitializerForRoute.routeName) + + //when + setLocation( + locationManager, + route.treasures[0].latitude + 0.01, + route.treasures[0].longitude + 0.01 + ) + + //then + val stepsToTreasure: SemanticsNodeInteraction = getNode(STEPS_TO_TREASURE) + stepsToTreasure.assertTextEquals("2374") + } + + private fun setLocation(locationManager: LocationManager, newLatitude: Double, newLongitude: Double) { + val mockLocation = Location(LocationManager.GPS_PROVIDER).apply { + latitude = newLatitude + longitude = newLongitude + altitude = 0.0 + accuracy = 1.0f + time = System.currentTimeMillis() + elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos() // Important for new Android versions + } + locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mockLocation) + Thread.sleep(100) + composeRule.waitForIdle() + } + + private fun createLocationManager(context: Context): LocationManager { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + with(getInstrumentation().uiAutomation) { + executeShellCommand("appops set " + InstrumentationRegistry.getTargetContext().packageName + " android:mock_location allow") + Thread.sleep(100) + } + } + val locationManager: LocationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager + // Enable mock locations for the LocationManager + locationManager.addTestProvider( + LocationManager.GPS_PROVIDER, + false, + false, + false, + false, + true, + true, + true, + ProviderProperties.POWER_USAGE_MEDIUM, + ProviderProperties.ACCURACY_FINE + ) + locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true) + return locationManager + } + +} \ No newline at end of file diff --git a/app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/UiTest.kt b/app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/UiTest.kt index 56d7420..89477c4 100644 --- a/app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/UiTest.kt +++ b/app/src/androidTestKalinowiceCustomDebug/java/pl/marianjureczko/poszukiwacz/UiTest.kt @@ -2,6 +2,7 @@ package pl.marianjureczko.poszukiwacz import androidx.compose.ui.test.SemanticsNodeInteraction import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.assertIsEnabled import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithTag @@ -12,6 +13,7 @@ import dagger.hilt.android.testing.HiltAndroidRule import org.junit.Before import org.junit.Rule import pl.marianjureczko.poszukiwacz.activity.main.MainActivity +import pl.marianjureczko.poszukiwacz.screen.main.START_BUTTON open class UiTest { @get:Rule(order = 0) @@ -48,4 +50,20 @@ open class UiTest { composeRule.onNodeWithTag(drawableId.toString()) .assertIsDisplayed() } + + protected fun goToSearching() { + var buttonDisabled = true + while (buttonDisabled) { + try { + composeRule + .onNodeWithContentDescription(START_BUTTON) + .assertIsEnabled() + buttonDisabled = false + } catch (ex: AssertionError) { + Thread.sleep(100) + } + } + performClick(START_BUTTON) + composeRule.waitForIdle() + } } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 90375b7..9c64075 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -41,7 +41,8 @@ android:icon="@mipmap/poszukiwacz_launcher" android:label="@string/app_name" android:supportsRtl="true" - android:theme="@style/LaunchTheme"> + android:theme="@style/LaunchTheme" + android:testOnly="true">