Skip to content

Commit

Permalink
Add PermissionGranter to Barista (#20)
Browse files Browse the repository at this point in the history
* Add PermissionGranter based on InfoJobs' app

* Add PermissionGranter's gradle dependencies

* Use BaristaSleepActions to sleep, avoiding Thread.sleep()

* Introduce PermissionGranter at README.md

* Avoid appcompat-v7 dependency to avoid dependency issues with Barista users

* Use Log.e instead of System.out

* Fix a bad message in the Log.e

* Override uiautomator version to enable Barista to work on projects below API 18

* Add the Exception to Log.e, to give more information about the issue

* Share that PermissionGranter needs API 18
  • Loading branch information
rocboronat authored Feb 22, 2017
1 parent 84c93ec commit 3ec70a9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ assertDrawerIsClosed(R.id.drawer);
assertThatBackButtonClosesTheApp();
```

## Dealing with the runtime permissions dialog

The new Marshmallow permissions system requires checking for permissions at runtime. As Espresso can't interact with the system dialog, Barista offers a way to allow permissions when needed.

```java
PermissionGranter.allowPermissionsIfNeeded(Manifest.permission.GET_ACCOUNTS);
```

# Download

```gradle
Expand Down
2 changes: 2 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ dependencies {
compile('com.android.support.test.espresso:espresso-contrib:2.0') {
exclude module: 'support-v4'
}
compile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
compile 'com.android.support:support-annotations:25.1.0'
}

publish {
Expand Down
8 changes: 7 additions & 1 deletion library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<manifest package="com.schibsted.spain.barista"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:tools="http://schemas.android.com/tools"
package="com.schibsted.spain.barista">

<uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.schibsted.spain.barista.permission;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.util.Log;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static com.schibsted.spain.barista.BaristaSleepActions.sleep;

@RequiresApi(18)
public class PermissionGranter {

private static final int PERMISSIONS_DIALOG_DELAY = 3000;
private static final int GRANT_BUTTON_INDEX = 1;

public static void allowPermissionsIfNeeded(String permissionNeeded) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(InstrumentationRegistry.getTargetContext(),
permissionNeeded)) {
sleep(PERMISSIONS_DIALOG_DELAY);
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject allowPermissions = device.findObject(new UiSelector()
.clickable(true)
.checkable(false)
.index(GRANT_BUTTON_INDEX));
if (allowPermissions.exists()) {
allowPermissions.click();
}
}
} catch (UiObjectNotFoundException e) {
Log.e("Barista", "There is no permissions dialog to interact with", e);
}
}

private static boolean hasNeededPermission(Context context, String permissionNeeded) {
int permissionStatus = checkSelfPermission(context, permissionNeeded);
return permissionStatus == PackageManager.PERMISSION_GRANTED;
}

private static int checkSelfPermission(@NonNull Context context, @NonNull String permission) {
if (permission == null) {
throw new IllegalArgumentException("permission is null");
}
return context.checkPermission(permission, android.os.Process.myPid(), android.os.Process.myUid());
}
}

0 comments on commit 3ec70a9

Please sign in to comment.