utility method TestModeFlags.runWithFlag, reset flags at the test end

GitOrigin-RevId: d320db6e39e4d686abd6ffbc0b0b4a0a535d036d
This commit is contained in:
Alexey Kudravtsev
2025-04-17 11:34:29 +02:00
committed by intellij-monorepo-bot
parent 0efa9bf192
commit eb8faafedb
8 changed files with 46 additions and 36 deletions

View File

@@ -55,15 +55,15 @@ class JavaRetypeTest : LightJavaCodeInsightFixtureTestCase() {
}
private fun doTestWithLookup() {
val autopopupOldValue = TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, true)
val filePath = "retype/${getTestName(false)}.java"
myFixture.configureByFile(filePath)
RetypeSession(project, myFixture.editor as EditorImpl, 50, null, 0).start()
while (editor.getUserData(RETYPE_SESSION_KEY) != null) {
IdeEventQueue.getInstance().flushQueue()
TestModeFlags.runWithFlag(CompletionAutoPopupHandler.ourTestingAutopopup, true) {
val filePath = "retype/${getTestName(false)}.java"
myFixture.configureByFile(filePath)
RetypeSession(project, myFixture.editor as EditorImpl, 50, null, 0).start()
while (editor.getUserData(RETYPE_SESSION_KEY) != null) {
IdeEventQueue.getInstance().flushQueue()
}
myFixture.checkResultByFile(filePath)
}
myFixture.checkResultByFile(filePath)
TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, autopopupOldValue)
}
private fun doTestWithoutLookup() {

View File

@@ -1146,7 +1146,7 @@ public class JavaAutoPopupTest extends JavaCompletionAutoPopupTestCase {
myFixture.configureByText("a.java", "class Foo <caret>");
type("ext");
TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, false);
TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, false, getTestRootDisposable());
edt(() -> myFixture.completeBasic());
assertNull(String.valueOf(myFixture.getLookupElementStrings()), getLookup());
myFixture.checkResult("class Foo extends <caret>");
@@ -1156,7 +1156,7 @@ public class JavaAutoPopupTest extends JavaCompletionAutoPopupTestCase {
myFixture.configureByText("a.java", "class Foo {<caret>}");
type("pr");
TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, false);
TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, false, getTestRootDisposable());
edt(() -> myFixture.completeBasic());
myFixture.checkResult("class Foo {pr<caret>}");

View File

@@ -5352,6 +5352,8 @@ f:com.intellij.testFramework.TestModeFlags
- s:get(com.intellij.openapi.util.Key):java.lang.Object
- s:is(com.intellij.openapi.util.Key):Z
- s:reset(com.intellij.openapi.util.Key):V
- s:runWithFlag(com.intellij.openapi.util.Key,java.lang.Object,com.intellij.openapi.util.ThrowableComputable):java.lang.Object
- s:runWithFlag(com.intellij.openapi.util.Key,java.lang.Object,java.lang.Runnable):V
- s:set(com.intellij.openapi.util.Key,java.lang.Object):java.lang.Object
- s:set(com.intellij.openapi.util.Key,java.lang.Object,com.intellij.openapi.Disposable):V
com.intellij.testIntegration.TestFramework

View File

