• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.any;
24 import static org.mockito.Mockito.doAnswer;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 
29 import android.content.Context;
30 import android.os.AsyncResult;
31 import android.os.Handler;
32 import android.os.IPowerManager;
33 import android.os.IThermalService;
34 import android.os.Looper;
35 import android.os.PersistableBundle;
36 import android.os.PowerManager;
37 import android.telephony.CarrierConfigManager;
38 import android.telephony.CellInfo;
39 import android.telephony.NetworkRegistrationInfo;
40 import android.telephony.PhysicalChannelConfig;
41 import android.telephony.RadioAccessFamily;
42 import android.telephony.ServiceState;
43 import android.telephony.TelephonyDisplayInfo;
44 import android.telephony.TelephonyManager;
45 import android.telephony.data.DataCallResponse;
46 import android.telephony.data.EpsQos;
47 import android.telephony.data.Qos;
48 import android.telephony.data.QosBearerSession;
49 import android.testing.AndroidTestingRunner;
50 import android.testing.TestableLooper;
51 
52 import com.android.internal.telephony.data.DataNetworkController.DataNetworkControllerCallback;
53 import com.android.internal.util.IState;
54 import com.android.internal.util.StateMachine;
55 
56 import org.junit.After;
57 import org.junit.Before;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.ArgumentCaptor;
61 
62 import java.lang.reflect.Method;
63 import java.util.ArrayList;
64 import java.util.Collections;
65 import java.util.List;
66 
67 @RunWith(AndroidTestingRunner.class)
68 @TestableLooper.RunWithLooper
69 public class NetworkTypeControllerTest extends TelephonyTest {
70     private NetworkTypeController mNetworkTypeController;
71     private PersistableBundle mBundle;
72     private CarrierConfigManager.CarrierConfigChangeListener mCarrierConfigChangeListener;
73 
getCurrentState()74     private IState getCurrentState() throws Exception {
75         Method method = StateMachine.class.getDeclaredMethod("getCurrentState");
76         method.setAccessible(true);
77         return (IState) method.invoke(mNetworkTypeController);
78     }
79 
updateOverrideNetworkType()80     private void updateOverrideNetworkType() throws Exception {
81         Method method = NetworkTypeController.class.getDeclaredMethod("updateOverrideNetworkType");
82         method.setAccessible(true);
83         method.invoke(mNetworkTypeController);
84     }
85 
sendCarrierConfigChanged()86     private void sendCarrierConfigChanged() {
87         if (mCarrierConfigChangeListener != null) {
88             mCarrierConfigChangeListener.onCarrierConfigChanged(mPhone.getPhoneId(),
89                     mPhone.getSubId(), TelephonyManager.UNKNOWN_CARRIER_ID,
90                     TelephonyManager.UNKNOWN_CARRIER_ID);
91         }
92         processAllMessages();
93     }
94 
95     @Before
setUp()96     public void setUp() throws Exception {
97         super.setUp(getClass().getSimpleName());
98         mBundle = mContextFixture.getCarrierConfigBundle();
99         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING,
100                 "connected_mmwave:5G_Plus,connected:5G,not_restricted_rrc_idle:5G,"
101                         + "not_restricted_rrc_con:5G");
102         mBundle.putInt(CarrierConfigManager.KEY_LTE_PLUS_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
103         sendCarrierConfigChanged();
104 
105         replaceInstance(Handler.class, "mLooper", mDisplayInfoController, Looper.myLooper());
106         doReturn(RadioAccessFamily.getRafFromNetworkType(
107                 TelephonyManager.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA)).when(
108                 mPhone).getCachedAllowedNetworkTypesBitmask();
109         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
110                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
111         doReturn(new int[] {0}).when(mServiceState).getCellBandwidths();
112         // Capture listener to emulate the carrier config change notification used later
113         ArgumentCaptor<CarrierConfigManager.CarrierConfigChangeListener> listenerArgumentCaptor =
114                 ArgumentCaptor.forClass(CarrierConfigManager.CarrierConfigChangeListener.class);
115         setPhysicalLinkStatus(true);
116         mNetworkTypeController =
117                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
118         processAllMessages();
119         verify(mCarrierConfigManager).registerCarrierConfigChangeListener(any(),
120                 listenerArgumentCaptor.capture());
121         mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);
122     }
123 
124     @After
tearDown()125     public void tearDown() throws Exception {
126         mNetworkTypeController.getHandler().removeCallbacksAndMessages(null);
127         mNetworkTypeController = null;
128         mBundle = null;
129         super.tearDown();
130     }
131 
132     @Test
testUpdateOverrideNetworkTypeNrNsa()133     public void testUpdateOverrideNetworkTypeNrNsa() throws Exception {
134         // not NR
135         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
136         updateOverrideNetworkType();
137         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
138                 mNetworkTypeController.getOverrideNetworkType());
139 
140         // NR NSA, restricted
141         doReturn(NetworkRegistrationInfo.NR_STATE_RESTRICTED).when(mServiceState).getNrState();
142         updateOverrideNetworkType();
143         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
144                 mNetworkTypeController.getOverrideNetworkType());
145 
146         // NR NSA, not restricted
147         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
148         updateOverrideNetworkType();
149         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
150                 mNetworkTypeController.getOverrideNetworkType());
151 
152         // NR NSA, sub 6 frequency
153         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
154         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
155         updateOverrideNetworkType();
156         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
157                 mNetworkTypeController.getOverrideNetworkType());
158 
159         // NR NSA, millimeter wave frequency
160         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
161         updateOverrideNetworkType();
162         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
163                 mNetworkTypeController.getOverrideNetworkType());
164     }
165 
166     @Test
testUpdateOverrideNetworkTypeNrSa()167     public void testUpdateOverrideNetworkTypeNrSa() throws Exception {
168         // not NR
169         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
170         updateOverrideNetworkType();
171         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
172                 mNetworkTypeController.getOverrideNetworkType());
173 
174         // NR SA connected
175         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
176                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
177                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
178                 .build();
179         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
180                 anyInt(), anyInt());
181 
182         updateOverrideNetworkType();
183         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
184                 mNetworkTypeController.getOverrideNetworkType());
185     }
186 
187     @Test
testUpdateOverrideNetworkTypeNrSaMmwave()188     public void testUpdateOverrideNetworkTypeNrSaMmwave() throws Exception {
189         // not NR
190         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
191         updateOverrideNetworkType();
192         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
193                 mNetworkTypeController.getOverrideNetworkType());
194 
195         // NR SA connected and millimeter wave frequency
196         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
197                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
198                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
199                 .build();
200         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
201                 anyInt(), anyInt());
202         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
203 
204         updateOverrideNetworkType();
205         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
206                 mNetworkTypeController.getOverrideNetworkType());
207     }
208 
209     @Test
testUpdateOverrideNetworkTypeLte()210     public void testUpdateOverrideNetworkTypeLte() throws Exception {
211         // normal LTE
212         updateOverrideNetworkType();
213         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
214                 mNetworkTypeController.getOverrideNetworkType());
215 
216         // LTE CA at bandwidth threshold
217         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
218         doReturn(new int[] {20000}).when(mServiceState).getCellBandwidths();
219         updateOverrideNetworkType();
220         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
221                 mNetworkTypeController.getOverrideNetworkType());
222 
223         // LTE CA above bandwidth threshold
224         doReturn(new int[] {20000, 1400}).when(mServiceState).getCellBandwidths();
225         updateOverrideNetworkType();
226         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
227                 mNetworkTypeController.getOverrideNetworkType());
228 
229         // LTE ADVANCED PRO
230         doReturn("test_patternShowAdvanced").when(mServiceState).getOperatorAlphaLongRaw();
231         mBundle.putString(CarrierConfigManager.KEY_SHOW_CARRIER_DATA_ICON_PATTERN_STRING,
232                 ".*_patternShowAdvanced");
233         sendCarrierConfigChanged();
234         updateOverrideNetworkType();
235         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO,
236                 mNetworkTypeController.getOverrideNetworkType());
237     }
238 
239     @Test
testUpdateOverrideNetworkType()240     public void testUpdateOverrideNetworkType() throws Exception {
241         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
242         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
243         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
244                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_HSPAP)
245                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
246                 .build();
247         doReturn(nri).when(mServiceState).getNetworkRegistrationInfo(anyInt(), anyInt());
248         updateOverrideNetworkType();
249 
250         // override shouldn't be NR if not on LTE despite NR_STATE_CONNECTED
251         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
252                 mNetworkTypeController.getOverrideNetworkType());
253     }
254 
255     @Test
testTransitionToCurrentStateLegacy()256     public void testTransitionToCurrentStateLegacy() throws Exception {
257         assertEquals("DefaultState", getCurrentState().getName());
258         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
259         processAllMessages();
260         assertEquals("legacy", getCurrentState().getName());
261     }
262 
263     @Test
testTransitionToCurrentStateRestricted()264     public void testTransitionToCurrentStateRestricted() throws Exception {
265         assertEquals("DefaultState", getCurrentState().getName());
266         doReturn(NetworkRegistrationInfo.NR_STATE_RESTRICTED).when(mServiceState).getNrState();
267         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
268         processAllMessages();
269         assertEquals("restricted", getCurrentState().getName());
270     }
271 
272     @Test
testTransitionToCurrentStateIdle()273     public void testTransitionToCurrentStateIdle() throws Exception {
274         assertEquals("DefaultState", getCurrentState().getName());
275         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
276         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
277                 DataCallResponse.LINK_STATUS_DORMANT);
278         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
279         processAllMessages();
280         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
281     }
282 
283     @Test
testTransitionToCurrentStateIdleSupportPhysicalChannelConfig1_6()284     public void testTransitionToCurrentStateIdleSupportPhysicalChannelConfig1_6() throws Exception {
285         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
286                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
287         mNetworkTypeController =
288                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
289         processAllMessages();
290         assertEquals("DefaultState", getCurrentState().getName());
291 
292         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
293         setPhysicalLinkStatus(false);
294         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
295                 new AsyncResult(null,
296                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
297         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
298         processAllMessages();
299         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
300     }
301 
302     @Test
testTransitionToCurrentStateIdle_usingUserDataForRrcDetection()303     public void testTransitionToCurrentStateIdle_usingUserDataForRrcDetection() throws Exception {
304         mBundle.putBoolean(
305                 CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL, true);
306         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
307                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
308         mNetworkTypeController =
309                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
310         sendCarrierConfigChanged();
311         processAllMessages();
312         assertEquals("DefaultState", getCurrentState().getName());
313 
314         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
315         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
316                 DataCallResponse.LINK_STATUS_DORMANT);
317         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
318         processAllMessages();
319         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
320     }
321 
322     @Test
testTransitionToCurrentStateLteConnected()323     public void testTransitionToCurrentStateLteConnected() throws Exception {
324         assertEquals("DefaultState", getCurrentState().getName());
325         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
326         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
327                 DataCallResponse.LINK_STATUS_ACTIVE);
328         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
329         processAllMessages();
330         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
331     }
332 
333     @Test
testTransitionToCurrentStateLteConnectedSupportPhysicalChannelConfig1_6()334     public void testTransitionToCurrentStateLteConnectedSupportPhysicalChannelConfig1_6()
335             throws Exception {
336         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
337                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
338         mNetworkTypeController =
339                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
340         sendCarrierConfigChanged();
341         processAllMessages();
342         assertEquals("DefaultState", getCurrentState().getName());
343 
344         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
345         setPhysicalLinkStatus(true);
346         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
347                 new AsyncResult(null,
348                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
349         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
350         processAllMessages();
351         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
352     }
353 
354     @Test
testTransitionToCurrentStateLteConnected_usingUserDataForRrcDetection()355     public void testTransitionToCurrentStateLteConnected_usingUserDataForRrcDetection()
356             throws Exception {
357         mBundle.putBoolean(
358                 CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL, true);
359         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
360                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
361         mNetworkTypeController =
362                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
363         sendCarrierConfigChanged();
364         processAllMessages();
365         assertEquals("DefaultState", getCurrentState().getName());
366 
367         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
368         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
369                 DataCallResponse.LINK_STATUS_ACTIVE);
370         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
371         processAllMessages();
372         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
373     }
374 
375     @Test
testTransitionToCurrentStateNrConnectedIdle()376     public void testTransitionToCurrentStateNrConnectedIdle() throws Exception {
377         assertEquals("DefaultState", getCurrentState().getName());
378         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
379         doReturn(new ArrayList<>()).when(mSST).getPhysicalChannelConfigList();
380         sendCarrierConfigChanged();
381 
382         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
383         processAllMessages();
384         assertEquals("connected_rrc_idle", getCurrentState().getName());
385     }
386 
387     @Test
testTransitionToCurrentStateNrConnected()388     public void testTransitionToCurrentStateNrConnected() throws Exception {
389         assertEquals("DefaultState", getCurrentState().getName());
390         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
391 
392         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
393         processAllMessages();
394         assertEquals("connected", getCurrentState().getName());
395     }
396 
397     @Test
testTransitionToCurrentStateNrConnectedMmwave()398     public void testTransitionToCurrentStateNrConnectedMmwave() throws Exception {
399         assertEquals("DefaultState", getCurrentState().getName());
400         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
401         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
402 
403         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
404         processAllMessages();
405         assertEquals("connected_mmwave", getCurrentState().getName());
406     }
407 
408     @Test
testTransitionToCurrentStateNrConnectedMmwaveWithAdditionalBandAndNoMmwave()409     public void testTransitionToCurrentStateNrConnectedMmwaveWithAdditionalBandAndNoMmwave()
410             throws Exception {
411         assertEquals("DefaultState", getCurrentState().getName());
412         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
413         doReturn(ServiceState.FREQUENCY_RANGE_HIGH).when(mServiceState).getNrFrequencyRange();
414         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
415                 new int[]{41});
416         PhysicalChannelConfig physicalChannelConfig = new PhysicalChannelConfig.Builder()
417                 .setPhysicalCellId(1)
418                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
419                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
420                 .setBand(41)
421                 .build();
422         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
423         lastPhysicalChannelConfigList.add(physicalChannelConfig);
424         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
425         sendCarrierConfigChanged();
426 
427         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
428         processAllMessages();
429         assertEquals("connected_mmwave", getCurrentState().getName());
430     }
431 
432     @Test
testTransitionToCurrentStateNrConnectedMmwaveWithAdditionalBandAndNoMmwaveNrNsa()433     public void testTransitionToCurrentStateNrConnectedMmwaveWithAdditionalBandAndNoMmwaveNrNsa()
434             throws Exception {
435         assertEquals("DefaultState", getCurrentState().getName());
436         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
437         doReturn(ServiceState.FREQUENCY_RANGE_HIGH).when(mServiceState).getNrFrequencyRange();
438         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
439                 new int[]{41});
440         PhysicalChannelConfig ltePhysicalChannelConfig = new PhysicalChannelConfig.Builder()
441                 .setPhysicalCellId(1)
442                 .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
443                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
444                 .build();
445         PhysicalChannelConfig nrPhysicalChannelConfig = new PhysicalChannelConfig.Builder()
446                 .setPhysicalCellId(2)
447                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
448                 .setCellConnectionStatus(CellInfo.CONNECTION_SECONDARY_SERVING)
449                 .setBand(41)
450                 .build();
451         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
452         lastPhysicalChannelConfigList.add(ltePhysicalChannelConfig);
453         lastPhysicalChannelConfigList.add(nrPhysicalChannelConfig);
454         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
455         sendCarrierConfigChanged();
456 
457         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
458         processAllMessages();
459         assertEquals("connected_mmwave", getCurrentState().getName());
460     }
461 
462     @Test
testTransitionToCurrentStateNrConnectedWithNoAdditionalBandAndNoMmwave()463     public void testTransitionToCurrentStateNrConnectedWithNoAdditionalBandAndNoMmwave()
464             throws Exception {
465         assertEquals("DefaultState", getCurrentState().getName());
466         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
467         doReturn(ServiceState.FREQUENCY_RANGE_HIGH).when(mServiceState).getNrFrequencyRange();
468         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
469                 new int[]{41});
470         PhysicalChannelConfig physicalChannelConfig = new PhysicalChannelConfig.Builder()
471                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
472                 .setBand(2)
473                 .build();
474         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
475         lastPhysicalChannelConfigList.add(physicalChannelConfig);
476         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
477         sendCarrierConfigChanged();
478 
479         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
480         processAllMessages();
481         assertEquals("connected", getCurrentState().getName());
482     }
483 
484     @Test
testTransitionToCurrentStateNrConnectedWithNrAdvancedCapable()485     public void testTransitionToCurrentStateNrConnectedWithNrAdvancedCapable() throws Exception {
486         assertEquals("DefaultState", getCurrentState().getName());
487         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
488         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
489         sendCarrierConfigChanged();
490 
491         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
492         processAllMessages();
493         assertEquals("connected_mmwave", getCurrentState().getName());
494     }
495 
496     @Test
testTransitionToCurrentStateNrConnectedWithPcoAndNoNrAdvancedCapable()497     public void testTransitionToCurrentStateNrConnectedWithPcoAndNoNrAdvancedCapable()
498             throws Exception {
499         assertEquals("DefaultState", getCurrentState().getName());
500         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
501         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
502         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT, 0xFF03);
503         sendCarrierConfigChanged();
504 
505         ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
506                 ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
507         verify(mDataNetworkController).registerDataNetworkControllerCallback(
508                 dataNetworkControllerCallbackCaptor.capture());
509         DataNetworkControllerCallback callback = dataNetworkControllerCallbackCaptor.getValue();
510         callback.onNrAdvancedCapableByPcoChanged(false);
511         processAllMessages();
512         assertEquals("connected", getCurrentState().getName());
513     }
514 
515     @Test
testTransitionToCurrentStateNrConnectedWithWrongPcoAndNoNrAdvancedCapable()516     public void testTransitionToCurrentStateNrConnectedWithWrongPcoAndNoNrAdvancedCapable()
517             throws Exception {
518         assertEquals("DefaultState", getCurrentState().getName());
519         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
520         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
521         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT, 0xFF00);
522         sendCarrierConfigChanged();
523 
524         ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
525                 ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
526         verify(mDataNetworkController).registerDataNetworkControllerCallback(
527                 dataNetworkControllerCallbackCaptor.capture());
528         DataNetworkControllerCallback callback = dataNetworkControllerCallbackCaptor.getValue();
529         callback.onNrAdvancedCapableByPcoChanged(false);
530         processAllMessages();
531         assertEquals("connected", getCurrentState().getName());
532     }
533 
534     @Test
testTransitionToCurrentStateNrConnectedWithNrAdvancedCapableAndPco()535     public void testTransitionToCurrentStateNrConnectedWithNrAdvancedCapableAndPco()
536             throws Exception {
537         assertEquals("DefaultState", getCurrentState().getName());
538         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
539         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
540         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_CAPABLE_PCO_ID_INT, 0xFF03);
541         sendCarrierConfigChanged();
542 
543         ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
544                 ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
545         verify(mDataNetworkController).registerDataNetworkControllerCallback(
546                 dataNetworkControllerCallbackCaptor.capture());
547         DataNetworkControllerCallback callback = dataNetworkControllerCallbackCaptor.getValue();
548         callback.onNrAdvancedCapableByPcoChanged(true);
549         processAllMessages();
550         assertEquals("connected_mmwave", getCurrentState().getName());
551     }
552 
553     @Test
testEventDataRatChanged()554     public void testEventDataRatChanged() throws Exception {
555         testTransitionToCurrentStateLegacy();
556         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
557 
558         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
559         processAllMessages();
560         assertEquals("connected", getCurrentState().getName());
561     }
562 
563     @Test
testEventNrStateChanged()564     public void testEventNrStateChanged() throws Exception {
565         testTransitionToCurrentStateNrConnected();
566         doReturn(NetworkRegistrationInfo.NR_STATE_RESTRICTED).when(mServiceState).getNrState();
567 
568         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
569         processAllMessages();
570         assertEquals("restricted", getCurrentState().getName());
571     }
572 
573     @Test
testEventNrFrequencyRangeChangedFromNrConnectedMmwaveToNrConnected()574     public void testEventNrFrequencyRangeChangedFromNrConnectedMmwaveToNrConnected()
575             throws Exception {
576         testTransitionToCurrentStateNrConnectedMmwave();
577         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
578 
579         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
580         processAllMessages();
581         assertEquals("connected", getCurrentState().getName());
582     }
583 
584     @Test
testEventNrFrequencyRangeChangedFromNrConnectedToNrConnectedMmwave()585     public void testEventNrFrequencyRangeChangedFromNrConnectedToNrConnectedMmwave()
586             throws Exception {
587         testTransitionToCurrentStateNrConnected();
588         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
589 
590         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
591         processAllMessages();
592         assertEquals("connected_mmwave", getCurrentState().getName());
593     }
594 
595     @Test
testEventPhysicalChannelConfigChangedWithRatcheting()596     public void testEventPhysicalChannelConfigChangedWithRatcheting() throws Exception {
597         testTransitionToCurrentStateNrConnected();
598         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
599                 new int[]{41, 77});
600         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
601         mBundle.putBoolean(CarrierConfigManager.KEY_RATCHET_NR_ADVANCED_BANDWIDTH_IF_RRC_IDLE_BOOL,
602                 true);
603         sendCarrierConfigChanged();
604 
605         // Primary serving NR PCC with cell ID = 1, band = none, bandwidth = 200000
606         PhysicalChannelConfig pcc1 = new PhysicalChannelConfig.Builder()
607                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
608                 .setPhysicalCellId(1)
609                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
610                 .setCellBandwidthDownlinkKhz(19999)
611                 .build();
612         // Secondary serving NR PCC with cell ID = 2, band = 41, bandwidth = 10000
613         PhysicalChannelConfig pcc2 = new PhysicalChannelConfig.Builder()
614                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
615                 .setPhysicalCellId(2)
616                 .setCellConnectionStatus(CellInfo.CONNECTION_SECONDARY_SERVING)
617                 .setCellBandwidthDownlinkKhz(10000)
618                 .setBand(41)
619                 .build();
620         // Primary serving NR PCC with cell ID = 3, band = 77, bandwidth = 0
621         PhysicalChannelConfig pcc3 = new PhysicalChannelConfig.Builder()
622                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
623                 .setPhysicalCellId(3)
624                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
625                 .setBand(77)
626                 .build();
627 
628         List<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
629         physicalChannelConfigs.add(pcc1);
630         physicalChannelConfigs.add(pcc2);
631         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
632 
633         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
634                 new AsyncResult(null, physicalChannelConfigs, null));
635         processAllMessages();
636         assertEquals("connected_mmwave", getCurrentState().getName());
637 
638         // bands and bandwidths should stay ratcheted even if an empty PCC list is sent
639         doReturn(new ArrayList<>()).when(mSST).getPhysicalChannelConfigList();
640         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
641                 new AsyncResult(null, new ArrayList<>(), null));
642         processAllMessages();
643         assertEquals("connected_rrc_idle", getCurrentState().getName());
644 
645         // bands and bandwidths should stay ratcheted as long as anchor NR cell is the same
646         physicalChannelConfigs.remove(pcc2);
647         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
648         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
649                 new AsyncResult(null, physicalChannelConfigs, null));
650         processAllMessages();
651         assertEquals("connected_mmwave", getCurrentState().getName());
652 
653         // bands and bandwidths should no longer be ratcheted if anchor NR cell changes
654         // add pcc3 to front of list to ensure anchor NR cell changes from 1 -> 3
655         physicalChannelConfigs.add(0, pcc3);
656         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
657                 new AsyncResult(null, physicalChannelConfigs, null));
658         processAllMessages();
659         assertEquals("connected", getCurrentState().getName());
660 
661         physicalChannelConfigs.add(pcc2);
662         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
663                 new AsyncResult(null, physicalChannelConfigs, null));
664         processAllMessages();
665         assertEquals("connected_mmwave", getCurrentState().getName());
666     }
667 
668     @Test
testEventPhysicalChannelConfigChangedWithoutRatcheting()669     public void testEventPhysicalChannelConfigChangedWithoutRatcheting() throws Exception {
670         testTransitionToCurrentStateNrConnected();
671         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
672                 new int[]{41, 77});
673         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
674         sendCarrierConfigChanged();
675 
676         // Primary serving NR PCC with cell ID = 1, band = none, bandwidth = 200000
677         PhysicalChannelConfig pcc1 = new PhysicalChannelConfig.Builder()
678                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
679                 .setPhysicalCellId(1)
680                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
681                 .setCellBandwidthDownlinkKhz(19999)
682                 .build();
683         // Secondary serving NR PCC with cell ID = 2, band = 41, bandwidth = 10000
684         PhysicalChannelConfig pcc2 = new PhysicalChannelConfig.Builder()
685                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
686                 .setPhysicalCellId(2)
687                 .setCellConnectionStatus(CellInfo.CONNECTION_SECONDARY_SERVING)
688                 .setCellBandwidthDownlinkKhz(10000)
689                 .setBand(41)
690                 .build();
691 
692         List<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
693         physicalChannelConfigs.add(pcc1);
694         physicalChannelConfigs.add(pcc2);
695         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
696 
697         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
698                 new AsyncResult(null, physicalChannelConfigs, null));
699         processAllMessages();
700         assertEquals("connected_mmwave", getCurrentState().getName());
701 
702         // bands and bandwidths should stay ratcheted even if an empty PCC list is sent
703         doReturn(new ArrayList<>()).when(mSST).getPhysicalChannelConfigList();
704         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
705                 new AsyncResult(null, new ArrayList<>(), null));
706         processAllMessages();
707         assertEquals("connected_rrc_idle", getCurrentState().getName());
708 
709         // bands and bandwidths should change if PCC list changes
710         physicalChannelConfigs.remove(pcc2);
711         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
712         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
713                 new AsyncResult(null, physicalChannelConfigs, null));
714         processAllMessages();
715         assertEquals("connected", getCurrentState().getName());
716     }
717 
718     @Test
testEventPhysicalChannelConfigChangedUsingUserDataForRrc()719     public void testEventPhysicalChannelConfigChangedUsingUserDataForRrc() throws Exception {
720         testTransitionToCurrentStateNrConnected();
721         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
722                 new int[]{41, 77});
723         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
724         mBundle.putBoolean(CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL,
725                 true);
726         sendCarrierConfigChanged();
727 
728         // Primary serving NR PCC with cell ID = 1, band = none, bandwidth = 200000
729         PhysicalChannelConfig pcc1 = new PhysicalChannelConfig.Builder()
730                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
731                 .setPhysicalCellId(1)
732                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
733                 .setCellBandwidthDownlinkKhz(19999)
734                 .build();
735         // Secondary serving NR PCC with cell ID = 2, band = 41, bandwidth = 10000
736         PhysicalChannelConfig pcc2 = new PhysicalChannelConfig.Builder()
737                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
738                 .setPhysicalCellId(2)
739                 .setCellConnectionStatus(CellInfo.CONNECTION_SECONDARY_SERVING)
740                 .setCellBandwidthDownlinkKhz(10000)
741                 .setBand(41)
742                 .build();
743 
744         List<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
745         physicalChannelConfigs.add(pcc1);
746         physicalChannelConfigs.add(pcc2);
747         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
748 
749         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
750                 new AsyncResult(null, physicalChannelConfigs, null));
751         processAllMessages();
752         assertEquals("connected_mmwave", getCurrentState().getName());
753 
754         // bands and bandwidths should not stay the same even if an empty PCC list is sent
755         doReturn(new ArrayList<>()).when(mSST).getPhysicalChannelConfigList();
756         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
757                 new AsyncResult(null, new ArrayList<>(), null));
758         processAllMessages();
759         assertEquals("connected", getCurrentState().getName());
760 
761         // bands and bandwidths should change if PCC list changes
762         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
763         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
764                 new AsyncResult(null, physicalChannelConfigs, null));
765         processAllMessages();
766         assertEquals("connected_mmwave", getCurrentState().getName());
767     }
768 
769     @Test
testNrPhysicalChannelChangeFromNrConnectedMmwaveToLteConnected()770     public void testNrPhysicalChannelChangeFromNrConnectedMmwaveToLteConnected() throws Exception {
771         testTransitionToCurrentStateNrConnectedMmwave();
772         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
773 
774         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
775                 DataCallResponse.LINK_STATUS_ACTIVE);
776         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
777         processAllMessages();
778         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
779     }
780 
781     @Test
testNrPhysicalChannelChange1_6FromNrConnectedMmwaveToLteConnected()782     public void testNrPhysicalChannelChange1_6FromNrConnectedMmwaveToLteConnected()
783             throws Exception {
784         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
785                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
786         mNetworkTypeController =
787                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
788         processAllMessages();
789         testTransitionToCurrentStateNrConnectedMmwave();
790         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
791         setPhysicalLinkStatus(true);
792         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
793                 new AsyncResult(null,
794                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
795         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
796 
797         processAllMessages();
798         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
799     }
800 
801     @Test
testUsingUserDataForRrcDetection_FromNrConnectedMmwaveToLteConnected()802     public void testUsingUserDataForRrcDetection_FromNrConnectedMmwaveToLteConnected()
803             throws Exception {
804         mBundle.putBoolean(
805                 CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL, true);
806         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
807                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
808         mNetworkTypeController =
809                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
810         sendCarrierConfigChanged();
811         processAllMessages();
812 
813         // service state of NrConnectedMmwave shouldn't affect the result
814         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
815         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
816 
817         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
818         processAllMessages();
819 
820         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
821         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
822                 DataCallResponse.LINK_STATUS_ACTIVE);
823         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
824 
825         processAllMessages();
826         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
827     }
828 
829     @Test
testEventPhysicalChannelChangeFromLteToLteCaInLegacyState()830     public void testEventPhysicalChannelChangeFromLteToLteCaInLegacyState() throws Exception {
831         testTransitionToCurrentStateLegacy();
832         updateOverrideNetworkType();
833         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
834                 mNetworkTypeController.getOverrideNetworkType());
835 
836         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
837         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
838         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
839                 new AsyncResult(null,
840                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
841         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
842         processAllMessages();
843         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
844                 mNetworkTypeController.getOverrideNetworkType());
845     }
846 
847     @Test
testEventPhysicalChannelChangeFromLteToLteCaInLteConnectedState()848     public void testEventPhysicalChannelChangeFromLteToLteCaInLteConnectedState() throws Exception {
849         // Remove RRC idle/RRC connected from 5G override
850         mBundle = mContextFixture.getCarrierConfigBundle();
851         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING,
852                 "connected_mmwave:5G_Plus,connected:5G");
853         sendCarrierConfigChanged();
854 
855         // Transition to LTE connected state
856         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
857         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
858                 DataCallResponse.LINK_STATUS_ACTIVE);
859         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
860         processAllMessages();
861         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
862         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
863                 mNetworkTypeController.getOverrideNetworkType());
864 
865         // LTE -> LTE+
866         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
867         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
868         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
869                 new AsyncResult(null,
870                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
871         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
872         processAllMessages();
873         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
874                 mNetworkTypeController.getOverrideNetworkType());
875     }
876 
877     @Test
testEventPhysicalChannelChangeFromLteToLteCaInIdleState()878     public void testEventPhysicalChannelChangeFromLteToLteCaInIdleState() throws Exception {
879         // Remove RRC idle/RRC connected from 5G override
880         mBundle = mContextFixture.getCarrierConfigBundle();
881         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING,
882                 "connected_mmwave:5G_Plus,connected:5G");
883         sendCarrierConfigChanged();
884 
885         // Transition to idle state
886         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
887         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
888                 DataCallResponse.LINK_STATUS_DORMANT);
889         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
890         processAllMessages();
891         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
892         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
893                 mNetworkTypeController.getOverrideNetworkType());
894 
895         // LTE -> LTE+
896         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
897         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
898         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
899                 new AsyncResult(null,
900                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
901         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
902         processAllMessages();
903         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
904                 mNetworkTypeController.getOverrideNetworkType());
905     }
906 
907     @Test
testEventPhysicalLinkStatusChanged()908     public void testEventPhysicalLinkStatusChanged() throws Exception {
909         testTransitionToCurrentStateLteConnected();
910         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
911         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
912                 DataCallResponse.LINK_STATUS_DORMANT);
913 
914         processAllMessages();
915         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
916     }
917 
918     @Test
testEventPhysicalLinkStatusChangedSupportPhysicalChannelConfig1_6()919     public void testEventPhysicalLinkStatusChangedSupportPhysicalChannelConfig1_6()
920             throws Exception {
921         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
922                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
923         mNetworkTypeController =
924                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
925         processAllMessages();
926         testTransitionToCurrentStateLteConnectedSupportPhysicalChannelConfig1_6();
927         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
928         setPhysicalLinkStatus(false);
929         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
930                 new AsyncResult(null,
931                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
932         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
933         processAllMessages();
934         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
935     }
936 
937     @Test
testEventPhysicalLinkStatusChanged_UsingUserDataForRrcDetection()938     public void testEventPhysicalLinkStatusChanged_UsingUserDataForRrcDetection()
939             throws Exception {
940         mBundle.putBoolean(
941                 CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL, true);
942         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
943                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
944         mNetworkTypeController =
945                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
946         sendCarrierConfigChanged();
947         processAllMessages();
948         testTransitionToCurrentStateLteConnected_usingUserDataForRrcDetection();
949         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
950         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
951                 DataCallResponse.LINK_STATUS_DORMANT);
952 
953         processAllMessages();
954         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
955     }
956 
957     @Test
testEventPhysicalChannelConfigNotifChanged()958     public void testEventPhysicalChannelConfigNotifChanged() throws Exception {
959         testTransitionToCurrentStateNrConnected();
960         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
961                 mNetworkTypeController.getOverrideNetworkType());
962 
963         mNetworkTypeController.sendMessage(5 /* EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED */,
964                 new AsyncResult(null, false, null));
965         processAllMessages();
966         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
967                 mNetworkTypeController.getOverrideNetworkType());
968     }
969 
970     @Test
testEventRadioOffOrUnavailable()971     public void testEventRadioOffOrUnavailable() throws Exception {
972         mBundle.putBoolean(CarrierConfigManager.KEY_RATCHET_NR_ADVANCED_BANDWIDTH_IF_RRC_IDLE_BOOL,
973                 true);
974         testTransitionToCurrentStateNrConnectedMmwaveWithAdditionalBandAndNoMmwaveNrNsa();
975 
976         // Radio off
977         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
978         mNetworkTypeController.sendMessage(9 /* EVENT_RADIO_OFF_OR_UNAVAILABLE */);
979         processAllMessages();
980 
981         assertEquals("legacy", getCurrentState().getName());
982         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
983                 mNetworkTypeController.getOverrideNetworkType());
984 
985         // NR connected: Primary serving NR PCC with cell ID = 1, band = none
986         PhysicalChannelConfig pcc = new PhysicalChannelConfig.Builder()
987                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
988                 .setPhysicalCellId(1)
989                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
990                 .build();
991 
992         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
993                 new AsyncResult(null, List.of(pcc), null));
994         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
995         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
996         processAllMessages();
997         assertEquals("connected", getCurrentState().getName());
998     }
999 
1000     @Test
testEventPreferredNetworkModeChanged()1001     public void testEventPreferredNetworkModeChanged() throws Exception {
1002         testTransitionToCurrentStateNrConnected();
1003         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1004                 mNetworkTypeController.getOverrideNetworkType());
1005 
1006         // remove NR from preferred network types
1007         doReturn(RadioAccessFamily.getRafFromNetworkType(
1008                 TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA)).when(
1009                 mPhone).getCachedAllowedNetworkTypesBitmask();
1010 
1011         mNetworkTypeController.sendMessage(10 /* EVENT_PREFERRED_NETWORK_MODE_CHANGED */);
1012         processAllMessages();
1013         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1014                 mNetworkTypeController.getOverrideNetworkType());
1015     }
1016 
1017     @Test
testPrimaryTimerExpire()1018     public void testPrimaryTimerExpire() throws Exception {
1019         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1020         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1021                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1022         sendCarrierConfigChanged();
1023 
1024         assertEquals("connected", getCurrentState().getName());
1025         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1026                 mNetworkTypeController.getOverrideNetworkType());
1027 
1028         // should trigger 10 second timer
1029         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1030         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1031         processAllMessages();
1032 
1033         assertEquals("legacy", getCurrentState().getName());
1034         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1035                 mNetworkTypeController.getOverrideNetworkType());
1036         assertTrue(mNetworkTypeController.areAnyTimersActive());
1037 
1038         // timer expires
1039         moveTimeForward(10 * 1000);
1040         processAllMessages();
1041 
1042         assertEquals("legacy", getCurrentState().getName());
1043         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1044                 mNetworkTypeController.getOverrideNetworkType());
1045         assertFalse(mNetworkTypeController.areAnyTimersActive());
1046     }
1047 
1048     @Test
testPrimaryTimerNetworkTypeChanged()1049     public void testPrimaryTimerNetworkTypeChanged() throws Exception {
1050         doAnswer(invocation -> {
1051             doReturn(new TelephonyDisplayInfo(
1052                     mNetworkTypeController.getDataNetworkType(),
1053                     mNetworkTypeController.getOverrideNetworkType(),
1054                     false, false, false))
1055                     .when(mDisplayInfoController).getTelephonyDisplayInfo();
1056             return null;
1057         }).when(mDisplayInfoController).updateTelephonyDisplayInfo();
1058         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1059                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
1060                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1061                 .build();
1062         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1063                 anyInt(), anyInt());
1064         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1065                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1066         sendCarrierConfigChanged();
1067 
1068         assertEquals("connected", getCurrentState().getName());
1069         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1070                 mNetworkTypeController.getOverrideNetworkType());
1071 
1072         // trigger 10 second timer after disconnecting from NR advanced
1073         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1074                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE)
1075                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1076                 .build();
1077         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1078                 anyInt(), anyInt());
1079         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1080         processAllMessages();
1081 
1082         assertEquals("legacy", getCurrentState().getName());
1083         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1084                 mNetworkTypeController.getOverrideNetworkType());
1085         assertTrue(mNetworkTypeController.areAnyTimersActive());
1086 
1087         // timer expires
1088         moveTimeForward(10 * 1000);
1089         processAllMessages();
1090 
1091         assertEquals("legacy", getCurrentState().getName());
1092         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1093                 mNetworkTypeController.getOverrideNetworkType());
1094         assertFalse(mNetworkTypeController.areAnyTimersActive());
1095     }
1096 
1097     @Test
testPrimaryTimerDeviceIdleMode()1098     public void testPrimaryTimerDeviceIdleMode() throws Exception {
1099         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1100         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1101                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1102         sendCarrierConfigChanged();
1103 
1104         IPowerManager powerManager = mock(IPowerManager.class);
1105         PowerManager pm = new PowerManager(mContext, powerManager, mock(IThermalService.class),
1106                 new Handler(Looper.myLooper()));
1107         doReturn(pm).when(mContext).getSystemService(Context.POWER_SERVICE);
1108         doReturn(true).when(powerManager).isDeviceIdleMode();
1109         mNetworkTypeController.sendMessage(12 /* EVENT_DEVICE_IDLE_MODE_CHANGED */);
1110 
1111         assertEquals("connected", getCurrentState().getName());
1112         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1113                 mNetworkTypeController.getOverrideNetworkType());
1114 
1115         // should not trigger timer
1116         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1117         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1118         processAllMessages();
1119 
1120         assertEquals("legacy", getCurrentState().getName());
1121         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1122                 mNetworkTypeController.getOverrideNetworkType());
1123         assertFalse(mNetworkTypeController.areAnyTimersActive());
1124     }
1125 
1126     @Test
testPrimaryTimerReset()1127     public void testPrimaryTimerReset() throws Exception {
1128         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1129         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1130                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1131         sendCarrierConfigChanged();
1132 
1133         assertEquals("connected", getCurrentState().getName());
1134         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1135                 mNetworkTypeController.getOverrideNetworkType());
1136 
1137         // trigger 10 second timer after disconnecting from NR
1138         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1139         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1140         processAllMessages();
1141 
1142         assertEquals("legacy", getCurrentState().getName());
1143         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1144                 mNetworkTypeController.getOverrideNetworkType());
1145         assertTrue(mNetworkTypeController.areAnyTimersActive());
1146 
1147         // reconnect to NR in the middle of the timer
1148         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1149         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1150 
1151         // timer expires
1152         moveTimeForward(10 * 1000);
1153         processAllMessages();
1154 
1155         // timer should not have gone off
1156         assertEquals("connected", getCurrentState().getName());
1157         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1158                 mNetworkTypeController.getOverrideNetworkType());
1159         assertFalse(mNetworkTypeController.areAnyTimersActive());
1160     }
1161 
1162     @Test
testPrimaryTimerReset_theNetworkModeWithoutNr()1163     public void testPrimaryTimerReset_theNetworkModeWithoutNr() throws Exception {
1164         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1165         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1166                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1167         sendCarrierConfigChanged();
1168 
1169         assertEquals("connected", getCurrentState().getName());
1170         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1171                 mNetworkTypeController.getOverrideNetworkType());
1172 
1173         // remove NR from preferred network types
1174         doReturn(RadioAccessFamily.getRafFromNetworkType(
1175                 TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA)).when(
1176                 mPhone).getCachedAllowedNetworkTypesBitmask();
1177 
1178         // trigger 10 second timer after disconnecting from NR, and then it does the timer reset
1179         // since the network mode without the NR capability.
1180         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1181         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1182         processAllMessages();
1183 
1184         // timer should be reset.
1185         assertEquals("legacy", getCurrentState().getName());
1186         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1187                 mNetworkTypeController.getOverrideNetworkType());
1188         assertFalse(mNetworkTypeController.areAnyTimersActive());
1189     }
1190 
1191     @Test
testPrimaryTimerExpireMmwave()1192     public void testPrimaryTimerExpireMmwave() throws Exception {
1193         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1194         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1195         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1196                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1197         sendCarrierConfigChanged();
1198 
1199         assertEquals("connected_mmwave", getCurrentState().getName());
1200         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1201                 mNetworkTypeController.getOverrideNetworkType());
1202 
1203         // should trigger 10 second timer
1204         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1205         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1206         processAllMessages();
1207 
1208         assertEquals("connected", getCurrentState().getName());
1209         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1210                 mNetworkTypeController.getOverrideNetworkType());
1211         assertTrue(mNetworkTypeController.areAnyTimersActive());
1212 
1213         // timer expires
1214         moveTimeForward(10 * 1000);
1215         processAllMessages();
1216 
1217         assertEquals("connected", getCurrentState().getName());
1218         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1219                 mNetworkTypeController.getOverrideNetworkType());
1220         assertFalse(mNetworkTypeController.areAnyTimersActive());
1221     }
1222 
1223     @Test
testPrimaryTimerResetMmwave()1224     public void testPrimaryTimerResetMmwave() throws Exception {
1225         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1226         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1227         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1228                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1229         sendCarrierConfigChanged();
1230 
1231         assertEquals("connected_mmwave", getCurrentState().getName());
1232         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1233                 mNetworkTypeController.getOverrideNetworkType());
1234 
1235         // trigger 10 second timer after disconnecting from NR
1236         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1237         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1238         processAllMessages();
1239 
1240         assertEquals("connected", getCurrentState().getName());
1241         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1242                 mNetworkTypeController.getOverrideNetworkType());
1243         assertTrue(mNetworkTypeController.areAnyTimersActive());
1244 
1245         // reconnect to NR in the middle of the timer
1246         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1247         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1248 
1249         // timer expires
1250         moveTimeForward(10 * 1000);
1251         processAllMessages();
1252 
1253         // timer should not have gone off
1254         assertEquals("connected_mmwave", getCurrentState().getName());
1255         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1256                 mNetworkTypeController.getOverrideNetworkType());
1257         assertFalse(mNetworkTypeController.areAnyTimersActive());
1258     }
1259 
1260     @Test
testPrimaryTimerPrimaryCellChangeNrIdle()1261     public void testPrimaryTimerPrimaryCellChangeNrIdle() throws Exception {
1262         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1263         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1264         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1265                 .setPhysicalCellId(1)
1266                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1267                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1268                 .setBand(41)
1269                 .build());
1270         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1271         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1272                 new int[]{41});
1273         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1274                 "connected_mmwave,any,10");
1275         sendCarrierConfigChanged();
1276 
1277         assertEquals("connected_mmwave", getCurrentState().getName());
1278         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1279                 mNetworkTypeController.getOverrideNetworkType());
1280 
1281         // should trigger 10 second primary timer
1282         physicalChannelConfigs.clear();
1283         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1284                 new AsyncResult(null, physicalChannelConfigs, null));
1285         processAllMessages();
1286 
1287         assertEquals("connected_rrc_idle", getCurrentState().getName());
1288         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1289                 mNetworkTypeController.getOverrideNetworkType());
1290         assertTrue(mNetworkTypeController.areAnyTimersActive());
1291 
1292         // change PCI during connected_rrc_idle
1293         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1294                 .setPhysicalCellId(2)
1295                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1296                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1297                 .build());
1298         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1299                 new AsyncResult(null, physicalChannelConfigs, null));
1300         processAllMessages();
1301 
1302         assertEquals("connected_rrc_idle", getCurrentState().getName());
1303         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1304                 mNetworkTypeController.getOverrideNetworkType());
1305         assertTrue(mNetworkTypeController.areAnyTimersActive());
1306 
1307         // change PCI for the second time during connected_rrc_idle
1308         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1309                 .setPhysicalCellId(3)
1310                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1311                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1312                 .build());
1313         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1314                 new AsyncResult(null, physicalChannelConfigs, null));
1315         processAllMessages();
1316 
1317         assertEquals("connected", getCurrentState().getName());
1318         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1319                 mNetworkTypeController.getOverrideNetworkType());
1320         assertTrue(mNetworkTypeController.areAnyTimersActive());
1321 
1322         // primary timer expires
1323         moveTimeForward(10 * 1000);
1324         processAllMessages();
1325 
1326         assertEquals("connected", getCurrentState().getName());
1327         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1328                 mNetworkTypeController.getOverrideNetworkType());
1329         assertFalse(mNetworkTypeController.areAnyTimersActive());
1330     }
1331 
1332     @Test
testSecondaryTimerExpire()1333     public void testSecondaryTimerExpire() throws Exception {
1334         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1335         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1336                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1337         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1338                 "connected,any,30");
1339         sendCarrierConfigChanged();
1340 
1341         assertEquals("connected", getCurrentState().getName());
1342         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1343                 mNetworkTypeController.getOverrideNetworkType());
1344 
1345         // should trigger 10 second primary timer
1346         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1347         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1348         processAllMessages();
1349 
1350         assertEquals("legacy", getCurrentState().getName());
1351         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1352                 mNetworkTypeController.getOverrideNetworkType());
1353         assertTrue(mNetworkTypeController.areAnyTimersActive());
1354 
1355         // primary timer expires
1356         moveTimeForward(10 * 1000);
1357         processAllMessages();
1358 
1359         // should trigger 30 second secondary timer
1360         assertEquals("legacy", getCurrentState().getName());
1361         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1362                 mNetworkTypeController.getOverrideNetworkType());
1363         assertTrue(mNetworkTypeController.areAnyTimersActive());
1364 
1365         // secondary timer expires
1366         moveTimeForward(30 * 1000);
1367         processAllMessages();
1368 
1369         assertEquals("legacy", getCurrentState().getName());
1370         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1371                 mNetworkTypeController.getOverrideNetworkType());
1372         assertFalse(mNetworkTypeController.areAnyTimersActive());
1373     }
1374 
1375     @Test
testSecondaryPciTimerExpire()1376     public void testSecondaryPciTimerExpire() throws Exception {
1377         testTransitionToCurrentStateNrConnectedMmwave();
1378         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1379                 mNetworkTypeController.getOverrideNetworkType());
1380 
1381         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1382                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1383         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_PCI_CHANGE_SECONDARY_TIMER_SECONDS_INT,
1384                 30);
1385         sendCarrierConfigChanged();
1386 
1387         // should trigger 10 second primary timer
1388         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1389         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1390         processAllMessages();
1391 
1392         assertEquals("legacy", getCurrentState().getName());
1393         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1394                 mNetworkTypeController.getOverrideNetworkType());
1395         assertTrue(mNetworkTypeController.areAnyTimersActive());
1396 
1397         // Before primary timer expires, PCI changed, indicating 5G UW might soon recover
1398         moveTimeForward(5 * 1000);
1399         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1400                 new AsyncResult(null,
1401                         List.of(new PhysicalChannelConfig.Builder()
1402                                 .setPhysicalCellId(2)
1403                                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1404                                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1405                         .build()),
1406                         null));
1407         processAllMessages();
1408 
1409         // primary timer expires
1410         moveTimeForward(5 * 1000);
1411         processAllMessages();
1412 
1413         // should trigger 30 second secondary timer
1414         assertEquals("legacy", getCurrentState().getName());
1415         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1416                 mNetworkTypeController.getOverrideNetworkType());
1417         assertTrue(mNetworkTypeController.areAnyTimersActive());
1418 
1419         // secondary timer expires
1420         moveTimeForward(30 * 1000);
1421         processAllMessages();
1422 
1423         assertEquals("legacy", getCurrentState().getName());
1424         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1425                 mNetworkTypeController.getOverrideNetworkType());
1426         assertFalse(mNetworkTypeController.areAnyTimersActive());
1427     }
1428 
1429     @Test
testSecondaryTimerReset()1430     public void testSecondaryTimerReset() throws Exception {
1431         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1432         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1433                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1434         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1435                 "connected,any,30");
1436         sendCarrierConfigChanged();
1437 
1438         assertEquals("connected", getCurrentState().getName());
1439         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1440                 mNetworkTypeController.getOverrideNetworkType());
1441 
1442         // should trigger 10 second primary timer
1443         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1444         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1445         processAllMessages();
1446 
1447         assertEquals("legacy", getCurrentState().getName());
1448         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1449                 mNetworkTypeController.getOverrideNetworkType());
1450         assertTrue(mNetworkTypeController.areAnyTimersActive());
1451 
1452         // primary timer expires
1453         moveTimeForward(10 * 1000);
1454         processAllMessages();
1455 
1456         // should trigger 30 second secondary timer
1457         assertEquals("legacy", getCurrentState().getName());
1458         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1459                 mNetworkTypeController.getOverrideNetworkType());
1460         assertTrue(mNetworkTypeController.areAnyTimersActive());
1461 
1462         // reconnect to NR in the middle of the timer
1463         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1464         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1465 
1466         // secondary timer expires
1467         moveTimeForward(30 * 1000);
1468         processAllMessages();
1469 
1470         // timer should not have gone off
1471         assertEquals("connected", getCurrentState().getName());
1472         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1473                 mNetworkTypeController.getOverrideNetworkType());
1474         assertFalse(mNetworkTypeController.areAnyTimersActive());
1475     }
1476 
1477     @Test
testSecondaryTimerExpireMmwave()1478     public void testSecondaryTimerExpireMmwave() throws Exception {
1479         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1480         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1481         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1482                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1483         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1484                 "connected_mmwave,any,30");
1485         sendCarrierConfigChanged();
1486 
1487         assertEquals("connected_mmwave", getCurrentState().getName());
1488         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1489                 mNetworkTypeController.getOverrideNetworkType());
1490 
1491         // should trigger 10 second primary timer
1492         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1493         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1494         processAllMessages();
1495 
1496         assertEquals("connected", getCurrentState().getName());
1497         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1498                 mNetworkTypeController.getOverrideNetworkType());
1499         assertTrue(mNetworkTypeController.areAnyTimersActive());
1500 
1501         // primary timer expires
1502         moveTimeForward(10 * 1000);
1503         processAllMessages();
1504 
1505         // should trigger 30 second secondary timer
1506         assertEquals("connected", getCurrentState().getName());
1507         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1508                 mNetworkTypeController.getOverrideNetworkType());
1509         assertTrue(mNetworkTypeController.areAnyTimersActive());
1510 
1511         // secondary timer expires
1512         moveTimeForward(30 * 1000);
1513         processAllMessages();
1514 
1515         assertEquals("connected", getCurrentState().getName());
1516         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1517                 mNetworkTypeController.getOverrideNetworkType());
1518         assertFalse(mNetworkTypeController.areAnyTimersActive());
1519     }
1520 
1521     @Test
testSecondaryTimerResetMmwave()1522     public void testSecondaryTimerResetMmwave() throws Exception {
1523         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1524         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1525         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1526                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1527         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1528                 "connected_mmwave,any,30");
1529         sendCarrierConfigChanged();
1530 
1531         assertEquals("connected_mmwave", getCurrentState().getName());
1532         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1533                 mNetworkTypeController.getOverrideNetworkType());
1534 
1535         // should trigger 10 second primary timer
1536         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1537         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1538         processAllMessages();
1539 
1540         assertEquals("connected", getCurrentState().getName());
1541         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1542                 mNetworkTypeController.getOverrideNetworkType());
1543         assertTrue(mNetworkTypeController.areAnyTimersActive());
1544 
1545         // primary timer expires
1546         moveTimeForward(10 * 1000);
1547         processAllMessages();
1548 
1549         // should trigger 30 second secondary timer
1550         assertEquals("connected", getCurrentState().getName());
1551         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1552                 mNetworkTypeController.getOverrideNetworkType());
1553         assertTrue(mNetworkTypeController.areAnyTimersActive());
1554 
1555         // reconnect to NR in the middle of the timer
1556         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1557         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1558 
1559         // secondary timer expires
1560         moveTimeForward(30 * 1000);
1561         processAllMessages();
1562 
1563         // timer should not have gone off
1564         assertEquals("connected_mmwave", getCurrentState().getName());
1565         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1566                 mNetworkTypeController.getOverrideNetworkType());
1567         assertFalse(mNetworkTypeController.areAnyTimersActive());
1568     }
1569 
1570     @Test
testTransitionToNrIdle()1571     public void testTransitionToNrIdle() throws Exception {
1572         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1573         doReturn(ServiceState.FREQUENCY_RANGE_HIGH).when(mServiceState).getNrFrequencyRange();
1574         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1575         // use advanced band
1576         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1577                 .setPhysicalCellId(1)
1578                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1579                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1580                 .setBand(41)
1581                 .build());
1582         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1583         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1584                 new int[]{41});
1585         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1586                 "connected_mmwave,any,10");
1587         sendCarrierConfigChanged();
1588 
1589         assertEquals("connected_mmwave", getCurrentState().getName());
1590         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1591                 mNetworkTypeController.getOverrideNetworkType());
1592 
1593         // empty PCC, switch to connected_rrc_idle,
1594         // isNrAdvanced is still true(due to either advance band OR frequency)
1595         physicalChannelConfigs.clear();
1596         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1597                 new AsyncResult(null, physicalChannelConfigs, null));
1598         processAllMessages();
1599 
1600         assertEquals("connected_rrc_idle", getCurrentState().getName());
1601         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1602                 mNetworkTypeController.getOverrideNetworkType());
1603         assertTrue(mNetworkTypeController.areAnyTimersActive());
1604 
1605         // Received an event update, verify should stay in idle because physical link didn't change,
1606         // otherwise there is a loop between advance state and idle state.
1607         mNetworkTypeController.sendMessage(0 /* EVENT_UPDATE */);
1608         processAllMessages();
1609 
1610         assertEquals("connected_rrc_idle", getCurrentState().getName());
1611         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1612                 mNetworkTypeController.getOverrideNetworkType());
1613     }
1614 
1615     @Test
testSecondaryTimerAdvanceBand()1616     public void testSecondaryTimerAdvanceBand() throws Exception {
1617         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1618         doReturn(ServiceState.FREQUENCY_RANGE_HIGH).when(mServiceState).getNrFrequencyRange();
1619         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1620         // use advanced band
1621         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1622                 .setPhysicalCellId(1)
1623                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1624                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1625                 .setBand(41)
1626                 .build());
1627         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1628         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1629                 new int[]{41});
1630         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1631                 "connected_mmwave,any,10");
1632         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1633                 "connected_mmwave,any,5");
1634         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_BANDS_SECONDARY_TIMER_SECONDS_INT,
1635                 20);
1636         sendCarrierConfigChanged();
1637 
1638         assertEquals("connected_mmwave", getCurrentState().getName());
1639         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1640                 mNetworkTypeController.getOverrideNetworkType());
1641 
1642         // lost the advance band, trigger 10 second connected_mmwave -> connected primary timer
1643         physicalChannelConfigs.clear();
1644         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1645                 .setPhysicalCellId(1)
1646                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1647                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1648                 .build());
1649         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1650                 new AsyncResult(null, physicalChannelConfigs, null));
1651         processAllMessages();
1652 
1653         assertEquals("connected", getCurrentState().getName());
1654         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1655                 mNetworkTypeController.getOverrideNetworkType());
1656         assertTrue(mNetworkTypeController.areAnyTimersActive());
1657 
1658         // empty PCC, switch to connected_rrc_idle before primary timer expires
1659         physicalChannelConfigs.clear();
1660         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1661                 new AsyncResult(null, physicalChannelConfigs, null));
1662         processAllMessages();
1663 
1664         assertEquals("connected_rrc_idle", getCurrentState().getName());
1665         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1666                 mNetworkTypeController.getOverrideNetworkType());
1667         assertTrue(mNetworkTypeController.areAnyTimersActive());
1668 
1669         // primary timer expires
1670         moveTimeForward(10 * 1000);
1671         processAllMessages();
1672 
1673         // should trigger 20(not 5) seconds connected_mmwave -> connected_rrc_idle secondary timer
1674         assertEquals("connected_rrc_idle", getCurrentState().getName());
1675         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1676                 mNetworkTypeController.getOverrideNetworkType());
1677         assertTrue(mNetworkTypeController.areAnyTimersActive());
1678 
1679         // Verify secondary timer is still active after 6 seconds passed during
1680         // connected_mmwave -> connected_rrc_idle secondary timer, should still keep the primary
1681         // state icon.
1682         moveTimeForward((5 + 1) * 1000);
1683         processAllMessages();
1684         assertEquals("connected_rrc_idle", getCurrentState().getName());
1685         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1686                 mNetworkTypeController.getOverrideNetworkType());
1687         assertTrue(mNetworkTypeController.areAnyTimersActive());
1688     }
1689 
1690     @Test
testSecondaryTimerAdvanceBandReduceOnPciChange()1691     public void testSecondaryTimerAdvanceBandReduceOnPciChange() throws Exception {
1692         // The advance band secondary timer has been running for 6 seconds, 20 - 6 seconds are left.
1693         testSecondaryTimerAdvanceBand();
1694 
1695         // PCI changed from 1 to 2 for the first while the timer is running.
1696         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1697                 new AsyncResult(null, List.of(
1698                         new PhysicalChannelConfig.Builder()
1699                                 .setPhysicalCellId(2)
1700                                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1701                                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1702                                 .build()), null));
1703         processAllMessages();
1704 
1705         // Verify the first PCI change is exempted from triggering state change.
1706         assertEquals("connected_rrc_idle", getCurrentState().getName());
1707         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1708                 mNetworkTypeController.getOverrideNetworkType());
1709         assertTrue(mNetworkTypeController.areAnyTimersActive());
1710 
1711 
1712         // the timer has been reduced from 20 - 6s(advance band) to 5s(regular). Suppose passed 1s,
1713         // a new PCC shouldn't affect the timer.
1714         moveTimeForward(1 * 1000);
1715         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1716                 new AsyncResult(null, Collections.emptyList(), null));
1717         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1718                 new AsyncResult(null, List.of(
1719                         new PhysicalChannelConfig.Builder()
1720                                 .setPhysicalCellId(3)
1721                                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1722                                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1723                                 .build()), null));
1724         processAllMessages();
1725 
1726         // Verify the timer has been reduced from 20 - 6s(advance band) to 5s(regular).
1727         moveTimeForward(4 * 1000);
1728         processAllMessages();
1729 
1730         assertEquals("connected_rrc_idle", getCurrentState().getName());
1731         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1732                 mNetworkTypeController.getOverrideNetworkType());
1733         assertFalse(mNetworkTypeController.areAnyTimersActive());
1734     }
1735 
1736     @Test
testSecondaryTimerExpireNrIdle()1737     public void testSecondaryTimerExpireNrIdle() throws Exception {
1738         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1739         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1740         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1741                 .setPhysicalCellId(1)
1742                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1743                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1744                 .setBand(41)
1745                 .build());
1746         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1747         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1748                 new int[]{41});
1749         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1750                 "connected_mmwave,any,10");
1751         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1752                 "connected_mmwave,any,30");
1753         sendCarrierConfigChanged();
1754 
1755         assertEquals("connected_mmwave", getCurrentState().getName());
1756         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1757                 mNetworkTypeController.getOverrideNetworkType());
1758 
1759         // should trigger 10 second primary timer
1760         physicalChannelConfigs.clear();
1761         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1762                 .setPhysicalCellId(1)
1763                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1764                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1765                 .build());
1766         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1767                 new AsyncResult(null, physicalChannelConfigs, null));
1768         processAllMessages();
1769 
1770         assertEquals("connected", getCurrentState().getName());
1771         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1772                 mNetworkTypeController.getOverrideNetworkType());
1773         assertTrue(mNetworkTypeController.areAnyTimersActive());
1774 
1775         // switch to connected_rrc_idle
1776         physicalChannelConfigs.clear();
1777         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1778                 new AsyncResult(null, physicalChannelConfigs, null));
1779         processAllMessages();
1780 
1781         assertEquals("connected_rrc_idle", getCurrentState().getName());
1782         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1783                 mNetworkTypeController.getOverrideNetworkType());
1784         assertTrue(mNetworkTypeController.areAnyTimersActive());
1785 
1786         // primary timer expires
1787         moveTimeForward(10 * 1000);
1788         processAllMessages();
1789 
1790         // should trigger 30 second secondary timer
1791         assertEquals("connected_rrc_idle", getCurrentState().getName());
1792         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1793                 mNetworkTypeController.getOverrideNetworkType());
1794         assertTrue(mNetworkTypeController.areAnyTimersActive());
1795 
1796         // secondary timer expires
1797         moveTimeForward(30 * 1000);
1798         processAllMessages();
1799 
1800         assertEquals("connected_rrc_idle", getCurrentState().getName());
1801         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1802                 mNetworkTypeController.getOverrideNetworkType());
1803         assertFalse(mNetworkTypeController.areAnyTimersActive());
1804     }
1805 
1806     @Test
testSecondaryTimerResetNrIdle()1807     public void testSecondaryTimerResetNrIdle() throws Exception {
1808         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1809         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1810         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1811                 .setPhysicalCellId(1)
1812                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1813                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1814                 .setBand(41)
1815                 .build());
1816         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1817         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1818                 new int[]{41});
1819         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1820                 "connected_mmwave,any,10");
1821         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1822                 "connected_mmwave,any,30");
1823         sendCarrierConfigChanged();
1824 
1825         assertEquals("connected_mmwave", getCurrentState().getName());
1826         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1827                 mNetworkTypeController.getOverrideNetworkType());
1828 
1829         // should trigger 10 second primary timer
1830         physicalChannelConfigs.clear();
1831         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1832                 .setPhysicalCellId(1)
1833                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1834                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1835                 .build());
1836         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1837                 new AsyncResult(null, physicalChannelConfigs, null));
1838         processAllMessages();
1839 
1840         assertEquals("connected", getCurrentState().getName());
1841         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1842                 mNetworkTypeController.getOverrideNetworkType());
1843         assertTrue(mNetworkTypeController.areAnyTimersActive());
1844 
1845         // switch to connected_rrc_idle
1846         physicalChannelConfigs.clear();
1847         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1848                 new AsyncResult(null, physicalChannelConfigs, null));
1849         processAllMessages();
1850 
1851         assertEquals("connected_rrc_idle", getCurrentState().getName());
1852         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1853                 mNetworkTypeController.getOverrideNetworkType());
1854         assertTrue(mNetworkTypeController.areAnyTimersActive());
1855 
1856         // primary timer expires
1857         moveTimeForward(10 * 1000);
1858         processAllMessages();
1859 
1860         // should trigger 30 second secondary timer
1861         assertEquals("connected_rrc_idle", getCurrentState().getName());
1862         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1863                 mNetworkTypeController.getOverrideNetworkType());
1864         assertTrue(mNetworkTypeController.areAnyTimersActive());
1865 
1866         // reconnect to NR in the middle of the timer
1867         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1868                 .setPhysicalCellId(1)
1869                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1870                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1871                 .setBand(41)
1872                 .build());
1873         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1874                 new AsyncResult(null, physicalChannelConfigs, null));
1875 
1876         // secondary timer expires
1877         moveTimeForward(30 * 1000);
1878         processAllMessages();
1879 
1880         // timer should not have gone off
1881         assertEquals("connected_mmwave", getCurrentState().getName());
1882         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1883                 mNetworkTypeController.getOverrideNetworkType());
1884         assertFalse(mNetworkTypeController.areAnyTimersActive());
1885     }
1886 
1887     @Test
testSecondaryTimerPrimaryCellChangeNrIdle()1888     public void testSecondaryTimerPrimaryCellChangeNrIdle() throws Exception {
1889         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1890         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1891         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1892                 .setPhysicalCellId(1)
1893                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1894                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1895                 .setBand(41)
1896                 .build());
1897         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1898         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1899                 new int[]{41});
1900         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1901                 "connected_mmwave,any,10");
1902         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1903                 "connected_mmwave,any,30");
1904         sendCarrierConfigChanged();
1905 
1906         assertEquals("connected_mmwave", getCurrentState().getName());
1907         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1908                 mNetworkTypeController.getOverrideNetworkType());
1909 
1910         // should trigger 10 second primary timer
1911         physicalChannelConfigs.clear();
1912         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1913                 new AsyncResult(null, physicalChannelConfigs, null));
1914         processAllMessages();
1915 
1916         assertEquals("connected_rrc_idle", getCurrentState().getName());
1917         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1918                 mNetworkTypeController.getOverrideNetworkType());
1919         assertTrue(mNetworkTypeController.areAnyTimersActive());
1920 
1921         // primary timer expires
1922         moveTimeForward(10 * 1000);
1923         processAllMessages();
1924 
1925         // should trigger 30 second secondary timer
1926         assertEquals("connected_rrc_idle", getCurrentState().getName());
1927         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1928                 mNetworkTypeController.getOverrideNetworkType());
1929         assertTrue(mNetworkTypeController.areAnyTimersActive());
1930 
1931         // primary cell changes
1932         physicalChannelConfigs.clear();
1933         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1934                 .setPhysicalCellId(2)
1935                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1936                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1937                 .build());
1938         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1939                 new AsyncResult(null, physicalChannelConfigs, null));
1940         processAllMessages();
1941 
1942         assertEquals("connected_rrc_idle", getCurrentState().getName());
1943         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1944                 mNetworkTypeController.getOverrideNetworkType());
1945         assertTrue(mNetworkTypeController.areAnyTimersActive());
1946 
1947         // primary cell changes again
1948         physicalChannelConfigs.clear();
1949         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1950                 .setPhysicalCellId(3)
1951                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1952                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1953                 .build());
1954         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1955                 new AsyncResult(null, physicalChannelConfigs, null));
1956         processAllMessages();
1957 
1958         assertEquals("connected", getCurrentState().getName());
1959         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1960                 mNetworkTypeController.getOverrideNetworkType());
1961         assertFalse(mNetworkTypeController.areAnyTimersActive());
1962     }
1963 
1964     @Test
testNrTimerResetIn3g()1965     public void testNrTimerResetIn3g() throws Exception {
1966         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1967         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1968         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1969                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1970         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1971                 "connected_mmwave,any,30");
1972         sendCarrierConfigChanged();
1973 
1974         assertEquals("connected_mmwave", getCurrentState().getName());
1975         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1976                 mNetworkTypeController.getOverrideNetworkType());
1977 
1978         // should trigger 10 second primary timer
1979         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1980         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1981         processAllMessages();
1982 
1983         assertEquals("connected", getCurrentState().getName());
1984         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1985                 mNetworkTypeController.getOverrideNetworkType());
1986         assertTrue(mNetworkTypeController.areAnyTimersActive());
1987 
1988         // rat is UMTS, should stop timer
1989         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
1990                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_UMTS)
1991                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1992                 .build();
1993         doReturn(nri).when(mServiceState).getNetworkRegistrationInfo(anyInt(), anyInt());
1994         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1995         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1996         processAllMessages();
1997 
1998         assertEquals("legacy", getCurrentState().getName());
1999         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
2000                 mNetworkTypeController.getOverrideNetworkType());
2001         assertFalse(mNetworkTypeController.areAnyTimersActive());
2002     }
2003 
2004     @Test
testNrTimerResetWhenConnected()2005     public void testNrTimerResetWhenConnected() throws Exception {
2006         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
2007                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
2008         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
2009                 "connected_mmwave,any,30");
2010         sendCarrierConfigChanged();
2011 
2012         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
2013         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
2014                 DataCallResponse.LINK_STATUS_ACTIVE);
2015         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2016         processAllMessages();
2017 
2018         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
2019         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
2020                 mNetworkTypeController.getOverrideNetworkType());
2021 
2022         // should trigger 10 second primary timer
2023         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
2024         doReturn(ServiceState.FREQUENCY_RANGE_UNKNOWN).when(mServiceState).getNrFrequencyRange();
2025         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2026         processAllMessages();
2027 
2028         assertEquals("legacy", getCurrentState().getName());
2029         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
2030                 mNetworkTypeController.getOverrideNetworkType());
2031         assertTrue(mNetworkTypeController.areAnyTimersActive());
2032 
2033         // rat is NR, should stop timer
2034         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
2035                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
2036                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
2037                 .build();
2038         doReturn(nri).when(mServiceState).getNetworkRegistrationInfo(anyInt(), anyInt());
2039         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
2040         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2041         processAllMessages();
2042 
2043         assertEquals("connected", getCurrentState().getName());
2044         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
2045                 mNetworkTypeController.getOverrideNetworkType());
2046         assertFalse(mNetworkTypeController.areAnyTimersActive());
2047     }
2048 
2049     @Test
testNrTimerResetWhenConnectedAdvanced()2050     public void testNrTimerResetWhenConnectedAdvanced() throws Exception {
2051         testTransitionToCurrentStateNrConnectedMmwave();
2052         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
2053                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
2054         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
2055                 "connected_mmwave,any,30");
2056         sendCarrierConfigChanged();
2057 
2058         // should trigger 10 second primary timer
2059         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
2060         doReturn(ServiceState.FREQUENCY_RANGE_UNKNOWN).when(mServiceState).getNrFrequencyRange();
2061         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2062         processAllMessages();
2063 
2064         assertEquals("legacy", getCurrentState().getName());
2065         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
2066                 mNetworkTypeController.getOverrideNetworkType());
2067         assertTrue(mNetworkTypeController.areAnyTimersActive());
2068 
2069         // not advanced, should not stop timer
2070         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
2071         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2072         processAllMessages();
2073 
2074         assertEquals("connected", getCurrentState().getName());
2075         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
2076                 mNetworkTypeController.getOverrideNetworkType());
2077         assertTrue(mNetworkTypeController.areAnyTimersActive());
2078     }
2079 
2080     @Test
testNrTimerResetWhenPlmnChanged()2081     public void testNrTimerResetWhenPlmnChanged() throws Exception {
2082         testTransitionToCurrentStateNrConnectedMmwave();
2083         mBundle.putBoolean(CarrierConfigManager.KEY_NR_TIMERS_RESET_ON_PLMN_CHANGE_BOOL,
2084                 true);
2085         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
2086                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
2087         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
2088                 "connected_mmwave,any,30");
2089         sendCarrierConfigChanged();
2090 
2091         // should trigger 10 second primary timer
2092         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
2093         doReturn(ServiceState.FREQUENCY_RANGE_UNKNOWN).when(mServiceState).getNrFrequencyRange();
2094         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2095         processAllMessages();
2096 
2097         assertEquals("legacy", getCurrentState().getName());
2098         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
2099                 mNetworkTypeController.getOverrideNetworkType());
2100         assertTrue(mNetworkTypeController.areAnyTimersActive());
2101 
2102         // PLMN changed, should cancel any active timers
2103         ServiceState newSS = mock(ServiceState.class);
2104         doReturn("different plmn").when(newSS).getOperatorNumeric();
2105         doReturn(newSS).when(mSST).getServiceState();
2106         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2107         processAllMessages();
2108 
2109         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
2110                 mNetworkTypeController.getOverrideNetworkType());
2111         assertFalse(mNetworkTypeController.areAnyTimersActive());
2112     }
2113 
2114     @Test
testNrTimerResetWhenVoiceQos()2115     public void testNrTimerResetWhenVoiceQos() throws Exception {
2116         testTransitionToCurrentStateNrConnectedMmwave();
2117         mBundle.putBoolean(CarrierConfigManager.KEY_NR_TIMERS_RESET_ON_VOICE_QOS_BOOL,
2118                 true);
2119         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
2120                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
2121         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
2122                 "connected_mmwave,any,30");
2123         sendCarrierConfigChanged();
2124 
2125         // should trigger 10 second primary timer
2126         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
2127         doReturn(ServiceState.FREQUENCY_RANGE_UNKNOWN).when(mServiceState).getNrFrequencyRange();
2128         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2129         processAllMessages();
2130 
2131         assertEquals("legacy", getCurrentState().getName());
2132         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
2133                 mNetworkTypeController.getOverrideNetworkType());
2134         assertTrue(mNetworkTypeController.areAnyTimersActive());
2135 
2136         // Qos changed, but not in call, so no thing happens.
2137         ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
2138                 ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
2139         verify(mDataNetworkController).registerDataNetworkControllerCallback(
2140                 dataNetworkControllerCallbackCaptor.capture());
2141         DataNetworkControllerCallback callback = dataNetworkControllerCallbackCaptor.getValue();
2142         callback.onQosSessionsChanged(List.of(
2143                 new QosBearerSession(1, new EpsQos(
2144                         new Qos.QosBandwidth(1000, 1),
2145                         new Qos.QosBandwidth(1000, 0),
2146                         9 /* QCI */), Collections.emptyList())));
2147         processAllMessages();
2148 
2149         // Confirm if QCI not 1, timers are still active.
2150         assertTrue(mNetworkTypeController.areAnyTimersActive());
2151 
2152         // Send Voice QOS where QCI is 1, confirm timers are cancelled.
2153         callback.onQosSessionsChanged(List.of(
2154                 new QosBearerSession(1, new EpsQos(
2155                         new Qos.QosBandwidth(1000, 1),
2156                         new Qos.QosBandwidth(1000, 0),
2157                         1 /* QCI */), Collections.emptyList())));
2158         processAllMessages();
2159 
2160         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
2161                 mNetworkTypeController.getOverrideNetworkType());
2162         assertFalse(mNetworkTypeController.areAnyTimersActive());
2163     }
2164 
setPhysicalLinkStatus(boolean state)2165     private void setPhysicalLinkStatus(boolean state) {
2166         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
2167         // If PhysicalChannelConfigList is empty, PhysicalLinkStatus is
2168         // DataCallResponse.LINK_STATUS_DORMANT
2169         // If PhysicalChannelConfigList is not empty, PhysicalLinkStatus is
2170         // DataCallResponse.LINK_STATUS_ACTIVE
2171 
2172         if (state) {
2173             PhysicalChannelConfig physicalChannelConfig = new PhysicalChannelConfig.Builder()
2174                     .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
2175                     .setBand(41)
2176                     .build();
2177             lastPhysicalChannelConfigList.add(physicalChannelConfig);
2178         }
2179         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
2180     }
2181 
2182     @Test
testTransitionToCurrentStateNrConnectedWithLowBandwidth()2183     public void testTransitionToCurrentStateNrConnectedWithLowBandwidth() throws Exception {
2184         assertEquals("DefaultState", getCurrentState().getName());
2185         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
2186         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
2187         doReturn(new int[] {19999}).when(mServiceState).getCellBandwidths();
2188         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
2189         sendCarrierConfigChanged();
2190 
2191         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2192         processAllMessages();
2193         assertEquals("connected", getCurrentState().getName());
2194     }
2195 
2196     @Test
testTransitionToCurrentStateNrConnectedWithHighBandwidth()2197     public void testTransitionToCurrentStateNrConnectedWithHighBandwidth() throws Exception {
2198         assertEquals("DefaultState", getCurrentState().getName());
2199         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
2200         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
2201         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
2202         lastPhysicalChannelConfigList.add(new PhysicalChannelConfig.Builder()
2203                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
2204                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
2205                 .setPhysicalCellId(1)
2206                 .setCellBandwidthDownlinkKhz(19999)
2207                 .build());
2208         lastPhysicalChannelConfigList.add(new PhysicalChannelConfig.Builder()
2209                 .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
2210                 .setCellBandwidthDownlinkKhz(10000)
2211                 .build());
2212         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
2213         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
2214         sendCarrierConfigChanged();
2215         assertEquals("connected", getCurrentState().getName());
2216 
2217         mBundle.putBoolean(
2218                 CarrierConfigManager.KEY_INCLUDE_LTE_FOR_NR_ADVANCED_THRESHOLD_BANDWIDTH_BOOL,
2219                 true);
2220         sendCarrierConfigChanged();
2221         assertEquals("connected_mmwave", getCurrentState().getName());
2222     }
2223 
2224     @Test
testNrAdvancedDisabledWhileRoaming()2225     public void testNrAdvancedDisabledWhileRoaming() throws Exception {
2226         assertEquals("DefaultState", getCurrentState().getName());
2227         doReturn(true).when(mServiceState).getDataRoaming();
2228         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
2229         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
2230         mBundle.putBoolean(CarrierConfigManager.KEY_ENABLE_NR_ADVANCED_WHILE_ROAMING_BOOL, false);
2231         sendCarrierConfigChanged();
2232 
2233         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2234         processAllMessages();
2235         assertEquals("connected", getCurrentState().getName());
2236     }
2237 }
2238