Merge "Check TTY enable flag and capability of IMS while selecting call domain."
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index 27e1606..d2c0e6b 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -1071,7 +1071,8 @@
         mSelectedNetworkOperatorName.remove(subId);
     }
 
-    private static long getTimeStamp() {
+    @VisibleForTesting
+    public long getTimeStamp() {
         return SystemClock.elapsedRealtime();
     }
 }
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index bf04d03..5b9da07 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -2287,7 +2287,7 @@
 
         if (reasonInfo != null
                 && reasonInfo.getCode() == ImsReasonInfo.CODE_SIP_ALTERNATE_EMERGENCY_CALL) {
-            onEmergencyRedial(c, c.getPhone());
+            onEmergencyRedial(c, c.getPhone().getDefaultPhone());
             return true;
         }
 
@@ -2322,7 +2322,7 @@
             if (future != null) {
                 future.thenAcceptAsync((result) -> {
                     Log.d(this, "reselectDomain-complete");
-                    onEmergencyRedialOnDomain(c, c.getPhone(), result);
+                    onEmergencyRedialOnDomain(c, c.getPhone().getDefaultPhone(), result);
                 }, mDomainSelectionMainExecutor);
                 return true;
             }
diff --git a/tests/src/com/android/phone/NotificationMgrTest.java b/tests/src/com/android/phone/NotificationMgrTest.java
index 3e8cf28..e009446 100644
--- a/tests/src/com/android/phone/NotificationMgrTest.java
+++ b/tests/src/com/android/phone/NotificationMgrTest.java
@@ -44,8 +44,10 @@
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.anyInt;
 import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.eq;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -58,6 +60,7 @@
 import android.content.res.Resources;
 import android.os.Build;
 import android.os.PersistableBundle;
+import android.os.SystemClock;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.telecom.TelecomManager;
@@ -89,6 +92,7 @@
 import org.mockito.MockitoAnnotations;
 
 import java.util.Collections;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Unit Test for NotificationMgr
@@ -128,6 +132,7 @@
 
     private Phone[] mPhones;
     private NotificationMgr mNotificationMgr;
+    private TestableLooper mTestableLooper;
 
     @Before
     public void setUp() throws Exception {
@@ -182,7 +187,10 @@
         when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
         when(mTelephonyManager.getServiceState()).thenReturn(mServiceState);
 
-        mNotificationMgr = new NotificationMgr(mApp);
+        mTestableLooper = TestableLooper.get(this);
+        // Spy it only to avoid sleep for SystemClock.elapsedRealtime()
+        mNotificationMgr = spy(new NotificationMgr(mApp));
+        mTestableLooper.processAllMessages();
     }
 
     @Test
@@ -278,10 +286,7 @@
         prepareResourcesForNetworkSelection();
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(2000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(2 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verify(mNotificationManager, never()).notify(any(), anyInt(), any());
@@ -298,12 +303,11 @@
         config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true);
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
+        // update to OOS as base state
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        // TODO: use effective TestLooper time eclipse instead of sleeping
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        // 10 seconds later
+        moveTimeForward(10 /* seconds */);
+        // verify the behavior on new request
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
@@ -322,10 +326,7 @@
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE,
                 INVALID_SUBSCRIPTION_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE,
                 INVALID_SUBSCRIPTION_ID);
 
@@ -339,10 +340,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(null);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verify(mNotificationManager, never()).notify(any(), anyInt(), any());
@@ -362,10 +360,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verify(mNotificationManager, never()).notify(any(), anyInt(), any());
@@ -386,10 +381,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verify(mNotificationManager, never()).notify(any(), anyInt(), any());
@@ -410,10 +402,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verify(mNotificationManager, never()).notify(any(), anyInt(), any());
@@ -439,10 +428,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verify(mNotificationManager, never()).notify(any(), anyInt(), any());
@@ -469,10 +455,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verify(mNotificationManager, never()).notify(any(), anyInt(), any());
@@ -500,10 +483,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
@@ -524,10 +504,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
@@ -549,10 +526,7 @@
         when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
@@ -572,10 +546,7 @@
         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
 
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
-        try {
-            Thread.sleep(10000);
-        } catch (InterruptedException ignored) {
-        }
+        moveTimeForward(10 /* seconds */);
         mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID);
 
         verify(mNotificationManager, never()).notify(any(), anyInt(), any());
@@ -661,4 +632,11 @@
         when(mApp.getString(R.string.mobile_network_settings_class)).thenReturn(
                 MOBILE_NETWORK_SELECTION_CLASS);
     }
+
+    private void moveTimeForward(long seconds) {
+        final long millis = TimeUnit.SECONDS.toMillis(seconds);
+        mTestableLooper.moveTimeForward(millis);
+        mTestableLooper.processAllMessages();
+        doReturn(SystemClock.elapsedRealtime() + millis).when(mNotificationMgr).getTimeStamp();
+    }
 }
diff --git a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
index a1d7405..e8365b0 100644
--- a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
+++ b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
@@ -1527,9 +1527,10 @@
         int selectedDomain = DOMAIN_CS;
 
         setupForDialForDomainSelection(mPhone0, selectedDomain, true);
+        doReturn(mPhone0).when(mImsPhone).getDefaultPhone();
 
         TestTelephonyConnection c = setupForReDialForDomainSelection(
-                mPhone0, selectedDomain, preciseDisconnectCause, disconnectCause, false);
+                mImsPhone, selectedDomain, preciseDisconnectCause, disconnectCause, false);
         c.setEmergencyServiceCategory(eccCategory);
         c.setAddress(TEST_ADDRESS, TelecomManager.PRESENTATION_ALLOWED);
 
@@ -1566,9 +1567,10 @@
         int selectedDomain = DOMAIN_PS;
 
         setupForDialForDomainSelection(mPhone0, selectedDomain, true);
+        doReturn(mPhone0).when(mImsPhone).getDefaultPhone();
 
         TestTelephonyConnection c = setupForReDialForDomainSelection(
-                mPhone0, selectedDomain, preciseDisconnectCause, disconnectCause, false);
+                mImsPhone, selectedDomain, preciseDisconnectCause, disconnectCause, false);
         c.setEmergencyServiceCategory(eccCategory);
         c.setAddress(TEST_ADDRESS, TelecomManager.PRESENTATION_ALLOWED);