aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/shadows/IntentTest.java
blob: d491606c6146808a9c40b5b857b64798b80ccda3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package com.xtremelabs.robolectric.shadows;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

@RunWith(WithTestDefaultsRunner.class)
public class IntentTest {
    @Test
    public void testStringExtra() throws Exception {
        Intent intent = new Intent();
        assertSame(intent, intent.putExtra("foo", "bar"));
        assertEquals("bar", intent.getExtras().get("foo"));
    }

    @Test
    public void testCharSequenceExtra() throws Exception {
        Intent intent = new Intent();
        CharSequence cs = new TestCharSequence("bar");
        assertSame(intent, intent.putExtra("foo", cs));
        assertSame(cs, intent.getExtras().get("foo"));
    }

    @Test
    public void testIntExtra() throws Exception {
        Intent intent = new Intent();
        assertSame(intent, intent.putExtra("foo", 2));
        assertEquals(2, intent.getExtras().get("foo"));
        assertEquals(2, intent.getIntExtra("foo", -1));
    }

    @Test
    public void testDoubleExtra() throws Exception {
        Intent intent = new Intent();
        assertSame(intent, intent.putExtra("foo", 2d));
        assertEquals(2d, intent.getExtras().get("foo"));
        assertEquals(2d, intent.getDoubleExtra("foo", -1));
    }

    @Test
    public void testFloatExtra() throws Exception {
        Intent intent = new Intent();
        assertSame(intent, intent.putExtra("foo", 2f));
        assertEquals(2f, intent.getExtras().get("foo"));
        assertEquals(2f, intent.getFloatExtra("foo", -1));
    }

    @Test
    public void testIntArrayExtra() throws Exception {
        Intent intent = new Intent();
        int[] array = new int[2];
        array[0] = 1;
        array[1] = 2;
        assertSame(intent, intent.putExtra("foo", array));
        assertEquals(1, intent.getIntArrayExtra("foo")[0]);
        assertEquals(2, intent.getIntArrayExtra("foo")[1]);
    }

    @Test
    public void testLongArrayExtra() throws Exception {
        Intent intent = new Intent();
        long[] array = new long[2];
        array[0] = 1L;
        array[1] = 2L;
        assertSame(intent, intent.putExtra("foo", array));
        assertEquals(1L, intent.getLongArrayExtra("foo")[0]);
        assertEquals(2L, intent.getLongArrayExtra("foo")[1]);
    }

    @Test
    public void testSerializableExtra() throws Exception {
        Intent intent = new Intent();
        TestSerializable serializable = new TestSerializable("some string");
        assertSame(intent, intent.putExtra("foo", serializable));
        assertEquals(serializable, intent.getExtras().get("foo"));
        assertNotSame(serializable, intent.getExtras().get("foo"));
        assertEquals(serializable, intent.getSerializableExtra("foo"));
        assertNotSame(serializable, intent.getSerializableExtra("foo"));
    }

    @Test
    public void testParcelableExtra() throws Exception {
        Intent intent = new Intent();
        Parcelable parcelable = new TestParcelable(44);
        assertSame(intent, intent.putExtra("foo", parcelable));
        assertSame(parcelable, intent.getExtras().get("foo"));
        assertSame(parcelable, intent.getParcelableExtra("foo"));
    }

    @Test
    public void testParcelableArrayExtra() throws Exception {
        Intent intent = new Intent();
        Parcelable parcelable = new TestParcelable(11);
        intent.putExtra("foo", parcelable);
        assertSame(null, intent.getParcelableArrayExtra("foo"));
        Parcelable[] parcelables = {new TestParcelable(12), new TestParcelable(13)};
        assertSame(intent, intent.putExtra("bar", parcelables));
        assertSame(parcelables, intent.getParcelableArrayExtra("bar"));
    }

