fix android kotlin test framework
- fix android jars not indexed by java indexer - fix goto test
This commit is contained in:
committed by
Yan Zhulanow
parent
1912b2b0ec
commit
04a905c35c
@@ -31,14 +31,14 @@ class IDEAndroidUIXmlProcessor(project: Project) : AndroidUIXmlProcessor(project
|
||||
override var androidAppPackage: String = ""
|
||||
get() = resourceManager.readManifest()._package
|
||||
|
||||
override val resourceManager: AndroidResourceManager = IDEAndroidResourceManager(project, searchPath)
|
||||
override val resourceManager: IDEAndroidResourceManager = IDEAndroidResourceManager(project, searchPath)
|
||||
|
||||
override fun parseSingleFileImpl(file: PsiFile): String {
|
||||
val ids: MutableCollection<AndroidWidget> = ArrayList()
|
||||
if (!ApplicationManager.getApplication()!!.isUnitTestMode()) (resourceManager as IDEAndroidResourceManager).resetAttributeCache()
|
||||
resourceManager.resetAttributeCache()
|
||||
file.accept(AndroidXmlVisitor(resourceManager, { id, wClass, valueElement ->
|
||||
ids.add(AndroidWidget(id, wClass))
|
||||
if (!ApplicationManager.getApplication()!!.isUnitTestMode()) (resourceManager as IDEAndroidResourceManager).addMapping(id, valueElement)
|
||||
resourceManager.addMapping(id, valueElement)
|
||||
}))
|
||||
return produceKotlinProperties(KotlinStringWriter(), ids).toString()
|
||||
}
|
||||
|
||||
@@ -6,8 +6,7 @@ import java.io.File
|
||||
|
||||
|
||||
public class MyActivity : Activity() {
|
||||
override fun onCreate(savedInstanceState: <caret>Bundle?) {}
|
||||
//val button = login
|
||||
val shit = File("")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {}
|
||||
val button = login<caret>
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,8 @@ import com.android.SdkConstants
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase
|
||||
import com.intellij.codeInsight.navigation.actions.GotoDeclarationAction
|
||||
import kotlin.test.fail
|
||||
import kotlin.test.assertEquals
|
||||
import org.jetbrains.jet.lang.psi.JetProperty
|
||||
|
||||
public abstract class AbstractAndroidGotoTest : KotlinAndroidTestCase() {
|
||||
|
||||
@@ -41,11 +43,13 @@ public abstract class AbstractAndroidGotoTest : KotlinAndroidTestCase() {
|
||||
override fun requireRecentSdk() = true
|
||||
|
||||
public fun doTest(path: String) {
|
||||
myFixture!!.copyDirectoryToProject(getResDir()!!, "res")
|
||||
myFixture!!.configureByFile(path + getTestName(true) + ".kt");
|
||||
val f = myFixture!!
|
||||
f.copyDirectoryToProject(getResDir()!!, "res")
|
||||
f.configureByFile(path + getTestName(true) + ".kt");
|
||||
|
||||
val resolved = GotoDeclarationAction.findTargetElement(f.getProject(), f.getEditor(), f.getCaretOffset())
|
||||
if (f.getElementAtCaret() !is JetProperty) fail("resolved element must be a property, not a ${f.getElementAtCaret().javaClass}")
|
||||
assertEquals("\"@+id/${(f.getElementAtCaret() as JetProperty).getName()}\"", resolved?.getText())
|
||||
|
||||
val asf = GotoDeclarationAction.findTargetElement(myFixture!!.getProject(), myFixture!!.getEditor(), myFixture!!.getCaretOffset())
|
||||
myFixture!!.getEditor().toString()
|
||||
fail("this test doesn't work yet")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,53 +90,54 @@ public abstract class KotlinAndroidTestCase extends KotlinAndroidTestCaseBase {
|
||||
return "res/";
|
||||
}
|
||||
|
||||
class LightAndroidProjectDescriptor extends JetLightProjectDescriptor {
|
||||
@Override
|
||||
public Sdk getSdk() {
|
||||
androidSdk = createAndroidSdk(getTestSdkPath(), getPlatformDir());
|
||||
return androidSdk;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureModule(
|
||||
Module module, ModifiableRootModel model, ContentEntry contentEntry
|
||||
) {
|
||||
NewLibraryEditor editor = new NewLibraryEditor();
|
||||
editor.setName("android.jar");
|
||||
editor.addRoot(androidSdk.getSdkModificator().getRoots(OrderRootType.CLASSES)[0], OrderRootType.CLASSES);
|
||||
|
||||
ConfigLibraryUtil.addLibrary(editor, model);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
// this will throw an exception if we don't have a full Android SDK, so we need to do this first thing before any other setup
|
||||
String sdkPath = getTestSdkPath();
|
||||
|
||||
|
||||
IdeaTestFixtureFactory factory = IdeaTestFixtureFactory.getFixtureFactory();
|
||||
//TestFixtureBuilder<IdeaProjectTestFixture> fixtureBuilder = factory.createLightFixtureBuilder(JetLightProjectDescriptor.INSTANCE);
|
||||
TestFixtureBuilder<IdeaProjectTestFixture> fixtureBuilder = factory.createLightFixtureBuilder(new LightAndroidProjectDescriptor());
|
||||
//final TestFixtureBuilder<IdeaProjectTestFixture> fixtureBuilder = factory.createLightFixtureBuilder(getName());
|
||||
final IdeaProjectTestFixture fixture = fixtureBuilder.getFixture();
|
||||
myFixture = IdeaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(fixture, new LightTempDirTestFixtureImpl(true));
|
||||
|
||||
final TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder =
|
||||
IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getName());
|
||||
myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(projectBuilder.getFixture());
|
||||
final JavaModuleFixtureBuilder moduleFixtureBuilder = projectBuilder.addModule(JavaModuleFixtureBuilder.class);
|
||||
final String dirPath = myFixture.getTempDirPath() + getContentRootPath();
|
||||
final File dir = new File(dirPath);
|
||||
|
||||
if (!dir.exists()) {
|
||||
assertTrue(dir.mkdirs());
|
||||
}
|
||||
tuneModule(moduleFixtureBuilder, dirPath);
|
||||
|
||||
final ArrayList<MyAdditionalModuleData> modules = new ArrayList<MyAdditionalModuleData>();
|
||||
configureAdditionalModules(projectBuilder, modules);
|
||||
|
||||
myFixture.setUp();
|
||||
myFixture.setTestDataPath(getTestDataPath());
|
||||
myModule = moduleFixtureBuilder.getFixture().getModule();
|
||||
|
||||
myModule = myFixture.getModule();
|
||||
|
||||
// Must be done before addAndroidFacet, and must always be done, even if !myCreateManifest.
|
||||
// We will delete it at the end of setUp; this is needed when unit tests want to rewrite
|
||||
// the manifest on their own.
|
||||
createManifest();
|
||||
|
||||
androidSdk = createAndroidSdk(getTestSdkPath(), getPlatformDir());
|
||||
myFacet = addAndroidFacet(myModule, sdkPath, getPlatformDir(), isToAddSdk());
|
||||
myFixture.copyDirectoryToProject(getResDir(), "res");
|
||||
|
||||
myAdditionalModules = new ArrayList<Module>();
|
||||
|
||||
for (MyAdditionalModuleData data : modules) {
|
||||
final Module additionalModule = data.myModuleFixtureBuilder.getFixture().getModule();
|
||||
myAdditionalModules.add(additionalModule);
|
||||
final AndroidFacet facet = addAndroidFacet(additionalModule, sdkPath, getPlatformDir());
|
||||
facet.setLibraryProject(data.myLibrary);
|
||||
final String rootPath = getContentRootPath(data.myDirName);
|
||||
myFixture.copyDirectoryToProject("res", rootPath + "/res");
|
||||
myFixture.copyFileToProject(SdkConstants.FN_ANDROID_MANIFEST_XML,
|
||||
rootPath + '/' + SdkConstants.FN_ANDROID_MANIFEST_XML);
|
||||
ModuleRootModificationUtil.addDependency(myModule, additionalModule);
|
||||
}
|
||||
|
||||
if (!myCreateManifest) {
|
||||
deleteManifest();
|
||||
|
||||
@@ -31,6 +31,7 @@ import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.vfs.JarFileSystem;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.xml.XmlAttributeValue;
|
||||
@@ -57,6 +58,7 @@ public abstract class KotlinAndroidTestCaseBase extends UsefulTestCase {
|
||||
protected CodeInsightTestFixture myFixture;
|
||||
|
||||
protected Sdk androidSdk;
|
||||
protected VirtualFile androidJar;
|
||||
|
||||
protected KotlinAndroidTestCaseBase() {
|
||||
IdeaTestCase.initPlatformPrefix();
|
||||
@@ -148,16 +150,15 @@ public abstract class KotlinAndroidTestCaseBase extends UsefulTestCase {
|
||||
SdkModificator sdkModificator = sdk.getSdkModificator();
|
||||
sdkModificator.setHomePath(sdkPath);
|
||||
|
||||
VirtualFile androidJar;
|
||||
if (platformDir.equals(getDefaultPlatformDir())) {
|
||||
// Compatibility: the unit tests were using android.jar outside the sdk1.5 install;
|
||||
// we need to use that one, rather than the real one in sdk1.5, in order for the
|
||||
// tests to pass. Longer term, we should switch the unit tests over to all using
|
||||
// a valid SDK.
|
||||
String androidJarPath = sdkPath + "/../android.jar!/";
|
||||
androidJar = JarFileSystem.getInstance().findFileByPath(androidJarPath);
|
||||
androidJar = VirtualFileManager.getInstance().findFileByUrl("jar://" + androidJarPath);
|
||||
} else {
|
||||
androidJar = LocalFileSystem.getInstance().findFileByPath(sdkPath + "/platforms/" + platformDir + "/android.jar");
|
||||
androidJar = VirtualFileManager.getInstance().findFileByUrl("jar://" + sdkPath + "/platforms/" + platformDir + "/android.jar!/");
|
||||
}
|
||||
sdkModificator.addRoot(androidJar, OrderRootType.CLASSES);
|
||||
|
||||
@@ -187,11 +188,6 @@ public abstract class KotlinAndroidTestCaseBase extends UsefulTestCase {
|
||||
}
|
||||
assertNotNull(target);
|
||||
data.setBuildTarget(target);
|
||||
data.setJavaSdk(PluginTestCaseBase.fullJdk());
|
||||
// Srsly, WTF? Why do i have to manually add jdk classes to android sdk roots? Even with setJavaSdk(PluginTestCaseBase.fullJdk())
|
||||
for (VirtualFile f : PluginTestCaseBase.fullJdk().getRootProvider().getFiles(OrderRootType.CLASSES)) {
|
||||
sdkModificator.addRoot(f, OrderRootType.CLASSES);
|
||||
}
|
||||
sdkModificator.setSdkAdditionalData(data);
|
||||
sdkModificator.commitChanges();
|
||||
return sdk;
|
||||
|
||||
Reference in New Issue
Block a user