Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to add better error reporting when espresso device is used without INTERNET permission. #2067

Merged
1 commit merged into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions espresso/device/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

**Bug Fixes**

* Add better error messaging when process does not have INTERNET permission

**New Features**

**Breaking Changes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="androidx.test.espresso.device" >

<!-- espresso-device uses GRPC to emulator, which requires internet permissions -->
<uses-permission android:name="android.permission.INTERNET"/>

<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="31" />
android:targetSdkVersion="34" />

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package androidx.test.espresso.device.controller.emulator

import android.Manifest.permission
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Process
import android.util.Log
import androidx.annotation.RestrictTo
import androidx.annotation.RestrictTo.Scope
Expand All @@ -25,12 +28,12 @@ import androidx.test.espresso.device.common.getDeviceApiLevel
import androidx.test.espresso.device.common.setAccelerometerRotationSetting
import androidx.test.espresso.device.controller.DeviceControllerOperationException
import androidx.test.espresso.device.controller.DeviceMode
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.platform.device.DeviceController
import androidx.test.platform.device.UnsupportedDeviceOperationException
import com.android.emulator.control.EmulatorControllerGrpc
import com.android.emulator.control.ParameterValue
import com.android.emulator.control.PhysicalModelValue
import com.android.emulator.control.PhysicalModelValue.PhysicalType
import com.android.emulator.control.Posture
import com.android.emulator.control.Posture.PostureValue
import io.grpc.StatusRuntimeException
Expand Down Expand Up @@ -72,12 +75,13 @@ constructor(
PostureValue.POSTURE_HALF_OPENED
}
val posture: Posture = Posture.newBuilder().setValue(postureValue).build()
checkInternetPermission()
try {
emulatorControllerStub.setPosture(posture)
} catch (e: StatusRuntimeException) {
throw DeviceControllerOperationException(
"Failed to set device mode. Please make sure the connected Emulator is foldable, the Android Emulator version" +
" is updated to 33.1.11+, and the controller gRPC service is enabled on the emulator." +
" is updated to 33.1.11+, and the controller gRPC service is enabled on the emulator" +
" See https://developer.android.com/studio/preview/features#set_up_your_project_for_the_espresso_device_api for set up instructions.",
e
)
Expand All @@ -98,6 +102,8 @@ constructor(
}
}

checkInternetPermission()

try {
val physicalModelValue: PhysicalModelValue =
emulatorControllerStub.getPhysicalModel(
Expand Down Expand Up @@ -130,4 +136,23 @@ constructor(
)
}
}

/**
* Making a connection to the Emulator GRPC requires the INTERNET permission. This method checks
* if the current process has the permission, and if not, throws a meaninful error message.
*/
private fun checkInternetPermission() {
if (
getInstrumentation()
.getTargetContext()
.checkPermission(permission.INTERNET, Process.myPid(), Process.myUid()) !=
PERMISSION_GRANTED
) {
throw DeviceControllerOperationException(
"The current process does not have the INTERNET permission. Ensure the app-under-test has '<uses-permission " +
"android:name=\"android.permission.INTERNET\"/>' in its AndroidManifest.xml. See " +
"See https://developer.android.com/studio/preview/features#set_up_your_project_for_the_espresso_device_api for set up instructions."
)
}
}
}