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.car.settings.CarSettings;
     19 import android.content.Context;
     20 import android.test.AndroidTestCase;
     21 import android.test.suitebuilder.annotation.MediumTest;
     22 
     23 import com.android.car.CarPowerManagementService;
     24 import com.android.car.DeviceIdleControllerWrapper;
     25 import com.android.car.GarageModeService;
     26 
     27 @MediumTest
     28 public class GarageModeTest extends AndroidTestCase {
     29 
     30     public void testMaintenanceActive() throws Exception {
     31         MockCarPowerManagementService powerManagementService = new MockCarPowerManagementService();
     32         MockDeviceIdleController controller = new MockDeviceIdleController(true);
     33         GarageModeServiceForTest garageMode = new GarageModeServiceForTest(getContext(),
     34                 powerManagementService,
     35                 controller);
     36         garageMode.init();
     37         final int index1 = garageMode.getGarageModeIndex();
     38         assertEquals(garageMode.getMaintenanceWindow(),
     39                 powerManagementService.doNotifyPrepareShutdown(false));
     40         assertEquals(true, garageMode.isInGarageMode());
     41         assertEquals(true, garageMode.isMaintenanceActive());
     42 
     43         controller.setMaintenanceActivity(false);
     44         assertEquals(false, garageMode.isInGarageMode());
     45         assertEquals(false, garageMode.isMaintenanceActive());
     46         final int index2 = garageMode.getGarageModeIndex();
     47 
     48         assertEquals(1, index2 - index1);
     49     }
     50 
     51     public void testMaintenanceInactive() throws Exception {
     52         MockCarPowerManagementService powerManagementService = new MockCarPowerManagementService();
     53         MockDeviceIdleController controller = new MockDeviceIdleController(false);
     54         GarageModeServiceForTest garageMode = new GarageModeServiceForTest(getContext(),
     55                 powerManagementService,
     56                 controller);
     57         garageMode.init();
     58         assertEquals(garageMode.getMaintenanceWindow(),
     59                 powerManagementService.doNotifyPrepareShutdown(false));
     60         assertEquals(true, garageMode.isInGarageMode());
     61         assertEquals(false, garageMode.isMaintenanceActive());
     62     }
     63 
     64     public void testDisplayOn() throws Exception {
     65         MockCarPowerManagementService powerManagementService = new MockCarPowerManagementService();
     66         MockDeviceIdleController controller = new MockDeviceIdleController(true);
     67         GarageModeServiceForTest garageMode = new GarageModeServiceForTest(getContext(),
     68                 powerManagementService,
     69                 controller);
     70         garageMode.init();
     71 
     72         powerManagementService.doNotifyPrepareShutdown(false);
     73         assertTrue(garageMode.getGarageModeIndex() > 0);
     74         powerManagementService.doNotifyPowerOn(true);
     75         assertEquals(0,garageMode.getGarageModeIndex());
     76     }
     77 
     78     private static class MockCarPowerManagementService extends CarPowerManagementService {
     79         public long doNotifyPrepareShutdown(boolean shuttingdown) {
     80             return notifyPrepareShutdown(shuttingdown);
     81         }
     82 
     83         public void doNotifyPowerOn(boolean displayOn) {
     84             notifyPowerOn(displayOn);
     85         }
     86     }
     87 
     88     private static class GarageModeServiceForTest extends GarageModeService {
     89         public GarageModeServiceForTest(Context context,
     90                 CarPowerManagementService powerManagementService,
     91                 DeviceIdleControllerWrapper controllerWrapper) {
     92             super(context, powerManagementService, controllerWrapper);
     93         }
     94 
     95         public long getMaintenanceWindow() {
     96             return CarSettings.DEFAULT_GARAGE_MODE_MAINTENANCE_WINDOW;
     97         }
     98 
     99         public boolean isInGarageMode() {
    100             synchronized (this) {
    101                 return mInGarageMode;
    102             }
    103         }
    104 
    105         public boolean isMaintenanceActive() {
    106             synchronized (this) {
    107                 return mMaintenanceActive;
    108             }
    109         }
    110 
    111         public int getGarageModeIndex() {
    112             synchronized (this) {
    113                 return mGarageModeIndex;
    114             }
    115         }
    116     }
    117 
    118     private static class MockDeviceIdleController extends DeviceIdleControllerWrapper {
    119 
    120         private final boolean mInitialActive;
    121         public MockDeviceIdleController(boolean active) {
    122             super();
    123             mInitialActive = active;
    124         }
    125 
    126         @Override
    127         protected boolean startLocked() {
    128             return mInitialActive;
    129         }
    130 
    131         @Override
    132         public void stopTracking() {
    133             // nothing to clean up
    134         }
    135 
    136         @Override
    137         protected void reportActiveLocked(final boolean active) {
    138             // directly calling the callback instead of posting to handler, to make testing easier.
    139             if (mListener.get() != null) {
    140                 mListener.get().onMaintenanceActivityChanged(active);
    141             }
    142         }
    143 
    144         public void setMaintenanceActivity(boolean active) {
    145             super.setMaintenanceActivity(active);
    146         }
    147     }
    148 }
    149