1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.phone; 17 18 import static androidx.test.espresso.Espresso.onView; 19 import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist; 20 import static androidx.test.espresso.assertion.ViewAssertions.matches; 21 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; 22 import static androidx.test.espresso.matcher.ViewMatchers.withText; 23 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.when; 26 27 import android.app.KeyguardManager; 28 import android.content.Context; 29 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.filters.FlakyTest; 32 import androidx.test.rule.ActivityTestRule; 33 34 import com.android.internal.telephony.IccCard; 35 import com.android.internal.telephony.Phone; 36 import com.android.internal.telephony.PhoneConstants; 37 38 import org.junit.Before; 39 import org.junit.Ignore; 40 import org.junit.Rule; 41 import org.junit.Test; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 45 import java.lang.reflect.Field; 46 47 public class CallFeaturesSettingTest { 48 @Mock 49 Phone mMockPhone; 50 @Mock 51 IccCard mMockIccCard; 52 @Rule 53 public ActivityTestRule<CallFeaturesSetting> mRule = 54 new ActivityTestRule<>(CallFeaturesSetting.class, false, true); 55 private CallFeaturesSetting mActivity; 56 57 @Before setUp()58 public void setUp() throws Throwable { 59 MockitoAnnotations.initMocks(this); 60 mActivity = mRule.getActivity(); 61 Context targetContext = InstrumentationRegistry.getTargetContext(); 62 doReturn(targetContext).when(mMockPhone).getContext(); 63 keepScreenOn(mRule, mActivity); 64 } 65 66 @Ignore 67 @FlakyTest 68 @Test onResume_fdnIsAvailable_shouldShowFdnMenu()69 public void onResume_fdnIsAvailable_shouldShowFdnMenu() throws NoSuchFieldException, 70 IllegalAccessException { 71 when(mMockPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM); 72 when(mMockPhone.getIccCard()).thenReturn(mMockIccCard); 73 when(mMockIccCard.getIccFdnAvailable()).thenReturn(true); 74 getField("mPhone").set(mActivity, mMockPhone); 75 76 mActivity.runOnUiThread(() -> mActivity.onResume()); 77 78 // Check the FDN menu is displayed. 79 onView(withText(R.string.fdn)).check(matches(isDisplayed())); 80 } 81 82 @Ignore 83 @FlakyTest 84 @Test onResume_iccCardIsNull_shouldNotShowFdnMenu()85 public void onResume_iccCardIsNull_shouldNotShowFdnMenu() throws NoSuchFieldException, 86 IllegalAccessException { 87 when(mMockPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM); 88 when(mMockPhone.getIccCard()).thenReturn(null); 89 getField("mPhone").set(mActivity, mMockPhone); 90 91 mActivity.runOnUiThread(() -> mActivity.onResume()); 92 93 // Check the FDN menu is not displayed. 94 onView(withText(R.string.fdn)).check(doesNotExist()); 95 } 96 97 @Ignore 98 @FlakyTest 99 @Test onResume_fdnIsNotAvailable_shouldNotShowFdnMenu()100 public void onResume_fdnIsNotAvailable_shouldNotShowFdnMenu() throws NoSuchFieldException, 101 IllegalAccessException { 102 when(mMockPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM); 103 when(mMockPhone.getIccCard()).thenReturn(mMockIccCard); 104 when(mMockIccCard.getIccFdnAvailable()).thenReturn(false); 105 getField("mPhone").set(mActivity, mMockPhone); 106 107 mActivity.runOnUiThread(() -> mActivity.onResume()); 108 109 // Check the FDN menu is not displayed. 110 onView(withText(R.string.fdn)).check(doesNotExist()); 111 } 112 getField(String fieldName)113 private Field getField(String fieldName) throws NoSuchFieldException { 114 Field field = mActivity.getClass().getDeclaredField(fieldName); 115 field.setAccessible(true); 116 return field; 117 } 118 119 /** 120 * Automatically wake up device to perform tests. 121 */ keepScreenOn(ActivityTestRule activityTestRule, final CallFeaturesSetting activity)122 private static void keepScreenOn(ActivityTestRule activityTestRule, 123 final CallFeaturesSetting activity) throws Throwable { 124 activityTestRule.runOnUiThread(() -> { 125 activity.setTurnScreenOn(true); 126 activity.setShowWhenLocked(true); 127 KeyguardManager keyguardManager = 128 activity.getSystemService(KeyguardManager.class); 129 keyguardManager.requestDismissKeyguard(activity, null); 130 }); 131 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 132 } 133 } 134