Bug 1476237 - Fix TestTelemetryUploadAllPingsImmediatelyScheduler the after Oreo migration; r=jchen

Refactored the previous test which verified if a Service was started with the
right Intent to now check if JobScheduler has the right Job enqueued with the
right work Intent.


MozReview-Commit-ID: G46GbjVVkqR

--HG--
extra : rebase_source : 4d9f030ed37767c02a27aa291f2c0338afd0619a
This commit is contained in:
Petru Lingurar 2018-07-18 15:21:03 +03:00
Родитель a66ba67aae
Коммит 8e537fb53f
1 изменённых файлов: 49 добавлений и 13 удалений

Просмотреть файл

@ -6,8 +6,12 @@
package org.mozilla.gecko.telemetry.schedulers; package org.mozilla.gecko.telemetry.schedulers;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.app.job.JobWorkItem;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -15,9 +19,19 @@ import org.mockito.ArgumentCaptor;
import org.mozilla.gecko.telemetry.TelemetryUploadService; import org.mozilla.gecko.telemetry.TelemetryUploadService;
import org.mozilla.gecko.telemetry.stores.TelemetryPingStore; import org.mozilla.gecko.telemetry.stores.TelemetryPingStore;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static junit.framework.Assert.*; import java.util.List;
import static org.mockito.Mockito.*; import java.util.Objects;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** /**
* Unit tests for the upload immediately scheduler. * Unit tests for the upload immediately scheduler.
@ -26,6 +40,7 @@ import static org.mockito.Mockito.*;
* (e.g. pass in current time) and these tests will be more useful. * (e.g. pass in current time) and these tests will be more useful.
*/ */
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
public class TestTelemetryUploadAllPingsImmediatelyScheduler { public class TestTelemetryUploadAllPingsImmediatelyScheduler {
private TelemetryUploadAllPingsImmediatelyScheduler testScheduler; private TelemetryUploadAllPingsImmediatelyScheduler testScheduler;
@ -39,22 +54,43 @@ public class TestTelemetryUploadAllPingsImmediatelyScheduler {
@Test @Test
public void testReadyToUpload() { public void testReadyToUpload() {
assertTrue("Scheduler is always ready to upload", testScheduler.isReadyToUpload( assertTrue("Scheduler is not ready to upload",
mock(Context.class), testStore)); testScheduler.isReadyToUpload(mock(Context.class), testStore));
} }
@Test @Test
public void testScheduleUpload() { public void testScheduleUpload() {
final Context context = mock(Context.class); final Context appContext = spy(getAppContext());
final JobScheduler scheduler = spy(getJobScheduler());
// Make sure that the internal system call to enqueue work will use our mocks
when(appContext.getApplicationContext()).thenReturn(appContext);
when(appContext.getSystemService(Context.JOB_SCHEDULER_SERVICE)).thenReturn(scheduler);
testScheduler.scheduleUpload(context, testStore); testScheduler.scheduleUpload(appContext, testStore);
final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); final List<JobInfo> pendingJobs = scheduler.getAllPendingJobs();
verify(context).startService(intentCaptor.capture()); assertEquals("Telemetry upload is not not started", pendingJobs.size(), 1);
final Intent actualIntent = intentCaptor.getValue();
assertEquals("Intent action is upload", TelemetryUploadService.ACTION_UPLOAD, actualIntent.getAction()); final JobInfo uploadJob = pendingJobs.get(0);
assertTrue("Intent contains store", actualIntent.hasExtra(TelemetryUploadService.EXTRA_STORE)); assertEquals("Enqueued class is not the telemetry upload service",
assertEquals("Intent class target is upload service", TelemetryUploadService.class.getName(), uploadJob.getService().getClassName());
TelemetryUploadService.class.getName(), actualIntent.getComponent().getClassName());
final ArgumentCaptor<JobWorkItem> workCaptor = ArgumentCaptor.forClass(JobWorkItem.class);
verify(scheduler).enqueue(any(JobInfo.class), workCaptor.capture());
final Intent receivedIntent = workCaptor.getValue().getIntent();
assertEquals("Work Intent has wrong action",
TelemetryUploadService.ACTION_UPLOAD, receivedIntent.getAction());
assertTrue("Work Intent has wrong extra",
receivedIntent.hasExtra(TelemetryUploadService.EXTRA_STORE));
}
private Context getAppContext() {
return RuntimeEnvironment.application;
}
private JobScheduler getJobScheduler() {
return Objects.requireNonNull((JobScheduler)
getAppContext().getSystemService(Context.JOB_SCHEDULER_SERVICE));
} }
} }