MetaDataTestpublic class MetaDataTest extends android.test.AndroidTestCase Tests for meta-data associated with application components. |
Methods Summary |
---|
private void | checkMetaData(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.unit_tests.string"));
assertTrue(md.getBoolean("com.android.unit_tests.boolean"));
assertEquals(100, md.getInt("com.android.unit_tests.integer"));
assertEquals(0xff000000, md.getInt("com.android.unit_tests.color"));
assertEquals((double) 1001,
Math.floor(md.getFloat("com.android.unit_tests.float") * 10 + .5));
assertEquals(R.xml.metadata, md.getInt("com.android.unit_tests.reference"));
XmlResourceParser xml = ci.loadXmlMetaData(mContext.getPackageManager(),
"com.android.unit_tests.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 void | testActivityWithData()
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 void | testPermissionWithData()
ComponentName cn = new ComponentName("foo",
"com.android.unit_tests.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 void | testProviderWithData()
ComponentName cn = new ComponentName(mContext, LocalProvider.class);
ProviderInfo pi = mContext.getPackageManager().resolveContentProvider(
"com.android.unit_tests.LocalProvider",
PackageManager.GET_META_DATA);
checkMetaData(cn, pi);
pi = mContext.getPackageManager().resolveContentProvider(
"com.android.unit_tests.LocalProvider", 0);
assertNull("Meta data returned when not requested", pi.metaData);
| public void | testReceiverWithData()
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 void | testServiceWithData()
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);
|
|