Skip to content

Commit

Permalink
feat(android): add support android:mock_location (#989)
Browse files Browse the repository at this point in the history
* feat(android): add support android:mock_location

* feat(android): android:mock_location is only for application package

---------

Co-authored-by: eugene_matsyuk <[email protected]>
  • Loading branch information
Malinskiy and matzuk authored Dec 14, 2024
1 parent eb63e88 commit 39fb827
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 application 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 @@ -41,6 +41,7 @@ class AndroidAppInstaller(configuration: Configuration) {
}
}
reinstall(device, applicationInfo.applicationPackage, applicationApk, bundle.splitApks ?: emptyList())
appops(device, applicationInfo.applicationPackage)
}

bundle.extraApplications?.let { extraApplications ->
Expand All @@ -56,6 +57,23 @@ class AndroidAppInstaller(configuration: Configuration) {
logger.debug { "Prepare installation finished for ${device.serialNumber}" }
}

private suspend fun appops(device: AndroidDevice, applicationPackage: 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 $applicationPackage 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 $applicationPackage. List of apps currently using android:mock_location:$output" }
}
}
}
}

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

0 comments on commit 39fb827

Please sign in to comment.