|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 package org.mozilla.gecko.background.announcements; |
|
5 |
|
6 import java.util.concurrent.BrokenBarrierException; |
|
7 |
|
8 import org.mozilla.gecko.background.common.GlobalConstants; |
|
9 import org.mozilla.gecko.background.helpers.BackgroundServiceTestCase; |
|
10 |
|
11 import android.content.Intent; |
|
12 import android.content.SharedPreferences; |
|
13 |
|
14 public class TestAnnouncementsBroadcastService |
|
15 extends BackgroundServiceTestCase<TestAnnouncementsBroadcastService.MockAnnouncementsBroadcastService> { |
|
16 public static class MockAnnouncementsBroadcastService extends AnnouncementsBroadcastService { |
|
17 @Override |
|
18 protected SharedPreferences getSharedPreferences() { |
|
19 return this.getSharedPreferences(sharedPrefsName, |
|
20 GlobalConstants.SHARED_PREFERENCES_MODE); |
|
21 } |
|
22 |
|
23 @Override |
|
24 protected void onHandleIntent(Intent intent) { |
|
25 super.onHandleIntent(intent); |
|
26 try { |
|
27 barrier.await(); |
|
28 } catch (InterruptedException e) { |
|
29 fail("Awaiting thread should not be interrupted."); |
|
30 } catch (BrokenBarrierException e) { |
|
31 // This will happen on timeout - do nothing. |
|
32 } |
|
33 } |
|
34 } |
|
35 |
|
36 public TestAnnouncementsBroadcastService() { |
|
37 super(MockAnnouncementsBroadcastService.class); |
|
38 } |
|
39 |
|
40 @Override |
|
41 public void setUp() throws Exception { |
|
42 super.setUp(); |
|
43 // We can't mock AlarmManager since it has a package-private constructor, so instead we reset |
|
44 // the alarm by hand. |
|
45 cancelAlarm(getServiceIntent()); |
|
46 } |
|
47 |
|
48 @Override |
|
49 public void tearDown() throws Exception { |
|
50 cancelAlarm(getServiceIntent()); |
|
51 AnnouncementsConstants.DISABLED = false; |
|
52 super.tearDown(); |
|
53 } |
|
54 |
|
55 protected Intent getServiceIntent() { |
|
56 final Intent intent = new Intent(getContext(), AnnouncementsService.class); |
|
57 return intent; |
|
58 } |
|
59 |
|
60 public void testIgnoredServicePrefIntents() throws Exception { |
|
61 // Intent without "enabled" extra is ignored. |
|
62 intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF); |
|
63 startService(intent); |
|
64 await(); |
|
65 |
|
66 assertFalse(isServiceAlarmSet(getServiceIntent())); |
|
67 } |
|
68 |
|
69 public void testServicePrefIntentDisabled() throws Exception { |
|
70 intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF) |
|
71 .putExtra("enabled", false); |
|
72 startService(intent); |
|
73 await(); |
|
74 assertFalse(isServiceAlarmSet(getServiceIntent())); |
|
75 } |
|
76 |
|
77 public void testServicePrefIntentEnabled() throws Exception { |
|
78 intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF) |
|
79 .putExtra("enabled", true); |
|
80 startService(intent); |
|
81 await(); |
|
82 assertTrue(isServiceAlarmSet(getServiceIntent())); |
|
83 } |
|
84 |
|
85 public void testServicePrefCancelled() throws Exception { |
|
86 intent.setAction(AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF) |
|
87 .putExtra("enabled", true); |
|
88 startService(intent); |
|
89 await(); |
|
90 |
|
91 assertTrue(isServiceAlarmSet(getServiceIntent())); |
|
92 barrier.reset(); |
|
93 |
|
94 intent.putExtra("enabled", false); |
|
95 startService(intent); |
|
96 await(); |
|
97 assertFalse(isServiceAlarmSet(getServiceIntent())); |
|
98 } |
|
99 } |