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