Search in sources :

Example 81 with ResourceItem

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

the class ResourceFolderRepositoryTest method testMoveResourceFileBetweenDensityFolders.

public void testMoveResourceFileBetweenDensityFolders() throws Exception {
    // Regression test for https://code.google.com/p/android/issues/detail?id=61648
    // Make sure we flush resource values when reusing resource items incrementally
    // Move a file-based resource file from one configuration to another; verify that
    // items are preserved, generation changed (since it can affect config matching),
    // and resource files updated.
    final VirtualFile file1 = myFixture.copyFileToProject(DRAWABLE, "res/drawable-mdpi/picture.png");
    final VirtualFile file2 = myFixture.copyFileToProject(DRAWABLE, "res/drawable-hdpi/dummy.ignore");
    PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    ResourceItem item = getOnlyItem(resources, ResourceType.DRAWABLE, "picture");
    ResourceFile source = item.getSource();
    assertNotNull(source);
    assertEquals("mdpi", source.getQualifiers());
    ResourceValue resourceValue = item.getResourceValue(false);
    assertNotNull(resourceValue);
    String valuePath = resourceValue.getValue().replace(File.separatorChar, '/');
    assertTrue(valuePath, valuePath.endsWith("res/drawable-mdpi/picture.png"));
    long generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            try {
                // Move file from one location to another
                file1.move(this, file2.getParent());
            } catch (IOException e) {
                fail(e.toString());
            }
        }
    });
    assertTrue(generation < resources.getModificationCount());
    assertTrue(resources.hasResourceItem(ResourceType.DRAWABLE, "picture"));
    item = getOnlyItem(resources, ResourceType.DRAWABLE, "picture");
    source = item.getSource();
    assertNotNull(source);
    assertEquals("hdpi", source.getQualifiers());
    resourceValue = item.getResourceValue(false);
    assertNotNull(resourceValue);
    valuePath = resourceValue.getValue().replace(File.separatorChar, '/');
    assertTrue(valuePath, valuePath.endsWith("res/drawable-hdpi/picture.png"));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFile(com.android.ide.common.res2.ResourceFile) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 82 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 83 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 84 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)

Example 85 with ResourceItem

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

the class ThemeEditorUtilsTest method testCopyTheme.

public void testCopyTheme() {
    myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values/styles.xml");
    myFixture.copyFileToProject("themeEditor/apiTestBefore/stylesApi-v19.xml", "res/values-v19/styles.xml");
    LocalResourceRepository repository = AppResourceRepository.getAppResources(myModule, true);
    assertNotNull(repository);
    List<ResourceItem> resources = repository.getResourceItem(ResourceType.STYLE, "AppTheme");
    assertNotNull(resources);
    assertFalse(resources.isEmpty());
    final XmlTag sourceXml = LocalResourceRepository.getItemTag(getProject(), resources.get(0));
    assertNotNull(sourceXml);
    new WriteCommandAction.Simple(myModule.getProject(), "Copy a theme") {

        @Override
        protected void run() throws Throwable {
            ThemeEditorUtils.copyTheme(16, sourceXml);
            ThemeEditorUtils.copyTheme(19, sourceXml);
        }
    }.execute();
    myFixture.checkResultByFile("res/values-v16/styles.xml", "themeEditor/testCopyTheme/styles-v16.xml", true);
    myFixture.checkResultByFile("res/values-v19/styles.xml", "themeEditor/testCopyTheme/styles-v19.xml", true);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag)

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