    @Test
    public void testParcelableArrayListExtra() {
        Intent intent = new Intent();
        Parcelable parcel1 = new TestParcelable(22);
        Parcelable parcel2 = new TestParcelable(23);
        ArrayList<Parcelable> parcels = new ArrayList<Parcelable>();
        parcels.add(parcel1);
        parcels.add(parcel2);

        assertSame(intent, intent.putParcelableArrayListExtra("foo", parcels));
        assertSame(parcels, intent.getParcelableArrayListExtra("foo"));
        assertSame(parcel1, intent.getParcelableArrayListExtra("foo").get(0));
        assertSame(parcel2, intent.getParcelableArrayListExtra("foo").get(1));
        assertSame(parcels, intent.getExtras().getParcelableArrayList("foo"));
    }

    @Test
    public void testLongExtra() throws Exception {
        Intent intent = new Intent();
        assertSame(intent, intent.putExtra("foo", 2L));
        assertEquals(2L, shadowOf(intent).getExtras().get("foo"));
        assertEquals(2L, intent.getLongExtra("foo", -1));
        assertEquals(-1L, intent.getLongExtra("bar", -1));
    }

    @Test
    public void testBundleExtra() throws Exception {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt("bar", 5);
        assertSame(intent, intent.putExtra("foo", bundle));
        assertEquals(5, intent.getBundleExtra("foo").getInt("bar"));
    }

    @Test
    public void testHasExtra() throws Exception {
        Intent intent = new Intent();
        assertSame(intent, intent.putExtra("foo", ""));
        assertTrue(intent.hasExtra("foo"));
        assertFalse(intent.hasExtra("bar"));
    }

    @Test
    public void testGetActionReturnsWhatWasSet() throws Exception {
        Intent intent = new Intent();
        assertSame(intent, intent.setAction("foo"));
        assertEquals("foo", intent.getAction());
    }

    @Test
    public void testSetData() throws Exception {
        Intent intent = new Intent();
        Uri uri = Uri.parse("content://this/and/that");
        intent.setType("abc");
        assertSame(intent, intent.setData(uri));
        assertSame(uri, intent.getData());
        assertNull(intent.getType());
    }

    @Test
    public void testSetType() throws Exception {
        Intent intent = new Intent();
        intent.setData(Uri.parse("content://this/and/that"));
        assertSame(intent, intent.setType("def"));
        assertNull(intent.getData());
        assertEquals("def", intent.getType());
    }

    @Test
    public void testSetDataAndType() throws Exception {
        Intent intent = new Intent();
        Uri uri = Uri.parse("content://this/and/that");
        assertSame(intent, intent.setDataAndType(uri, "ghi"));
        assertSame(uri, intent.getData());
        assertEquals("ghi", intent.getType());
    }

    @Test
    public void testSetClass() throws Exception {
        Intent intent = new Intent();
        Class<? extends IntentTest> thisClass = getClass();
        Intent output = intent.setClass(new Activity(), thisClass);

        assertSame(output, intent);
        ShadowIntent si = shadowOf(intent);
        assertSame(si.getIntentClass(), thisClass);
    }

    @Test
    public void testSetClassName() throws Exception {
        Intent intent = new Intent();
        Class<? extends IntentTest> thisClass = getClass();
        intent.setClassName("package.name", thisClass.getName());
        assertSame(thisClass.getName(), intent.getComponent().getClassName());
        assertEquals("package.name", intent.getComponent().getPackageName());
        ShadowIntent si = shadowOf(intent);
        assertSame(si.getIntentClass(), thisClass);
    }

    @Test
    public void testSetClassThroughConstructor() throws Exception {
        Intent intent = new Intent(new Activity(), getClass());
        assertEquals(shadowOf(intent).getIntentClass(), getClass());
    }

    @Test
    public void shouldSetFlags() throws Exception {
        Intent intent = new Intent();
        Intent self = intent.setFlags(1234);
        assertEquals(1234, intent.getFlags());
        assertSame(self, intent);
    }

    @Test
    public void shouldAddFlags() throws Exception {
        Intent intent = new Intent();
        Intent self = intent.addFlags(4);
        self.addFlags(8);
        assertEquals(12, intent.getFlags());
        assertSame(self, intent);
    }

