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:
Raff Tsai
2019-08-02 18:14:13 +08:00
parent c078ee3b5d
commit d68802cf6d
3 changed files with 18 additions and 5 deletions

View File

@@ -27,10 +27,13 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.JobSchedulerImpl;
import android.app.job.IJobScheduler;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.content.Context;
import android.os.Binder;
import com.android.settings.R;
import com.android.settings.testutils.DatabaseTestUtils;
@@ -67,7 +70,7 @@ public class AnomalyCleanupJobServiceTest {
MockitoAnnotations.initMocks(this);
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);
}

View File

@@ -30,10 +30,13 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.RuntimeEnvironment.application;
import android.app.JobSchedulerImpl;
import android.app.StatsManager;
import android.app.job.IJobScheduler;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.Context;
import android.os.Binder;
import android.provider.Settings;
import com.android.settings.R;
@@ -66,7 +69,7 @@ public class AnomalyConfigJobServiceTest {
MockitoAnnotations.initMocks(this);
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);
mJobService = spy(new AnomalyConfigJobService());

View File

@@ -38,7 +38,9 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.RuntimeEnvironment.application;
import android.app.JobSchedulerImpl;
import android.app.StatsManager;
import android.app.job.IJobScheduler;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
@@ -46,6 +48,7 @@ import android.app.job.JobWorkItem;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Process;
import android.os.StatsDimensionsValue;
@@ -105,12 +108,16 @@ public class AnomalyDetectionJobServiceTest {
private AnomalyDetectionJobService mAnomalyDetectionJobService;
private FakeFeatureFactory mFeatureFactory;
private Context mContext;
private JobScheduler mJobScheduler;
@Before
public void setUp() {
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);
mBundle = new Bundle();
mBundle.putParcelable(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, mStatsDimensionsValue);
@@ -125,9 +132,9 @@ public class AnomalyDetectionJobServiceTest {
@Test
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();
assertThat(pendingJobs).hasSize(1);