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