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