Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 1 | package com.android.contacts; |
| 2 | |
| 3 | import android.Manifest; |
| 4 | import android.content.Context; |
| 5 | import android.content.Intent; |
Marcus Hagerott | 03a8bb0 | 2016-10-06 08:50:01 -0700 | [diff] [blame] | 6 | import android.content.pm.PackageManager; |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 7 | import android.support.test.InstrumentationRegistry; |
| 8 | import android.support.test.filters.MediumTest; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 9 | import android.support.test.filters.Suppress; |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 10 | import android.support.test.runner.AndroidJUnit4; |
| 11 | import android.support.test.uiautomator.By; |
| 12 | import android.support.test.uiautomator.UiDevice; |
| 13 | import android.support.test.uiautomator.UiObject2; |
| 14 | import android.support.test.uiautomator.Until; |
| 15 | |
| 16 | import org.junit.Before; |
| 17 | import org.junit.Test; |
| 18 | import org.junit.runner.RunWith; |
| 19 | |
| 20 | import static com.android.contacts.common.util.PermissionsUtil.hasPermission; |
| 21 | import static org.junit.Assume.assumeTrue; |
| 22 | |
| 23 | /** |
| 24 | * Make sure the app doesn't crash when it is started without permissions. Note: this won't |
| 25 | * run in most environments because permissions will already have been granted. |
| 26 | * |
| 27 | * To exercise this run: |
| 28 | * |
| 29 | * $ adb shell pm revoke com.android.contacts android.permission.READ_CONTACTS |
| 30 | * $ adb shell pm revoke com.android.contacts android.permission.WRITE_CONTACTS |
| 31 | * $ adb shell pm revoke com.android.contacts android.permission.GET_ACCOUNTS |
| 32 | * $ adb shell pm revoke com.android.contacts android.permission.READ_PHONE_STATE |
| 33 | * $ adb shell pm revoke com.android.contacts android.permission.READ_CALL_LOG |
| 34 | * $ adb shell pm revoke com.android.contacts android.permission.CALL_PHONE |
| 35 | * $ adb shell am instrument -w \ |
| 36 | * com.google.android.contacts.tests/android.support.test.runner.AndroidJUnitRunner \ |
| 37 | * -e class com.android.contacts.NoPermissionsLaunchSmokeTest |
| 38 | */ |
| 39 | @MediumTest |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 40 | // suppressed because failed assumptions are reported as test failures by the build server |
| 41 | @Suppress |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 42 | @RunWith(AndroidJUnit4.class) |
| 43 | public class NoPermissionsLaunchSmokeTest { |
| 44 | private static final long TIMEOUT = 5000; |
| 45 | |
| 46 | private Context mTargetContext; |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() throws Exception { |
| 50 | mTargetContext = InstrumentationRegistry.getTargetContext(); |
| 51 | assumeTrue(!hasPermission(mTargetContext, Manifest.permission.READ_CONTACTS)); |
| 52 | assumeTrue(!hasPermission(mTargetContext, Manifest.permission.WRITE_CONTACTS)); |
| 53 | assumeTrue(!hasPermission(mTargetContext, Manifest.permission.GET_ACCOUNTS)); |
| 54 | assumeTrue(!hasPermission(mTargetContext, Manifest.permission.READ_PHONE_STATE)); |
| 55 | assumeTrue(!hasPermission(mTargetContext, Manifest.permission.READ_CALL_LOG)); |
| 56 | assumeTrue(!hasPermission(mTargetContext, Manifest.permission.CALL_PHONE)); |
| 57 | |
| 58 | // remove state that might exist outside of the app |
| 59 | // (e.g. launcher shortcuts and scheduled jobs) |
| 60 | DynamicShortcuts.reset(mTargetContext); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void launchingMainActivityDoesntCrash() throws Exception { |
| 65 | final UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); |
| 66 | |
| 67 | // Launch the main activity |
| 68 | InstrumentationRegistry.getContext().startActivity( |
| 69 | new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_DEFAULT) |
| 70 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) |
| 71 | .setPackage(InstrumentationRegistry.getTargetContext().getPackageName())); |
| 72 | |
| 73 | device.waitForIdle(); |
| 74 | |
| 75 | device.wait(Until.hasObject(By.textStartsWith("Allow Contacts")), TIMEOUT); |
| 76 | final UiObject2 grantContactsPermissionButton = device.findObject(By.text("ALLOW")); |
| 77 | |
| 78 | grantContactsPermissionButton.click(); |
| 79 | |
| 80 | device.wait(Until.hasObject(By.textEndsWith("make and manage phone calls?")), TIMEOUT); |
| 81 | |
Marcus Hagerott | 03a8bb0 | 2016-10-06 08:50:01 -0700 | [diff] [blame] | 82 | final PackageManager packageManager = mTargetContext.getPackageManager(); |
| 83 | if (!packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) { |
| 84 | device.waitForIdle(); |
| 85 | return; |
| 86 | } |
| 87 | |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 88 | final UiObject2 grantPhonePermissionButton = device.findObject(By.text("ALLOW")); |
| 89 | |
| 90 | grantPhonePermissionButton.clickAndWait(Until.newWindow(), TIMEOUT); |
| 91 | |
| 92 | // Not sure if this actually waits for the load to complete or not. |
| 93 | device.waitForIdle(); |
| 94 | } |
| 95 | |
| 96 | // TODO: it would be good to have similar tests for other entry points that might be reached |
| 97 | // without required permissions. |
| 98 | } |