1 /* 2 * Copyright 2024 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.federatedcompute.services; 18 19 import static org.junit.Assert.assertTrue; 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.Mockito.doNothing; 22 import static org.mockito.Mockito.verify; 23 24 import android.app.job.JobScheduler; 25 import android.content.Context; 26 import android.content.Intent; 27 28 import androidx.test.core.app.ApplicationProvider; 29 30 import com.android.dx.mockito.inline.extended.ExtendedMockito; 31 import com.android.federatedcompute.services.common.FederatedComputeJobInfo; 32 import com.android.federatedcompute.services.common.PhFlagsTestUtil; 33 import com.android.federatedcompute.services.scheduling.FederatedComputeLearningJobScheduleOrchestrator; 34 import com.android.modules.utils.testing.ExtendedMockitoRule; 35 import com.android.modules.utils.testing.TestableDeviceConfig; 36 import com.android.odp.module.common.DeviceUtils; 37 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.junit.runners.JUnit4; 43 import org.mockito.Mock; 44 import org.mockito.quality.Strictness; 45 46 @RunWith(JUnit4.class) 47 public class FederatedComputeBroadcastReceiverTest { 48 49 private final Context mContext = ApplicationProvider.getApplicationContext(); 50 51 @Rule 52 public final ExtendedMockitoRule mExtendedMockitoRule = 53 new ExtendedMockitoRule.Builder(this) 54 .addStaticMockFixtures(TestableDeviceConfig::new) 55 .spyStatic(DeviceUtils.class) 56 .spyStatic(FederatedComputeLearningJobScheduleOrchestrator.class) 57 .setStrictness(Strictness.LENIENT) 58 .build(); 59 60 @Mock private FederatedComputeLearningJobScheduleOrchestrator mMockOrchestrator; 61 62 @Before setup()63 public void setup() throws Exception { 64 PhFlagsTestUtil.setUpDeviceConfigPermissions(); 65 PhFlagsTestUtil.disableGlobalKillSwitch(); 66 ExtendedMockito.doReturn(true).when(() -> DeviceUtils.isOdpSupported(any())); 67 ExtendedMockito.doReturn(mMockOrchestrator) 68 .when(() -> FederatedComputeLearningJobScheduleOrchestrator.getInstance(any())); 69 doNothing().when(mMockOrchestrator).checkAndSchedule(); 70 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 71 jobScheduler.cancel(FederatedComputeJobInfo.ENCRYPTION_KEY_FETCH_JOB_ID); 72 jobScheduler.cancel(FederatedComputeJobInfo.DELETE_EXPIRED_JOB_ID); 73 } 74 75 @Test testOnReceive()76 public void testOnReceive() { 77 FederatedComputeBroadcastReceiver receiver = 78 new FederatedComputeBroadcastReceiver(Runnable::run); 79 80 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); 81 receiver.onReceive(mContext, intent); 82 83 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 84 85 assertTrue( 86 jobScheduler.getPendingJob(FederatedComputeJobInfo.ENCRYPTION_KEY_FETCH_JOB_ID) 87 != null); 88 assertTrue( 89 jobScheduler.getPendingJob(FederatedComputeJobInfo.DELETE_EXPIRED_JOB_ID) != null); 90 verify(mMockOrchestrator).checkAndSchedule(); 91 } 92 93 @Test testOnReceiveKillSwitchOn()94 public void testOnReceiveKillSwitchOn() { 95 PhFlagsTestUtil.enableGlobalKillSwitch(); 96 FederatedComputeBroadcastReceiver receiver = new FederatedComputeBroadcastReceiver(); 97 98 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); 99 receiver.onReceive(mContext, intent); 100 101 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 102 assertTrue( 103 jobScheduler.getPendingJob(FederatedComputeJobInfo.ENCRYPTION_KEY_FETCH_JOB_ID) 104 == null); 105 assertTrue( 106 jobScheduler.getPendingJob(FederatedComputeJobInfo.DELETE_EXPIRED_JOB_ID) == null); 107 } 108 109 @Test testOnReceiveDeviceNotSupported()110 public void testOnReceiveDeviceNotSupported() { 111 ExtendedMockito.doReturn(false).when(() -> DeviceUtils.isOdpSupported(any())); 112 FederatedComputeBroadcastReceiver receiver = new FederatedComputeBroadcastReceiver(); 113 114 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); 115 receiver.onReceive(mContext, intent); 116 117 JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class); 118 assertTrue( 119 jobScheduler.getPendingJob(FederatedComputeJobInfo.ENCRYPTION_KEY_FETCH_JOB_ID) 120 == null); 121 assertTrue( 122 jobScheduler.getPendingJob(FederatedComputeJobInfo.DELETE_EXPIRED_JOB_ID) == null); 123 } 124 } 125