diff options
| -rw-r--r-- | core/java/android/content/pm/RegisteredServicesCache.java | 33 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/content/pm/RegisteredServicesCacheTest.java | 6 |
2 files changed, 37 insertions, 2 deletions
diff --git a/core/java/android/content/pm/RegisteredServicesCache.java b/core/java/android/content/pm/RegisteredServicesCache.java index 74da62c85ed2..10d3051cff6f 100644 --- a/core/java/android/content/pm/RegisteredServicesCache.java +++ b/core/java/android/content/pm/RegisteredServicesCache.java @@ -166,7 +166,15 @@ public abstract class RegisteredServicesCache<V> { @UnsupportedAppUsage public RegisteredServicesCache(Context context, String interfaceName, String metaDataName, String attributeName, XmlSerializerAndParser<V> serializerAndParser) { - mContext = context; + this(new Injector<V>(context), interfaceName, metaDataName, attributeName, + serializerAndParser); + } + + /** Provides the basic functionality for unit tests. */ + @VisibleForTesting + public RegisteredServicesCache(Injector<V> injector, String interfaceName, String metaDataName, + String attributeName, XmlSerializerAndParser<V> serializerAndParser) { + mContext = injector.getContext(); mInterfaceName = interfaceName; mMetaDataName = metaDataName; mAttributesName = attributeName; @@ -184,7 +192,7 @@ public abstract class RegisteredServicesCache<V> { if (isCore) { intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); } - mBackgroundHandler = BackgroundThread.getHandler(); + mBackgroundHandler = injector.getBackgroundHandler(); mContext.registerReceiverAsUser( mPackageReceiver, UserHandle.ALL, intentFilter, null, mBackgroundHandler); @@ -918,4 +926,25 @@ public abstract class RegisteredServicesCache<V> { return null; } } + + /** + * Point of injection for test dependencies. + * @param <V> The type of the value. + */ + @VisibleForTesting + public static class Injector<V> { + private final Context mContext; + + public Injector(Context context) { + mContext = context; + } + + public Context getContext() { + return mContext; + } + + public Handler getBackgroundHandler() { + return BackgroundThread.getHandler(); + } + } } diff --git a/core/tests/coretests/src/android/content/pm/RegisteredServicesCacheTest.java b/core/tests/coretests/src/android/content/pm/RegisteredServicesCacheTest.java index ee4761b9d024..dccbf4036b3e 100644 --- a/core/tests/coretests/src/android/content/pm/RegisteredServicesCacheTest.java +++ b/core/tests/coretests/src/android/content/pm/RegisteredServicesCacheTest.java @@ -245,6 +245,12 @@ public class RegisteredServicesCacheTest extends AndroidTestCase { SERVICE_INTERFACE, SERVICE_META_DATA, ATTRIBUTES_NAME, new TestSerializer()); } + TestServicesCache(Injector<TestServiceType> injector, + XmlSerializerAndParser<TestServiceType> serializerAndParser) { + super(injector, SERVICE_INTERFACE, SERVICE_META_DATA, ATTRIBUTES_NAME, + serializerAndParser); + } + @Override public TestServiceType parseServiceAttributes(Resources res, String packageName, AttributeSet attrs) { |