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