1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.content.cts;
18 
19 import org.xmlpull.v1.XmlPullParser;
20 import org.xmlpull.v1.XmlPullParserException;
21 
22 import android.content.ComponentName;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.cts.util.XmlUtils;
27 import android.content.pm.ActivityInfo;
28 import android.content.pm.PackageManager;
29 import android.content.pm.PackageManager.NameNotFoundException;
30 import android.content.res.XmlResourceParser;
31 import android.net.Uri;
32 import android.os.Bundle;
33 import android.os.IBinder;
34 import android.os.Parcel;
35 import android.os.ServiceManager;
36 import android.provider.Contacts.People;
37 import android.test.AndroidTestCase;
38 import android.util.AttributeSet;
39 import android.util.Xml;
40 
41 import com.android.content.cts.DummyParcelable;
42 
43 import java.io.IOException;
44 import java.io.Serializable;
45 import java.net.URISyntaxException;
46 import java.util.ArrayList;
47 import java.util.Objects;
48 import java.util.Set;
49 
50 public class IntentTest extends AndroidTestCase {
51 
52     private Intent mIntent;
53     private static final String TEST_ACTION = "android.content.IntentTest_test";
54     private static final Uri TEST_URI = People.CONTENT_URI;
55     private static final Uri ANOTHER_TEST_URI = People.CONTENT_FILTER_URI;
56     private static final String TEST_EXTRA_NAME = "testExtraName";
57     private Context mContext;
58     private PackageManager mPm;
59     private ComponentName mComponentName;
60     private ComponentName mAnotherComponentName;
61     private static final String TEST_TYPE = "testType";
62     private static final String ANOTHER_TEST_TYPE = "anotherTestType";
63     private static final String TEST_CATEGORY = "testCategory";
64     private static final String ANOTHER_TEST_CATEGORY = "testAnotherCategory";
65     private static final String TEST_PACKAGE = "android.content.cts";
66     private static final String ANOTHER_TEST_PACKAGE = "android.database.cts";
67 
68     @Override
setUp()69     protected void setUp() throws Exception {
70         super.setUp();
71         mIntent = new Intent();
72         mContext = getContext();
73         mPm = mContext.getPackageManager();
74         mComponentName = new ComponentName(mContext, MockActivity.class);
75         mAnotherComponentName = new ComponentName(mContext, "tmp");
76     }
77 
testConstructor()78     public void testConstructor() {
79         mIntent = new Intent();
80         assertNotNull(mIntent);
81 
82         Intent intent = new Intent();
83         intent.setAction(TEST_ACTION);
84 
85         mIntent = new Intent(intent);
86         assertNotNull(mIntent);
87         assertEquals(TEST_ACTION, mIntent.getAction());
88 
89         mIntent = new Intent(TEST_ACTION);
90         assertNotNull(mIntent);
91         assertEquals(TEST_ACTION, mIntent.getAction());
92 
93         mIntent = new Intent(TEST_ACTION, TEST_URI);
94         assertNotNull(mIntent);
95         assertEquals(TEST_ACTION, mIntent.getAction());
96         assertEquals(TEST_URI, mIntent.getData());
97 
98         mIntent = new Intent(mContext, MockActivity.class);
99         assertNotNull(mIntent);
100         assertEquals(mComponentName, mIntent.getComponent());
101 
102         mIntent = new Intent(TEST_ACTION, TEST_URI, mContext, MockActivity.class);
103         assertNotNull(mIntent);
104         assertEquals(TEST_ACTION, mIntent.getAction());
105         assertEquals(TEST_URI, mIntent.getData());
106         assertEquals(mComponentName, mIntent.getComponent());
107     }
108 
testRemoveExtra()109     public void testRemoveExtra() {
110         mIntent = new Intent();
111         mIntent.putExtra(TEST_EXTRA_NAME, "testvalue");
112         assertNotNull(mIntent.getStringExtra(TEST_EXTRA_NAME));
113         mIntent.removeExtra(TEST_EXTRA_NAME);
114         assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME));
115     }
116 
testGetCharSequenceExtra()117     public void testGetCharSequenceExtra() {
118         final CharSequence expected = "CharSequencetest";
119         mIntent.putExtra(TEST_EXTRA_NAME, expected);
120         assertEquals(expected, mIntent.getCharSequenceExtra(TEST_EXTRA_NAME));
121     }
122 
testReadFromParcel()123     public void testReadFromParcel() {
124         mIntent.setAction(TEST_ACTION);
125         mIntent.setData(TEST_URI);
126         mIntent.setType(TEST_TYPE);
127         mIntent.setFlags(0);
128         mIntent.setComponent(mComponentName);
129         mIntent.addCategory(TEST_CATEGORY);
130         final Parcel parcel = Parcel.obtain();
131         mIntent.writeToParcel(parcel, 0);
132         parcel.setDataPosition(0);
133         final Intent target = new Intent();
134         target.readFromParcel(parcel);
135         assertEquals(mIntent.getAction(), target.getAction());
136         assertEquals(mIntent.getData(), target.getData());
137         assertEquals(mIntent.getFlags(), target.getFlags());
138         assertEquals(mIntent.getComponent(), target.getComponent());
139         assertEquals(mIntent.getCategories(), target.getCategories());
140         assertEquals(mIntent.toURI(), target.toURI());
141     }
142 
testGetParcelableArrayListExtra()143     public void testGetParcelableArrayListExtra() {
144         final ArrayList<Intent> expected = new ArrayList<Intent>();
145         Intent intent = new Intent(TEST_ACTION);
146         expected.add(intent);
147 
148         mIntent.putParcelableArrayListExtra(TEST_EXTRA_NAME, expected);
149         final ArrayList<Intent> target = mIntent.getParcelableArrayListExtra(TEST_EXTRA_NAME);
150         assertEquals(expected.size(), target.size());
151         assertEquals(expected, target);
152     }
153 
testFilterHashCode()154     public void testFilterHashCode() {
155         mIntent.filterHashCode();
156     }
157 
testGetCategories()158     public void testGetCategories() {
159         mIntent.addCategory(TEST_CATEGORY);
160         final Set<String> target = mIntent.getCategories();
161         assertEquals(TEST_CATEGORY, target.toArray()[0]);
162     }
163 
testGetScheme()164     public void testGetScheme() {
165         assertNull(mIntent.getScheme());
166         mIntent.setData(TEST_URI);
167         assertEquals(TEST_URI.getScheme(), mIntent.getScheme());
168     }
169 
testGetIntegerArrayListExtra()170     public void testGetIntegerArrayListExtra() {
171         final ArrayList<Integer> expected = new ArrayList<Integer>();
172         expected.add(0);
173         mIntent.putIntegerArrayListExtra(TEST_EXTRA_NAME, expected);
174         assertEquals(expected, mIntent.getIntegerArrayListExtra(TEST_EXTRA_NAME));
175     }
176 
testHasExtra()177     public void testHasExtra() {
178         mIntent = new Intent();
179         assertFalse(mIntent.hasExtra(TEST_EXTRA_NAME));
180         mIntent.putExtra(TEST_EXTRA_NAME, "test");
181         assertTrue(mIntent.hasExtra(TEST_EXTRA_NAME));
182     }
183 
testGetIntArrayExtra()184     public void testGetIntArrayExtra() {
185         final int[] expected = { 1, 2, 3 };
186         assertNull(mIntent.getIntArrayExtra(TEST_EXTRA_NAME));
187         mIntent.putExtra(TEST_EXTRA_NAME, expected);
188         assertEquals(expected, mIntent.getIntArrayExtra(TEST_EXTRA_NAME));
189     }
190 
testSetClassName1()191     public void testSetClassName1() {
192         final Intent intent = mIntent.setClassName(mContext, MockActivity.class.getName());
193         assertEquals(mComponentName, mIntent.getComponent());
194         assertSame(mIntent, intent);
195     }
196 
testSetClassName2()197     public void testSetClassName2() {
198         mIntent.setClassName(mContext.getPackageName(), MockActivity.class.getName());
199         assertEquals(mComponentName, mIntent.getComponent());
200     }
201 
testGetIntExtra()202     public void testGetIntExtra() {
203         final int expected = 0;
204         mIntent = new Intent();
205         assertEquals(expected, mIntent.getIntExtra(TEST_EXTRA_NAME, expected));
206         mIntent.putExtra(TEST_EXTRA_NAME, 100);
207         assertEquals(100, mIntent.getIntExtra(TEST_EXTRA_NAME, 1));
208 
209     }
210 
testPutIntegerArrayListExtra()211     public void testPutIntegerArrayListExtra() {
212         final ArrayList<Integer> expected = new ArrayList<Integer>();
213         expected.add(0);
214         mIntent = new Intent();
215         mIntent.putIntegerArrayListExtra(TEST_EXTRA_NAME, expected);
216         assertEquals(expected, mIntent.getIntegerArrayListExtra(TEST_EXTRA_NAME));
217     }
218 
testAccessType()219     public void testAccessType() {
220         mIntent.setType(TEST_TYPE);
221         assertEquals(TEST_TYPE, mIntent.getType());
222     }
223 
testGetBundleExtra()224     public void testGetBundleExtra() {
225         final Bundle expected = new Bundle();
226         expected.putBoolean("testTrue", true);
227         mIntent.putExtras(expected);
228         mIntent.putExtra(TEST_EXTRA_NAME, expected);
229 
230         assertEquals(expected, mIntent.getBundleExtra(TEST_EXTRA_NAME));
231     }
232 
testGetCharArrayExtra()233     public void testGetCharArrayExtra() {
234         final char[] expected = { 'a', 'b', 'c' };
235         mIntent.putExtra(TEST_EXTRA_NAME, expected);
236         final char[] actual= mIntent.getCharArrayExtra(TEST_EXTRA_NAME);
237         assertEquals(expected.length, actual.length);
238         assertEquals(expected[0], actual[0]);
239         assertEquals(expected[1], actual[1]);
240         assertEquals(expected[2], actual[2]);
241     }
242 
testGetDoubleArrayExtra()243     public void testGetDoubleArrayExtra() {
244         final double[] expected = { 1d, 2d };
245         mIntent.putExtra(TEST_EXTRA_NAME, expected);
246         assertEquals(expected, mIntent.getDoubleArrayExtra(TEST_EXTRA_NAME));
247     }
248 
testPutStringArrayListExtra()249     public void testPutStringArrayListExtra() {
250         final ArrayList<String> expected = new ArrayList<String>();
251         expected.add("testString");
252         mIntent.putStringArrayListExtra(TEST_EXTRA_NAME, expected);
253         assertEquals(expected, mIntent.getStringArrayListExtra(TEST_EXTRA_NAME));
254     }
255 
testResolveType1()256     public void testResolveType1() {
257         final ContentResolver contentResolver = mContext.getContentResolver();
258         assertNull(mIntent.resolveType(mContext));
259         mIntent.setType(TEST_TYPE);
260         assertEquals(TEST_TYPE, mIntent.resolveType(mContext));
261         mIntent.setType(null);
262         mIntent.setData(TEST_URI);
263         assertEquals(contentResolver.getType(TEST_URI), mIntent.resolveType(mContext));
264         mIntent.setData(Uri.parse("test"));
265         assertNull(mIntent.resolveType(mContext));
266     }
267 
testResolveType2()268     public void testResolveType2() {
269         final ContentResolver contentResolver = mContext.getContentResolver();
270         assertNull(mIntent.resolveType(contentResolver));
271         mIntent.setType(TEST_TYPE);
272         assertEquals(TEST_TYPE, mIntent.resolveType(contentResolver));
273         mIntent.setType(null);
274         mIntent.setData(TEST_URI);
275         assertEquals(contentResolver.getType(TEST_URI), mIntent.resolveType(contentResolver));
276         mIntent.setData(Uri.parse("test"));
277         assertNull(mIntent.resolveType(contentResolver));
278     }
279 
testAccessComponent()280     public void testAccessComponent() {
281         mIntent.setComponent(mComponentName);
282         assertEquals(mComponentName, mIntent.getComponent());
283     }
284 
testGetDataString()285     public void testGetDataString() {
286         assertNull(mIntent.getDataString());
287         mIntent.setData(TEST_URI);
288         assertEquals(TEST_URI.toString(), mIntent.getDataString());
289     }
290 
testHasCategory()291     public void testHasCategory() {
292         assertFalse(mIntent.hasCategory(TEST_CATEGORY));
293         mIntent.addCategory(TEST_CATEGORY);
294         assertTrue(mIntent.hasCategory(TEST_CATEGORY));
295     }
296 
testGetLongArrayExtra()297     public void testGetLongArrayExtra() {
298         final long[] expected = { 1l, 2l, 3l };
299         mIntent.putExtra(TEST_EXTRA_NAME, expected);
300         assertEquals(expected, mIntent.getLongArrayExtra(TEST_EXTRA_NAME));
301     }
302 
testParseIntent()303     public void testParseIntent() throws XmlPullParserException, IOException,
304         NameNotFoundException {
305         mIntent = null;
306         XmlResourceParser parser = null;
307         AttributeSet attrs = null;
308         ActivityInfo ai = null;
309         try {
310             mIntent = Intent.parseIntent(mContext.getResources(), parser, attrs);
311             fail("should thow exception!");
312         } catch (NullPointerException e) {
313             // expected
314         }
315 
316         ai = mContext.getPackageManager().getActivityInfo(mComponentName,
317                 PackageManager.GET_META_DATA);
318         parser = ai.loadXmlMetaData(mContext.getPackageManager(), "android.app.alias");
319 
320         attrs = Xml.asAttributeSet(parser);
321         int type;
322         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
323                 && type != XmlPullParser.START_TAG) {
324         }
325 
326         String nodeName = parser.getName();
327         if (!"alias".equals(nodeName)) {
328             throw new RuntimeException();
329         }
330 
331         int outerDepth = parser.getDepth();
332         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
333                 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
334             if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
335                 continue;
336             }
337 
338             nodeName = parser.getName();
339             if ("intent".equals(nodeName)) {
340                 mIntent = Intent.parseIntent(mContext.getResources(), parser, attrs);
341             } else {
342                 XmlUtils.skipCurrentTag(parser);
343             }
344         }
345 
346         assertNotNull(mIntent);
347         assertEquals("android.intent.action.MAIN", mIntent.getAction());
348         assertEquals(Uri.parse("http://www.google.com/"), mIntent.getData());
349     }
350 
testSetClass()351     public void testSetClass() {
352         assertNull(mIntent.getComponent());
353         mIntent.setClass(mContext, MockActivity.class);
354         assertEquals(mComponentName, mIntent.getComponent());
355     }
356 
testResolveTypeIfNeeded()357     public void testResolveTypeIfNeeded() {
358         ContentResolver contentResolver = mContext.getContentResolver();
359         assertNull(mIntent.resolveTypeIfNeeded(contentResolver));
360         mIntent.setType(TEST_TYPE);
361         assertEquals(TEST_TYPE, mIntent.resolveTypeIfNeeded(contentResolver));
362 
363         mIntent.setType(null);
364         mIntent.setComponent(mComponentName);
365         assertEquals(null, mIntent.resolveTypeIfNeeded(contentResolver));
366 
367         mIntent.setType(TEST_TYPE);
368         mIntent.setComponent(mComponentName);
369         assertEquals(TEST_TYPE, mIntent.resolveTypeIfNeeded(contentResolver));
370 
371         mIntent.setType(null);
372         mIntent.setData(TEST_URI);
373         assertNull(mIntent.resolveTypeIfNeeded(contentResolver));
374     }
375 
testPutExtra1()376     public void testPutExtra1() {
377         assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false));
378         mIntent.putExtra(TEST_EXTRA_NAME, true);
379         assertTrue(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false));
380         mIntent.putExtra(TEST_EXTRA_NAME, false);
381         assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false));
382     }
383 
testPutExtra2()384     public void testPutExtra2() {
385         final byte expected = Byte.valueOf("1");
386         mIntent.putExtra(TEST_EXTRA_NAME, expected);
387         assertEquals(expected, mIntent.getByteExtra(TEST_EXTRA_NAME, Byte.valueOf("1")));
388     }
389 
testPutExtra3()390     public void testPutExtra3() {
391         assertEquals('a', mIntent.getCharExtra(TEST_EXTRA_NAME, 'a'));
392         final char expected = 'a';
393         mIntent.putExtra(TEST_EXTRA_NAME, expected);
394         assertEquals(expected, mIntent.getCharExtra(TEST_EXTRA_NAME, 'a'));
395     }
396 
testPutExtra4()397     public void testPutExtra4() {
398         final Short expected = Short.valueOf("2");
399         assertEquals(Short.valueOf("1").shortValue(), mIntent.getShortExtra(
400                 TEST_EXTRA_NAME, Short.valueOf("1")));
401         mIntent.putExtra(TEST_EXTRA_NAME, expected);
402         assertEquals(expected.shortValue(), mIntent.getShortExtra(TEST_EXTRA_NAME, Short.valueOf("1")));
403     }
404 
testPutExtra5()405     public void testPutExtra5() {
406         final int expected = 2;
407         assertEquals(1, mIntent.getIntExtra(TEST_EXTRA_NAME, 1));
408         mIntent.putExtra(TEST_EXTRA_NAME, expected);
409         assertEquals(expected, mIntent.getIntExtra(TEST_EXTRA_NAME, 1));
410     }
411 
testPutExtra6()412     public void testPutExtra6() {
413         final long expected = 2l;
414         assertEquals(1l, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l));
415         mIntent.putExtra(TEST_EXTRA_NAME, expected);
416         assertEquals(expected, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l));
417     }
418 
testPutExtra7()419     public void testPutExtra7() {
420         final float expected = 2f;
421         assertEquals(1f, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f));
422         mIntent.putExtra(TEST_EXTRA_NAME, expected);
423         assertEquals(expected, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f));
424     }
425 
testPutExtra8()426     public void testPutExtra8() {
427         final double expected = 2d;
428         assertEquals(1d, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d));
429         mIntent.putExtra(TEST_EXTRA_NAME, expected);
430         assertEquals(expected, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d));
431     }
432 
testPutExtra9()433     public void testPutExtra9() {
434         final String expected = "testString";
435         assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME));
436         mIntent.putExtra(TEST_EXTRA_NAME, expected);
437         assertEquals(expected, mIntent.getStringExtra(TEST_EXTRA_NAME));
438     }
439 
testPutExtra10()440     public void testPutExtra10() {
441         final CharSequence expected = "testString";
442         assertNull(mIntent.getCharSequenceExtra(TEST_EXTRA_NAME));
443         mIntent.putExtra(TEST_EXTRA_NAME, expected);
444         assertEquals(expected, mIntent.getCharSequenceExtra(TEST_EXTRA_NAME));
445     }
446 
testPutExtra11()447     public void testPutExtra11() {
448         final Intent expected = new Intent(TEST_ACTION);
449         mIntent.putExtra(TEST_EXTRA_NAME, expected);
450         assertEquals(expected, mIntent.getParcelableExtra(TEST_EXTRA_NAME));
451     }
452 
testPutExtra12()453     public void testPutExtra12() {
454         final Intent[] expected = { new Intent(TEST_ACTION), new Intent(mContext, MockActivity.class) };
455         mIntent.putExtra(TEST_EXTRA_NAME, expected);
456         assertEquals(expected, mIntent.getParcelableArrayExtra(TEST_EXTRA_NAME));
457     }
458 
testPutExtra13()459     public void testPutExtra13() {
460         final TestSerializable expected = new TestSerializable();
461         expected.Name = "testName";
462         mIntent.putExtra(TEST_EXTRA_NAME, expected);
463         assertEquals(expected, mIntent.getSerializableExtra(TEST_EXTRA_NAME));
464         TestSerializable target = (TestSerializable) mIntent.getSerializableExtra(TEST_EXTRA_NAME);
465         assertEquals(expected.Name, target.Name);
466     }
467 
testPutExtra14()468     public void testPutExtra14() {
469         final boolean[] expected = { true, true, false };
470         mIntent.putExtra(TEST_EXTRA_NAME, expected);
471         assertEquals(expected, mIntent.getBooleanArrayExtra(TEST_EXTRA_NAME));
472     }
473 
testPutExtra15()474     public void testPutExtra15() {
475         final byte[] expected = TEST_ACTION.getBytes();
476         mIntent.putExtra(TEST_EXTRA_NAME, expected);
477         assertEquals(expected, mIntent.getByteArrayExtra(TEST_EXTRA_NAME));
478     }
479 
testPutExtra16()480     public void testPutExtra16() {
481         final short[] expected = { 1, 2, 3 };
482         mIntent.putExtra(TEST_EXTRA_NAME, expected);
483         assertEquals(expected, mIntent.getShortArrayExtra(TEST_EXTRA_NAME));
484     }
485 
testPutExtra17()486     public void testPutExtra17() {
487         final char[] expected = { '1', '2', '3' };
488         mIntent.putExtra(TEST_EXTRA_NAME, expected);
489         assertEquals(expected, mIntent.getCharArrayExtra(TEST_EXTRA_NAME));
490     }
491 
testPutExtra18()492     public void testPutExtra18() {
493         final int[] expected = { 1, 2, 3 };
494         mIntent.putExtra(TEST_EXTRA_NAME, expected);
495         assertEquals(expected, mIntent.getIntArrayExtra(TEST_EXTRA_NAME));
496     }
497 
testPutExtra19()498     public void testPutExtra19() {
499         final long[] expected = { 1l, 2l, 3l };
500         mIntent.putExtra(TEST_EXTRA_NAME, expected);
501         assertEquals(expected, mIntent.getLongArrayExtra(TEST_EXTRA_NAME));
502     }
503 
testPutExtra20()504     public void testPutExtra20() {
505         final float[] expected = { 1f, 2f, 3f };
506         mIntent.putExtra(TEST_EXTRA_NAME, expected);
507         assertEquals(expected, mIntent.getFloatArrayExtra(TEST_EXTRA_NAME));
508     }
509 
testPutExtra21()510     public void testPutExtra21() {
511         final double[] expected = { 1d, 2d, 3d };
512         mIntent.putExtra(TEST_EXTRA_NAME, expected);
513         assertEquals(expected, mIntent.getDoubleArrayExtra(TEST_EXTRA_NAME));
514     }
515 
testPutExtra22()516     public void testPutExtra22() {
517         final String[] expected = { "1d", "2d", "3d" };
518         mIntent.putExtra(TEST_EXTRA_NAME, expected);
519         assertEquals(expected, mIntent.getStringArrayExtra(TEST_EXTRA_NAME));
520     }
521 
testPutExtra23()522     public void testPutExtra23() {
523         final Bundle expected = new Bundle();
524         expected.putString("key", "value");
525         mIntent.putExtra(TEST_EXTRA_NAME, expected);
526         assertEquals(expected, mIntent.getBundleExtra(TEST_EXTRA_NAME));
527     }
528 
529     @SuppressWarnings("deprecation")
testPutExtra24()530     public void testPutExtra24() {
531         final IBinder expected = ServiceManager.getService("activity");
532         mIntent.putExtra(TEST_EXTRA_NAME, expected);
533         assertEquals(expected, mIntent.getIBinderExtra(TEST_EXTRA_NAME));
534     }
535 
testAddCategory()536     public void testAddCategory() {
537         assertFalse(mIntent.hasCategory(TEST_CATEGORY));
538         mIntent.addCategory(TEST_CATEGORY);
539         assertTrue(mIntent.hasCategory(TEST_CATEGORY));
540     }
541 
testPutParcelableArrayListExtra()542     public void testPutParcelableArrayListExtra() {
543         ArrayList<Intent> expected = new ArrayList<Intent>();
544         mIntent.putExtra(TEST_EXTRA_NAME, expected);
545         assertEquals(expected, mIntent.getParcelableArrayListExtra(TEST_EXTRA_NAME));
546     }
547 
testToString()548     public void testToString() {
549         assertNotNull(mIntent.toString());
550     }
551 
testAccessData()552     public void testAccessData() {
553         mIntent.setData(TEST_URI);
554         assertEquals(TEST_URI, mIntent.getData());
555     }
556 
testSetExtrasClassLoader()557     public void testSetExtrasClassLoader() {
558     }
559 
testGetStringArrayListExtra()560     public void testGetStringArrayListExtra() {
561         final ArrayList<String> expected = new ArrayList<String>();
562         expected.add("testString");
563         mIntent.putStringArrayListExtra(TEST_EXTRA_NAME, expected);
564         assertEquals(expected, mIntent.getStringArrayListExtra(TEST_EXTRA_NAME));
565     }
566 
testGetCharSequenceArrayListExtra()567     public void testGetCharSequenceArrayListExtra() {
568         final ArrayList<CharSequence> expected = new ArrayList<CharSequence>();
569         expected.add("testCharSequence");
570         mIntent.putCharSequenceArrayListExtra(TEST_EXTRA_NAME, expected);
571         assertEquals(expected, mIntent.getCharSequenceArrayListExtra(TEST_EXTRA_NAME));
572     }
573 
testResolveActivityInfo()574     public void testResolveActivityInfo() throws NameNotFoundException {
575         final PackageManager pm = mContext.getPackageManager();
576         assertEquals(null, mIntent.resolveActivityInfo(pm, 1));
577         mIntent.setComponent(mComponentName);
578         ActivityInfo target = null;
579 
580         target = pm.getActivityInfo(mComponentName, 1);
581         assertEquals(target.targetActivity, mIntent.resolveActivityInfo(pm, 1).targetActivity);
582     }
583 
testGetParcelableExtra()584     public void testGetParcelableExtra() {
585         final Intent expected = new Intent(TEST_ACTION);
586         mIntent.putExtra(TEST_EXTRA_NAME, expected);
587         assertEquals(expected, mIntent.getParcelableExtra(TEST_EXTRA_NAME));
588     }
589 
testAccessAction()590     public void testAccessAction() {
591         mIntent.setAction(TEST_ACTION);
592         assertEquals(TEST_ACTION, mIntent.getAction());
593     }
594 
testAddFlags()595     public void testAddFlags() {
596         final int flag = 1;
597         int expected = 0;
598         mIntent.addFlags(flag);
599         expected |= flag;
600         assertEquals(expected, mIntent.getFlags());
601     }
602 
testDescribeContents()603     public void testDescribeContents() {
604         final int expected = 0;
605         assertEquals(expected, mIntent.describeContents());
606         mIntent.putExtra(TEST_EXTRA_NAME, "test");
607         assertEquals(mIntent.getExtras().describeContents(), mIntent.describeContents());
608     }
609 
testGetShortExtra()610     public void testGetShortExtra() {
611 
612         final Short expected = Short.valueOf("2");
613         assertEquals(Short.valueOf("1").shortValue(), mIntent.getShortExtra(
614                 TEST_EXTRA_NAME, Short.valueOf("1")));
615         mIntent.putExtra(TEST_EXTRA_NAME, expected);
616         assertEquals(expected.shortValue(), mIntent.getShortExtra(TEST_EXTRA_NAME, Short.valueOf("1")));
617     }
618 
testClone()619     public void testClone() {
620         mIntent.setAction(TEST_ACTION);
621         mIntent.setClass(mContext, MockActivity.class);
622         mIntent.setComponent(mComponentName);
623         mIntent.setDataAndType(TEST_URI, TEST_TYPE);
624         mIntent.addCategory(TEST_CATEGORY);
625         final String key = "testkey";
626         final String excepted = "testValue";
627         mIntent.putExtra(key, excepted);
628         Intent actual = (Intent) mIntent.clone();
629         assertEquals(mComponentName, actual.getComponent());
630         assertEquals(TEST_ACTION, actual.getAction());
631         assertEquals(mComponentName, actual.getComponent());
632         assertEquals(TEST_URI, actual.getData());
633         assertEquals(TEST_TYPE, actual.getType());
634         assertEquals(TEST_CATEGORY, (String) (actual.getCategories().toArray()[0]));
635         assertEquals(excepted, actual.getStringExtra(key));
636     }
637 
testGetDoubleExtra()638     public void testGetDoubleExtra() {
639         final double expected = 2d;
640         assertEquals(1d, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d));
641         mIntent.putExtra(TEST_EXTRA_NAME, expected);
642         assertEquals(expected, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d));
643     }
644 
testCloneFilter()645     public void testCloneFilter() {
646         mIntent.setAction(TEST_ACTION);
647         mIntent.setClass(mContext, MockActivity.class);
648         mIntent.setComponent(mComponentName);
649         mIntent.setDataAndType(TEST_URI, TEST_TYPE);
650         mIntent.addCategory(TEST_CATEGORY);
651         final String key = "testkey";
652         mIntent.putExtra(key, "testValue");
653         Intent actual = mIntent.cloneFilter();
654         assertEquals(mComponentName, actual.getComponent());
655         assertEquals(TEST_ACTION, actual.getAction());
656         assertEquals(mComponentName, actual.getComponent());
657         assertEquals(TEST_URI, actual.getData());
658         assertEquals(TEST_TYPE, actual.getType());
659         assertEquals(TEST_CATEGORY, (String) (actual.getCategories().toArray()[0]));
660         assertNull(actual.getStringExtra(key));
661     }
662 
testGetIntentOld()663     public void testGetIntentOld() throws URISyntaxException {
664         String uri = "test";
665         mIntent = Intent.getIntentOld(uri);
666         assertEquals(Intent.ACTION_VIEW, mIntent.getAction());
667 
668         mIntent = null;
669         try {
670             uri = "test#action(test)categories(test)type(mtype)launchFlags(test)extras(test";
671             mIntent = Intent.getIntentOld(uri);
672             fail("should throw URISyntaxException.");
673         } catch (Exception e) {
674             // expected
675         }
676 
677         final String compnent =
678                 "component(" + mContext.getPackageName() + "!" + MockActivity.class.getName() + ")";
679         uri = "testdata#action(test)categories(test!test2)type(mtype)launchFlags(5)" + compnent
680                 + "extras(Stest=testString!btestbyte=1!"
681                 + "Btestboolean=true!ctestchar=a!dtestdouble=1d!"
682                 + "itestint=1!ltestlong=1!stestshort=1!ftestfloat=1f)";
683         mIntent = Intent.getIntentOld(uri);
684         assertEquals("test", mIntent.getAction());
685         assertEquals("testdata", mIntent.getData().toString());
686         assertEquals(mComponentName, mIntent.getComponent());
687         assertEquals("test", (String) (mIntent.getCategories().toArray()[0]));
688         assertEquals("mtype", mIntent.getType());
689         assertEquals(4, mIntent.getFlags());
690         assertEquals("testString", mIntent.getStringExtra("test"));
691         assertTrue(mIntent.getBooleanExtra("testboolean", false));
692         final byte b = 1;
693         final byte defaulttByte = 2;
694         assertEquals(b, mIntent.getByteExtra("testbyte", defaulttByte));
695         assertEquals('a', mIntent.getCharExtra("testchar", 'b'));
696         final float testFloat = 1f;
697         assertEquals(testFloat, mIntent.getFloatExtra("testfloat", 2f));
698         final double testDouble = 1d;
699         assertEquals(testDouble, mIntent.getDoubleExtra("testdouble", 2d));
700 
701         final long testLong = 1;
702         assertEquals(testLong, mIntent.getLongExtra("testlong", 2l));
703 
704         final short testShort = 1;
705         final short defaultShort = 2;
706         assertEquals(testShort, mIntent.getShortExtra("testshort", defaultShort));
707         assertEquals(1, mIntent.getIntExtra("testint", 2));
708     }
709 
testGetParcelableArrayExtra()710     public void testGetParcelableArrayExtra() {
711         final Intent[] expected = { new Intent(TEST_ACTION), new Intent(mContext, MockActivity.class) };
712         mIntent.putExtra(TEST_EXTRA_NAME, expected);
713         assertEquals(expected, mIntent.getParcelableArrayExtra(TEST_EXTRA_NAME));
714     }
715 
testResolveActivityEmpty()716     public void testResolveActivityEmpty() {
717         final Intent emptyIntent = new Intent();
718 
719         // Empty intent shouldn't resolve to anything
720         final ComponentName target = emptyIntent.resolveActivity(mPm);
721         assertNull(target);
722     }
723 
testResolveActivitySingleMatch()724     public void testResolveActivitySingleMatch() {
725         final Intent intent = new Intent("android.content.cts.action.TEST_ACTION");
726         intent.addCategory("android.content.cts.category.TEST_CATEGORY");
727 
728         // Should only have one activity responding to narrow category
729         final ComponentName target = intent.resolveActivity(mPm);
730         assertEquals("android.content.cts", target.getPackageName());
731         assertEquals("android.content.cts.MockActivity", target.getClassName());
732     }
733 
testResolveActivityShortcutMatch()734     public void testResolveActivityShortcutMatch() {
735         final Intent intent = new Intent("android.content.cts.action.TEST_ACTION");
736         intent.setComponent(
737                 new ComponentName("android.content.cts", "android.content.cts.MockActivity2"));
738 
739         // Multiple activities match, but we asked for explicit component
740         final ComponentName target = intent.resolveActivity(mPm);
741         assertEquals("android.content.cts", target.getPackageName());
742         assertEquals("android.content.cts.MockActivity2", target.getClassName());
743     }
744 
testResolveActivityMultipleMatch()745     public void testResolveActivityMultipleMatch() {
746         final Intent intent = new Intent("android.content.cts.action.TEST_ACTION");
747 
748         // Should have multiple activities, resulting in resolver dialog
749         final ComponentName target = intent.resolveActivity(mPm);
750         final String pkgName = target.getPackageName();
751         assertFalse("android.content.cts".equals(pkgName));
752 
753         // Whoever they are must be able to set preferred activities
754         if (!"android".equals(pkgName)) {
755             if (mPm.checkPermission(android.Manifest.permission.SET_PREFERRED_APPLICATIONS, pkgName)
756                     != PackageManager.PERMISSION_GRANTED) {
757                 fail("Resolved target " + target
758                         + " doesn't have SET_PREFERRED_APPLICATIONS permission");
759             }
760         }
761     }
762 
testGetCharExtra()763     public void testGetCharExtra() {
764         assertEquals('a', mIntent.getCharExtra(TEST_EXTRA_NAME, 'a'));
765         final char expected = 'b';
766         mIntent.putExtra(TEST_EXTRA_NAME, expected);
767         assertEquals(expected, mIntent.getCharExtra(TEST_EXTRA_NAME, 'a'));
768     }
769 
testGetIntent()770     public void testGetIntent() throws URISyntaxException {
771         mIntent = Intent.getIntent("test#");
772         assertEquals(Intent.ACTION_VIEW, mIntent.getAction());
773 
774         try {
775             String uri = "#Intent;action=android.content.IntentTest_test;"
776                     + "category=testCategory;type=testtype;launchFlags=0x1;"
777                     + "component=com.android/.app.MockActivity;K.testExtraName=1;end";
778             mIntent = Intent.getIntent(uri);
779             fail("should throw URISyntaxException.");
780         } catch (Exception e) {
781             // expected
782         }
783         mIntent = new Intent();
784 
785         String uri = mIntent.toURI();
786         Intent target = Intent.getIntent(uri);
787         assertEquals(Intent.ACTION_VIEW, target.getAction());
788 
789         mIntent.setAction(TEST_ACTION);
790         uri = mIntent.toURI();
791         target = Intent.getIntent(uri);
792         assertEquals(TEST_ACTION, target.getAction());
793 
794         mIntent.setData(TEST_URI);
795         uri = mIntent.toURI();
796         target = Intent.getIntent(uri);
797         assertEquals(TEST_URI, target.getData());
798 
799         mIntent.setComponent(mComponentName);
800         uri = mIntent.toURI();
801         target = Intent.getIntent(uri);
802         assertEquals(mComponentName, target.getComponent());
803 
804         mIntent.addCategory(TEST_CATEGORY);
805         uri = mIntent.toURI();
806         target = Intent.getIntent(uri);
807         assertEquals(1, target.getCategories().size());
808         assertEquals(TEST_CATEGORY, (String) (target.getCategories().toArray()[0]));
809 
810         mIntent.setType(TEST_TYPE);
811         uri = mIntent.toURI();
812         target = Intent.getIntent(uri);
813         assertEquals(TEST_TYPE, target.getType());
814 
815         mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT
816                 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
817                 | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
818                 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
819         uri = mIntent.toURI();
820         target = Intent.getIntent(uri);
821         assertEquals(Intent.FLAG_ACTIVITY_NEW_DOCUMENT, target.getFlags());
822 
823         String stringValue = "testString";
824         mIntent.putExtra(TEST_EXTRA_NAME, stringValue);
825         uri = mIntent.toURI();
826         target = Intent.getIntent(uri);
827         assertEquals(stringValue, target.getStringExtra(TEST_EXTRA_NAME));
828 
829         mIntent.putExtra(TEST_EXTRA_NAME, true);
830         uri = mIntent.toURI();
831         target = Intent.getIntent(uri);
832         assertTrue(target.getBooleanExtra(TEST_EXTRA_NAME, false));
833 
834         final byte b = 1;
835         mIntent.putExtra(TEST_EXTRA_NAME, b);
836 
837         uri = mIntent.toURI();
838         target = Intent.getIntent(uri);
839         final byte defaulttByte = 2;
840         assertEquals(b, target.getByteExtra(TEST_EXTRA_NAME, defaulttByte));
841 
842         final char testChar = 'a';
843         mIntent.putExtra(TEST_EXTRA_NAME, testChar);
844         uri = mIntent.toURI();
845         target = Intent.getIntent(uri);
846         assertEquals(testChar, target.getCharExtra(TEST_EXTRA_NAME, 'b'));
847 
848         final double testDouble = 1;
849         mIntent.putExtra(TEST_EXTRA_NAME, testDouble);
850         uri = mIntent.toURI();
851         target = Intent.getIntent(uri);
852         assertEquals(testDouble, target.getDoubleExtra(TEST_EXTRA_NAME, 2));
853 
854         final int testInt = 1;
855         mIntent.putExtra(TEST_EXTRA_NAME, testInt);
856         uri = mIntent.toURI();
857         target = Intent.getIntent(uri);
858         assertEquals(testInt, target.getIntExtra(TEST_EXTRA_NAME, 2));
859 
860         final long testLong = 1l;
861         mIntent.putExtra(TEST_EXTRA_NAME, testLong);
862         uri = mIntent.toURI();
863         target = Intent.getIntent(uri);
864         assertEquals(testLong, target.getLongExtra(TEST_EXTRA_NAME, 2l));
865 
866         final short testShort = 1;
867         final short defaultShort = 2;
868         mIntent.putExtra(TEST_EXTRA_NAME, testShort);
869         uri = mIntent.toURI();
870         target = Intent.getIntent(uri);
871         assertEquals(testShort, target.getShortExtra(TEST_EXTRA_NAME, defaultShort));
872     }
873 
testToURI()874     public void testToURI() {
875         mIntent = new Intent();
876         assertEquals("", mIntent.toURI());
877 
878         mIntent.setData(TEST_URI);
879         assertTrue(mIntent.toURI().indexOf(TEST_URI.toString()) != -1);
880 
881         mIntent.setAction(TEST_ACTION);
882         assertTrue(mIntent.toURI().indexOf("action=" + TEST_ACTION) != -1);
883 
884         mIntent.addCategory(TEST_CATEGORY);
885         assertTrue(mIntent.toURI().indexOf("category=") != -1);
886 
887         mIntent.setType(TEST_TYPE);
888 
889         assertTrue(mIntent.toURI().indexOf("type=" + TEST_TYPE) != -1);
890 
891         mIntent.setFlags(1);
892         assertFalse(mIntent.toURI().indexOf("launchFlags=" + Integer.toHexString(1)) != -1);
893 
894         mIntent.setComponent(mComponentName);
895         assertTrue(mIntent.toURI().indexOf(
896                 "component=" + mComponentName.flattenToShortString()) != -1);
897 
898         final String stringValue = "testString";
899         mIntent.putExtra(TEST_EXTRA_NAME, stringValue);
900 
901         assertTrue(mIntent.toURI().indexOf(getString("S", TEST_EXTRA_NAME, stringValue)) != -1);
902 
903         mIntent.putExtra(TEST_EXTRA_NAME, true);
904 
905         assertTrue(mIntent.toURI().indexOf(getString("B", TEST_EXTRA_NAME, true)) != -1);
906 
907         final byte b = 1;
908         mIntent.putExtra(TEST_EXTRA_NAME, b);
909         assertTrue(mIntent.toURI().indexOf(getString("b", TEST_EXTRA_NAME, b)) != -1);
910 
911         final Character testChar = 'a';
912         mIntent.putExtra(TEST_EXTRA_NAME, testChar);
913 
914         assertTrue(mIntent.toURI().indexOf(getString("c", TEST_EXTRA_NAME, testChar)) != -1);
915 
916         final double testDouble = 1;
917         mIntent.putExtra(TEST_EXTRA_NAME, testDouble);
918         assertTrue(mIntent.toURI().indexOf(getString("d", TEST_EXTRA_NAME, testDouble)) != -1);
919 
920         final int testInt = 1;
921         mIntent.putExtra(TEST_EXTRA_NAME, testInt);
922         assertTrue(mIntent.toURI().indexOf(getString("i", TEST_EXTRA_NAME, testInt)) != -1);
923 
924         final long testLong = 1l;
925         mIntent.putExtra(TEST_EXTRA_NAME, testLong);
926         assertTrue(mIntent.toURI().indexOf(getString("l", TEST_EXTRA_NAME, testLong)) != -1);
927         final short testShort = 1;
928         mIntent.putExtra(TEST_EXTRA_NAME, testShort);
929         assertTrue(mIntent.toURI().indexOf(getString("s", TEST_EXTRA_NAME, testShort)) != -1);
930         assertTrue(mIntent.toURI().indexOf("end") != -1);
931     }
932 
getString(String entryType, String key, Object value)933     private String getString(String entryType, String key, Object value) {
934         StringBuilder uri = new StringBuilder();
935         uri.append(entryType);
936         uri.append('.');
937         uri.append(Uri.encode(key));
938         uri.append('=');
939         uri.append(Uri.encode(value.toString()));
940         return uri.toString();
941     }
942 
makeSelector(Intent baseIntent, Intent selectorIntent)943     static Intent makeSelector(Intent baseIntent, Intent selectorIntent) {
944         baseIntent.setSelector(selectorIntent);
945         return baseIntent;
946     }
947 
testUris()948     public void testUris() {
949         checkIntentUri(
950                 "intent:#Intent;action=android.test.FOO;end",
951                 null,
952                 new Intent().setAction("android.test.FOO"));
953         checkIntentUri(
954                 "intent:#Intent;category=android.test.FOO;end",
955                 null,
956                 new Intent().setAction(Intent.ACTION_VIEW).addCategory("android.test.FOO"));
957         checkIntentUri(
958                 "intent:#Intent;action=android.test.FOO;launchFlags=0x20;end",
959                 null,
960                 new Intent().setAction("android.test.FOO").setFlags(0x20));
961         checkIntentUri(
962                 "intent://www.example.com/blah#Intent;scheme=http;end",
963                 null,
964                 new Intent().setAction(Intent.ACTION_VIEW)
965                         .setData(Uri.parse("http://www.example.com/blah")));
966         checkIntentUri(
967                 "intent://www.example.com/blah#Intent;scheme=http;component=com.exfoo/com.argh.Bar;end",
968                 null,
969                 new Intent().setAction(Intent.ACTION_VIEW)
970                         .setData(Uri.parse("http://www.example.com/blah"))
971                         .setComponent(new ComponentName("com.exfoo", "com.argh.Bar")));
972         checkIntentUri(
973                 "intent://www.example.com/blah#fragment#Intent;scheme=http;end",
974                 null,
975                 new Intent().setAction(Intent.ACTION_VIEW)
976                         .setData(Uri.parse("http://www.example.com/blah#fragment")));
977         checkIntentUri(
978                 "intent://www.example.com/blah#Intent;scheme=http;action=android.test.foo;end",
979                 null,
980                 new Intent().setAction("android.test.foo")
981                         .setData(Uri.parse("http://www.example.com/blah")));
982         checkIntentUri(
983                 "intent:foo#Intent;scheme=mailto;type=image/foo;end",
984                 null,
985                 new Intent().setAction(Intent.ACTION_VIEW)
986                         .setDataAndType(Uri.parse("mailto:foo"), "image/foo"));
987         checkIntentUri(
988                 "intent:foo#Intent;scheme=mailto;S.string=text;end",
989                 null,
990                 new Intent().setAction(Intent.ACTION_VIEW)
991                         .setData(Uri.parse("mailto:foo"))
992                         .putExtra("string", "text"));
993         checkIntentUri(
994                 "intent:#Intent;action=android.test.FOO;S.string=text;end",
995                 null,
996                 new Intent().setAction("android.test.FOO").putExtra("string", "text"));
997         checkIntentUri(
998                 "intent:foo#Intent;scheme=mailto;i.int=1000;end",
999                 null,
1000                 new Intent().setAction(Intent.ACTION_VIEW)
1001                         .setData(Uri.parse("mailto:foo"))
1002                         .putExtra("int", 1000));
1003         checkIntentUri(
1004                 "intent:foo#Intent;scheme=mailto;l.long=1000;end",
1005                 null,
1006                 new Intent().setAction(Intent.ACTION_VIEW)
1007                         .setData(Uri.parse("mailto:foo"))
1008                         .putExtra("long", (long) 1000));
1009         checkIntentUri(
1010                 "intent:foo#Intent;scheme=mailto;B.boolean=true;end",
1011                 null,
1012                 new Intent().setAction(Intent.ACTION_VIEW)
1013                         .setData(Uri.parse("mailto:foo"))
1014                         .putExtra("boolean", true));
1015         checkIntentUri(
1016                 "intent:foo#Intent;scheme=mailto;f.float=10.4;end",
1017                 null,
1018                 new Intent().setAction(Intent.ACTION_VIEW)
1019                         .setData(Uri.parse("mailto:foo"))
1020                         .putExtra("float", 10.4f));
1021         checkIntentUri(
1022                 "intent:foo#Intent;scheme=mailto;d.double=10.4;end",
1023                 null,
1024                 new Intent().setAction(Intent.ACTION_VIEW)
1025                         .setData(Uri.parse("mailto:foo"))
1026                         .putExtra("double", (double) 10.4));
1027         checkIntentUri(
1028                 "intent:#Intent;S.string=text;i.int=1000;l.long=1000;B.boolean=true;f.float=10.4;end",
1029                 null,
1030                 new Intent().setAction(Intent.ACTION_VIEW).putExtra("string", "text")
1031                         .putExtra("int", 1000).putExtra("long", (long) 1000)
1032                         .putExtra("boolean", true).putExtra("float", 10.4f));
1033         checkIntentUri(
1034                 "intent:foo#Intent;scheme=mailto;SEL;scheme=foobar;action=android.test.FOO;end",
1035                 null,
1036                 makeSelector(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("mailto:foo")),
1037                         new Intent("android.test.FOO").setData(Uri.parse("foobar:"))));
1038         checkIntentUri(
1039                 "intent:foo#Intent;scheme=mailto;SEL;action=android.test.FOO;package=com.myapp;end",
1040                 null,
1041                 makeSelector(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("mailto:foo")),
1042                         new Intent("android.test.FOO").setPackage("com.myapp")));
1043         checkIntentUri(
1044                 "intent:foo#Intent;scheme=mailto;SEL;action=android.test.FOO;component=com.exfoo/com.argh.Bar;end",
1045                 null,
1046                 makeSelector(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("mailto:foo")),
1047                         new Intent("android.test.FOO")
1048                                 .setComponent(new ComponentName("com.exfoo", "com.argh.Bar"))));
1049 
1050         checkIntentUri(
1051                 "intent:#Intent;action=android.test.FOO;package=com.myapp;end",
1052                 "android-app://com.myapp#Intent;action=android.test.FOO;end",
1053                 new Intent().setAction("android.test.FOO").setPackage("com.myapp"));
1054         checkIntentUri(
1055                 "intent:#Intent;action=android.intent.action.MAIN;package=com.myapp;end",
1056                 "android-app://com.myapp",
1057                 new Intent().setAction(Intent.ACTION_MAIN).setPackage("com.myapp"));
1058         checkIntentUri(
1059                 "intent:#Intent;package=com.myapp;end",
1060                 "android-app://com.myapp#Intent;action=android.intent.action.VIEW;end",
1061                 new Intent().setAction(Intent.ACTION_VIEW).setPackage("com.myapp"));
1062         checkIntentUri(
1063                 "intent:#Intent;category=android.test.FOO;package=com.myapp;end",
1064                 "android-app://com.myapp#Intent;action=android.intent.action.VIEW;category=android.test.FOO;end",
1065                 new Intent().setAction(Intent.ACTION_VIEW).addCategory("android.test.FOO")
1066                         .setPackage("com.myapp"));
1067         checkIntentUri(
1068                 "intent:#Intent;action=android.test.FOO;launchFlags=0x20;package=com.myapp;end",
1069                 "android-app://com.myapp#Intent;action=android.test.FOO;launchFlags=0x20;end",
1070                 new Intent().setAction("android.test.FOO").setFlags(0x20)
1071                         .setPackage("com.myapp"));
1072         checkIntentUri(
1073                 "intent://www.example.com/blah#Intent;scheme=http;package=com.myapp;end",
1074                 "android-app://com.myapp/http/www.example.com/blah",
1075                 new Intent().setAction(Intent.ACTION_VIEW)
1076                         .setData(Uri.parse("http://www.example.com/blah"))
1077                         .setPackage("com.myapp"));
1078         checkIntentUri(
1079                 "intent://www.example.com/blah#Intent;scheme=http;package=com.myapp;component=com.exfoo/com.argh.Bar;end",
1080                 "android-app://com.myapp/http/www.example.com/blah#Intent;component=com.exfoo/com.argh.Bar;end",
1081                 new Intent().setAction(Intent.ACTION_VIEW)
1082                         .setData(Uri.parse("http://www.example.com/blah"))
1083                         .setComponent(new ComponentName("com.exfoo", "com.argh.Bar"))
1084                         .setPackage("com.myapp"));
1085         checkIntentUri(
1086                 "intent://www.example.com/blah#fragment#Intent;scheme=http;package=com.myapp;end",
1087                 "android-app://com.myapp/http/www.example.com/blah#fragment",
1088                 new Intent().setAction(Intent.ACTION_VIEW)
1089                         .setData(Uri.parse("http://www.example.com/blah#fragment"))
1090                         .setPackage("com.myapp"));
1091         checkIntentUri(
1092                 "intent://www.example.com/blah#fragment#Intent;scheme=http;action=android.test.FOO;package=com.myapp;end",
1093                 "android-app://com.myapp/http/www.example.com/blah#fragment#Intent;action=android.test.FOO;end",
1094                 new Intent().setAction("android.test.FOO")
1095                         .setData(Uri.parse("http://www.example.com/blah#fragment"))
1096                         .setPackage("com.myapp"));
1097         checkIntentUri(
1098                 "intent://www.example.com/blah#Intent;scheme=http;package=com.myapp;end",
1099                 "android-app://com.myapp/http/www.example.com/blah",
1100                 new Intent().setAction(Intent.ACTION_VIEW)
1101                         .setData(Uri.parse("http://www.example.com/blah"))
1102                         .setPackage("com.myapp"));
1103         checkIntentUri(
1104                 "intent:#Intent;scheme=mailto;type=image/foo;package=com.myapp;end",
1105                 "android-app://com.myapp/mailto#Intent;type=image/foo;end",
1106                 new Intent().setAction(Intent.ACTION_VIEW)
1107                         .setDataAndType(Uri.parse("mailto:"), "image/foo")
1108                         .setPackage("com.myapp"));
1109         checkIntentUri(
1110                 "intent:#Intent;scheme=mailto;package=com.myapp;S.string=text;end",
1111                 "android-app://com.myapp/mailto#Intent;S.string=text;end",
1112                 new Intent().setAction(Intent.ACTION_VIEW).putExtra("string", "text")
1113                         .setData(Uri.parse("mailto:")).setPackage("com.myapp"));
1114         checkIntentUri(
1115                 "intent:#Intent;action=android.test.FOO;package=com.myapp;S.string=text;end",
1116                 "android-app://com.myapp#Intent;action=android.test.FOO;S.string=text;end",
1117                 new Intent().setAction("android.test.FOO").putExtra("string", "text")
1118                         .setPackage("com.myapp"));
1119         checkIntentUri(
1120                 "intent:#Intent;scheme=mailto;package=com.myapp;i.int=1000;end",
1121                 "android-app://com.myapp/mailto#Intent;i.int=1000;end",
1122                 new Intent().setAction(Intent.ACTION_VIEW).putExtra("int", 1000)
1123                         .setData(Uri.parse("mailto:")).setPackage("com.myapp"));
1124         checkIntentUri(
1125                 "intent:#Intent;scheme=mailto;package=com.myapp;l.long=1000;end",
1126                 "android-app://com.myapp/mailto#Intent;l.long=1000;end",
1127                 new Intent().setAction(Intent.ACTION_VIEW).putExtra("long", (long) 1000)
1128                         .setData(Uri.parse("mailto:")).setPackage("com.myapp"));
1129         checkIntentUri(
1130                 "intent:#Intent;scheme=mailto;package=com.myapp;B.boolean=true;end",
1131                 "android-app://com.myapp/mailto#Intent;B.boolean=true;end",
1132                 new Intent().setAction(Intent.ACTION_VIEW).putExtra("boolean", true)
1133                         .setData(Uri.parse("mailto:")).setPackage("com.myapp"));
1134         checkIntentUri(
1135                 "intent:#Intent;scheme=mailto;package=com.myapp;f.float=10.4;end",
1136                 "android-app://com.myapp/mailto#Intent;f.float=10.4;end",
1137                 new Intent().setAction(Intent.ACTION_VIEW).putExtra("float", 10.4f)
1138                         .setData(Uri.parse("mailto:")).setPackage("com.myapp"));
1139         checkIntentUri(
1140                 "intent:#Intent;scheme=mailto;package=com.myapp;d.double=10.4;end",
1141                 "android-app://com.myapp/mailto#Intent;d.double=10.4;end",
1142                 new Intent().setAction(Intent.ACTION_VIEW).putExtra("double", (double) 10.4)
1143                         .setData(Uri.parse("mailto:")).setPackage("com.myapp"));
1144         checkIntentUri(
1145                 "intent:#Intent;package=com.myapp;S.string=text;i.int=1000;l.long=1000;B.boolean=true;f.float=10.4;end",
1146                 "android-app://com.myapp#Intent;action=android.intent.action.VIEW;S.string=text;i.int=1000;l.long=1000;B.boolean=true;f.float=10.4;end",
1147                 new Intent().setAction(Intent.ACTION_VIEW).putExtra("string", "text")
1148                         .putExtra("int", 1000).putExtra("long", (long) 1000)
1149                         .putExtra("boolean", true).putExtra("float", 10.4f)
1150                         .setPackage("com.myapp"));
1151     }
1152 
compareIntents(Intent expected, Intent actual)1153     private boolean compareIntents(Intent expected, Intent actual) {
1154         if (!Objects.equals(expected.getAction(), actual.getAction())) {
1155             return false;
1156         }
1157         if (!Objects.equals(expected.getData(), actual.getData())) {
1158             return false;
1159         }
1160         if (!Objects.equals(expected.getType(), actual.getType())) {
1161             return false;
1162         }
1163         if (!Objects.equals(expected.getPackage(), actual.getPackage())) {
1164             return false;
1165         }
1166         if (!Objects.equals(expected.getComponent(), actual.getComponent())) {
1167             return false;
1168         }
1169         if (expected.getFlags() != actual.getFlags()) {
1170             return false;
1171         }
1172         Set<String> expectedCat = expected.getCategories();
1173         Set<String> actualCat = actual.getCategories();
1174         if (expectedCat != actualCat) {
1175             if (expectedCat == null || actualCat == null) {
1176                 return false;
1177             }
1178             for (String cat : expectedCat) {
1179                 if (!actual.hasCategory(cat)) {
1180                     return false;
1181                 }
1182             }
1183             for (String cat : actualCat) {
1184                 if (!expected.hasCategory(cat)) {
1185                     return false;
1186                 }
1187             }
1188         }
1189         Bundle extras1 = expected.getExtras();
1190         Bundle extras2 = actual.getExtras();
1191         if (extras1 != extras2) {
1192             if (extras1 == null || extras2 == null) {
1193                 return false;
1194             }
1195             for (String key : extras1.keySet()) {
1196                 if (!Objects.equals(extras1.get(key), extras2.get(key))) {
1197                     return false;
1198                 }
1199             }
1200             for (String key : extras2.keySet()) {
1201                 if (!Objects.equals(extras1.get(key), extras2.get(key))) {
1202                     return false;
1203                 }
1204             }
1205         }
1206         return true;
1207     }
1208 
assertEqualsIntent(String msg, Intent expected, Intent actual)1209     private void assertEqualsIntent(String msg, Intent expected, Intent actual) {
1210         if (!compareIntents(expected, actual)) {
1211             failNotEquals(msg, expected, actual);
1212         }
1213         Intent expectedSel = expected.getSelector();
1214         Intent actualSel = actual.getSelector();
1215         if (expectedSel != actualSel) {
1216             if (expectedSel == null || actualSel == null) {
1217                 failNotEquals(msg, expected, actual);
1218             }
1219             if (!compareIntents(expectedSel, actualSel)) {
1220                 failNotEquals(msg, expected, actual);
1221             }
1222         }
1223     }
1224 
checkIntentUri(String intentSchemeUri, String androidAppSchemeUri, Intent intent)1225     private void checkIntentUri(String intentSchemeUri, String androidAppSchemeUri, Intent intent) {
1226         if (intentSchemeUri != null) {
1227             try {
1228                 Intent genIntent = Intent.parseUri(intentSchemeUri, 0);
1229                 assertEqualsIntent("Implicitly converting " + intentSchemeUri + " to Intent",
1230                         intent, genIntent);
1231                 genIntent = Intent.parseUri(intentSchemeUri, Intent.URI_INTENT_SCHEME);
1232                 assertEqualsIntent("Explicitly converting " + intentSchemeUri + " to Intent",
1233                         intent, genIntent);
1234             } catch (URISyntaxException e) {
1235                 fail("Failure parsing " + intentSchemeUri + ": " + e);
1236             }
1237             String genUri = intent.toUri(Intent.URI_INTENT_SCHEME);
1238             assertEquals("Converting " + intent + " to intent: uri",
1239                     intentSchemeUri, genUri);
1240         }
1241         if (androidAppSchemeUri != null) {
1242             try {
1243                 Intent genIntent = Intent.parseUri(androidAppSchemeUri, 0);
1244                 assertEqualsIntent("Implicitly converting " + androidAppSchemeUri + " to Intent",
1245                         intent, genIntent);
1246                 genIntent = Intent.parseUri(intentSchemeUri, Intent.URI_ANDROID_APP_SCHEME);
1247                 assertEqualsIntent("Explicitly converting " + androidAppSchemeUri + " to Intent",
1248                         intent, genIntent);
1249             } catch (URISyntaxException e) {
1250                 fail("Failure parsing " + androidAppSchemeUri + ": " + e);
1251             }
1252             String genUri = intent.toUri(Intent.URI_ANDROID_APP_SCHEME);
1253             assertEquals("Converting " + intent + " to android-app: uri",
1254                     androidAppSchemeUri, genUri);
1255         }
1256     }
1257 
testAccessFlags()1258     public void testAccessFlags() {
1259         int expected = 1;
1260         mIntent.setFlags(expected);
1261         assertEquals(expected, mIntent.getFlags());
1262     }
1263 
testCreateChooser()1264     public void testCreateChooser() {
1265         Intent target = Intent.createChooser(mIntent, null);
1266         assertEquals(Intent.ACTION_CHOOSER, target.getAction());
1267         Intent returnIntent = (Intent) target.getParcelableExtra(Intent.EXTRA_INTENT);
1268         assertEquals(mIntent.toString(), returnIntent.toString());
1269         assertEquals(mIntent.toURI(), returnIntent.toURI());
1270         assertNull(returnIntent.getStringExtra(Intent.EXTRA_INTENT));
1271         final String title = "title String";
1272         target = Intent.createChooser(mIntent, title);
1273         assertEquals(title, target.getStringExtra(Intent.EXTRA_TITLE));
1274     }
1275 
testGetFloatArrayExtra()1276     public void testGetFloatArrayExtra() {
1277         final float[] expected = { 1f, 2f, 3f };
1278         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1279         assertEquals(expected, mIntent.getFloatArrayExtra(TEST_EXTRA_NAME));
1280     }
1281 
testSetDataAndType()1282     public void testSetDataAndType() {
1283         mIntent.setDataAndType(TEST_URI, TEST_TYPE);
1284         assertEquals(TEST_URI, mIntent.getData());
1285         assertEquals(TEST_TYPE, mIntent.getType());
1286     }
1287 
testSetData()1288     public void testSetData() {
1289         mIntent.setData(TEST_URI);
1290         assertEquals(TEST_URI, mIntent.getData());
1291         assertNull(mIntent.getType());
1292 
1293         mIntent.setType(TEST_TYPE);
1294         mIntent.setData(TEST_URI);
1295         assertEquals(TEST_URI, mIntent.getData());
1296         assertNull(mIntent.getType());
1297     }
1298 
testSetType()1299     public void testSetType() {
1300         mIntent.setType(TEST_TYPE);
1301         assertEquals(TEST_TYPE, mIntent.getType());
1302         assertNull(mIntent.getData());
1303 
1304         mIntent.setData(TEST_URI);
1305         mIntent.setType(TEST_TYPE);
1306         assertEquals(TEST_TYPE, mIntent.getType());
1307         assertNull(mIntent.getData());
1308     }
1309 
testGetStringExtra()1310     public void testGetStringExtra() {
1311         final String expected = "testString";
1312         assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME));
1313         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1314         assertEquals(expected, mIntent.getStringExtra(TEST_EXTRA_NAME));
1315     }
1316 
1317     /**
1318      * Test that fillIn has no effect when no fields are set.
1319      */
testFillIn_blank()1320     public void testFillIn_blank() {
1321         Intent sourceIntent = new Intent();
1322         Intent destIntent = new Intent();
1323         assertEquals(0, destIntent.fillIn(sourceIntent, Intent.FILL_IN_ACTION));
1324         assertEquals(0, destIntent.fillIn(sourceIntent, 0));
1325         assertNull(destIntent.getAction());
1326     }
1327 
1328     /**
1329      * Test that fillIn copies the action field.
1330      */
testFillIn_action()1331     public void testFillIn_action() {
1332         Intent sourceIntent = new Intent();
1333         Intent destIntent = new Intent();
1334         // test action copied when null
1335         sourceIntent.setAction(TEST_ACTION);
1336         assertEquals(Intent.FILL_IN_ACTION, destIntent.fillIn(sourceIntent, 0));
1337         assertEquals(TEST_ACTION, destIntent.getAction());
1338     }
1339 
1340     /**
1341      * Test that fillIn does not copy action when its already set in target Intent.
1342      */
testFillIn_actionSet()1343     public void testFillIn_actionSet() {
1344         Intent sourceIntent = new Intent();
1345         Intent destIntent = new Intent();
1346         final String newAction = "foo";
1347         sourceIntent = new Intent();
1348         sourceIntent.setAction(newAction);
1349         destIntent.setAction(TEST_ACTION);
1350 
1351         assertEquals(0, destIntent.fillIn(sourceIntent, 0));
1352         assertEquals(TEST_ACTION, destIntent.getAction());
1353     }
1354 
1355     /**
1356      * Test that fillIn copies action when {@link Intent#FILL_IN_ACTION} flag is set.
1357      */
testFillIn_actionOverride()1358     public void testFillIn_actionOverride() {
1359         Intent sourceIntent = new Intent();
1360         Intent destIntent = new Intent();
1361         final String newAction = "foo";
1362         sourceIntent = new Intent();
1363         sourceIntent.setAction(newAction);
1364         destIntent.setAction(TEST_ACTION);
1365 
1366         assertEquals(Intent.FILL_IN_ACTION, destIntent.fillIn(sourceIntent, Intent.FILL_IN_ACTION));
1367         assertEquals(newAction, destIntent.getAction());
1368     }
1369 
1370     /**
1371      * Test that fillIn copies data.
1372      */
testFillIn_data()1373     public void testFillIn_data() {
1374         Intent sourceIntent = new Intent();
1375         Intent destIntent = new Intent();
1376         sourceIntent.setData(TEST_URI);
1377         assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, 0));
1378         assertEquals(TEST_URI, destIntent.getData());
1379     }
1380 
1381     /**
1382      * Test that fillIn does not copy data when already its already set in target Intent.
1383      */
testFillIn_dataSet()1384     public void testFillIn_dataSet() {
1385         Intent sourceIntent = new Intent();
1386         Intent destIntent = new Intent();
1387         sourceIntent.setData(TEST_URI);
1388         destIntent.setData(ANOTHER_TEST_URI);
1389         assertEquals(0, destIntent.fillIn(sourceIntent, 0));
1390         assertEquals(ANOTHER_TEST_URI, destIntent.getData());
1391     }
1392 
1393     /**
1394      * Test that fillIn overrides data when {@link Intent#FILL_IN_DATA} flag is set.
1395      */
testFillIn_dataOverride()1396     public void testFillIn_dataOverride() {
1397         Intent sourceIntent = new Intent();
1398         Intent destIntent = new Intent();
1399         sourceIntent.setData(TEST_URI);
1400         destIntent.setData(ANOTHER_TEST_URI);
1401         assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, Intent.FILL_IN_DATA));
1402         assertEquals(TEST_URI, destIntent.getData());
1403     }
1404 
1405     /**
1406      * Test that fillIn copies data type.
1407      */
testFillIn_dataType()1408     public void testFillIn_dataType() {
1409         Intent sourceIntent = new Intent();
1410         Intent destIntent = new Intent();
1411         sourceIntent.setType(TEST_TYPE);
1412         assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, 0));
1413         assertEquals(TEST_TYPE, destIntent.getType());
1414     }
1415 
1416     /**
1417      * Test that fillIn does not copy data type when already its already set in target Intent.
1418      */
testFillIn_dataTypeSet()1419     public void testFillIn_dataTypeSet() {
1420         Intent sourceIntent = new Intent();
1421         Intent destIntent = new Intent();
1422         sourceIntent.setType(TEST_TYPE);
1423         destIntent.setType(ANOTHER_TEST_TYPE);
1424         assertEquals(0, destIntent.fillIn(sourceIntent, 0));
1425         assertEquals(ANOTHER_TEST_TYPE, destIntent.getType());
1426     }
1427 
1428     /**
1429      * Test that fillIn overrides data type when {@link Intent#FILL_IN_DATA} flag is set.
1430      */
testFillIn_dataTypeOverride()1431     public void testFillIn_dataTypeOverride() {
1432         Intent sourceIntent = new Intent();
1433         Intent destIntent = new Intent();
1434         sourceIntent.setType(TEST_TYPE);
1435         destIntent.setType(ANOTHER_TEST_TYPE);
1436         assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, Intent.FILL_IN_DATA));
1437         assertEquals(TEST_TYPE, destIntent.getType());
1438     }
1439 
1440     /**
1441      * Test component is not copied by fillIn method when {@link Intent#FILL_IN_COMPONENT} flag is
1442      * not set.
1443      */
testFillIn_componentNoCopy()1444     public void testFillIn_componentNoCopy() {
1445         Intent sourceIntent = new Intent();
1446         Intent destIntent = new Intent();
1447         sourceIntent.setComponent(mComponentName);
1448         assertEquals(0, destIntent.fillIn(sourceIntent, 0));
1449         assertEquals(null, destIntent.getComponent());
1450     }
1451 
1452     /**
1453      * Test that fillIn copies component when {@link Intent#FILL_IN_COMPONENT} flag is set.
1454      */
testFillIn_componentOverride()1455     public void testFillIn_componentOverride() {
1456         Intent sourceIntent = new Intent();
1457         Intent destIntent = new Intent();
1458         sourceIntent.setComponent(mComponentName);
1459         destIntent.setComponent(mAnotherComponentName);
1460         assertEquals(Intent.FILL_IN_COMPONENT, destIntent.fillIn(sourceIntent,
1461                 Intent.FILL_IN_COMPONENT));
1462         assertEquals(mComponentName, destIntent.getComponent());
1463     }
1464 
1465     /**
1466      * Test that fillIn copies categories.
1467      */
testFillIn_category()1468     public void testFillIn_category() {
1469         Intent sourceIntent = new Intent();
1470         Intent destIntent = new Intent();
1471         // add two categories to ensure all categories are copied
1472         sourceIntent.addCategory(TEST_CATEGORY);
1473         sourceIntent.addCategory(ANOTHER_TEST_CATEGORY);
1474         assertEquals(Intent.FILL_IN_CATEGORIES, destIntent.fillIn(sourceIntent, 0));
1475         assertEquals(2, destIntent.getCategories().size());
1476         assertTrue(destIntent.getCategories().contains(TEST_CATEGORY));
1477         assertTrue(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY));
1478     }
1479 
1480     /**
1481      * Test fillIn does not copy categories by default when already set.
1482      */
testFillIn_categorySet()1483     public void testFillIn_categorySet() {
1484         Intent sourceIntent = new Intent();
1485         Intent destIntent = new Intent();
1486         sourceIntent = new Intent();
1487         sourceIntent.addCategory(TEST_CATEGORY);
1488         destIntent.addCategory(ANOTHER_TEST_CATEGORY);
1489 
1490         assertEquals(0, destIntent.fillIn(sourceIntent, 0));
1491         assertEquals(1, destIntent.getCategories().size());
1492         assertTrue(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY));
1493         assertFalse(destIntent.getCategories().contains(TEST_CATEGORY));
1494     }
1495 
1496     /**
1497      * Test that fillIn adds categories when {@link Intent#FILL_IN_CATEGORIES} flag is set.
1498      */
testFillIn_categoryOverride()1499     public void testFillIn_categoryOverride() {
1500         Intent sourceIntent = new Intent();
1501         Intent destIntent = new Intent();
1502         sourceIntent = new Intent();
1503         sourceIntent.addCategory(TEST_CATEGORY);
1504         destIntent.addCategory(ANOTHER_TEST_CATEGORY);
1505 
1506         assertEquals(Intent.FILL_IN_CATEGORIES, destIntent.fillIn(sourceIntent, Intent.FILL_IN_CATEGORIES));
1507         assertEquals(1, destIntent.getCategories().size());
1508         assertFalse(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY));
1509         assertTrue(destIntent.getCategories().contains(TEST_CATEGORY));
1510     }
1511 
1512     /**
1513      * Test fillIn copies package.
1514      */
testFillIn_package()1515     public void testFillIn_package() {
1516         Intent sourceIntent = new Intent();
1517         Intent destIntent = new Intent();
1518         sourceIntent.setPackage(TEST_PACKAGE);
1519         assertEquals(Intent.FILL_IN_PACKAGE, destIntent.fillIn(sourceIntent, 0));
1520         assertEquals(TEST_PACKAGE, destIntent.getPackage());
1521     }
1522 
1523     /**
1524      * Test fillIn does not copy package by default when already set.
1525      */
testFillIn_packageSet()1526     public void testFillIn_packageSet() {
1527         Intent sourceIntent = new Intent();
1528         Intent destIntent = new Intent();
1529         sourceIntent.setPackage(TEST_PACKAGE);
1530         destIntent.setPackage(ANOTHER_TEST_PACKAGE);
1531         assertEquals(0, destIntent.fillIn(sourceIntent, 0));
1532         assertEquals(ANOTHER_TEST_PACKAGE, destIntent.getPackage());
1533     }
1534 
1535     /**
1536      * Test that fillIn overrides package when {@link Intent#FILL_IN_PACKAGE} flag is set.
1537      */
testFillIn_packageOverride()1538     public void testFillIn_packageOverride() {
1539         Intent sourceIntent = new Intent();
1540         Intent destIntent = new Intent();
1541         sourceIntent.setPackage(TEST_PACKAGE);
1542         destIntent.setPackage(ANOTHER_TEST_PACKAGE);
1543         assertEquals(Intent.FILL_IN_PACKAGE, destIntent.fillIn(sourceIntent, Intent.FILL_IN_PACKAGE));
1544         assertEquals(TEST_PACKAGE, destIntent.getPackage());
1545     }
1546 
1547     /**
1548      * Test that fillIn copies extras.
1549      */
testFillIn_extras()1550     public void testFillIn_extras() {
1551         Intent sourceIntent = new Intent();
1552         Intent destIntent = new Intent();
1553         final Bundle bundle = new Bundle();
1554         bundle.putBoolean(TEST_EXTRA_NAME, true);
1555         sourceIntent.putExtras(bundle);
1556         assertEquals(0, destIntent.fillIn(sourceIntent, 0));
1557         assertTrue(destIntent.getExtras().getBoolean(TEST_EXTRA_NAME));
1558     }
1559 
testGetExtras()1560     public void testGetExtras() {
1561         assertNull(mIntent.getExtras());
1562         final String expected = "testString";
1563         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1564         assertNotNull(mIntent.getExtras());
1565         assertEquals(expected, mIntent.getExtras().getString(TEST_EXTRA_NAME));
1566     }
1567 
testGetBooleanExtra()1568     public void testGetBooleanExtra() {
1569         assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false));
1570         mIntent.putExtra(TEST_EXTRA_NAME, true);
1571         assertTrue(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false));
1572         mIntent.putExtra(TEST_EXTRA_NAME, false);
1573         assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false));
1574     }
1575 
testGetFloatExtra()1576     public void testGetFloatExtra() {
1577         float expected = 2f;
1578         assertEquals(1f, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f));
1579         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1580         assertEquals(expected, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f));
1581     }
1582 
testGetShortArrayExtra()1583     public void testGetShortArrayExtra() {
1584         final short[] expected = { 1, 2, 3 };
1585         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1586         assertEquals(expected, mIntent.getShortArrayExtra(TEST_EXTRA_NAME));
1587     }
1588 
testGetStringArrayExtra()1589     public void testGetStringArrayExtra() {
1590         final String[] expected = { "1d", "2d", "3d" };
1591         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1592         assertEquals(expected, mIntent.getStringArrayExtra(TEST_EXTRA_NAME));
1593     }
1594 
testGetCharSequenceArrayExtra()1595     public void testGetCharSequenceArrayExtra() {
1596         final String[] expected = { "1d", "2d", "3d" };
1597         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1598         assertEquals(expected, mIntent.getCharSequenceArrayExtra(TEST_EXTRA_NAME));
1599     }
1600 
testGetByteArrayExtra()1601     public void testGetByteArrayExtra() {
1602         final byte[] expected = TEST_ACTION.getBytes();
1603         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1604         assertEquals(expected, mIntent.getByteArrayExtra(TEST_EXTRA_NAME));
1605     }
1606 
testHasFileDescriptors()1607     public void testHasFileDescriptors() {
1608         Bundle bundle = mIntent.getExtras();
1609         assertEquals(bundle != null && bundle.hasFileDescriptors(), mIntent.hasFileDescriptors());
1610         final byte[] expected = TEST_ACTION.getBytes();
1611         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1612         bundle = mIntent.getExtras();
1613         assertEquals(bundle != null && bundle.hasFileDescriptors(), mIntent.hasFileDescriptors());
1614     }
1615 
testGetBooleanArrayExtra()1616     public void testGetBooleanArrayExtra() {
1617         final boolean[] expected = { true, true, false };
1618         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1619         assertEquals(expected, mIntent.getBooleanArrayExtra(TEST_EXTRA_NAME));
1620     }
1621 
testGetLongExtra()1622     public void testGetLongExtra() {
1623         final long expected = 2l;
1624         assertEquals(1l, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l));
1625         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1626         assertEquals(expected, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l));
1627     }
1628 
testRemoveCategory()1629     public void testRemoveCategory() {
1630         assertNull(mIntent.getCategories());
1631         mIntent.addCategory(TEST_CATEGORY);
1632         assertNotNull(mIntent.getCategories());
1633         assertEquals(1, mIntent.getCategories().size());
1634         assertEquals(TEST_CATEGORY, (String) (mIntent.getCategories().toArray()[0]));
1635         mIntent.removeCategory(TEST_CATEGORY);
1636         assertFalse(mIntent.hasCategory(TEST_CATEGORY));
1637     }
1638 
testFilterEquals()1639     public void testFilterEquals() {
1640         assertFalse(mIntent.filterEquals(null));
1641 
1642         Intent target = new Intent();
1643         assertTrue(mIntent.filterEquals(target));
1644 
1645         target.setAction(TEST_ACTION);
1646         assertFalse(mIntent.filterEquals(target));
1647         mIntent.setAction(TEST_ACTION + "test");
1648         assertFalse(mIntent.filterEquals(target));
1649         mIntent.setAction(null);
1650         assertFalse(mIntent.filterEquals(target));
1651         mIntent.setAction(TEST_ACTION);
1652         assertTrue(mIntent.filterEquals(target));
1653 
1654         target.setData(TEST_URI);
1655         assertFalse(mIntent.filterEquals(target));
1656         mIntent.setData(Uri.parse("myURI"));
1657         assertFalse(mIntent.filterEquals(target));
1658         mIntent.setData(null);
1659         assertFalse(mIntent.filterEquals(target));
1660         mIntent.setData(TEST_URI);
1661         assertTrue(mIntent.filterEquals(target));
1662 
1663         target.setType(TEST_TYPE);
1664         assertFalse(mIntent.filterEquals(target));
1665         mIntent.setType(TEST_TYPE + "test");
1666         assertFalse(mIntent.filterEquals(target));
1667         mIntent.setType(null);
1668         assertFalse(mIntent.filterEquals(target));
1669         mIntent.setType(TEST_TYPE);
1670         assertTrue(mIntent.filterEquals(target));
1671 
1672         target.setComponent(mComponentName);
1673         assertFalse(mIntent.filterEquals(target));
1674         mIntent.setComponent(new ComponentName(mContext, MockReceiver.class));
1675         assertFalse(mIntent.filterEquals(target));
1676         mIntent.setComponent(null);
1677         assertFalse(mIntent.filterEquals(target));
1678         mIntent.setComponent(mComponentName);
1679         assertTrue(mIntent.filterEquals(target));
1680 
1681         target.addCategory(TEST_CATEGORY);
1682         assertFalse(mIntent.filterEquals(target));
1683         mIntent.addCategory(TEST_CATEGORY + "test");
1684         assertFalse(mIntent.filterEquals(target));
1685         mIntent.addCategory(TEST_CATEGORY);
1686         assertFalse(mIntent.filterEquals(target));
1687     }
1688 
testPutExtras1()1689     public void testPutExtras1() {
1690         final Intent intent = new Intent();
1691         mIntent.putExtras(intent);
1692         assertEquals(intent.getExtras(), mIntent.getExtras());
1693         intent.putExtra("test2", true);
1694         mIntent.putExtras(intent);
1695         assertEquals(intent.getExtras().toString(), mIntent.getExtras().toString());
1696     }
1697 
testPutExtras2()1698     public void testPutExtras2() {
1699         final Bundle bundle = new Bundle();
1700         mIntent.putExtras(bundle);
1701         assertEquals(0, mIntent.getExtras().size());
1702         String expected = "testString";
1703         bundle.putString(TEST_EXTRA_NAME, expected);
1704         mIntent.putExtras(bundle);
1705         assertEquals(1, mIntent.getExtras().size());
1706         assertEquals(expected, mIntent.getExtras().getString(TEST_EXTRA_NAME));
1707         mIntent.putExtra(TEST_EXTRA_NAME, bundle);
1708         assertEquals(bundle, mIntent.getBundleExtra(TEST_EXTRA_NAME));
1709     }
1710 
testGetByteExtra()1711     public void testGetByteExtra() {
1712         final byte expected = Byte.valueOf("1");
1713         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1714         assertEquals(expected, mIntent.getByteExtra(TEST_EXTRA_NAME, Byte.valueOf("1")));
1715     }
1716 
testGetSerializableExtra()1717     public void testGetSerializableExtra() {
1718         TestSerializable expected = new TestSerializable();
1719         expected.Name = "testName";
1720         mIntent.putExtra(TEST_EXTRA_NAME, expected);
1721         assertEquals(expected, mIntent.getSerializableExtra(TEST_EXTRA_NAME));
1722         TestSerializable target = (TestSerializable) mIntent.getSerializableExtra(TEST_EXTRA_NAME);
1723         assertEquals(expected.Name, target.Name);
1724     }
1725 
testReplaceExtras()1726     public void testReplaceExtras() {
1727         Bundle extras = new Bundle();
1728         String bundleKey = "testKey";
1729         String bundleValue = "testValue";
1730         extras.putString(bundleKey, bundleValue);
1731 
1732         Intent intent = mIntent.replaceExtras(extras);
1733         assertSame(mIntent, intent);
1734         String actualValue = intent.getExtras().getString(bundleKey);
1735         assertEquals(bundleValue, actualValue);
1736 
1737         Intent src = new Intent();
1738         String intentName = "srcName";
1739         String intentValue = "srcValue";
1740         src.putExtra(intentName, intentValue);
1741 
1742         intent = mIntent.replaceExtras(src);
1743         assertSame(mIntent, intent);
1744         actualValue = intent.getExtras().getString(intentName);
1745         assertEquals(intentValue, actualValue);
1746     }
1747 
testNormalizeMimeType()1748     public void testNormalizeMimeType() {
1749         assertEquals(null, Intent.normalizeMimeType(null));
1750         assertEquals("text/plain", Intent.normalizeMimeType("text/plain; charset=UTF-8"));
1751         assertEquals("text/x-vcard", Intent.normalizeMimeType("text/x-vCard"));
1752         assertEquals("foo/bar", Intent.normalizeMimeType("   foo/bar    "));
1753     }
1754 
testRemoveUnsafeExtras()1755     public void testRemoveUnsafeExtras() {
1756         final Intent intent = new Intent();
1757         final DummyParcelable dummyParcelable = new DummyParcelable();
1758         intent.removeUnsafeExtras();
1759         assertNull(intent.getExtras());
1760 
1761         // Check that removeUnsafeExtras keeps the same bundle if no changes are made.
1762         Bundle origExtras = new Bundle();
1763         origExtras.putString("foo", "bar");
1764         intent.replaceExtras(origExtras);
1765         intent.removeUnsafeExtras();
1766         Bundle newExtras = intent.getExtras();
1767         assertEquals(1, newExtras.size());
1768         assertEquals("bar", newExtras.get("foo"));
1769 
1770         // Check that removeUnsafeExtras will strip non-framework parcelables without modifying
1771         // the original extras bundle.
1772         origExtras.putParcelable("baddy", dummyParcelable);
1773         intent.replaceExtras(origExtras);
1774         intent.removeUnsafeExtras();
1775         newExtras = intent.getExtras();
1776         assertEquals(1, newExtras.size());
1777         assertEquals("bar", newExtras.get("foo"));
1778         assertEquals(2, origExtras.size());
1779         assertEquals("bar", origExtras.get("foo"));
1780         assertSame(dummyParcelable, origExtras.get("baddy"));
1781 
1782         // Check that nested bad values will be stripped.
1783         Bundle origSubExtras = new Bundle();
1784         origSubExtras.putParcelable("baddy", dummyParcelable);
1785         origExtras.putBundle("baddy", origSubExtras);
1786         intent.replaceExtras(origExtras);
1787         intent.removeUnsafeExtras();
1788         newExtras = intent.getExtras();
1789         assertEquals(2, newExtras.size());
1790         assertEquals("bar", newExtras.get("foo"));
1791         Bundle newSubExtras = newExtras.getBundle("baddy");
1792         assertNotSame(origSubExtras, newSubExtras);
1793         assertEquals(0, newSubExtras.size());
1794         assertEquals(1, origSubExtras.size());
1795         assertSame(dummyParcelable, origSubExtras.get("baddy"));
1796     }
1797 
1798     private static class TestSerializable implements Serializable {
1799         static final long serialVersionUID = 1l;
1800         public String Name;
1801     }
1802 }
1803