Search in sources :

Example 11 with XmlTagValue

use of com.intellij.psi.xml.XmlTagValue 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

XmlTagValue (com.intellij.psi.xml.XmlTagValue)11 TextRange (com.intellij.openapi.util.TextRange)5 XmlTag (com.intellij.psi.xml.XmlTag)5 NotNull (org.jetbrains.annotations.NotNull)4 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)3 PsiFile (com.intellij.psi.PsiFile)2 XmlText (com.intellij.psi.xml.XmlText)2 ResourceRepository (com.android.ide.common.resources.ResourceRepository)1 ResourceResolver (com.android.ide.common.resources.ResourceResolver)1 Density (com.android.resources.Density)1 ResourceType (com.android.resources.ResourceType)1 ResourceReferenceType (com.android.tools.idea.AndroidPsiUtils.ResourceReferenceType)1 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)1 XmlLikeFileType (com.intellij.ide.highlighter.XmlLikeFileType)1 XmlLexer (com.intellij.lexer.XmlLexer)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)1