summaryrefslogtreecommitdiff
path: root/tests/tests/hibernation/src/android/hibernation/cts/AppHibernationUtils.kt
blob: a1335370ee85c15e63920fb218d16b76f57a7bb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hibernation.cts

import android.app.Activity
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_TOP_SLEEPING
import android.app.UiAutomation
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Point
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.Process
import android.provider.DeviceConfig
import android.util.Log
import androidx.test.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.BySelector
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject2
import androidx.test.uiautomator.UiScrollable
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.Until
import com.android.compatibility.common.util.ExceptionUtils.wrappingExceptions
import com.android.compatibility.common.util.FeatureUtil
import com.android.compatibility.common.util.SystemUtil.eventually
import com.android.compatibility.common.util.SystemUtil.runShellCommandOrThrow
import com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity
import com.android.compatibility.common.util.ThrowingSupplier
import com.android.compatibility.common.util.UiAutomatorUtils2
import com.android.compatibility.common.util.UiDumpUtils
import com.android.compatibility.common.util.click
import com.android.compatibility.common.util.depthFirstSearch
import com.android.compatibility.common.util.textAsString
import com.android.modules.utils.build.SdkLevel
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import org.hamcrest.Matcher
import org.hamcrest.Matchers
import org.junit.Assert
import org.junit.Assert.assertThat
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeFalse

private const val BROADCAST_TIMEOUT_MS = 60000L

const val PROPERTY_SAFETY_CENTER_ENABLED = "safety_center_is_enabled"
const val HIBERNATION_BOOT_RECEIVER_CLASS_NAME =
    "com.android.permissioncontroller.hibernation.HibernationOnBootReceiver"
const val ACTION_SET_UP_HIBERNATION =
    "com.android.permissioncontroller.action.SET_UP_HIBERNATION"

const val SYSUI_PKG_NAME = "com.android.systemui"
const val NOTIF_LIST_ID = "notification_stack_scroller"
const val NOTIF_LIST_ID_AUTOMOTIVE = "notifications"
const val CLEAR_ALL_BUTTON_ID = "dismiss_text"
const val MANAGE_BUTTON_AUTOMOTIVE = "manage_button"
// Time to find a notification. Unlikely, but in cases with a lot of notifications, it may take
// time to find the notification we're looking for
const val NOTIF_FIND_TIMEOUT = 20000L
const val VIEW_WAIT_TIMEOUT = 3000L
const val JOB_RUN_TIMEOUT = 40000L
const val JOB_RUN_WAIT_TIME = 3000L

const val CMD_EXPAND_NOTIFICATIONS = "cmd statusbar expand-notifications"
const val CMD_COLLAPSE = "cmd statusbar collapse"
const val CMD_CLEAR_NOTIFS = "service call notification 1"

const val APK_PATH_S_APP = "/data/local/tmp/cts/hibernation/CtsAutoRevokeSApp.apk"
const val APK_PACKAGE_NAME_S_APP = "android.hibernation.cts.autorevokesapp"
const val APK_PATH_R_APP = "/data/local/tmp/cts/hibernation/CtsAutoRevokeRApp.apk"
const val APK_PACKAGE_NAME_R_APP = "android.hibernation.cts.autorevokerapp"
const val APK_PATH_Q_APP = "/data/local/tmp/cts/hibernation/CtsAutoRevokeQApp.apk"
const val APK_PACKAGE_NAME_Q_APP = "android.hibernation.cts.autorevokeqapp"

