1 /*
2  * Copyright (C) 2024 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.permissioncontroller.tests.mocking.utils
18 
19 import android.app.AppOpsManager
20 import android.content.pm.ApplicationInfo
21 import android.content.pm.PackageInfo
22 import org.mockito.Mockito
23 import org.mockito.Mockito.`when` as whenever
24 
25 object MockUtil {
createMockPackageOpsnull26     fun createMockPackageOps(
27         packageName: String,
28         ops: List<AppOpsManager.OpEntry>,
29         uid: Int
30     ): AppOpsManager.PackageOps {
31         val pkgOp = Mockito.mock(AppOpsManager.PackageOps::class.java)
32         whenever(pkgOp.packageName).thenReturn(packageName)
33         whenever(pkgOp.ops).thenReturn(ops)
34         whenever(pkgOp.uid).thenReturn(uid)
35         return pkgOp
36     }
37 
createOpEntrynull38     fun createOpEntry(opStr: String, time: Long): AppOpsManager.OpEntry {
39         val opEntry = Mockito.mock(AppOpsManager.OpEntry::class.java)
40         whenever(opEntry.opStr).thenReturn(opStr)
41         whenever(opEntry.getLastAccessTime(Mockito.anyInt())).thenReturn(time)
42         return opEntry
43     }
44 
createPackageInfonull45     fun createPackageInfo(
46         testPackageName: String,
47         requestedPerms: List<String>,
48         requestedFlags: List<Int>,
49         applicationInfoFlags: Int = 0
50     ): PackageInfo {
51         val appInfo = ApplicationInfo()
52         appInfo.flags = applicationInfoFlags
53         return PackageInfo().apply {
54             packageName = testPackageName
55             requestedPermissions = requestedPerms.toTypedArray()
56             requestedPermissionsFlags = requestedFlags.toIntArray()
57             applicationInfo = appInfo
58         }
59     }
60 }
61