1 /* 2 * Copyright (C) 2019 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.os.cts; 18 19 import static android.os.PowerManagerInternalProto.Wakefulness.WAKEFULNESS_ASLEEP; 20 import static android.os.PowerManagerInternalProto.Wakefulness.WAKEFULNESS_AWAKE; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertNull; 24 import static org.junit.Assume.assumeTrue; 25 import static org.junit.Assume.assumeFalse; 26 27 import android.os.PowerManagerInternalProto.Wakefulness; 28 29 import com.android.compatibility.common.util.PropertyUtil; 30 import com.android.compatibility.common.util.ProtoUtils; 31 import com.android.compatibility.common.util.WindowManagerUtil; 32 import com.android.server.power.PowerManagerServiceDumpProto; 33 import com.android.tradefed.device.ITestDevice; 34 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 35 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 36 37 import org.junit.After; 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.ArrayList; 43 import java.util.List; 44 45 @RunWith(DeviceJUnit4ClassRunner.class) 46 public class QuiescentBootTests extends BaseHostJUnit4Test { 47 private static final int REBOOT_TIMEOUT = 60000; 48 49 private static final String FEATURE_LEANBACK_ONLY = "android.software.leanback_only"; 50 private static final String CMD_DUMPSYS_POWER = "dumpsys power --proto"; 51 private static final String CMD_INPUT_WAKEUP = "input keyevent WAKEUP"; 52 private static final String CMD_INPUT_POWER = "input keyevent POWER"; 53 54 // A reference to the device under test, which gives us a handle to run commands. 55 private ITestDevice mDevice; 56 57 @Before setUp()58 public synchronized void setUp() throws Exception { 59 mDevice = getDevice(); 60 assumeTrue("Test only applicable to TVs.", hasDeviceFeature(FEATURE_LEANBACK_ONLY)); 61 assumeFalse("Test only applicable to devices launching on Android 11 or later.", 62 (PropertyUtil.getFirstApiLevel(mDevice) < 30)); 63 } 64 65 @After tearDown()66 public void tearDown() throws Exception { 67 if (hasDeviceFeature(FEATURE_LEANBACK_ONLY)) { 68 mDevice.executeShellCommand(CMD_INPUT_WAKEUP); 69 } 70 } 71 72 @Test testQuiescentBoot_asleep()73 public void testQuiescentBoot_asleep() throws Exception { 74 mDevice.executeAdbCommand("reboot", "quiescent"); 75 mDevice.waitForBootComplete(REBOOT_TIMEOUT); 76 assertEquals("Expected to boot into sleep state.", WAKEFULNESS_ASLEEP, getWakefulness()); 77 } 78 79 @Test testQuiescentBoot_wakesUpWithPowerButton()80 public void testQuiescentBoot_wakesUpWithPowerButton() throws Exception { 81 mDevice.executeAdbCommand("reboot", "quiescent"); 82 mDevice.waitForBootComplete(REBOOT_TIMEOUT); 83 mDevice.executeShellCommand(CMD_INPUT_POWER); 84 assertEquals("Expected to wake up when pressing the power button.", 85 WAKEFULNESS_AWAKE, getWakefulness()); 86 } 87 88 @Test testQuiescentBoot_asleepAfterQuiescentReboot()89 public void testQuiescentBoot_asleepAfterQuiescentReboot() throws Exception { 90 mDevice.executeAdbCommand("reboot", "quiescent"); 91 mDevice.waitForBootComplete(REBOOT_TIMEOUT); 92 93 assertEquals("Expected to boot into sleep state.", WAKEFULNESS_ASLEEP, getWakefulness()); 94 } 95 96 @Test testQuiescentBoot_awakeAfterReboot()97 public void testQuiescentBoot_awakeAfterReboot() throws Exception { 98 mDevice.executeAdbCommand("reboot", "quiescent"); 99 mDevice.waitForBootComplete(REBOOT_TIMEOUT); 100 101 mDevice.executeAdbCommand("reboot"); 102 mDevice.waitForBootComplete(REBOOT_TIMEOUT); 103 104 assertEquals("Expected to boot in awake state.", WAKEFULNESS_AWAKE, getWakefulness()); 105 } 106 107 @Test testQuiescentBoot_activitiesNotResumedAfterBoot()108 public void testQuiescentBoot_activitiesNotResumedAfterBoot() throws Exception { 109 mDevice.executeAdbCommand("reboot", "quiescent"); 110 mDevice.waitForBootComplete(REBOOT_TIMEOUT); 111 112 List<String> resumedActivities = WindowManagerUtil.getResumedActivities(getDevice()); 113 assertEquals("Expected no resumed activities", 0, resumedActivities.size()); 114 } 115 getWakefulness()116 private Wakefulness getWakefulness() throws Exception { 117 return ((PowerManagerServiceDumpProto) ProtoUtils.getProto(getDevice(), 118 PowerManagerServiceDumpProto.parser(), 119 CMD_DUMPSYS_POWER)).getWakefulness(); 120 } 121 } 122