diff options
Diffstat (limited to 'adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventFallbackReportingJobServiceTest.java')
-rw-r--r-- | adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventFallbackReportingJobServiceTest.java | 81 |
1 files changed, 55 insertions, 26 deletions
diff --git a/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventFallbackReportingJobServiceTest.java b/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventFallbackReportingJobServiceTest.java index 0fb5730cc3..84f2bbfa74 100644 --- a/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventFallbackReportingJobServiceTest.java +++ b/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventFallbackReportingJobServiceTest.java @@ -19,6 +19,7 @@ package com.android.adservices.service.measurement.reporting; import static com.android.adservices.service.AdServicesConfig.MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -35,33 +36,31 @@ import android.app.job.JobInfo; import android.app.job.JobParameters; import android.app.job.JobScheduler; import android.content.Context; -import android.provider.DeviceConfig; +import com.android.adservices.data.enrollment.EnrollmentDao; import com.android.adservices.data.measurement.DatastoreManager; import com.android.adservices.data.measurement.DatastoreManagerFactory; +import com.android.adservices.service.AdServicesConfig; +import com.android.adservices.service.Flags; +import com.android.adservices.service.FlagsFactory; import com.android.compatibility.common.util.TestUtils; import com.android.dx.mockito.inline.extended.ExtendedMockito; -import com.android.modules.utils.testing.TestableDeviceConfig; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.mockito.MockitoSession; import org.mockito.quality.Strictness; import java.util.Optional; +import java.util.concurrent.TimeUnit; /** * Unit test for {@link EventFallbackReportingJobService */ public class EventFallbackReportingJobServiceTest { - // This rule is used for configuring P/H flags - @Rule - public final TestableDeviceConfig.TestableDeviceConfigRule mDeviceConfigRule = - new TestableDeviceConfig.TestableDeviceConfigRule(); - - private static final long WAIT_IN_MILLIS = 50L; + private static final long WAIT_IN_MILLIS = 1_000L; private DatastoreManager mMockDatastoreManager; private JobScheduler mMockJobScheduler; @@ -77,10 +76,11 @@ public class EventFallbackReportingJobServiceTest { @Test public void onStartJob_killSwitchOn() throws Exception { - enableKillSwitch(); - runWithMocks( () -> { + // Setup + enableKillSwitch(); + // Execute boolean result = mSpyService.onStartJob(Mockito.mock(JobParameters.class)); @@ -97,11 +97,11 @@ public class EventFallbackReportingJobServiceTest { @Test public void onStartJob_killSwitchOff() throws Exception { - disableKillSwitch(); - runWithMocks( () -> { // Setup + disableKillSwitch(); + ExtendedMockito.doNothing() .when( () -> @@ -124,11 +124,11 @@ public class EventFallbackReportingJobServiceTest { @Test public void scheduleIfNeeded_killSwitchOn_dontSchedule() throws Exception { - enableKillSwitch(); - runWithMocks( () -> { // Setup + enableKillSwitch(); + final Context mockContext = mock(Context.class); doReturn(mMockJobScheduler) .when(mockContext) @@ -155,11 +155,11 @@ public class EventFallbackReportingJobServiceTest { @Test public void scheduleIfNeeded_killSwitchOff_previouslyExecuted_dontForceSchedule_dontSchedule() throws Exception { - disableKillSwitch(); - runWithMocks( () -> { // Setup + disableKillSwitch(); + final Context mockContext = mock(Context.class); doReturn(mMockJobScheduler) .when(mockContext) @@ -186,11 +186,11 @@ public class EventFallbackReportingJobServiceTest { @Test public void scheduleIfNeeded_killSwitchOff_previouslyExecuted_forceSchedule_schedule() throws Exception { - disableKillSwitch(); - runWithMocks( () -> { // Setup + disableKillSwitch(); + final Context mockContext = mock(Context.class); doReturn(mMockJobScheduler) .when(mockContext) @@ -218,11 +218,11 @@ public class EventFallbackReportingJobServiceTest { @Test public void scheduleIfNeeded_killSwitchOff_previouslyNotExecuted_dontForceSchedule_schedule() throws Exception { - disableKillSwitch(); - runWithMocks( () -> { // Setup + disableKillSwitch(); + final Context mockContext = mock(Context.class); doReturn(mMockJobScheduler) .when(mockContext) @@ -245,11 +245,34 @@ public class EventFallbackReportingJobServiceTest { }); } + @Test + public void testSchedule_jobInfoIsPersisted() throws Exception { + runWithMocks( + () -> { + // Setup + final JobScheduler jobScheduler = mock(JobScheduler.class); + final ArgumentCaptor<JobInfo> captor = ArgumentCaptor.forClass(JobInfo.class); + + // Execute + ExtendedMockito.doCallRealMethod() + .when(() -> EventFallbackReportingJobService.schedule(any(), any())); + EventFallbackReportingJobService.schedule(mock(Context.class), jobScheduler); + + // Validate + verify(jobScheduler, times(1)).schedule(captor.capture()); + assertNotNull(captor.getValue()); + assertTrue(captor.getValue().isPersisted()); + }); + } + private void runWithMocks(TestUtils.RunnableWithThrow execute) throws Exception { MockitoSession session = ExtendedMockito.mockitoSession() + .spyStatic(AdServicesConfig.class) .spyStatic(DatastoreManagerFactory.class) + .spyStatic(EnrollmentDao.class) .spyStatic(EventFallbackReportingJobService.class) + .spyStatic(FlagsFactory.class) .strictness(Strictness.LENIENT) .startMocking(); try { @@ -261,6 +284,12 @@ public class EventFallbackReportingJobServiceTest { doNothing().when(mSpyService).jobFinished(any(), anyBoolean()); doReturn(mMockJobScheduler).when(mSpyService).getSystemService(JobScheduler.class); doReturn(Mockito.mock(Context.class)).when(mSpyService).getApplicationContext(); + ExtendedMockito.doReturn(TimeUnit.HOURS.toMillis(4)) + .when(AdServicesConfig::getMeasurementEventMainReportingJobPeriodMs); + ExtendedMockito.doReturn(TimeUnit.HOURS.toMillis(24)) + .when(AdServicesConfig::getMeasurementEventFallbackReportingJobPeriodMs); + ExtendedMockito.doReturn(mock(EnrollmentDao.class)) + .when(() -> EnrollmentDao.getInstance(any())); ExtendedMockito.doReturn(mMockDatastoreManager) .when(() -> DatastoreManagerFactory.getDatastoreManager(any())); ExtendedMockito.doNothing() @@ -282,10 +311,10 @@ public class EventFallbackReportingJobServiceTest { } private void toggleKillSwitch(boolean value) { - DeviceConfig.setProperty( - DeviceConfig.NAMESPACE_ADSERVICES, - "measurement_job_event_fallback_reporting_kill_switch", - Boolean.toString(value), - /* makeDefault */ false); + Flags mockFlags = Mockito.mock(Flags.class); + ExtendedMockito.doReturn(mockFlags).when(FlagsFactory::getFlags); + ExtendedMockito.doReturn(value) + .when(mockFlags) + .getMeasurementJobEventFallbackReportingKillSwitch(); } } |