1 /* 2 * Copyright (C) 2022 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.ondevicepersonalization.services; 18 19 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 20 21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertTrue; 25 import static org.mockito.ArgumentMatchers.any; 26 import static org.mockito.Mockito.never; 27 28 import android.app.job.JobScheduler; 29 import android.content.ComponentName; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.content.pm.PackageManager; 33 34 import androidx.test.core.app.ApplicationProvider; 35 36 import com.android.dx.mockito.inline.extended.ExtendedMockito; 37 import com.android.modules.utils.testing.ExtendedMockitoRule; 38 import com.android.modules.utils.testing.TestableDeviceConfig; 39 import com.android.odp.module.common.DeviceUtils; 40 import com.android.ondevicepersonalization.services.download.mdd.MobileDataDownloadFactory; 41 import com.android.ondevicepersonalization.services.maintenance.OnDevicePersonalizationMaintenanceJob; 42 43 import com.google.common.util.concurrent.ListeningExecutorService; 44 import com.google.common.util.concurrent.MoreExecutors; 45 46 import org.junit.Before; 47 import org.junit.Rule; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.junit.runners.JUnit4; 51 import org.mockito.quality.Strictness; 52 53 @RunWith(JUnit4.class) 54 public class OnDevicePersonalizationBroadcastReceiverTests { 55 private final Context mContext = ApplicationProvider.getApplicationContext(); 56 57 @Rule 58 public final ExtendedMockitoRule mExtendedMockitoRule = 59 new ExtendedMockitoRule.Builder(this) 60 .addStaticMockFixtures(TestableDeviceConfig::new) 61 .spyStatic(DeviceUtils.class) 62 .spyStatic(OnDevicePersonalizationMaintenanceJob.class) 63 .setStrictness(Strictness.LENIENT) 64 .build(); 65 66 @Before setup()67 public void setup() throws Exception { 68 PhFlagsTestUtil.setUpDeviceConfigPermissions(); 69 PhFlagsTestUtil.disableGlobalKillSwitch(); 70 71 // By default, disable SPE. 72 PhFlagsTestUtil.setSpePilotJobEnabled(false); 73 74 ExtendedMockito.doReturn(true).when(() -> DeviceUtils.isOdpSupported(any())); 75 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 76 jobScheduler.cancel(OnDevicePersonalizationConfig.MDD_MAINTENANCE_PERIODIC_TASK_JOB_ID); 77 jobScheduler.cancel(OnDevicePersonalizationConfig.MDD_CHARGING_PERIODIC_TASK_JOB_ID); 78 jobScheduler.cancel( 79 OnDevicePersonalizationConfig.MDD_CELLULAR_CHARGING_PERIODIC_TASK_JOB_ID); 80 jobScheduler.cancel(OnDevicePersonalizationConfig.MDD_WIFI_CHARGING_PERIODIC_TASK_JOB_ID); 81 jobScheduler.cancel(OnDevicePersonalizationConfig.MAINTENANCE_TASK_JOB_ID); 82 jobScheduler.cancel(OnDevicePersonalizationConfig.USER_DATA_COLLECTION_ID); 83 } 84 85 @Test testOnReceive()86 public void testOnReceive() { 87 // Use direct executor to keep all work sequential for the tests 88 ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService(); 89 MobileDataDownloadFactory.getMdd(mContext, executorService, executorService); 90 91 OnDevicePersonalizationBroadcastReceiver receiver = 92 new OnDevicePersonalizationBroadcastReceiver(executorService); 93 94 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); 95 receiver.onReceive(mContext, intent); 96 97 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 98 99 assertTrue( 100 jobScheduler.getPendingJob(OnDevicePersonalizationConfig.MAINTENANCE_TASK_JOB_ID) 101 != null); 102 verify(() -> OnDevicePersonalizationMaintenanceJob.schedule(mContext)); 103 assertTrue( 104 jobScheduler.getPendingJob(OnDevicePersonalizationConfig.USER_DATA_COLLECTION_ID) 105 != null); 106 // MDD tasks 107 assertTrue( 108 jobScheduler.getPendingJob( 109 OnDevicePersonalizationConfig.MDD_MAINTENANCE_PERIODIC_TASK_JOB_ID) 110 != null); 111 assertTrue( 112 jobScheduler.getPendingJob( 113 OnDevicePersonalizationConfig.MDD_CHARGING_PERIODIC_TASK_JOB_ID) 114 != null); 115 assertTrue( 116 jobScheduler.getPendingJob( 117 OnDevicePersonalizationConfig 118 .MDD_CELLULAR_CHARGING_PERIODIC_TASK_JOB_ID) 119 != null); 120 assertTrue( 121 jobScheduler.getPendingJob( 122 OnDevicePersonalizationConfig 123 .MDD_WIFI_CHARGING_PERIODIC_TASK_JOB_ID) 124 != null); 125 } 126 127 @Test testOnReceiveKillSwitchOn()128 public void testOnReceiveKillSwitchOn() { 129 PhFlagsTestUtil.enableGlobalKillSwitch(); 130 // Use direct executor to keep all work sequential for the tests 131 ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService(); 132 OnDevicePersonalizationBroadcastReceiver receiver = 133 new OnDevicePersonalizationBroadcastReceiver(executorService); 134 135 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); 136 receiver.onReceive(mContext, intent); 137 138 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 139 140 assertTrue( 141 jobScheduler.getPendingJob(OnDevicePersonalizationConfig.MAINTENANCE_TASK_JOB_ID) 142 == null); 143 verify(() -> OnDevicePersonalizationMaintenanceJob.schedule(mContext), never()); 144 assertTrue( 145 jobScheduler.getPendingJob(OnDevicePersonalizationConfig.USER_DATA_COLLECTION_ID) 146 == null); 147 // MDD tasks 148 assertTrue( 149 jobScheduler.getPendingJob( 150 OnDevicePersonalizationConfig.MDD_MAINTENANCE_PERIODIC_TASK_JOB_ID) 151 == null); 152 assertTrue( 153 jobScheduler.getPendingJob( 154 OnDevicePersonalizationConfig.MDD_CHARGING_PERIODIC_TASK_JOB_ID) 155 == null); 156 assertTrue( 157 jobScheduler.getPendingJob( 158 OnDevicePersonalizationConfig.MDD_CELLULAR_CHARGING_PERIODIC_TASK_JOB_ID) 159 == null); 160 assertTrue( 161 jobScheduler.getPendingJob( 162 OnDevicePersonalizationConfig.MDD_WIFI_CHARGING_PERIODIC_TASK_JOB_ID) 163 == null); 164 } 165 166 @Test testOnReceiveDeviceNotSupported()167 public void testOnReceiveDeviceNotSupported() { 168 ExtendedMockito.doReturn(false).when(() -> DeviceUtils.isOdpSupported(any())); 169 // Use direct executor to keep all work sequential for the tests 170 ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService(); 171 OnDevicePersonalizationBroadcastReceiver receiver = 172 new OnDevicePersonalizationBroadcastReceiver(executorService); 173 174 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); 175 receiver.onReceive(mContext, intent); 176 177 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 178 179 assertTrue( 180 jobScheduler.getPendingJob(OnDevicePersonalizationConfig.MAINTENANCE_TASK_JOB_ID) 181 == null); 182 verify(() -> OnDevicePersonalizationMaintenanceJob.schedule(mContext), never()); 183 assertTrue( 184 jobScheduler.getPendingJob(OnDevicePersonalizationConfig.USER_DATA_COLLECTION_ID) 185 == null); 186 // MDD tasks 187 assertTrue( 188 jobScheduler.getPendingJob( 189 OnDevicePersonalizationConfig.MDD_MAINTENANCE_PERIODIC_TASK_JOB_ID) 190 == null); 191 assertTrue( 192 jobScheduler.getPendingJob( 193 OnDevicePersonalizationConfig.MDD_CHARGING_PERIODIC_TASK_JOB_ID) 194 == null); 195 assertTrue( 196 jobScheduler.getPendingJob( 197 OnDevicePersonalizationConfig.MDD_CELLULAR_CHARGING_PERIODIC_TASK_JOB_ID) 198 == null); 199 assertTrue( 200 jobScheduler.getPendingJob( 201 OnDevicePersonalizationConfig.MDD_WIFI_CHARGING_PERIODIC_TASK_JOB_ID) 202 == null); 203 } 204 205 @Test testOnReceiveInvalidIntent()206 public void testOnReceiveInvalidIntent() { 207 OnDevicePersonalizationBroadcastReceiver receiver = 208 new OnDevicePersonalizationBroadcastReceiver(); 209 210 Intent intent = new Intent(Intent.ACTION_DIAL_EMERGENCY); 211 receiver.onReceive(mContext, intent); 212 213 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 214 215 assertTrue( 216 jobScheduler.getPendingJob(OnDevicePersonalizationConfig.MAINTENANCE_TASK_JOB_ID) 217 == null); 218 verify(() -> OnDevicePersonalizationMaintenanceJob.schedule(mContext), never()); 219 assertTrue( 220 jobScheduler.getPendingJob(OnDevicePersonalizationConfig.USER_DATA_COLLECTION_ID) 221 == null); 222 // MDD tasks 223 assertTrue( 224 jobScheduler.getPendingJob( 225 OnDevicePersonalizationConfig.MDD_MAINTENANCE_PERIODIC_TASK_JOB_ID) 226 == null); 227 assertTrue( 228 jobScheduler.getPendingJob( 229 OnDevicePersonalizationConfig.MDD_CHARGING_PERIODIC_TASK_JOB_ID) 230 == null); 231 assertTrue( 232 jobScheduler.getPendingJob( 233 OnDevicePersonalizationConfig 234 .MDD_CELLULAR_CHARGING_PERIODIC_TASK_JOB_ID) 235 == null); 236 assertTrue( 237 jobScheduler.getPendingJob( 238 OnDevicePersonalizationConfig 239 .MDD_WIFI_CHARGING_PERIODIC_TASK_JOB_ID) 240 == null); 241 } 242 243 @Test testEnableReceiver()244 public void testEnableReceiver() { 245 assertTrue(OnDevicePersonalizationBroadcastReceiver.enableReceiver(mContext)); 246 ComponentName componentName = 247 new ComponentName(mContext, OnDevicePersonalizationBroadcastReceiver.class); 248 final PackageManager pm = mContext.getPackageManager(); 249 final int result = pm.getComponentEnabledSetting(componentName); 250 assertEquals(COMPONENT_ENABLED_STATE_ENABLED, result); 251 } 252 } 253