Fix CarrierSettings preference

This used the CarrierConfigManager to get the CarrierSettings activity
component and open that activity when the preference is clicked.

Bug: 117651939
Test: make -j40 ROBOTEST_FILTER=telephony RunSettingsRoboTests
Change-Id: Ic5267bba18ab32fbcc2e04aa61e7361151130275
This commit is contained in:
Pengquan Meng
2018-11-20 13:56:28 -08:00
parent 408dc883f7
commit 6da4302864
2 changed files with 88 additions and 1 deletions

View File

@@ -21,10 +21,18 @@ import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_U
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
@@ -38,12 +46,15 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.ArgumentCaptor;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class CarrierPreferenceControllerTest {
private static final int SUB_ID = 2;
private static final String CARRIER_SETTINGS_COMPONENT = "packageName/className";
@Mock
private TelephonyManager mTelephonyManager;
@@ -106,4 +117,55 @@ public class CarrierPreferenceControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void handlePreferenceClick_activityFound_openCarrierSettingActivity() {
final PersistableBundle bundle = new PersistableBundle();
bundle.putString(
CarrierConfigManager.KEY_CARRIER_SETTINGS_ACTIVITY_COMPONENT_NAME_STRING,
CARRIER_SETTINGS_COMPONENT);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
PackageManager pm = Mockito.mock(PackageManager.class);
doReturn(pm).when(mContext).getPackageManager();
doReturn(new ResolveInfo()).when(pm).resolveActivity(any(Intent.class), anyInt());
mController.handlePreferenceTreeClick(mPreference);
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(captor.capture());
final Intent intent = captor.getValue();
assertThat(intent.getComponent()).isEqualTo(
ComponentName.unflattenFromString(CARRIER_SETTINGS_COMPONENT));
}
@Test
public void handlePreferenceClick_activityNotFound_DoNothing() {
final PersistableBundle bundle = new PersistableBundle();
bundle.putString(
CarrierConfigManager.KEY_CARRIER_SETTINGS_ACTIVITY_COMPONENT_NAME_STRING,
CARRIER_SETTINGS_COMPONENT);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
PackageManager pm = Mockito.mock(PackageManager.class);
doReturn(pm).when(mContext).getPackageManager();
doReturn(null).when(pm).resolveActivity(any(Intent.class), anyInt());
mController.handlePreferenceTreeClick(mPreference);
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
verify(mContext, never()).startActivity(captor.capture());
}
@Test
public void handlePreferenceClick_activityNotConfigured_DoNothing() {
final PersistableBundle bundle = new PersistableBundle();
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
PackageManager pm = Mockito.mock(PackageManager.class);
doReturn(pm).when(mContext).getPackageManager();
doReturn(new ResolveInfo()).when(pm).resolveActivity(any(Intent.class), anyInt());
mController.handlePreferenceTreeClick(mPreference);
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
verify(mContext, never()).startActivity(captor.capture());
}
}