summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventReportingJobServiceTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventReportingJobServiceTest.java')
-rw-r--r--adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventReportingJobServiceTest.java75
1 files changed, 50 insertions, 25 deletions
diff --git a/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventReportingJobServiceTest.java b/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventReportingJobServiceTest.java
index bfae948a64..4cee88d060 100644
--- a/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventReportingJobServiceTest.java
+++ b/adservices/tests/unittest/service-core/src/com/android/adservices/service/measurement/reporting/EventReportingJobServiceTest.java
@@ -19,6 +19,7 @@ package com.android.adservices.service.measurement.reporting;
import static com.android.adservices.service.AdServicesConfig.MEASUREMENT_EVENT_MAIN_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,32 +36,30 @@ 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 EventReportingJobService
*/
public class EventReportingJobServiceTest {
- // 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 DatastoreManager mMockDatastoreManager;
@@ -77,10 +76,11 @@ public class EventReportingJobServiceTest {
@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 EventReportingJobServiceTest {
@Test
public void onStartJob_killSwitchOff() throws Exception {
- disableKillSwitch();
-
runWithMocks(
() -> {
// Setup
+ disableKillSwitch();
+
ExtendedMockito.doNothing()
.when(
() ->
@@ -124,11 +124,11 @@ public class EventReportingJobServiceTest {
@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 EventReportingJobServiceTest {
@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 EventReportingJobServiceTest {
@Test
public void scheduleIfNeeded_killSwitchOff_previouslyExecuted_forceSchedule_schedule()
throws Exception {
- disableKillSwitch();
-
runWithMocks(
() -> {
// Setup
+ disableKillSwitch();
+
final Context mockContext = mock(Context.class);
doReturn(mMockJobScheduler)
.when(mockContext)
@@ -217,11 +217,11 @@ public class EventReportingJobServiceTest {
@Test
public void scheduleIfNeeded_killSwitchOff_previouslyNotExecuted_dontForceSchedule_schedule()
throws Exception {
- disableKillSwitch();
-
runWithMocks(
() -> {
// Setup
+ disableKillSwitch();
+
final Context mockContext = mock(Context.class);
doReturn(mMockJobScheduler)
.when(mockContext)
@@ -243,11 +243,34 @@ public class EventReportingJobServiceTest {
});
}
+ @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(() -> EventReportingJobService.schedule(any(), any()));
+ EventReportingJobService.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(EventReportingJobService.class)
+ .spyStatic(FlagsFactory.class)
.strictness(Strictness.LENIENT)
.startMocking();
try {
@@ -259,6 +282,10 @@ public class EventReportingJobServiceTest {
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(mock(EnrollmentDao.class))
+ .when(() -> EnrollmentDao.getInstance(any()));
ExtendedMockito.doReturn(mMockDatastoreManager)
.when(() -> DatastoreManagerFactory.getDatastoreManager(any()));
ExtendedMockito.doNothing().when(() -> EventReportingJobService.schedule(any(), any()));
@@ -279,10 +306,8 @@ public class EventReportingJobServiceTest {
}
private void toggleKillSwitch(boolean value) {
- DeviceConfig.setProperty(
- DeviceConfig.NAMESPACE_ADSERVICES,
- "measurement_job_event_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).getMeasurementJobEventReportingKillSwitch();
}
}