fun runBootCompleteReceiver(context: Context, testTag: String) {
    val pkgManager = context.packageManager
    val permissionControllerPkg = pkgManager.permissionControllerPackageName
    var permissionControllerSetupIntent = Intent(ACTION_SET_UP_HIBERNATION).apply {
        setPackage(permissionControllerPkg)
        setFlags(Intent.FLAG_RECEIVER_FOREGROUND)
    }
    val receivers = pkgManager.queryBroadcastReceivers(
        permissionControllerSetupIntent, /* flags= */ 0)
    if (receivers.size == 0) {
        // May be on an older, pre-built PermissionController. In this case, try sending directly.
        permissionControllerSetupIntent = Intent().apply {
            setPackage(permissionControllerPkg)
            setClassName(permissionControllerPkg, HIBERNATION_BOOT_RECEIVER_CLASS_NAME)
            setFlags(Intent.FLAG_RECEIVER_FOREGROUND)
        }
    }
    val countdownLatch = CountDownLatch(1)
    Log.d(testTag, "Sending boot complete broadcast directly to $permissionControllerPkg")
    context.sendOrderedBroadcast(
        permissionControllerSetupIntent,
        /* receiverPermission= */ null,
        object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                countdownLatch.countDown()
                Log.d(testTag, "Broadcast received by $permissionControllerPkg")
            }
        },
        Handler.createAsync(Looper.getMainLooper()),
        Activity.RESULT_OK,
        /* initialData= */ null,
        /* initialExtras= */ null)
    assertTrue("Timed out while waiting for boot receiver broadcast to be received",
        countdownLatch.await(BROADCAST_TIMEOUT_MS, TimeUnit.MILLISECONDS))
}

fun bypassBatterySavingRestrictions(context: Context) {
    if (SdkLevel.isAtLeastU()) {
        val userId = Process.myUserHandle().identifier
        val permissionControllerPackageName =
            context.packageManager.permissionControllerPackageName
        runShellCommandOrThrow("cmd tare set-vip $userId $permissionControllerPackageName true")
    }
}

fun resetBatterySavingRestrictions(context: Context) {
    if (SdkLevel.isAtLeastU()) {
        val userId = Process.myUserHandle().identifier
        val permissionControllerPackageName =
            context.packageManager.permissionControllerPackageName
        runShellCommandOrThrow("cmd tare set-vip $userId $permissionControllerPackageName default")
    }
}

fun resetJob(context: Context) {
    val userId = Process.myUserHandle().identifier
    val permissionControllerPackageName =
        context.packageManager.permissionControllerPackageName
    runShellCommandOrThrow("cmd jobscheduler reset-execution-quota -u " +
            "$userId $permissionControllerPackageName")
    runShellCommandOrThrow("cmd jobscheduler reset-schedule-quota")
}

fun runAppHibernationJob(context: Context, tag: String) {
    runAppHibernationJobInternal(context, tag)
    if (Build.VERSION.SDK_INT == 31) {
        // On S and S only, run the job twice as a workaround for a deadlock. See b/291147868
        runAppHibernationJobInternal(context, tag)
    }
}

private fun runAppHibernationJobInternal(context: Context, tag: String) {
    val userId = Process.myUserHandle().identifier
    val permissionControllerPackageName =
        context.packageManager.permissionControllerPackageName
    runShellCommandOrThrow("cmd jobscheduler run -u " +
            "$userId -f " +
            "$permissionControllerPackageName 2")
    eventually({
        Thread.sleep(JOB_RUN_WAIT_TIME)
        val jobState = runShellCommandOrThrow("cmd jobscheduler get-job-state -u " +
            "$userId " +
            "$permissionControllerPackageName 2")
        Log.d(tag, "Job output: $jobState")
        assertTrue("Job expected waiting but is $jobState", jobState.contains("waiting"))
    }, JOB_RUN_TIMEOUT)
}

fun runPermissionEventCleanupJob(context: Context) {
    eventually {
        runShellCommandOrThrow("cmd jobscheduler run -u " +
            "${Process.myUserHandle().identifier} -f " +
            "${context.packageManager.permissionControllerPackageName} 3")
    }
}

inline fun withApp(
    apk: String,
    packageName: String,
    action: () -> Unit
) {
    installApk(apk)
    try {
        // Try to reduce flakiness caused by new package update not propagating in time
        Thread.sleep(1000)
        action()
    } finally {
        uninstallApp(packageName)
    }
}

inline fun withAppNoUninstallAssertion(
    apk: String,
    packageName: String,
    action: () -> Unit
) {
    installApk(apk)
    try {
        // Try to reduce flakiness caused by new package update not propagating in time
        Thread.sleep(1000)
        action()
    } finally {
        uninstallAppWithoutAssertion(packageName)
    }
}

inline fun <T> withDeviceConfig(
    namespace: String,
    name: String,
    value: String,
    action: () -> T
): T {
    val oldValue = runWithShellPermissionIdentity(ThrowingSupplier {
        DeviceConfig.getProperty(namespace, name)
    })
    try {
        runWithShellPermissionIdentity {
            DeviceConfig.setProperty(namespace, name, value, false /* makeDefault */)
        }
        return action()
    } finally {
        runWithShellPermissionIdentity {
            DeviceConfig.setProperty(namespace, name, oldValue, false /* makeDefault */)
        }
    }
}

