• 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.assertNotNull;
21 import static org.junit.Assert.assertThrows;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.ActivityInfo;
28 import android.content.pm.PackageManager;
29 import android.content.pm.ResolveInfo;
30 import android.os.Parcel;
31 import android.platform.test.annotations.AppModeFull;
32 import android.platform.test.annotations.DisabledOnRavenwood;
33 import android.platform.test.ravenwood.RavenwoodRule;
34 import android.util.Printer;
35 import android.util.StringBuilderPrinter;
36 
37 import androidx.test.platform.app.InstrumentationRegistry;
38 import androidx.test.runner.AndroidJUnit4;
39 
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 
44 @RunWith(AndroidJUnit4.class)
45 @AppModeFull // TODO(Instant) Figure out which APIs should work.
46 public class ResolveInfoTest {
47     @Rule
48     public final RavenwoodRule mRavenwood = new RavenwoodRule();
49 
50     private static final String PACKAGE_NAME = "android.content.cts";
51     private static final String MAIN_ACTION_NAME = "android.intent.action.MAIN";
52     private static final String ACTIVITY_NAME = "android.content.pm.cts.TestPmActivity";
53     private static final String SERVICE_NAME = "android.content.pm.cts.activity.PMTEST_SERVICE";
54 
getContext()55     private Context getContext() {
56         return InstrumentationRegistry.getInstrumentation().getTargetContext();
57     }
58 
59     @Test
testSimple()60     public void testSimple() {
61         ResolveInfo info = new ResolveInfo();
62         info.activityInfo = new ActivityInfo();
63         info.activityInfo.packageName = "com.example";
64         info.activityInfo.name = "com.example.Example";
65         new ResolveInfo(info);
66         assertNotNull(info.toString());
67         info.dump(new StringBuilderPrinter(new StringBuilder()), "");
68     }
69 
70     @Test
71     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testResolveInfo()72     public final void testResolveInfo() {
73         // Test constructor
74         new ResolveInfo();
75 
76         PackageManager pm = getContext().getPackageManager();
77         Intent intent = new Intent(MAIN_ACTION_NAME);
78         intent.setComponent(new ComponentName(PACKAGE_NAME, ACTIVITY_NAME));
79         ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
80         // Test loadLabel, loadIcon, getIconResource, toString, describeContents
81         String expectedLabel = "Android TestCase";
82         assertEquals(expectedLabel, resolveInfo.loadLabel(pm).toString());
83         assertNotNull(resolveInfo.loadIcon(pm));
84         assertTrue(resolveInfo.getIconResource() != 0);
85         assertNotNull(resolveInfo.toString());
86         assertEquals(0, resolveInfo.describeContents());
87     }
88 
89     @Test
90     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testLoadLabel_noNonLocalizedLabelAndNullPM_throwsNPE()91     public final void testLoadLabel_noNonLocalizedLabelAndNullPM_throwsNPE() {
92         final ResolveInfo resolveInfo = new ResolveInfo();
93         resolveInfo.activityInfo = new ActivityInfo();
94 
95         assertThrows("Should throw NullPointerException", NullPointerException.class,
96                 () -> resolveInfo.loadLabel(null));
97     }
98 
99     @Test
100     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testLoadLabel_hasNonLocalizedLabelAndNullPM_correctResult()101     public final void testLoadLabel_hasNonLocalizedLabelAndNullPM_correctResult() {
102         final ResolveInfo resolveInfo = new ResolveInfo();
103         final String expectedResult = "none";
104         resolveInfo.nonLocalizedLabel = expectedResult;
105 
106         assertEquals(expectedResult, resolveInfo.loadLabel(null));
107     }
108 
109     @Test
110     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testDump()111     public final void testDump() {
112         PackageManager pm = getContext().getPackageManager();
113         Intent intent = new Intent(SERVICE_NAME);
114         ResolveInfo resolveInfo = pm.resolveService(intent, PackageManager.GET_RESOLVED_FILTER);
115 
116         Parcel p = Parcel.obtain();
117         resolveInfo.writeToParcel(p, 0);
118         p.setDataPosition(0);
119         ResolveInfo infoFromParcel = ResolveInfo.CREATOR.createFromParcel(p);
120         // Test writeToParcel
121         assertEquals(resolveInfo.getIconResource(), infoFromParcel.getIconResource());
122         assertEquals(resolveInfo.priority, infoFromParcel.priority);
123         assertEquals(resolveInfo.preferredOrder, infoFromParcel.preferredOrder);
124         assertEquals(resolveInfo.match, infoFromParcel.match);
125         assertEquals(resolveInfo.specificIndex, infoFromParcel.specificIndex);
126         assertEquals(resolveInfo.labelRes, infoFromParcel.labelRes);
127         assertEquals(resolveInfo.nonLocalizedLabel, infoFromParcel.nonLocalizedLabel);
128         assertEquals(resolveInfo.icon, infoFromParcel.icon);
129 
130         // Test dump
131         TestPrinter printer = new TestPrinter();
132         String prefix = "TestResolveInfo";
133         resolveInfo.dump(printer, prefix);
134         assertTrue(printer.isPrintlnCalled);
135     }
136 
137     private class TestPrinter implements Printer {
138         public boolean isPrintlnCalled;
println(String x)139         public void println(String x) {
140             isPrintlnCalled = true;
141         }
142     }
143 }
144