|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 package org.mozilla.gecko.background.healthreport; |
|
5 |
|
6 import java.util.concurrent.BrokenBarrierException; |
|
7 |
|
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; |
|
12 |
|
13 import android.content.Intent; |
|
14 import android.content.SharedPreferences; |
|
15 |
|
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 } |
|
23 |
|
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 } |
|
36 |
|
37 public TestHealthReportBroadcastService() { |
|
38 super(MockHealthReportBroadcastService.class); |
|
39 } |
|
40 |
|
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 } |
|
48 |
|
49 @Override |
|
50 public void tearDown() throws Exception { |
|
51 cancelAlarm(getUploadIntent()); |
|
52 super.tearDown(); |
|
53 } |
|
54 |
|
55 protected Intent getUploadIntent() { |
|
56 final Intent intent = new Intent(getContext(), HealthReportUploadService.class); |
|
57 intent.setAction("upload"); |
|
58 return intent; |
|
59 } |
|
60 |
|
61 protected Intent getPruneIntent() { |
|
62 final Intent intent = new Intent(getContext(), HealthReportPruneService.class); |
|
63 intent.setAction("prune"); |
|
64 return intent; |
|
65 } |
|
66 |
|
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(); |
|
74 |
|
75 assertFalse(isServiceAlarmSet(getUploadIntent())); |
|
76 barrier.reset(); |
|
77 |
|
78 // No "profileName" extra. |
|
79 intent.putExtra("enabled", true) |
|
80 .removeExtra("profileName"); |
|
81 startService(intent); |
|
82 await(); |
|
83 |
|
84 assertFalse(isServiceAlarmSet(getUploadIntent())); |
|
85 barrier.reset(); |
|
86 |
|
87 // No "profilePath" extra. |
|
88 intent.putExtra("profileName", "profileName") |
|
89 .removeExtra("profilePath"); |
|
90 startService(intent); |
|
91 await(); |
|
92 |
|
93 assertFalse(isServiceAlarmSet(getUploadIntent())); |
|
94 } |
|
95 |
|
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(); |
|
103 |
|
104 assertFalse(isServiceAlarmSet(getUploadIntent())); |
|
105 } |
|
106 |
|
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(); |
|
114 |
|
115 assertTrue(isServiceAlarmSet(getUploadIntent())); |
|
116 } |
|
117 |
|
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(); |
|
125 |
|
126 assertTrue(isServiceAlarmSet(getUploadIntent())); |
|
127 barrier.reset(); |
|
128 |
|
129 intent.putExtra("enabled", false); |
|
130 startService(intent); |
|
131 await(); |
|
132 |
|
133 assertFalse(isServiceAlarmSet(getUploadIntent())); |
|
134 } |
|
135 |
|
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 } |