From d68802cf6d0fb066ba956d1ef9768353482141fe Mon Sep 17 00:00:00 2001 From: Raff Tsai Date: Fri, 2 Aug 2019 18:14:13 +0800 Subject: [PATCH] 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 --- .../batterytip/AnomalyCleanupJobServiceTest.java | 5 ++++- .../batterytip/AnomalyConfigJobServiceTest.java | 5 ++++- .../batterytip/AnomalyDetectionJobServiceTest.java | 13 ++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanupJobServiceTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanupJobServiceTest.java index a2250697f4d..6f1e37a0393 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanupJobServiceTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyCleanupJobServiceTest.java @@ -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); } diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigJobServiceTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigJobServiceTest.java index 30cab77f6bc..c6aaa3240fb 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigJobServiceTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigJobServiceTest.java @@ -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()); diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java index ba67793cc05..e839717b8b8 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobServiceTest.java @@ -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 pendingJobs = jobScheduler.getAllPendingJobs(); assertThat(pendingJobs).hasSize(1);