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         mNetworkTypeController =
116                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
117         processAllMessages();
118         verify(mCarrierConfigManager).registerCarrierConfigChangeListener(any(),
119                 listenerArgumentCaptor.capture());
120         mCarrierConfigChangeListener = listenerArgumentCaptor.getAllValues().get(0);
121     }
122 
123     @After
tearDown()124     public void tearDown() throws Exception {
125         mNetworkTypeController.getHandler().removeCallbacksAndMessages(null);
126         mNetworkTypeController = null;
127         mBundle = null;
128         super.tearDown();
129     }
130 
131     @Test
testUpdateOverrideNetworkTypeNrNsa()132     public void testUpdateOverrideNetworkTypeNrNsa() throws Exception {
133         // not NR
134         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
135         updateOverrideNetworkType();
136         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
137                 mNetworkTypeController.getOverrideNetworkType());
138 
139         // NR NSA, restricted
140         doReturn(NetworkRegistrationInfo.NR_STATE_RESTRICTED).when(mServiceState).getNrState();
141         updateOverrideNetworkType();
142         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
143                 mNetworkTypeController.getOverrideNetworkType());
144 
145         // NR NSA, not restricted
146         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
147         updateOverrideNetworkType();
148         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
149                 mNetworkTypeController.getOverrideNetworkType());
150 
151         // NR NSA, sub 6 frequency
152         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
153         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
154         updateOverrideNetworkType();
155         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
156                 mNetworkTypeController.getOverrideNetworkType());
157 
158         // NR NSA, millimeter wave frequency
159         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
160         updateOverrideNetworkType();
161         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
162                 mNetworkTypeController.getOverrideNetworkType());
163     }
164 
165     @Test
testUpdateOverrideNetworkTypeNrSa()166     public void testUpdateOverrideNetworkTypeNrSa() throws Exception {
167         // not NR
168         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
169         updateOverrideNetworkType();
170         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
171                 mNetworkTypeController.getOverrideNetworkType());
172 
173         // NR SA connected
174         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
175                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
176                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
177                 .build();
178         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
179                 anyInt(), anyInt());
180 
181         updateOverrideNetworkType();
182         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
183                 mNetworkTypeController.getOverrideNetworkType());
184     }
185 
186     @Test
testUpdateOverrideNetworkTypeNrSaMmwave()187     public void testUpdateOverrideNetworkTypeNrSaMmwave() throws Exception {
188         // not NR
189         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
190         updateOverrideNetworkType();
191         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
192                 mNetworkTypeController.getOverrideNetworkType());
193 
194         // NR SA connected and millimeter wave frequency
195         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
196                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
197                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
198                 .build();
199         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
200                 anyInt(), anyInt());
201         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
202 
203         updateOverrideNetworkType();
204         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
205                 mNetworkTypeController.getOverrideNetworkType());
206     }
207 
208     @Test
testUpdateOverrideNetworkTypeLte()209     public void testUpdateOverrideNetworkTypeLte() throws Exception {
210         // normal LTE
211         updateOverrideNetworkType();
212         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
213                 mNetworkTypeController.getOverrideNetworkType());
214 
215         // LTE CA at bandwidth threshold
216         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
217         doReturn(new int[] {20000}).when(mServiceState).getCellBandwidths();
218         updateOverrideNetworkType();
219         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
220                 mNetworkTypeController.getOverrideNetworkType());
221 
222         // LTE CA above bandwidth threshold
223         doReturn(new int[] {20000, 1400}).when(mServiceState).getCellBandwidths();
224         updateOverrideNetworkType();
225         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
226                 mNetworkTypeController.getOverrideNetworkType());
227 
228         // LTE ADVANCED PRO
229         doReturn("test_patternShowAdvanced").when(mServiceState).getOperatorAlphaLongRaw();
230         mBundle.putString(CarrierConfigManager.KEY_SHOW_CARRIER_DATA_ICON_PATTERN_STRING,
231                 ".*_patternShowAdvanced");
232         sendCarrierConfigChanged();
233         updateOverrideNetworkType();
234         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO,
235                 mNetworkTypeController.getOverrideNetworkType());
236     }
237 
238     @Test
testUpdateOverrideNetworkType()239     public void testUpdateOverrideNetworkType() throws Exception {
240         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
241         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
242         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
243                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_HSPAP)
244                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
245                 .build();
246         doReturn(nri).when(mServiceState).getNetworkRegistrationInfo(anyInt(), anyInt());
247         updateOverrideNetworkType();
248 
249         // override shouldn't be NR if not on LTE despite NR_STATE_CONNECTED
250         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
251                 mNetworkTypeController.getOverrideNetworkType());
252     }
253 
254     @Test
testTransitionToCurrentStateLegacy()255     public void testTransitionToCurrentStateLegacy() throws Exception {
256         assertEquals("DefaultState", getCurrentState().getName());
257         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
258         processAllMessages();
259         assertEquals("legacy", getCurrentState().getName());
260     }
261 
262     @Test
testTransitionToCurrentStateRestricted()263     public void testTransitionToCurrentStateRestricted() throws Exception {
264         assertEquals("DefaultState", getCurrentState().getName());
265         doReturn(NetworkRegistrationInfo.NR_STATE_RESTRICTED).when(mServiceState).getNrState();
266         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
267         processAllMessages();
268         assertEquals("restricted", getCurrentState().getName());
269     }
270 
271     @Test
testTransitionToCurrentStateIdle()272     public void testTransitionToCurrentStateIdle() throws Exception {
273         assertEquals("DefaultState", getCurrentState().getName());
274         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
275         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
276                 DataCallResponse.LINK_STATUS_DORMANT);
277         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
278         processAllMessages();
279         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
280     }
281 
282     @Test
testTransitionToCurrentStateIdleSupportPhysicalChannelConfig1_6()283     public void testTransitionToCurrentStateIdleSupportPhysicalChannelConfig1_6() throws Exception {
284         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
285                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
286         mNetworkTypeController =
287                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
288         processAllMessages();
289         assertEquals("DefaultState", getCurrentState().getName());
290 
291         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
292         setPhysicalLinkStatus(false);
293         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
294                 new AsyncResult(null,
295                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
296         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
297         processAllMessages();
298         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
299     }
300 
301     @Test
testTransitionToCurrentStateIdle_usingUserDataForRrcDetection()302     public void testTransitionToCurrentStateIdle_usingUserDataForRrcDetection() throws Exception {
303         mBundle.putBoolean(
304                 CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL, true);
305         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
306                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
307         mNetworkTypeController =
308                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
309         sendCarrierConfigChanged();
310         processAllMessages();
311         assertEquals("DefaultState", getCurrentState().getName());
312 
313         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
314         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
315                 DataCallResponse.LINK_STATUS_DORMANT);
316         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
317         processAllMessages();
318         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
319     }
320 
321     @Test
testTransitionToCurrentStateLteConnected()322     public void testTransitionToCurrentStateLteConnected() throws Exception {
323         assertEquals("DefaultState", getCurrentState().getName());
324         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
325         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
326                 DataCallResponse.LINK_STATUS_ACTIVE);
327         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
328         processAllMessages();
329         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
330     }
331 
332     @Test
testTransitionToCurrentStateLteConnectedSupportPhysicalChannelConfig1_6()333     public void testTransitionToCurrentStateLteConnectedSupportPhysicalChannelConfig1_6()
334             throws Exception {
335         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
336                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
337         mNetworkTypeController =
338                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
339         sendCarrierConfigChanged();
340         processAllMessages();
341         assertEquals("DefaultState", getCurrentState().getName());
342 
343         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
344         setPhysicalLinkStatus(true);
345         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
346                 new AsyncResult(null,
347                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
348         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
349         processAllMessages();
350         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
351     }
352 
353     @Test
testTransitionToCurrentStateLteConnected_usingUserDataForRrcDetection()354     public void testTransitionToCurrentStateLteConnected_usingUserDataForRrcDetection()
355             throws Exception {
356         mBundle.putBoolean(
357                 CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL, true);
358         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
359                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
360         mNetworkTypeController =
361                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
362         sendCarrierConfigChanged();
363         processAllMessages();
364         assertEquals("DefaultState", getCurrentState().getName());
365 
366         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
367         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
368                 DataCallResponse.LINK_STATUS_ACTIVE);
369         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
370         processAllMessages();
371         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
372     }
373 
374     @Test
testTransitionToCurrentStateNrConnectedIdle()375     public void testTransitionToCurrentStateNrConnectedIdle() throws Exception {
376         assertEquals("DefaultState", getCurrentState().getName());
377         doReturn(true).when(mFeatureFlags).supportNrSaRrcIdle();
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_mmwave", 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_mmwave", 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         testTransitionToCurrentStateNrConnectedMmwave();
813         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
814         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
815                 DataCallResponse.LINK_STATUS_ACTIVE);
816         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
817 
818         processAllMessages();
819         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
820     }
821 
822     @Test
testEventPhysicalChannelChangeFromLteToLteCaInLegacyState()823     public void testEventPhysicalChannelChangeFromLteToLteCaInLegacyState() throws Exception {
824         testTransitionToCurrentStateLegacy();
825         updateOverrideNetworkType();
826         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
827                 mNetworkTypeController.getOverrideNetworkType());
828 
829         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
830         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
831         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
832                 new AsyncResult(null,
833                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
834         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
835         processAllMessages();
836         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
837                 mNetworkTypeController.getOverrideNetworkType());
838     }
839 
840     @Test
testEventPhysicalChannelChangeFromLteToLteCaInLteConnectedState()841     public void testEventPhysicalChannelChangeFromLteToLteCaInLteConnectedState() throws Exception {
842         // Remove RRC idle/RRC connected from 5G override
843         mBundle = mContextFixture.getCarrierConfigBundle();
844         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING,
845                 "connected_mmwave:5G_Plus,connected:5G");
846         sendCarrierConfigChanged();
847 
848         // Transition to LTE connected state
849         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
850         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
851                 DataCallResponse.LINK_STATUS_ACTIVE);
852         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
853         processAllMessages();
854         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
855         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
856                 mNetworkTypeController.getOverrideNetworkType());
857 
858         // LTE -> LTE+
859         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
860         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
861         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
862                 new AsyncResult(null,
863                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
864         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
865         processAllMessages();
866         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
867                 mNetworkTypeController.getOverrideNetworkType());
868     }
869 
870     @Test
testEventPhysicalChannelChangeFromLteToLteCaInIdleState()871     public void testEventPhysicalChannelChangeFromLteToLteCaInIdleState() throws Exception {
872         // Remove RRC idle/RRC connected from 5G override
873         mBundle = mContextFixture.getCarrierConfigBundle();
874         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_CONFIGURATION_STRING,
875                 "connected_mmwave:5G_Plus,connected:5G");
876         sendCarrierConfigChanged();
877 
878         // Transition to idle state
879         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
880         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
881                 DataCallResponse.LINK_STATUS_DORMANT);
882         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
883         processAllMessages();
884         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
885         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
886                 mNetworkTypeController.getOverrideNetworkType());
887 
888         // LTE -> LTE+
889         doReturn(true).when(mServiceState).isUsingCarrierAggregation();
890         doReturn(new int[] {30000}).when(mServiceState).getCellBandwidths();
891         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
892                 new AsyncResult(null,
893                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
894         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
895         processAllMessages();
896         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA,
897                 mNetworkTypeController.getOverrideNetworkType());
898     }
899 
900     @Test
testEventPhysicalLinkStatusChanged()901     public void testEventPhysicalLinkStatusChanged() throws Exception {
902         testTransitionToCurrentStateLteConnected();
903         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
904         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
905                 DataCallResponse.LINK_STATUS_DORMANT);
906 
907         processAllMessages();
908         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
909     }
910 
911     @Test
testEventPhysicalLinkStatusChangedSupportPhysicalChannelConfig1_6()912     public void testEventPhysicalLinkStatusChangedSupportPhysicalChannelConfig1_6()
913             throws Exception {
914         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
915                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
916         mNetworkTypeController =
917                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
918         processAllMessages();
919         testTransitionToCurrentStateLteConnectedSupportPhysicalChannelConfig1_6();
920         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
921         setPhysicalLinkStatus(false);
922         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
923                 new AsyncResult(null,
924                         mPhone.getServiceStateTracker().getPhysicalChannelConfigList(), null));
925         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
926         processAllMessages();
927         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
928     }
929 
930     @Test
testEventPhysicalLinkStatusChanged_UsingUserDataForRrcDetection()931     public void testEventPhysicalLinkStatusChanged_UsingUserDataForRrcDetection()
932             throws Exception {
933         mBundle.putBoolean(
934                 CarrierConfigManager.KEY_LTE_ENDC_USING_USER_DATA_FOR_RRC_DETECTION_BOOL, true);
935         doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
936                 TelephonyManager.CAPABILITY_PHYSICAL_CHANNEL_CONFIG_1_6_SUPPORTED);
937         mNetworkTypeController =
938                 new NetworkTypeController(mPhone, mDisplayInfoController, mFeatureFlags);
939         sendCarrierConfigChanged();
940         processAllMessages();
941         testTransitionToCurrentStateLteConnected_usingUserDataForRrcDetection();
942         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
943         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
944                 DataCallResponse.LINK_STATUS_DORMANT);
945 
946         processAllMessages();
947         assertEquals("not_restricted_rrc_idle", getCurrentState().getName());
948     }
949 
950     @Test
testEventPhysicalChannelConfigNotifChanged()951     public void testEventPhysicalChannelConfigNotifChanged() throws Exception {
952         testTransitionToCurrentStateNrConnected();
953         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
954                 mNetworkTypeController.getOverrideNetworkType());
955 
956         mNetworkTypeController.sendMessage(5 /* EVENT_PHYSICAL_CHANNEL_CONFIG_NOTIF_CHANGED */,
957                 new AsyncResult(null, false, null));
958         processAllMessages();
959         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
960                 mNetworkTypeController.getOverrideNetworkType());
961     }
962 
963     @Test
testEventRadioOffOrUnavailable()964     public void testEventRadioOffOrUnavailable() throws Exception {
965         mBundle.putBoolean(CarrierConfigManager.KEY_RATCHET_NR_ADVANCED_BANDWIDTH_IF_RRC_IDLE_BOOL,
966                 true);
967         testTransitionToCurrentStateNrConnectedMmwaveWithAdditionalBandAndNoMmwaveNrNsa();
968 
969         // Radio off
970         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
971         mNetworkTypeController.sendMessage(9 /* EVENT_RADIO_OFF_OR_UNAVAILABLE */);
972         processAllMessages();
973 
974         assertEquals("legacy", getCurrentState().getName());
975         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
976                 mNetworkTypeController.getOverrideNetworkType());
977 
978         // NR connected: Primary serving NR PCC with cell ID = 1, band = none
979         PhysicalChannelConfig pcc = new PhysicalChannelConfig.Builder()
980                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
981                 .setPhysicalCellId(1)
982                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
983                 .build();
984 
985         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
986                 new AsyncResult(null, List.of(pcc), null));
987         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
988         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
989         processAllMessages();
990         assertEquals("connected", getCurrentState().getName());
991     }
992 
993     @Test
testEventPreferredNetworkModeChanged()994     public void testEventPreferredNetworkModeChanged() throws Exception {
995         testTransitionToCurrentStateNrConnected();
996         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
997                 mNetworkTypeController.getOverrideNetworkType());
998 
999         // remove NR from preferred network types
1000         doReturn(RadioAccessFamily.getRafFromNetworkType(
1001                 TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA)).when(
1002                 mPhone).getCachedAllowedNetworkTypesBitmask();
1003 
1004         mNetworkTypeController.sendMessage(10 /* EVENT_PREFERRED_NETWORK_MODE_CHANGED */);
1005         processAllMessages();
1006         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1007                 mNetworkTypeController.getOverrideNetworkType());
1008     }
1009 
1010     @Test
testPrimaryTimerExpire()1011     public void testPrimaryTimerExpire() throws Exception {
1012         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1013         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1014                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1015         sendCarrierConfigChanged();
1016 
1017         assertEquals("connected", getCurrentState().getName());
1018         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1019                 mNetworkTypeController.getOverrideNetworkType());
1020 
1021         // should trigger 10 second timer
1022         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1023         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1024         processAllMessages();
1025 
1026         assertEquals("legacy", getCurrentState().getName());
1027         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1028                 mNetworkTypeController.getOverrideNetworkType());
1029         assertTrue(mNetworkTypeController.areAnyTimersActive());
1030 
1031         // timer expires
1032         moveTimeForward(10 * 1000);
1033         processAllMessages();
1034 
1035         assertEquals("legacy", getCurrentState().getName());
1036         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1037                 mNetworkTypeController.getOverrideNetworkType());
1038         assertFalse(mNetworkTypeController.areAnyTimersActive());
1039     }
1040 
1041     @Test
testPrimaryTimerNetworkTypeChanged()1042     public void testPrimaryTimerNetworkTypeChanged() throws Exception {
1043         doAnswer(invocation -> {
1044             doReturn(new TelephonyDisplayInfo(
1045                     mNetworkTypeController.getDataNetworkType(),
1046                     mNetworkTypeController.getOverrideNetworkType(),
1047                     false)).when(mDisplayInfoController).getTelephonyDisplayInfo();
1048             return null;
1049         }).when(mDisplayInfoController).updateTelephonyDisplayInfo();
1050         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1051                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
1052                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1053                 .build();
1054         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1055                 anyInt(), anyInt());
1056         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1057                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1058         sendCarrierConfigChanged();
1059 
1060         assertEquals("connected", getCurrentState().getName());
1061         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1062                 mNetworkTypeController.getOverrideNetworkType());
1063 
1064         // trigger 10 second timer after disconnecting from NR advanced
1065         mNetworkRegistrationInfo = new NetworkRegistrationInfo.Builder()
1066                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE)
1067                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1068                 .build();
1069         doReturn(mNetworkRegistrationInfo).when(mServiceState).getNetworkRegistrationInfo(
1070                 anyInt(), anyInt());
1071         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1072         processAllMessages();
1073 
1074         assertEquals("legacy", getCurrentState().getName());
1075         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1076                 mNetworkTypeController.getOverrideNetworkType());
1077         assertTrue(mNetworkTypeController.areAnyTimersActive());
1078 
1079         // timer expires
1080         moveTimeForward(10 * 1000);
1081         processAllMessages();
1082 
1083         assertEquals("legacy", getCurrentState().getName());
1084         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1085                 mNetworkTypeController.getOverrideNetworkType());
1086         assertFalse(mNetworkTypeController.areAnyTimersActive());
1087     }
1088 
1089     @Test
testPrimaryTimerDeviceIdleMode()1090     public void testPrimaryTimerDeviceIdleMode() throws Exception {
1091         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1092         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1093                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1094         sendCarrierConfigChanged();
1095 
1096         IPowerManager powerManager = mock(IPowerManager.class);
1097         PowerManager pm = new PowerManager(mContext, powerManager, mock(IThermalService.class),
1098                 new Handler(Looper.myLooper()));
1099         doReturn(pm).when(mContext).getSystemService(Context.POWER_SERVICE);
1100         doReturn(true).when(powerManager).isDeviceIdleMode();
1101         mNetworkTypeController.sendMessage(12 /* EVENT_DEVICE_IDLE_MODE_CHANGED */);
1102 
1103         assertEquals("connected", getCurrentState().getName());
1104         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1105                 mNetworkTypeController.getOverrideNetworkType());
1106 
1107         // should not trigger timer
1108         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1109         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1110         processAllMessages();
1111 
1112         assertEquals("legacy", getCurrentState().getName());
1113         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1114                 mNetworkTypeController.getOverrideNetworkType());
1115         assertFalse(mNetworkTypeController.areAnyTimersActive());
1116     }
1117 
1118     @Test
testPrimaryTimerReset()1119     public void testPrimaryTimerReset() throws Exception {
1120         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1121         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1122                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1123         sendCarrierConfigChanged();
1124 
1125         assertEquals("connected", getCurrentState().getName());
1126         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1127                 mNetworkTypeController.getOverrideNetworkType());
1128 
1129         // trigger 10 second timer after disconnecting from NR
1130         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1131         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1132         processAllMessages();
1133 
1134         assertEquals("legacy", getCurrentState().getName());
1135         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1136                 mNetworkTypeController.getOverrideNetworkType());
1137         assertTrue(mNetworkTypeController.areAnyTimersActive());
1138 
1139         // reconnect to NR in the middle of the timer
1140         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1141         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1142 
1143         // timer expires
1144         moveTimeForward(10 * 1000);
1145         processAllMessages();
1146 
1147         // timer should not have gone off
1148         assertEquals("connected", getCurrentState().getName());
1149         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1150                 mNetworkTypeController.getOverrideNetworkType());
1151         assertFalse(mNetworkTypeController.areAnyTimersActive());
1152     }
1153 
1154     @Test
testPrimaryTimerReset_theNetworkModeWithoutNr()1155     public void testPrimaryTimerReset_theNetworkModeWithoutNr() throws Exception {
1156         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1157         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1158                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1159         sendCarrierConfigChanged();
1160 
1161         assertEquals("connected", getCurrentState().getName());
1162         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1163                 mNetworkTypeController.getOverrideNetworkType());
1164 
1165         // remove NR from preferred network types
1166         doReturn(RadioAccessFamily.getRafFromNetworkType(
1167                 TelephonyManager.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA)).when(
1168                 mPhone).getCachedAllowedNetworkTypesBitmask();
1169 
1170         // trigger 10 second timer after disconnecting from NR, and then it does the timer reset
1171         // since the network mode without the NR capability.
1172         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1173         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1174         processAllMessages();
1175 
1176         // timer should be reset.
1177         assertEquals("legacy", getCurrentState().getName());
1178         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1179                 mNetworkTypeController.getOverrideNetworkType());
1180         assertFalse(mNetworkTypeController.areAnyTimersActive());
1181     }
1182 
1183     @Test
testPrimaryTimerExpireMmwave()1184     public void testPrimaryTimerExpireMmwave() throws Exception {
1185         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1186         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1187         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1188                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1189         sendCarrierConfigChanged();
1190 
1191         assertEquals("connected_mmwave", getCurrentState().getName());
1192         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1193                 mNetworkTypeController.getOverrideNetworkType());
1194 
1195         // should trigger 10 second timer
1196         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1197         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1198         processAllMessages();
1199 
1200         assertEquals("connected", getCurrentState().getName());
1201         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1202                 mNetworkTypeController.getOverrideNetworkType());
1203         assertTrue(mNetworkTypeController.areAnyTimersActive());
1204 
1205         // timer expires
1206         moveTimeForward(10 * 1000);
1207         processAllMessages();
1208 
1209         assertEquals("connected", getCurrentState().getName());
1210         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1211                 mNetworkTypeController.getOverrideNetworkType());
1212         assertFalse(mNetworkTypeController.areAnyTimersActive());
1213     }
1214 
1215     @Test
testPrimaryTimerResetMmwave()1216     public void testPrimaryTimerResetMmwave() throws Exception {
1217         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1218         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1219         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1220                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1221         sendCarrierConfigChanged();
1222 
1223         assertEquals("connected_mmwave", getCurrentState().getName());
1224         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1225                 mNetworkTypeController.getOverrideNetworkType());
1226 
1227         // trigger 10 second timer after disconnecting from NR
1228         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1229         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1230         processAllMessages();
1231 
1232         assertEquals("connected", getCurrentState().getName());
1233         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1234                 mNetworkTypeController.getOverrideNetworkType());
1235         assertTrue(mNetworkTypeController.areAnyTimersActive());
1236 
1237         // reconnect to NR in the middle of the timer
1238         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1239         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1240 
1241         // timer expires
1242         moveTimeForward(10 * 1000);
1243         processAllMessages();
1244 
1245         // timer should not have gone off
1246         assertEquals("connected_mmwave", getCurrentState().getName());
1247         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1248                 mNetworkTypeController.getOverrideNetworkType());
1249         assertFalse(mNetworkTypeController.areAnyTimersActive());
1250     }
1251 
1252     @Test
testPrimaryTimerPrimaryCellChangeNrIdle()1253     public void testPrimaryTimerPrimaryCellChangeNrIdle() throws Exception {
1254         doReturn(true).when(mFeatureFlags).supportNrSaRrcIdle();
1255         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1256         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1257         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1258                 .setPhysicalCellId(1)
1259                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1260                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1261                 .setBand(41)
1262                 .build());
1263         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1264         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1265                 new int[]{41});
1266         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1267                 "connected_mmwave,any,10");
1268         sendCarrierConfigChanged();
1269 
1270         assertEquals("connected_mmwave", getCurrentState().getName());
1271         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1272                 mNetworkTypeController.getOverrideNetworkType());
1273 
1274         // should trigger 10 second primary timer
1275         physicalChannelConfigs.clear();
1276         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1277                 new AsyncResult(null, physicalChannelConfigs, null));
1278         processAllMessages();
1279 
1280         assertEquals("connected_rrc_idle", getCurrentState().getName());
1281         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1282                 mNetworkTypeController.getOverrideNetworkType());
1283         assertTrue(mNetworkTypeController.areAnyTimersActive());
1284 
1285         // change PCI during connected_rrc_idle
1286         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1287                 .setPhysicalCellId(2)
1288                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1289                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1290                 .build());
1291         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1292                 new AsyncResult(null, physicalChannelConfigs, null));
1293         processAllMessages();
1294 
1295         assertEquals("connected_rrc_idle", getCurrentState().getName());
1296         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1297                 mNetworkTypeController.getOverrideNetworkType());
1298         assertTrue(mNetworkTypeController.areAnyTimersActive());
1299 
1300         // change PCI for the second time during connected_rrc_idle
1301         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1302                 .setPhysicalCellId(3)
1303                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1304                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1305                 .build());
1306         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1307                 new AsyncResult(null, physicalChannelConfigs, null));
1308         processAllMessages();
1309 
1310         assertEquals("connected", getCurrentState().getName());
1311         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1312                 mNetworkTypeController.getOverrideNetworkType());
1313         assertTrue(mNetworkTypeController.areAnyTimersActive());
1314 
1315         // primary timer expires
1316         moveTimeForward(10 * 1000);
1317         processAllMessages();
1318 
1319         assertEquals("connected", getCurrentState().getName());
1320         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1321                 mNetworkTypeController.getOverrideNetworkType());
1322         assertFalse(mNetworkTypeController.areAnyTimersActive());
1323     }
1324 
1325     @Test
testSecondaryTimerExpire()1326     public void testSecondaryTimerExpire() throws Exception {
1327         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1328         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1329                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1330         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1331                 "connected,any,30");
1332         sendCarrierConfigChanged();
1333 
1334         assertEquals("connected", getCurrentState().getName());
1335         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1336                 mNetworkTypeController.getOverrideNetworkType());
1337 
1338         // should trigger 10 second primary timer
1339         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1340         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1341         processAllMessages();
1342 
1343         assertEquals("legacy", getCurrentState().getName());
1344         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1345                 mNetworkTypeController.getOverrideNetworkType());
1346         assertTrue(mNetworkTypeController.areAnyTimersActive());
1347 
1348         // primary timer expires
1349         moveTimeForward(10 * 1000);
1350         processAllMessages();
1351 
1352         // should trigger 30 second secondary timer
1353         assertEquals("legacy", getCurrentState().getName());
1354         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1355                 mNetworkTypeController.getOverrideNetworkType());
1356         assertTrue(mNetworkTypeController.areAnyTimersActive());
1357 
1358         // secondary timer expires
1359         moveTimeForward(30 * 1000);
1360         processAllMessages();
1361 
1362         assertEquals("legacy", getCurrentState().getName());
1363         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1364                 mNetworkTypeController.getOverrideNetworkType());
1365         assertFalse(mNetworkTypeController.areAnyTimersActive());
1366     }
1367 
1368     @Test
testSecondaryTimerReset()1369     public void testSecondaryTimerReset() throws Exception {
1370         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1371         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1372                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1373         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1374                 "connected,any,30");
1375         sendCarrierConfigChanged();
1376 
1377         assertEquals("connected", getCurrentState().getName());
1378         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1379                 mNetworkTypeController.getOverrideNetworkType());
1380 
1381         // should trigger 10 second primary timer
1382         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1383         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1384         processAllMessages();
1385 
1386         assertEquals("legacy", getCurrentState().getName());
1387         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1388                 mNetworkTypeController.getOverrideNetworkType());
1389         assertTrue(mNetworkTypeController.areAnyTimersActive());
1390 
1391         // primary timer expires
1392         moveTimeForward(10 * 1000);
1393         processAllMessages();
1394 
1395         // should trigger 30 second secondary timer
1396         assertEquals("legacy", getCurrentState().getName());
1397         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1398                 mNetworkTypeController.getOverrideNetworkType());
1399         assertTrue(mNetworkTypeController.areAnyTimersActive());
1400 
1401         // reconnect to NR in the middle of the timer
1402         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1403         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1404 
1405         // secondary timer expires
1406         moveTimeForward(30 * 1000);
1407         processAllMessages();
1408 
1409         // timer should not have gone off
1410         assertEquals("connected", getCurrentState().getName());
1411         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1412                 mNetworkTypeController.getOverrideNetworkType());
1413         assertFalse(mNetworkTypeController.areAnyTimersActive());
1414     }
1415 
1416     @Test
testSecondaryTimerExpireMmwave()1417     public void testSecondaryTimerExpireMmwave() throws Exception {
1418         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1419         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1420         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1421                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1422         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1423                 "connected_mmwave,any,30");
1424         sendCarrierConfigChanged();
1425 
1426         assertEquals("connected_mmwave", getCurrentState().getName());
1427         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1428                 mNetworkTypeController.getOverrideNetworkType());
1429 
1430         // should trigger 10 second primary timer
1431         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1432         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1433         processAllMessages();
1434 
1435         assertEquals("connected", getCurrentState().getName());
1436         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1437                 mNetworkTypeController.getOverrideNetworkType());
1438         assertTrue(mNetworkTypeController.areAnyTimersActive());
1439 
1440         // primary timer expires
1441         moveTimeForward(10 * 1000);
1442         processAllMessages();
1443 
1444         // should trigger 30 second secondary timer
1445         assertEquals("connected", getCurrentState().getName());
1446         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1447                 mNetworkTypeController.getOverrideNetworkType());
1448         assertTrue(mNetworkTypeController.areAnyTimersActive());
1449 
1450         // secondary timer expires
1451         moveTimeForward(30 * 1000);
1452         processAllMessages();
1453 
1454         assertEquals("connected", getCurrentState().getName());
1455         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1456                 mNetworkTypeController.getOverrideNetworkType());
1457         assertFalse(mNetworkTypeController.areAnyTimersActive());
1458     }
1459 
1460     @Test
testSecondaryTimerResetMmwave()1461     public void testSecondaryTimerResetMmwave() throws Exception {
1462         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1463         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1464         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1465                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1466         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1467                 "connected_mmwave,any,30");
1468         sendCarrierConfigChanged();
1469 
1470         assertEquals("connected_mmwave", getCurrentState().getName());
1471         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1472                 mNetworkTypeController.getOverrideNetworkType());
1473 
1474         // should trigger 10 second primary timer
1475         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1476         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1477         processAllMessages();
1478 
1479         assertEquals("connected", getCurrentState().getName());
1480         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1481                 mNetworkTypeController.getOverrideNetworkType());
1482         assertTrue(mNetworkTypeController.areAnyTimersActive());
1483 
1484         // primary timer expires
1485         moveTimeForward(10 * 1000);
1486         processAllMessages();
1487 
1488         // should trigger 30 second secondary timer
1489         assertEquals("connected", getCurrentState().getName());
1490         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1491                 mNetworkTypeController.getOverrideNetworkType());
1492         assertTrue(mNetworkTypeController.areAnyTimersActive());
1493 
1494         // reconnect to NR in the middle of the timer
1495         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1496         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1497 
1498         // secondary timer expires
1499         moveTimeForward(30 * 1000);
1500         processAllMessages();
1501 
1502         // timer should not have gone off
1503         assertEquals("connected_mmwave", getCurrentState().getName());
1504         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1505                 mNetworkTypeController.getOverrideNetworkType());
1506         assertFalse(mNetworkTypeController.areAnyTimersActive());
1507     }
1508 
1509     @Test
testSecondaryTimerAdvanceBand()1510     public void testSecondaryTimerAdvanceBand() throws Exception {
1511         doReturn(true).when(mFeatureFlags).supportNrSaRrcIdle();
1512         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1513         doReturn(ServiceState.FREQUENCY_RANGE_HIGH).when(mServiceState).getNrFrequencyRange();
1514         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1515         // use advanced band
1516         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1517                 .setPhysicalCellId(1)
1518                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1519                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1520                 .setBand(41)
1521                 .build());
1522         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1523         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1524                 new int[]{41});
1525         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1526                 "connected_mmwave,any,10");
1527         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1528                 "connected_mmwave,any,5");
1529         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_BANDS_SECONDARY_TIMER_SECONDS_INT,
1530                 20);
1531         sendCarrierConfigChanged();
1532 
1533         assertEquals("connected_mmwave", getCurrentState().getName());
1534         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1535                 mNetworkTypeController.getOverrideNetworkType());
1536 
1537         // lost the advance band, trigger 10 second connected_mmwave -> connected primary timer
1538         physicalChannelConfigs.clear();
1539         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1540                 .setPhysicalCellId(1)
1541                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1542                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1543                 .build());
1544         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1545                 new AsyncResult(null, physicalChannelConfigs, null));
1546         processAllMessages();
1547 
1548         assertEquals("connected", getCurrentState().getName());
1549         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1550                 mNetworkTypeController.getOverrideNetworkType());
1551         assertTrue(mNetworkTypeController.areAnyTimersActive());
1552 
1553         // empty PCC, switch to connected_rrc_idle before primary timer expires
1554         physicalChannelConfigs.clear();
1555         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1556                 new AsyncResult(null, physicalChannelConfigs, null));
1557         processAllMessages();
1558 
1559         assertEquals("connected_rrc_idle", getCurrentState().getName());
1560         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1561                 mNetworkTypeController.getOverrideNetworkType());
1562         assertTrue(mNetworkTypeController.areAnyTimersActive());
1563 
1564         // primary timer expires
1565         moveTimeForward(10 * 1000);
1566         processAllMessages();
1567 
1568         // should trigger 20(not 5) seconds connected_mmwave -> connected_rrc_idle secondary timer
1569         assertEquals("connected_rrc_idle", getCurrentState().getName());
1570         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1571                 mNetworkTypeController.getOverrideNetworkType());
1572         assertTrue(mNetworkTypeController.areAnyTimersActive());
1573 
1574         // Verify secondary timer is still active after 6 seconds passed during
1575         // connected_mmwave -> connected_rrc_idle secondary timer, should still keep the primary
1576         // state icon.
1577         moveTimeForward((5 + 1) * 1000);
1578         processAllMessages();
1579         assertEquals("connected_rrc_idle", getCurrentState().getName());
1580         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1581                 mNetworkTypeController.getOverrideNetworkType());
1582         assertTrue(mNetworkTypeController.areAnyTimersActive());
1583     }
1584 
1585     @Test
testSecondaryTimerAdvanceBandReduceOnPciChange()1586     public void testSecondaryTimerAdvanceBandReduceOnPciChange() throws Exception {
1587         // The advance band secondary timer has been running for 6 seconds, 20 - 6 seconds are left.
1588         testSecondaryTimerAdvanceBand();
1589 
1590         // PCI changed from 1 to 2 for the first while the timer is running.
1591         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1592                 new AsyncResult(null, List.of(
1593                         new PhysicalChannelConfig.Builder()
1594                                 .setPhysicalCellId(2)
1595                                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1596                                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1597                                 .build()), null));
1598         processAllMessages();
1599 
1600         // Verify the first PCI change is exempted from triggering state change.
1601         assertEquals("connected_rrc_idle", getCurrentState().getName());
1602         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1603                 mNetworkTypeController.getOverrideNetworkType());
1604         assertTrue(mNetworkTypeController.areAnyTimersActive());
1605 
1606         // Verify the timer has been reduced from 20 - 6s(advance band) to 5s(regular).
1607         moveTimeForward(5 * 1000);
1608         processAllMessages();
1609 
1610         assertEquals("connected_rrc_idle", getCurrentState().getName());
1611         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1612                 mNetworkTypeController.getOverrideNetworkType());
1613         assertFalse(mNetworkTypeController.areAnyTimersActive());
1614     }
1615 
1616     @Test
testSecondaryTimerExpireNrIdle()1617     public void testSecondaryTimerExpireNrIdle() throws Exception {
1618         doReturn(true).when(mFeatureFlags).supportNrSaRrcIdle();
1619         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1620         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
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,30");
1634         sendCarrierConfigChanged();
1635 
1636         assertEquals("connected_mmwave", getCurrentState().getName());
1637         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1638                 mNetworkTypeController.getOverrideNetworkType());
1639 
1640         // should trigger 10 second primary timer
1641         physicalChannelConfigs.clear();
1642         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1643                 .setPhysicalCellId(1)
1644                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1645                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1646                 .build());
1647         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1648                 new AsyncResult(null, physicalChannelConfigs, null));
1649         processAllMessages();
1650 
1651         assertEquals("connected", getCurrentState().getName());
1652         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1653                 mNetworkTypeController.getOverrideNetworkType());
1654         assertTrue(mNetworkTypeController.areAnyTimersActive());
1655 
1656         // switch to connected_rrc_idle
1657         physicalChannelConfigs.clear();
1658         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1659                 new AsyncResult(null, physicalChannelConfigs, null));
1660         processAllMessages();
1661 
1662         assertEquals("connected_rrc_idle", getCurrentState().getName());
1663         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1664                 mNetworkTypeController.getOverrideNetworkType());
1665         assertTrue(mNetworkTypeController.areAnyTimersActive());
1666 
1667         // primary timer expires
1668         moveTimeForward(10 * 1000);
1669         processAllMessages();
1670 
1671         // should trigger 30 second secondary timer
1672         assertEquals("connected_rrc_idle", getCurrentState().getName());
1673         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1674                 mNetworkTypeController.getOverrideNetworkType());
1675         assertTrue(mNetworkTypeController.areAnyTimersActive());
1676 
1677         // secondary timer expires
1678         moveTimeForward(30 * 1000);
1679         processAllMessages();
1680 
1681         assertEquals("connected_rrc_idle", getCurrentState().getName());
1682         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1683                 mNetworkTypeController.getOverrideNetworkType());
1684         assertFalse(mNetworkTypeController.areAnyTimersActive());
1685     }
1686 
1687     @Test
testSecondaryTimerResetNrIdle()1688     public void testSecondaryTimerResetNrIdle() throws Exception {
1689         doReturn(true).when(mFeatureFlags).supportNrSaRrcIdle();
1690         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1691         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1692         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1693                 .setPhysicalCellId(1)
1694                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1695                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1696                 .setBand(41)
1697                 .build());
1698         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1699         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1700                 new int[]{41});
1701         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1702                 "connected_mmwave,any,10");
1703         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1704                 "connected_mmwave,any,30");
1705         sendCarrierConfigChanged();
1706 
1707         assertEquals("connected_mmwave", getCurrentState().getName());
1708         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1709                 mNetworkTypeController.getOverrideNetworkType());
1710 
1711         // should trigger 10 second primary timer
1712         physicalChannelConfigs.clear();
1713         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1714                 .setPhysicalCellId(1)
1715                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1716                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1717                 .build());
1718         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1719                 new AsyncResult(null, physicalChannelConfigs, null));
1720         processAllMessages();
1721 
1722         assertEquals("connected", getCurrentState().getName());
1723         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1724                 mNetworkTypeController.getOverrideNetworkType());
1725         assertTrue(mNetworkTypeController.areAnyTimersActive());
1726 
1727         // switch to connected_rrc_idle
1728         physicalChannelConfigs.clear();
1729         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1730                 new AsyncResult(null, physicalChannelConfigs, null));
1731         processAllMessages();
1732 
1733         assertEquals("connected_rrc_idle", getCurrentState().getName());
1734         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1735                 mNetworkTypeController.getOverrideNetworkType());
1736         assertTrue(mNetworkTypeController.areAnyTimersActive());
1737 
1738         // primary timer expires
1739         moveTimeForward(10 * 1000);
1740         processAllMessages();
1741 
1742         // should trigger 30 second secondary timer
1743         assertEquals("connected_rrc_idle", getCurrentState().getName());
1744         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1745                 mNetworkTypeController.getOverrideNetworkType());
1746         assertTrue(mNetworkTypeController.areAnyTimersActive());
1747 
1748         // reconnect to NR in the middle of the timer
1749         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1750                 .setPhysicalCellId(1)
1751                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1752                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1753                 .setBand(41)
1754                 .build());
1755         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1756                 new AsyncResult(null, physicalChannelConfigs, null));
1757 
1758         // secondary timer expires
1759         moveTimeForward(30 * 1000);
1760         processAllMessages();
1761 
1762         // timer should not have gone off
1763         assertEquals("connected_mmwave", getCurrentState().getName());
1764         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1765                 mNetworkTypeController.getOverrideNetworkType());
1766         assertFalse(mNetworkTypeController.areAnyTimersActive());
1767     }
1768 
1769     @Test
testSecondaryTimerPrimaryCellChangeNrIdle()1770     public void testSecondaryTimerPrimaryCellChangeNrIdle() throws Exception {
1771         doReturn(true).when(mFeatureFlags).supportNrSaRrcIdle();
1772         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1773         ArrayList<PhysicalChannelConfig> physicalChannelConfigs = new ArrayList<>();
1774         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1775                 .setPhysicalCellId(1)
1776                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1777                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1778                 .setBand(41)
1779                 .build());
1780         doReturn(physicalChannelConfigs).when(mSST).getPhysicalChannelConfigList();
1781         mBundle.putIntArray(CarrierConfigManager.KEY_ADDITIONAL_NR_ADVANCED_BANDS_INT_ARRAY,
1782                 new int[]{41});
1783         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1784                 "connected_mmwave,any,10");
1785         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1786                 "connected_mmwave,any,30");
1787         sendCarrierConfigChanged();
1788 
1789         assertEquals("connected_mmwave", getCurrentState().getName());
1790         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1791                 mNetworkTypeController.getOverrideNetworkType());
1792 
1793         // should trigger 10 second primary timer
1794         physicalChannelConfigs.clear();
1795         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1796                 new AsyncResult(null, physicalChannelConfigs, null));
1797         processAllMessages();
1798 
1799         assertEquals("connected_rrc_idle", getCurrentState().getName());
1800         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1801                 mNetworkTypeController.getOverrideNetworkType());
1802         assertTrue(mNetworkTypeController.areAnyTimersActive());
1803 
1804         // primary timer expires
1805         moveTimeForward(10 * 1000);
1806         processAllMessages();
1807 
1808         // should trigger 30 second secondary timer
1809         assertEquals("connected_rrc_idle", getCurrentState().getName());
1810         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1811                 mNetworkTypeController.getOverrideNetworkType());
1812         assertTrue(mNetworkTypeController.areAnyTimersActive());
1813 
1814         // primary cell changes
1815         physicalChannelConfigs.clear();
1816         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1817                 .setPhysicalCellId(2)
1818                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1819                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1820                 .build());
1821         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1822                 new AsyncResult(null, physicalChannelConfigs, null));
1823         processAllMessages();
1824 
1825         assertEquals("connected_rrc_idle", getCurrentState().getName());
1826         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1827                 mNetworkTypeController.getOverrideNetworkType());
1828         assertTrue(mNetworkTypeController.areAnyTimersActive());
1829 
1830         // primary cell changes again
1831         physicalChannelConfigs.clear();
1832         physicalChannelConfigs.add(new PhysicalChannelConfig.Builder()
1833                 .setPhysicalCellId(3)
1834                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
1835                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
1836                 .build());
1837         mNetworkTypeController.sendMessage(11 /* EVENT_PHYSICAL_CHANNEL_CONFIGS_CHANGED */,
1838                 new AsyncResult(null, physicalChannelConfigs, null));
1839         processAllMessages();
1840 
1841         assertEquals("connected", getCurrentState().getName());
1842         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1843                 mNetworkTypeController.getOverrideNetworkType());
1844         assertFalse(mNetworkTypeController.areAnyTimersActive());
1845     }
1846 
1847     @Test
testNrTimerResetIn3g()1848     public void testNrTimerResetIn3g() throws Exception {
1849         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1850         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
1851         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1852                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1853         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1854                 "connected_mmwave,any,30");
1855         sendCarrierConfigChanged();
1856 
1857         assertEquals("connected_mmwave", getCurrentState().getName());
1858         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1859                 mNetworkTypeController.getOverrideNetworkType());
1860 
1861         // should trigger 10 second primary timer
1862         doReturn(ServiceState.FREQUENCY_RANGE_LOW).when(mServiceState).getNrFrequencyRange();
1863         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1864         processAllMessages();
1865 
1866         assertEquals("connected", getCurrentState().getName());
1867         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1868                 mNetworkTypeController.getOverrideNetworkType());
1869         assertTrue(mNetworkTypeController.areAnyTimersActive());
1870 
1871         // rat is UMTS, should stop timer
1872         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
1873                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_UMTS)
1874                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1875                 .build();
1876         doReturn(nri).when(mServiceState).getNetworkRegistrationInfo(anyInt(), anyInt());
1877         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1878         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1879         processAllMessages();
1880 
1881         assertEquals("legacy", getCurrentState().getName());
1882         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1883                 mNetworkTypeController.getOverrideNetworkType());
1884         assertFalse(mNetworkTypeController.areAnyTimersActive());
1885     }
1886 
1887     @Test
testNrTimerResetWhenConnected()1888     public void testNrTimerResetWhenConnected() throws Exception {
1889         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1890                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1891         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1892                 "connected_mmwave,any,30");
1893         sendCarrierConfigChanged();
1894 
1895         doReturn(NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED).when(mServiceState).getNrState();
1896         mNetworkTypeController.sendMessage(4 /* EVENT_PHYSICAL_LINK_STATUS_CHANGED */,
1897                 DataCallResponse.LINK_STATUS_ACTIVE);
1898         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1899         processAllMessages();
1900 
1901         assertEquals("not_restricted_rrc_con", getCurrentState().getName());
1902         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1903                 mNetworkTypeController.getOverrideNetworkType());
1904 
1905         // should trigger 10 second primary timer
1906         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1907         doReturn(ServiceState.FREQUENCY_RANGE_UNKNOWN).when(mServiceState).getNrFrequencyRange();
1908         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1909         processAllMessages();
1910 
1911         assertEquals("legacy", getCurrentState().getName());
1912         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA,
1913                 mNetworkTypeController.getOverrideNetworkType());
1914         assertTrue(mNetworkTypeController.areAnyTimersActive());
1915 
1916         // rat is NR, should stop timer
1917         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
1918                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_NR)
1919                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
1920                 .build();
1921         doReturn(nri).when(mServiceState).getNetworkRegistrationInfo(anyInt(), anyInt());
1922         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1923         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1924         processAllMessages();
1925 
1926         assertEquals("connected", getCurrentState().getName());
1927         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1928                 mNetworkTypeController.getOverrideNetworkType());
1929         assertFalse(mNetworkTypeController.areAnyTimersActive());
1930     }
1931 
1932     @Test
testNrTimerResetWhenConnectedAdvanced()1933     public void testNrTimerResetWhenConnectedAdvanced() throws Exception {
1934         testTransitionToCurrentStateNrConnectedMmwave();
1935         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
1936                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
1937         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
1938                 "connected_mmwave,any,30");
1939         sendCarrierConfigChanged();
1940 
1941         // should trigger 10 second primary timer
1942         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1943         doReturn(ServiceState.FREQUENCY_RANGE_UNKNOWN).when(mServiceState).getNrFrequencyRange();
1944         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1945         processAllMessages();
1946 
1947         assertEquals("legacy", getCurrentState().getName());
1948         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1949                 mNetworkTypeController.getOverrideNetworkType());
1950         assertTrue(mNetworkTypeController.areAnyTimersActive());
1951 
1952         // not advanced, should not stop timer
1953         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
1954         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1955         processAllMessages();
1956 
1957         assertEquals("connected", getCurrentState().getName());
1958         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1959                 mNetworkTypeController.getOverrideNetworkType());
1960         assertTrue(mNetworkTypeController.areAnyTimersActive());
1961     }
1962 
1963     @Test
testNrTimerResetWhenPlmnChanged()1964     public void testNrTimerResetWhenPlmnChanged() throws Exception {
1965         testTransitionToCurrentStateNrConnectedMmwave();
1966         mBundle.putBoolean(CarrierConfigManager.KEY_NR_TIMERS_RESET_ON_PLMN_CHANGE_BOOL,
1967                 true);
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         // should trigger 10 second primary timer
1975         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
1976         doReturn(ServiceState.FREQUENCY_RANGE_UNKNOWN).when(mServiceState).getNrFrequencyRange();
1977         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1978         processAllMessages();
1979 
1980         assertEquals("legacy", getCurrentState().getName());
1981         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
1982                 mNetworkTypeController.getOverrideNetworkType());
1983         assertTrue(mNetworkTypeController.areAnyTimersActive());
1984 
1985         // PLMN changed, should cancel any active timers
1986         ServiceState newSS = mock(ServiceState.class);
1987         doReturn("different plmn").when(newSS).getOperatorNumeric();
1988         doReturn(newSS).when(mSST).getServiceState();
1989         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
1990         processAllMessages();
1991 
1992         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
1993                 mNetworkTypeController.getOverrideNetworkType());
1994         assertFalse(mNetworkTypeController.areAnyTimersActive());
1995     }
1996 
1997     @Test
testNrTimerResetWhenVoiceQos()1998     public void testNrTimerResetWhenVoiceQos() throws Exception {
1999         testTransitionToCurrentStateNrConnectedMmwave();
2000         mBundle.putBoolean(CarrierConfigManager.KEY_NR_TIMERS_RESET_ON_VOICE_QOS_BOOL,
2001                 true);
2002         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_GRACE_PERIOD_STRING,
2003                 "connected_mmwave,any,10;connected,any,10;not_restricted_rrc_con,any,10");
2004         mBundle.putString(CarrierConfigManager.KEY_5G_ICON_DISPLAY_SECONDARY_GRACE_PERIOD_STRING,
2005                 "connected_mmwave,any,30");
2006         sendCarrierConfigChanged();
2007 
2008         // should trigger 10 second primary timer
2009         doReturn(NetworkRegistrationInfo.NR_STATE_NONE).when(mServiceState).getNrState();
2010         doReturn(ServiceState.FREQUENCY_RANGE_UNKNOWN).when(mServiceState).getNrFrequencyRange();
2011         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2012         processAllMessages();
2013 
2014         assertEquals("legacy", getCurrentState().getName());
2015         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED,
2016                 mNetworkTypeController.getOverrideNetworkType());
2017         assertTrue(mNetworkTypeController.areAnyTimersActive());
2018 
2019         // Qos changed, but not in call, so no thing happens.
2020         ArgumentCaptor<DataNetworkControllerCallback> dataNetworkControllerCallbackCaptor =
2021                 ArgumentCaptor.forClass(DataNetworkControllerCallback.class);
2022         verify(mDataNetworkController).registerDataNetworkControllerCallback(
2023                 dataNetworkControllerCallbackCaptor.capture());
2024         DataNetworkControllerCallback callback = dataNetworkControllerCallbackCaptor.getValue();
2025         callback.onQosSessionsChanged(List.of(
2026                 new QosBearerSession(1, new EpsQos(
2027                         new Qos.QosBandwidth(1000, 1),
2028                         new Qos.QosBandwidth(1000, 0),
2029                         9 /* QCI */), Collections.emptyList())));
2030         processAllMessages();
2031 
2032         // Confirm if QCI not 1, timers are still active.
2033         assertTrue(mNetworkTypeController.areAnyTimersActive());
2034 
2035         // Send Voice QOS where QCI is 1, confirm timers are cancelled.
2036         callback.onQosSessionsChanged(List.of(
2037                 new QosBearerSession(1, new EpsQos(
2038                         new Qos.QosBandwidth(1000, 1),
2039                         new Qos.QosBandwidth(1000, 0),
2040                         1 /* QCI */), Collections.emptyList())));
2041         processAllMessages();
2042 
2043         assertEquals(TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE,
2044                 mNetworkTypeController.getOverrideNetworkType());
2045         assertFalse(mNetworkTypeController.areAnyTimersActive());
2046     }
2047 
setPhysicalLinkStatus(boolean state)2048     private void setPhysicalLinkStatus(boolean state) {
2049         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
2050         // If PhysicalChannelConfigList is empty, PhysicalLinkStatus is
2051         // DataCallResponse.LINK_STATUS_DORMANT
2052         // If PhysicalChannelConfigList is not empty, PhysicalLinkStatus is
2053         // DataCallResponse.LINK_STATUS_ACTIVE
2054 
2055         if (state) {
2056             PhysicalChannelConfig physicalChannelConfig = new PhysicalChannelConfig.Builder()
2057                     .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
2058                     .setBand(41)
2059                     .build();
2060             lastPhysicalChannelConfigList.add(physicalChannelConfig);
2061         }
2062         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
2063     }
2064 
2065     @Test
testTransitionToCurrentStateNrConnectedWithLowBandwidth()2066     public void testTransitionToCurrentStateNrConnectedWithLowBandwidth() throws Exception {
2067         assertEquals("DefaultState", getCurrentState().getName());
2068         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
2069         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
2070         doReturn(new int[] {19999}).when(mServiceState).getCellBandwidths();
2071         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
2072         sendCarrierConfigChanged();
2073 
2074         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2075         processAllMessages();
2076         assertEquals("connected", getCurrentState().getName());
2077     }
2078 
2079     @Test
testTransitionToCurrentStateNrConnectedWithHighBandwidth()2080     public void testTransitionToCurrentStateNrConnectedWithHighBandwidth() throws Exception {
2081         assertEquals("DefaultState", getCurrentState().getName());
2082         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
2083         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
2084         List<PhysicalChannelConfig> lastPhysicalChannelConfigList = new ArrayList<>();
2085         lastPhysicalChannelConfigList.add(new PhysicalChannelConfig.Builder()
2086                 .setNetworkType(TelephonyManager.NETWORK_TYPE_NR)
2087                 .setCellConnectionStatus(CellInfo.CONNECTION_PRIMARY_SERVING)
2088                 .setPhysicalCellId(1)
2089                 .setCellBandwidthDownlinkKhz(19999)
2090                 .build());
2091         lastPhysicalChannelConfigList.add(new PhysicalChannelConfig.Builder()
2092                 .setNetworkType(TelephonyManager.NETWORK_TYPE_LTE)
2093                 .setCellBandwidthDownlinkKhz(10000)
2094                 .build());
2095         doReturn(lastPhysicalChannelConfigList).when(mSST).getPhysicalChannelConfigList();
2096         mBundle.putInt(CarrierConfigManager.KEY_NR_ADVANCED_THRESHOLD_BANDWIDTH_KHZ_INT, 20000);
2097         sendCarrierConfigChanged();
2098         assertEquals("connected", getCurrentState().getName());
2099 
2100         mBundle.putBoolean(
2101                 CarrierConfigManager.KEY_INCLUDE_LTE_FOR_NR_ADVANCED_THRESHOLD_BANDWIDTH_BOOL,
2102                 true);
2103         sendCarrierConfigChanged();
2104         assertEquals("connected_mmwave", getCurrentState().getName());
2105     }
2106 
2107     @Test
testNrAdvancedDisabledWhileRoaming()2108     public void testNrAdvancedDisabledWhileRoaming() throws Exception {
2109         assertEquals("DefaultState", getCurrentState().getName());
2110         doReturn(true).when(mServiceState).getDataRoaming();
2111         doReturn(NetworkRegistrationInfo.NR_STATE_CONNECTED).when(mServiceState).getNrState();
2112         doReturn(ServiceState.FREQUENCY_RANGE_MMWAVE).when(mServiceState).getNrFrequencyRange();
2113         mBundle.putBoolean(CarrierConfigManager.KEY_ENABLE_NR_ADVANCED_WHILE_ROAMING_BOOL, false);
2114         sendCarrierConfigChanged();
2115 
2116         mNetworkTypeController.sendMessage(3 /* EVENT_SERVICE_STATE_CHANGED */);
2117         processAllMessages();
2118         assertEquals("connected", getCurrentState().getName());
2119     }
2120 }
2121