Fix robolectric test error
jobService is moved to mainline module. It is not registered in the system at the beginning, therefore getSystemService returns null. Instead of getSystemService, directly create a JobSchedulerImpl and mock getSystemService method to return the JobSchedulerImpl instance. Fixes: 138714772 Test: make RunSettingsRoboTests Change-Id: If91b6a63c0de92530d69f7b80515669352aab760
This commit is contained in:
@@ -27,10 +27,13 @@ import static org.mockito.Mockito.times;
|
|||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.JobSchedulerImpl;
|
||||||
|
import android.app.job.IJobScheduler;
|
||||||
import android.app.job.JobInfo;
|
import android.app.job.JobInfo;
|
||||||
import android.app.job.JobParameters;
|
import android.app.job.JobParameters;
|
||||||
import android.app.job.JobScheduler;
|
import android.app.job.JobScheduler;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Binder;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.testutils.DatabaseTestUtils;
|
import com.android.settings.testutils.DatabaseTestUtils;
|
||||||
@@ -67,7 +70,7 @@ public class AnomalyCleanupJobServiceTest {
|
|||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mJobScheduler = spy(mContext.getSystemService(JobScheduler.class));
|
mJobScheduler = spy(new JobSchedulerImpl(IJobScheduler.Stub.asInterface(new Binder())));
|
||||||
when(mContext.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler);
|
when(mContext.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -30,10 +30,13 @@ import static org.mockito.Mockito.verify;
|
|||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.robolectric.RuntimeEnvironment.application;
|
import static org.robolectric.RuntimeEnvironment.application;
|
||||||
|
|
||||||
|
import android.app.JobSchedulerImpl;
|
||||||
import android.app.StatsManager;
|
import android.app.StatsManager;
|
||||||
|
import android.app.job.IJobScheduler;
|
||||||
import android.app.job.JobInfo;
|
import android.app.job.JobInfo;
|
||||||
import android.app.job.JobScheduler;
|
import android.app.job.JobScheduler;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Binder;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -66,7 +69,7 @@ public class AnomalyConfigJobServiceTest {
|
|||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mJobScheduler = spy(mContext.getSystemService(JobScheduler.class));
|
mJobScheduler = spy(new JobSchedulerImpl(IJobScheduler.Stub.asInterface(new Binder())));
|
||||||
when(mContext.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler);
|
when(mContext.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler);
|
||||||
|
|
||||||
mJobService = spy(new AnomalyConfigJobService());
|
mJobService = spy(new AnomalyConfigJobService());
|
||||||
|
@@ -38,7 +38,9 @@ import static org.mockito.Mockito.verify;
|
|||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.robolectric.RuntimeEnvironment.application;
|
import static org.robolectric.RuntimeEnvironment.application;
|
||||||
|
|
||||||
|
import android.app.JobSchedulerImpl;
|
||||||
import android.app.StatsManager;
|
import android.app.StatsManager;
|
||||||
|
import android.app.job.IJobScheduler;
|
||||||
import android.app.job.JobInfo;
|
import android.app.job.JobInfo;
|
||||||
import android.app.job.JobParameters;
|
import android.app.job.JobParameters;
|
||||||
import android.app.job.JobScheduler;
|
import android.app.job.JobScheduler;
|
||||||
@@ -46,6 +48,7 @@ import android.app.job.JobWorkItem;
|
|||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.os.Binder;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Process;
|
import android.os.Process;
|
||||||
import android.os.StatsDimensionsValue;
|
import android.os.StatsDimensionsValue;
|
||||||
@@ -105,12 +108,16 @@ public class AnomalyDetectionJobServiceTest {
|
|||||||
private AnomalyDetectionJobService mAnomalyDetectionJobService;
|
private AnomalyDetectionJobService mAnomalyDetectionJobService;
|
||||||
private FakeFeatureFactory mFeatureFactory;
|
private FakeFeatureFactory mFeatureFactory;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private JobScheduler mJobScheduler;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
mJobScheduler = spy(new JobSchedulerImpl(IJobScheduler.Stub.asInterface(new Binder())));
|
||||||
|
when(mContext.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler);
|
||||||
|
|
||||||
mPolicy = new BatteryTipPolicy(mContext);
|
mPolicy = new BatteryTipPolicy(mContext);
|
||||||
mBundle = new Bundle();
|
mBundle = new Bundle();
|
||||||
mBundle.putParcelable(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, mStatsDimensionsValue);
|
mBundle.putParcelable(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, mStatsDimensionsValue);
|
||||||
@@ -125,9 +132,9 @@ public class AnomalyDetectionJobServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void scheduleCleanUp() {
|
public void scheduleCleanUp() {
|
||||||
AnomalyDetectionJobService.scheduleAnomalyDetection(application, new Intent());
|
AnomalyDetectionJobService.scheduleAnomalyDetection(mContext, new Intent());
|
||||||
|
|
||||||
JobScheduler jobScheduler = application.getSystemService(JobScheduler.class);
|
JobScheduler jobScheduler = mContext.getSystemService(JobScheduler.class);
|
||||||
List<JobInfo> pendingJobs = jobScheduler.getAllPendingJobs();
|
List<JobInfo> pendingJobs = jobScheduler.getAllPendingJobs();
|
||||||
assertThat(pendingJobs).hasSize(1);
|
assertThat(pendingJobs).hasSize(1);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user