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

Commit c02a8019 authored by Brandon Liu's avatar Brandon Liu
Browse files

Add a Framework unit test to mock gmail crash.

Previously when new Resources created, it may find an outdated
ResourcesImpl given the ResourcesKey and reuse it, which will
not have proper asset paths appended for shared library.
The issue has been fixed in ag/26665708. This CL is adding a
framework unit test to cover this case.

Bug: b/329915827
Test: Added new tests and verified affected tests pass.
Change-Id: I7772fde67ed72ce7bbbb2b24efd44823a1df2be1
parent 3e4ffe8b
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -421,6 +421,47 @@ public class ResourcesManagerTest extends TestCase {
        ResourcesManager.setInstance(oriResourcesManager);
    }

    @Test
    @SmallTest
    @RequiresFlagsEnabled(Flags.FLAG_REGISTER_RESOURCE_PATHS)
    public void testNewResourcesWithOutdatedImplAfterResourcePathsRegistration()
            throws PackageManager.NameNotFoundException {
        ResourcesManager oriResourcesManager = ResourcesManager.getInstance();
        ResourcesManager.setInstance(mResourcesManager);

        Resources old_resources = mResourcesManager.getResources(
                null, APP_ONE_RES_DIR, null, null, null, null, null, null,
                CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
        assertNotNull(old_resources);
        ResourcesImpl oldImpl = old_resources.getImpl();

        ApplicationInfo appInfo = mPackageManager.getApplicationInfo(TEST_LIB, 0);
        Resources.registerResourcePaths(TEST_LIB, appInfo);

        // Create another resources with identical parameters.
        Resources resources = mResourcesManager.getResources(
                null, APP_ONE_RES_DIR, null, null, null, null, null, null,
                CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
        assertNotNull(resources);
        // For a normal ResourcesImpl redirect, new Resources may find an old ResourcesImpl cache
        // and reuse it based on the ResourcesKey. But for shared library ResourcesImpl redirect,
        // new created Resources should never reuse any old impl, it has to recreate a new impl
        // which has proper asset paths appended.
        assertNotSame(oldImpl, resources.getImpl());

        String[] resourcePaths = appInfo.getAllApkPaths();
        resourcePaths = removeDuplicates(resourcePaths);
        ApkAssets[] loadedAssets = resources.getAssets().getApkAssets();
        assertTrue(allResourcePathsLoaded(resourcePaths, loadedAssets));

        // Package resources' paths should be cached in ResourcesManager.
        assertEquals(Arrays.toString(resourcePaths), Arrays.toString(ResourcesManager.getInstance()
                .getSharedLibAssetsMap().get(TEST_LIB).getAllAssetPaths()));

        // Revert the ResourcesManager instance back.
        ResourcesManager.setInstance(oriResourcesManager);
    }

    private static boolean allResourcePathsLoaded(String[] resourcePaths, ApkAssets[] loadedAsset) {
        for (int i = 0; i < resourcePaths.length; i++) {
            if (!resourcePaths[i].endsWith(".apk")) {