14b0212c1b3576f4174c292bbcdd72815584ff075keunyoung/* 24b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * Copyright (C) 2015 The Android Open Source Project 34b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * 44b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * Licensed under the Apache License, Version 2.0 (the "License"); 54b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * you may not use this file except in compliance with the License. 64b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * You may obtain a copy of the License at 74b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * 84b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * http://www.apache.org/licenses/LICENSE-2.0 94b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * 104b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * Unless required by applicable law or agreed to in writing, software 114b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * distributed under the License is distributed on an "AS IS" BASIS, 124b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * See the License for the specific language governing permissions and 144b0212c1b3576f4174c292bbcdd72815584ff075keunyoung * limitations under the License. 154b0212c1b3576f4174c292bbcdd72815584ff075keunyoung */ 161488ef2171bbc3bf25fc00c424a9979843f5ec6eKeun-young Parkpackage com.android.car.test; 174b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 18cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleApPowerBootupReason; 19cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleApPowerSetState; 20cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleApPowerState; 21cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleApPowerStateConfigFlag; 22cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleApPowerStateIndex; 23cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleApPowerStateShutdownParam; 24cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehiclePropValue; 25cfe93105f637c2822da113308f113ed418d0b319Pavel Maltsevimport android.hardware.automotive.vehicle.V2_0.VehicleProperty; 264b0212c1b3576f4174c292bbcdd72815584ff075keunyoungimport android.os.SystemClock; 272906c4461afa9ac0c5865768a3238281e5ecf26dPavel Maltsevimport android.test.suitebuilder.annotation.MediumTest; 284b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 290d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport com.google.android.collect.Lists; 304b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 310d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport com.android.car.vehiclehal.VehiclePropValueBuilder; 320d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport com.android.car.vehiclehal.test.MockedVehicleHal.VehicleHalPropertyHandler; 330d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev 340d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsevimport java.util.ArrayList; 354b0212c1b3576f4174c292bbcdd72815584ff075keunyoungimport java.util.LinkedList; 364b0212c1b3576f4174c292bbcdd72815584ff075keunyoungimport java.util.concurrent.Semaphore; 374b0212c1b3576f4174c292bbcdd72815584ff075keunyoungimport java.util.concurrent.TimeUnit; 384b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 392906c4461afa9ac0c5865768a3238281e5ecf26dPavel Maltsev@MediumTest 404b0212c1b3576f4174c292bbcdd72815584ff075keunyoungpublic class CarPowerManagementTest extends MockedCarTestBase { 414b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 424b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private final PowerStatePropertyHandler mPowerStateHandler = new PowerStatePropertyHandler(); 434b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 440d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev private void setupPowerPropertyAndStart(boolean allowSleep) { 450d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev addProperty(VehicleProperty.AP_POWER_STATE, mPowerStateHandler) 460d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .setConfigArray(Lists.newArrayList( 470d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev allowSleep ? VehicleApPowerStateConfigFlag.ENABLE_DEEP_SLEEP_FLAG : 0)); 481f4d6a7b960e66116de0c1ccb90f900275cff207Keun-young Park 490d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev addStaticProperty(VehicleProperty.AP_POWER_BOOTUP_REASON, 500d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev VehiclePropValueBuilder.newBuilder(VehicleProperty.AP_POWER_BOOTUP_REASON) 510d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .addIntValue(VehicleApPowerBootupReason.USER_POWER_ON) 520d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .build()); 531f4d6a7b960e66116de0c1ccb90f900275cff207Keun-young Park 540d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev reinitializeMockedHal(); 554b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 564b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 574b0212c1b3576f4174c292bbcdd72815584ff075keunyoung public void testImmediateShutdown() throws Exception { 581f4d6a7b960e66116de0c1ccb90f900275cff207Keun-young Park setupPowerPropertyAndStart(true); 594b0212c1b3576f4174c292bbcdd72815584ff075keunyoung assertBootComplete(); 604b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mPowerStateHandler.sendPowerState( 610d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev VehicleApPowerState.SHUTDOWN_PREPARE, 620d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev VehicleApPowerStateShutdownParam.SHUTDOWN_IMMEDIATELY); 630d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev mPowerStateHandler.waitForStateSetAndGetAll(DEFAULT_WAIT_TIMEOUT_MS, 640d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev VehicleApPowerSetState.SHUTDOWN_START); 650d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev mPowerStateHandler.sendPowerState(VehicleApPowerState.ON_FULL, 0); 664b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 674b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 684b0212c1b3576f4174c292bbcdd72815584ff075keunyoung public void testDisplayOnOff() throws Exception { 691f4d6a7b960e66116de0c1ccb90f900275cff207Keun-young Park setupPowerPropertyAndStart(true); 704b0212c1b3576f4174c292bbcdd72815584ff075keunyoung assertBootComplete(); 714b0212c1b3576f4174c292bbcdd72815584ff075keunyoung for (int i = 0; i < 2; i++) { 720d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev mPowerStateHandler.sendPowerState(VehicleApPowerState.ON_DISP_OFF, 0); 730d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev waitForFakeDisplayState(false); 740d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev mPowerStateHandler.sendPowerState(VehicleApPowerState.ON_FULL, 0); 750d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev waitForFakeDisplayState(true); 764b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 774b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 784b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 794b0212c1b3576f4174c292bbcdd72815584ff075keunyoung /* TODO make deep sleep work to test this 804b0212c1b3576f4174c292bbcdd72815584ff075keunyoung public void testSleepEntry() throws Exception { 814b0212c1b3576f4174c292bbcdd72815584ff075keunyoung assertBootComplete(); 824b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mPowerStateHandler.sendPowerState( 830d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev VehicleApPowerState.SHUTDOWN_PREPARE, 840d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev VehicleApPowerStateShutdownParam.CAN_SLEEP); 850d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev assertResponse(VehicleApPowerSetState.DEEP_SLEEP_ENTRY, 0); 860d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev assertResponse(VehicleApPowerSetState.DEEP_SLEEP_EXIT, 0); 874b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mPowerStateHandler.sendPowerState( 880d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev VehicleApPowerState.ON_FULL, 894b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 0); 904b0212c1b3576f4174c292bbcdd72815584ff075keunyoung }*/ 914b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 924b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private void assertResponse(int expectedResponseState, int expectedResponseParam) 934b0212c1b3576f4174c292bbcdd72815584ff075keunyoung throws Exception { 940d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev LinkedList<int[]> setEvents = mPowerStateHandler.waitForStateSetAndGetAll( 950d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev DEFAULT_WAIT_TIMEOUT_MS, expectedResponseState); 964b0212c1b3576f4174c292bbcdd72815584ff075keunyoung int[] last = setEvents.getLast(); 974b0212c1b3576f4174c292bbcdd72815584ff075keunyoung assertEquals(expectedResponseState, last[0]); 984b0212c1b3576f4174c292bbcdd72815584ff075keunyoung assertEquals(expectedResponseParam, last[1]); 994b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1004b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1014b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private void assertBootComplete() throws Exception { 1020d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev mPowerStateHandler.waitForSubscription(DEFAULT_WAIT_TIMEOUT_MS); 1030d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev LinkedList<int[]> setEvents = mPowerStateHandler.waitForStateSetAndGetAll( 1040d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev DEFAULT_WAIT_TIMEOUT_MS, VehicleApPowerSetState.BOOT_COMPLETE); 1054b0212c1b3576f4174c292bbcdd72815584ff075keunyoung int[] first = setEvents.getFirst(); 1060d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev assertEquals(VehicleApPowerSetState.BOOT_COMPLETE, first[0]); 1074b0212c1b3576f4174c292bbcdd72815584ff075keunyoung assertEquals(0, first[1]); 1084b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1094b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1104b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private class PowerStatePropertyHandler implements VehicleHalPropertyHandler { 1114b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1120d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev private int mPowerState = VehicleApPowerState.ON_FULL; 1134b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private int mPowerParam = 0; 1144b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1154b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private final Semaphore mSubscriptionWaitSemaphore = new Semaphore(0); 1164b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private final Semaphore mSetWaitSemaphore = new Semaphore(0); 1174b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private LinkedList<int[]> mSetStates = new LinkedList<>(); 1184b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1194b0212c1b3576f4174c292bbcdd72815584ff075keunyoung @Override 1204b0212c1b3576f4174c292bbcdd72815584ff075keunyoung public void onPropertySet(VehiclePropValue value) { 1210d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev ArrayList<Integer> v = value.value.int32Values; 1224b0212c1b3576f4174c292bbcdd72815584ff075keunyoung synchronized (this) { 1234b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mSetStates.add(new int[] { 1240d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev v.get(VehicleApPowerStateIndex.STATE), 1250d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev v.get(VehicleApPowerStateIndex.ADDITIONAL) 1260d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev }); 1274b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1284b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mSetWaitSemaphore.release(); 1294b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1304b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1314b0212c1b3576f4174c292bbcdd72815584ff075keunyoung @Override 1324b0212c1b3576f4174c292bbcdd72815584ff075keunyoung public synchronized VehiclePropValue onPropertyGet(VehiclePropValue value) { 1330d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev return VehiclePropValueBuilder.newBuilder(VehicleProperty.AP_POWER_STATE) 1340d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .setTimestamp(SystemClock.elapsedRealtimeNanos()) 1350d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .addIntValue(mPowerState, mPowerParam) 1360d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .build(); 1374b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1384b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1394b0212c1b3576f4174c292bbcdd72815584ff075keunyoung @Override 1400d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev public void onPropertySubscribe(int property, int zones, float sampleRate) { 1414b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mSubscriptionWaitSemaphore.release(); 1424b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1434b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1444b0212c1b3576f4174c292bbcdd72815584ff075keunyoung @Override 1454b0212c1b3576f4174c292bbcdd72815584ff075keunyoung public void onPropertyUnsubscribe(int property) { 1464b0212c1b3576f4174c292bbcdd72815584ff075keunyoung //ignore 1474b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1484b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1494b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private synchronized void setCurrentState(int state, int param) { 1504b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mPowerState = state; 1514b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mPowerParam = param; 1524b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1534b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1544b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private void waitForSubscription(long timeoutMs) throws Exception { 1554b0212c1b3576f4174c292bbcdd72815584ff075keunyoung if (!mSubscriptionWaitSemaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { 1564b0212c1b3576f4174c292bbcdd72815584ff075keunyoung fail("waitForSubscription timeout"); 1574b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1584b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1594b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1604b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private LinkedList<int[]> waitForStateSetAndGetAll(long timeoutMs, int expectedSet) 1614b0212c1b3576f4174c292bbcdd72815584ff075keunyoung throws Exception { 1624b0212c1b3576f4174c292bbcdd72815584ff075keunyoung while (true) { 1634b0212c1b3576f4174c292bbcdd72815584ff075keunyoung if (!mSetWaitSemaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { 1644b0212c1b3576f4174c292bbcdd72815584ff075keunyoung fail("waitForStateSetAndGetAll timeout"); 1654b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1664b0212c1b3576f4174c292bbcdd72815584ff075keunyoung synchronized (this) { 1674b0212c1b3576f4174c292bbcdd72815584ff075keunyoung boolean found = false; 1684b0212c1b3576f4174c292bbcdd72815584ff075keunyoung for (int[] state : mSetStates) { 1694b0212c1b3576f4174c292bbcdd72815584ff075keunyoung if (state[0] == expectedSet) { 1704b0212c1b3576f4174c292bbcdd72815584ff075keunyoung found = true; 1714b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1724b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1734b0212c1b3576f4174c292bbcdd72815584ff075keunyoung if (found) { 1744b0212c1b3576f4174c292bbcdd72815584ff075keunyoung LinkedList<int[]> res = mSetStates; 1754b0212c1b3576f4174c292bbcdd72815584ff075keunyoung mSetStates = new LinkedList<>(); 1764b0212c1b3576f4174c292bbcdd72815584ff075keunyoung return res; 1774b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1784b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1794b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1804b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1814b0212c1b3576f4174c292bbcdd72815584ff075keunyoung 1824b0212c1b3576f4174c292bbcdd72815584ff075keunyoung private void sendPowerState(int state, int param) { 1830d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev getMockedVehicleHal().injectEvent( 1840d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev VehiclePropValueBuilder.newBuilder(VehicleProperty.AP_POWER_STATE) 1850d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .setTimestamp(SystemClock.elapsedRealtimeNanos()) 1860d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .addIntValue(state, param) 1870d07c76bbc788fba8c77d8e932330ab22ec6ba27Pavel Maltsev .build()); 1884b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1894b0212c1b3576f4174c292bbcdd72815584ff075keunyoung } 1904b0212c1b3576f4174c292bbcdd72815584ff075keunyoung} 191