Skip to content

Instantly share code, notes, and snippets.

View Egorand's full-sized avatar
🔲
sup sup

Egor Andreevich Egorand

🔲
sup sup
View GitHub Profile
// Test.java
public class Test {
public String foo() {
return null;
}
}
// Main.kt
fun main() {
print(Test().foo().toString())
@Egorand
Egorand / ContactsSyncTest.java
Created July 17, 2016 12:03
android-testing-runtime-permissions-should-load-contacts
@Test
public void d_shouldLoadContactsIfPermissionWasGranted() throws Exception {
for (Contact contact : TEST_CONTACTS) {
onView(withText(contact.name)).check(matches(isDisplayed()));
onView(withText(contact.phoneNumber)).check(matches(isDisplayed()));
}
}
@Egorand
Egorand / MainActivity.java
Created July 17, 2016 12:02
android-testing-runtime-permissions-on-grant-permission
public void onGrantPermission(View view) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS},
REQ_CODE_PERMISSIONS_READ_CONTACTS);
} else {
goToSettings();
}
}
private void goToSettings() {
@Egorand
Egorand / MainActivity.java
Created July 17, 2016 12:01
android-testing-runtime-permissions-showing-long-rationale
...
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
permissionDeniedRationaleView.setText(R.string.permission_denied_rationale_short);
} else {
permissionDeniedRationaleView.setText(R.string.permission_denied_rationale_long);
}
...
@Egorand
Egorand / UiAutomatorUtils.java
Created July 17, 2016 12:00
android-testing-runtime-permissions-grant-permission
public static void grantPermission(UiDevice device, String permissionTitle) throws UiObjectNotFoundException {
UiObject permissionEntry = device.findObject(new UiSelector().text(permissionTitle));
permissionEntry.click();
}
@Egorand
Egorand / UiAutomatorUtils.java
Created July 17, 2016 11:59
android-testing-runtime-permissions-open-permissions
public static void openPermissions(UiDevice device) throws UiObjectNotFoundException {
UiObject permissions = device.findObject(new UiSelector().text(TEXT_PERMISSIONS));
permissions.click();
}
@Egorand
Egorand / UiAutomatorUtils.java
Created July 17, 2016 11:58
android-testing-runtime-permissions-deny-current-permission-permanently
public static void denyCurrentPermissionPermanently(UiDevice device) throws UiObjectNotFoundException {
UiObject neverAskAgainCheckbox = device.findObject(new UiSelector().text(TEXT_NEVER_ASK_AGAIN));
neverAskAgainCheckbox.click();
denyCurrentPermission(device);
}
@Egorand
Egorand / ContactsSyncTest.java
Created July 17, 2016 11:57
android-testing-runtime-permissions-should-display-long-rationale
@Test
public void c_shouldDisplayLongRationaleIfPermissionWasDeniedPermanently() throws Exception {
denyCurrentPermissionPermanently(device);
onView(withText(R.string.permission_denied_rationale_long)).check(matches(isDisplayed()));
onView(withText(R.string.grant_permission)).check(matches(isDisplayed()));
// will grant the permission for the next test
onView(withText(R.string.grant_permission)).perform(click());
openPermissions(device);
@Egorand
Egorand / MainActivity.java
Created July 17, 2016 11:56
android-testing-runtime-permissions-showing-rationale
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQ_CODE_PERMISSIONS_READ_CONTACTS && grantResults.length > 0) {
int grantResult = grantResults[0];
if (grantResult == PackageManager.PERMISSION_GRANTED) {
loadContacts();
} else {
isPermissionAlreadyDenied = true;
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
@Egorand
Egorand / ContactsSyncTest.java
Created July 17, 2016 11:54
android-testing-runtime-permissions-should-display-short-rationale
@Test
public void b_shouldDisplayShortRationaleIfPermissionWasDenied() throws Exception {
denyCurrentPermission(device);
onView(withText(R.string.permission_denied_rationale_short)).check(matches(isDisplayed()));
onView(withText(R.string.grant_permission)).check(matches(isDisplayed()));
}