    @Test
    public void shouldSupportCategories() throws Exception {
        Intent intent = new Intent();
        Intent self = intent.addCategory("category.name.1");
        intent.addCategory("category.name.2");

        assertTrue(intent.hasCategory("category.name.1"));
        assertTrue(intent.hasCategory("category.name.2"));

        Set<String> categories = intent.getCategories();
        assertTrue(categories.contains("category.name.1"));
        assertTrue(categories.contains("category.name.2"));

        intent.removeCategory("category.name.1");
        assertFalse(intent.hasCategory("category.name.1"));
        assertTrue(intent.hasCategory("category.name.2"));

        intent.removeCategory("category.name.2");
        assertFalse(intent.hasCategory("category.name.2"));

        assertEquals(0, intent.getCategories().size());

        assertSame(self, intent);
    }

    @Test
    public void shouldAddCategories() throws Exception {
        Intent intent = new Intent();
        Intent self = intent.addCategory("foo");
        assertTrue(intent.getCategories().contains("foo"));
        assertSame(self, intent);
    }

    @Test
    public void shouldFillIn() throws Exception {
        Intent intentA = new Intent();
        Intent intentB = new Intent();

        intentB.setAction("foo");
        Uri uri = Uri.parse("http://www.foo.com");
        intentB.setDataAndType(uri, "text/html");
        String category = "category";
        intentB.addCategory(category);
        intentB.setPackage("com.foobar.app");
        ComponentName cn = new ComponentName("com.foobar.app", "activity");
        intentB.setComponent(cn);
        intentB.putExtra("FOO", 23);

        int flags = Intent.FILL_IN_ACTION |
                Intent.FILL_IN_DATA |
                Intent.FILL_IN_CATEGORIES |
                Intent.FILL_IN_PACKAGE |
                Intent.FILL_IN_COMPONENT;

        int result = intentA.fillIn(intentB, flags);
        assertEquals("foo", intentA.getAction());
        assertSame(uri, intentA.getData());
        assertEquals("text/html", intentA.getType());
        assertTrue(intentA.getCategories().contains(category));
        assertEquals("com.foobar.app", intentA.getPackage());
        assertSame(cn, intentA.getComponent());
        assertEquals(23, intentA.getIntExtra("FOO", -1));
        assertEquals(result, flags);
    }

    @Test
    public void equals_shouldTestActionComponentNameDataAndExtras() throws Exception {
        Intent intentA = new Intent()
                .setAction("action")
                .setData(Uri.parse("content:1"))
                .setComponent(new ComponentName("pkg", "cls"))
                .putExtra("extra", "blah")
                .setType("image/*")
                .addCategory("category.name");

        Intent intentB = new Intent()
                .setAction("action")
                .setData(Uri.parse("content:1"))
                .setComponent(new ComponentName("pkg", "cls"))
                .putExtra("extra", "blah")
                .setType("image/*")
                .addCategory("category.name");

        assertThat(intentA, equalTo(intentB));

        intentB.setAction("other action");
        assertThat(intentA, not(equalTo(intentB)));

        intentB.setAction("action");
        intentB.setData(Uri.parse("content:other"));
        assertThat(intentA, not(equalTo(intentB)));

        intentB.setData(Uri.parse("content:1"));
        intentB.setComponent(new ComponentName("other-pkg", "other-cls"));
        assertThat(intentA, not(equalTo(intentB)));

        intentB.setComponent(new ComponentName("pkg", "cls"));
        intentB.putExtra("extra", "foo");
        assertThat(intentA, not(equalTo(intentB)));

        intentB.putExtra("extra", "blah");
        intentB.setType("other/*");
        assertThat(intentA, not(equalTo(intentB)));

        intentB.setType("image/*");
        assertThat(intentA, equalTo(intentB));

        intentB.removeCategory("category.name");
        assertThat(intentA, not(equalTo(intentB)));
    }

