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 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.pm.ActivityInfo; 25 import android.content.pm.PackageItemInfo; 26 import android.content.pm.PackageManager; 27 import android.content.pm.PackageManager.NameNotFoundException; 28 import android.content.res.XmlResourceParser; 29 import android.os.Parcel; 30 import android.platform.test.annotations.AppModeFull; 31 import android.platform.test.annotations.DisabledOnRavenwood; 32 import android.platform.test.ravenwood.RavenwoodRule; 33 import android.util.Printer; 34 35 import androidx.test.platform.app.InstrumentationRegistry; 36 import androidx.test.runner.AndroidJUnit4; 37 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 @RunWith(AndroidJUnit4.class) 43 @AppModeFull // TODO(Instant) Figure out which APIs should work. 44 public class PackageItemInfoTest { 45 @Rule 46 public final RavenwoodRule mRavenwood = new RavenwoodRule(); 47 48 private static final String PACKAGE_NAME = "android.content.cts"; 49 private static final String ACTIVITY_NAME = "android.content.pm.cts.TestPmActivity"; 50 private static final String METADATA_NAME = "android.content.pm.cts.xmltest"; 51 getContext()52 private Context getContext() { 53 return InstrumentationRegistry.getInstrumentation().getTargetContext(); 54 } 55 getPackageManager()56 private PackageManager getPackageManager() { 57 return getContext().getPackageManager(); 58 } 59 60 @Test testSimple()61 public void testSimple() { 62 PackageItemInfo info = new PackageItemInfo(); 63 new PackageItemInfo(info); 64 assertNotNull(info.toString()); 65 } 66 67 @Test 68 @DisabledOnRavenwood(blockedBy = PackageManager.class) testLoadMethods()69 public void testLoadMethods() throws NameNotFoundException { 70 // Test constructors 71 ActivityInfo activityInfo = (ActivityInfo) getTestItemInfo(); 72 new PackageItemInfo(); 73 PackageItemInfo pkgItemInfo = new PackageItemInfo(activityInfo); 74 checkInfoSame(activityInfo, pkgItemInfo); 75 // Test loadLabel 76 assertEquals(ACTIVITY_NAME, pkgItemInfo.loadLabel(getPackageManager())); 77 // Test loadIcon 78 assertNotNull(pkgItemInfo.loadIcon(getPackageManager())); 79 80 // Test loadXmlMetaData 81 XmlResourceParser parser = pkgItemInfo.loadXmlMetaData(getPackageManager(), METADATA_NAME); 82 assertNotNull(parser); 83 } 84 85 @Test testDump()86 public void testDump() { 87 MockPackageItemInfo pkgItemInfo = new MockPackageItemInfo(); 88 MockPrinter printer = new MockPrinter(); 89 // dumpBack is empty method 90 pkgItemInfo.dumpBack(printer, ""); 91 92 // Test dumpFront 93 String prefix = "PackageItemInfoTest"; 94 pkgItemInfo.dumpFront(printer, prefix); 95 } 96 97 @Test 98 @DisabledOnRavenwood(blockedBy = PackageManager.class) testWriteToParcel()99 public void testWriteToParcel() throws NameNotFoundException { 100 ActivityInfo activityInfo = (ActivityInfo) getTestItemInfo(); 101 PackageItemInfo expectedInfo = new PackageItemInfo(activityInfo); 102 103 Parcel p = Parcel.obtain(); 104 expectedInfo.writeToParcel(p, 0); 105 p.setDataPosition(0); 106 // PackageItemInfo(Parcel p) is protected 107 MockPackageItemInfo infoFromParcel = new MockPackageItemInfo(p); 108 checkInfoSame(expectedInfo, infoFromParcel); 109 p.recycle(); 110 } 111 checkInfoSame(PackageItemInfo expected, PackageItemInfo actual)112 private void checkInfoSame(PackageItemInfo expected, PackageItemInfo actual) { 113 assertEquals(expected.name, actual.name); 114 assertEquals(expected.packageName, actual.packageName); 115 assertEquals(expected.labelRes, actual.labelRes); 116 assertEquals(expected.nonLocalizedLabel, actual.nonLocalizedLabel); 117 assertEquals(expected.icon, actual.icon); 118 assertEquals(expected.metaData.size(), actual.metaData.size()); 119 final int resPmTest = getContext().getResources() 120 .getIdentifier("pm_test", "xml", getContext().getPackageName()); 121 assertEquals(resPmTest, actual.metaData.getInt(METADATA_NAME)); 122 } 123 getTestItemInfo()124 private PackageItemInfo getTestItemInfo() throws NameNotFoundException { 125 ComponentName componentName = new ComponentName(PACKAGE_NAME, ACTIVITY_NAME); 126 ActivityInfo activityInfo = 127 getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA); 128 return activityInfo; 129 } 130 131 private class MockPackageItemInfo extends PackageItemInfo { 132 MockPackageItemInfo()133 public MockPackageItemInfo() { 134 super(); 135 } 136 MockPackageItemInfo(PackageItemInfo orig)137 public MockPackageItemInfo(PackageItemInfo orig) { 138 super(orig); 139 } 140 MockPackageItemInfo(Parcel source)141 public MockPackageItemInfo(Parcel source) { 142 super(source); 143 } 144 dumpFront(Printer pw, String prefix)145 public void dumpFront(Printer pw, String prefix) { 146 super.dumpFront(pw, prefix); 147 } 148 dumpBack(Printer pw, String prefix)149 public void dumpBack(Printer pw, String prefix) { 150 super.dumpBack(pw, prefix); 151 } 152 } 153 154 private class MockPrinter implements Printer { println(String x)155 public void println(String x) { 156 } 157 } 158 } 159