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.federatedcompute.services; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.Mockito.doNothing; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.times; 24 import static org.mockito.Mockito.verify; 25 26 import android.content.Intent; 27 import android.os.IBinder; 28 29 import androidx.test.core.app.ApplicationProvider; 30 31 import com.android.dx.mockito.inline.extended.ExtendedMockito; 32 import com.android.federatedcompute.services.encryption.BackgroundKeyFetchJobService; 33 import com.android.federatedcompute.services.scheduling.DeleteExpiredJob; 34 import com.android.federatedcompute.services.scheduling.FederatedComputeLearningJobScheduleOrchestrator; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.mockito.MockitoSession; 41 42 public final class FederatedComputeManagingServiceImplTest { 43 44 @Mock FederatedComputeLearningJobScheduleOrchestrator mMockOrchestrator; 45 46 @Before setup()47 public void setup() { 48 MockitoAnnotations.initMocks(this); 49 } 50 51 @Test testBindableFederatedComputeService()52 public void testBindableFederatedComputeService() { 53 MockitoSession session = 54 ExtendedMockito.mockitoSession() 55 .spyStatic(BackgroundKeyFetchJobService.class) 56 .spyStatic(DeleteExpiredJob.class) 57 .spyStatic(FederatedComputeLearningJobScheduleOrchestrator.class) 58 .startMocking(); 59 ExtendedMockito.doReturn(true) 60 .when(() -> BackgroundKeyFetchJobService.scheduleJobIfNeeded(any(), any())); 61 ExtendedMockito.doNothing().when(() -> DeleteExpiredJob.schedule(any(), any())); 62 ExtendedMockito.doReturn(mMockOrchestrator) 63 .when(() -> FederatedComputeLearningJobScheduleOrchestrator.getInstance(any())); 64 doNothing().when(mMockOrchestrator).checkAndSchedule(); 65 try { 66 FederatedComputeManagingServiceImpl spyFcpService = 67 spy(new FederatedComputeManagingServiceImpl(Runnable::run)); 68 spyFcpService.onCreate(); 69 Intent intent = 70 new Intent( 71 ApplicationProvider.getApplicationContext(), 72 FederatedComputeManagingServiceImpl.class); 73 IBinder binder = spyFcpService.onBind(intent); 74 ExtendedMockito.verify( 75 () -> BackgroundKeyFetchJobService.scheduleJobIfNeeded(any(), any()), times(1)); 76 ExtendedMockito.verify(() -> DeleteExpiredJob.schedule(any(), any())); 77 verify(mMockOrchestrator).checkAndSchedule(); 78 assertNotNull(binder); 79 } finally { 80 session.finishMocking(); 81 } 82 } 83 } 84