javafx: check fx:id inside component hierarchy only; test reworked

(cherry picked from commit 9aa0382)
This commit is contained in:
anna
2013-03-21 10:40:38 +01:00
parent e8822476d7
commit 84fb6f7ac0
8 changed files with 102 additions and 42 deletions
@@ -15,14 +15,18 @@
*/
package org.jetbrains.plugins.javaFX.fxml;
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase;
import com.intellij.codeInsight.daemon.impl.analysis.XmlPathReferenceInspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.htmlInspections.RequiredAttributesInspection;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.psi.*;
import com.intellij.psi.search.ProjectScope;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
@@ -30,17 +34,19 @@ import org.jetbrains.annotations.NotNull;
* @author anna
* @since 10.01.2013
*/
public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
@Override
protected void setUpModule() {
super.setUpModule();
//noinspection SpellCheckingInspection
PsiTestUtil.addLibrary(getModule(), "javafx", PluginPathManager.getPluginHomePath("javaFX") + "/testData", "jfxrt.jar");
}
public class JavaFXHighlightingTest extends LightCodeInsightFixtureTestCase {
public static final DefaultLightProjectDescriptor JAVA_FX_DESCRIPTOR = new DefaultLightProjectDescriptor() {
@Override
public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) {
PsiTestUtil.addLibrary(module, model, "javafx", PluginPathManager.getPluginHomePath("javaFX") + "/testData", "jfxrt.jar");
super.configureModule(module, model, contentEntry);
}
};
@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[] {new XmlPathReferenceInspection(), new RequiredAttributesInspection() };
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_FX_DESCRIPTOR;
}
public void testLoginForm() throws Exception {
@@ -80,8 +86,9 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
}
public void testImageIcon() throws Exception {
configureByFiles(null, getTestName(true) + ".fxml", "appIcon.png");
doDoTest(false, false);
myFixture.copyFileToProject("appIcon.png");
myFixture.configureByFiles(getTestName(true) + ".fxml");
myFixture.testHighlighting(false, false, false, getTestName(true) + ".fxml");
}
public void testControllerIdRef() throws Exception {
@@ -89,23 +96,21 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
}
public void testPackageLocalController() throws Exception {
configureByFiles(null, getTestName(true) + ".fxml", getTestName(false) + ".java");
doDoTest(false, false);
doTest(getTestName(false) + ".java");
}
public void testNoParamsHandler() throws Exception {
configureByFiles(null, getTestName(true) + ".fxml", getTestName(false) + ".java");
doDoTest(false, false);
doTest(getTestName(false) + ".java");
}
private void doTestIdController() throws Exception {
final String controllerClassName = getTestName(false) + "Controller";
configureByFiles(null, getTestName(true) + ".fxml", controllerClassName + ".java");
final PsiClass controllerClass = findClass(controllerClassName);
myFixture.configureByFiles(getTestName(true) + ".fxml", controllerClassName + ".java");
final PsiClass controllerClass = myFixture.findClass(controllerClassName);
assertNotNull(controllerClass);
assertTrue(controllerClass.getFields().length > 0);
final int offset = myEditor.getCaretModel().getOffset();
final PsiReference reference = myFile.findReferenceAt(offset);
final int offset = myFixture.getCaretOffset();
final PsiReference reference = myFixture.getFile().findReferenceAt(offset);
assertNotNull(reference);
assertEquals(controllerClass.getFields()[0], reference.resolve());
}
@@ -123,17 +128,17 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
}
public void testCustomComponentFieldsWithSameProperties() throws Exception {
configureByFiles(null, "custom/" + getTestName(true) + ".fxml", "custom/" + getTestName(false)+ ".java");
doDoTest(false, false);
doTest("custom/" + getTestName(true) + ".fxml", "custom/" + getTestName(false) + ".java");
}
public void testCustomComponent_Fields() throws Exception {
configureByFiles(null, "custom/" + getTestName(true) + ".fxml", "custom/_CustomVBox.java");
doDoTest(false, false);
doTest("custom/" + getTestName(true) + ".fxml", "custom/_CustomVBox.java");
}
public void testInjectedController() throws Exception {
doTestNavigation("MyController", "label", "injected/" + getTestName(true) + ".fxml", "injected/FooVBox.java", "injected/MyController.java");
myFixture.copyFileToProject("injected/MyController.java");
myFixture.copyFileToProject("injected/FooVBox.java");
doTestNavigation("injected.MyController", "label", "injected/" + getTestName(true) + ".fxml");
}
public void testNamedColor() throws Exception {
@@ -146,14 +151,14 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
private void doTestNavigation(String resultClassName, String resultFieldName, String... additionalPaths) throws Exception {
if (additionalPaths.length == 0) {
configureByFiles(null, getTestName(true) + ".fxml");
myFixture.configureByFiles(getTestName(true) + ".fxml");
} else {
configureByFiles(null, additionalPaths);
myFixture.configureByFiles(additionalPaths);
}
final int offset = myEditor.getCaretModel().getOffset();
final PsiReference reference = myFile.findReferenceAt(offset);
final int offset = myFixture.getCaretOffset();
final PsiReference reference = myFixture.getFile().findReferenceAt(offset);
assertNotNull(reference);
final PsiClass resultClass = myJavaFacade.findClass(resultClassName, ProjectScope.getAllScope(getProject()));
final PsiClass resultClass = myFixture.getJavaFacade().findClass(resultClassName, GlobalSearchScope.allScope(getProject()));
assertNotNull("Class " + resultClassName + " not found", resultClass);
final PsiField resultField = resultClass.findFieldByName(resultFieldName, false);
assertNotNull(resultField);
@@ -161,9 +166,9 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
}
public void testNavigationFromMainToFxml() throws Exception {
configureByFiles(null, getTestName(false) + ".java", getTestName(true) + ".fxml");
final int offset = myEditor.getCaretModel().getOffset();
final PsiReference reference = myFile.findReferenceAt(offset);
myFixture.configureByFiles(getTestName(false) + ".java", getTestName(true) + ".fxml");
final int offset = myFixture.getCaretOffset();
final PsiReference reference = myFixture.getFile().findReferenceAt(offset);
assertNotNull(reference);
final PsiElement resolve = reference.resolve();
assertNotNull(resolve);
@@ -204,9 +209,20 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
doTest();
}
public void testIdOutOfHierarchy() throws Exception {
doTest("btn.fxml", "MyController.java");
}
public void testIncludeBtn() throws Exception {
configureByFiles(null, getTestName(true) + ".fxml", "btn.fxml");
doDoTest(false, false);
doTest("btn.fxml");
}
public void testWrongBindingType() throws Exception {
doTest(getTestName(false) + ".java");
}
public void testAllowIncludeTagInsideDefine() throws Exception {
doTest("btn.fxml");
}
public void testValueOfAcceptance() throws Exception {
@@ -238,8 +254,17 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
}
public void testScriptSource() throws Exception {
configureByFiles(null, getTestName(true) + ".fxml", "s1.js");
doDoTest(false, false);
doTest("s1.js");
}
private void doTest(String additionalPath) {
myFixture.configureByFiles(getTestName(true) + ".fxml", additionalPath);
myFixture.testHighlighting(false, false, false, getTestName(true) + ".fxml");
}
private void doTest(String... paths) {
myFixture.configureByFiles(paths);
myFixture.testHighlighting(false, false, false, paths[0]);
}
public void testExpressionBinding() throws Exception {
@@ -267,7 +292,13 @@ public class JavaFXHighlightingTest extends DaemonAnalyzerTestCase {
}
private void doTest() throws Exception {
doTest(false, false, getTestName(true) + ".fxml");
myFixture.testHighlighting(false, false, false, getTestName(true) + ".fxml");
}
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new XmlPathReferenceInspection(), new RequiredAttributesInspection());
}
@NotNull
@@ -486,6 +486,17 @@ public class JavaFxPsiUtil {
return "Unable to coerce " + HighlightUtil.formatClass(aClass)+ " to " + qualifiedName;
}
public static boolean isOutOfHierarchy(final XmlAttributeValue element) {
XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
while (tag != null) {
if (FxmlConstants.FX_DEFINE.equals(tag.getName())) {
return true;
}
tag = tag.getParentTag();
}
return false;
}
private static class JavaFxControllerCachedValueProvider implements CachedValueProvider<PsiClass> {
private final Project myProject;
private final PsiFile myContainingFile;
@@ -81,8 +81,10 @@ class JavaFxFieldIdReferenceProvider extends JavaFxControllerBasedReferenceProvi
if (myAClass != null) {
final XmlFile xmlFile = (XmlFile)myXmlAttributeValue.getContainingFile();
final XmlTag rootTag = xmlFile.getRootTag();
if (rootTag != null && !FxmlConstants.FX_ROOT.equals(rootTag.getName())) {
return null;
if (rootTag != null) {
if (!JavaFxPsiUtil.isOutOfHierarchy(myXmlAttributeValue) && !FxmlConstants.FX_ROOT.equals(rootTag.getName())) {
return null;
}
}
}
return myXmlAttributeValue;
@@ -0,0 +1 @@
public class MyController {}
@@ -1,2 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<Button/>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.image.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="MyController">
<fx:define>
<fx:include fx:id="foo" source="btn.fxml"/>
</fx:define>
</AnchorPane>
@@ -1,3 +1,5 @@
package injected;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;
@@ -1,3 +1,5 @@
package injected;
import javafx.scene.control.Label;
public class MyController {