Skip to content

Commit

Permalink
#17 in progress - espresso test for updating location
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Oct 4, 2024
1 parent e49596b commit b2bc43c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ android {
versionCode 9
versionName "0.9"
testInstrumentationRunner "pl.marianjureczko.poszukiwacz.CustomTestRunner"
testInstrumentationRunnerArguments["grantPermissions"] = "true"
manifestPlaceholders = [facebookToken: FACEBOOK_TOKEN]
javaCompileOptions {
annotationProcessorOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Context>()
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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import pl.marianjureczko.poszukiwacz.ui.isOnStack
import java.net.URLEncoder

const val COMPASS = "Compass"
const val STEPS_TO_TREASURE = "Steps to treasure"

@OptIn(ExperimentalPermissionsApi::class)
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
Expand Down Expand Up @@ -263,7 +264,9 @@ fun Steps(stepsToTreasure: Int?) {
) {
if (stepsToTreasure != null) {
Text(
modifier = Modifier.padding(start = 40.dp),
modifier = Modifier
.padding(start = 40.dp)
.semantics { contentDescription = STEPS_TO_TREASURE },
style = MaterialTheme.typography.h2,
color = Color.Gray,
text = stepsToTreasure.toString()
Expand Down

0 comments on commit b2bc43c

Please sign in to comment.