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 com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; 20 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; 21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; 22 23 import static org.junit.Assert.assertNotNull; 24 import static org.mockito.ArgumentMatchers.any; 25 import static org.mockito.Mockito.spy; 26 27 import android.content.Intent; 28 import android.os.IBinder; 29 30 import androidx.test.core.app.ApplicationProvider; 31 32 import com.android.dx.mockito.inline.extended.ExtendedMockito; 33 import com.android.federatedcompute.services.encryption.BackgroundKeyFetchJob; 34 import com.android.federatedcompute.services.scheduling.DeleteExpiredJob; 35 import com.android.federatedcompute.services.scheduling.FederatedComputeLearningJobScheduleOrchestrator; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.mockito.MockitoSession; 42 43 public final class FederatedComputeManagingServiceImplTest { 44 45 @Mock FederatedComputeLearningJobScheduleOrchestrator mMockOrchestrator; 46 47 @Before setup()48 public void setup() { 49 MockitoAnnotations.initMocks(this); 50 } 51 52 @Test testBindableFederatedComputeService()53 public void testBindableFederatedComputeService() { 54 MockitoSession session = 55 ExtendedMockito.mockitoSession() 56 .spyStatic(BackgroundKeyFetchJob.class) 57 .spyStatic(DeleteExpiredJob.class) 58 .spyStatic(FederatedComputeLearningJobScheduleOrchestrator.class) 59 .startMocking(); 60 doNothing().when(() -> BackgroundKeyFetchJob.schedule(any())); 61 doNothing().when(() -> DeleteExpiredJob.schedule(any(), any())); 62 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 verify(() -> BackgroundKeyFetchJob.schedule(any())); 75 verify(() -> DeleteExpiredJob.schedule(any(), any())); 76 verify(mMockOrchestrator).checkAndSchedule(); 77 assertNotNull(binder); 78 } finally { 79 session.finishMocking(); 80 } 81 } 82 } 83