From 2db21e78430578f3a07f22cc75ee3fe9aaa7a3f2 Mon Sep 17 00:00:00 2001 From: Mikhail Sokolov Date: Mon, 13 Jan 2020 15:27:57 +0300 Subject: [PATCH] IDEA-223845 Search Everywhere: update Goto tests to make them more similar to real usecases GitOrigin-RevId: e85dba21ed5f14afad7bc2d98a8685a8da13eb95 --- .../navigation/SearchEverywhereTest.groovy | 60 +++++++++++++++++++ .../AbstractGotoSEContributor.java | 5 +- .../searcheverywhere/SearchEverywhereUI.java | 21 +++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testSrc/com/intellij/java/navigation/SearchEverywhereTest.groovy diff --git a/java/java-tests/testSrc/com/intellij/java/navigation/SearchEverywhereTest.groovy b/java/java-tests/testSrc/com/intellij/java/navigation/SearchEverywhereTest.groovy new file mode 100644 index 000000000000..fb51813cfc2e --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/navigation/SearchEverywhereTest.groovy @@ -0,0 +1,60 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.navigation + +import com.intellij.ide.actions.searcheverywhere.SearchEverywhereContributor +import com.intellij.ide.actions.searcheverywhere.SearchEverywhereManagerImpl +import com.intellij.ide.actions.searcheverywhere.SearchEverywhereUI +import com.intellij.openapi.util.Disposer +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase +import com.intellij.util.ui.UIUtil +import org.junit.Assert + +import java.util.concurrent.Future + +class SearchEverywhereTest extends LightJavaCodeInsightFixtureTestCase { + SearchEverywhereUI mySearchUI + + @Override + protected void tearDown() throws Exception { + if (mySearchUI != null) { + Disposer.dispose(mySearchUI) + mySearchUI = null + } + + super.tearDown() + } + + void "test switch to external files when nothing is found"() { + def strBuffer = myFixture.addClass("class StrBuffer{ }") + def stringBuffer = myFixture.findClass("java.lang.StringBuffer") + + def ui = createTestUI([ChooseByNameTest.createClassContributor(project)]) + + def future = ui.findElementsForPattern("StrBuffer") + if (!waitFor(future)) Assert.fail("Search haven't finished") + assert future.get() == [strBuffer] + + future = ui.findElementsForPattern("StringBuffer") + if (!waitFor(future)) Assert.fail("Search haven't finished") + assert future.get() == [stringBuffer] + } + + private static boolean waitFor(Future future, long timeout = 30000) { + def start = System.currentTimeMillis() + while (!future.isDone()) { + UIUtil.dispatchAllInvocationEvents() + if (System.currentTimeMillis() - start > timeout) return false + } + + return true + } + + + private SearchEverywhereUI createTestUI(List> contributors) { + if (mySearchUI != null) Disposer.dispose(mySearchUI) + + mySearchUI = new SearchEverywhereUI(project, contributors) + mySearchUI.switchToContributor(SearchEverywhereManagerImpl.ALL_CONTRIBUTORS_GROUP_ID) + return mySearchUI + } +} diff --git a/platform/lang-impl/src/com/intellij/ide/actions/searcheverywhere/AbstractGotoSEContributor.java b/platform/lang-impl/src/com/intellij/ide/actions/searcheverywhere/AbstractGotoSEContributor.java index 30df93ac639d..8b430908098a 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/searcheverywhere/AbstractGotoSEContributor.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/searcheverywhere/AbstractGotoSEContributor.java @@ -16,6 +16,7 @@ import com.intellij.openapi.actionSystem.ex.ActionManagerEx; import com.intellij.openapi.actionSystem.ex.CustomComponentAction; import com.intellij.openapi.actionSystem.impl.ActionButtonWithText; import com.intellij.openapi.actionSystem.impl.SimpleDataContext; +import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileEditor.OpenFileDescriptor; @@ -267,7 +268,9 @@ public abstract class AbstractGotoSEContributor implements WeightedSearchEverywh } }; - if (ApplicationManager.getApplication().isUnitTestMode()) { + + Application application = ApplicationManager.getApplication(); + if (application.isUnitTestMode() && application.isDispatchThread()) { fetchRunnable.run(); } else { diff --git a/platform/lang-impl/src/com/intellij/ide/actions/searcheverywhere/SearchEverywhereUI.java b/platform/lang-impl/src/com/intellij/ide/actions/searcheverywhere/SearchEverywhereUI.java index 5055fb03d3f3..d7ed117a2819 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/searcheverywhere/SearchEverywhereUI.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/searcheverywhere/SearchEverywhereUI.java @@ -65,6 +65,7 @@ import com.intellij.util.ui.JBUI; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; import javax.swing.*; import javax.swing.border.Border; @@ -73,6 +74,8 @@ import java.awt.*; import java.awt.event.*; import java.util.List; import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -952,6 +955,16 @@ public class SearchEverywhereUI extends BigPopupUI implements DataProvider, Quic return isAllTabSelected() ? getAllTabContributors() : Collections.singleton(mySelectedTab.getContributor().get()); } + @TestOnly + private CompletableFuture> testResultsFuture; + + @TestOnly + public Future> findElementsForPattern(String pattern) { + testResultsFuture = new CompletableFuture<>(); + mySearchField.setText(pattern); + return testResultsFuture; + } + private class CompositeCellRenderer implements ListCellRenderer { @Override @@ -1618,6 +1631,14 @@ public class SearchEverywhereUI extends BigPopupUI implements DataProvider, Quic hasMoreContributors.forEach(myListModel::setHasMore); mySelectionTracker.resetSelectionIfNeeded(); + + //noinspection TestOnlyProblems + if (testResultsFuture != null) { + //noinspection TestOnlyProblems + testResultsFuture.complete(myListModel.getItems()); + //noinspection TestOnlyProblems + testResultsFuture = null; + } } }