OSDN Git Service

[Telephony Setting] Add supplementary conditions for CDMA display IMEI info when...
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / deviceinfo / imei / ImeiInfoDialogControllerTest.java
1 /*
2  * Copyright (C) 2017 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
17 package com.android.settings.deviceinfo.imei;
18
19 import static com.android.settings.deviceinfo.imei.ImeiInfoDialogController.ID_CDMA_SETTINGS;
20 import static com.android.settings.deviceinfo.imei.ImeiInfoDialogController.ID_GSM_SETTINGS;
21 import static com.android.settings.deviceinfo.imei.ImeiInfoDialogController.ID_IMEI_SV_VALUE;
22 import static com.android.settings.deviceinfo.imei.ImeiInfoDialogController.ID_IMEI_VALUE;
23 import static com.android.settings.deviceinfo.imei.ImeiInfoDialogController.ID_MEID_NUMBER_VALUE;
24 import static com.android.settings.deviceinfo.imei.ImeiInfoDialogController.ID_MIN_NUMBER_VALUE;
25 import static com.android.settings.deviceinfo.imei.ImeiInfoDialogController.ID_PRL_VERSION_VALUE;
26
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyInt;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.eq;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 import android.content.Context;
37 import android.telephony.SubscriptionInfo;
38 import android.telephony.SubscriptionManager;
39 import android.telephony.TelephonyManager;
40
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.shadow.api.Shadow;
49 import org.robolectric.shadows.ShadowSubscriptionManager;
50 import org.robolectric.shadows.ShadowTelephonyManager;
51 import org.robolectric.util.ReflectionHelpers;
52
53 @RunWith(RobolectricTestRunner.class)
54 public class ImeiInfoDialogControllerTest {
55
56     private static final String PRL_VERSION = "some_prl_version";
57     private static final String MEID_NUMBER = "12871234124";
58     private static final String IMEI_NUMBER = "2341982751254";
59     private static final String MIN_NUMBER = "123417851315";
60     private static final String IMEI_SV_NUMBER = "12";
61     private static final int SLOT_ID = 0;
62     private static final int SUB_ID = 0;
63
64     @Mock
65     private ImeiInfoDialogFragment mDialog;
66     @Mock
67     private TelephonyManager mTelephonyManager;
68     @Mock
69     private SubscriptionInfo mSubscriptionInfo;
70
71     private Context mContext;
72     private ImeiInfoDialogController mController;
73
74     @Before
75     public void setup() {
76         MockitoAnnotations.initMocks(this);
77         mContext = spy(RuntimeEnvironment.application);
78         final ShadowSubscriptionManager ssm = Shadow.extract(mContext.getSystemService(
79                 SubscriptionManager.class));
80         ssm.setActiveSubscriptionInfos(mSubscriptionInfo);
81         when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
82         final ShadowTelephonyManager stm = Shadow.extract(mContext.getSystemService(
83                 TelephonyManager.class));
84         stm.setTelephonyManagerForSubscriptionId(SUB_ID, mTelephonyManager);
85         when(mDialog.getContext()).thenReturn(mContext);
86
87         mController = spy(new ImeiInfoDialogController(mDialog, SLOT_ID));
88
89         when(mTelephonyManager.getCdmaPrlVersion()).thenReturn(PRL_VERSION);
90         when(mTelephonyManager.getMeid(anyInt())).thenReturn(MEID_NUMBER);
91         when(mTelephonyManager.getCdmaMin(anyInt())).thenReturn(MIN_NUMBER);
92         when(mTelephonyManager.getDeviceSoftwareVersion(anyInt())).thenReturn(IMEI_SV_NUMBER);
93         when(mTelephonyManager.getImei(anyInt())).thenReturn(IMEI_NUMBER);
94     }
95
96     @Test
97     public void populateImeiInfo_invalidSlot_shouldSetNothing() {
98         mController = spy(new ImeiInfoDialogController(mDialog, SLOT_ID + 1));
99
100         mController.populateImeiInfo();
101
102         verify(mDialog, never()).setText(anyInt(), any());
103     }
104
105     @Test
106     public void populateImeiInfo_cdmaLteEnabled_shouldSetMeidAndImeiAndMin() {
107         doReturn(true).when(mController).isCdmaLteEnabled();
108         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
109
110         mController.populateImeiInfo();
111
112         verify(mDialog).setText(ID_MEID_NUMBER_VALUE, MEID_NUMBER);
113         verify(mDialog).setText(ID_MIN_NUMBER_VALUE, MIN_NUMBER);
114         verify(mDialog).setText(ID_PRL_VERSION_VALUE, PRL_VERSION);
115         verify(mDialog).setText(eq(ID_IMEI_VALUE), any());
116         verify(mDialog).setText(eq(ID_IMEI_SV_VALUE), any());
117     }
118
119     @Test
120     public void populateImeiInfo_cdmaLteDisabled_shouldSetMeidAndMinAndRemoveGsmSettings() {
121         doReturn(false).when(mController).isCdmaLteEnabled();
122         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
123
124         mController.populateImeiInfo();
125
126         verify(mDialog).setText(ID_MEID_NUMBER_VALUE, MEID_NUMBER);
127         verify(mDialog).setText(ID_MIN_NUMBER_VALUE, MIN_NUMBER);
128         verify(mDialog).setText(ID_PRL_VERSION_VALUE, PRL_VERSION);
129         verify(mDialog).removeViewFromScreen(ID_GSM_SETTINGS);
130     }
131
132     @Test
133     public void populateImeiInfo_cdmaSimDisabled_shouldRemoveImeiInfoAndSetMinPrlToEmpty() {
134         ReflectionHelpers.setField(mController, "mSubscriptionInfo", null);
135         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
136
137         mController.populateImeiInfo();
138
139         verify(mDialog).setText(ID_MEID_NUMBER_VALUE, MEID_NUMBER);
140         verify(mDialog).setText(ID_MIN_NUMBER_VALUE, "");
141         verify(mDialog).setText(ID_PRL_VERSION_VALUE, "");
142         verify(mDialog).removeViewFromScreen(ID_GSM_SETTINGS);
143     }
144
145
146     @Test
147     public void populateImeiInfo_cdmaSimPresent_shouldSetImeiInfoAndSetAllCdmaSetting() {
148         ReflectionHelpers.setField(mController, "mSubscriptionInfo", null);
149         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
150         when(mTelephonyManager.getSimState(anyInt())).thenReturn(
151                 TelephonyManager.SIM_STATE_PRESENT);
152
153         mController.populateImeiInfo();
154
155         verify(mDialog).setText(ID_MEID_NUMBER_VALUE, MEID_NUMBER);
156         verify(mDialog).setText(ID_MIN_NUMBER_VALUE, "");
157         verify(mDialog).setText(ID_PRL_VERSION_VALUE, "");
158         verify(mDialog).setText(eq(ID_IMEI_VALUE), any());
159         verify(mDialog).setText(eq(ID_IMEI_SV_VALUE), any());
160     }
161
162     @Test
163     public void populateImeiInfo_cdmaSimABSENT_shouldSetImeiInfoAndSetAllCdmaSetting() {
164         ReflectionHelpers.setField(mController, "mSubscriptionInfo", null);
165         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
166         when(mTelephonyManager.getSimState(anyInt())).thenReturn(TelephonyManager.SIM_STATE_ABSENT);
167
168         mController.populateImeiInfo();
169
170         verify(mDialog).setText(ID_MEID_NUMBER_VALUE, MEID_NUMBER);
171         verify(mDialog).setText(ID_MIN_NUMBER_VALUE, "");
172         verify(mDialog).setText(ID_PRL_VERSION_VALUE, "");
173         verify(mDialog).removeViewFromScreen(ID_GSM_SETTINGS);
174     }
175
176     @Test
177     public void populateImeiInfo_gsmSimDisabled_shouldSetImeiAndRemoveCdmaSettings() {
178         ReflectionHelpers.setField(mController, "mSubscriptionInfo", null);
179         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);
180
181         mController.populateImeiInfo();
182
183         verify(mDialog).setText(eq(ID_IMEI_VALUE), any());
184         verify(mDialog).setText(eq(ID_IMEI_SV_VALUE), any());
185         verify(mDialog).removeViewFromScreen(ID_CDMA_SETTINGS);
186     }
187
188     @Test
189     public void populateImeinfo_gsm_shouldSetImeiAndRemoveCdmaSettings() {
190         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);
191
192         mController.populateImeiInfo();
193
194         verify(mDialog).setText(eq(ID_IMEI_VALUE), any());
195         verify(mDialog).setText(eq(ID_IMEI_SV_VALUE), any());
196         verify(mDialog).removeViewFromScreen(ID_CDMA_SETTINGS);
197     }
198
199     @Test
200     public void populateImeiInfo_emptyImei_shouldSetMeid_imeiSetToEmptyString() {
201         doReturn(true).when(mController).isCdmaLteEnabled();
202         when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
203         when(mTelephonyManager.getImei(anyInt())).thenReturn(null);
204
205         mController.populateImeiInfo();
206
207         verify(mDialog).setText(ID_MEID_NUMBER_VALUE, MEID_NUMBER);
208         verify(mDialog).setText(ID_MIN_NUMBER_VALUE, MIN_NUMBER);
209         verify(mDialog).setText(ID_PRL_VERSION_VALUE, PRL_VERSION);
210         verify(mDialog).setText(eq(ID_IMEI_VALUE), eq(""));
211         verify(mDialog).setText(eq(ID_IMEI_SV_VALUE), any());
212     }
213 }