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