1 /* 2 * Copyright (C) 2022 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.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.eq; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.spy; 26 27 import android.content.Context; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 import android.telephony.SubscriptionManager; 31 import android.telephony.TelephonyManager; 32 33 import com.android.settings.R; 34 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.annotation.Config; 43 44 import java.util.Arrays; 45 import java.util.Collections; 46 47 @RunWith(RobolectricTestRunner.class) 48 @Config(shadows = ShadowAlertDialogCompat.class) 49 public class EnableAutoDataSwitchDialogFragmentTest 50 extends SimDialogFragmentTestBase<EnableAutoDataSwitchDialogFragment> { 51 private static final String SUMMARY = "fake summary"; 52 private static final String WARNING = "fake warning"; 53 54 // Mock 55 @Mock 56 private Context mContext; 57 @Mock 58 private SubscriptionManager mSubscriptionManager; 59 @Mock 60 private TelephonyManager mTelephonyManager; 61 @Mock 62 private UserManager mUserManager; 63 64 @Before setUp()65 public void setUp() { 66 super.setUp(); 67 mFragment = spy(EnableAutoDataSwitchDialogFragment.newInstance()); 68 doReturn(mContext).when(mFragment).getContext(); 69 70 doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class); 71 doReturn(mTelephonyManager).when(mContext).getSystemService(TelephonyManager.class); 72 doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt()); 73 doReturn(mUserManager).when(mContext).getSystemService(UserManager.class); 74 75 doReturn(SIM1_ID).when(mFragment).getDefaultDataSubId(); 76 doReturn(Arrays.asList(mSim1, mSim2)).when(mSubscriptionManager) 77 .getActiveSubscriptionInfoList(); 78 doReturn(true).when(mUserManager) 79 .isManagedProfile(UserHandle.MIN_SECONDARY_USER_ID); 80 81 doReturn(SUMMARY).when(mContext).getString( 82 eq(R.string.enable_auto_data_switch_dialog_message), any()); 83 doReturn(WARNING).when(mContext).getString( 84 R.string.auto_data_switch_dialog_managed_profile_warning); 85 } 86 87 @After tearDown()88 public void tearDown() { 89 mFragment = null; 90 } 91 92 @Test updateDialog_getMessage_noDdsExists()93 public void updateDialog_getMessage_noDdsExists() { 94 doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mFragment).getDefaultDataSubId(); 95 String msg = mFragment.getMessage(); 96 assertThat(msg).isEqualTo(null); 97 } 98 99 @Test updateDialog_getMessage_noBackupSubExists()100 public void updateDialog_getMessage_noBackupSubExists() { 101 doReturn(Collections.singletonList(mSim1)).when(mSubscriptionManager) 102 .getActiveSubscriptionInfoList(); 103 String msg = mFragment.getMessage(); 104 assertThat(msg).isEqualTo(null); 105 } 106 107 @Test updateDialog_getMessage_autoSwitchAlreadyEnabled()108 public void updateDialog_getMessage_autoSwitchAlreadyEnabled() { 109 doReturn(true).when(mTelephonyManager).isMobileDataPolicyEnabled( 110 TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH); 111 String msg = mFragment.getMessage(); 112 assertThat(msg).isEqualTo(null); 113 } 114 115 @Test updateDialog_getMessage_noManagedProfile()116 public void updateDialog_getMessage_noManagedProfile() { 117 UserHandle userHandle = UserHandle.of(UserHandle.USER_NULL); 118 UserHandle userHandle2 = UserHandle.of(UserHandle.USER_SYSTEM); 119 doReturn(userHandle).when(mSubscriptionManager).getSubscriptionUserHandle(SIM1_ID); 120 doReturn(userHandle2).when(mSubscriptionManager).getSubscriptionUserHandle(SIM2_ID); 121 String msg = mFragment.getMessage(); 122 assertThat(msg).contains(SUMMARY); 123 assertThat(msg).doesNotContain(WARNING); 124 } 125 126 @Test updateDialog_getMessage_hasManagedProfile()127 public void updateDialog_getMessage_hasManagedProfile() { 128 UserHandle userHandle = UserHandle.of(UserHandle.USER_NULL); 129 UserHandle userHandle2 = UserHandle.of(UserHandle.MIN_SECONDARY_USER_ID); 130 doReturn(userHandle).when(mSubscriptionManager).getSubscriptionUserHandle(SIM1_ID); 131 doReturn(userHandle2).when(mSubscriptionManager).getSubscriptionUserHandle(SIM2_ID); 132 String msg = mFragment.getMessage(); 133 assertThat(msg).contains(SUMMARY); 134 assertThat(msg).contains(WARNING); 135 } 136 137 @Test updateDialog_getMessage_BothManagedProfile()138 public void updateDialog_getMessage_BothManagedProfile() { 139 UserHandle userHandle = UserHandle.of(UserHandle.MIN_SECONDARY_USER_ID); 140 UserHandle userHandle2 = UserHandle.of(UserHandle.MIN_SECONDARY_USER_ID); 141 doReturn(userHandle).when(mSubscriptionManager).getSubscriptionUserHandle(SIM1_ID); 142 doReturn(userHandle2).when(mSubscriptionManager).getSubscriptionUserHandle(SIM2_ID); 143 String msg = mFragment.getMessage(); 144 assertThat(msg).contains(SUMMARY); 145 assertThat(msg).doesNotContain(WARNING); 146 } 147 } 148