Search in sources :

Example 76 with ResourceItem

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

the class ResourceFolderRepositoryTest method testEditArrayItemText.

public void testEditArrayItemText() 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:index and fallback handling for arrays works correctly
    // for getResourceValue()
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    ResourceValue resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("Question 4", resourceValue.getValue());
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("10", resourceValue.getValue());
    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("Question 4");
            document.insertString(offset, "Q");
            documentManager.commitDocument(document);
        }
    });
    // First edit won't be incremental (file -> Psi).
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("QQuestion 4", resourceValue.getValue());
    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("QQuestion 4");
            document.insertString(offset, "Q");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
    resourceValue = array.getResourceValue(false);
    assertNotNull(resourceValue);
    assertEquals("QQQuestion 4", resourceValue.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 77 with ResourceItem

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

the class ResourceFolderRepositoryTest method testEditDeclareStyleableFlag.

public void testEditDeclareStyleableFlag() throws Exception {
    // Rename, add and remove <flag> and <enum> nodes under a declare styleable and assert
    // that the declare styleable parent is updated
    resetScanCounter();
    VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
    final PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
    assertNotNull(psiFile1);
    final ResourceFolderRepository resources = createRepository();
    assertNotNull(resources);
    assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
    // Fetch resource value to ensure it gets replaced after update
    assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
    assertFalse(resources.hasResourceItem(ResourceType.ATTR, "ignore_no_format"));
    final ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
    final DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
    assertNotNull(srv);
    assertEquals(5, srv.getAllAttributes().size());
    final AttrResourceValue flagType = findAttr(srv.getAllAttributes(), "flagType");
    assertNotNull(flagType);
    assertEquals(2, flagType.getAttributeValues().size());
    assertEquals(Integer.valueOf(16), flagType.getAttributeValues().get("flag1"));
    assertEquals(Integer.valueOf(32), flagType.getAttributeValues().get("flag2"));
    final 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("flag1");
            document.insertString(offset + 1, "l");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.isScanPending(psiFile1));
    UIUtil.dispatchAllInvocationEvents();
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            ensureSingleScan();
            assertTrue(generation < resources.getModificationCount());
            assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
            assertTrue(resources.hasResourceItem(ResourceType.ATTR, "flagType"));
            ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
            DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
            assertNotNull(srv);
            assertEquals(5, srv.getAllAttributes().size());
            AttrResourceValue flagType = findAttr(srv.getAllAttributes(), "flagType");
            assertNotNull(flagType);
            assertEquals(2, flagType.getAttributeValues().size());
            assertNull(flagType.getAttributeValues().get("flag1"));
            assertEquals(Integer.valueOf(16), flagType.getAttributeValues().get("fllag1"));
            // Now insert a new enum and delete one and make sure we're still okay
            resetScanCounter();
            long nextGeneration = resources.getModificationCount();
            WriteCommandAction.runWriteCommandAction(null, new Runnable() {

                @Override
                public void run() {
                    String enumAttr = "<enum name=\"type_stopwatch\" value=\"1\"/>";
                    int offset = document.getText().indexOf(enumAttr);
                    document.deleteString(offset, offset + enumAttr.length());
                    String flagAttr = "<flag name=\"flag2\" value=\"0x20\"/>";
                    offset = document.getText().indexOf(flagAttr);
                    document.insertString(offset, "<flag name=\"flag3\" value=\"0x40\"/>");
                    documentManager.commitDocument(document);
                }
            });
            assertTrue(nextGeneration < resources.getModificationCount());
            assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
            assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
            assertTrue(resources.hasResourceItem(ResourceType.ATTR, "flagType"));
            style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
            srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
            assertNotNull(srv);
            assertEquals(5, srv.getAllAttributes().size());
            flagType = findAttr(srv.getAllAttributes(), "flagType");
            assertNotNull(flagType);
            assertEquals(3, flagType.getAttributeValues().size());
            assertEquals(Integer.valueOf(16), flagType.getAttributeValues().get("fllag1"));
            assertEquals(Integer.valueOf(32), flagType.getAttributeValues().get("flag2"));
            assertEquals(Integer.valueOf(64), flagType.getAttributeValues().get("flag3"));
            AttrResourceValue watchType = findAttr(srv.getAllAttributes(), "watchType");
            assertNotNull(watchType);
            assertEquals(1, watchType.getAttributeValues().size());
            assertEquals(Integer.valueOf(0), watchType.getAttributeValues().get("type_countdown"));
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 78 with ResourceItem

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

the class ModuleResourceRepositoryTest method testOverlayUpdates1.

public void testOverlayUpdates1() {
    final VirtualFile layout = myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
    final VirtualFile layoutOverlay = myFixture.copyFileToProject(LAYOUT_OVERLAY, "res2/layout/layout1.xml");
    VirtualFile res1 = myFixture.copyFileToProject(VALUES, "res/values/values.xml").getParent().getParent();
    VirtualFile res2 = myFixture.copyFileToProject(VALUES_OVERLAY1, "res2/values/values.xml").getParent().getParent();
    VirtualFile res3 = myFixture.copyFileToProject(VALUES_OVERLAY2, "res3/values/nameDoesNotMatter.xml").getParent().getParent();
    myFixture.copyFileToProject(VALUES_OVERLAY2_NO, "res3/values-no/values.xml");
    ModuleResourceRepository resources = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2, res3));
    // sanity check
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Layout resource check:
    // Check that our @/layout/layout1 resource currently refers to res2 override,
    // then rename it to @layout/layout2, and verify that we have both, and then
    // rename base to @layout/layout2 and verify that we are back to overriding.
    assertTrue(resources.hasResourceItem(ResourceType.LAYOUT, "layout1"));
    assertFalse(resources.hasResourceItem(ResourceType.LAYOUT, "layout2"));
    ResourceItem layout1 = getSingleItem(resources, ResourceType.LAYOUT, "layout1");
    assertItemIsInDir(res2, layout1);
    long generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            try {
                layoutOverlay.rename(this, "layout2.xml");
            } catch (IOException e) {
                fail(e.toString());
            }
        }
    });
    assertTrue(resources.getModificationCount() > generation);
    assertTrue(resources.hasResourceItem(ResourceType.LAYOUT, "layout2"));
    assertTrue(resources.hasResourceItem(ResourceType.LAYOUT, "layout1"));
    // Layout should now be coming through from res1 since res2 is no longer overriding it
    layout1 = getSingleItem(resources, ResourceType.LAYOUT, "layout1");
    assertItemIsInDir(res1, layout1);
    ResourceItem layout2 = getSingleItem(resources, ResourceType.LAYOUT, "layout2");
    assertItemIsInDir(res2, layout2);
    // Now rename layout1 to layout2 to hide it again
    generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            try {
                layout.rename(this, "layout2.xml");
            } catch (IOException e) {
                fail(e.toString());
            }
        }
    });
    assertTrue(resources.getModificationCount() > generation);
    assertTrue(resources.hasResourceItem(ResourceType.LAYOUT, "layout2"));
    assertFalse(resources.hasResourceItem(ResourceType.LAYOUT, "layout1"));
    layout2 = getSingleItem(resources, ResourceType.LAYOUT, "layout2");
    assertItemIsInDir(res2, layout2);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException) ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 79 with ResourceItem

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

