Merge changes I773aeabd,I6a9d7044

* changes:
  Refactor DataServicePreference
  Refactor 4gForLTE preference
This commit is contained in:
Lei Yu
2018-10-19 20:42:12 +00:00
committed by Android (Google) Code Review
6 changed files with 526 additions and 93 deletions

View File

@@ -0,0 +1,137 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network.telephony;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import androidx.preference.Preference;
import com.android.internal.telephony.PhoneConstants;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class DataServiceSetupPreferenceControllerTest {
private static final int SUB_ID = 2;
private static final String SETUP_URL = "url://tmp_url:^1";
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private PersistableBundle mCarrierConfig;
private DataServiceSetupPreferenceController mController;
private Preference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL, SETUP_URL);
mCarrierConfig = new PersistableBundle();
doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
mPreference = new RestrictedPreference(mContext);
mController = new DataServiceSetupPreferenceController(mContext, "data_service_setup");
mController.init(SUB_ID);
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_allConfigOn_returnAvailable() {
doReturn(PhoneConstants.LTE_ON_CDMA_TRUE).when(mTelephonyManager).getLteOnCdmaMode();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_missUrl_returnUnavailable() {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL, "");
doReturn(PhoneConstants.LTE_ON_CDMA_TRUE).when(mTelephonyManager).getLteOnCdmaMode();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
mController = new DataServiceSetupPreferenceController(mContext, "data_service_setup");
mController.init(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_notCdma_returnUnavailable() {
doReturn(PhoneConstants.LTE_ON_CDMA_FALSE).when(mTelephonyManager).getLteOnCdmaMode();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void handlePreferenceTreeClick_startActivity() {
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
mController.handlePreferenceTreeClick(mPreference);
verify(mContext).startActivity(captor.capture());
final Intent intent = captor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
assertThat(intent.getData()).isEqualTo(
Uri.parse(TextUtils.expandTemplate(SETUP_URL, "").toString()));
}
}

View File

@@ -0,0 +1,138 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network.telephony;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.SwitchPreference;
import com.android.ims.ImsManager;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedSwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class Enhanced4gLtePreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
@Mock
private ImsManager mImsManager;
private Enhanced4gLtePreferenceController mController;
private SwitchPreference mPreference;
private PersistableBundle mCarrierConfig;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
mCarrierConfig = new PersistableBundle();
doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
mPreference = new RestrictedSwitchPreference(mContext);
mController = new Enhanced4gLtePreferenceController(mContext, "roaming");
mController.mImsManager = mImsManager;
mController.init(SUB_ID);
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_invalidSubId_returnUnavailable() {
mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_volteDisabled_returnUnavailable() {
doReturn(false).when(mImsManager).isVolteEnabledByPlatform();
doReturn(true).when(mImsManager).isVolteProvisionedOnDevice();
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
}
@Test
public void updateState_variant4gLte_useVariantTitle() {
mCarrierConfig.putInt(CarrierConfigManager.KEY_ENHANCED_4G_LTE_TITLE_VARIANT_INT, 1);
mController.updateState(mPreference);
assertThat(mPreference.getTitle()).isEqualTo(
mContext.getString(R.string.enhanced_4g_lte_mode_title_variant));
}
@Test
public void updateState_configEnabled_prefEnabled() {
mPreference.setEnabled(false);
mCarrierConfig.putInt(CarrierConfigManager.KEY_ENHANCED_4G_LTE_TITLE_VARIANT_INT, 1);
doReturn(TelephonyManager.CALL_STATE_IDLE).when(mTelephonyManager).getCallState(SUB_ID);
doReturn(true).when(mImsManager).isNonTtyOrTtyOnVolteEnabled();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_EDITABLE_ENHANCED_4G_LTE_BOOL, true);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isTrue();
}
@Test
public void updateState_configOn_prefChecked() {
mPreference.setChecked(false);
doReturn(true).when(mImsManager).isEnhanced4gLteModeSettingEnabledByUser();
doReturn(true).when(mImsManager).isNonTtyOrTtyOnVolteEnabled();
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
}