inline fun <T> withUnusedThresholdMs(threshold: Long, action: () -> T): T {
    return withDeviceConfig(
        DeviceConfig.NAMESPACE_PERMISSIONS, "auto_revoke_unused_threshold_millis2",
        threshold.toString(), action)
}

inline fun <T> withSafetyCenterEnabled(action: () -> T): T {
    assumeFalse("This test is only supported on phones",
        hasFeatureWatch() || hasFeatureTV() || hasFeatureAutomotive()
    )

    return withDeviceConfig(
        DeviceConfig.NAMESPACE_PRIVACY, PROPERTY_SAFETY_CENTER_ENABLED,
        true.toString(), action)
}

fun awaitAppState(pkg: String, stateMatcher: Matcher<Int>) {
    val context: Context = InstrumentationRegistry.getTargetContext()
    eventually {
        runWithShellPermissionIdentity {
            val packageImportance = context
                .getSystemService(ActivityManager::class.java)!!
                .getPackageImportance(pkg)
            assertThat(packageImportance, stateMatcher)
        }
    }
}

fun startApp(packageName: String) {
    val context = InstrumentationRegistry.getTargetContext()
    val intent = context.packageManager.getLaunchIntentForPackage(packageName)
    context.startActivity(intent)
    awaitAppState(packageName, Matchers.lessThanOrEqualTo(IMPORTANCE_TOP_SLEEPING))
    waitForIdle()
}

fun goHome() {
    runShellCommandOrThrow("input keyevent KEYCODE_HOME")
    waitForIdle()
}

/**
 * Clear notifications from shade
 */
fun clearNotifications() {
    runWithShellPermissionIdentity {
        runShellCommandOrThrow(CMD_CLEAR_NOTIFS)
    }
}

/**
 * Open the "unused apps" notification which is sent after the hibernation job.
 */
fun openUnusedAppsNotification() {
    val notifSelector = By.textContains("unused app")
    if (hasFeatureWatch()) {
        val uiAutomation = InstrumentationRegistry.getInstrumentation().uiAutomation
        val clickRunnable = object : Runnable {
            override fun run () {
                waitFindObject(uiAutomation, notifSelector).click()
            }
        }
        expandAndClickNotificationWatch(UiAutomatorUtils2.getUiDevice(), clickRunnable)
        // In wear os, notification has one additional button to open it
        waitFindObject(uiAutomation, By.textContains("Open")).click()
    } else {
        val permissionPkg: String = InstrumentationRegistry.getTargetContext()
            .packageManager.permissionControllerPackageName
        eventually({
            // Eventually clause because clicking is sometimes inconsistent if the screen is
            // scrolling
            runShellCommandOrThrow(CMD_EXPAND_NOTIFICATIONS)
            val notification = waitFindNotification(notifSelector, NOTIF_FIND_TIMEOUT)
            if (hasFeatureAutomotive()) {
                notification.click(Point(0, 0))
            } else {
                notification.click()
            }
            wrappingExceptions({ cause: Throwable? -> UiDumpUtils.wrapWithUiDump(cause) }) {
                assertTrue(
                    "Unused apps page did not open after tapping notification.",
                    UiAutomatorUtils2.getUiDevice().wait(
                        Until.hasObject(By.pkg(permissionPkg).depth(0)), VIEW_WAIT_TIMEOUT
                    )
                )
            }
        }, NOTIF_FIND_TIMEOUT)
    }
}

fun hasFeatureWatch(): Boolean {
    return InstrumentationRegistry.getTargetContext().packageManager.hasSystemFeature(
        PackageManager.FEATURE_WATCH)
}

fun hasFeatureTV(): Boolean {
    return InstrumentationRegistry.getTargetContext().packageManager.hasSystemFeature(
            PackageManager.FEATURE_LEANBACK) ||
            InstrumentationRegistry.getTargetContext().packageManager.hasSystemFeature(
                    PackageManager.FEATURE_TELEVISION)
}

fun hasFeatureAutomotive(): Boolean {
    return InstrumentationRegistry.getTargetContext().packageManager.hasSystemFeature(
        PackageManager.FEATURE_AUTOMOTIVE)
}

