1 package com.xtremelabs.robolectric.shadows;
2 
3 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
4 import static org.hamcrest.CoreMatchers.equalTo;
5 import static org.hamcrest.CoreMatchers.is;
6 import static org.hamcrest.CoreMatchers.not;
7 import static org.hamcrest.CoreMatchers.sameInstance;
8 import static org.junit.Assert.assertNull;
9 import static org.junit.Assert.assertThat;
10 
11 import android.app.Activity;
12 import android.app.PendingIntent;
13 import android.content.Intent;
14 import android.content.IntentSender;
15 import android.content.TestIntentSender;
16 import android.os.Parcel;
17 
18 import com.xtremelabs.robolectric.Robolectric;
19 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
20 
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 
24 @RunWith(WithTestDefaultsRunner.class)
25 public class PendingIntentTest {
26     @Test
shouldGetIntentSender()27     public void shouldGetIntentSender() {
28         Intent expectedIntent = new Intent();
29         PendingIntent service = PendingIntent.getService(null, 0, expectedIntent, 0);
30 
31         IntentSender intentSender = service.getIntentSender();
32         assertThat(expectedIntent, equalTo(((TestIntentSender) intentSender).intent));
33     }
34 
35     @Test
getBroadcast__shouldCreateIntentForBroadcast()36     public void getBroadcast__shouldCreateIntentForBroadcast() throws Exception {
37         Intent intent = new Intent();
38         PendingIntent pendingIntent = PendingIntent.getBroadcast(Robolectric.application, 99, intent, 100);
39         ShadowPendingIntent shadow = shadowOf(pendingIntent);
40         assertThat(shadow.isActivityIntent(), is(false));
41         assertThat(shadow.isBroadcastIntent(), is(true));
42         assertThat(shadow.isServiceIntent(), is(false));
43         assertThat(intent, equalTo(shadow.getSavedIntent()));
44         assertThat(Robolectric.application, equalTo(shadow.getSavedContext()));
45     }
46 
47     @Test
getActivity__shouldCreateIntentForBroadcast()48     public void getActivity__shouldCreateIntentForBroadcast() throws Exception {
49         Intent intent = new Intent();
50         PendingIntent pendingIntent = PendingIntent.getActivity(Robolectric.application, 99, intent, 100);
51         ShadowPendingIntent shadow = shadowOf(pendingIntent);
52         assertThat(shadow.isActivityIntent(), is(true));
53         assertThat(shadow.isBroadcastIntent(), is(false));
54         assertThat(shadow.isServiceIntent(), is(false));
55         assertThat(intent, equalTo(shadow.getSavedIntent()));
56         assertThat(Robolectric.application, equalTo(shadow.getSavedContext()));
57     }
58 
59     @Test
getService__shouldCreateIntentForBroadcast()60     public void getService__shouldCreateIntentForBroadcast() throws Exception {
61         Intent intent = new Intent();
62         PendingIntent pendingIntent = PendingIntent.getService(Robolectric.application, 99, intent, 100);
63         ShadowPendingIntent shadow = shadowOf(pendingIntent);
64         assertThat(shadow.isActivityIntent(), is(false));
65         assertThat(shadow.isBroadcastIntent(), is(false));
66         assertThat(shadow.isServiceIntent(), is(true));
67         assertThat(intent, equalTo(shadow.getSavedIntent()));
68         assertThat(Robolectric.application, equalTo(shadow.getSavedContext()));
69     }
70 
71     @Test
send__shouldFillInIntentData()72     public void send__shouldFillInIntentData() throws Exception {
73         Intent intent = new Intent();
74         Activity context = new Activity();
75         PendingIntent forActivity = PendingIntent.getActivity(context, 99, intent, 100);
76 
77         Activity otherContext = new Activity();
78         Intent fillIntent = new Intent();
79         fillIntent.putExtra("TEST", 23);
80         forActivity.send(otherContext, 0, fillIntent);
81 
82         Intent i = shadowOf(otherContext).getNextStartedActivity();
83         assertThat(i, sameInstance(intent));
84         assertThat(i.getIntExtra("TEST", -1), equalTo(23));
85     }
86 
87     @Test
testEquals()88     public void testEquals() throws Exception {
89         PendingIntent pi1 = PendingIntent.getActivity(Robolectric.application, 99,
90             new Intent("action"), 100);
91         PendingIntent pi2 = PendingIntent.getActivity(null, 99, new Intent("action"), 100);
92         PendingIntent pi3 = PendingIntent.getService(Robolectric.application, 99,
93             new Intent("action"), 100);
94         assertThat(pi1, equalTo(pi2));
95         assertThat(pi1, not(equalTo(pi3)));
96     }
97 
98     @Test
parcelIo_nullPendingIntent()99     public void parcelIo_nullPendingIntent() {
100         verifyPendingIntentReadIsWhatWasWrittenToParcel(null);
101     }
102 
103     @Test
parcelIo_shouldGetBackBroadcastIntentWrittenToParcelWithNullIntent()104     public void parcelIo_shouldGetBackBroadcastIntentWrittenToParcelWithNullIntent() {
105         verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent
106             .getBroadcast(Robolectric.application, 99, null, 100));
107     }
108 
109     @Test
parcelIo_shouldGetBackBroadcastIntentWrittenToParcel()110     public void parcelIo_shouldGetBackBroadcastIntentWrittenToParcel() {
111       verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent
112           .getBroadcast(Robolectric.application, 99, new Intent(), 100));
113     }
114 
115     @Test
parcelIo_shouldGetBackActivityIntentWrittenToParcel()116     public void parcelIo_shouldGetBackActivityIntentWrittenToParcel() {
117         verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent
118             .getActivity(Robolectric.application, 99, new Intent(), 100));
119     }
120 
121     @Test
parcelIo_shouldGetBackServiceIntentWrittenToParcel()122     public void parcelIo_shouldGetBackServiceIntentWrittenToParcel() {
123         verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent
124             .getService(Robolectric.application, 99, new Intent(), 100));
125     }
126 
verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent expected)127     private void verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent expected) {
128         Parcel parcel = Parcel.obtain();
129         PendingIntent.writePendingIntentOrNullToParcel(expected, parcel);
130         parcel.setDataPosition(0);
131         PendingIntent actual = PendingIntent.readPendingIntentOrNullFromParcel(parcel);
132         if (expected == null) {
133             assertNull(actual);
134         } else {
135             assertThat(expected, equalTo(actual));
136         }
137     }
138 }
139