|
| 1 | +/* |
| 2 | + * Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 | + * Use of this source code is governed by a BSD-style license that can be |
| 4 | + * found in the LICENSE file. |
| 5 | + */ |
| 6 | +package io.flutter.testing; |
| 7 | + |
| 8 | +import com.intellij.openapi.Disposable; |
| 9 | +import com.intellij.openapi.application.ApplicationManager; |
| 10 | +import com.intellij.openapi.application.PathManager; |
| 11 | +import com.intellij.openapi.module.Module; |
| 12 | +import com.intellij.openapi.roots.impl.libraries.ApplicationLibraryTable; |
| 13 | +import com.intellij.openapi.roots.libraries.Library; |
| 14 | +import com.intellij.openapi.util.Disposer; |
| 15 | +import com.intellij.openapi.util.io.FileUtil; |
| 16 | +import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess; |
| 17 | +import com.intellij.util.PathUtil; |
| 18 | +import io.flutter.dart.DartPlugin; |
| 19 | +import io.flutter.sdk.FlutterSdk; |
| 20 | +import io.flutter.sdk.FlutterSdkUtil; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | +import org.junit.Assert; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.util.Collections; |
| 26 | + |
| 27 | +public class FlutterTestUtils { |
| 28 | + public static final String BASE_TEST_DATA_PATH = findTestDataPath(); |
| 29 | + public static final String SDK_HOME_PATH = BASE_TEST_DATA_PATH + "/sdk"; |
| 30 | + |
| 31 | + public static void configureFlutterSdk(@NotNull final Module module, @NotNull final Disposable disposable, final boolean realSdk) { |
| 32 | + final String sdkHome; |
| 33 | + if (realSdk) { |
| 34 | + sdkHome = System.getProperty("flutter.sdk"); |
| 35 | + if (sdkHome == null) { |
| 36 | + Assert.fail("To run tests that use the flutter tools you need to add '-Dflutter.sdk=[real SDK home]' to the VM Options field of " + |
| 37 | + "the corresponding JUnit run configuration (Run | Edit Configurations)"); |
| 38 | + } |
| 39 | + if (!FlutterSdkUtil.isFlutterSdkHome(sdkHome)) { |
| 40 | + Assert.fail("Incorrect path to the Flutter SDK (" + sdkHome + ") is set as '-Dflutter.sdk' VM option of " + |
| 41 | + "the corresponding JUnit run configuration (Run | Edit Configurations)"); |
| 42 | + } |
| 43 | + VfsRootAccess.allowRootAccess(disposable, sdkHome); |
| 44 | + } |
| 45 | + else { |
| 46 | + sdkHome = SDK_HOME_PATH; |
| 47 | + } |
| 48 | + |
| 49 | + ApplicationManager.getApplication().runWriteAction(() -> { |
| 50 | + FlutterSdkUtil.setFlutterSdkPath(module.getProject(), sdkHome); |
| 51 | + DartPlugin.enableDartSdk(module); |
| 52 | + }); |
| 53 | + |
| 54 | + Disposer.register(disposable, () -> ApplicationManager.getApplication().runWriteAction(() -> { |
| 55 | + if (!module.isDisposed()) { |
| 56 | + DartPlugin.disableDartSdk(Collections.singletonList(module)); |
| 57 | + } |
| 58 | + |
| 59 | + final ApplicationLibraryTable libraryTable = ApplicationLibraryTable.getApplicationTable(); |
| 60 | + final Library library = libraryTable.getLibraryByName(FlutterSdk.FLUTTER_SDK_GLOBAL_LIB_NAME); |
| 61 | + if (library != null) { |
| 62 | + libraryTable.removeLibrary(library); |
| 63 | + } |
| 64 | + })); |
| 65 | + } |
| 66 | + |
| 67 | + private static String findTestDataPath() { |
| 68 | + if (new File(PathManager.getHomePath() + "/contrib").isDirectory()) { |
| 69 | + // started from IntelliJ IDEA Ultimate project |
| 70 | + return FileUtil.toSystemIndependentName(PathManager.getHomePath() + "/contrib/flutter-intellij/testData"); |
| 71 | + } |
| 72 | + |
| 73 | + final File f = new File("testData"); |
| 74 | + if (f.isDirectory()) { |
| 75 | + // started from flutter plugin project |
| 76 | + return FileUtil.toSystemIndependentName(f.getAbsolutePath()); |
| 77 | + } |
| 78 | + |
| 79 | + final String parentPath = PathUtil.getParentPath(PathManager.getHomePath()); |
| 80 | + |
| 81 | + if (new File(parentPath + "/intellij-plugins").isDirectory()) { |
| 82 | + // started from IntelliJ IDEA Community Edition + flutter plugin project |
| 83 | + return FileUtil.toSystemIndependentName(parentPath + "/intellij-plugins/flutter-intellij/testData"); |
| 84 | + } |
| 85 | + |
| 86 | + if (new File(parentPath + "/contrib").isDirectory()) { |
| 87 | + // started from IntelliJ IDEA Community + flutter plugin project |
| 88 | + return FileUtil.toSystemIndependentName(parentPath + "/contrib/flutter-intellij/testData"); |
| 89 | + } |
| 90 | + |
| 91 | + return ""; |
| 92 | + } |
| 93 | +} |
0 commit comments