• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 
4 import android.app.Activity;
5 import android.app.AlarmManager;
6 import android.app.PendingIntent;
7 import android.content.Context;
8 import android.content.Intent;
9 import com.xtremelabs.robolectric.Robolectric;
10 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
11 import org.junit.After;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 
16 import java.util.Date;
17 
18 import static junit.framework.Assert.assertEquals;
19 import static org.hamcrest.CoreMatchers.*;
20 import static org.junit.Assert.assertThat;
21 
22 @RunWith(WithTestDefaultsRunner.class)
23 public class AlarmManagerTest {
24 
25     private MyActivity activity;
26     private AlarmManager alarmManager;
27     private ShadowAlarmManager shadowAlarmManager;
28 
29     @Before
setUp()30     public void setUp() throws Exception {
31         activity = new MyActivity();
32         alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
33         shadowAlarmManager = Robolectric.shadowOf(alarmManager);
34     }
35 
36     @After
tearDown()37     public void tearDown() throws Exception {
38     }
39 
40     @Test
shouldSupportSet()41     public void shouldSupportSet() throws Exception {
42         assertThat(shadowAlarmManager.getNextScheduledAlarm(), nullValue());
43         alarmManager.set(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
44 
45         ShadowAlarmManager.ScheduledAlarm scheduledAlarm = shadowAlarmManager.getNextScheduledAlarm();
46         assertThat(scheduledAlarm, notNullValue());
47     }
48 
49     @Test
shouldSupportSetRepeating()50     public void shouldSupportSetRepeating() throws Exception {
51         assertThat(shadowAlarmManager.getNextScheduledAlarm(), nullValue());
52         alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, AlarmManager.INTERVAL_HOUR,
53                                   PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
54 
55         ShadowAlarmManager.ScheduledAlarm scheduledAlarm = shadowAlarmManager.getNextScheduledAlarm();
56         assertThat(scheduledAlarm, notNullValue());
57     }
58     @Test
setShouldReplaceDuplicates()59     public void setShouldReplaceDuplicates() {
60         alarmManager.set(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
61         alarmManager.set(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
62         assertEquals(1, shadowAlarmManager.getScheduledAlarms().size());
63     }
64     @Test
setRepeatingShouldReplaceDuplicates()65     public void setRepeatingShouldReplaceDuplicates() {
66         alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, AlarmManager.INTERVAL_HOUR, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
67         alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, AlarmManager.INTERVAL_HOUR, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
68         assertEquals(1, shadowAlarmManager.getScheduledAlarms().size());
69     }
70 
71     @Test
shouldSupportGetNextScheduledAlarm()72     public void shouldSupportGetNextScheduledAlarm() throws Exception {
73         assertThat(shadowAlarmManager.getNextScheduledAlarm(), nullValue());
74 
75         long now = new Date().getTime();
76         PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0);
77         alarmManager.set(AlarmManager.ELAPSED_REALTIME, now, pendingIntent);
78 
79         ShadowAlarmManager.ScheduledAlarm scheduledAlarm = shadowAlarmManager.getNextScheduledAlarm();
80         assertThat(shadowAlarmManager.getNextScheduledAlarm(), nullValue());
81         assertScheduledAlarm(now, pendingIntent, scheduledAlarm);
82     }
83 
84     @Test
shouldSupportGetNextScheduledAlarmForRepeatingAlarms()85     public void shouldSupportGetNextScheduledAlarmForRepeatingAlarms() throws Exception {
86         assertThat(shadowAlarmManager.getNextScheduledAlarm(), nullValue());
87 
88         long now = new Date().getTime();
89         PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0);
90         alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, now, AlarmManager.INTERVAL_HOUR, pendingIntent);
91 
92         ShadowAlarmManager.ScheduledAlarm scheduledAlarm = shadowAlarmManager.getNextScheduledAlarm();
93         assertThat(shadowAlarmManager.getNextScheduledAlarm(), nullValue());
94         assertRepeatingScheduledAlarm(now, AlarmManager.INTERVAL_HOUR, pendingIntent, scheduledAlarm);
95     }
96 
97     @Test
shouldSupportPeekScheduledAlarm()98     public void shouldSupportPeekScheduledAlarm() throws Exception {
99         assertThat(shadowAlarmManager.getNextScheduledAlarm(), nullValue());
100 
101         long now = new Date().getTime();
102         PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0);
103         alarmManager.set(AlarmManager.ELAPSED_REALTIME, now, pendingIntent);
104 
105         ShadowAlarmManager.ScheduledAlarm scheduledAlarm = shadowAlarmManager.peekNextScheduledAlarm();
106         assertThat(shadowAlarmManager.peekNextScheduledAlarm(), notNullValue());
107         assertScheduledAlarm(now, pendingIntent, scheduledAlarm);
108     }
109 
110     @Test
cancel_removesMatchingPendingIntents()111     public void cancel_removesMatchingPendingIntents() {
112         Intent newIntent = new Intent(Robolectric.application.getApplicationContext(), String.class);
113         PendingIntent pendingIntent = PendingIntent.getBroadcast(Robolectric.application.getApplicationContext(), 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
114         alarmManager.set(AlarmManager.RTC, 1337, pendingIntent);
115 
116         Intent newIntent2 = new Intent(Robolectric.application.getApplicationContext(), Integer.class);
117         PendingIntent pendingIntent2 = PendingIntent.getBroadcast(Robolectric.application.getApplicationContext(), 0, newIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
118         alarmManager.set(AlarmManager.RTC, 1337, pendingIntent2);
119 
120         assertEquals(2, shadowAlarmManager.getScheduledAlarms().size());
121 
122         Intent newIntent3 = new Intent(Robolectric.application.getApplicationContext(), String.class);
123         PendingIntent newPendingIntent = PendingIntent.getBroadcast(Robolectric.application.getApplicationContext(), 0, newIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
124         alarmManager.cancel(newPendingIntent);
125         assertEquals(1, shadowAlarmManager.getScheduledAlarms().size());
126     }
127 
128     @Test
cancel_removesMatchingPendingIntentsWithActions()129     public void cancel_removesMatchingPendingIntentsWithActions() {
130         Intent newIntent = new Intent("someAction");
131         PendingIntent pendingIntent = PendingIntent.getBroadcast(Robolectric.application.getApplicationContext(), 0, newIntent, 0);
132         alarmManager.set(AlarmManager.RTC, 1337, pendingIntent);
133         assertEquals(1, shadowAlarmManager.getScheduledAlarms().size());
134         alarmManager.cancel(PendingIntent.getBroadcast(Robolectric.application, 0, new Intent("anotherAction"), 0));
135         assertEquals(1, shadowAlarmManager.getScheduledAlarms().size());
136         alarmManager.cancel(PendingIntent.getBroadcast(Robolectric.application, 0, new Intent("someAction"), 0));
137         assertEquals(0, shadowAlarmManager.getScheduledAlarms().size());
138     }
139 
assertScheduledAlarm(long now, PendingIntent pendingIntent, ShadowAlarmManager.ScheduledAlarm scheduledAlarm)140     private void assertScheduledAlarm(long now, PendingIntent pendingIntent,
141                                       ShadowAlarmManager.ScheduledAlarm scheduledAlarm) {
142 	    assertRepeatingScheduledAlarm(now, 0L, pendingIntent, scheduledAlarm);
143     }
144 
assertRepeatingScheduledAlarm(long now, long interval, PendingIntent pendingIntent, ShadowAlarmManager.ScheduledAlarm scheduledAlarm)145     private void assertRepeatingScheduledAlarm(long now, long interval, PendingIntent pendingIntent,
146                                       ShadowAlarmManager.ScheduledAlarm scheduledAlarm) {
147         assertThat(scheduledAlarm, notNullValue());
148         assertThat(scheduledAlarm.operation, notNullValue());
149         assertThat(scheduledAlarm.operation, sameInstance(pendingIntent));
150         assertThat(scheduledAlarm.type, equalTo(AlarmManager.ELAPSED_REALTIME));
151         assertThat(scheduledAlarm.triggerAtTime, equalTo(now));
152         assertThat(scheduledAlarm.interval, equalTo(interval));
153     }
154 
155     private static class MyActivity extends Activity {
156         @Override
onDestroy()157         protected void onDestroy() {
158             super.onDestroy();
159         }
160     }
161 
162 }
163