1 /*
2  * Copyright (C) 2019 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.internal.telephony;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.doReturn;
23 
24 import android.content.Context;
25 import android.os.PersistableBundle;
26 import android.telephony.CarrierConfigManager;
27 import android.telephony.ServiceState;
28 
29 import com.android.internal.telephony.cdnr.CarrierDisplayNameData;
30 import com.android.internal.telephony.cdnr.CarrierDisplayNameResolver;
31 import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppState;
32 import com.android.internal.telephony.uicc.RuimRecords;
33 import com.android.internal.telephony.uicc.SIMRecords;
34 import com.android.internal.telephony.uicc.UiccCardApplication;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40 
41 public class CarrierDisplayNameResolverTest extends TelephonyTest {
42 
43     private static final String PLMN_1 = "310260";
44     private static final String PLMN_2 = "480123";
45     private static final String PLMN_3 = "586111";
46     private static final String HOME_PLMN_NUMERIC = PLMN_1;
47     private static final String NON_HOME_PLMN_NUMERIC = "123456";
48 
49     private static final String SPN_FROM_CC = "spn from carrier config";
50 
51     /** No PLMN in home, not SPN in roaming. */
52     private static final int SPN_DISPLAY_CONDITION_FROM_USIM = 0;
53     private static final String SPN_FROM_USIM = "spn from usim";
54     private static final String PNN_HOME_NAME_FROM_USIM = "pnnHomeName";
55     private static final String[] SPDI_FROM_USIM = new String[] { PLMN_1, PLMN_2 };
56     private static final String[] EHPLMNS_FROM_USIM = new String[] {
57             PLMN_1, PLMN_2, PLMN_3
58     };
59 
60     private static final boolean ROAMING = true;
61     private static final boolean NON_ROAMING = false;
62 
63     private PersistableBundle mConfig;
64 
65     private CarrierDisplayNameResolver mCdnr;
66 
67     private final ServiceState mSS = new ServiceState();
68 
69     @Before
setUp()70     public void setUp() throws Exception {
71         super.setUp("CDNRTest");
72 
73         mCdnr = new CarrierDisplayNameResolver(mPhone);
74 
75         UiccCardApplication uiccApp = Mockito.mock(UiccCardApplication.class);
76         doReturn(uiccApp).when(mPhone).getUiccCardApplication();
77         doReturn(AppState.APPSTATE_READY).when(uiccApp).getState();
78         doReturn(mSS).when(mSST).getServiceState();
79 
80         doReturn(false).when(mPhone).isWifiCallingEnabled();
81         doReturn(true).when(mPhone).isPhoneTypeGsm();
82 
83         mConfig = mContextFixture.getCarrierConfigBundle();
84         CarrierConfigManager mockConfigManager = Mockito.mock(CarrierConfigManager.class);
85         doReturn(mockConfigManager).when(mContext).getSystemService(Context.CARRIER_CONFIG_SERVICE);
86         doReturn(mConfig).when(mockConfigManager).getConfigForSubId(anyInt());
87 
88         mSS.setEmergencyOnly(false /* emergencyCallOnly" */);
89         mSS.setOperatorName("long name", "short name", HOME_PLMN_NUMERIC);
90         mSS.setVoiceRegState(ServiceState.STATE_IN_SERVICE);
91         mSS.setDataRegState(ServiceState.STATE_IN_SERVICE);
92 
93         SIMRecords usim = Mockito.mock(SIMRecords.class);
94         doReturn(SPN_FROM_USIM).when(usim).getServiceProviderName();
95         doReturn(PNN_HOME_NAME_FROM_USIM).when(usim).getPnnHomeName();
96         doReturn(EHPLMNS_FROM_USIM).when(usim).getEhplmns();
97         doReturn(SPDI_FROM_USIM).when(usim).getServiceProviderDisplayInformation();
98         doReturn(SPN_DISPLAY_CONDITION_FROM_USIM).when(usim).getCarrierNameDisplayCondition();
99 
100         mCdnr.updateEfFromUsim(usim);
101     }
102 
103     @After
tearDown()104     public void tearDown() throws Exception {
105         super.tearDown();
106     }
107 
108     @Test
testUpdateSPNFromHigherPrioritySource_shouldOverrideRecord()109     public void testUpdateSPNFromHigherPrioritySource_shouldOverrideRecord() {
110         // Carrier config source > sim record source
111         mConfig.putString(CarrierConfigManager.KEY_CARRIER_NAME_STRING, SPN_FROM_CC);
112 
113         // Update ef records from carrier config
114         mCdnr.updateEfFromCarrierConfig(mConfig);
115 
116         CarrierDisplayNameData data = mCdnr.getCarrierDisplayNameData();
117         assertThat(data.getSpn()).isEqualTo(SPN_FROM_CC);
118         assertThat(data.shouldShowSpn()).isTrue();
119     }
120 
121     @Test
testUpdateSPNFromLowerPrioritySource_shouldNotOverrideRecord()122     public void testUpdateSPNFromLowerPrioritySource_shouldNotOverrideRecord() {
123         // Ruim's priority < Usim's priority
124         RuimRecords ruim = Mockito.mock(RuimRecords.class);
125         doReturn("spn from ruim").when(ruim).getServiceProviderName();
126 
127         // Update ef records from Ruim
128         CarrierDisplayNameData data = mCdnr.getCarrierDisplayNameData();
129         assertThat(data.getSpn()).isEqualTo(SPN_FROM_USIM);
130     }
131 
132     @Test
testShouldShowSPN_nonRoaming_showSPN()133     public void testShouldShowSPN_nonRoaming_showSPN() {
134         mSS.setRoaming(NON_ROAMING);
135 
136         CarrierDisplayNameData data = mCdnr.getCarrierDisplayNameData();
137         assertThat(data.shouldShowSpn()).isTrue();
138     }
139 
140     @Test
testShouldShowSPN_plmnNotInProvidedList_notShowSPN()141     public void testShouldShowSPN_plmnNotInProvidedList_notShowSPN() {
142         mSS.setOperatorName("long", "short", NON_HOME_PLMN_NUMERIC);
143         mSS.setRoaming(ROAMING);
144 
145         CarrierDisplayNameData data = mCdnr.getCarrierDisplayNameData();
146         assertThat(data.shouldShowSpn()).isFalse();
147     }
148 
149     @Test
testShouldShowSPN_plmnInProvidedList_showSPN()150     public void testShouldShowSPN_plmnInProvidedList_showSPN() {
151         mSS.setOperatorName("long", "short", SPDI_FROM_USIM[0]);
152         mSS.setRoaming(ROAMING);
153 
154         CarrierDisplayNameData data = mCdnr.getCarrierDisplayNameData();
155         assertThat(data.shouldShowSpn()).isTrue();
156     }
157 
158     @Test
testShouldShowPLMNNetworkName_plmnNotInProvidedList_showPLMNNetworkName()159     public void testShouldShowPLMNNetworkName_plmnNotInProvidedList_showPLMNNetworkName() {
160         mSS.setOperatorName("long", "short", NON_HOME_PLMN_NUMERIC);
161         mSS.setRoaming(ROAMING);
162 
163         CarrierDisplayNameData data = mCdnr.getCarrierDisplayNameData();
164         assertThat(data.shouldShowPlmn()).isTrue();
165     }
166 
167     @Test
testGetPLMNNetworkName_oplNotPresent_returnTheFirstEntryOfPNNList()168     public void testGetPLMNNetworkName_oplNotPresent_returnTheFirstEntryOfPNNList() {
169         // Set the roaming state to on roaming, we should show the plmn network name based on the
170         // default settings.
171         mSS.setRoaming(ROAMING);
172 
173         CarrierDisplayNameData data = mCdnr.getCarrierDisplayNameData();
174         assertThat(data.getPlmn()).isEqualTo(PNN_HOME_NAME_FROM_USIM);
175     }
176 }
177