Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ee5c8cba authored by lpeter's avatar lpeter
Browse files

Refactor RegisteredServicesCache to use injection for test dependencies.

Flag: android.content.pm.optimize_parsing_in_registered_services_cache

Bug: 396226221
Test: atest RegisteredServicesCacheTest
Change-Id: I528e5fe549e4c4ff6864a1176ef51e97ac5d0d9b
parent c10218e7
Loading
Loading
Loading
Loading
+31 −2
Original line number Diff line number Diff line
@@ -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();
        }
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -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) {