1 /* 2 * Copyright (C) 2006 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.app.activity; 18 19 import android.content.ComponentName; 20 import android.content.pm.ActivityInfo; 21 import android.content.pm.PackageItemInfo; 22 import android.content.pm.PackageManager; 23 import android.content.pm.PermissionInfo; 24 import android.content.pm.ProviderInfo; 25 import android.content.pm.ServiceInfo; 26 import android.content.res.TypedArray; 27 import android.content.res.XmlResourceParser; 28 import android.os.Bundle; 29 import android.test.AndroidTestCase; 30 31 import androidx.test.filters.SmallTest; 32 33 import com.android.frameworks.coretests.R; 34 35 import org.xmlpull.v1.XmlPullParser; 36 import org.xmlpull.v1.XmlPullParserException; 37 38 import java.io.IOException; 39 40 /** 41 * Tests for meta-data associated with application components. 42 */ 43 public class MetaDataTest extends AndroidTestCase { 44 checkMetaData(ComponentName cn, PackageItemInfo ci)45 private void checkMetaData(ComponentName cn, PackageItemInfo ci) 46 throws IOException, XmlPullParserException { 47 assertNotNull("Unable to find component " + cn, ci); 48 49 Bundle md = ci.metaData; 50 assertNotNull("No meta data found", md); 51 52 assertEquals("foo", md.getString("com.android.frameworks.coretests.string")); 53 assertTrue(md.getBoolean("com.android.frameworks.coretests.boolean")); 54 assertEquals(100, md.getInt("com.android.frameworks.coretests.integer")); 55 assertEquals(0xff000000, md.getInt("com.android.frameworks.coretests.color")); 56 57 assertEquals((double) 1001, 58 Math.floor(md.getFloat("com.android.frameworks.coretests.float") * 10 + .5)); 59 60 assertEquals(R.xml.metadata, md.getInt("com.android.frameworks.coretests.reference")); 61 62 XmlResourceParser xml = ci.loadXmlMetaData(mContext.getPackageManager(), 63 "com.android.frameworks.coretests.reference"); 64 assertNotNull(xml); 65 66 int type; 67 while ((type = xml.next()) != XmlPullParser.START_TAG 68 && type != XmlPullParser.END_DOCUMENT) { 69 } 70 assertEquals(XmlPullParser.START_TAG, type); 71 assertEquals("thedata", xml.getName()); 72 73 // method 1: direct access 74 final String rawAttr = xml.getAttributeValue(null, "rawText"); 75 assertEquals("some raw text", rawAttr); 76 77 // method 2: direct access of typed value 78 final int rawColorIntAttr = xml.getAttributeIntValue(null, "rawColor", 0); 79 assertEquals(0xffffff00, rawColorIntAttr); 80 final String rawColorStrAttr = xml.getAttributeValue(null, "rawColor"); 81 assertEquals("#ffffff00", rawColorStrAttr); 82 83 // method 2: direct access of resource attribute 84 final String nameSpace = "http://schemas.android.com/apk/res/android"; 85 final int colorIntAttr = xml.getAttributeIntValue(nameSpace, "color", 0); 86 assertEquals(0xffff0000, colorIntAttr); 87 final String colorStrAttr = xml.getAttributeValue(nameSpace, "color"); 88 assertEquals("#ffff0000", colorStrAttr); 89 90 // method 3: styled access (borrowing an attr from view system here) 91 TypedArray a = mContext.obtainStyledAttributes(xml, 92 android.R.styleable.TextView); 93 String styledAttr = a.getString(android.R.styleable.TextView_text); 94 assertEquals("text", styledAttr); 95 a.recycle(); 96 97 xml.close(); 98 } 99 100 @SmallTest testActivityWithData()101 public void testActivityWithData() throws Exception { 102 ComponentName cn = new ComponentName(mContext, LocalActivity.class); 103 ActivityInfo ai = mContext.getPackageManager().getActivityInfo( 104 cn, PackageManager.GET_META_DATA); 105 106 checkMetaData(cn, ai); 107 108 ai = mContext.getPackageManager().getActivityInfo(cn, 0); 109 110 assertNull("Meta data returned when not requested", ai.metaData); 111 } 112 113 @SmallTest testReceiverWithData()114 public void testReceiverWithData() throws Exception { 115 ComponentName cn = new ComponentName(mContext, LocalReceiver.class); 116 ActivityInfo ai = mContext.getPackageManager().getReceiverInfo( 117 cn, PackageManager.GET_META_DATA); 118 119 checkMetaData(cn, ai); 120 121 ai = mContext.getPackageManager().getReceiverInfo(cn, 0); 122 123 assertNull("Meta data returned when not requested", ai.metaData); 124 } 125 126 @SmallTest testServiceWithData()127 public void testServiceWithData() throws Exception { 128 ComponentName cn = new ComponentName(mContext, LocalService.class); 129 ServiceInfo si = mContext.getPackageManager().getServiceInfo( 130 cn, PackageManager.GET_META_DATA); 131 132 checkMetaData(cn, si); 133 134 si = mContext.getPackageManager().getServiceInfo(cn, 0); 135 136 assertNull("Meta data returned when not requested", si.metaData); 137 } 138 139 @SmallTest testProviderWithData()140 public void testProviderWithData() throws Exception { 141 ComponentName cn = new ComponentName(mContext, LocalProvider.class); 142 ProviderInfo pi = mContext.getPackageManager().resolveContentProvider( 143 "com.android.frameworks.coretests.LocalProvider", 144 PackageManager.GET_META_DATA); 145 checkMetaData(cn, pi); 146 147 pi = mContext.getPackageManager().resolveContentProvider( 148 "com.android.frameworks.coretests.LocalProvider", 0); 149 150 assertNull("Meta data returned when not requested", pi.metaData); 151 } 152 153 @SmallTest testPermissionWithData()154 public void testPermissionWithData() throws Exception { 155 ComponentName cn = new ComponentName("foo", 156 "com.android.frameworks.coretests.permission.TEST_GRANTED"); 157 PermissionInfo pi = mContext.getPackageManager().getPermissionInfo( 158 cn.getClassName(), PackageManager.GET_META_DATA); 159 checkMetaData(cn, pi); 160 161 pi = mContext.getPackageManager().getPermissionInfo( 162 cn.getClassName(), 0); 163 164 assertNull("Meta data returned when not requested", pi.metaData); 165 } 166 } 167 168 169