• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.res;
2 
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.assertThat;
5 
6 import com.xtremelabs.robolectric.Robolectric;
7 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
8 import org.junit.After;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 
13 import android.content.ComponentName;
14 import android.content.Intent;
15 import android.content.pm.ApplicationInfo;
16 import android.content.pm.PackageInfo;
17 import android.content.pm.PackageManager;
18 import android.content.pm.ResolveInfo;
19 import android.graphics.drawable.BitmapDrawable;
20 import android.graphics.drawable.Drawable;
21 
22 import java.util.List;
23 
24 @RunWith(WithTestDefaultsRunner.class)
25 public class RobolectricPackageManagerTest {
26 
27     private static final String TEST_PACKAGE_NAME = "com.some.other.package";
28     private static final String TEST_PACKAGE_LABEL = "My Little App";
29 
30     RobolectricPackageManager rpm;
31 
32     @Before
setUp()33     public void setUp() throws Exception {
34         rpm = (RobolectricPackageManager) Robolectric.application.getPackageManager();
35     }
36 
37     @After
tearDown()38     public void tearDown() throws Exception {
39     }
40 
41     @Test
getApplicationInfo__ThisApplication()42     public void getApplicationInfo__ThisApplication() throws Exception {
43         ApplicationInfo info = rpm.getApplicationInfo(Robolectric.application.getPackageName(), 0);
44         assertThat(info, notNullValue());
45         assertThat(info.packageName, equalTo(Robolectric.application.getPackageName()));
46     }
47 
48     @Test
getApplicationInfo__OtherApplication()49     public void getApplicationInfo__OtherApplication() throws Exception {
50         PackageInfo packageInfo = new PackageInfo();
51         packageInfo.packageName = TEST_PACKAGE_NAME;
52         packageInfo.applicationInfo = new ApplicationInfo();
53         packageInfo.applicationInfo.packageName = TEST_PACKAGE_NAME;
54         packageInfo.applicationInfo.name = TEST_PACKAGE_LABEL;
55         rpm.addPackage(packageInfo);
56 
57         ApplicationInfo info = rpm.getApplicationInfo(TEST_PACKAGE_NAME, 0);
58         assertThat(info, notNullValue());
59         assertThat(info.packageName, equalTo(TEST_PACKAGE_NAME));
60         assertThat(rpm.getApplicationLabel(info).toString(), equalTo(TEST_PACKAGE_LABEL));
61     }
62 
63     @Test
queryIntentActivities__EmptyResult()64     public void queryIntentActivities__EmptyResult() throws Exception {
65         Intent i = new Intent(Intent.ACTION_MAIN, null);
66         i.addCategory(Intent.CATEGORY_LAUNCHER);
67 
68         List<ResolveInfo> activities = rpm.queryIntentActivities(i, 0);
69         assertThat(activities, notNullValue());        // empty list, not null
70         assertThat(activities.size(), equalTo(0));
71     }
72 
73     @Test
queryIntentActivities__Match()74     public void queryIntentActivities__Match() throws Exception {
75         Intent i = new Intent(Intent.ACTION_MAIN, null);
76         i.addCategory(Intent.CATEGORY_LAUNCHER);
77 
78         ResolveInfo info = new ResolveInfo();
79         info.nonLocalizedLabel = TEST_PACKAGE_LABEL;
80 
81         rpm.addResolveInfoForIntent(i, info);
82 
83         List<ResolveInfo> activities = rpm.queryIntentActivities(i, 0);
84         assertThat(activities, notNullValue());
85         assertThat(activities.size(), equalTo(1));
86         assertThat(activities.get(0).nonLocalizedLabel.toString(), equalTo(TEST_PACKAGE_LABEL));
87     }
88 
89     @Test
queryBroadcastReceivers__EmptyResult()90     public void queryBroadcastReceivers__EmptyResult() throws Exception {
91         Intent i = new Intent(Intent.ACTION_MAIN, null);
92         i.addCategory(Intent.CATEGORY_LAUNCHER);
93 
94         List<ResolveInfo> broadCastReceivers = rpm.queryBroadcastReceivers(i, 0);
95         assertThat(broadCastReceivers.size(), equalTo(0));
96     }
97 
98     @Test
queryBroadcastReceivers__Match()99     public void queryBroadcastReceivers__Match() throws Exception {
100         Intent i = new Intent(Intent.ACTION_MAIN, null);
101 
102         ResolveInfo info = new ResolveInfo();
103         info.nonLocalizedLabel = TEST_PACKAGE_LABEL;
104 
105         rpm.addResolveInfoForIntent(i, info);
106 
107         List<ResolveInfo> broadCastReceivers = rpm.queryBroadcastReceivers(i, 0);
108         assertThat(broadCastReceivers.size(), equalTo(1));
109         assertThat(broadCastReceivers.get(0).nonLocalizedLabel.toString(),
110                 equalTo(TEST_PACKAGE_LABEL));
111     }
112 
113     @Test
resolveActivity__Match()114     public void resolveActivity__Match() throws Exception {
115         Intent i = new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER);
116         ResolveInfo info = new ResolveInfo();
117         info.nonLocalizedLabel = TEST_PACKAGE_LABEL;
118         rpm.addResolveInfoForIntent(i, info);
119 
120         assertThat(rpm.resolveActivity(i, 0), sameInstance(info));
121     }
122 
123     @Test
resolveActivity__NoMatch()124     public void resolveActivity__NoMatch() throws Exception {
125         Intent i = new Intent();
126         i.setComponent(new ComponentName("foo.bar", "No Activity"));
127         assertThat(rpm.resolveActivity(i, 0), nullValue());
128     }
129 
130     @Test
resolveService__Match()131     public void resolveService__Match() throws Exception {
132         Intent i = new Intent(Intent.ACTION_MAIN, null);
133         i.addCategory(Intent.CATEGORY_LAUNCHER);
134 
135         ResolveInfo info = new ResolveInfo();
136         info.nonLocalizedLabel = TEST_PACKAGE_LABEL;
137         rpm.addResolveInfoForIntent(i, info);
138 
139         assertThat(rpm.resolveService(i, 0), sameInstance(info));
140     }
141 
142     @Test
resolveService__NoMatch()143     public void resolveService__NoMatch() throws Exception {
144         Intent i = new Intent();
145         i.setComponent(new ComponentName("foo.bar", "No Activity"));
146         assertThat(rpm.resolveService(i, 0), nullValue());
147     }
148 
149     @Test
queryActivityIcons__Match()150     public void queryActivityIcons__Match() throws Exception {
151         Intent i = rpm.getLaunchIntentForPackage(TEST_PACKAGE_NAME);
152         Drawable d = new BitmapDrawable();
153 
154         rpm.addActivityIcon(i, d);
155 
156         assertThat(rpm.getActivityIcon(i), sameInstance(d));
157         assertThat(rpm.getActivityIcon(i.getComponent()), sameInstance(d));
158     }
159 
160     @Test
hasSystemFeature()161     public void hasSystemFeature() throws Exception {
162         // uninitialized
163         assertThat(rpm.hasSystemFeature(PackageManager.FEATURE_CAMERA), equalTo(false));
164 
165         // positive
166         rpm.setSystemFeature(PackageManager.FEATURE_CAMERA, true);
167         assertThat(rpm.hasSystemFeature(PackageManager.FEATURE_CAMERA), equalTo(true));
168 
169         // negative
170         rpm.setSystemFeature(PackageManager.FEATURE_CAMERA, false);
171         assertThat(rpm.hasSystemFeature(PackageManager.FEATURE_CAMERA), equalTo(false));
172     }
173 
174 }
175