@@ -4,13 +4,14 @@ package com.intellij.testFramework;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.ThrowableComputable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
@@ -18,14 +19,13 @@ import java.util.concurrent.CopyOnWriteArrayList;
* features can be tested.
*/
public final class TestModeFlags {
private static final Map<String, Object> ourFlags = new HashMap<>();
private static final Map<String, Object> ourFlags = new ConcurrentHashMap<>();
private static final List<TestModeFlagListener> ourListeners = new CopyOnWriteArrayList<>();
@TestOnly
public static <T> T set(@NotNull Key<T> flag, @Nullable T value) {
//noinspection unchecked
T oldValue = (T)ourFlags.get(flag.toString());
ourFlags.put(flag.toString(), value);
T oldValue = (T)(value == null ? ourFlags.remove(flag.toString()) : ourFlags.put(flag.toString(), value));
for (TestModeFlagListener listener : ourListeners) {
listener.testModeFlagChanged(flag, value);
}
@@ -39,11 +39,28 @@ public final class TestModeFlags {
@TestOnly
public static <T> void set(@NotNull Key<T> flag, T value, @NotNull Disposable parentDisposable) {
T oldValue = get(flag);
set(flag, value);
T oldValue = set(flag, value);
Disposer.register(parentDisposable, () -> set(flag, oldValue));
}
@TestOnly
public static <T,R,E extends Throwable> R runWithFlag(@NotNull Key<T> flag, T value, @NotNull ThrowableComputable<R, E> runnable) throws E {
T oldValue = set(flag, value);
try {
return runnable.compute();
}
finally {
set(flag, oldValue);
}
}
@TestOnly
public static <T> void runWithFlag(@NotNull Key<T> flag, T value, @NotNull Runnable runnable) {
runWithFlag(flag, value, ()->{
runnable.run();
return null;
});
}
public static <T> T get(@NotNull Key<T> flag) {
//noinspection unchecked
return (T)ourFlags.get(flag.toString());

View File

@@ -26,14 +26,14 @@ public final class IdentifierHighlighterPassFactory {
public IdentifierHighlighterPass createHighlightingPass(@NotNull PsiFile file,
@NotNull Editor editor,
@NotNull TextRange visibleRange) {
if (editor.isOneLineMode() && ((EditorEx)editor).isEmbeddedIntoDialogWrapper()) return null;
if (!CodeInsightSettings.getInstance().HIGHLIGHT_IDENTIFIER_UNDER_CARET ||
!checkDumbMode(file) ||
!isEnabled() ||
(!file.isPhysical() && !file.getOriginalFile().isPhysical())) {
return null;
if (CodeInsightSettings.getInstance().HIGHLIGHT_IDENTIFIER_UNDER_CARET &&
(!editor.isOneLineMode() || !((EditorEx)editor).isEmbeddedIntoDialogWrapper()) &&
checkDumbMode(file) &&
isEnabled() &&
(file.isPhysical() || file.getOriginalFile().isPhysical())) {
return new IdentifierHighlighterPass(file, editor, visibleRange);
}
return new IdentifierHighlighterPass(file, editor, visibleRange);
return null;
}
private static boolean checkDumbMode(@NotNull PsiFile file) {
@@ -49,13 +49,11 @@ public final class IdentifierHighlighterPassFactory {
public static void doWithHighlightingEnabled(@NotNull Project project, @NotNull Disposable parentDisposable, @NotNull Runnable r) {
ThreadingAssertions.assertEventDispatchThread();
BackgroundHighlighter.Companion.enableListenersInTest(project, parentDisposable);
TestModeFlags.set(ourTestingIdentifierHighlighting, true);
try {
r.run();
TestModeFlags.runWithFlag(ourTestingIdentifierHighlighting, true, r);
}
finally {
waitForIdentifierHighlighting();
TestModeFlags.reset(ourTestingIdentifierHighlighting);
}
}

View File

@@ -62,7 +62,7 @@ public final class VirtualFilePointerManagerImpl extends VirtualFilePointerManag
static boolean shouldCheckConsistency() {
return IS_UNDER_UNIT_TEST && !ApplicationManagerEx.isInStressTest()
&& !Boolean.TRUE.equals(TestModeFlags.get(DISABLE_VFS_CONSISTENCY_CHECK_IN_TEST));
&& !TestModeFlags.is(DISABLE_VFS_CONSISTENCY_CHECK_IN_TEST);
}
@TestOnly

View File

@@ -65,12 +65,7 @@ public abstract class DependenciesIndexedStatusServiceBaseTest {
public final ExternalResource dependenciesIndexedStatusServiceEnabler = new ExternalResource() {
@Override
protected void before() {
TestModeFlags.set(DependenciesIndexedStatusService.ENFORCEMENT_USAGE_TEST_MODE_FLAG, true);
}
@Override
protected void after() {
TestModeFlags.set(DependenciesIndexedStatusService.ENFORCEMENT_USAGE_TEST_MODE_FLAG, false);
TestModeFlags.set(DependenciesIndexedStatusService.ENFORCEMENT_USAGE_TEST_MODE_FLAG, true, disposableRule.getDisposable());
}
};

View File

@@ -28,12 +28,10 @@ import java.util.function.Predicate;
public abstract class CompletionAutoPopupTesterBase {
public void runWithAutoPopupEnabled(@NotNull ThrowableRunnable<Throwable> r) throws Throwable {
ApplicationManager.getApplication().assertIsNonDispatchThread();
TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, true);
try {
r.run();
TestModeFlags.runWithFlag(CompletionAutoPopupHandler.ourTestingAutopopup, true, ()->{r.run(); return null;});
}
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