1 package android.platform.helpers 2 3 import android.platform.helpers.CommonUtils.assertScreenOn 4 import android.platform.helpers.Constants.UI_PACKAGE_NAME_SYSUI 5 import android.platform.helpers.LockscreenUtils.LockscreenType 6 import android.platform.helpers.features.common.HomeLockscreenPage 7 import android.platform.uiautomator_helpers.DeviceHelpers.assertVisibility 8 import android.platform.uiautomator_helpers.DeviceHelpers.uiDevice 9 import androidx.test.uiautomator.By 10 import java.util.regex.Pattern 11 12 /** Restarts system ui. */ 13 object SysuiRestarter { 14 15 private val sysuiProcessUtils = ProcessUtil(UI_PACKAGE_NAME_SYSUI) 16 17 private val PAGE_TITLE_SELECTOR_PATTERN = 18 Pattern.compile( 19 String.format( 20 "com.android.systemui:id/(%s|%s)", 21 "lockscreen_clock_view", 22 "lockscreen_clock_view_large" 23 ) 24 ) 25 private val PAGE_TITLE_SELECTOR = By.res(PAGE_TITLE_SELECTOR_PATTERN) 26 27 /** 28 * Restart System UI by running `am crash com.android.systemui`. 29 * 30 * This is sometimes necessary after changing flags, configs, or settings ensure that systemui 31 * is properly initialized with the new changes. This method will wait until the home screen is 32 * visible, then it will optionally dismiss the home screen via swipe. 33 * 34 * @param swipeUp whether to call [HomeLockscreenPage.swipeUp] after restarting System UI 35 */ 36 @JvmStatic restartSystemUInull37 fun restartSystemUI(swipeUp: Boolean) { 38 // This method assumes the screen is on. 39 assertScreenOn("restartSystemUI needs the screen to be on.") 40 // make sure the lock screen is enable. 41 LockscreenUtils.setLockscreen( 42 LockscreenType.SWIPE, 43 /* lockscreenCode= */ null, 44 /* expectedResult= */ false 45 ) 46 sysuiProcessUtils.restart() 47 assertLockscreenVisibility(true) { "Lockscreen not visible after restart" } 48 if (swipeUp) { 49 HomeLockscreenPage().swipeUp() 50 assertLockscreenVisibility(false) { "Lockscreen still visible after swiping up." } 51 } 52 } 53 assertLockscreenVisibilitynull54 private fun assertLockscreenVisibility(visible: Boolean, errorMessageProvider: () -> String) { 55 uiDevice.assertVisibility( 56 PAGE_TITLE_SELECTOR, 57 visible, 58 errorProvider = errorMessageProvider 59 ) 60 } 61 } 62