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

Permissions API cleanup. #2073

Merged
1 commit merged into from
Jan 8, 2024
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
7 changes: 5 additions & 2 deletions runner/android_junit_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
`androidx.test:runner:{version}` is released.

**Bug Fixes**

* Attempt to clarify limitations and deprecation reasons in RequiresDevice documentation

**New Features**

**Breaking Changes**

**API Changes**

* Mark androidx.test.services.** as RestrictTo LIBRARY_GROUP
* Remove ExperimentalTestApi from CustomFilter
* Remove ExperimentalTestApi from PackagePrefixClasspathSuite
* Remove ExperimentalTestApi from CustomFilter - making it public
* Remove ExperimentalTestApi from PackagePrefixClasspathSuite - make it public
* Mark PermissionRequester as RestrictTo LIBRARY_GROUP instead of ExperimentalTestApi

**Breaking API Changes**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
// Signature format: 3.0
package androidx.test.runner.permission {

@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP) public class PermissionRequester implements androidx.test.internal.platform.content.PermissionGranter {
ctor public PermissionRequester();
method public void addPermissions(java.lang.String!...);
method public void requestPermissions();
method @VisibleForTesting protected void setAndroidRuntimeVersion(int);
}

}

package @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP) @RestrictTo({androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP}) androidx.test.services.events {

@RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP) public final class AnnotationInfo implements android.os.Parcelable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.test.annotation.ExperimentalTestApi;

/** Grants a permission at runtime using a @{link ShellCommand} */
@ExperimentalTestApi
/** Grants a permission at runtime using a {@link ShellCommand} */
class GrantPermissionCallable extends RequestPermissionCallable {

private static final String TAG = "GrantPermissionCallable";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.ChecksSdkIntAtLeast;
import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;
import androidx.test.annotation.ExperimentalTestApi;
import androidx.test.internal.platform.content.PermissionGranter;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.permission.UiAutomationShellCommand.PmCommand;
Expand All @@ -45,17 +47,19 @@
* #addPermissions(String...)} to add a permission to the permission list. To request all
* permissions use the {@link #requestPermissions()} method.
*
* <p>Note: Usually this class would not be used directly, but through {@link
* androidx.test.rule.GrantPermissionRule}.
* <p>Note: This class is not intended to be used directly, but rather through {@link
* androidx.test.rule.GrantPermissionRule}. For tests running on Android SDKs >= API 28, use {@link
* android.app.UiAutomation#grantRuntimePermission(String, String)} instead.
*
* <p><b>This API is currently in beta.</b>
* @hide
*/
@ExperimentalTestApi
@RestrictTo(Scope.LIBRARY_GROUP) // used by rule
@TargetApi(value = 23)
public class PermissionRequester implements PermissionGranter {

private static final String TAG = "PermissionRequester";

@ChecksSdkIntAtLeast(extension = 0)
private int androidRuntimeVersion = Build.VERSION.SDK_INT;

@NonNull private final Context targetContext;
Expand All @@ -81,6 +85,7 @@ public PermissionRequester() {
*
* @param permissions a list of Android runtime permissions.
*/
@Override
public void addPermissions(@NonNull String... permissions) {
checkNotNull(permissions, "permissions cannot be null!");
if (deviceSupportsRuntimePermissions()) {
Expand All @@ -103,6 +108,7 @@ public void addPermissions(@NonNull String... permissions) {
* <p>Precondition: This method does nothing when called on an API level lower than {@link
* Build.VERSION_CODES#M}.
*/
@Override
public void requestPermissions() {
if (deviceSupportsRuntimePermissions()) {
for (RequestPermissionCallable requestPermissionCallable : requestedPermissions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import android.content.pm.PackageManager;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;
import androidx.test.annotation.ExperimentalTestApi;
import androidx.test.runner.permission.RequestPermissionCallable.Result;
import java.util.Objects;
import java.util.concurrent.Callable;
Expand All @@ -33,9 +34,11 @@
* Base class for runtime permission {@link Callable}s.
*
* <p>Note: This class is visible only for testing. Please do not use it directly.
*
* @hide
*/
@VisibleForTesting
@ExperimentalTestApi
@RestrictTo(Scope.LIBRARY)
public abstract class RequestPermissionCallable implements Callable<Result> {

private final ShellCommand shellCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
package androidx.test.runner.permission;

import android.app.UiAutomation;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;
import androidx.test.annotation.ExperimentalTestApi;

/**
* Ideally we wouldn't need this abstraction but since {@link UiAutomation} is final we need an
* abstraction on top to be able to mock it in tests. It also gives us the flexibility to switch to
* another implementation in the future.
*
* @hide
*/
@VisibleForTesting
@ExperimentalTestApi
@RestrictTo(Scope.LIBRARY)
public abstract class ShellCommand {

/** Characters that have no special meaning to the shell. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.test.InstrumentationRegistry;
import androidx.test.annotation.ExperimentalTestApi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -36,7 +35,6 @@
* runtime.
*/
@TargetApi(value = 23)
@ExperimentalTestApi
class UiAutomationShellCommand extends ShellCommand {

private static final String TAG = "UiAutomationShellCmd";
Expand Down
2 changes: 2 additions & 0 deletions runner/rules/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

**API Changes**

* Recommend use of UiAutomation#grantRuntimePermissions instead of GrantPermissionRule

**Breaking API Changes**

**Known Issues**
3 changes: 3 additions & 0 deletions runner/rules/java/androidx/test/rule/GrantPermissionRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
*
* <p>See <a href="https://developer.android.com/training/permissions/requesting">Request App
* Permissions</a> for more details on runtime permissions.
*
* <p>For tests running on Android SDKs >= API 28, use
* {@link android.app.UiAutomation#grantRuntimePermission(String, String)} instead.
*/
public class GrantPermissionRule implements TestRule {

Expand Down