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();
}
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();
}
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());
}
});
}
Aggregations