Search in sources :

Example 11 with ResourceRepository

use of com.android.ide.common.resources.ResourceRepository in project android by JetBrains.

the class ResourceResolverCacheTest method test.

public void test() throws Exception {
    VirtualFile file1 = myFixture.copyFileToProject("render/layout1.xml", "res/layout/layout1.xml");
    VirtualFile file2 = myFixture.copyFileToProject("render/layout2.xml", "res/layout/layout2.xml");
    VirtualFile file3 = myFixture.copyFileToProject("javadoc/strings/strings.xml", "res/values/strings.xml");
    assertNotNull(file1);
    assertNotNull(file2);
    assertNotNull(file3);
    AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertNotNull(facet);
    Project project = getProject();
    PsiFile psiFile1 = PsiManager.getInstance(project).findFile(file1);
    assertNotNull(psiFile1);
    PsiFile psiFile2 = PsiManager.getInstance(project).findFile(file2);
    assertNotNull(psiFile2);
    final PsiFile psiFile3 = PsiManager.getInstance(project).findFile(file3);
    assertNotNull(psiFile3);
    ConfigurationManager configurationManager = facet.getConfigurationManager();
    assertNotNull(configurationManager);
    final Configuration configuration1 = configurationManager.getConfiguration(file1);
    Configuration configuration2 = configurationManager.getConfiguration(file2);
    assertNotNull(configuration1.getTheme());
    assertEquals(configuration2.getTheme(), configuration1.getTheme());
    ResourceResolver resolver1 = configuration1.getResourceResolver();
    ResourceResolver resolver2 = configuration2.getResourceResolver();
    assertSame(resolver1, resolver2);
    assertSame(resolver1, configuration1.getResourceResolver());
    configuration1.setTheme("Theme.Light");
    final ResourceResolver resolver1b = configuration1.getResourceResolver();
    assertNotSame(resolver1b, resolver1);
    assertNotSame(resolver1b, resolver2);
    assertSame(resolver1b, configuration1.getResourceResolver());
    configuration2.setTheme("Theme.Light");
    assertSame(resolver1b, configuration2.getResourceResolver());
    // Test project resource changes, should invalidate
    final LocalResourceRepository resources = myFacet.getModuleResources(true);
    assertNotNull(resources);
    final long generation = resources.getModificationCount();
    assertEquals("Cancel", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue());
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            //noinspection ConstantConditions
            XmlTagValue value = ((XmlFile) psiFile3).getRootTag().getSubTags()[1].getValue();
            assertEquals("Cancel", value.getTrimmedText());
            value.setText("\"FooBar\"");
        }
    });
    assertTrue(resources.isScanPending(psiFile3));
    ApplicationManager.getApplication().invokeLater(new Runnable() {

        @Override
        public void run() {
            assertTrue(generation < resources.getModificationCount());
            assertNotSame(resolver1b, configuration1.getResourceResolver());
            assertEquals("FooBar", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue());
        }
    });
    ResourceResolverCache cache = configuration1.getConfigurationManager().getResolverCache();
    assertSame(cache, configuration2.getConfigurationManager().getResolverCache());
    ResourceRepository frameworkResources = cache.getFrameworkResources(configuration1.getFullConfig(), configuration1.getTarget());
    assertTrue(frameworkResources instanceof FrameworkResourceLoader.IdeFrameworkResources);
    assertTrue(((FrameworkResourceLoader.IdeFrameworkResources) frameworkResources).getSkippedLocales());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlTagValue(com.intellij.psi.xml.XmlTagValue) XmlFile(com.intellij.psi.xml.XmlFile) FrameworkResourceLoader(org.jetbrains.android.sdk.FrameworkResourceLoader) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceResolver(com.android.ide.common.resources.ResourceResolver) PsiFile(com.intellij.psi.PsiFile) ResourceRepository(com.android.ide.common.resources.ResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository)

Aggregations

ResourceRepository (com.android.ide.common.resources.ResourceRepository)11 ResourceItem (com.android.ide.common.resources.ResourceItem)7 NonNull (android.annotation.NonNull)5 FrameworkResources (com.android.ide.common.resources.FrameworkResources)5 FolderWrapper (com.android.io.FolderWrapper)5 Bridge (com.android.layoutlib.bridge.Bridge)5 File (java.io.File)5 BeforeClass (org.junit.BeforeClass)5 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)4 IAndroidTarget (com.android.sdklib.IAndroidTarget)3 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)3 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 ResourceItemResolver (com.android.ide.common.resources.ResourceItemResolver)1 ResourceValueMap (com.android.ide.common.resources.ResourceValueMap)1 Configurable (com.android.ide.common.resources.configuration.Configurable)1 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)1 ResourceType (com.android.resources.ResourceType)1