Search in sources :

Example 86 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class ResourceFolderRepositoryTest method testAddStyleItem.

public void testAddStyleItem() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkActionBar"));
    ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkActionBar");
    StyleResourceValue srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertSameElements(srv.getNames(), "android:background", "android:textColor");
    ResourceValue background = srv.getItem("background", true);
    assertNotNull(background);
    assertEquals("@android:color/transparent", background.getValue());
    ResourceValue textColor = srv.getItem("textColor", true);
    assertNotNull(textColor);
    assertEquals("#008", textColor.getValue());
    long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("<item name=\"android:background\"");
            assertTrue(offset > 0);
            document.insertString(offset, "<item name=\"android:textSize\">20sp</item>");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(generation < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkActionBar"));
    style = getOnlyItem(resources, ResourceType.STYLE, "DarkActionBar");
    srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertSameElements(srv.getNames(), "android:background", "android:textSize", "android:textColor");
    ResourceValue textSize = srv.getItem("textSize", true);
    assertNotNull(textSize);
    assertEquals("20sp", textSize.getValue());
    resetScanCounter();
    // Now try a second edit.
    long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("<item name=\"android:background\"");
            assertTrue(offset > 0);
            document.insertString(offset, "<item name=\"android:typeface\">monospace</item>");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(generation2 < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkActionBar"));
    style = getOnlyItem(resources, ResourceType.STYLE, "DarkActionBar");
    srv = (StyleResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertSameElements(srv.getNames(), "android:background", "android:typeface", "android:textSize", "android:textColor");
    ResourceValue typeface = srv.getItem("typeface", true);
    assertNotNull(typeface);
    assertEquals("monospace", typeface.getValue());
    // Shouldn't have done any full file rescans during the above edits
    ensureIncremental();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 87 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class ResourceFolderRepositoryTest method testEditPluralItems.

public void testEditPluralItems() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    // Test that our tools:quantity works correctly for getResourceValue()
    assertTrue(resources.hasResourceItem(ResourceType.PLURALS, "my_plural"));
    ResourceItem plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
    ResourceValue resourceValue = plural.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("@string/hello_two", resourceValue.getValue());
    // TODO: It would be nice to avoid updating the generation if you
    // edit a different item than the one being picked (default or via
    // tools:quantity) but for now we're not worrying about that optimization
    long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            final int offset = document.getText().indexOf("@string/hello_two");
            document.replaceString(offset + 9, offset + 10, "a");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
    resourceValue = plural.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("@string/hallo_two", resourceValue.getValue());
    assertTrue(generation < resources.getModificationCount());
    resetScanCounter();
    // However, the second edit can then be incremental.
    long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("@string/hallo_two");
            document.replaceString(offset + 9, offset + 10, "i");
            documentManager.commitDocument(document);
        }
    });
    plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
    resourceValue = plural.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("@string/hillo_two", resourceValue.getValue());
    assertTrue(generation2 < resources.getModificationCount());
    // Shouldn't have done any full file rescans during the above edits
    ensureIncremental();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 88 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class ResourceFolderRepositoryTest method testEditStyleParent.

public void testEditStyleParent() throws Exception {
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
    // Change style parent
    final long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiFile1);
    assertNotNull(document);
    // First edit won't be incremental (file -> Psi).
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            final int offset = document.getText().indexOf("android:Theme.Holo");
            document.replaceString(offset, offset + "android:Theme.Holo".length(), "android:Theme.Light");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            ensureSingleScan();
            assertTrue(generation < resources.getModificationCount());
            ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
            ResourceValue resourceValue = style.getResourceValue(false);
            assertNotNull(resourceValue);
            assertTrue(resourceValue instanceof StyleResourceValue);
            StyleResourceValue srv = (StyleResourceValue) resourceValue;
            assertEquals("android:Theme.Light", srv.getParentStyle());
            ResourceValue actionBarStyle = srv.getItem("actionBarStyle", true);
            assertNotNull(actionBarStyle);
            assertEquals("@style/DarkActionBar", actionBarStyle.getValue());
            resetScanCounter();
        }
    });
    // Even on the second edit we don't expect editing the style parent to be incremental.
    final long generation2 = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            final int offset = document.getText().indexOf("android:Theme.Light");
            document.replaceString(offset, offset + "android:Theme.Light".length(), "android:Theme.Material");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            ensureSingleScan();
            assertTrue(generation2 < resources.getModificationCount());
            ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
            ResourceValue resourceValue = style.getResourceValue(false);
            assertNotNull(resourceValue);
            assertTrue(resourceValue instanceof StyleResourceValue);
            StyleResourceValue srv = (StyleResourceValue) resourceValue;
            assertEquals("android:Theme.Material", srv.getParentStyle());
            ResourceValue actionBarStyle = srv.getItem("actionBarStyle", true);
            assertNotNull(actionBarStyle);
            assertEquals("@style/DarkActionBar", actionBarStyle.getValue());
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) ResourceItem(com.android.ide.common.res2.ResourceItem) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Aggregations

ResourceItem (com.android.ide.common.res2.ResourceItem)83 VirtualFile (com.intellij.openapi.vfs.VirtualFile)44 PsiFile (com.intellij.psi.PsiFile)35 Document (com.intellij.openapi.editor.Document)26 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)26 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)11 XmlTag (com.intellij.psi.xml.XmlTag)11 NotNull (org.jetbrains.annotations.NotNull)11 IOException (java.io.IOException)10 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)8 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)8 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)7 ResourceFile (com.android.ide.common.res2.ResourceFile)7 ResourceType (com.android.resources.ResourceType)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)7 Nullable (org.jetbrains.annotations.Nullable)7 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)6 Project (com.intellij.openapi.project.Project)6 File (java.io.File)6