    @Test
    public void equals_whenOtherObjectIsNotAnIntent_shouldReturnFalse() throws Exception {
        assertThat(new Intent(), not(equalTo(new Object())));
    }

    @Test
    public void createChooser_shouldWrapIntent() throws Exception {
        Intent originalIntent = new Intent(Intent.ACTION_BATTERY_CHANGED, Uri.parse("foo://blah"));
        Intent chooserIntent = Intent.createChooser(originalIntent, "The title");
        Intent expectedIntent = new Intent(Intent.ACTION_CHOOSER);
        expectedIntent.putExtra(Intent.EXTRA_INTENT, originalIntent);
        expectedIntent.putExtra(Intent.EXTRA_TITLE, "The title");
        assertEquals(expectedIntent, chooserIntent);
    }

    @Test
    public void putStringArrayListExtra_addsListToExtras() {
        Intent intent = new Intent();
        final ArrayList<String> strings = new ArrayList<String>(Arrays.asList("hi", "there"));

        intent.putStringArrayListExtra("KEY", strings);
        assertThat(intent.getStringArrayListExtra("KEY"), equalTo(strings));
        assertThat(Robolectric.shadowOf(intent.getExtras()).getStringArrayList("KEY"), equalTo(strings));
    }

    @Test
    public void putIntegerArrayListExtra_addsListToExtras() {
        Intent intent = new Intent();
        final ArrayList<Integer> integers = new ArrayList<Integer>(Arrays.asList(100, 200, 300));

        intent.putIntegerArrayListExtra("KEY", integers);
        assertThat(intent.getIntegerArrayListExtra("KEY"), equalTo(integers));
        assertThat(Robolectric.shadowOf(intent.getExtras()).getIntegerArrayList("KEY"), equalTo(integers));
    }

    @Test
    public void testParcelIo_explicitIntent() {
        Intent intent = new Intent(new Activity(), getClass());
        intent.putExtra("boolean", true);
        intent.putExtra("string", "string value");
        Bundle bundle = new Bundle();
        bundle.putDouble("bundle double", 3.14);
        intent.putExtra("bundle", bundle);
        intent.putExtra("long", 893);

        verifyIntentReadIsWhatWasWrittenToParcel(intent);
    }

    @Test
    public void testParcelIo_actionTypeCategory() {
        Intent intent = new Intent("action");
        intent.setType("type");
        intent.addCategory("category");
        verifyIntentReadIsWhatWasWrittenToParcel(intent);
    }

    @Test
    public void shouldHaveCreator() throws Exception {
        Intent expected = new Intent("action");
        expected.setType("type");
        expected.addCategory("category");
        Parcel p = Parcel.obtain();
        expected.writeToParcel(p, 0);

        p.setDataPosition(0);

        Intent actual = Intent.CREATOR.createFromParcel(p);
        assertThat(expected, equalTo(actual));
    }

    private void verifyIntentReadIsWhatWasWrittenToParcel(Intent expected) {
        Parcel parcel = Parcel.obtain();
        expected.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        Intent actual = new Intent();
        actual.readFromParcel(parcel);

        assertThat(expected, equalTo(actual));
    }

    private static class TestSerializable implements Serializable {
        private final String someValue;

        public TestSerializable(String someValue) {
            this.someValue = someValue;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            TestSerializable that = (TestSerializable) o;

            if (someValue != null ? !someValue.equals(that.someValue) : that.someValue != null) return false;

            return true;
        }

        @Override
        public int hashCode() {
            return someValue != null ? someValue.hashCode() : 0;
        }
    }

    private class TestCharSequence implements CharSequence {
        String s;

        public TestCharSequence(String s) {
            this.s = s;
        }

        @Override
        public char charAt(int index) {
            return s.charAt(index);
        }

        @Override
        public int length() {
            return s.length();
        }

        @Override
        public CharSequence subSequence(int start, int end) {
            return s.subSequence(start, end);
        }

    }
}