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