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.uiautomator_helpers.DeviceHelpers.assertVisibility 23 import android.platform.uiautomator_helpers.DeviceHelpers.uiDevice 24 import android.platform.uiautomator_helpers.DurationUtils.platformAdjust 25 import androidx.test.uiautomator.By 26 import com.android.app.tracing.traceSection 27 import com.android.systemui.Flags.migrateClocksToBlueprint 28 import java.time.Duration 29 import java.util.regex.Pattern 30 31 /** Restarts system ui. */ 32 object SysuiRestarter { 33 34 private val sysuiProcessUtils = ProcessUtil(UI_PACKAGE_NAME_SYSUI) 35 36 // https://hsv.googleplex.com/5130837462876160?node=117 37 private val PAGE_TITLE_SELECTOR_PATTERN = if (migrateClocksToBlueprint()) { 38 Pattern.compile(String.format("com.android.systemui:id/%s", "keyguard_indication_area")) 39 } else { 40 Pattern.compile(String.format("com.android.systemui:id/%s", "keyguard_clock_container")) 41 } 42 private val PAGE_TITLE_SELECTOR = By.res(PAGE_TITLE_SELECTOR_PATTERN) 43 44 /** 45 * Restart System UI by running `am crash com.android.systemui`. 46 * 47 * This is sometimes necessary after changing flags, configs, or settings ensure that systemui 48 * is properly initialized with the new changes. This method will wait until the home screen is 49 * visible, then it will optionally dismiss the home screen via swipe. 50 * 51 * @param swipeUp whether to call [HomeLockscreenPage.swipeUp] after restarting System UI 52 */ 53 @JvmStatic restartSystemUInull54 fun restartSystemUI(swipeUp: Boolean) { 55 traceSection("restartSystemUI") { 56 // This method assumes the screen is on. 57 assertScreenOn("restartSystemUI needs the screen to be on.") 58 // make sure the lock screen is enable. 59 LockscreenUtils.setLockscreen( 60 LockscreenType.SWIPE, 61 /* lockscreenCode= */ null, 62 /* expectedResult= */ false 63 ) 64 sysuiProcessUtils.restart() 65 assertLockscreenVisibility(true) { "Lockscreen not visible after restart" } 66 if (swipeUp) { 67 HomeLockscreenPage().swipeUp() 68 assertLockscreenVisibility(false) { "Lockscreen still visible after swiping up." } 69 } 70 } 71 } 72 assertLockscreenVisibilitynull73 private fun assertLockscreenVisibility(visible: Boolean, errorMessageProvider: () -> String) { 74 uiDevice.assertVisibility( 75 PAGE_TITLE_SELECTOR, 76 visible, 77 timeout = Duration.ofSeconds(10).platformAdjust(), 78 errorProvider = errorMessageProvider 79 ) 80 } 81 } 82