Search in sources :

Example 71 with ResourceItem

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

the class ModuleResourceRepositoryTest method getFirstItem.

@NotNull
static ResourceItem getFirstItem(LocalResourceRepository repository, ResourceType type, String key) {
    List<ResourceItem> list = repository.getResourceItem(type, key);
    assertNotNull(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 72 with ResourceItem

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

the class ModuleResourceRepositoryTest method testOverlays.

public void testOverlays() {
    myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
    myFixture.copyFileToProject(LAYOUT_OVERLAY, "res2/layout/layout1.xml");
    myFixture.copyFileToProject(LAYOUT_IDS_1, "res2/layout/layout_ids1.xml");
    myFixture.copyFileToProject(LAYOUT_IDS_2, "res2/layout/layout_ids2.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");
    assertNotSame(res1, res2);
    assertNotSame(res1, res3);
    assertNotSame(res2, res3);
    ModuleResourceRepository resources = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2, res3));
    // Check that values are handled correctly. First a plain value (not overridden anywhere).
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Check that an overridden key (overridden in just one flavor) is picked up
    // Overridden in res2
    assertStringIs(resources, "title_crossfade", "Complex Crossfade");
    // Overridden in res3
    assertStringIs(resources, "title_zoom", "Zoom!");
    // Make sure that new/unique strings from flavors are available
    // Overridden in res2
    assertStringIs(resources, "unique_string", "Unique");
    // Overridden in res3
    assertStringIs(resources, "another_unique_string", "Another Unique", false);
    // Check that an overridden key (overridden in multiple flavors) picks the last one
    // res3 (not unique because we have a values-no item too)
    assertStringIs(resources, "app_name", "Very Different App Name", false);
    // Layouts: Should only be offered id's from the overriding layout (plus those defined in values.xml)
    // from values.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
    // from res2 layout1.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
    // Layout masking does not currently work. I'm not 100% certain what the intended behavior is
    // here (e.g. res1's layout1 contains @+id/button1, res2's layout1 does not; should @+id/button1 be visible?)
    //assertFalse(resources.hasResourceItem(ResourceType.ID, "btn_title_refresh")); // masked in res1 by res2's layout replacement
    // Check that localized lookup (qualifier matching works)
    List<ResourceItem> stringList = resources.getResourceItem(ResourceType.STRING, "another_unique_string");
    assertNotNull(stringList);
    assertSize(2, stringList);
    FolderConfiguration valueConfig = FolderConfiguration.getConfigForFolder("values-no");
    assertNotNull(valueConfig);
    ResourceValue stringValue = resources.getConfiguredResources(ResourceType.STRING, valueConfig).get("another_unique_string");
    assertNotNull(stringValue);
    assertEquals("En Annen", stringValue.getValue());
    // Change flavor order and make sure things are updated and work correctly
    resources.updateRoots(Arrays.asList(res1, res3, res2));
    // Should now be picking app_name from res2 rather than res3 since it's now last
    // res2
    assertStringIs(resources, "app_name", "Different App Name", false);
    // Sanity check other merging
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Overridden in res2
    assertStringIs(resources, "title_crossfade", "Complex Crossfade");
    // Overridden in res3
    assertStringIs(resources, "title_zoom", "Zoom!");
    // Overridden in res2
    assertStringIs(resources, "unique_string", "Unique");
    // Overridden in res3
    assertStringIs(resources, "another_unique_string", "Another Unique", false);
    // Hide a resource root (res2)
    resources.updateRoots(Arrays.asList(res1, res3));
    // No longer aliasing the main layout
    // res1 layout1.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
    // from res1 layout1.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
    // from values.xml
    assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
    // No longer overridden in res2
    assertStringIs(resources, "title_crossfade", "Simple Crossfade");
    // Finally ensure that we can switch roots repeatedly (had some earlier bugs related to root unregistration)
    resources.updateRoots(Arrays.asList(res1, res3, res2));
    resources.updateRoots(Arrays.asList(res1));
    resources.updateRoots(Arrays.asList(res1, res3, res2));
    resources.updateRoots(Arrays.asList(res1));
    resources.updateRoots(Arrays.asList(res1, res3, res2));
    resources.updateRoots(Arrays.asList(res2));
    resources.updateRoots(Arrays.asList(res1));
    resources.updateRoots(Arrays.asList(res1, res2, res3));
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Make sure I get all the resource ids (there can be multiple; these are not replaced via overlays)
    List<ResourceItem> ids = resources.getResourceItem(ResourceType.ID, "my_id");
    assertNotNull(ids);
    assertSize(2, ids);
    Collections.sort(ids, new Comparator<ResourceItem>() {

        @SuppressWarnings("ConstantConditions")
        @Override
        public int compare(ResourceItem item1, ResourceItem item2) {
            return item1.getSource().getFile().getName().compareTo(item2.getSource().getFile().getName());
        }
    });
    //noinspection ConstantConditions
    assertEquals("layout_ids1.xml", ids.get(0).getSource().getFile().getName());
    //noinspection ConstantConditions
    assertEquals("layout_ids2.xml", ids.get(1).getSource().getFile().getName());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 73 with ResourceItem

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

the class ModuleResourceRepositoryTest method testOverlayUpdates2.

public void testOverlayUpdates2() {
    // Like testOverlayUpdates1, but rather than testing changes to layout resources (file-based resource)
    // perform document edits in value-documents
    myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
    myFixture.copyFileToProject(LAYOUT_OVERLAY, "res2/layout/layout1.xml");
    VirtualFile values1 = myFixture.copyFileToProject(VALUES, "res/values/values.xml");
    VirtualFile values2 = myFixture.copyFileToProject(VALUES_OVERLAY1, "res2/values/values.xml");
    VirtualFile values3 = myFixture.copyFileToProject(VALUES_OVERLAY2, "res3/values/nameDoesNotMatter.xml");
    final VirtualFile values3No = myFixture.copyFileToProject(VALUES_OVERLAY2_NO, "res3/values-no/values.xml");
    VirtualFile res1 = values1.getParent().getParent();
    VirtualFile res2 = values2.getParent().getParent();
    VirtualFile res3 = values3.getParent().getParent();
    ModuleResourceRepository resources = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2, res3));
    PsiFile psiValues1 = PsiManager.getInstance(getProject()).findFile(values1);
    assertNotNull(psiValues1);
    PsiFile psiValues2 = PsiManager.getInstance(getProject()).findFile(values2);
    assertNotNull(psiValues2);
    PsiFile psiValues3 = PsiManager.getInstance(getProject()).findFile(values3);
    assertNotNull(psiValues3);
    PsiFile psiValues3No = PsiManager.getInstance(getProject()).findFile(values3No);
    assertNotNull(psiValues3No);
    // Initial state; sanity check from #testOverlays()
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    // Overridden in res2
    assertStringIs(resources, "title_crossfade", "Complex Crossfade");
    // Overridden in res3
    assertStringIs(resources, "title_zoom", "Zoom!");
    // Overridden in res2
    assertStringIs(resources, "unique_string", "Unique");
    // Overridden in res3
    assertStringIs(resources, "another_unique_string", "Another Unique", false);
    // res3 (not unique because we have a values-no item too)
    assertStringIs(resources, "app_name", "Very Different App Name", false);
    // Value resource check:
    // Verify that an edit in a value file, both in a non-overridden and an overridden
    // value, is observed; and that an override in an overridden value is not observed.
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "app_name"));
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "title_layout_changes"));
    ResourceItem appName = getFirstItem(resources, ResourceType.STRING, "app_name");
    assertItemIsInDir(res3, appName);
    // res3 (not unique because we have a values-no item too)
    assertStringIs(resources, "app_name", "Very Different App Name", false);
    long generation = resources.getModificationCount();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
    final Document document = documentManager.getDocument(psiValues3);
    assertNotNull(document);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("Very Different App Name");
            document.insertString(offset, "Not ");
            documentManager.commitDocument(document);
        }
    });
    // The first edit to psiValues3 causes ResourceFolderRepository to transition from non-Psi -> Psi which requires a rescan.
    assertTrue(resources.isScanPending(psiValues3));
    UIUtil.dispatchAllInvocationEvents();
    assertTrue(resources.getModificationCount() > generation);
    // Should still be defined in res3 but have new value.
    // The order of items may have swapped if a full rescan is done.
    List<ResourceItem> list = resources.getResourceItem(ResourceType.STRING, "app_name");
    assertNotNull(list);
    assertSize(2, list);
    appName = ContainerUtil.find(list, new Condition<ResourceItem>() {

        @Override
        public boolean value(ResourceItem resourceItem) {
            return resourceItem.getQualifiers().isEmpty();
        }
    });
    assertNotNull(appName);
    assertItemIsInDir(res3, appName);
    ResourceValue appNameResourceValue = appName.getResourceValue(false);
    assertNotNull(appNameResourceValue);
    assertEquals("Not Very Different App Name", appNameResourceValue.getValue());
    // Try renaming the item name.
    generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document.getText().indexOf("app_name");
            document.insertString(offset, "r");
            documentManager.commitDocument(document);
        }
    });
    assertTrue(resources.getModificationCount() > generation);
    assertTrue(resources.hasResourceItem(ResourceType.STRING, "rapp_name"));
    appName = getFirstItem(resources, ResourceType.STRING, "app_name");
    // The item is still under res3, but now it's in the Norwegian translation
    assertEquals("no", appName.getSource().getQualifiers());
    assertStringIs(resources, "app_name", "Forskjellig Navn", false);
    // Delete that file:
    generation = resources.getModificationCount();
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            try {
                values3No.delete(this);
            } catch (IOException e) {
                fail(e.toString());
            }
        }
    });
    assertTrue(resources.getModificationCount() > generation);
    // Now the item is no longer available in res3; should fallback to res 2
    appName = getFirstItem(resources, ResourceType.STRING, "app_name");
    assertItemIsInDir(res2, appName);
    assertStringIs(resources, "app_name", "Different App Name", false);
    // Check that editing an overridden attribute does not count as a change
    final Document document2 = documentManager.getDocument(psiValues1);
    assertNotNull(document2);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document2.getText().indexOf("Animations Demo");
            document2.insertString(offset, "Cool ");
            documentManager.commitDocument(document2);
        }
    });
    // The first edit to psiValues1 causes ResourceFolderRepository to transition from non-Psi -> Psi which requires a rescan.
    assertTrue(resources.isScanPending(psiValues1));
    UIUtil.dispatchAllInvocationEvents();
    // Unaffected by above change
    assertStringIs(resources, "app_name", "Different App Name", false);
    // Finally check that editing an non-overridden attribute also gets picked up as a change
    generation = resources.getModificationCount();
    // Observe after the rescan, so that an edit causes a generation bump.
    assertStringIs(resources, "title_layout_changes", "Layout Changes");
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            int offset = document2.getText().indexOf("Layout Changes");
            document2.insertString(offset, "New ");
            documentManager.commitDocument(document2);
        }
    });
    assertTrue(resources.getModificationCount() > generation);
    assertStringIs(resources, "title_layout_changes", "New Layout Changes", false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Condition(com.intellij.openapi.util.Condition) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) PsiFile(com.intellij.psi.PsiFile) ResourceItem(com.android.ide.common.res2.ResourceItem) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 74 with ResourceItem

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

