use of com.android.tools.idea.res.ResourceNotificationManager.ResourceVersion in project android by JetBrains.
the class ResourceNotificationManagerTest method test.
public void test() {
@Language("XML") String xml;
// Setup sample project: a strings file, and a couple of layout file
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<FrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + " android:layout_width=\"match_parent\"\n" + " android:layout_height=\"match_parent\">\n" + " <!-- My comment -->\n" + " <TextView " + " android:layout_width=\"match_parent\"\n" + " android:layout_height=\"match_parent\"\n" + " android:text=\"@string/hello\" />\n" + "</FrameLayout>";
final XmlFile layout1 = (XmlFile) myFixture.addFileToProject("res/layout/my_layout1.xml", xml);
@SuppressWarnings("ConstantConditions") VirtualFile resourceDir = layout1.getParent().getParent().getVirtualFile();
assertNotNull(resourceDir);
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + " android:layout_width=\"match_parent\"\n" + " android:layout_height=\"match_parent\" />\n";
final XmlFile layout2 = (XmlFile) myFixture.addFileToProject("res/layout/my_layout2.xml", xml);
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <string name=\"hello\">Hello</string>\n" + "\n" + " <!-- Base application theme. -->\n" + " <style name=\"AppTheme\" parent=\"Theme.AppCompat.Light.DarkActionBar\">\n" + " <!-- Customize your theme here. -->\n" + " <item name=\"android:colorBackground\">#ff0000</item>\n" + " </style>" + "</resources>";
final XmlFile values1 = (XmlFile) myFixture.addFileToProject("res/values/my_values1.xml", xml);
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " \n" + "</resources>";
myFixture.addFileToProject("res/values/colors.xml", xml);
final Configuration configuration1 = myFacet.getConfigurationManager().getConfiguration(layout1.getVirtualFile());
final ResourceNotificationManager manager = ResourceNotificationManager.getInstance(getProject());
// Listener 1: Listens for changes in layout 1
final Ref<Boolean> called1 = new Ref<>(false);
final Ref<Set<Reason>> calledValue1 = new Ref<>();
ResourceChangeListener listener1 = new ResourceChangeListener() {
@Override
public void resourcesChanged(@NotNull Set<Reason> reason) {
called1.set(true);
calledValue1.set(reason);
}
};
// Listener 2: Only listens for general changes in the module
final Ref<Boolean> called2 = new Ref<>(false);
final Ref<Set<Reason>> calledValue2 = new Ref<>();
ResourceChangeListener listener2 = new ResourceChangeListener() {
@Override
public void resourcesChanged(@NotNull Set<Reason> reason) {
called2.set(true);
calledValue2.set(reason);
}
};
manager.addListener(listener1, myFacet, layout1, configuration1);
manager.addListener(listener2, myFacet, null, null);
// Make sure that when we're modifying multiple files, with complicated
// edits (that trigger full file rescans), we handle that scenario correctly.
clear(called1, calledValue1, called2, calledValue2);
// There's actually some special optimizations done via PsiResourceItem#recomputeValue
// to only mark the resource repository changed if the value has actually been looked
// up. This allows us to not recompute layout if you're editing some string that
// hasn't actually been looked up and rendered in a layout. In order to make sure
// that that optimization doesn't kick in here, we need to look up the value of
// the resource item first:
//noinspection ConstantConditions
assertEquals("#ff0000", configuration1.getResourceResolver().getStyle("AppTheme", false).getItem("colorBackground", true).getValue());
AndroidResourceUtil.createValueResource(getProject(), resourceDir, "color2", ResourceType.COLOR, "colors.xml", Collections.singletonList("values"), "#fa2395");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
clear(called1, calledValue1, called2, calledValue2);
@SuppressWarnings("ConstantConditions") final XmlTag tag = values1.getDocument().getRootTag().getSubTags()[1].getSubTags()[0];
assertEquals("item", tag.getName());
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
tag.getValue().setEscapedText("@color/color2");
}
});
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
// First check: Modify the layout by changing @string/hello to @string/hello_world
// and verify that our listeners are called.
ResourceVersion version1 = manager.getCurrentVersion(myFacet, layout1, configuration1);
addText(layout1, "@string/hello^", "_world");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.EDIT);
ResourceVersion version2 = manager.getCurrentVersion(myFacet, layout1, configuration1);
assertFalse(version1.toString(), version1.equals(version2));
// Next check: Modify a <string> value definition in a values file
// and check that those changes are flagged too
clear(called1, calledValue1, called2, calledValue2);
ResourceVersion version3 = manager.getCurrentVersion(myFacet, layout1, configuration1);
addText(values1, "name=\"hello^\"", "_world");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
ResourceVersion version4 = manager.getCurrentVersion(myFacet, layout1, configuration1);
assertFalse(version4.toString(), version3.equals(version4));
// Next check: Modify content in a comment and verify that no changes are fired
clear(called1, calledValue1, called2, calledValue2);
addText(layout1, "My ^comment", "new ");
ensureNotCalled(called1, called2);
// Check that editing text in a layout file has no effect
clear(called1, calledValue1, called2, calledValue2);
addText(layout1, " ^ <TextView", "abc");
ensureNotCalled(called1, called2);
// Make sure that's true for replacements too
replaceText(layout1, "^abc", "abc".length(), "def");
ensureNotCalled(called1, called2);
// ...and for deletions
removeText(layout1, "^def", "def".length());
ensureNotCalled(called1, called2);
// Check that editing text in a *values file* -does- have an effect
// Read the value first to ensure that we trigger it as a read (see comment above for previous
// resource resolver lookup)
//noinspection ConstantConditions
assertEquals("Hello", configuration1.getResourceResolver().findResValue("@string/hello_world", false).getValue());
addText(values1, "Hello^</string>", " World");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
// Check that recreating AppResourceRepository object doesn't affect the ResourceNotificationManager
clear(called1, calledValue1, called2, calledValue2);
myFacet.refreshResources();
AndroidResourceUtil.createValueResource(getProject(), resourceDir, "color4", ResourceType.COLOR, "colors.xml", Collections.singletonList("values"), "#ff2300");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
// Finally check that once we remove the listeners there are no more notifications
manager.removeListener(listener1, myFacet, layout1, configuration1);
manager.removeListener(listener2, myFacet, layout2, configuration1);
clear(called1, calledValue1, called2, calledValue2);
addText(layout1, "@string/hello_world^", "2");
ensureNotCalled(called1, called2);
// TODO: Check that editing a partial URL doesn't re-render
// Check module dependency triggers!
// TODO: Test that remove and replace editing also works as expected
}
use of com.android.tools.idea.res.ResourceNotificationManager.ResourceVersion in project android by JetBrains.
the class NlModel method activate.
/**
* Notify model that it's active. A model is active by default.
*/
public void activate() {
if (!myActive) {
myActive = true;
myConfiguration.addListener(myConfigurationListener);
ResourceNotificationManager manager = ResourceNotificationManager.getInstance(myFile.getProject());
ResourceVersion version = manager.addListener(this, myFacet, myFile, myConfiguration);
// If the resources have changed or the configuration has been modified, request a model update
if (!version.equals(myRenderedVersion) || (myConfiguration.getModificationCount() != myConfigurationModificationCount)) {
String theme = myConfiguration.getTheme();
if (theme != null && !theme.startsWith(ANDROID_STYLE_RESOURCE_PREFIX) && !myProjectResourceRepository.hasResourceItem(theme)) {
myConfiguration.setTheme(myConfiguration.getConfigurationManager().computePreferredTheme(myConfiguration));
}
requestModelUpdate();
myModelVersion.myResourceVersion.incrementAndGet();
}
}
}
Aggregations