the class ModuleResourceRepositoryTest method getSingleItem.

@NotNull
private static ResourceItem getSingleItem(LocalResourceRepository repository, ResourceType type, String key) {
    List<ResourceItem> list = repository.getResourceItem(type, key);
    assertNotNull(list);
    assertSize(1, list);
    ResourceItem item = list.get(0);
    assertNotNull(item);
    return item;
}
Also used : ResourceItem(com.android.ide.common.res2.ResourceItem) NotNull(org.jetbrains.annotations.NotNull)

Example 80 with ResourceItem

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

the class ResourceFolderRepositoryTest method testAddPluralItems.

public void testAddPluralItems() 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.PLURALS, "my_plural"));
    ResourceItem plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
    ResourceValue resourceValue = plural.getResourceValue(false);
    assertNotNull(resourceValue);
    assertInstanceOf(resourceValue, PluralsResourceValue.class);
    PluralsResourceValue prv = (PluralsResourceValue) resourceValue;
    assertEquals(3, prv.getPluralsCount());
    assertEquals("@string/hello_two", resourceValue.getValue());
    assertEquals("two", prv.getQuantity(1));
    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 quantity=\"two\">@string/hello_two</item>");
            document.insertString(offset, "<item quantity=\"one_and_half\">@string/hello_one_and_half</item>");
            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);
    assertInstanceOf(resourceValue, PluralsResourceValue.class);
    prv = (PluralsResourceValue) resourceValue;
    assertEquals(4, prv.getPluralsCount());
    assertEquals("@string/hello_two", resourceValue.getValue());
    assertEquals("one_and_half", prv.getQuantity(1));
    assertTrue(resources.getModificationCount() > generation);
    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 quantity=\"one_and_half\">@string/hello_one_and_half</item>");
            document.insertString(offset, "<item quantity=\"one_and_a_quarter\">@string/hello_one_and_a_quarter</item>");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.getModificationCount() > generation2);
    plural = getOnlyItem(resources, ResourceType.PLURALS, "my_plural");
    resourceValue = plural.getResourceValue(false);
    assertNotNull(resourceValue);
    assertInstanceOf(resourceValue, PluralsResourceValue.class);
    prv = (PluralsResourceValue) resourceValue;
    assertEquals(5, prv.getPluralsCount());
    assertEquals("@string/hello_two", resourceValue.getValue());
    assertEquals("one_and_a_quarter", prv.getQuantity(1));
    // 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)

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