FileDocCategorySizeDatePackage
MetaDataTest.javaAPI DocAndroid 5.1 API6413Thu Mar 12 22:22:12 GMT 2015android.app.activity

MetaDataTest

public class MetaDataTest extends android.test.AndroidTestCase
Tests for meta-data associated with application components.

Fields Summary
Constructors Summary
Methods Summary
private voidcheckMetaData(android.content.ComponentName cn, android.content.pm.PackageItemInfo ci)

        assertNotNull("Unable to find component " + cn, ci);

        Bundle md = ci.metaData;
        assertNotNull("No meta data found", md);

        assertEquals("foo", md.getString("com.android.frameworks.coretests.string"));
        assertTrue(md.getBoolean("com.android.frameworks.coretests.boolean"));
        assertEquals(100, md.getInt("com.android.frameworks.coretests.integer"));
        assertEquals(0xff000000, md.getInt("com.android.frameworks.coretests.color"));

        assertEquals((double) 1001,
                Math.floor(md.getFloat("com.android.frameworks.coretests.float") * 10 + .5));

        assertEquals(R.xml.metadata, md.getInt("com.android.frameworks.coretests.reference"));

        XmlResourceParser xml = ci.loadXmlMetaData(mContext.getPackageManager(),
                "com.android.frameworks.coretests.reference");
        assertNotNull(xml);

        int type;
        while ((type = xml.next()) != XmlPullParser.START_TAG
                && type != XmlPullParser.END_DOCUMENT) {
        }
        assertEquals(XmlPullParser.START_TAG, type);
        assertEquals("thedata", xml.getName());

        // method 1: direct access
        final String rawAttr = xml.getAttributeValue(null, "rawText");
        assertEquals("some raw text", rawAttr);

        // method 2: direct access of typed value
        final int rawColorIntAttr = xml.getAttributeIntValue(null, "rawColor", 0);
        assertEquals(0xffffff00, rawColorIntAttr);
        final String rawColorStrAttr = xml.getAttributeValue(null, "rawColor");
        assertEquals("#ffffff00", rawColorStrAttr);

        // method 2: direct access of resource attribute
        final String nameSpace = "http://schemas.android.com/apk/res/android";
        final int colorIntAttr = xml.getAttributeIntValue(nameSpace, "color", 0);
        assertEquals(0xffff0000, colorIntAttr);
        final String colorStrAttr = xml.getAttributeValue(nameSpace, "color");
        assertEquals("#ffff0000", colorStrAttr);

        // method 3: styled access (borrowing an attr from view system here)
        TypedArray a = mContext.obtainStyledAttributes(xml,
                android.R.styleable.TextView);
        String styledAttr = a.getString(android.R.styleable.TextView_text);
        assertEquals("text", styledAttr);
        a.recycle();
        
        xml.close();
    
public voidtestActivityWithData()

        ComponentName cn = new ComponentName(mContext, LocalActivity.class);
        ActivityInfo ai = mContext.getPackageManager().getActivityInfo(
                cn, PackageManager.GET_META_DATA);

        checkMetaData(cn, ai);

        ai = mContext.getPackageManager().getActivityInfo(cn, 0);

        assertNull("Meta data returned when not requested", ai.metaData);
    
public voidtestPermissionWithData()

        ComponentName cn = new ComponentName("foo",
                "com.android.frameworks.coretests.permission.TEST_GRANTED");
        PermissionInfo pi = mContext.getPackageManager().getPermissionInfo(
                cn.getClassName(), PackageManager.GET_META_DATA);
        checkMetaData(cn, pi);

        pi = mContext.getPackageManager().getPermissionInfo(
                cn.getClassName(), 0);

        assertNull("Meta data returned when not requested", pi.metaData);
    
public voidtestProviderWithData()

        ComponentName cn = new ComponentName(mContext, LocalProvider.class);
        ProviderInfo pi = mContext.getPackageManager().resolveContentProvider(
                "com.android.frameworks.coretests.LocalProvider",
                PackageManager.GET_META_DATA);
        checkMetaData(cn, pi);

        pi = mContext.getPackageManager().resolveContentProvider(
                "com.android.frameworks.coretests.LocalProvider", 0);

        assertNull("Meta data returned when not requested", pi.metaData);
    
public voidtestReceiverWithData()

        ComponentName cn = new ComponentName(mContext, LocalReceiver.class);
        ActivityInfo ai = mContext.getPackageManager().getReceiverInfo(
                cn, PackageManager.GET_META_DATA);

        checkMetaData(cn, ai);

        ai = mContext.getPackageManager().getReceiverInfo(cn, 0);

        assertNull("Meta data returned when not requested", ai.metaData);
    
public voidtestServiceWithData()

        ComponentName cn = new ComponentName(mContext, LocalService.class);
        ServiceInfo si = mContext.getPackageManager().getServiceInfo(
                cn, PackageManager.GET_META_DATA);

        checkMetaData(cn, si);

        si = mContext.getPackageManager().getServiceInfo(cn, 0);

        assertNull("Meta data returned when not requested", si.metaData);