Merge "Fix problem of multiple stacked copies of "Select SIM" dialog" into qt-dev am: 01b633275a

am: eb16066b90

Change-Id: I9b459f89192765b8862d8532a0606c8a4a620534
This commit is contained in:
Antony Sargent
2019-04-16 01:44:44 -07:00
committed by android-build-merger
9 changed files with 805 additions and 269 deletions

View File

@@ -0,0 +1,122 @@
/*
* 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.sim;
import static com.android.settings.sim.SimDialogActivity.PREFERRED_PICK;
import static com.android.settings.sim.SimDialogActivity.PREFERRED_SIM;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doNothing;
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 static org.mockito.Mockito.when;
import android.content.DialogInterface;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowAlertDialogCompat.class)
public class PreferredSimDialogFragmentTest extends
SimDialogFragmentTestBase<PreferredSimDialogFragment> {
@Override
public void setUp() {
super.setUp();
setDialogType(PREFERRED_PICK);
mFragment = spy(PreferredSimDialogFragment.newInstance());
doReturn(mSubscriptionManager).when(mFragment).getSubscriptionManager();
}
@Test
public void onCreateDialog_noSims_dismissed() {
when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(anyInt()))
.thenReturn(null);
mIntent.putExtra(PREFERRED_SIM, 0);
startDialog();
verify(mFragment).dismiss();
}
@Test
public void onCreateDialog_oneSimWrongSlotArgument_dismissed() {
when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(1)).thenReturn(null);
mIntent.putExtra(PREFERRED_SIM, 1);
startDialog();
verify(mFragment).dismiss();
}
@Test
public void onCreateDialog_twoSimsSelectFirst_correctMessage() {
mIntent.putExtra(PREFERRED_SIM, 0);
final AlertDialog alertDialog = startDialog();
final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(alertDialog);
final String message = (String) shadowDialog.getMessage();
assertThat(message).contains(SIM1_NAME);
assertThat(message).doesNotContain(SIM2_NAME);
}
@Test
public void onCreateDialog_twoSimsSelectSecond_correctMessage() {
mIntent.putExtra(PREFERRED_SIM, 1);
final AlertDialog alertDialog = startDialog();
final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(alertDialog);
final String message = (String) shadowDialog.getMessage();
assertThat(message).contains(SIM2_NAME);
assertThat(message).doesNotContain(SIM1_NAME);
}
@Test
public void onClick_yesClicked_callsOnSubscriptionSelected() {
mIntent.putExtra(PREFERRED_SIM, 0);
final AlertDialog alertDialog = startDialog();
final SimDialogActivity activity = (SimDialogActivity) spy(mFragment.getActivity());
doReturn(activity).when(mFragment).getActivity();
doNothing().when(activity).onSubscriptionSelected(anyInt(), anyInt());
mFragment.onClick(alertDialog, DialogInterface.BUTTON_POSITIVE);
verify(activity).onSubscriptionSelected(PREFERRED_PICK, SIM1_ID);
}
@Test
public void onClick_noClicked_doesNotCallOnSubscriptionSelected() {
mIntent.putExtra(PREFERRED_SIM, 0);
final AlertDialog alertDialog = startDialog();
final SimDialogActivity activity = (SimDialogActivity) spy(mFragment.getActivity());
doReturn(activity).when(mFragment).getActivity();
doNothing().when(activity).onSubscriptionSelected(anyInt(), anyInt());
mFragment.onClick(alertDialog, DialogInterface.BUTTON_NEGATIVE);
verify(activity, never()).onSubscriptionSelected(anyInt(), anyInt());
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.sim;
import static com.android.settings.sim.SimDialogActivity.DIALOG_TYPE_KEY;
import static org.mockito.Mockito.when;
import android.content.Intent;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import org.junit.Before;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.shadows.androidx.fragment.FragmentController;
public abstract class SimDialogFragmentTestBase<T extends SimDialogFragment> {
protected static final int SIM1_ID = 111;
protected static final int SIM2_ID = 222;
protected static final String SIM1_NAME = "sim111";
protected static final String SIM2_NAME = "sim222";
@Mock
protected SubscriptionManager mSubscriptionManager;
@Mock
protected SubscriptionInfo mSim1;
@Mock
protected SubscriptionInfo mSim2;
protected T mFragment;
protected Intent mIntent;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mIntent = new Intent();
when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(0)).thenReturn(mSim1);
when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(1)).thenReturn(mSim2);
when(mSim1.getSubscriptionId()).thenReturn(SIM1_ID);
when(mSim1.getDisplayName()).thenReturn(SIM1_NAME);
when(mSim2.getSubscriptionId()).thenReturn(SIM2_ID);
when(mSim2.getDisplayName()).thenReturn(SIM2_NAME);
}
protected void setDialogType(int dialogType) {
mIntent.putExtra(DIALOG_TYPE_KEY, dialogType);
}
protected AlertDialog startDialog() {
final FragmentController controller = FragmentController.of(mFragment,
SimDialogActivity.class, mIntent);
controller.create(0 /* containerViewId */, null /* bundle */).start().visible();
return ShadowAlertDialogCompat.getLatestAlertDialog();
}
}

