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(mSubscriptionManager).when(mSubscriptionManager).createForAllUserProfiles(); 72 doReturn(mTelephonyManager).when(mContext).getSystemService(TelephonyManager.class); 73 doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt()); 74 doReturn(mUserManager).when(mContext).getSystemService(UserManager.class); 75 76 doReturn(SIM1_ID).when(mFragment).getDefaultDataSubId(); 77 doReturn(Arrays.asList(mSim1, mSim2)).when(mSubscriptionManager) 78 .getActiveSubscriptionInfoList(); 79 doReturn(true).when(mUserManager) 80 .isManagedProfile(UserHandle.MIN_SECONDARY_USER_ID); 81 82 doReturn(SUMMARY).when(mContext).getString( 83 eq(R.string.enable_auto_data_switch_dialog_message), any()); 84 doReturn(WARNING).when(mContext).getString( 85 R.string.auto_data_switch_dialog_managed_profile_warning); 86 } 87 88 @After tearDown()89 public void tearDown() { 90 mFragment = null; 91 } 92 93 @Test updateDialog_getMessage_noDdsExists()94 public void updateDialog_getMessage_noDdsExists() { 95 doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mFragment).getDefaultDataSubId(); 96 String msg = mFragment.getMessage(); 97 assertThat(msg).isEqualTo(null); 98 } 99 100 @Test updateDialog_getMessage_noBackupSubExists()101 public void updateDialog_getMessage_noBackupSubExists() { 102 doReturn(Collections.singletonList(mSim1)).when(mSubscriptionManager) 103 .getActiveSubscriptionInfoList(); 104 String msg = mFragment.getMessage(); 105 assertThat(msg).isEqualTo(null); 106 } 107 108 @Test updateDialog_getMessage_autoSwitchAlreadyEnabled()109 public void updateDialog_getMessage_autoSwitchAlreadyEnabled() { 110 doReturn(true).when(mTelephonyManager).isMobileDataPolicyEnabled( 111 TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH); 112 String msg = mFragment.getMessage(); 113 assertThat(msg).isEqualTo(null); 114 } 115 116 @Test updateDialog_getMessage_noManagedProfile()117 public void updateDialog_getMessage_noManagedProfile() { 118 UserHandle userHandle = UserHandle.of(UserHandle.USER_NULL); 119 UserHandle userHandle2 = UserHandle.of(UserHandle.USER_SYSTEM); 120 doReturn(userHandle).when(mSubscriptionManager).getSubscriptionUserHandle(SIM1_ID); 121 doReturn(userHandle2).when(mSubscriptionManager).getSubscriptionUserHandle(SIM2_ID); 122 String msg = mFragment.getMessage(); 123 assertThat(msg).contains(SUMMARY); 124 assertThat(msg).doesNotContain(WARNING); 125 } 126 127 @Test updateDialog_getMessage_hasManagedProfile()128 public void updateDialog_getMessage_hasManagedProfile() { 129 UserHandle userHandle = UserHandle.of(UserHandle.USER_NULL); 130 UserHandle userHandle2 = UserHandle.of(UserHandle.MIN_SECONDARY_USER_ID); 131 doReturn(userHandle).when(mSubscriptionManager).getSubscriptionUserHandle(SIM1_ID); 132 doReturn(userHandle2).when(mSubscriptionManager).getSubscriptionUserHandle(SIM2_ID); 133 String msg = mFragment.getMessage(); 134 assertThat(msg).contains(SUMMARY); 135 assertThat(msg).contains(WARNING); 136 } 137 138 @Test updateDialog_getMessage_BothManagedProfile()139 public void updateDialog_getMessage_BothManagedProfile() { 140 UserHandle userHandle = UserHandle.of(UserHandle.MIN_SECONDARY_USER_ID); 141 UserHandle userHandle2 = UserHandle.of(UserHandle.MIN_SECONDARY_USER_ID); 142 doReturn(userHandle).when(mSubscriptionManager).getSubscriptionUserHandle(SIM1_ID); 143 doReturn(userHandle2).when(mSubscriptionManager).getSubscriptionUserHandle(SIM2_ID); 144 String msg = mFragment.getMessage(); 145 assertThat(msg).contains(SUMMARY); 146 assertThat(msg).doesNotContain(WARNING); 147 } 148 } 149