• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.pm.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNotSame;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 
26 import android.content.Context;
27 import android.content.pm.ApplicationInfo;
28 import android.content.pm.ComponentInfo;
29 import android.content.pm.PackageManager;
30 import android.content.pm.PackageManager.NameNotFoundException;
31 import android.graphics.Bitmap;
32 import android.graphics.Canvas;
33 import android.graphics.drawable.Drawable;
34 import android.os.Parcel;
35 import android.platform.test.annotations.AppModeSdkSandbox;
36 import android.platform.test.annotations.DisabledOnRavenwood;
37 import android.platform.test.ravenwood.RavenwoodRule;
38 import android.util.Printer;
39 import android.util.StringBuilderPrinter;
40 
41 import androidx.test.platform.app.InstrumentationRegistry;
42 import androidx.test.runner.AndroidJUnit4;
43 
44 import com.android.compatibility.common.util.WidgetTestUtils;
45 
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 
50 /**
51  * Test {@link ComponentInfo}.
52  */
53 @RunWith(AndroidJUnit4.class)
54 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).")
55 public class ComponentInfoTest {
56     @Rule
57     public final RavenwoodRule mRavenwood = new RavenwoodRule();
58 
59     private final String PACKAGE_NAME = "android.content.cts";
60     private ComponentInfo mComponentInfo;
61 
getContext()62     private Context getContext() {
63         return InstrumentationRegistry.getInstrumentation().getTargetContext();
64     }
65 
66     @Test
testSimple()67     public void testSimple() {
68         ComponentInfo info = new ComponentInfo();
69         new ComponentInfo(info);
70         assertNotNull(info.toString());
71     }
72 
73     @Test
74     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testConstructor()75     public void testConstructor() {
76         Parcel p = Parcel.obtain();
77         ComponentInfo componentInfo = new ComponentInfo();
78         componentInfo.applicationInfo = new ApplicationInfo();
79         componentInfo.writeToParcel(p, 0);
80         p.setDataPosition(0);
81 
82         new MyComponentInfo(p);
83 
84         new ComponentInfo();
85 
86         new ComponentInfo(componentInfo);
87 
88         try {
89             new ComponentInfo((ComponentInfo) null);
90             fail("should throw NullPointerException.");
91         } catch (NullPointerException e) {
92             // expected
93         }
94 
95         try {
96             new MyComponentInfo((Parcel) null);
97             fail("should throw NullPointerException.");
98         } catch (NullPointerException e) {
99             // expected
100         }
101     }
102 
createIconBitmap(Drawable d)103     private Bitmap createIconBitmap(Drawable d) {
104         int size = Math.round(100 * getContext().getResources().getDisplayMetrics().density) + 100;
105         Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
106         d.setBounds(0, 0, size, size);
107         d.draw(new Canvas(bitmap));
108         return bitmap;
109     }
110 
111     @Test
112     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testLoadIcon()113     public void testLoadIcon() {
114         mComponentInfo = new ComponentInfo();
115         mComponentInfo.applicationInfo = new ApplicationInfo();
116 
117         PackageManager pm = getContext().getPackageManager();
118         assertNotNull(pm);
119 
120         Drawable defaultIcon = pm.getDefaultActivityIcon();
121         Drawable d = null;
122         Drawable d2 = null;
123         d = mComponentInfo.loadIcon(pm);
124         assertNotNull(d);
125         assertNotSame(d, defaultIcon);
126         WidgetTestUtils.assertEquals(createIconBitmap(d), createIconBitmap(defaultIcon));
127 
128         d2 = mComponentInfo.loadIcon(pm);
129         assertNotNull(d2);
130         assertNotSame(d, d2);
131         WidgetTestUtils.assertEquals(createIconBitmap(d), createIconBitmap(d2));
132 
133         try {
134             mComponentInfo.loadIcon(null);
135             fail("ComponentInfo#loadIcon() throw NullPointerException");
136         } catch (NullPointerException e) {
137             // expected
138         }
139     }
140 
141     @Test
testDumpBack()142     public void testDumpBack() {
143         MyComponentInfo ci = new MyComponentInfo();
144 
145         StringBuilder sb = new StringBuilder();
146         assertEquals(0, sb.length());
147         StringBuilderPrinter p = new StringBuilderPrinter(sb);
148         String prefix = "";
149         ci.dumpBack(p, prefix);
150 
151         assertNotNull(sb.toString());
152         assertTrue(sb.length() > 0);
153 
154         ci.applicationInfo = new ApplicationInfo();
155         sb = new StringBuilder();
156         assertEquals(0, sb.length());
157         p = new StringBuilderPrinter(sb);
158 
159         ci.dumpBack(p, prefix);
160         assertNotNull(sb.toString());
161         assertTrue(sb.length() > 0);
162 
163         try {
164             ci.dumpBack(null, null);
165             fail("ComponentInfo#dumpBack() throw NullPointerException here.");
166         } catch (NullPointerException e) {
167             // expected
168         }
169     }
170 
171     @Test
172     @DisabledOnRavenwood(blockedBy = android.content.res.Resources.class)
testGetIconResource()173     public void testGetIconResource() {
174         mComponentInfo = new ComponentInfo();
175         mComponentInfo.applicationInfo = new ApplicationInfo();
176         assertEquals(0, mComponentInfo.getIconResource());
177 
178         final int resRed = getContext().getResources()
179                 .getIdentifier("red", "drawable", getContext().getPackageName());
180         mComponentInfo.icon = resRed;
181         assertEquals(mComponentInfo.icon, mComponentInfo.getIconResource());
182 
183         mComponentInfo.icon = 0;
184         assertEquals(mComponentInfo.applicationInfo.icon, mComponentInfo.getIconResource());
185     }
186 
187     @Test
testIsEnabled()188     public void testIsEnabled() {
189         mComponentInfo = new ComponentInfo();
190         mComponentInfo.applicationInfo = new ApplicationInfo();
191         assertTrue(mComponentInfo.isEnabled());
192 
193         mComponentInfo.enabled = false;
194         assertFalse(mComponentInfo.isEnabled());
195 
196         mComponentInfo.enabled = true;
197         mComponentInfo.applicationInfo.enabled = false;
198         assertFalse(mComponentInfo.isEnabled());
199     }
200 
201     @Test
testDumpFront()202     public void testDumpFront() {
203         MyComponentInfo ci = new MyComponentInfo();
204 
205         StringBuilder sb = new StringBuilder();
206         assertEquals(0, sb.length());
207         StringBuilderPrinter p = new StringBuilderPrinter(sb);
208 
209         String prefix = "";
210         ci.dumpFront(p, prefix);
211         assertNotNull(sb.toString());
212         assertTrue(sb.length() > 0);
213 
214         ci.applicationInfo = new ApplicationInfo();
215 
216         sb = new StringBuilder();
217         p = new StringBuilderPrinter(sb);
218         assertEquals(0, sb.length());
219 
220         ci.dumpFront(p, prefix);
221         assertNotNull(sb.toString());
222         assertTrue(sb.length() > 0);
223 
224         try {
225             ci.dumpFront(null, null);
226             fail("ComponentInfo#dumpFront() throw NullPointerException here.");
227         } catch (NullPointerException e) {
228             // expected
229         }
230     }
231 
232     @Test
233     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testLoadLabel()234     public void testLoadLabel() throws NameNotFoundException {
235         mComponentInfo = new ComponentInfo();
236         mComponentInfo.applicationInfo = new ApplicationInfo();
237 
238         final PackageManager pm = getContext().getPackageManager();
239 
240         assertNotNull(mComponentInfo);
241         mComponentInfo.packageName = PACKAGE_NAME;
242         mComponentInfo.nonLocalizedLabel = "nonLocalizedLabel";
243         assertEquals("nonLocalizedLabel", mComponentInfo.loadLabel(pm));
244 
245         mComponentInfo.nonLocalizedLabel = null;
246         mComponentInfo.labelRes = 0;
247         mComponentInfo.name = "name";
248         assertEquals("name", mComponentInfo.loadLabel(pm));
249 
250         mComponentInfo.applicationInfo =
251                 getContext().getPackageManager().getApplicationInfo(PACKAGE_NAME,
252                         PackageManager.ApplicationInfoFlags.of(0));
253 
254         mComponentInfo.nonLocalizedLabel = null;
255         final int resHelloAndroid = getContext().getResources()
256                 .getIdentifier("hello_android", "string", PACKAGE_NAME);
257         mComponentInfo.labelRes = resHelloAndroid;
258         assertEquals(getContext().getString(mComponentInfo.labelRes), mComponentInfo.loadLabel(pm));
259 
260         try {
261             mComponentInfo.loadLabel(null);
262             fail("ComponentInfo#loadLabel throw NullPointerException");
263         } catch (NullPointerException e) {
264             // expected
265         }
266     }
267 
268     @Test
269     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testWriteToParcel()270     public void testWriteToParcel() {
271         Parcel p = Parcel.obtain();
272         mComponentInfo = new ComponentInfo();
273         mComponentInfo.applicationInfo = new ApplicationInfo();
274         mComponentInfo.writeToParcel(p, 0);
275         p.setDataPosition(0);
276 
277         MyComponentInfo ci = new MyComponentInfo(p);
278         assertEquals(mComponentInfo.processName, ci.processName);
279         assertEquals(mComponentInfo.enabled, ci.enabled);
280         assertEquals(mComponentInfo.exported, ci.exported);
281 
282         StringBuilder sb1 = new StringBuilder();
283         StringBuilderPrinter p1 = new StringBuilderPrinter(sb1);
284         StringBuilder sb2 = new StringBuilder();
285         StringBuilderPrinter p2 = new StringBuilderPrinter(sb2);
286         mComponentInfo.applicationInfo.dump(p1, "");
287         ci.applicationInfo.dump(p2, "");
288         assertEquals(sb1.toString(), sb2.toString());
289 
290         try {
291             mComponentInfo.writeToParcel(null, 0);
292             fail("ComponentInfo#writeToParcel() throw NullPointerException");
293         } catch (NullPointerException e) {
294             // expected
295         }
296     }
297 
298     private static class MyComponentInfo extends ComponentInfo {
MyComponentInfo()299         public MyComponentInfo() {
300             super();
301         }
302 
MyComponentInfo(ComponentInfo orig)303         public MyComponentInfo(ComponentInfo orig) {
304             super(orig);
305         }
MyComponentInfo(Parcel source)306         public MyComponentInfo(Parcel source) {
307             super(source);
308         }
309 
dumpBack(Printer pw, String prefix)310         public void dumpBack(Printer pw, String prefix) {
311             super.dumpBack(pw, prefix);
312         }
313 
dumpFront(Printer pw, String prefix)314         public void dumpFront(Printer pw, String prefix) {
315             super.dumpFront(pw, prefix);
316         }
317     }
318 }
319