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; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.app.admin.DevicePolicyManager; 26 import android.app.admin.FactoryResetProtectionPolicy; 27 import android.content.Context; 28 import android.platform.test.annotations.DisableFlags; 29 import android.platform.test.annotations.EnableFlags; 30 import android.platform.test.flag.junit.SetFlagsRule; 31 import android.security.Flags; 32 import android.service.persistentdata.PersistentDataBlockManager; 33 import android.view.LayoutInflater; 34 35 import androidx.fragment.app.FragmentActivity; 36 37 import com.google.android.setupdesign.GlifLayout; 38 39 import org.junit.Before; 40 import org.junit.Rule; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.Robolectric; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.annotation.Config; 48 49 import java.util.ArrayList; 50 51 @RunWith(RobolectricTestRunner.class) 52 @Config(shadows = { 53 com.android.settings.testutils.shadow.ShadowFragment.class, 54 }) 55 public class MainClearConfirmTest { 56 57 @Rule 58 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 59 60 private FragmentActivity mActivity; 61 62 @Mock 63 private FragmentActivity mMockActivity; 64 65 @Mock 66 private DevicePolicyManager mDevicePolicyManager; 67 68 @Mock 69 private PersistentDataBlockManager mPersistentDataBlockManager; 70 71 private MainClearConfirm mMainClearConfirm; 72 73 @Before setUp()74 public void setUp() { 75 MockitoAnnotations.initMocks(this); 76 mActivity = Robolectric.setupActivity(FragmentActivity.class); 77 mMainClearConfirm = spy(new MainClearConfirm()); 78 79 when(mMockActivity.getSystemService(Context.DEVICE_POLICY_SERVICE)) 80 .thenReturn(mDevicePolicyManager); 81 when(mPersistentDataBlockManager.isFactoryResetProtectionActive()).thenReturn(false); 82 } 83 84 @Test setSubtitle_eraseEsim()85 public void setSubtitle_eraseEsim() { 86 MainClearConfirm mainClearConfirm = new MainClearConfirm(); 87 mainClearConfirm.mEraseEsims = true; 88 mainClearConfirm.mContentView = 89 (GlifLayout) LayoutInflater.from(mActivity) 90 .inflate(R.layout.main_clear_confirm, null); 91 92 mainClearConfirm.setSubtitle(); 93 94 assertThat(mainClearConfirm.mContentView.getDescriptionText()) 95 .isEqualTo(mActivity.getString(R.string.main_clear_final_desc_esim)); 96 } 97 98 @Test setSubtitle_notEraseEsim()99 public void setSubtitle_notEraseEsim() { 100 MainClearConfirm mainClearConfirm = new MainClearConfirm(); 101 mainClearConfirm.mEraseEsims = false; 102 mainClearConfirm.mContentView = 103 (GlifLayout) LayoutInflater.from(mActivity) 104 .inflate(R.layout.main_clear_confirm, null); 105 106 mainClearConfirm.setSubtitle(); 107 108 assertThat(mainClearConfirm.mContentView.getDescriptionText()) 109 .isEqualTo(mActivity.getString(R.string.main_clear_final_desc)); 110 } 111 112 @Test shouldWipePersistentDataBlock_noPersistentDataBlockManager_shouldReturnFalse()113 public void shouldWipePersistentDataBlock_noPersistentDataBlockManager_shouldReturnFalse() { 114 assertThat(mMainClearConfirm.shouldWipePersistentDataBlock(null)).isFalse(); 115 } 116 117 @Test shouldWipePersistentDataBlock_frpIsAlive_shouldReturnFalse()118 public void shouldWipePersistentDataBlock_frpIsAlive_shouldReturnFalse() { 119 when(mPersistentDataBlockManager.isFactoryResetProtectionActive()).thenReturn(true); 120 assertThat(mMainClearConfirm.shouldWipePersistentDataBlock(mPersistentDataBlockManager)) 121 .isFalse(); 122 } 123 124 @Test shouldWipePersistentDataBlock_deviceIsStillBeingProvisioned_shouldReturnFalse()125 public void shouldWipePersistentDataBlock_deviceIsStillBeingProvisioned_shouldReturnFalse() { 126 doReturn(true).when(mMainClearConfirm).isDeviceStillBeingProvisioned(); 127 128 assertThat(mMainClearConfirm.shouldWipePersistentDataBlock( 129 mPersistentDataBlockManager)).isFalse(); 130 } 131 132 @Test 133 @DisableFlags(Flags.FLAG_FRP_ENFORCEMENT) shouldWipePersistentDataBlock_oemUnlockAllowedAndFlagDiscabled_shouldReturnFalse()134 public void shouldWipePersistentDataBlock_oemUnlockAllowedAndFlagDiscabled_shouldReturnFalse() { 135 when(mMainClearConfirm.getActivity()).thenReturn(mMockActivity); 136 137 when(mDevicePolicyManager.isFactoryResetProtectionPolicySupported()).thenReturn(true); 138 doReturn(false).when(mMainClearConfirm).isDeviceStillBeingProvisioned(); 139 doReturn(true).when(mMainClearConfirm).isOemUnlockedAllowed(); 140 141 assertThat(mMainClearConfirm.shouldWipePersistentDataBlock(mPersistentDataBlockManager)) 142 .isFalse(); 143 } 144 145 @Test 146 @EnableFlags(Flags.FLAG_FRP_ENFORCEMENT) shouldWipePersistentDataBlock_oemUnlockAllowedAndFlagEnabled_shouldReturnTrue()147 public void shouldWipePersistentDataBlock_oemUnlockAllowedAndFlagEnabled_shouldReturnTrue() { 148 when(mMainClearConfirm.getActivity()).thenReturn(mMockActivity); 149 150 when(mDevicePolicyManager.isFactoryResetProtectionPolicySupported()).thenReturn(true); 151 doReturn(false).when(mMainClearConfirm).isDeviceStillBeingProvisioned(); 152 doReturn(true).when(mMainClearConfirm).isOemUnlockedAllowed(); 153 154 assertThat(mMainClearConfirm.shouldWipePersistentDataBlock(mPersistentDataBlockManager)) 155 .isTrue(); 156 } 157 158 @Test shouldWipePersistentDataBlock_frpPolicyNotSupported_shouldReturnFalse()159 public void shouldWipePersistentDataBlock_frpPolicyNotSupported_shouldReturnFalse() { 160 when(mMainClearConfirm.getActivity()).thenReturn(mMockActivity); 161 162 doReturn(false).when(mMainClearConfirm).isDeviceStillBeingProvisioned(); 163 doReturn(false).when(mMainClearConfirm).isOemUnlockedAllowed(); 164 165 when(mDevicePolicyManager.isFactoryResetProtectionPolicySupported()).thenReturn(false); 166 167 assertThat(mMainClearConfirm.shouldWipePersistentDataBlock( 168 mPersistentDataBlockManager)).isFalse(); 169 } 170 171 @Test shouldWipePersistentDataBlock_hasFactoryResetProtectionPolicy_shouldReturnFalse()172 public void shouldWipePersistentDataBlock_hasFactoryResetProtectionPolicy_shouldReturnFalse() { 173 when(mMainClearConfirm.getActivity()).thenReturn(mMockActivity); 174 175 doReturn(false).when(mMainClearConfirm).isDeviceStillBeingProvisioned(); 176 doReturn(false).when(mMainClearConfirm).isOemUnlockedAllowed(); 177 ArrayList<String> accounts = new ArrayList<>(); 178 accounts.add("test"); 179 FactoryResetProtectionPolicy frp = new FactoryResetProtectionPolicy.Builder() 180 .setFactoryResetProtectionAccounts(accounts) 181 .setFactoryResetProtectionEnabled(true) 182 .build(); 183 when(mDevicePolicyManager.isFactoryResetProtectionPolicySupported()).thenReturn(true); 184 when(mDevicePolicyManager.getFactoryResetProtectionPolicy(null)).thenReturn(frp); 185 when(mDevicePolicyManager.isOrganizationOwnedDeviceWithManagedProfile()).thenReturn(true); 186 187 assertThat(mMainClearConfirm.shouldWipePersistentDataBlock( 188 mPersistentDataBlockManager)).isFalse(); 189 } 190 191 @Test shouldWipePersistentDataBlock_isNotOrganizationOwnedDevice_shouldReturnTrue()192 public void shouldWipePersistentDataBlock_isNotOrganizationOwnedDevice_shouldReturnTrue() { 193 when(mMainClearConfirm.getActivity()).thenReturn(mMockActivity); 194 195 doReturn(false).when(mMainClearConfirm).isDeviceStillBeingProvisioned(); 196 doReturn(false).when(mMainClearConfirm).isOemUnlockedAllowed(); 197 198 when(mDevicePolicyManager.isFactoryResetProtectionPolicySupported()).thenReturn(true); 199 when(mDevicePolicyManager.getFactoryResetProtectionPolicy(null)).thenReturn(null); 200 when(mDevicePolicyManager.isOrganizationOwnedDeviceWithManagedProfile()).thenReturn(false); 201 202 assertThat(mMainClearConfirm.shouldWipePersistentDataBlock( 203 mPersistentDataBlockManager)).isTrue(); 204 } 205 } 206