From dd6f51fbddc5bba8d6a5ca9bf96def27dc70cd64 Mon Sep 17 00:00:00 2001 From: "Alexander.Kass" Date: Fri, 16 Aug 2024 13:10:51 +0300 Subject: [PATCH] no more inheritors of SqlLightCodeInsightFixtureTestCase GitOrigin-RevId: 91010785e0f401ae74cba5951561c5137a943796 --- .../fixtures/CompletionAutoPopupTester.java | 116 +++-------------- .../CompletionAutoPopupTesterBase.java | 120 ++++++++++++++++++ 2 files changed, 137 insertions(+), 99 deletions(-) create mode 100644 platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTesterBase.java diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTester.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTester.java index 686cfd7f5d97..c91ed299dfd1 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTester.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTester.java @@ -1,118 +1,36 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.testFramework.fixtures; -import com.intellij.codeInsight.completion.CompletionPhase; -import com.intellij.codeInsight.completion.impl.CompletionServiceImpl; -import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler; import com.intellij.codeInsight.lookup.impl.LookupImpl; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.ex.DocumentEx; -import com.intellij.openapi.util.EmptyRunnable; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.testFramework.EdtTestUtil; -import com.intellij.testFramework.TestModeFlags; -import com.intellij.testFramework.common.ThreadUtil; -import com.intellij.util.ThrowableRunnable; -import com.intellij.util.TimeoutUtil; -import com.intellij.util.ui.UIUtil; -import junit.framework.TestCase; +import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Predicate; - -public final class CompletionAutoPopupTester { +public final class CompletionAutoPopupTester extends CompletionAutoPopupTesterBase { private final CodeInsightTestFixture myFixture; public CompletionAutoPopupTester(CodeInsightTestFixture fixture) { myFixture = fixture; } - public void runWithAutoPopupEnabled(@NotNull ThrowableRunnable r) throws Throwable { - ApplicationManager.getApplication().assertIsNonDispatchThread(); - TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, true); - try { - r.run(); - } - finally { - TestModeFlags.reset(CompletionAutoPopupHandler.ourTestingAutopopup); - Editor editor = myFixture.getEditor(); - if (editor != null) { - ((DocumentEx)editor.getDocument()).setModificationStamp(0);// to force possible autopopup handler's invokeLater cancel itself - } - } - } - - public void joinCompletion() { - waitPhase(phase -> { - return !(phase instanceof CompletionPhase.CommittingDocuments || - phase instanceof CompletionPhase.Synchronous || - phase instanceof CompletionPhase.BgCalculation); - }); - } - - @SuppressWarnings("UseOfSystemOutOrSystemErr") - private static void waitPhase(Predicate condition) { - for (int j = 1; j < 1000; j++) { - // in EDT to avoid getting the phase in the middle of a complex EDT operation that changes the phase several times - CompletionPhase phase = EdtTestUtil.runInEdtAndGet(() -> CompletionServiceImpl.getCompletionPhase()); - if (condition.test(phase)) { - return; - } - if (j >= 400 && j % 100 == 0) { - System.out.println("Free memory: " + Runtime.getRuntime().freeMemory() + " of " + Runtime.getRuntime().totalMemory() + "\n"); - ThreadUtil.printThreadDump(); - System.out.println("\n\n----------------------------\n\n"); - } - - TimeoutUtil.sleep(10); - } - - TestCase.fail("Too long completion: " + CompletionServiceImpl.getCompletionPhase()); - } - - public void joinCommit(Runnable c1) { - AtomicBoolean committed = new AtomicBoolean(); - AtomicBoolean run = new AtomicBoolean(); - AtomicBoolean executed = new AtomicBoolean(true); - EdtTestUtil.runInEdtAndWait(() -> executed.set(PsiDocumentManager.getInstance(myFixture.getProject()).performWhenAllCommitted(() -> { - run.set(true); - ApplicationManager.getApplication().invokeLater(() -> { - c1.run(); - committed.set(true); - }); - }))); - ApplicationManager.getApplication().assertReadAccessNotAllowed(); - ApplicationManager.getApplication().assertIsNonDispatchThread(); - long start = System.currentTimeMillis(); - while (!committed.get()) { - if (System.currentTimeMillis() - start >= 20000) { - ThreadUtil.printThreadDump(); - TestCase.fail("too long waiting for documents to be committed. executed: " + executed + "; run: " + run + "; "); - } - - UIUtil.pump(); - } - } - - public void joinCommit() { - joinCommit(EmptyRunnable.getInstance()); - } - - public void joinAutopopup() { - waitPhase(phase -> !(phase instanceof CompletionPhase.CommittingDocuments)); - } - + @Override public LookupImpl getLookup() { return (LookupImpl)myFixture.getLookup(); } - public void typeWithPauses(String s) { - for (int i = 0; i < s.length(); i++) { - myFixture.type(s.charAt(i)); - joinAutopopup();// for the autopopup handler's alarm, or the restartCompletion's invokeLater - joinCompletion(); - } + @Override + protected @Nullable Editor getEditor() { + return myFixture.getEditor(); + } + + @Override + protected @NotNull Project getProject() { + return myFixture.getProject(); + } + + @Override + protected void type(char c) { + myFixture.type(c); } } diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTesterBase.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTesterBase.java new file mode 100644 index 000000000000..bfae13ab49fb --- /dev/null +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/CompletionAutoPopupTesterBase.java @@ -0,0 +1,120 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.testFramework.fixtures; + +import com.intellij.codeInsight.completion.CompletionPhase; +import com.intellij.codeInsight.completion.impl.CompletionServiceImpl; +import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler; +import com.intellij.codeInsight.lookup.impl.LookupImpl; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.ex.DocumentEx; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.EmptyRunnable; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.testFramework.EdtTestUtil; +import com.intellij.testFramework.TestModeFlags; +import com.intellij.testFramework.common.ThreadUtil; +import com.intellij.util.ThrowableRunnable; +import com.intellij.util.TimeoutUtil; +import com.intellij.util.ui.UIUtil; +import junit.framework.TestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Predicate; + +public abstract class CompletionAutoPopupTesterBase { + public void runWithAutoPopupEnabled(@NotNull ThrowableRunnable r) throws Throwable { + ApplicationManager.getApplication().assertIsNonDispatchThread(); + TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, true); + try { + r.run(); + } + finally { + TestModeFlags.reset(CompletionAutoPopupHandler.ourTestingAutopopup); + Editor editor = getEditor(); + if (editor != null) { + ((DocumentEx)editor.getDocument()).setModificationStamp(0);// to force possible autopopup handler's invokeLater cancel itself + } + } + } + + @Nullable + protected abstract Editor getEditor(); + + public void joinCompletion() { + waitPhase(phase -> { + return !(phase instanceof CompletionPhase.CommittingDocuments || + phase instanceof CompletionPhase.Synchronous || + phase instanceof CompletionPhase.BgCalculation); + }); + } + + @SuppressWarnings("UseOfSystemOutOrSystemErr") + private static void waitPhase(Predicate condition) { + for (int j = 1; j < 1000; j++) { + // in EDT to avoid getting the phase in the middle of a complex EDT operation that changes the phase several times + CompletionPhase phase = EdtTestUtil.runInEdtAndGet(() -> CompletionServiceImpl.getCompletionPhase()); + if (condition.test(phase)) { + return; + } + if (j >= 400 && j % 100 == 0) { + System.out.println("Free memory: " + Runtime.getRuntime().freeMemory() + " of " + Runtime.getRuntime().totalMemory() + "\n"); + ThreadUtil.printThreadDump(); + System.out.println("\n\n----------------------------\n\n"); + } + + TimeoutUtil.sleep(10); + } + + TestCase.fail("Too long completion: " + CompletionServiceImpl.getCompletionPhase()); + } + + @NotNull + protected abstract Project getProject(); + + public void joinCommit(Runnable c1) { + AtomicBoolean committed = new AtomicBoolean(); + AtomicBoolean run = new AtomicBoolean(); + AtomicBoolean executed = new AtomicBoolean(true); + EdtTestUtil.runInEdtAndWait(() -> executed.set(PsiDocumentManager.getInstance(getProject()).performWhenAllCommitted(() -> { + run.set(true); + ApplicationManager.getApplication().invokeLater(() -> { + c1.run(); + committed.set(true); + }); + }))); + ApplicationManager.getApplication().assertReadAccessNotAllowed(); + ApplicationManager.getApplication().assertIsNonDispatchThread(); + long start = System.currentTimeMillis(); + while (!committed.get()) { + if (System.currentTimeMillis() - start >= 20000) { + ThreadUtil.printThreadDump(); + TestCase.fail("too long waiting for documents to be committed. executed: " + executed + "; run: " + run + "; "); + } + + UIUtil.pump(); + } + } + + public void joinCommit() { + joinCommit(EmptyRunnable.getInstance()); + } + + public void joinAutopopup() { + waitPhase(phase -> !(phase instanceof CompletionPhase.CommittingDocuments)); + } + + public abstract LookupImpl getLookup(); + + protected abstract void type(char c); + + public void typeWithPauses(String s) { + for (int i = 0; i < s.length(); i++) { + type(s.charAt(i)); + joinAutopopup();// for the autopopup handler's alarm, or the restartCompletion's invokeLater + joinCompletion(); + } + } +}