Skip to content

Commit

Permalink
chore: add tests for the pinned files feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jansorg committed Sep 23, 2024
1 parent 96f4e30 commit ab1b0d0
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private boolean hasSelectedFiles(@NotNull AnActionEvent e) {

private @Nullable NavieEditor findActiveNavieEditor(@NotNull AnActionEvent e) {
var editorManager = FileEditorManager.getInstance(Objects.requireNonNull(e.getProject()));
var editor = (editorManager).getSelectedEditor();
var editor = editorManager.getSelectedEditor();

// If invoked with the context menu of an editor tab, then "editor" equals the clicked tab's editor and not the
// visible Navie editor tab. We're attempting a fallback in such a case.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ public static boolean isWebviewTextInputBroken() {
*/
@SuppressWarnings("removal")
public static void showWebviewTextInputBrokenMessage(@NotNull Project project, boolean forNavie) {
// don't show in our unit tests because there's no user interaction
if (ApplicationManager.getApplication().isUnitTestMode()) {
return;
}

var properties = PropertiesComponent.getInstance();
var hideMessagePropertyKey = forNavie ? "appmap.navie.hideIsBrokenMessage" : "appmap.signin.hideIsBrokenMessage";
if (properties.getBoolean(hideMessagePropertyKey, false)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package appland.webviews;

import com.google.common.base.Predicates;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorPolicy;
Expand Down Expand Up @@ -106,6 +107,10 @@ public boolean isWebViewFile(@NotNull VirtualFile file) {
public LightVirtualFile createVirtualFile(@NotNull String title) {
var file = new LightVirtualFile(title);
WEBVIEW_EDITOR_KEY.set(file, webviewTypeId);
// TestEditorManagerImpl is used in tests and only uses custom editor provider is set via KEY :(
if (ApplicationManager.getApplication().isUnitTestMode()) {
FileEditorProvider.KEY.set(file, this);
}
return file;
}

Expand Down
69 changes: 69 additions & 0 deletions plugin-core/src/test/java/appland/AppMapBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import appland.config.AppMapConfigFile;
import appland.files.AppMapFiles;
import appland.problemsView.TestFindingsManager;
import appland.rpcService.AppLandJsonRpcListener;
import appland.rpcService.AppLandJsonRpcListenerAdapter;
import appland.rpcService.AppLandJsonRpcService;
import appland.settings.AppMapApplicationSettings;
import appland.settings.AppMapApplicationSettingsService;
Expand Down Expand Up @@ -33,6 +35,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public abstract class AppMapBaseTest extends LightPlatformCodeInsightFixture4TestCase {
Expand Down Expand Up @@ -186,6 +189,72 @@ protected void waitUntilIndexesAreReady() {
IndexTestUtils.waitUntilIndexesAreReady(getProject());
}

protected void waitForJsonRpcServer() {
var service = AppLandJsonRpcService.getInstance(getProject());
if (service.isServerRunning()) {
return;
}

var latch = createWaitForJsonRpcServerRestartCondition(true);
ApplicationManager.getApplication().executeOnPooledThread(service::startServer);
try {
assertTrue("The AppMap JSON-RPC server must launch", latch.await(30, TimeUnit.SECONDS));
} catch (InterruptedException e) {
addSuppressedException(e);
}
}

protected void waitForJsonRpcServerPort() {
var service = AppLandJsonRpcService.getInstance(getProject());
if (service.isServerRunning()) {
return;
}

var latch = createWaitForJsonRpcServerPortCondition();
ApplicationManager.getApplication().executeOnPooledThread(service::startServer);
try {
assertTrue("The AppMap JSON-RPC server must launch", latch.await(30, TimeUnit.SECONDS));
} catch (InterruptedException e) {
addSuppressedException(e);
}
}

protected @NotNull CountDownLatch createWaitForJsonRpcServerRestartCondition(boolean allowStart) {
var latch = new CountDownLatch(1);
getProject().getMessageBus()
.connect(getTestRootDisposable())
.subscribe(AppLandJsonRpcListener.TOPIC, new AppLandJsonRpcListenerAdapter() {
@Override
public void serverStarted() {
if (allowStart) {
latch.countDown();
}
}

@Override
public void serverRestarted() {
latch.countDown();
}
});
return latch;
}

protected @NotNull CountDownLatch createWaitForJsonRpcServerPortCondition() {
var latch = new CountDownLatch(1);
getProject().getMessageBus()
.connect(getTestRootDisposable())
.subscribe(AppLandJsonRpcListener.TOPIC, new AppLandJsonRpcListenerAdapter() {
@Override
public void serverConfigurationUpdated(@NotNull Collection<VirtualFile> contentRoots,
@NotNull Collection<VirtualFile> appMapConfigFiles) {
if (AppLandJsonRpcService.getInstance(getProject()).getServerPort() != null) {
latch.countDown();
}
}
});
return latch;
}

private String createAppMapEvents(int requestCount, int queryCount) {
var json = new StringBuilder();
json.append("[");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package appland.actions;

import appland.AppMapBaseTest;
import appland.cli.TestAppLandDownloadService;
import appland.utils.DataContexts;
import appland.webviews.navie.NavieEditor;
import appland.webviews.navie.NavieEditorProvider;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.EdtTestUtil;
import com.intellij.testFramework.TestActionEvent;
import com.intellij.testFramework.fixtures.TempDirTestFixture;
import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

public class AddNavieContextFilesActionTest extends AppMapBaseTest {
@Override
protected TempDirTestFixture createTempDirTestFixture() {
return new TempDirTestFixtureImpl();
}

@Before
public void ensureToolsDownloaded() {
TestAppLandDownloadService.ensureDownloaded();
}

@Test
public void enabledForNavieEditorAndContextFile() throws Exception {
// must be created first because the Navie editor must be the selected editor
var contextFile = myFixture.configureByText("test.txt", "");

var tempDir = myFixture.createFile("test.txt", "").getParent();
withContentRoot(tempDir, () -> {
openNavieEditor(tempDir);

var presentation = updateAction(createActionContext(contextFile.getVirtualFile()));
Assert.assertTrue("Action must be enabled for a file", presentation.isEnabledAndVisible());
});
}

@Test
public void disabledWithoutNavieEditor() throws Exception {
var contextFile = myFixture.configureByText("test.txt", "");

var tempDir = myFixture.createFile("test.txt", "").getParent();
withContentRoot(tempDir, () -> {
var presentation = updateAction(createActionContext(contextFile.getVirtualFile()));
Assert.assertFalse("Action must be unavailable without selected Navie editor", presentation.isEnabledAndVisible());
});
}

@Test
public void disabledWithoutContextFile() throws Exception {
var contextDir = myFixture.configureByText("test.txt", "").getVirtualFile().getParent();

var tempDir = myFixture.createFile("test.txt", "").getParent();
withContentRoot(tempDir, () -> {
openNavieEditor(tempDir);

var presentation = updateAction(createActionContext(contextDir));
Assert.assertFalse("Action must be unavailable for a selected directory", presentation.isEnabledAndVisible());
});
}

private void openNavieEditor(@NotNull VirtualFile tempDir) throws Exception {
createAppMapConfig(tempDir, Path.of("tmp", "appmap"));
waitForJsonRpcServerPort();

edt(() -> NavieEditorProvider.openEditor(getProject(), DataContext.EMPTY_CONTEXT));

// wait until the Navie editor is the selected editor
var deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(30);
while (System.currentTimeMillis() <= deadline) {
var editor = EdtTestUtil.runInEdtAndGet(() -> FileEditorManager.getInstance(getProject()).getSelectedEditor());
if (editor instanceof NavieEditor) {
break;
}
Thread.sleep(500);
}

var editor = EdtTestUtil.runInEdtAndGet(() -> FileEditorManager.getInstance(getProject()).getSelectedEditor());
assertTrue("Navie editor must be open", editor instanceof NavieEditor);
}

private @NotNull DataContext createActionContext(@Nullable VirtualFile contextFile) {
return DataContexts.createCustomContext(dataId -> {
if (contextFile != null && PlatformDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) {
return new VirtualFile[]{contextFile};
}
if (PlatformDataKeys.PROJECT.is(dataId)) {
return myFixture.getProject();
}
return null;
});
}

private static @NotNull Presentation updateAction(@NotNull DataContext context) {
var action = ActionManager.getInstance().getAction("appmap.navie.pinContextFile");
assertNotNull(action);

var e = TestActionEvent.createFromAnAction(action, null, ActionPlaces.MAIN_MENU, context);
ActionUtil.performDumbAwareUpdate(action, e, false);
return e.getPresentation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import appland.AppMapBaseTest;
import appland.cli.AppLandCommandLineService;
import appland.cli.TestAppLandDownloadService;
import appland.settings.AppMapApplicationSettingsService;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

import java.util.Collection;
Expand All @@ -27,6 +28,11 @@ protected boolean runInDispatchThread() {
return false;
}

@Before
public void ensureToolsDownloaded() {
TestAppLandDownloadService.ensureDownloaded();
}

@Test
public void launchedWithProject() {
waitForJsonRpcServer();
Expand Down Expand Up @@ -65,7 +71,7 @@ public void serverConfigurationUpdated(@NotNull Collection<VirtualFile> contentR
public void serverRestartAfterTermination() throws Exception {
waitForJsonRpcServer();

var latch = createWaitForRestartCondition(false);
var latch = createWaitForJsonRpcServerRestartCondition(false);

// kill and wait for restart
TestAppLandJsonRpcService.killJsonRpcProcess(getProject());
Expand Down Expand Up @@ -97,7 +103,7 @@ public void restartAfterApiKeyChange() throws Exception {
var appMapSettings = AppMapApplicationSettingsService.getInstance();
appMapSettings.setApiKey("dummy");
try {
var latch = createWaitForRestartCondition(false);
var latch = createWaitForJsonRpcServerRestartCondition(false);

// change API key and wait for restart
appMapSettings.setApiKeyNotifying("new-api-key");
Expand All @@ -119,39 +125,4 @@ public void codeEditorInfo() {
var matches = editorInfo.matches("IntelliJ IDEA [0-9.]+( EAP | Beta)? by JetBrains s\\.r\\.o\\.");
assertTrue("Code editor info must match: " + editorInfo, matches);
}

private void waitForJsonRpcServer() {
var service = AppLandJsonRpcService.getInstance(getProject());
if (service.isServerRunning()) {
return;
}

var latch = createWaitForRestartCondition(true);
ApplicationManager.getApplication().executeOnPooledThread(service::startServer);
try {
assertTrue("The AppMap JSON-RPC server must launch", latch.await(30, TimeUnit.SECONDS));
} catch (InterruptedException e) {
addSuppressedException(e);
}
}

private @NotNull CountDownLatch createWaitForRestartCondition(boolean allowStart) {
var latch = new CountDownLatch(1);
getProject().getMessageBus()
.connect(getTestRootDisposable())
.subscribe(AppLandJsonRpcListener.TOPIC, new AppLandJsonRpcListenerAdapter() {
@Override
public void serverStarted() {
if (allowStart) {
latch.countDown();
}
}

@Override
public void serverRestarted() {
latch.countDown();
}
});
return latch;
}
}
Loading

0 comments on commit ab1b0d0

Please sign in to comment.