• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.network.telephony;
18 
19 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.Activity;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.os.PersistableBundle;
33 import android.provider.Settings;
34 import android.telephony.CarrierConfigManager;
35 import android.telephony.SubscriptionManager;
36 import android.telephony.TelephonyManager;
37 
38 import com.android.internal.telephony.PhoneConstants;
39 import com.android.settings.network.ApnSettings;
40 import com.android.settingslib.RestrictedPreference;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.ArgumentCaptor;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.Robolectric;
49 import org.robolectric.RobolectricTestRunner;
50 
51 @RunWith(RobolectricTestRunner.class)
52 public class ApnPreferenceControllerTest {
53     private static final int SUB_ID = 2;
54 
55     @Mock
56     private TelephonyManager mTelephonyManager;
57     @Mock
58     private TelephonyManager mInvalidTelephonyManager;
59     @Mock
60     private SubscriptionManager mSubscriptionManager;
61     @Mock
62     private CarrierConfigManager mCarrierConfigManager;
63 
64     private ApnPreferenceController mController;
65     private RestrictedPreference mPreference;
66     private Context mContext;
67 
68     @Before
setUp()69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71 
72         mContext = spy(Robolectric.setupActivity(Activity.class));
73         doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
74         doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
75         doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
76         doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
77                 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
78         doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
79 
80         mPreference = new RestrictedPreference(mContext);
81         mController = new ApnPreferenceController(mContext, "mobile_data");
82         mController.init(SUB_ID);
83         mController.setPreference(mPreference);
84         mController.mCarrierConfigManager = mCarrierConfigManager;
85         mPreference.setKey(mController.getPreferenceKey());
86     }
87 
88     @Test
getAvailabilityStatus_apnSettingsNotSupported_returnUnavailable()89     public void getAvailabilityStatus_apnSettingsNotSupported_returnUnavailable() {
90         doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
91         final PersistableBundle bundle = new PersistableBundle();
92         bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, false);
93         doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
94 
95         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
96     }
97 
98     @Test
getAvailabilityStatus_apnSettingsSupportedWithCDMA_returnAvailable()99     public void getAvailabilityStatus_apnSettingsSupportedWithCDMA_returnAvailable() {
100         doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
101         final PersistableBundle bundle = new PersistableBundle();
102         bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, true);
103         doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
104 
105         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
106     }
107 
108     @Test
getAvailabilityStatus_apnSettingsSupportedWithGsm_returnAvailable()109     public void getAvailabilityStatus_apnSettingsSupportedWithGsm_returnAvailable() {
110         doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
111         final PersistableBundle bundle = new PersistableBundle();
112         bundle.putBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL, true);
113         doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
114 
115         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
116     }
117 
118     @Test
getAvailabilityStatus_carrierConfigNull_returnUnavailable()119     public void getAvailabilityStatus_carrierConfigNull_returnUnavailable() {
120         doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
121         when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(null);
122 
123         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
124     }
125 
126 
127     @Test
getAvailabilityStatus_hideCarrierNetworkSettings_returnUnavailable()128     public void getAvailabilityStatus_hideCarrierNetworkSettings_returnUnavailable() {
129         doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
130         final PersistableBundle bundle = new PersistableBundle();
131         bundle.putBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL, true);
132         bundle.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, true);
133         doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
134 
135         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
136     }
137 
138     @Test
handPreferenceTreeClick_fireIntent()139     public void handPreferenceTreeClick_fireIntent() {
140         ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
141 
142         mController.handlePreferenceTreeClick(mPreference);
143 
144         verify(mContext).startActivity(captor.capture());
145         final Intent intent = captor.getValue();
146         assertThat(intent.getAction()).isEqualTo(Settings.ACTION_APN_SETTINGS);
147         assertThat(intent.getIntExtra(ApnSettings.SUB_ID, 0)).isEqualTo(SUB_ID);
148     }
149 }
150