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