• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.platform.helpers
17 
18 import android.platform.helpers.CommonUtils.assertScreenOn
19 import android.platform.helpers.Constants.UI_PACKAGE_NAME_SYSUI
20 import android.platform.helpers.LockscreenUtils.LockscreenType
21 import android.platform.helpers.features.common.HomeLockscreenPage
22 import android.platform.uiautomatorhelpers.DeviceHelpers.assertVisibility
23 import android.platform.uiautomatorhelpers.DeviceHelpers.uiDevice
24 import android.platform.uiautomatorhelpers.DurationUtils.platformAdjust
25 import androidx.test.uiautomator.By
26 import com.android.app.tracing.traceSection
27 import com.android.systemui.Flags
28 import java.time.Duration
29 
30 /** Restarts system ui. */
31 object SysuiRestarter {
32 
33     private val sysuiProcessUtils = ProcessUtil(UI_PACKAGE_NAME_SYSUI)
34 
35     val LOCKSCREEN_SELECTOR =
36         if (Flags.sceneContainer()) {
37             By.res("element:lockscreen")
38         } else {
39             By.res("com.android.systemui", "keyguard_indication_area")
40         }
41 
42     /**
43      * Restart System UI by running `am crash com.android.systemui`.
44      *
45      * This is sometimes necessary after changing flags, configs, or settings ensure that systemui
46      * is properly initialized with the new changes. This method will wait until the home screen is
47      * visible, then it will optionally dismiss the home screen via swipe.
48      *
49      * @param swipeUp whether to call [HomeLockscreenPage.swipeUp] after restarting System UI
50      */
51     @JvmStatic
restartSystemUInull52     fun restartSystemUI(swipeUp: Boolean) {
53         traceSection("restartSystemUI") {
54             // This method assumes the screen is on.
55             assertScreenOn("restartSystemUI needs the screen to be on.")
56             // make sure the lock screen is enable.
57             LockscreenUtils.setLockscreen(
58                 LockscreenType.SWIPE,
59                 /* lockscreenCode= */ null,
60                 /* expectedResult= */ false,
61             )
62             sysuiProcessUtils.restart()
63             assertLockscreenVisibility(true) { "Lockscreen not visible after restart" }
64             if (swipeUp) {
65                 HomeLockscreenPage().swipeUp()
66                 assertLockscreenVisibility(false) { "Lockscreen still visible after swiping up." }
67             }
68         }
69     }
70 
assertLockscreenVisibilitynull71     private fun assertLockscreenVisibility(visible: Boolean, errorMessageProvider: () -> String) {
72         uiDevice.assertVisibility(
73             LOCKSCREEN_SELECTOR,
74             visible,
75             timeout = Duration.ofSeconds(10).platformAdjust(),
76             errorProvider = errorMessageProvider,
77         )
78     }
79 }
80