use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ProjectResourceRepositoryTest method testInvalidateIds.
// Ensure that we invalidate the id cache when the file is rescanned but ids don't change
// (this was broken)
public void testInvalidateIds() {
// Like testOverlayUpdates1, but rather than testing changes to layout resources (file-based resource)
// perform document edits in value-documents
VirtualFile layoutFile = myFixture.copyFileToProject(LAYOUT, "res/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");
assertNotSame(res1, res2);
assertNotSame(res1, res3);
assertNotSame(res2, res3);
// Just need an empty repository to make it a real module -set-; otherwise with a single
// module we just get a module repository, not a module set repository
LocalResourceRepository other = new LocalResourceRepository("unit test") {
@NonNull
@Override
protected Map<ResourceType, ListMultimap<String, ResourceItem>> getMap() {
return Collections.emptyMap();
}
@Nullable
@Override
protected ListMultimap<String, ResourceItem> getMap(ResourceType type, boolean create) {
return ArrayListMultimap.create();
}
@NotNull
@Override
protected Set<VirtualFile> computeResourceDirs() {
return ImmutableSet.of();
}
};
ModuleResourceRepository module = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2, res3));
final ProjectResourceRepository resources = ProjectResourceRepository.createForTest(myFacet, Arrays.asList(module, other));
PsiFile layoutPsiFile = PsiManager.getInstance(getProject()).findFile(layoutFile);
assertNotNull(layoutPsiFile);
assertTrue(resources.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
final ResourceItem item = getFirstItem(resources, ResourceType.ID, "btn_title_refresh");
final long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(layoutPsiFile);
assertNotNull(document);
WriteCommandAction.runWriteCommandAction(null, () -> {
String string = "<ImageView style=\"@style/TitleBarSeparator\" />";
int offset = document.getText().indexOf(string);
document.deleteString(offset, offset + string.length());
documentManager.commitDocument(document);
});
assertTrue(resources.isScanPending(layoutPsiFile));
ApplicationManager.getApplication().invokeLater(() -> {
assertTrue(generation < resources.getModificationCount());
// Should still be defined:
assertTrue(resources.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
ResourceItem newItem = getFirstItem(resources, ResourceType.ID, "btn_title_refresh");
assertNotNull(newItem.getSource());
// However, should be a different item
assertNotSame(item, newItem);
});
UIUtil.dispatchAllInvocationEvents();
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testComputeResourceStrings.
public void testComputeResourceStrings() throws Exception {
// Tests the handling of markup to raw strings
// For example, for this strings definition
// <string name="title_template_step">Step <xliff:g id="step_number">%1$d</xliff:g>: Lorem Ipsum</string>
// the resource value should be
// Step %1$d: Lorem Ipsum
VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
List<ResourceItem> labelList = resources.getResourceItem(ResourceType.STRING, "title_template_step");
assertNotNull(labelList);
assertEquals(1, labelList.size());
ResourceItem label = labelList.get(0);
ResourceValue resourceValue = label.getResourceValue(false);
assertNotNull(resourceValue);
// In the file, there's whitespace unlike example above
assertEquals("Step ${step_number}: Lorem Ipsum", resourceValue.getValue());
// Test unicode escape handling: <string name="ellipsis">Here it is: …!</string>
labelList = resources.getResourceItem(ResourceType.STRING, "ellipsis");
assertNotNull(labelList);
assertEquals(1, labelList.size());
label = labelList.get(0);
resourceValue = label.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Here it is: …!", resourceValue.getValue());
// Make sure we pick up id's defined using types
assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
assertFalse(resources.hasResourceItem(ResourceType.ID, "action_next2"));
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testMoveValueResourceFileToNewConfiguration.
public void testMoveValueResourceFileToNewConfiguration() throws Exception {
// Move a value 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(VALUES1, "res/values-en/layout1.xml");
final VirtualFile file2 = myFixture.copyFileToProject(VALUES1, "res/values-no/dummy.ignore");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
ResourceItem item = getOnlyItem(resources, ResourceType.STRING, "app_name");
assertEquals("en", item.getSource().getQualifiers());
assertEquals("en", item.getConfiguration().getLocaleQualifier().getLanguage());
//noinspection ConstantConditions
assertEquals("Animations Demo", item.getResourceValue(false).getValue());
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());
item = getOnlyItem(resources, ResourceType.STRING, "app_name");
assertEquals("no", item.getSource().getQualifiers());
assertEquals("no", item.getConfiguration().getLocaleQualifier().getLanguage());
//noinspection ConstantConditions
assertEquals("Animations Demo", item.getResourceValue(false).getValue());
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testAddTypedArrayItemElements.
public void testAddTypedArrayItemElements() 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);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "my_colors"));
ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "my_colors");
ResourceValue resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("#FFFF0000", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
ArrayResourceValue arv = (ArrayResourceValue) resourceValue;
assertEquals(3, arv.getElementCount());
assertEquals("#FFFF0000", arv.getElement(0));
assertEquals("#FF00FF00", arv.getElement(1));
assertEquals("#FF0000FF", arv.getElement(2));
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("<item>#FFFF0000</item>");
document.insertString(offset, "<item>#FFFFFF00</item>");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "my_colors"));
array = getOnlyItem(resources, ResourceType.ARRAY, "my_colors");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("#FFFFFF00", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(4, arv.getElementCount());
assertEquals("#FFFFFF00", arv.getElement(0));
assertEquals("#FFFF0000", arv.getElement(1));
assertEquals("#FF00FF00", arv.getElement(2));
resetScanCounter();
// Now try a second edit.
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("<item>#FFFFFF00</item>");
document.insertString(offset, "<item>#FFFFFFFF</item>");
documentManager.commitDocument(document);
}
});
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "my_colors"));
array = getOnlyItem(resources, ResourceType.ARRAY, "my_colors");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("#FFFFFFFF", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(5, arv.getElementCount());
assertEquals("#FFFFFFFF", arv.getElement(0));
assertEquals("#FFFFFF00", arv.getElement(1));
assertEquals("#FFFF0000", arv.getElement(2));
assertEquals("#FF00FF00", arv.getElement(3));
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testMoveFileResourceFileToNewType.
public void testMoveFileResourceFileToNewType() throws Exception {
// Move a file resource file file from one folder to another, changing the type
// (e.g. anim to animator), verify that resource types are updated
final VirtualFile file1 = myFixture.copyFileToProject(LAYOUT1, "res/layout/layout1.xml");
final VirtualFile file2 = myFixture.copyFileToProject(LAYOUT1, "res/menu/dummy.ignore");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
assertTrue(resources.hasResourceItem(ResourceType.LAYOUT, "layout1"));
long generation = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
try {
file1.move(this, file2.getParent());
} catch (IOException e) {
fail(e.toString());
}
}
});
assertTrue(generation < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.MENU, "layout1"));
assertFalse(resources.hasResourceItem(ResourceType.LAYOUT, "layout1"));
ResourceItem item = getOnlyItem(resources, ResourceType.MENU, "layout1");
assertSame(ResourceFolderType.MENU, ((PsiResourceFile) item.getSource()).getFolderType());
}
Aggregations