Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2016 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 com.android.car.test;
     17 
     18 import android.util.Log;
     19 
     20 import com.android.car.hal.PowerHalService;
     21 import com.android.car.hal.PowerHalService.PowerEventListener;
     22 import com.android.car.hal.PowerHalService.PowerState;
     23 import com.android.car.hal.VehicleHal;
     24 
     25 import java.util.LinkedList;
     26 
     27 public class MockedPowerHalService extends PowerHalService {
     28     private static final String TAG = MockedPowerHalService.class.getSimpleName();
     29 
     30     private final boolean mIsPowerStateSupported;
     31     private final boolean mIsDeepSleepAllowed;
     32     private final boolean mIsTimedWakeupAllowed;
     33     private PowerState mCurrentPowerState = new PowerState(PowerHalService.STATE_ON_FULL, 0);
     34     private PowerEventListener mListener;
     35 
     36     private final LinkedList<int[]> mSentStates = new LinkedList<>();
     37 
     38     public MockedPowerHalService(boolean isPowerStateSupported, boolean isDeepSleepAllowed,
     39             boolean isTimedWakeupAllowed) {
     40         super(new VehicleHal(null, null, null, null, null, null, null));
     41         mIsPowerStateSupported = isPowerStateSupported;
     42         mIsDeepSleepAllowed = isDeepSleepAllowed;
     43         mIsTimedWakeupAllowed = isTimedWakeupAllowed;
     44     }
     45 
     46     @Override
     47     public synchronized void setListener(PowerEventListener listener) {
     48         mListener = listener;
     49     }
     50 
     51     @Override
     52     public void sendBootComplete() {
     53         Log.i(TAG, "sendBootComplete");
     54         doSendState(SET_BOOT_COMPLETE, 0);
     55     }
     56 
     57     @Override
     58     public void sendSleepEntry() {
     59         Log.i(TAG, "sendSleepEntry");
     60         doSendState(SET_DEEP_SLEEP_ENTRY, 0);
     61     }
     62 
     63     @Override
     64     public void sendSleepExit() {
     65         Log.i(TAG, "sendSleepExit");
     66         doSendState(SET_DEEP_SLEEP_EXIT, 0);
     67     }
     68 
     69     @Override
     70     public void sendShutdownPostpone(int postponeTimeMs) {
     71         Log.i(TAG, "sendShutdownPostpone");
     72         doSendState(SET_SHUTDOWN_POSTPONE, postponeTimeMs);
     73     }
     74 
     75     @Override
     76     public void sendShutdownStart(int wakeupTimeSec) {
     77         Log.i(TAG, "sendShutdownStart");
     78         doSendState(SET_SHUTDOWN_START, wakeupTimeSec);
     79     }
     80 
     81     @Override
     82     public void sendDisplayOn() {
     83         Log.i(TAG, "sendDisplayOn");
     84         doSendState(SET_DISPLAY_ON, 0);
     85     }
     86 
     87     @Override
     88     public void sendDisplayOff() {
     89         Log.i(TAG, "sendDisplayOff");
     90         doSendState(SET_DISPLAY_OFF, 0);
     91     }
     92 
     93     public synchronized int[] waitForSend(long timeoutMs) throws Exception {
     94         if (mSentStates.size() == 0) {
     95             wait(timeoutMs);
     96         }
     97         return mSentStates.removeFirst();
     98     }
     99 
    100     private synchronized void doSendState(int state, int param) {
    101         int[] toSend = new int[] {state, param};
    102         mSentStates.addLast(toSend);
    103         notifyAll();
    104     }
    105 
    106     @Override
    107     public boolean isPowerStateSupported() {
    108         return mIsPowerStateSupported;
    109     }
    110 
    111     @Override
    112     public boolean isDeepSleepAllowed() {
    113         return mIsDeepSleepAllowed;
    114     }
    115 
    116     @Override
    117     public boolean isTimedWakeupAllowed() {
    118         return mIsTimedWakeupAllowed;
    119     }
    120 
    121     @Override
    122     public synchronized PowerState getCurrentPowerState() {
    123         return mCurrentPowerState;
    124     }
    125 
    126     public void setCurrentPowerState(PowerState state) {
    127         setCurrentPowerState(state, true);
    128     }
    129 
    130     public void setCurrentPowerState(PowerState state, boolean notify) {
    131         PowerEventListener listener;
    132         synchronized (this) {
    133             mCurrentPowerState = state;
    134             listener = mListener;
    135         }
    136         if (listener != null && notify) {
    137             listener.onApPowerStateChange(state);
    138         }
    139     }
    140 }
    141