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