blob: 7c7d6f8d84719b8f641f4752a05c48201aef6179 [file] [log] [blame]
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +00001/*
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 */
16package com.android.phone;
17
KOUSHIK PANUGANTI6dc0dd82019-03-14 00:20:51 -070018import static androidx.test.espresso.Espresso.onView;
19import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
20import static androidx.test.espresso.assertion.ViewAssertions.matches;
21import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
22import static androidx.test.espresso.matcher.ViewMatchers.withText;
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000023
Torbjorn Eklundc0d13182019-04-11 09:55:07 +020024import static org.mockito.Mockito.doReturn;
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000025import static org.mockito.Mockito.when;
26
Brad Ebinger26fc1d52019-11-20 15:53:09 -080027import android.app.KeyguardManager;
Torbjorn Eklundc0d13182019-04-11 09:55:07 +020028import android.content.Context;
29
30import androidx.test.InstrumentationRegistry;
Brad Ebinger131a7b42019-12-02 13:03:46 -080031import androidx.test.filters.FlakyTest;
KOUSHIK PANUGANTI6dc0dd82019-03-14 00:20:51 -070032import androidx.test.rule.ActivityTestRule;
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000033
34import com.android.internal.telephony.IccCard;
35import com.android.internal.telephony.Phone;
36import com.android.internal.telephony.PhoneConstants;
37
38import org.junit.Before;
Brad Ebingerdc555b42019-12-09 16:12:57 -080039import org.junit.Ignore;
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000040import org.junit.Rule;
41import org.junit.Test;
42import org.mockito.Mock;
43import org.mockito.MockitoAnnotations;
44
45import java.lang.reflect.Field;
46
47public class CallFeaturesSettingTest {
48 @Mock
49 Phone mMockPhone;
50 @Mock
51 IccCard mMockIccCard;
52 @Rule
53 public ActivityTestRule<CallFeaturesSetting> mRule =
Brad Ebinger26fc1d52019-11-20 15:53:09 -080054 new ActivityTestRule<>(CallFeaturesSetting.class, false, true);
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000055 private CallFeaturesSetting mActivity;
56
57 @Before
Brad Ebinger26fc1d52019-11-20 15:53:09 -080058 public void setUp() throws Throwable {
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000059 MockitoAnnotations.initMocks(this);
60 mActivity = mRule.getActivity();
Torbjorn Eklundc0d13182019-04-11 09:55:07 +020061 Context targetContext = InstrumentationRegistry.getTargetContext();
62 doReturn(targetContext).when(mMockPhone).getContext();
Brad Ebinger26fc1d52019-11-20 15:53:09 -080063 keepScreenOn(mRule, mActivity);
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000064 }
65
Brad Ebingerdc555b42019-12-09 16:12:57 -080066 @Ignore
Brad Ebinger131a7b42019-12-02 13:03:46 -080067 @FlakyTest
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000068 @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 Ebinger26fc1d52019-11-20 15:53:09 -080076 mActivity.runOnUiThread(() -> mActivity.onResume());
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000077
78 // Check the FDN menu is displayed.
79 onView(withText(R.string.fdn)).check(matches(isDisplayed()));
80 }
81
Brad Ebingerdc555b42019-12-09 16:12:57 -080082 @Ignore
Brad Ebinger131a7b42019-12-02 13:03:46 -080083 @FlakyTest
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000084 @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 Ebinger26fc1d52019-11-20 15:53:09 -080091 mActivity.runOnUiThread(() -> mActivity.onResume());
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000092
93 // Check the FDN menu is not displayed.
94 onView(withText(R.string.fdn)).check(doesNotExist());
95 }
96
Brad Ebingerdc555b42019-12-09 16:12:57 -080097 @Ignore
Brad Ebinger131a7b42019-12-02 13:03:46 -080098 @FlakyTest
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +000099 @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 Ebinger26fc1d52019-11-20 15:53:09 -0800107 mActivity.runOnUiThread(() -> mActivity.onResume());
Xiangyu/Malcolm Chen2f810e82018-03-29 18:56:16 +0000108
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 Ebinger26fc1d52019-11-20 15:53:09 -0800118
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 Chen2f810e82018-03-29 18:56:16 +0000133}