1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.settings.sim; 18 19 import static com.android.settings.sim.SimDialogActivity.PREFERRED_PICK; 20 import static com.android.settings.sim.SimDialogActivity.PREFERRED_SIM; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.Mockito.doNothing; 26 import static org.mockito.Mockito.doReturn; 27 import static org.mockito.Mockito.never; 28 import static org.mockito.Mockito.spy; 29 import static org.mockito.Mockito.verify; 30 import static org.mockito.Mockito.when; 31 32 import android.content.DialogInterface; 33 34 import androidx.appcompat.app.AlertDialog; 35 36 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; 37 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.annotation.Config; 42 43 @RunWith(RobolectricTestRunner.class) 44 @Config(shadows = ShadowAlertDialogCompat.class) 45 public class PreferredSimDialogFragmentTest extends 46 SimDialogFragmentTestBase<PreferredSimDialogFragment> { 47 48 @Override setUp()49 public void setUp() { 50 super.setUp(); 51 setDialogType(PREFERRED_PICK); 52 mFragment = spy(PreferredSimDialogFragment.newInstance()); 53 doReturn(mSubscriptionManager).when(mFragment).getSubscriptionManager(); 54 } 55 56 @Test onCreateDialog_noSims_dismissed()57 public void onCreateDialog_noSims_dismissed() { 58 when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(anyInt())) 59 .thenReturn(null); 60 mIntent.putExtra(PREFERRED_SIM, 0); 61 startDialog(); 62 verify(mFragment).dismiss(); 63 } 64 65 @Test onCreateDialog_oneSimWrongSlotArgument_dismissed()66 public void onCreateDialog_oneSimWrongSlotArgument_dismissed() { 67 when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(1)).thenReturn(null); 68 mIntent.putExtra(PREFERRED_SIM, 1); 69 startDialog(); 70 verify(mFragment).dismiss(); 71 } 72 73 @Test onCreateDialog_twoSimsSelectFirst_correctMessage()74 public void onCreateDialog_twoSimsSelectFirst_correctMessage() { 75 mIntent.putExtra(PREFERRED_SIM, 0); 76 77 final AlertDialog alertDialog = startDialog(); 78 final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(alertDialog); 79 final String message = (String) shadowDialog.getMessage(); 80 assertThat(message).contains(SIM1_NAME); 81 assertThat(message).doesNotContain(SIM2_NAME); 82 } 83 84 @Test onCreateDialog_twoSimsSelectSecond_correctMessage()85 public void onCreateDialog_twoSimsSelectSecond_correctMessage() { 86 mIntent.putExtra(PREFERRED_SIM, 1); 87 88 final AlertDialog alertDialog = startDialog(); 89 final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(alertDialog); 90 final String message = (String) shadowDialog.getMessage(); 91 assertThat(message).contains(SIM2_NAME); 92 assertThat(message).doesNotContain(SIM1_NAME); 93 } 94 95 @Test onClick_yesClicked_callsOnSubscriptionSelected()96 public void onClick_yesClicked_callsOnSubscriptionSelected() { 97 mIntent.putExtra(PREFERRED_SIM, 0); 98 99 final AlertDialog alertDialog = startDialog(); 100 101 final SimDialogActivity activity = (SimDialogActivity) spy(mFragment.getActivity()); 102 doReturn(activity).when(mFragment).getActivity(); 103 doNothing().when(activity).onSubscriptionSelected(anyInt(), anyInt()); 104 105 mFragment.onClick(alertDialog, DialogInterface.BUTTON_POSITIVE); 106 verify(activity).onSubscriptionSelected(PREFERRED_PICK, SIM1_ID); 107 } 108 109 @Test onClick_noClicked_doesNotCallOnSubscriptionSelected()110 public void onClick_noClicked_doesNotCallOnSubscriptionSelected() { 111 mIntent.putExtra(PREFERRED_SIM, 0); 112 113 final AlertDialog alertDialog = startDialog(); 114 115 final SimDialogActivity activity = (SimDialogActivity) spy(mFragment.getActivity()); 116 doReturn(activity).when(mFragment).getActivity(); 117 doNothing().when(activity).onSubscriptionSelected(anyInt(), anyInt()); 118 119 mFragment.onClick(alertDialog, DialogInterface.BUTTON_NEGATIVE); 120 verify(activity, never()).onSubscriptionSelected(anyInt(), anyInt()); 121 } 122 } 123