View File

@@ -0,0 +1,101 @@
/*
* 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.sim;
import static com.android.settings.sim.SimDialogActivity.DATA_PICK;
import static com.android.settings.sim.SimDialogActivity.SMS_PICK;
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.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.telephony.SubscriptionManager;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.R;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowAlertDialogCompat.class)
public class SimListDialogFragmentTest extends SimDialogFragmentTestBase<SimListDialogFragment> {
@Test
public void onCreateDialog_noSubscriptions_dismissed() {
final int dialogType = DATA_PICK;
setDialogType(dialogType);
mFragment = spy(SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_data,
false /* includeAskEveryTime */));
doReturn(null).when(mFragment).getCurrentSubscriptions();
startDialog();
verify(mFragment).dismiss();
}
@Test
public void onCreateDialog_twoSubscriptionsNoAskEveryTime_twoSubsForDisplay() {
final int dialogType = DATA_PICK;
setDialogType(dialogType);
mFragment = spy(SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_data,
false /* includeAskEveryTime */));
doReturn(Arrays.asList(mSim1, mSim2)).when(mFragment).getCurrentSubscriptions();
// Avoid problems robolectric has with our real adapter.
doNothing().when(mFragment).setAdapter(any());
final AlertDialog alertDialog = startDialog();
assertThat(mFragment.mSubscriptions).hasSize(2);
final SimDialogActivity activity = (SimDialogActivity) spy(mFragment.getActivity());
doReturn(activity).when(mFragment).getActivity();
doNothing().when(activity).onSubscriptionSelected(anyInt(), anyInt());
mFragment.onClick(alertDialog, 1);
verify(activity).onSubscriptionSelected(dialogType, SIM2_ID);
}
@Test
public void onCreateDialog_twoSubscriptionsAskEveryTime_threeSubsForDisplay() {
final int dialogType = SMS_PICK;
setDialogType(dialogType);
mFragment = spy(SimListDialogFragment.newInstance(dialogType, R.string.select_sim_for_sms,
true /* includeAskEveryTime */));
doReturn(Arrays.asList(mSim1, mSim2)).when(mFragment).getCurrentSubscriptions();
// Avoid problems robolectric has with our real adapter.
doNothing().when(mFragment).setAdapter(any());
final AlertDialog alertDialog = startDialog();
assertThat(mFragment.mSubscriptions).hasSize(3);
assertThat(mFragment.mSubscriptions.get(0)).isNull();
final SimDialogActivity activity = (SimDialogActivity) spy(mFragment.getActivity());
doReturn(activity).when(mFragment).getActivity();
doNothing().when(activity).onSubscriptionSelected(anyInt(), anyInt());
mFragment.onClick(alertDialog, 0);
verify(activity).onSubscriptionSelected(dialogType,
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
}
}