1 /* 2 * Copyright (C) 2023 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 17 package com.android.devicelockcontroller; 18 19 import static org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.when; 21 22 import android.app.Application; 23 import android.content.Context; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 import androidx.work.ListenableWorker; 28 import androidx.work.WorkerParameters; 29 30 import com.android.devicelockcontroller.policy.DevicePolicyController; 31 import com.android.devicelockcontroller.policy.DeviceStateController; 32 import com.android.devicelockcontroller.policy.FinalizationController; 33 import com.android.devicelockcontroller.policy.PolicyObjectsProvider; 34 import com.android.devicelockcontroller.policy.ProvisionStateController; 35 import com.android.devicelockcontroller.schedule.DeviceLockControllerScheduler; 36 import com.android.devicelockcontroller.schedule.DeviceLockControllerSchedulerProvider; 37 import com.android.devicelockcontroller.stats.StatsLogger; 38 import com.android.devicelockcontroller.stats.StatsLoggerProvider; 39 import com.android.devicelockcontroller.storage.GlobalParametersClient; 40 import com.android.devicelockcontroller.storage.GlobalParametersService; 41 import com.android.devicelockcontroller.storage.SetupParametersClient; 42 import com.android.devicelockcontroller.storage.SetupParametersService; 43 44 import com.google.common.util.concurrent.Futures; 45 import com.google.common.util.concurrent.ListenableFuture; 46 import com.google.common.util.concurrent.testing.TestingExecutors; 47 48 import org.robolectric.Robolectric; 49 import org.robolectric.TestLifecycleApplication; 50 51 import java.lang.reflect.Method; 52 53 /** 54 * Application class that provides mock objects for tests. 55 */ 56 public final class TestDeviceLockControllerApplication extends Application implements 57 PolicyObjectsProvider, 58 TestLifecycleApplication, 59 DeviceLockControllerSchedulerProvider, 60 FcmRegistrationTokenProvider, 61 PlayInstallPackageTaskClassProvider, 62 StatsLoggerProvider { 63 64 public static final String TEST_FCM_TOKEN = "fcmToken"; 65 66 private DevicePolicyController mPolicyController; 67 private DeviceStateController mStateController; 68 private ProvisionStateController mProvisionStateController; 69 private FinalizationController mFinalizationController; 70 private DeviceLockControllerScheduler mDeviceLockControllerScheduler; 71 private SetupParametersClient mSetupParametersClient; 72 private GlobalParametersClient mGlobalParametersClient; 73 private StatsLogger mStatsLogger; 74 75 @Override getDeviceStateController()76 public DeviceStateController getDeviceStateController() { 77 if (mStateController == null) { 78 mStateController = mock(DeviceStateController.class); 79 } 80 return mStateController; 81 } 82 83 @Override getProvisionStateController()84 public ProvisionStateController getProvisionStateController() { 85 if (mProvisionStateController == null) { 86 mProvisionStateController = mock(ProvisionStateController.class); 87 when(mProvisionStateController.getDevicePolicyController()).thenReturn( 88 getPolicyController()); 89 when(mProvisionStateController.getDeviceStateController()).thenReturn( 90 getDeviceStateController()); 91 } 92 return mProvisionStateController; 93 } 94 95 @Override getPolicyController()96 public DevicePolicyController getPolicyController() { 97 if (mPolicyController == null) { 98 mPolicyController = mock(DevicePolicyController.class); 99 } 100 return mPolicyController; 101 } 102 103 @Override getFinalizationController()104 public FinalizationController getFinalizationController() { 105 if (mFinalizationController == null) { 106 mFinalizationController = mock(FinalizationController.class); 107 } 108 return mFinalizationController; 109 } 110 111 @Override getStatsLogger()112 public synchronized StatsLogger getStatsLogger() { 113 if (null == mStatsLogger) { 114 mStatsLogger = mock(StatsLogger.class); 115 } 116 return mStatsLogger; 117 } 118 119 @Override 120 @NonNull getFcmRegistrationToken()121 public ListenableFuture<String> getFcmRegistrationToken() { 122 return Futures.immediateFuture(TEST_FCM_TOKEN); 123 } 124 125 @Override destroyObjects()126 public void destroyObjects() { 127 mPolicyController = null; 128 mStateController = null; 129 } 130 131 132 @Override beforeTest(Method method)133 public void beforeTest(Method method) { 134 mSetupParametersClient = SetupParametersClient.getInstance(this, 135 TestingExecutors.sameThreadScheduledExecutor()); 136 mSetupParametersClient.setService( 137 Robolectric.setupService(SetupParametersService.class).onBind(/* intent= */ null)); 138 139 mGlobalParametersClient = GlobalParametersClient.getInstance( 140 this, TestingExecutors.sameThreadScheduledExecutor()); 141 mGlobalParametersClient.setService( 142 Robolectric.setupService(GlobalParametersService.class).onBind(/* intent= */ null)); 143 } 144 145 @Override prepareTest(Object test)146 public void prepareTest(Object test) { 147 } 148 149 @Override afterTest(Method method)150 public void afterTest(Method method) { 151 GlobalParametersClient.reset(); 152 SetupParametersClient.reset(); 153 } 154 155 @Override getDeviceLockControllerScheduler()156 public DeviceLockControllerScheduler getDeviceLockControllerScheduler() { 157 if (mDeviceLockControllerScheduler == null) { 158 mDeviceLockControllerScheduler = mock(DeviceLockControllerScheduler.class); 159 } 160 return mDeviceLockControllerScheduler; 161 } 162 163 @Nullable 164 @Override getPlayInstallPackageTaskClass()165 public Class<? extends ListenableWorker> getPlayInstallPackageTaskClass() { 166 return PlayInstallPackageWorker.class; 167 } 168 169 /** 170 * A stub class for play install worker. 171 */ 172 public static final class PlayInstallPackageWorker extends ListenableWorker { 173 PlayInstallPackageWorker(@onNull Context appContext, @NonNull WorkerParameters workerParameters)174 public PlayInstallPackageWorker(@NonNull Context appContext, 175 @NonNull WorkerParameters workerParameters) { 176 super(appContext, workerParameters); 177 } 178 179 @NonNull 180 @Override startWork()181 public ListenableFuture<Result> startWork() { 182 return Futures.immediateFuture(Result.success()); 183 } 184 } 185 } 186