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.DIALOG_TYPE_KEY; 20 21 import static org.mockito.Mockito.when; 22 23 import android.content.Intent; 24 import android.telephony.SubscriptionInfo; 25 import android.telephony.SubscriptionManager; 26 27 import androidx.appcompat.app.AlertDialog; 28 29 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; 30 31 import org.junit.Before; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.shadows.androidx.fragment.FragmentController; 35 36 public abstract class SimDialogFragmentTestBase<T extends SimDialogFragment> { 37 protected static final int SIM1_ID = 111; 38 protected static final int SIM2_ID = 222; 39 protected static final String SIM1_NAME = "sim111"; 40 protected static final String SIM2_NAME = "sim222"; 41 42 @Mock 43 protected SubscriptionManager mSubscriptionManager; 44 @Mock 45 protected SubscriptionInfo mSim1; 46 @Mock 47 protected SubscriptionInfo mSim2; 48 49 protected T mFragment; 50 protected Intent mIntent; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 mIntent = new Intent(); 56 57 when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(0)).thenReturn(mSim1); 58 when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(1)).thenReturn(mSim2); 59 60 when(mSim1.getSubscriptionId()).thenReturn(SIM1_ID); 61 when(mSim1.getDisplayName()).thenReturn(SIM1_NAME); 62 when(mSim2.getSubscriptionId()).thenReturn(SIM2_ID); 63 when(mSim2.getDisplayName()).thenReturn(SIM2_NAME); 64 } 65 setDialogType(int dialogType)66 protected void setDialogType(int dialogType) { 67 mIntent.putExtra(DIALOG_TYPE_KEY, dialogType); 68 } 69 startDialog()70 protected AlertDialog startDialog() { 71 final FragmentController controller = FragmentController.of(mFragment, 72 SimDialogActivity.class, mIntent); 73 controller.create(0 /* containerViewId */, null /* bundle */).start().visible(); 74 return ShadowAlertDialogCompat.getLatestAlertDialog(); 75 } 76 } 77