Merge "DO NOT MERGE: Extract MockVendorPolicy, add standard mechanism to inject it." into gingerbread

This commit is contained in:
Makoto Onuki 2010-09-21 10:24:36 -07:00 committed by Android (Google) Code Review
commit 7fbdc44c58
3 changed files with 86 additions and 24 deletions

View File

@ -71,6 +71,28 @@ public class VendorPolicyLoader {
return sInstance;
}
/**
* For testing only.
*
* Replaces the instance with a new instance that loads a specified class.
*/
public static void injectPolicyForTest(Context context, String apkPackageName, Class<?> clazz) {
String name = clazz.getName();
Log.d(Email.LOG_TAG, String.format("Using policy: package=%s name=%s",
apkPackageName, name));
sInstance = new VendorPolicyLoader(context, apkPackageName, name, true);
}
/**
* For testing only.
*
* Clear the instance so that the next {@link #getInstance} call will return a regular,
* non-injected instance.
*/
public static void clearInstanceForTest() {
sInstance = null;
}
private VendorPolicyLoader(Context context) {
this(context, POLICY_PACKAGE, POLICY_CLASS, false);
}
@ -79,9 +101,9 @@ public class VendorPolicyLoader {
* Constructor for testing, where we need to use an alternate package/class name, and skip
* the system apk check.
*/
/* package */ VendorPolicyLoader(Context context, String packageName, String className,
/* package */ VendorPolicyLoader(Context context, String apkPackageName, String className,
boolean allowNonSystemApk) {
if (!allowNonSystemApk && !isSystemPackage(context, packageName)) {
if (!allowNonSystemApk && !isSystemPackage(context, apkPackageName)) {
mPolicyMethod = null;
return;
}
@ -89,7 +111,7 @@ public class VendorPolicyLoader {
Class<?> clazz = null;
Method method = null;
try {
final Context policyContext = context.createPackageContext(packageName,
final Context policyContext = context.createPackageContext(apkPackageName,
Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
final ClassLoader classLoader = policyContext.getClassLoader();
clazz = classLoader.loadClass(className);

View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.email;
import android.content.Context;
import android.os.Bundle;
public class MockVendorPolicy {
public static String passedPolicy;
public static Bundle passedBundle;
public static Bundle mockResult;
public static Bundle getPolicy(String policy, Bundle args) {
passedPolicy = policy;
passedBundle = args;
return mockResult;
}
/**
* Call it to enable {@link MockVendorPolicy}.
*/
public static void inject(Context context) {
VendorPolicyLoader.injectPolicyForTest(context, context.getPackageName(),
MockVendorPolicy.class);
}
}

View File

@ -23,6 +23,20 @@ import android.os.Bundle;
import android.test.AndroidTestCase;
public class VendorPolicyLoaderTest extends AndroidTestCase {
private String mTestApkPackageName;
@Override
protected void setUp() throws Exception {
super.setUp();
mTestApkPackageName = getContext().getPackageName() + ".tests";
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
VendorPolicyLoader.clearInstanceForTest();
}
/**
* Test for the case where the helper package doesn't exist.
*/
@ -37,7 +51,7 @@ public class VendorPolicyLoaderTest extends AndroidTestCase {
public void testIsSystemPackage() {
final Context c = getContext();
assertEquals(false, VendorPolicyLoader.isSystemPackage(c, "no.such.package"));
assertEquals(false, VendorPolicyLoader.isSystemPackage(c, "com.android.email.tests"));
assertEquals(false, VendorPolicyLoader.isSystemPackage(c, mTestApkPackageName));
assertEquals(true, VendorPolicyLoader.isSystemPackage(c, "com.android.settings"));
}
@ -46,9 +60,8 @@ public class VendorPolicyLoaderTest extends AndroidTestCase {
* policy.
*/
public void testGetPolicy() {
// Because MockVendorPolicy lives in a non-system apk, we need to skip the system-apk check.
VendorPolicyLoader pl = new VendorPolicyLoader(getContext(), getContext().getPackageName(),
MockVendorPolicy.class.getName(), true);
MockVendorPolicy.inject(getContext());
VendorPolicyLoader pl = VendorPolicyLoader.getInstance(getContext());
// Prepare result
Bundle result = new Bundle();
@ -63,20 +76,19 @@ public class VendorPolicyLoaderTest extends AndroidTestCase {
Bundle actualResult = pl.getPolicy("policy1", args);
// Check passed args
assertEquals("operation", "policy1", MockVendorPolicy.passedPolicy);
assertEquals("policy", "policy1", MockVendorPolicy.passedPolicy);
assertEquals("arg", "a", MockVendorPolicy.passedBundle.getString("arg1"));
// Check return value
assertEquals("result", 1, actualResult.getInt("ret"));
}
/**
* Same as {@link #testGetPolicy}, but with the system-apk check. It's a test for the case
* where we have a non-system vendor policy installed, which shouldn't be used.
*/
public void testGetPolicyNonSystem() {
VendorPolicyLoader pl = new VendorPolicyLoader(getContext(), "com.android.email.tests",
VendorPolicyLoader pl = new VendorPolicyLoader(getContext(), mTestApkPackageName,
MockVendorPolicy.class.getName(), false);
MockVendorPolicy.passedPolicy = null;
@ -88,22 +100,10 @@ public class VendorPolicyLoaderTest extends AndroidTestCase {
assertNull(MockVendorPolicy.passedPolicy);
}
private static class MockVendorPolicy {
public static String passedPolicy;
public static Bundle passedBundle;
public static Bundle mockResult;
public static Bundle getPolicy(String operation, Bundle args) {
passedPolicy = operation;
passedBundle = args;
return mockResult;
}
}
/**
* Test that any vendor policy that happens to be installed returns legal values
* for getImapIdValues() per its API.
*
*
* Note, in most cases very little will happen in this test, because there is
* no vendor policy package. Most of this test exists to test a vendor policy
* package itself, to make sure that its API returns reasonable values.
@ -132,7 +132,7 @@ public class VendorPolicyLoaderTest extends AndroidTestCase {
assertTrue(elements[i+1].charAt(0) != ' ');
assertTrue(elements[i+2].startsWith(" "));
assertTrue(elements[i+3].charAt(0) != ' ');
i += 4;
i += 4;
}
}