1 /* 2 * Copyright (C) 2018 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 package android.permission.cts; 17 18 import static org.junit.Assert.fail; 19 20 import org.junit.runner.RunWith; 21 import org.junit.Test; 22 23 import android.content.pm.PackageInfo; 24 import android.content.pm.PackageManager; 25 import android.os.UserHandle; 26 import android.support.test.InstrumentationRegistry; 27 import android.support.test.filters.SmallTest; 28 import android.support.test.runner.AndroidJUnit4; 29 30 import java.util.List; 31 32 /** 33 * Build, install and run tests with following command: 34 * atest AppIdleStatePermissionTest 35 */ 36 @SmallTest 37 @RunWith(AndroidJUnit4.class) 38 public class AppIdleStatePermissionTest { 39 40 /** 41 * Verify that the {@link android.Manifest.permission#CHANGE_APP_IDLE_STATE} 42 * permission is only held by at most one package. 43 */ 44 @Test 45 public void testChangeAppIdleStatePermission() throws Exception { 46 final PackageManager pm = InstrumentationRegistry.getContext().getPackageManager(); 47 final List<PackageInfo> holding = pm.getPackagesHoldingPermissions(new String[]{ 48 android.Manifest.permission.CHANGE_APP_IDLE_STATE 49 }, PackageManager.MATCH_SYSTEM_ONLY); 50 51 int count = 0; 52 String pkgNames = ""; 53 for (PackageInfo pkg : holding) { 54 int uid = pm.getApplicationInfo(pkg.packageName, 0).uid; 55 if (UserHandle.isApp(uid)) { 56 pkgNames += pkg.packageName + "\n"; 57 count++; 58 } 59 } 60 if (count > 1) { 61 fail("Only one app may hold the CHANGE_APP_IDLE_STATE permission; found packages: \n" 62 + pkgNames); 63 } 64 } 65 }