Merge "Make default dialer tests less flaky" into mnc-dev
diff --git a/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java b/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
index 1c6e9b1..b574a96 100644
--- a/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
@@ -37,12 +37,23 @@
     private TelecomManager mTelecomManager;
     private PhoneAccountHandle mPhoneAccountHandle;
     private String mPreviousDefaultDialer = null;
+    private String mSystemDialer = null;
 
     @Override
     protected void setUp() throws Exception {
         super.setUp();
         mContext = getInstrumentation().getContext();
+
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         mPreviousDefaultDialer = TestUtils.getDefaultDialer(getInstrumentation());
+        // Reset the current dialer to the system dialer, to ensure that we start each test
+        // without being the default dialer.
+        mSystemDialer = TestUtils.getSystemDialer(getInstrumentation());
+        if (!TextUtils.isEmpty(mSystemDialer)) {
+            TestUtils.setDefaultDialer(getInstrumentation(), mSystemDialer);
+        }
         mTelecomManager = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
         final List<PhoneAccountHandle> accounts = mTelecomManager.getCallCapablePhoneAccounts();
         if (accounts != null && !accounts.isEmpty()) {
@@ -53,18 +64,26 @@
     @Override
     protected void tearDown() throws Exception {
         if (!TextUtils.isEmpty(mPreviousDefaultDialer)) {
+            // Restore the default dialer to whatever the default dialer was before the tests
+            // were started. This may or may not be the system dialer.
             TestUtils.setDefaultDialer(getInstrumentation(), mPreviousDefaultDialer);
         }
         super.tearDown();
     }
 
     public void testGetDefaultDialerPackage() throws Exception {
-        assertEquals(mPreviousDefaultDialer, mTelecomManager.getDefaultDialerPackage());
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
+        assertEquals(mSystemDialer, mTelecomManager.getDefaultDialerPackage());
         TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE);
         assertEquals(TestUtils.PACKAGE, mTelecomManager.getDefaultDialerPackage());
     }
 
     public void testVoicemailReadWritePermissions() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         try {
             mContext.getContentResolver().query(Voicemails.CONTENT_URI, null, null, null, null);
             fail("Reading voicemails should throw SecurityException if not default Dialer");
@@ -96,6 +115,9 @@
     }
 
     public void testSilenceRingerPermissions() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         try {
             mTelecomManager.silenceRinger();
             fail("TelecomManager.silenceRinger should throw SecurityException if not default "
@@ -110,6 +132,9 @@
 
     public void testCancelMissedCallsNotificationPermissions()
             throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         try {
             mTelecomManager.cancelMissedCallsNotification();
             fail("TelecomManager.cancelMissedCallsNotification should throw SecurityException if "
@@ -124,6 +149,9 @@
 
     public void testHandlePinMmPermissions()
             throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         try {
             mTelecomManager.handleMmi("0");
             fail("TelecomManager.handleMmi should throw SecurityException if not default dialer");
@@ -143,6 +171,9 @@
     }
 
     public void testGetAdnForPhoneAccountPermissions() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         try {
             mTelecomManager.getAdnUriForPhoneAccount(mPhoneAccountHandle);
             fail("TelecomManager.getAdnUriForPhoneAccount should throw SecurityException if "
@@ -156,6 +187,9 @@
     }
 
     public void testSetDefaultDialerNoDialIntent_notSupported() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         final PackageManager pm = mContext.getPackageManager();
         final ComponentName name = new ComponentName(mContext,
                 "android.telecom.cts.MockDialerActivity");
diff --git a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
index c72e3c2..ddc85a6 100644
--- a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
+++ b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
@@ -46,6 +46,8 @@
 
     private static final String COMMAND_GET_DEFAULT_DIALER = "telecom get-default-dialer";
 
+    private static final String COMMAND_GET_SYSTEM_DIALER = "telecom get-system-dialer";
+
     private static final String COMMAND_ENABLE = "telecom set-phone-account-enabled ";
 
     public static boolean shouldTestTelecom(Context context) {
@@ -65,6 +67,10 @@
         return executeShellCommand(instrumentation, COMMAND_GET_DEFAULT_DIALER);
     }
 
+    public static String getSystemDialer(Instrumentation instrumentation) throws Exception {
+        return executeShellCommand(instrumentation, COMMAND_GET_SYSTEM_DIALER);
+    }
+
     public static void enablePhoneAccount(Instrumentation instrumentation,
             PhoneAccountHandle handle) throws Exception {
         final ComponentName component = handle.getComponentName();
diff --git a/tests/tests/telecom2/src/android/telecom/cts/DefaultDialerOperationsNoPermissionsTest.java b/tests/tests/telecom2/src/android/telecom/cts/DefaultDialerOperationsNoPermissionsTest.java
index 4629992..86f850e 100644
--- a/tests/tests/telecom2/src/android/telecom/cts/DefaultDialerOperationsNoPermissionsTest.java
+++ b/tests/tests/telecom2/src/android/telecom/cts/DefaultDialerOperationsNoPermissionsTest.java
@@ -28,25 +28,40 @@
     private Context mContext;
     private TelecomManager mTelecomManager;
     private String mPreviousDefaultDialer = null;
+    private String mSystemDialer = null;
 
     @Override
     protected void setUp() throws Exception {
         super.setUp();
         mContext = getInstrumentation().getContext();
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         TestUtils.PACKAGE = mContext.getPackageName();
         mPreviousDefaultDialer = TestUtils.getDefaultDialer(getInstrumentation());
+        // Reset the current dialer to the system dialer, to ensure that we start each test
+        // without being the default dialer.
+        mSystemDialer = TestUtils.getSystemDialer(getInstrumentation());
+        if (!TextUtils.isEmpty(mSystemDialer)) {
+            TestUtils.setDefaultDialer(getInstrumentation(), mSystemDialer);
+        }
         mTelecomManager = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
     }
 
     @Override
     protected void tearDown() throws Exception {
         if (!TextUtils.isEmpty(mPreviousDefaultDialer)) {
+            // Restore the default dialer to whatever the default dialer was before the tests
+            // were started. This may or may not be the system dialer.
             TestUtils.setDefaultDialer(getInstrumentation(), mPreviousDefaultDialer);
         }
         super.tearDown();
     }
 
     public void testShowInCallScreenPermissions() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
             @Override
             public void run() {
@@ -56,6 +71,9 @@
     }
 
     public void testGetCallCapableAccountsPermissions() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
             @Override
             public void run() {
@@ -65,6 +83,9 @@
     }
 
     public void testGetDefaultOutgoingPhoneAccount() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
             @Override
             public void run() {
@@ -74,6 +95,9 @@
     }
 
     public void testGetLine1Number() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
             @Override
             public void run() {
@@ -83,6 +107,9 @@
     }
 
     public void testGetVoicemailNumber() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
             @Override
             public void run() {
@@ -92,6 +119,9 @@
     }
 
     public void testIsInCall() throws Exception {
+        if (!TestUtils.shouldTestTelecom(mContext)) {
+            return;
+        }
         verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
             @Override
             public void run() {