Add Test for missing absent SIM + PIN lock case

When sorting for the SIM to use when absent+PIN locked, we
were incorrectly choosing the absent case. This was fixed in
aosp/1120355. Adding regression test case for this.

Also fix long-standing broken test

Test: atest TeleServiceTests
Change-Id: I5c7af642c42b51808189d865621272612c8e6c95
diff --git a/tests/src/com/android/phone/CallFeaturesSettingTest.java b/tests/src/com/android/phone/CallFeaturesSettingTest.java
index 15d48ba..0666c56 100644
--- a/tests/src/com/android/phone/CallFeaturesSettingTest.java
+++ b/tests/src/com/android/phone/CallFeaturesSettingTest.java
@@ -24,10 +24,10 @@
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.when;
 
+import android.app.KeyguardManager;
 import android.content.Context;
 
 import androidx.test.InstrumentationRegistry;
-import androidx.test.filters.FlakyTest;
 import androidx.test.rule.ActivityTestRule;
 
 import com.android.internal.telephony.IccCard;
@@ -49,18 +49,18 @@
     IccCard mMockIccCard;
     @Rule
     public ActivityTestRule<CallFeaturesSetting> mRule =
-            new ActivityTestRule<>(CallFeaturesSetting.class);
+            new ActivityTestRule<>(CallFeaturesSetting.class, false, true);
     private CallFeaturesSetting mActivity;
 
     @Before
-    public void setUp() {
+    public void setUp() throws Throwable {
         MockitoAnnotations.initMocks(this);
         mActivity = mRule.getActivity();
         Context targetContext = InstrumentationRegistry.getTargetContext();
         doReturn(targetContext).when(mMockPhone).getContext();
+        keepScreenOn(mRule, mActivity);
     }
 
-    @FlakyTest
     @Test
     public void onResume_fdnIsAvailable_shouldShowFdnMenu() throws NoSuchFieldException,
             IllegalAccessException {
@@ -69,13 +69,12 @@
         when(mMockIccCard.getIccFdnAvailable()).thenReturn(true);
         getField("mPhone").set(mActivity, mMockPhone);
 
-        mActivity.onResume();
+        mActivity.runOnUiThread(() -> mActivity.onResume());
 
         // Check the FDN menu is displayed.
         onView(withText(R.string.fdn)).check(matches(isDisplayed()));
     }
 
-    @FlakyTest
     @Test
     public void onResume_iccCardIsNull_shouldNotShowFdnMenu() throws NoSuchFieldException,
             IllegalAccessException {
@@ -83,13 +82,12 @@
         when(mMockPhone.getIccCard()).thenReturn(null);
         getField("mPhone").set(mActivity, mMockPhone);
 
-        mActivity.onResume();
+        mActivity.runOnUiThread(() -> mActivity.onResume());
 
         // Check the FDN menu is not displayed.
         onView(withText(R.string.fdn)).check(doesNotExist());
     }
 
-    @FlakyTest
     @Test
     public void onResume_fdnIsNotAvailable_shouldNotShowFdnMenu() throws NoSuchFieldException,
             IllegalAccessException {
@@ -98,7 +96,7 @@
         when(mMockIccCard.getIccFdnAvailable()).thenReturn(false);
         getField("mPhone").set(mActivity, mMockPhone);
 
-        mActivity.onResume();
+        mActivity.runOnUiThread(() -> mActivity.onResume());
 
         // Check the FDN menu is not displayed.
         onView(withText(R.string.fdn)).check(doesNotExist());
@@ -109,4 +107,19 @@
         field.setAccessible(true);
         return field;
     }
+
+    /**
+     * Automatically wake up device to perform tests.
+     */
+    private static void keepScreenOn(ActivityTestRule activityTestRule,
+            final CallFeaturesSetting activity) throws Throwable {
+        activityTestRule.runOnUiThread(() -> {
+            activity.setTurnScreenOn(true);
+            activity.setShowWhenLocked(true);
+            KeyguardManager keyguardManager =
+                    activity.getSystemService(KeyguardManager.class);
+            keyguardManager.requestDismissKeyguard(activity, null);
+        });
+        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+    }
 }
diff --git a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
index ab4c067..2dee0e1 100644
--- a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
+++ b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
@@ -280,6 +280,37 @@
 
     /**
      * Prerequisites:
+     * - MSIM Device, only slot 1 inserted and PUK locked
+     * - slot 1 has higher capabilities
+     *
+     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone because it is the only one
+     * with a SIM inserted (even if it is PUK locked)
+     */
+    @Test
+    @SmallTest
+    public void testSlot1PinLockedAndSlot0Absent() {
+        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
+                false /*isEmergencyOnly*/);
+        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
+                false /*isEmergencyOnly*/);
+        setDefaultPhone(slot0Phone);
+        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
+        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_ABSENT);
+        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_PIN_REQUIRED);
+        // Slot 1 has more capabilities
+        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_GSM);
+        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_LTE);
+        // Slot 1 has SIM inserted.
+        setSlotHasIccCard(SLOT_0_PHONE_ID, false /*isInserted*/);
+        setSlotHasIccCard(SLOT_1_PHONE_ID, true /*isInserted*/);
+
+        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
+
+        assertEquals(slot1Phone, resultPhone);
+    }
+
+    /**
+     * Prerequisites:
      * - MSIM Device, two slots with SIMs inserted
      * - Slot 1 is LTE capable, Slot 0 is GSM capable
      *