Add 'Erase SIM' to mobile network details page

This adds a preference to the mobile network details page that lets a
user delete an eSIM profile.

Bug: 124254555
Test: make RunSettingsRoboTests
Change-Id: I1e266566afc36ff39bf1b1c6d1db674c7c6e8648
This commit is contained in:
Antony Sargent
2019-04-26 10:06:41 -07:00
parent 64693e31d9
commit 562f7b2f91
9 changed files with 615 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
/*
* Copyright (C) 2019 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 org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.telephony.SubscriptionInfo;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.androidx.fragment.FragmentController;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowAlertDialogCompat.class)
public class DeleteSimProfileConfirmationDialogTest {
@Mock
private SubscriptionInfo mSubscriptionInfo;
private DeleteSimProfileConfirmationDialog mDialogFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mDialogFragment = spy(DeleteSimProfileConfirmationDialog.newInstance(mSubscriptionInfo));
doNothing().when(mDialogFragment).beginDeletionWithProgress();
}
@Test
public void showDialog_dialogCancelled_deleteNotCalled() {
FragmentController.setupFragment(mDialogFragment, FragmentActivity.class,
0 /* containerViewId */,
null /* bundle */);
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
verify(mDialogFragment, never()).beginDeletionWithProgress();
}
@Test
public void showDialog_dialogOk_deleteWasCalled() {
FragmentController.setupFragment(mDialogFragment, FragmentActivity.class,
0 /* containerViewId */,
null /* bundle */);
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
verify(mDialogFragment).beginDeletionWithProgress();
}
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright (C) 2019 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.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.telephony.SubscriptionInfo;
import android.telephony.euicc.EuiccManager;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.network.SubscriptionUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
public class DeleteSimProfilePreferenceControllerTest {
private static final String PREF_KEY = "delete_profile_key";
private static final int SUB_ID = 1234;
private static final int OTHER_ID = 5678;
@Mock
private Fragment mFragment;
@Mock
private SubscriptionInfo mSubscriptionInfo;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private Preference mPreference;
private DeleteSimProfilePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSubscriptionInfo));
when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
when(mSubscriptionInfo.isEmbedded()).thenReturn(true);
mPreference = new Preference(mContext);
mPreference.setKey(PREF_KEY);
when(mScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
mController = new DeleteSimProfilePreferenceController(mContext, PREF_KEY);
}
@After
public void tearDown() {
SubscriptionUtil.setAvailableSubscriptionsForTesting(null);
}
@Test
public void getAvailabilityStatus_noSubs_notAvailable() {
SubscriptionUtil.setAvailableSubscriptionsForTesting(new ArrayList<>());
mController.init(SUB_ID, mFragment);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void getAvailabilityStatus_physicalSim_notAvailable() {
when(mSubscriptionInfo.isEmbedded()).thenReturn(false);
mController.init(SUB_ID, mFragment);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void getAvailabilityStatus_unknownSim_notAvailable() {
when(mSubscriptionInfo.getSubscriptionId()).thenReturn(OTHER_ID);
mController.init(SUB_ID, mFragment);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void getAvailabilityStatus_knownEsim_isAvailable() {
mController.init(SUB_ID, mFragment);
assertThat(mController.isAvailable()).isTrue();
}
}

View File

@@ -0,0 +1,139 @@
/*
* Copyright (C) 2019 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.network.telephony.DeleteSimProfileProgressDialog.KEY_DELETE_STARTED;
import static com.android.settings.network.telephony.DeleteSimProfileProgressDialog.PENDING_INTENT;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.euicc.EuiccManager;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
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.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowAlertDialogCompat.class)
public class DeleteSimProfileProgressDialogTest {
private static final int SUB_ID = 111;
@Mock
private FragmentActivity mActivity;
@Mock
private Fragment mTargetFragment;
@Mock
private EuiccManager mEuiccManager;
private Context mContext;
private DeleteSimProfileProgressDialog mDialogFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(EuiccManager.class)).thenReturn(mEuiccManager);
mDialogFragment = spy(DeleteSimProfileProgressDialog.newInstance(SUB_ID));
when(mDialogFragment.getContext()).thenReturn(mContext);
when(mDialogFragment.getTargetFragment()).thenReturn(mTargetFragment);
when(mDialogFragment.getActivity()).thenReturn(mActivity);
}
@Test
public void onCreateDialog_firstShowing_deleteStartedAndRecordedInOutState() {
mDialogFragment.onCreateDialog(null);
verify(mEuiccManager).deleteSubscription(eq(SUB_ID), notNull());
final Bundle outState = new Bundle();
mDialogFragment.onSaveInstanceState(outState);
assertThat(outState.containsKey(KEY_DELETE_STARTED)).isTrue();
assertThat(outState.getBoolean(KEY_DELETE_STARTED)).isTrue();
}
@Test
public void showDialog_secondShowing_deleteNotStarted() {
final Bundle inState = new Bundle();
inState.putBoolean(KEY_DELETE_STARTED, true);
mDialogFragment.onCreateDialog(inState);
verify(mEuiccManager, never()).deleteSubscription(anyInt(), any());
final Bundle outState = new Bundle();
mDialogFragment.onSaveInstanceState(outState);
assertThat(outState.containsKey(KEY_DELETE_STARTED)).isTrue();
assertThat(outState.getBoolean(KEY_DELETE_STARTED)).isTrue();
}
@Test
public void showDialog_pendingIntentReceiverFired_activityFinished() {
mDialogFragment.onCreateDialog(null);
final ArgumentCaptor<PendingIntent> intentCaptor = ArgumentCaptor.forClass(
PendingIntent.class);
verify(mEuiccManager).deleteSubscription(eq(SUB_ID), intentCaptor.capture());
assertThat(intentCaptor.getValue()).isNotNull();
final ArgumentCaptor<BroadcastReceiver> receiverCaptor = ArgumentCaptor.forClass(
BroadcastReceiver.class);
verify(mContext).registerReceiver(receiverCaptor.capture(), any(IntentFilter.class));
doNothing().when(mDialogFragment).dismiss();
receiverCaptor.getValue().onReceive(mContext, new Intent(PENDING_INTENT));
verify(mDialogFragment).dismiss();
verify(mActivity).finish();
}
@Test
public void onDismiss_receiverUnregistered() {
Dialog dialog = mDialogFragment.onCreateDialog(null);
final ArgumentCaptor<BroadcastReceiver> receiverCaptor = ArgumentCaptor.forClass(
BroadcastReceiver.class);
verify(mContext).registerReceiver(receiverCaptor.capture(), any(IntentFilter.class));
mDialogFragment.onDismiss(dialog);
verify(mContext).unregisterReceiver(eq(receiverCaptor.getValue()));
}
}