1package com.xtremelabs.robolectric.shadows;
2
3import static com.xtremelabs.robolectric.Robolectric.shadowOf;
4import static org.hamcrest.CoreMatchers.equalTo;
5import static org.hamcrest.CoreMatchers.is;
6import static org.hamcrest.CoreMatchers.not;
7import static org.hamcrest.CoreMatchers.sameInstance;
8import static org.junit.Assert.assertNull;
9import static org.junit.Assert.assertThat;
10
11import android.app.Activity;
12import android.app.PendingIntent;
13import android.content.Intent;
14import android.content.IntentSender;
15import android.content.TestIntentSender;
16import android.os.Parcel;
17
18import com.xtremelabs.robolectric.Robolectric;
19import com.xtremelabs.robolectric.WithTestDefaultsRunner;
20
21import org.junit.Test;
22import org.junit.runner.RunWith;
23
24@RunWith(WithTestDefaultsRunner.class)
25public class PendingIntentTest {
26    @Test
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
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
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
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
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
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
99    public void parcelIo_nullPendingIntent() {
100        verifyPendingIntentReadIsWhatWasWrittenToParcel(null);
101    }
102
103    @Test
104    public void parcelIo_shouldGetBackBroadcastIntentWrittenToParcelWithNullIntent() {
105        verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent
106            .getBroadcast(Robolectric.application, 99, null, 100));
107    }
108
109    @Test
110    public void parcelIo_shouldGetBackBroadcastIntentWrittenToParcel() {
111      verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent
112          .getBroadcast(Robolectric.application, 99, new Intent(), 100));
113    }
114
115    @Test
116    public void parcelIo_shouldGetBackActivityIntentWrittenToParcel() {
117        verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent
118            .getActivity(Robolectric.application, 99, new Intent(), 100));
119    }
120
121    @Test
122    public void parcelIo_shouldGetBackServiceIntentWrittenToParcel() {
123        verifyPendingIntentReadIsWhatWasWrittenToParcel(PendingIntent
124            .getService(Robolectric.application, 99, new Intent(), 100));
125    }
126
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