1 /* 2 * Copyright (C) 2022 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 android.server.wm; 18 19 import static android.provider.Settings.Global.STAY_ON_WHILE_PLUGGED_IN; 20 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; 21 import static android.server.wm.app.Components.TEST_ACTIVITY; 22 import static android.server.wm.app.Components.TURN_SCREEN_ON_ACTIVITY; 23 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assert.assertTrue; 26 import static org.junit.Assume.assumeFalse; 27 import static org.junit.Assume.assumeTrue; 28 29 import android.content.ContentResolver; 30 import android.content.Intent; 31 import android.content.pm.PackageManager; 32 import android.content.res.Resources; 33 import android.os.PowerManager; 34 import android.os.SystemClock; 35 import android.provider.Settings; 36 37 import com.android.compatibility.common.util.ApiTest; 38 import com.android.compatibility.common.util.BlockingBroadcastReceiver; 39 40 import org.junit.After; 41 import org.junit.Before; 42 import org.junit.Test; 43 44 public class KeepScreenOnTests extends MultiDisplayTestBase { 45 private static final String TAG = "KeepScreenOnTests"; 46 private String mInitialDisplayTimeout; 47 private int mInitialStayOnWhilePluggedInSetting; 48 private PowerManager mPowerManager; 49 private ContentResolver mContentResolver; 50 private boolean mIsTv; 51 52 @Before setUp()53 public void setUp() throws Exception { 54 super.setUp(); 55 mIsTv = mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK); 56 mContentResolver = mContext.getContentResolver(); 57 mInitialDisplayTimeout = 58 Settings.System.getString(mContentResolver, SCREEN_OFF_TIMEOUT); 59 mInitialStayOnWhilePluggedInSetting = 60 Settings.Global.getInt(mContentResolver, STAY_ON_WHILE_PLUGGED_IN); 61 Settings.Global.putInt(mContentResolver, STAY_ON_WHILE_PLUGGED_IN, 0); 62 mPowerManager = mContext.getSystemService(PowerManager.class); 63 assumeFalse("Automotive main display is always on - skipping test", 64 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)); 65 } 66 67 @After tearDown()68 public void tearDown() { 69 setScreenOffTimeoutMs(mInitialDisplayTimeout); 70 Settings.Global.putInt(mContentResolver, STAY_ON_WHILE_PLUGGED_IN, 71 mInitialStayOnWhilePluggedInSetting); 72 wakeUpAndUnlock(mContext); 73 } 74 75 @ApiTest(apis = "android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON") 76 @Test testKeepScreenOn_activityOnDefaultDisplay_screenStaysOn()77 public void testKeepScreenOn_activityOnDefaultDisplay_screenStaysOn() { 78 setScreenOffTimeoutMs("500"); 79 launchActivity(TURN_SCREEN_ON_ACTIVITY); 80 assertTrue(mPowerManager.isInteractive()); 81 82 SystemClock.sleep(getMinimumScreenOffTimeoutMs()); 83 84 assertTrue(mPowerManager.isInteractive()); 85 mWmState.assertVisibility(TURN_SCREEN_ON_ACTIVITY, true); 86 } 87 88 @ApiTest(apis = "android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON") 89 @Test testKeepScreenOn_activityNotForeground_screenTurnsOff()90 public void testKeepScreenOn_activityNotForeground_screenTurnsOff() { 91 assumeFalse("TVs may start screen saver instead of turning screen off - skipping test", 92 mIsTv); 93 setScreenOffTimeoutMs("500"); 94 95 launchActivity(TURN_SCREEN_ON_ACTIVITY); 96 assertTrue(mPowerManager.isInteractive()); 97 try (BlockingBroadcastReceiver r = BlockingBroadcastReceiver.create(mContext, 98 Intent.ACTION_SCREEN_OFF).register()) { 99 launchActivity(TEST_ACTIVITY); 100 } 101 mWmState.waitAndAssertVisibilityGone(TURN_SCREEN_ON_ACTIVITY); 102 assertFalse(mPowerManager.isInteractive()); 103 } 104 105 @ApiTest(apis = "android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON") 106 @Test testKeepScreenOn_activityOnVirtualDisplay_screenStaysOn()107 public void testKeepScreenOn_activityOnVirtualDisplay_screenStaysOn() { 108 assumeTrue(supportsMultiDisplay()); 109 setScreenOffTimeoutMs("500"); 110 111 final WindowManagerState.DisplayContent newDisplay = createManagedVirtualDisplaySession() 112 .setSimulateDisplay(true).createDisplay(); 113 launchActivityOnDisplay(TURN_SCREEN_ON_ACTIVITY, newDisplay.mId); 114 mWmState.assertVisibility(TURN_SCREEN_ON_ACTIVITY, true); 115 assertTrue(mPowerManager.isInteractive()); 116 117 SystemClock.sleep(getMinimumScreenOffTimeoutMs()); 118 119 assertTrue(mPowerManager.isInteractive()); 120 mWmState.assertVisibility(TURN_SCREEN_ON_ACTIVITY, true); 121 } 122 123 @ApiTest(apis = "android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON") 124 @Test testKeepScreenOn_activityOnVirtualDisplayNotForeground_screenTurnsOff()125 public void testKeepScreenOn_activityOnVirtualDisplayNotForeground_screenTurnsOff() { 126 assumeTrue(supportsMultiDisplay()); 127 setScreenOffTimeoutMs("500"); 128 129 final WindowManagerState.DisplayContent newDisplay = createManagedVirtualDisplaySession() 130 .setSimulateDisplay(true).createDisplay(); 131 launchActivityOnDisplay(TURN_SCREEN_ON_ACTIVITY, newDisplay.mId); 132 assertTrue(mPowerManager.isInteractive()); 133 try (BlockingBroadcastReceiver r = BlockingBroadcastReceiver.create(mContext, 134 Intent.ACTION_SCREEN_OFF).register()) { 135 launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId); 136 } 137 mWmState.waitAndAssertVisibilityGone(TURN_SCREEN_ON_ACTIVITY); 138 assertFalse(mPowerManager.isInteractive()); 139 } 140 setScreenOffTimeoutMs(String timeoutMs)141 private void setScreenOffTimeoutMs(String timeoutMs) { 142 Settings.System.putString(mContentResolver, SCREEN_OFF_TIMEOUT, timeoutMs); 143 } 144 getMinimumScreenOffTimeoutMs()145 private int getMinimumScreenOffTimeoutMs() { 146 return mContext.getResources().getInteger( 147 Resources.getSystem().getIdentifier("config_minimumScreenOffTimeout", "integer", 148 "android")); 149 } 150 } 151