private fun expandAndClickNotificationWatch(uiDevice: UiDevice, clickRunnable: Runnable) {
    with(uiDevice) {
        wakeUp()
        val x = displayWidth / 2
        // Swipe up from bottom to reveal notifications
        swipe(x, displayHeight, x, 0, 1)
        try {
            clickRunnable.run()
            return
        } catch (e: Exception) {
            // TODO(b/338772456) we catch the exception here since som watches have their
            // notifications tray on the horizontal swipe. Find a way to find a solution that does
            // not require vertical/horizontal swipe retries.
        }
        // Upwards swipe did not find notifications. Undo the upwards swipe, and try sideways.
        swipe(x, 0, x, displayHeight, 5)
        val y = displayHeight / 2
        swipe(0, y, displayWidth, y, 5)

        clickRunnable.run()

    }
}

/**
 * Reset to the top of the notifications list.
 */
private fun resetNotifications(notificationList: UiScrollable) {
    runShellCommandOrThrow(CMD_COLLAPSE)
    notificationList.waitUntilGone(VIEW_WAIT_TIMEOUT)
    runShellCommandOrThrow(CMD_EXPAND_NOTIFICATIONS)
}

private fun waitFindNotification(selector: BySelector, timeoutMs: Long):
    UiObject2 {
    var view: UiObject2? = null
    val start = System.currentTimeMillis()
    val uiDevice = UiAutomatorUtils2.getUiDevice()

    var isAtEnd = false
    var wasScrolledUpAlready = false
    val notificationListId = if (FeatureUtil.isAutomotive()) {
        NOTIF_LIST_ID_AUTOMOTIVE
    } else {
        NOTIF_LIST_ID
    }
    val notificationEndViewId = if (FeatureUtil.isAutomotive()) {
        MANAGE_BUTTON_AUTOMOTIVE
    } else {
        CLEAR_ALL_BUTTON_ID
    }
    while (view == null && start + timeoutMs > System.currentTimeMillis()) {
        view = uiDevice.wait(Until.findObject(selector), VIEW_WAIT_TIMEOUT)
        if (view == null) {
            val notificationList = UiScrollable(UiSelector().resourceId(
                SYSUI_PKG_NAME + ":id/" + notificationListId))
            wrappingExceptions({ cause: Throwable? -> UiDumpUtils.wrapWithUiDump(cause) }) {
                Assert.assertTrue("Notification list view not found",
                    notificationList.waitForExists(VIEW_WAIT_TIMEOUT))
            }
            if (isAtEnd) {
                if (wasScrolledUpAlready) {
                    break
                }
                resetNotifications(notificationList)
                isAtEnd = false
                wasScrolledUpAlready = true
            } else {
                notificationList.scrollForward()
                isAtEnd = uiDevice.hasObject(By.res(SYSUI_PKG_NAME, notificationEndViewId))
            }
        }
    }
    wrappingExceptions({ cause: Throwable? -> UiDumpUtils.wrapWithUiDump(cause) }) {
        Assert.assertNotNull("View not found after waiting for " + timeoutMs + "ms: " + selector,
            view)
    }
    return view!!
}

fun waitFindObject(uiAutomation: UiAutomation, selector: BySelector): UiObject2 {
    try {
        return UiAutomatorUtils2.waitFindObject(selector)
    } catch (e: RuntimeException) {
        val ui = uiAutomation.rootInActiveWindow

        val title = ui.depthFirstSearch { node ->
            node.viewIdResourceName?.contains("alertTitle") == true
        }
        val okCloseButton = ui.depthFirstSearch { node ->
            (node.textAsString?.equals("OK", ignoreCase = true) ?: false) ||
                (node.textAsString?.equals("Close app", ignoreCase = true) ?: false)
        }
        val titleString = title?.text?.toString()
        if (okCloseButton != null &&
            titleString != null &&
            (titleString == "Android System" ||
                titleString.endsWith("keeps stopping"))) {
            // Auto dismiss occasional system dialogs to prevent interfering with the test
            android.util.Log.w(AutoRevokeTest.LOG_TAG, "Ignoring exception", e)
            okCloseButton.click()
            return UiAutomatorUtils2.waitFindObject(selector)
        } else {
            throw e
        }
    }
}