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 String REBOOT_REASON_QUIESCENT = "quiescent"; 48 private static final String FEATURE_LEANBACK_ONLY = "android.software.leanback_only"; 49 private static final String CMD_DUMPSYS_POWER = "dumpsys power --proto"; 50 private static final String CMD_INPUT_WAKEUP = "input keyevent WAKEUP"; 51 private static final String CMD_INPUT_POWER = "input keyevent POWER"; 52 53 // A reference to the device under test, which gives us a handle to run commands. 54 private ITestDevice mDevice; 55 56 @Before setUp()57 public synchronized void setUp() throws Exception { 58 mDevice = getDevice(); 59 assumeTrue("Test only applicable to TVs.", hasDeviceFeature(FEATURE_LEANBACK_ONLY)); 60 assumeFalse("Test only applicable to devices launching on Android 11 or later.", 61 (PropertyUtil.getFirstApiLevel(mDevice) < 30)); 62 } 63 64 @After tearDown()65 public void tearDown() throws Exception { 66 if (hasDeviceFeature(FEATURE_LEANBACK_ONLY)) { 67 mDevice.executeShellCommand(CMD_INPUT_WAKEUP); 68 } 69 } 70 71 @Test testQuiescentBoot_asleep()72 public void testQuiescentBoot_asleep() throws Exception { 73 mDevice.reboot(REBOOT_REASON_QUIESCENT); 74 assertEquals("Expected to boot into sleep state.", WAKEFULNESS_ASLEEP, getWakefulness()); 75 } 76 77 @Test testQuiescentBoot_wakesUpWithPowerButton()78 public void testQuiescentBoot_wakesUpWithPowerButton() throws Exception { 79 mDevice.reboot(REBOOT_REASON_QUIESCENT); 80 mDevice.executeShellCommand(CMD_INPUT_POWER); 81 assertEquals("Expected to wake up when pressing the power button.", 82 WAKEFULNESS_AWAKE, getWakefulness()); 83 } 84 85 @Test testQuiescentBoot_asleepAfterQuiescentReboot()86 public void testQuiescentBoot_asleepAfterQuiescentReboot() throws Exception { 87 mDevice.reboot(REBOOT_REASON_QUIESCENT); 88 assertEquals("Expected to boot into sleep state.", WAKEFULNESS_ASLEEP, getWakefulness()); 89 } 90 91 @Test testQuiescentBoot_awakeAfterReboot()92 public void testQuiescentBoot_awakeAfterReboot() throws Exception { 93 mDevice.reboot(REBOOT_REASON_QUIESCENT); 94 mDevice.reboot(); 95 assertEquals("Expected to boot in awake state.", WAKEFULNESS_AWAKE, getWakefulness()); 96 } 97 98 @Test testQuiescentBoot_activitiesNotResumedAfterBoot()99 public void testQuiescentBoot_activitiesNotResumedAfterBoot() throws Exception { 100 mDevice.reboot(REBOOT_REASON_QUIESCENT); 101 List<String> resumedActivities = WindowManagerUtil.getResumedActivities(getDevice()); 102 assertEquals("Expected no resumed activities", 0, resumedActivities.size()); 103 } 104 getWakefulness()105 private Wakefulness getWakefulness() throws Exception { 106 return ((PowerManagerServiceDumpProto) ProtoUtils.getProto(getDevice(), 107 PowerManagerServiceDumpProto.parser(), 108 CMD_DUMPSYS_POWER)).getWakefulness(); 109 } 110 } 111