Adds Enable2gPreferenceController and unit tests.

Adds (1) Enable2gPreferenceController, the controller for the
"Enable 2G" toggle, and (2) Enable2gPreferenceControllerTest,
the corresponding unit tests.

Test: manul & atest -c Enable2gPreferenceControllerTest
Bug: b/163168917
Change-Id: Ib827cfcfd96fdea77b586c92ae0ec5b2bd5818c3
This commit is contained in:
Yomna Nasser
2021-01-18 22:08:06 +00:00
parent 776919fc0a
commit 6d65cb995b
5 changed files with 230 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2020 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.when;
import android.content.Context;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public final class Enable2gPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private PersistableBundle mPersistableBundle;
private Enable2gPreferenceController mController;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(CarrierConfigManager.class))
.thenReturn(mCarrierConfigManager);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
mPersistableBundle = new PersistableBundle();
doReturn(mPersistableBundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
doReturn(mPersistableBundle).when(mCarrierConfigManager).getConfigForSubId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
mController = new Enable2gPreferenceController(mContext, "mobile_data");
mController.init(SUB_ID);
}
@Test
public void getAvailabilityStatus_invalidSubId_returnUnavailable() {
mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_hideEnable2g_returnUnavailable() {
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_nullCarrierConfig_returnUnavailable() {
doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_ALLOWED_NETWORK_TYPES_USED);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
false);
doReturn(null).when(mCarrierConfigManager);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_capabilityNotSupported_returnUnavailable() {
doReturn(false).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_ALLOWED_NETWORK_TYPES_USED);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_returnAvailable() {
doReturn(true).when(mTelephonyManager).isRadioInterfaceCapabilitySupported(
mTelephonyManager.CAPABILITY_ALLOWED_NETWORK_TYPES_USED);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_ENABLE_2G,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
}