Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 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 | |
KOUSHIK PANUGANTI | 6dc0dd8 | 2019-03-14 00:20:51 -0700 | [diff] [blame] | 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; |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 23 | |
Torbjorn Eklund | c0d1318 | 2019-04-11 09:55:07 +0200 | [diff] [blame] | 24 | import static org.mockito.Mockito.doReturn; |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 25 | import static org.mockito.Mockito.when; |
| 26 | |
Brad Ebinger | 26fc1d5 | 2019-11-20 15:53:09 -0800 | [diff] [blame] | 27 | import android.app.KeyguardManager; |
Torbjorn Eklund | c0d1318 | 2019-04-11 09:55:07 +0200 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | |
| 30 | import androidx.test.InstrumentationRegistry; |
Brad Ebinger | 131a7b4 | 2019-12-02 13:03:46 -0800 | [diff] [blame] | 31 | import androidx.test.filters.FlakyTest; |
KOUSHIK PANUGANTI | 6dc0dd8 | 2019-03-14 00:20:51 -0700 | [diff] [blame] | 32 | import androidx.test.rule.ActivityTestRule; |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 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; |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 39 | import org.junit.Ignore; |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 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 = |
Brad Ebinger | 26fc1d5 | 2019-11-20 15:53:09 -0800 | [diff] [blame] | 54 | new ActivityTestRule<>(CallFeaturesSetting.class, false, true); |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 55 | private CallFeaturesSetting mActivity; |
| 56 | |
| 57 | @Before |
Brad Ebinger | 26fc1d5 | 2019-11-20 15:53:09 -0800 | [diff] [blame] | 58 | public void setUp() throws Throwable { |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 59 | MockitoAnnotations.initMocks(this); |
| 60 | mActivity = mRule.getActivity(); |
Torbjorn Eklund | c0d1318 | 2019-04-11 09:55:07 +0200 | [diff] [blame] | 61 | Context targetContext = InstrumentationRegistry.getTargetContext(); |
| 62 | doReturn(targetContext).when(mMockPhone).getContext(); |
Brad Ebinger | 26fc1d5 | 2019-11-20 15:53:09 -0800 | [diff] [blame] | 63 | keepScreenOn(mRule, mActivity); |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 66 | @Ignore |
Brad Ebinger | 131a7b4 | 2019-12-02 13:03:46 -0800 | [diff] [blame] | 67 | @FlakyTest |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 68 | @Test |
| 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 | |
Brad Ebinger | 26fc1d5 | 2019-11-20 15:53:09 -0800 | [diff] [blame] | 76 | mActivity.runOnUiThread(() -> mActivity.onResume()); |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 77 | |
| 78 | // Check the FDN menu is displayed. |
| 79 | onView(withText(R.string.fdn)).check(matches(isDisplayed())); |
| 80 | } |
| 81 | |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 82 | @Ignore |
Brad Ebinger | 131a7b4 | 2019-12-02 13:03:46 -0800 | [diff] [blame] | 83 | @FlakyTest |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 84 | @Test |
| 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 | |
Brad Ebinger | 26fc1d5 | 2019-11-20 15:53:09 -0800 | [diff] [blame] | 91 | mActivity.runOnUiThread(() -> mActivity.onResume()); |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 92 | |
| 93 | // Check the FDN menu is not displayed. |
| 94 | onView(withText(R.string.fdn)).check(doesNotExist()); |
| 95 | } |
| 96 | |
Brad Ebinger | dc555b4 | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 97 | @Ignore |
Brad Ebinger | 131a7b4 | 2019-12-02 13:03:46 -0800 | [diff] [blame] | 98 | @FlakyTest |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 99 | @Test |
| 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 | |
Brad Ebinger | 26fc1d5 | 2019-11-20 15:53:09 -0800 | [diff] [blame] | 107 | mActivity.runOnUiThread(() -> mActivity.onResume()); |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 108 | |
| 109 | // Check the FDN menu is not displayed. |
| 110 | onView(withText(R.string.fdn)).check(doesNotExist()); |
| 111 | } |
| 112 | |
| 113 | private Field getField(String fieldName) throws NoSuchFieldException { |
| 114 | Field field = mActivity.getClass().getDeclaredField(fieldName); |
| 115 | field.setAccessible(true); |
| 116 | return field; |
| 117 | } |
Brad Ebinger | 26fc1d5 | 2019-11-20 15:53:09 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Automatically wake up device to perform tests. |
| 121 | */ |
| 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 | } |
Xiangyu/Malcolm Chen | 2f810e8 | 2018-03-29 18:56:16 +0000 | [diff] [blame] | 133 | } |