use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testRenameValueFile.
public void testRenameValueFile() throws Exception {
final 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.STRING, "title_template_step"));
List<ResourceItem> items = resources.getResourceItem(ResourceType.STRING, "title_template_step");
assertNotNull(items);
assertEquals(1, items.size());
ResourceItem item = items.get(0);
assertEquals("myvalues.xml", item.getSource().getFile().getName());
// We need to make sure there is a document. PsiVFSListener uses createFileCopyWithNewName
// to populate the new Psi file with the old Psi file's content. However, by the time that runs,
// the old file will already be physically moved to the new file. If it cannot find a cached
// document for the old file, then it will make up an empty old Psi file and copy the empty content
// to the new PsiFile.
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
// Renaming a value file should have no visible effect
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
try {
file1.rename(this, "renamedvalues.xml");
} catch (IOException e) {
fail(e.toString());
}
}
});
assertTrue(resources.hasResourceItem(ResourceType.STRING, "title_template_step"));
items = resources.getResourceItem(ResourceType.STRING, "title_template_step");
assertNotNull(items);
assertEquals(1, items.size());
item = items.get(0);
assertEquals("renamedvalues.xml", item.getSource().getFile().getName());
// TODO: Optimize this such that there's no modification change for this. It's tricky because
// for file names we get separate notification from the old file deletion (beforePropertyChanged)
// and the new file name (propertyChanged). (Note that I tried performing the rename via a
// setName operation on the PsiFile instead of at the raw VirtualFile level, but the resulting
// events were the same.)
//assertEquals(generation, resources.getModificationCount());
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditStyleItemText.
public void testEditStyleItemText() 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, "DarkTheme"));
ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
StyleResourceValue srv = (StyleResourceValue) style.getResourceValue(false);
assertNotNull(srv);
ResourceValue actionBarStyle = srv.getItem("actionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/DarkActionBar", actionBarStyle.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("@style/DarkActionBar");
document.replaceString(offset + 7, offset + 11, "Grey");
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, "DarkTheme"));
style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
srv = (StyleResourceValue) style.getResourceValue(false);
assertNotNull(srv);
actionBarStyle = srv.getItem("actionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/GreyActionBar", actionBarStyle.getValue());
resetScanCounter();
// Now try another edit where things should be incremental now.
long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("@style/GreyActionBar");
document.replaceString(offset + 7, offset + 11, "Light");
documentManager.commitDocument(document);
}
});
assertTrue(generation2 < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkTheme"));
style = getOnlyItem(resources, ResourceType.STYLE, "DarkTheme");
srv = (StyleResourceValue) style.getResourceValue(false);
assertNotNull(srv);
actionBarStyle = srv.getItem("actionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/LightActionBar", actionBarStyle.getValue());
// 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 ModuleResourceRepositoryTest method assertStringIs.
static void assertStringIs(LocalResourceRepository repository, String key, String expected, boolean mustBeUnique) {
assertTrue(repository.hasResourceItem(ResourceType.STRING, key));
List<ResourceItem> list = repository.getResourceItem(ResourceType.STRING, key);
assertNotNull(list);
// expected)
if (mustBeUnique) {
assertSize(1, list);
}
ResourceItem item = list.get(0);
ResourceValue resourceValue = item.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals(expected, resourceValue.getValue());
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ModuleResourceRepositoryTest method assertItemIsInDir.
// Unit test support methods
static void assertItemIsInDir(VirtualFile dir, ResourceItem item) {
ResourceFile resourceFile = item.getSource();
assertNotNull(resourceFile);
VirtualFile parent = VfsUtil.findFileByIoFile(resourceFile.getFile(), false);
assertNotNull(parent);
assertEquals(dir, parent.getParent().getParent());
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditAttr.
public void testEditAttr() throws Exception {
// Insert, remove and change <attr> attributes inside a <declare-styleable> and ensure that
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.DECLARE_STYLEABLE, "MyCustomView"));
// Fetch resource value to ensure it gets replaced after update
assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
assertNotNull(srv);
assertEquals(5, srv.getAllAttributes().size());
AttrResourceValue watchType = findAttr(srv.getAllAttributes(), "watchType");
assertNotNull(watchType);
assertEquals(2, watchType.getAttributeValues().size());
assertEquals(Integer.valueOf(1), watchType.getAttributeValues().get("type_stopwatch"));
assertEquals(Integer.valueOf(0), watchType.getAttributeValues().get("type_countdown"));
AttrResourceValue crash = findAttr(srv.getAllAttributes(), "crash");
assertNotNull(crash);
assertNull(crash.getAttributeValues());
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() {
int offset = document.getText().indexOf("watchType");
document.insertString(offset, "y");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(generation < resources.getModificationCount());
assertFalse(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
assertTrue(resources.hasResourceItem(ResourceType.ATTR, "ywatchType"));
resetScanCounter();
// However, the second edit can then be incremental.
final long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("ywatchType");
document.replaceString(offset, offset + 1, "w");
documentManager.commitDocument(document);
}
});
assertFalse(resources.isScanPending(psiFile1));
assertTrue(generation2 < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
assertFalse(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
assertTrue(resources.hasResourceItem(ResourceType.ATTR, "wwatchType"));
style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
assertNotNull(srv);
assertEquals(5, srv.getAllAttributes().size());
watchType = findAttr(srv.getAllAttributes(), "wwatchType");
assertNotNull(watchType);
assertEquals(2, watchType.getAttributeValues().size());
assertEquals(Integer.valueOf(1), watchType.getAttributeValues().get("type_stopwatch"));
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
// Now insert a new item and delete one and make sure we're still okay
resetScanCounter();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String crashAttr = "<attr name=\"crash\" format=\"boolean\" />";
int offset = document.getText().indexOf(crashAttr);
document.deleteString(offset, offset + crashAttr.length());
document.insertString(offset, "<attr name=\"newcrash\" format=\"integer\" />");
documentManager.commitDocument(document);
}
});
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
ensureSingleScan();
assertTrue(generation2 < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
assertFalse(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
assertTrue(resources.hasResourceItem(ResourceType.ATTR, "wwatchType"));
ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
assertNotNull(srv);
assertEquals(5, srv.getAllAttributes().size());
AttrResourceValue watchType = findAttr(srv.getAllAttributes(), "wwatchType");
assertNotNull(watchType);
assertEquals(2, watchType.getAttributeValues().size());
assertEquals(Integer.valueOf(1), watchType.getAttributeValues().get("type_stopwatch"));
assertEquals(Integer.valueOf(0), watchType.getAttributeValues().get("type_countdown"));
AttrResourceValue crash = findAttr(srv.getAllAttributes(), "crash");
assertNull(crash);
AttrResourceValue newcrash = findAttr(srv.getAllAttributes(), "newcrash");
assertNotNull(newcrash);
assertNull(newcrash.getAttributeValues());
}
});
}
Aggregations