• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.permissionui
18 
19 import androidx.test.uiautomator.By
20 import com.android.compatibility.common.util.SystemUtil.getEventually
21 import com.android.compatibility.common.util.UiAutomatorUtils2.waitFindObject
22 
23 private const val SUMMARY_TEXT = "apps allowed"
24 
25 /**
26  * Read the {@link UsageCount} of the group of the permission from the Ui.
27  *
28  * @param groupLabel label fo the group the count should be read for
29  *
30  * @return usage counts for the group of the permission
31  */
getUsageCountsFromUinull32 fun getUsageCountsFromUi(groupLabel: CharSequence): UsageCount {
33     waitFindObject(By.text(groupLabel.toString()))
34 
35     return getEventually {
36         val summaryText = waitFindObject(By.hasChild(By.text(groupLabel.toString()))
37             .hasChild(By.textContains(SUMMARY_TEXT))).findObject(By.textContains(SUMMARY_TEXT)).text
38 
39         // Matches two numbers out of the summary line, i.e. "...3...12..." -> "3", "12"
40         val groups = Regex("^[^\\d]*(\\d+)[^\\d]*(\\d+)[^\\d]*\$")
41             .find(summaryText)?.groupValues
42             ?: throw Exception("No usage counts found")
43 
44         UsageCount(groups[1].toInt(), groups[2].toInt())
45     }
46 }
47 
48 /**
49  * Usage counts as read via {@link #getUsageCountsFromUi}.
50  */
51 data class UsageCount(
52     /** Number of apps with permission granted */
53     val granted: Int,
54     /** Number of apps that request permissions */
55     val total: Int
56 )