the class ProjectResourceRepositoryTest method testDependencies.

// Regression test for https://code.google.com/p/android/issues/detail?id=65140
public void testDependencies() throws Exception {
    myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
    Module lib1 = null;
    Module lib2 = null;
    Module sharedLib = null;
    Module app = null;
    for (Module module : ModuleManager.getInstance(getProject()).getModules()) {
        if (module != myModule) {
            VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots();
            assertEquals(1, contentRoots.length);
            String name = contentRoots[0].getName();
            switch(name) {
                case "lib1":
                    lib1 = module;
                    break;
                case "lib2":
                    lib2 = module;
                    break;
                case "sharedlib":
                    sharedLib = module;
                    break;
                case "app":
                    app = module;
                    break;
                default:
                    fail(name);
                    break;
            }
        }
    }
    assertNotNull(lib1);
    assertNotNull(lib2);
    assertNotNull(sharedLib);
    assertNotNull(app);
    renameModule(lib1, "lib1");
    renameModule(lib2, "lib2");
    renameModule(sharedLib, "sharedLib");
    renameModule(app, "app");
    AndroidFacet lib1Facet = AndroidFacet.getInstance(lib1);
    AndroidFacet lib2Facet = AndroidFacet.getInstance(lib2);
    AndroidFacet sharedLibFacet = AndroidFacet.getInstance(sharedLib);
    AndroidFacet appFacet = AndroidFacet.getInstance(app);
    assertNotNull(lib1Facet);
    assertNotNull(lib2Facet);
    assertNotNull(sharedLibFacet);
    assertNotNull(appFacet);
    // Set up project dependencies
    addModuleDependency(lib1, sharedLib);
    addModuleDependency(lib2, sharedLib);
    addModuleDependency(app, lib1);
    addModuleDependency(app, lib2);
    assertTrue(ModuleRootManager.getInstance(app).isDependsOn(lib1));
    assertTrue(ModuleRootManager.getInstance(lib1).isDependsOn(sharedLib));
    assertFalse(ModuleRootManager.getInstance(sharedLib).isDependsOn(lib1));
    assertFalse(ModuleRootManager.getInstance(lib2).isDependsOn(lib1));
    // Note that these are currently direct dependencies only, so app.isDependsOn(sharedLib) is false
    // Test AndroidUtils#getallAndroidDependencies
    List<AndroidFacet> appDependsOn = AndroidUtils.getAllAndroidDependencies(app, true);
    assertTrue(appDependsOn.contains(lib1Facet));
    assertTrue(appDependsOn.contains(lib2Facet));
    assertTrue(appDependsOn.contains(sharedLibFacet));
    assertFalse(appDependsOn.contains(appFacet));
    List<AndroidFacet> lib1DependsOn = AndroidUtils.getAllAndroidDependencies(lib1, true);
    assertTrue(lib1DependsOn.contains(sharedLibFacet));
    assertFalse(lib1DependsOn.contains(appFacet));
    assertFalse(lib1DependsOn.contains(lib1Facet));
    assertFalse(lib1DependsOn.contains(lib2Facet));
    // Set up resources so we can check which values are inherited through module dependencies
    VirtualFile v1 = myFixture.copyFileToProject(VALUES, "additionalModules/sharedlib/res/values/sharedvalues.xml");
    VirtualFile v2 = myFixture.copyFileToProject(VALUES_OVERLAY1, "additionalModules/lib2/res/values/lib2values.xml");
    assertNotNull(v1);
    assertNotNull(v2);
    PsiManager manager = PsiManager.getInstance(getProject());
    PsiFile sharedLibValues = manager.findFile(v1);
    PsiFile lib2Values = manager.findFile(v2);
    assertNotNull(sharedLibValues);
    assertNotNull(lib2Values);
    final AppResourceRepository lib1Resources = lib1Facet.getAppResources(true);
    final AppResourceRepository lib2Resources = lib2Facet.getAppResources(true);
    assertNotNull(lib1Resources);
    assertNotNull(lib2Resources);
    assertNotSame(lib1Resources, lib2Resources);
    assertFalse(lib1Resources.isScanPending(sharedLibValues));
    assertFalse(lib1Resources.isScanPending(lib2Values));
    assertFalse(lib2Resources.isScanPending(sharedLibValues));
    assertFalse(lib2Resources.isScanPending(lib2Values));
    assertTrue(lib1Resources.hasResourceItem(ResourceType.PLURALS, "my_plural"));
    assertTrue(lib1Resources.hasResourceItem(ResourceType.STRING, "ellipsis"));
    assertTrue(lib1Resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    List<ResourceItem> items = lib1Resources.getResourceItem(ResourceType.STRING, "ellipsis");
    assertNotNull(items);
    ResourceValue firstValue = items.get(0).getResourceValue(false);
    assertNotNull(firstValue);
    assertEquals("Here it is: …!", firstValue.getValue());
    assertTrue(lib2Resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
    assertTrue(lib2Resources.hasResourceItem(ResourceType.PLURALS, "my_plural"));
    assertTrue(lib2Resources.hasResourceItem(ResourceType.STRING, "ellipsis"));
    // ONLY defined in lib2: should not be visible from lib1
    assertTrue(lib2Resources.hasResourceItem(ResourceType.STRING, "unique_string"));
    items = lib2Resources.getResourceItem(ResourceType.STRING, "unique_string");
    assertNotNull(items);
    firstValue = items.get(0).getResourceValue(false);
    assertNotNull(firstValue);
    assertEquals("Unique", firstValue.getValue());
    assertFalse(lib1Resources.hasResourceItem(ResourceType.STRING, "unique_string"));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) ResourceItem(com.android.ide.common.res2.ResourceItem) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 75 with ResourceItem

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

the class ResourceClassGeneratorTest method test.

public void test() throws Exception {
    final ResourceRepository repository = TestResourceRepository.createRes2(false, new Object[] { "layout/layout1.xml", "<!--contents doesn't matter-->", "layout-land/layout1.xml", "<!--contents doesn't matter-->", "values/styles.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "    <style name=\"MyTheme.Dark\" parent=\"android:Theme.Light\">\n" + "        <item name=\"android:textColor\">#999999</item>\n" + "        <item name=\"foo\">?android:colorForeground</item>\n" + "    </style>\n" + "    <declare-styleable name=\"GridLayout_Layout\">\n" + "        <attr name=\"android:layout_width\" />\n" + "        <attr name=\"android:layout_height\" />\n" + "        <attr name=\"layout_columnSpan\" format=\"integer\" min=\"1\" />\n" + "        <attr name=\"layout_gravity\">\n" + "            <flag name=\"top\" value=\"0x30\" />\n" + "            <flag name=\"bottom\" value=\"0x50\" />\n" + "            <flag name=\"center_vertical\" value=\"0x10\" />\n" + "        </attr>\n" + "    </declare-styleable>\n" + "</resources>\n", "values/strings.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "    <item type=\"id\" name=\"action_bar_refresh\" />\n" + "    <item type=\"dimen\" name=\"dialog_min_width_major\">45%</item>\n" + "    <string name=\"show_all_apps\">All</string>\n" + "    <string name=\"menu_wallpaper\">Wallpaper</string>\n" + "</resources>\n", "values-es/strings.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + "    <string name=\"show_all_apps\">Todo</string>\n" + "</resources>\n" });
    LocalResourceRepository resources = new LocalResourceRepository("test") {

        @NonNull
        @Override
        protected Map<ResourceType, ListMultimap<String, ResourceItem>> getMap() {
            return repository.getItems();
        }

        @Nullable
        @Override
        protected ListMultimap<String, ResourceItem> getMap(ResourceType type, boolean create) {
            return repository.getItems().get(type);
        }

        @NotNull
        @Override
        protected Set<VirtualFile> computeResourceDirs() {
            return ImmutableSet.of();
        }
    };
    AppResourceRepository appResources = new AppResourceRepository(myFacet, Collections.singletonList(resources), Collections.<FileResourceRepository>emptyList());
    ResourceClassGenerator generator = ResourceClassGenerator.create(appResources);
    assertNotNull(generator);
    String name = "my.test.pkg.R";
    Class<?> clz = generateClass(generator, name);
    assertNotNull(clz);
    assertEquals(name, clz.getName());
    assertTrue(Modifier.isPublic(clz.getModifiers()));
    assertTrue(Modifier.isFinal(clz.getModifiers()));
    assertFalse(Modifier.isInterface(clz.getModifiers()));
    Object r = clz.newInstance();
    assertNotNull(r);
    name = "my.test.pkg.R$string";
    clz = generateClass(generator, name);
    assertNotNull(clz);
    assertEquals(name, clz.getName());
    assertTrue(Modifier.isPublic(clz.getModifiers()));
    assertTrue(Modifier.isFinal(clz.getModifiers()));
    assertFalse(Modifier.isInterface(clz.getModifiers()));
    try {
        clz.getField("nonexistent");
        fail("Shouldn't find nonexistent fields");
    } catch (NoSuchFieldException e) {
    // pass
    }
    Field field1 = clz.getField("menu_wallpaper");
    Object value1 = field1.get(null);
    assertEquals(Integer.TYPE, field1.getType());
    assertNotNull(value1);
    assertEquals(2, clz.getFields().length);
    Field field2 = clz.getField("show_all_apps");
    assertNotNull(field2);
    assertEquals(Integer.TYPE, field2.getType());
    assertTrue(Modifier.isPublic(field2.getModifiers()));
    assertTrue(Modifier.isFinal(field2.getModifiers()));
    assertTrue(Modifier.isStatic(field2.getModifiers()));
    assertFalse(Modifier.isSynchronized(field2.getModifiers()));
    assertFalse(Modifier.isTransient(field2.getModifiers()));
    assertFalse(Modifier.isStrict(field2.getModifiers()));
    assertFalse(Modifier.isVolatile(field2.getModifiers()));
    r = clz.newInstance();
    assertNotNull(r);
    // Make sure the id's match what we've dynamically allocated in the resource repository
    @SuppressWarnings("deprecation") Pair<ResourceType, String> pair = appResources.resolveResourceId((Integer) clz.getField("menu_wallpaper").get(null));
    assertNotNull(pair);
    assertEquals(ResourceType.STRING, pair.getFirst());
    assertEquals("menu_wallpaper", pair.getSecond());
    assertEquals(clz.getField("menu_wallpaper").get(null), appResources.getResourceId(ResourceType.STRING, "menu_wallpaper"));
    assertEquals(clz.getField("show_all_apps").get(null), appResources.getResourceId(ResourceType.STRING, "show_all_apps"));
    // Test attr class!
    name = "my.test.pkg.R$attr";
    clz = generateClass(generator, name);
    assertNotNull(clz);
    assertEquals(name, clz.getName());
    assertTrue(Modifier.isPublic(clz.getModifiers()));
    assertTrue(Modifier.isFinal(clz.getModifiers()));
    assertFalse(Modifier.isInterface(clz.getModifiers()));
    assertEquals(2, clz.getFields().length);
    field1 = clz.getField("layout_gravity");
    assertNotNull(field1);
    Object gravityValue = field1.get(null);
    Object layoutColumnSpanValue = clz.getField("layout_columnSpan").get(null);
    // Test style class
    styleTest(generator);
    // Run the same test to check caching.
    styleTest(generator);
    // Test styleable class!
    styleableTest(generator, gravityValue, layoutColumnSpanValue);
    // Run the same test again to ensure that caching is working as expected.
    styleableTest(generator, gravityValue, layoutColumnSpanValue);
    name = "my.test.pkg.R$id";
    clz = generateClass(generator, name);
    assertNotNull(clz);
    r = clz.newInstance();
    assertNotNull(r);
    assertEquals(name, clz.getName());
    // getEnclosingClass() results in generating all R classes. So, this should be called at the end
    // so that tests for caching work as expected.
    Class<?> enclosingClass = clz.getEnclosingClass();
    assertNotNull(enclosingClass);
// TODO: Flag and enum values should also be created as id's by the ValueResourceParser
//assertNotNull(clz.getField("top"));
//assertNotNull(clz.getField("bottom"));
//assertNotNull(clz.getField("center_vertical"));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) Field(java.lang.reflect.Field) TestResourceRepository(com.android.ide.common.resources.TestResourceRepository) ResourceRepository(com.android.ide.common.res2.ResourceRepository) ListMultimap(com.google.common.collect.ListMultimap) ResourceItem(com.android.ide.common.res2.ResourceItem)

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