From 618f1d8ac513f9cf3d12fb9d773bc457a8f7259b Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Fri, 11 Jun 2010 10:22:39 -0700 Subject: [PATCH] DO NOT MERGE: Extract MockVendorPolicy, add standard mechanism to inject it. One thing that bothers me regarding the new ImapStore is that there is no tests to verify if the way how getImapId() uses a vendor policy hasn't changed. This part is hard to test with a real vendor policy, and it can easily be overlooked even if it's broken. This CL offers ImapStoreUnitTests a way to test the interaction between getImapId() and a vendor policy. Also fixed a bug in VendorPolicyLoaderTest where it assumed the test apk package name is "com.android.email.tests", but it may actually be "com.google.android.email.tests" now. (Broken since the test makefile used inherit-package.) Backport of I8feb616ea28cb5cae5b4fba57e363771014ac599 Change-Id: I59536bc9a0e5c09c23eab21cdfb2f8283ef01a42 --- src/com/android/email/VendorPolicyLoader.java | 28 +++++++++++-- .../com/android/email/MockVendorPolicy.java | 40 ++++++++++++++++++ .../android/email/VendorPolicyLoaderTest.java | 42 +++++++++---------- 3 files changed, 86 insertions(+), 24 deletions(-) create mode 100644 tests/src/com/android/email/MockVendorPolicy.java diff --git a/src/com/android/email/VendorPolicyLoader.java b/src/com/android/email/VendorPolicyLoader.java index 8a80359f2..a95921f29 100644 --- a/src/com/android/email/VendorPolicyLoader.java +++ b/src/com/android/email/VendorPolicyLoader.java @@ -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); diff --git a/tests/src/com/android/email/MockVendorPolicy.java b/tests/src/com/android/email/MockVendorPolicy.java new file mode 100644 index 000000000..86165c959 --- /dev/null +++ b/tests/src/com/android/email/MockVendorPolicy.java @@ -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); + } +} diff --git a/tests/src/com/android/email/VendorPolicyLoaderTest.java b/tests/src/com/android/email/VendorPolicyLoaderTest.java index 9fcda930b..e08493f5d 100644 --- a/tests/src/com/android/email/VendorPolicyLoaderTest.java +++ b/tests/src/com/android/email/VendorPolicyLoaderTest.java @@ -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; } }