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 android.service.persistentdata; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.content.Context; 24 25 import com.android.bedstead.harrier.BedsteadJUnit4; 26 import com.android.bedstead.harrier.DeviceState; 27 import com.android.bedstead.permissions.annotations.EnsureDoesNotHavePermission; 28 import com.android.bedstead.permissions.annotations.EnsureHasPermission; 29 import com.android.bedstead.nene.TestApis; 30 31 import org.junit.ClassRule; 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @RunWith(BedsteadJUnit4.class) 37 public class PersistentDataBlockManagerTest { 38 @ClassRule 39 @Rule 40 public static final DeviceState sDeviceState = new DeviceState(); 41 42 private static final Context sContext = TestApis.context().instrumentedContext(); 43 private static final PersistentDataBlockManager sPersistentDataBlockManager = 44 sContext.getSystemService(PersistentDataBlockManager.class); 45 46 @EnsureHasPermission(android.Manifest.permission.ACCESS_PDB_STATE) 47 @Test getPersistentDataPackageName_returnsNonNullResult()48 public void getPersistentDataPackageName_returnsNonNullResult() { 49 if (sPersistentDataBlockManager == null) { 50 return; 51 } 52 assertThat(sPersistentDataBlockManager.getPersistentDataPackageName()).isNotNull(); 53 } 54 55 @EnsureDoesNotHavePermission(android.Manifest.permission.ACCESS_PDB_STATE) 56 @Test getPersistentDataPackageName_withoutPermission_throwsException()57 public void getPersistentDataPackageName_withoutPermission_throwsException() { 58 if (sPersistentDataBlockManager == null) { 59 return; 60 } 61 assertThrows(SecurityException.class, 62 sPersistentDataBlockManager::getPersistentDataPackageName); 63 } 64 } 65