mobile/android/tests/background/junit3/src/healthreport/TestHealthReportBroadcastService.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 package org.mozilla.gecko.background.healthreport;
michael@0 5
michael@0 6 import java.util.concurrent.BrokenBarrierException;
michael@0 7
michael@0 8 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 9 import org.mozilla.gecko.background.healthreport.prune.HealthReportPruneService;
michael@0 10 import org.mozilla.gecko.background.healthreport.upload.HealthReportUploadService;
michael@0 11 import org.mozilla.gecko.background.helpers.BackgroundServiceTestCase;
michael@0 12
michael@0 13 import android.content.Intent;
michael@0 14 import android.content.SharedPreferences;
michael@0 15
michael@0 16 public class TestHealthReportBroadcastService
michael@0 17 extends BackgroundServiceTestCase<TestHealthReportBroadcastService.MockHealthReportBroadcastService> {
michael@0 18 public static class MockHealthReportBroadcastService extends HealthReportBroadcastService {
michael@0 19 @Override
michael@0 20 protected SharedPreferences getSharedPreferences() {
michael@0 21 return this.getSharedPreferences(sharedPrefsName, GlobalConstants.SHARED_PREFERENCES_MODE);
michael@0 22 }
michael@0 23
michael@0 24 @Override
michael@0 25 protected void onHandleIntent(Intent intent) {
michael@0 26 super.onHandleIntent(intent);
michael@0 27 try {
michael@0 28 barrier.await();
michael@0 29 } catch (InterruptedException e) {
michael@0 30 fail("Awaiting Service thread should not be interrupted.");
michael@0 31 } catch (BrokenBarrierException e) {
michael@0 32 // This will happen on timeout - do nothing.
michael@0 33 }
michael@0 34 }
michael@0 35 }
michael@0 36
michael@0 37 public TestHealthReportBroadcastService() {
michael@0 38 super(MockHealthReportBroadcastService.class);
michael@0 39 }
michael@0 40
michael@0 41 @Override
michael@0 42 public void setUp() throws Exception {
michael@0 43 super.setUp();
michael@0 44 // We can't mock AlarmManager since it has a package-private constructor, so instead we reset
michael@0 45 // the alarm by hand.
michael@0 46 cancelAlarm(getUploadIntent());
michael@0 47 }
michael@0 48
michael@0 49 @Override
michael@0 50 public void tearDown() throws Exception {
michael@0 51 cancelAlarm(getUploadIntent());
michael@0 52 super.tearDown();
michael@0 53 }
michael@0 54
michael@0 55 protected Intent getUploadIntent() {
michael@0 56 final Intent intent = new Intent(getContext(), HealthReportUploadService.class);
michael@0 57 intent.setAction("upload");
michael@0 58 return intent;
michael@0 59 }
michael@0 60
michael@0 61 protected Intent getPruneIntent() {
michael@0 62 final Intent intent = new Intent(getContext(), HealthReportPruneService.class);
michael@0 63 intent.setAction("prune");
michael@0 64 return intent;
michael@0 65 }
michael@0 66
michael@0 67 public void testIgnoredUploadPrefIntents() throws Exception {
michael@0 68 // Intent without "upload" extra is ignored.
michael@0 69 intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF)
michael@0 70 .putExtra("profileName", "profileName")
michael@0 71 .putExtra("profilePath", "profilePath");
michael@0 72 startService(intent);
michael@0 73 await();
michael@0 74
michael@0 75 assertFalse(isServiceAlarmSet(getUploadIntent()));
michael@0 76 barrier.reset();
michael@0 77
michael@0 78 // No "profileName" extra.
michael@0 79 intent.putExtra("enabled", true)
michael@0 80 .removeExtra("profileName");
michael@0 81 startService(intent);
michael@0 82 await();
michael@0 83
michael@0 84 assertFalse(isServiceAlarmSet(getUploadIntent()));
michael@0 85 barrier.reset();
michael@0 86
michael@0 87 // No "profilePath" extra.
michael@0 88 intent.putExtra("profileName", "profileName")
michael@0 89 .removeExtra("profilePath");
michael@0 90 startService(intent);
michael@0 91 await();
michael@0 92
michael@0 93 assertFalse(isServiceAlarmSet(getUploadIntent()));
michael@0 94 }
michael@0 95
michael@0 96 public void testUploadPrefIntentDisabled() throws Exception {
michael@0 97 intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF)
michael@0 98 .putExtra("enabled", false)
michael@0 99 .putExtra("profileName", "profileName")
michael@0 100 .putExtra("profilePath", "profilePath");
michael@0 101 startService(intent);
michael@0 102 await();
michael@0 103
michael@0 104 assertFalse(isServiceAlarmSet(getUploadIntent()));
michael@0 105 }
michael@0 106
michael@0 107 public void testUploadPrefIntentEnabled() throws Exception {
michael@0 108 intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF)
michael@0 109 .putExtra("enabled", true)
michael@0 110 .putExtra("profileName", "profileName")
michael@0 111 .putExtra("profilePath", "profilePath");
michael@0 112 startService(intent);
michael@0 113 await();
michael@0 114
michael@0 115 assertTrue(isServiceAlarmSet(getUploadIntent()));
michael@0 116 }
michael@0 117
michael@0 118 public void testUploadServiceCancelled() throws Exception {
michael@0 119 intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF)
michael@0 120 .putExtra("enabled", true)
michael@0 121 .putExtra("profileName", "profileName")
michael@0 122 .putExtra("profilePath", "profilePath");
michael@0 123 startService(intent);
michael@0 124 await();
michael@0 125
michael@0 126 assertTrue(isServiceAlarmSet(getUploadIntent()));
michael@0 127 barrier.reset();
michael@0 128
michael@0 129 intent.putExtra("enabled", false);
michael@0 130 startService(intent);
michael@0 131 await();
michael@0 132
michael@0 133 assertFalse(isServiceAlarmSet(getUploadIntent()));
michael@0 134 }
michael@0 135
michael@0 136 public void testPruneService() throws Exception {
michael@0 137 intent.setAction(HealthReportConstants.ACTION_HEALTHREPORT_PRUNE)
michael@0 138 .putExtra("profileName", "profileName")
michael@0 139 .putExtra("profilePath", "profilePath");
michael@0 140 startService(intent);
michael@0 141 await();
michael@0 142 assertTrue(isServiceAlarmSet(getPruneIntent()));
michael@0 143 barrier.reset();
michael@0 144 }
michael@0 145 }

mercurial