Skip to content

Commit f5b1f43

Browse files
committed
feat(android): add support android:mock_location
1 parent 8b5087f commit f5b1f43

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

configuration/src/main/kotlin/com/malinskiy/marathon/config/vendor/VendorConfiguration.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const val DEFAULT_APPLICATION_PM_CLEAR = false
3838
const val DEFAULT_TEST_APPLICATION_PM_CLEAR = false
3939
const val DEFAULT_INSTALL_OPTIONS = ""
4040
const val DEFAULT_WAIT_FOR_DEVICES_TIMEOUT = 30000L
41+
const val DEFAULT_MOCK_LOCATION = false
4142

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

@@ -125,6 +127,8 @@ sealed class VendorConfiguration {
125127
var testAccessConfiguration: TestAccessConfiguration = TestAccessConfiguration()
126128
var adbServers: List<AdbEndpoint> = listOf(AdbEndpoint())
127129
var disableWindowAnimation: Boolean = DEFAULT_DISABLE_WINDOW_ANIMATION
130+
var profilingConfiguration: ProfilingConfiguration = ProfilingConfiguration()
131+
var mockLocation: Boolean = DEFAULT_MOCK_LOCATION
128132

129133
fun build() = AndroidConfiguration(
130134
androidSdk,
@@ -149,7 +153,9 @@ sealed class VendorConfiguration {
149153
testParserConfiguration,
150154
testAccessConfiguration,
151155
adbServers,
152-
disableWindowAnimation
156+
disableWindowAnimation,
157+
profilingConfiguration,
158+
mockLocation,
153159
)
154160
}
155161

docs/runner/android/configure.md

+15
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,21 @@ marathon {
10891089
</TabItem>
10901090
</Tabs>
10911091

1092+
### Location mock access
1093+
Some tests require mocking device location. Marathon can setup access for instrumentation package if you enable this option as following:
1094+
1095+
<Tabs>
1096+
<TabItem value="YAML" label="Marathonfile">
1097+
1098+
```yaml
1099+
vendorConfiguration:
1100+
type: "Android"
1101+
mockLocation: true
1102+
```
1103+
1104+
</TabItem>
1105+
</Tabs>
1106+
10921107
[1]: https://developer.android.com/studio/
10931108

10941109
[2]: https://developer.android.com/studio/command-line/adb#issuingcommands

vendor/vendor-android/src/main/kotlin/com/malinskiy/marathon/android/AndroidAppInstaller.kt

+18
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,27 @@ class AndroidAppInstaller(configuration: Configuration) {
5353

5454
logger.debug { "Installing instrumentation package to ${device.serialNumber}" }
5555
reinstall(device, applicationInfo.instrumentationPackage, bundle.testApplication)
56+
appops(device, applicationInfo.instrumentationPackage)
5657
logger.debug { "Prepare installation finished for ${device.serialNumber}" }
5758
}
5859

60+
private suspend fun appops(device: AndroidDevice, instrumentationPackage: String) {
61+
if (androidConfiguration.mockLocation) {
62+
if (device.apiLevel < 23) {
63+
logger.warn { "Can't setup mock location: device ${device.serialNumber} doesn't support appops" }
64+
return
65+
}
66+
67+
val appopsMessage = device.criticalExecuteShellCommand("appops set $instrumentationPackage android:mock_location allow")
68+
appopsMessage.let {
69+
if (it.exitCode != 0) {
70+
val (output, _) = device.criticalExecuteShellCommand("appops query-op android:mock_location allow")
71+
logger.error { "Can't set android:mock_location on $instrumentationPackage. List of apps currently using android:mock_location:$output" }
72+
}
73+
}
74+
}
75+
}
76+
5977
/**
6078
* @throws DeviceSetupException if unable to reinstall (even with retries)
6179
*/

0 commit comments

Comments
 (0)