Skip to content

Commit

Permalink
feat(android): add support android:mock_location
Browse files Browse the repository at this point in the history
  • Loading branch information
Malinskiy committed Dec 13, 2024
1 parent 8b5087f commit f5b1f43
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const val DEFAULT_APPLICATION_PM_CLEAR = false
const val DEFAULT_TEST_APPLICATION_PM_CLEAR = false
const val DEFAULT_INSTALL_OPTIONS = ""
const val DEFAULT_WAIT_FOR_DEVICES_TIMEOUT = 30000L
const val DEFAULT_MOCK_LOCATION = false

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
Expand Down Expand Up @@ -77,6 +78,7 @@ sealed class VendorConfiguration {
@JsonProperty("adbServers") val adbServers: List<AdbEndpoint> = listOf(AdbEndpoint()),
@JsonProperty("disableWindowAnimation") val disableWindowAnimation: Boolean = DEFAULT_DISABLE_WINDOW_ANIMATION,
@JsonProperty("profilingConfiguration") val profilingConfiguration: ProfilingConfiguration = ProfilingConfiguration(),
@JsonProperty("mockLocation") val mockLocation: Boolean = DEFAULT_MOCK_LOCATION
) : VendorConfiguration() {
fun safeAndroidSdk(): File = androidSdk ?: throw ConfigurationException("No android SDK path specified")

Expand Down Expand Up @@ -125,6 +127,8 @@ sealed class VendorConfiguration {
var testAccessConfiguration: TestAccessConfiguration = TestAccessConfiguration()
var adbServers: List<AdbEndpoint> = listOf(AdbEndpoint())
var disableWindowAnimation: Boolean = DEFAULT_DISABLE_WINDOW_ANIMATION
var profilingConfiguration: ProfilingConfiguration = ProfilingConfiguration()
var mockLocation: Boolean = DEFAULT_MOCK_LOCATION

fun build() = AndroidConfiguration(
androidSdk,
Expand All @@ -149,7 +153,9 @@ sealed class VendorConfiguration {
testParserConfiguration,
testAccessConfiguration,
adbServers,
disableWindowAnimation
disableWindowAnimation,
profilingConfiguration,
mockLocation,
)
}

Expand Down
15 changes: 15 additions & 0 deletions docs/runner/android/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,21 @@ marathon {
</TabItem>
</Tabs>

### Location mock access
Some tests require mocking device location. Marathon can setup access for instrumentation package if you enable this option as following:

<Tabs>
<TabItem value="YAML" label="Marathonfile">

```yaml
vendorConfiguration:
type: "Android"
mockLocation: true
```

</TabItem>
</Tabs>

[1]: https://developer.android.com/studio/

[2]: https://developer.android.com/studio/command-line/adb#issuingcommands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,27 @@ class AndroidAppInstaller(configuration: Configuration) {

logger.debug { "Installing instrumentation package to ${device.serialNumber}" }
reinstall(device, applicationInfo.instrumentationPackage, bundle.testApplication)
appops(device, applicationInfo.instrumentationPackage)
logger.debug { "Prepare installation finished for ${device.serialNumber}" }
}

private suspend fun appops(device: AndroidDevice, instrumentationPackage: String) {
if (androidConfiguration.mockLocation) {
if (device.apiLevel < 23) {
logger.warn { "Can't setup mock location: device ${device.serialNumber} doesn't support appops" }
return
}

val appopsMessage = device.criticalExecuteShellCommand("appops set $instrumentationPackage android:mock_location allow")
appopsMessage.let {
if (it.exitCode != 0) {
val (output, _) = device.criticalExecuteShellCommand("appops query-op android:mock_location allow")
logger.error { "Can't set android:mock_location on $instrumentationPackage. List of apps currently using android:mock_location:$output" }
}
}
}
}

/**
* @throws DeviceSetupException if unable to reinstall (even with retries)
*/
Expand Down

0 comments on commit f5b1f43

Please sign in to comment.