use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceNameValidatorTest method testIds2.
public void testIds2() throws Exception {
final Map<ResourceType, ListMultimap<String, ResourceItem>> map = Maps.newHashMap();
ListMultimap<String, ResourceItem> multimap = ArrayListMultimap.create();
map.put(ResourceType.ID, multimap);
multimap.put("foo1", new ResourceItem("foo1", ResourceType.ID, null, null));
multimap.put("foo3", new ResourceItem("foo3", ResourceType.ID, null, null));
multimap.put("foo.4", new ResourceItem("foo.4", ResourceType.ID, null, null));
multimap.put("foo_5", new ResourceItem("foo_5", ResourceType.ID, null, null));
multimap.put("foo-6", new ResourceItem("foo-6", ResourceType.ID, null, null));
multimap.put("foo:7", new ResourceItem("foo:7", ResourceType.ID, null, null));
LocalResourceRepository resources = new LocalResourceRepository("unit test") {
@NonNull
@Override
protected Map<ResourceType, ListMultimap<String, ResourceItem>> getMap() {
return map;
}
@Nullable
@Override
protected ListMultimap<String, ResourceItem> getMap(ResourceType type, boolean create) {
return map.get(type);
}
@NotNull
@Override
protected Set<VirtualFile> computeResourceDirs() {
return ImmutableSet.of();
}
};
ResourceNameValidator validator = ResourceNameValidator.create(false, resources, ResourceType.ID);
assertEquals("foo1 already exists", validator.getErrorText("foo1"));
assertEquals(null, validator.getErrorText("foo2"));
assertEquals("foo3 already exists", validator.getErrorText("foo3"));
assertEquals("foo_4 already exists", validator.getErrorText("foo.4"));
assertEquals("foo_4 already exists", validator.getErrorText("foo:4"));
assertEquals("foo_4 already exists", validator.getErrorText("foo-4"));
assertEquals("foo_4 already exists", validator.getErrorText("foo_4"));
assertEquals("foo_5 already exists", validator.getErrorText("foo.5"));
assertEquals("foo_5 already exists", validator.getErrorText("foo:5"));
assertEquals("foo_5 already exists", validator.getErrorText("foo-5"));
assertEquals("foo_5 already exists", validator.getErrorText("foo_5"));
assertEquals("foo_6 already exists", validator.getErrorText("foo.6"));
assertEquals("foo_6 already exists", validator.getErrorText("foo:6"));
assertEquals("foo_6 already exists", validator.getErrorText("foo-6"));
assertEquals("foo_6 already exists", validator.getErrorText("foo_6"));
assertEquals("foo_7 already exists", validator.getErrorText("foo.7"));
assertEquals("foo_7 already exists", validator.getErrorText("foo:7"));
assertEquals("foo_7 already exists", validator.getErrorText("foo-7"));
assertEquals("foo_7 already exists", validator.getErrorText("foo_7"));
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testNestedEditValueText.
public void testNestedEditValueText() 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);
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);
assertEquals("Step ${step_number}: Lorem Ipsum", resourceValue.getValue());
long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
// Edit value should cause update
final int textOffset = document.getText().indexOf("Lorem");
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.insertString(textOffset + 1, "l");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(generation < resources.getModificationCount());
// Because of the file -> psi transition, we can't rely on the old ResourceItem references (replaced).
// We need to ensure that callers don't hang on to the old ResourceItem, assuming it'll get
// updated in place after an edit.
labelList = resources.getResourceItem(ResourceType.STRING, "title_template_step");
assertNotNull(labelList);
assertEquals(1, labelList.size());
label = labelList.get(0);
resourceValue = label.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Step ${step_number}: Llorem Ipsum", resourceValue.getValue());
resetScanCounter();
// Try a second edit
long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int textOffset2 = document.getText().indexOf("Llorem");
document.insertString(textOffset + 1, "l");
documentManager.commitDocument(document);
}
});
assertTrue(generation2 < resources.getModificationCount());
List<ResourceItem> labelList2 = resources.getResourceItem(ResourceType.STRING, "title_template_step");
assertNotNull(labelList2);
assertEquals(1, labelList2.size());
final ResourceItem label2 = labelList2.get(0);
resourceValue = label2.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Step ${step_number}: Lllorem Ipsum", resourceValue.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 testRemoveStyleItem.
public void testRemoveStyleItem() 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() {
String item = ("<item name=\"android:textColor\">#008</item>");
int offset = document.getText().indexOf(item);
assertTrue(offset > 0);
document.deleteString(offset, offset + item.length());
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");
background = srv.getItem("background", true);
assertNotNull(background);
assertEquals("@android:color/transparent", background.getValue());
resetScanCounter();
// Try second edit, which is incremental.
long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String item = ("<item name=\"android:background\">@android:color/transparent</item>");
int offset = document.getText().indexOf(item);
assertTrue(offset > 0);
document.deleteString(offset, offset + item.length());
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);
assertEmpty(srv.getNames());
// 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 testEditStyleItemName.
public void testEditStyleItemName() 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("android:actionBarStyle");
document.insertString(offset + 8, "n");
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("nactionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/DarkActionBar", 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("android:nactionBarStyle");
document.insertString(offset + 8, "i");
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("inactionBarStyle", true);
assertNotNull(actionBarStyle);
assertEquals("@style/DarkActionBar", 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 ResourceFolderRepositoryTest method testRemoveIntegerArrayItemElements.
public void testRemoveIntegerArrayItemElements() 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, "integers"));
ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
ResourceValue resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("10", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
ArrayResourceValue arv = (ArrayResourceValue) resourceValue;
assertEquals(2, arv.getElementCount());
assertEquals("10", arv.getElement(0));
assertEquals("20", arv.getElement(1));
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String item = "<item>10</item>";
int offset = document.getText().indexOf(item);
document.deleteString(offset, offset + item.length());
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(1, arv.getElementCount());
assertEquals("20", resourceValue.getValue());
resetScanCounter();
// Try a second edit.
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String item = "<item>20</item>";
int offset = document.getText().indexOf(item);
document.deleteString(offset, offset + item.length());
documentManager.commitDocument(document);
}
});
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(0, arv.getElementCount());
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
Aggregations