1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-06-10 19:34:06 +02:00

Fix threading for property tests

This commit is contained in:
Alex Plate 2025-03-04 18:06:52 +02:00
parent ce01318032
commit cf49c0b76f
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
4 changed files with 52 additions and 25 deletions
src/testFixtures/kotlin/org/jetbrains/plugins/ideavim
tests/property-tests/src/test/kotlin/org/jetbrains/plugins/ideavim/propertybased

View File

@ -1062,6 +1062,7 @@ abstract class VimTestCase {
val keyHandler = KeyHandler.getInstance() val keyHandler = KeyHandler.getInstance()
val dataContext = injector.executionContextManager.getEditorExecutionContext(editor.vim) val dataContext = injector.executionContextManager.getEditorExecutionContext(editor.vim)
TestInputModel.getInstance(editor).setKeyStrokes(keys.filterNotNull()) TestInputModel.getInstance(editor).setKeyStrokes(keys.filterNotNull())
ApplicationManager.getApplication().invokeAndWait {
injector.actionExecutor.executeCommand( injector.actionExecutor.executeCommand(
editor.vim, editor.vim,
Runnable { Runnable {
@ -1076,6 +1077,7 @@ abstract class VimTestCase {
null, null,
) )
} }
}
@JvmStatic @JvmStatic
fun commandToKeys(command: String): List<KeyStroke> { fun commandToKeys(command: String): List<KeyStroke> {

View File

@ -9,6 +9,7 @@
package org.jetbrains.plugins.ideavim.propertybased package org.jetbrains.plugins.ideavim.propertybased
import com.intellij.ide.IdeEventQueue import com.intellij.ide.IdeEventQueue
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.testFramework.PlatformTestUtil import com.intellij.testFramework.PlatformTestUtil
import com.maddyhome.idea.vim.KeyHandler import com.maddyhome.idea.vim.KeyHandler
@ -36,48 +37,60 @@ class RandomActionsPropertyTest : VimPropertyTestBase() {
PropertyChecker.checkScenarios { PropertyChecker.checkScenarios {
ImperativeCommand { env -> ImperativeCommand { env ->
val editor = configureByText(text) val editor = configureByText(text)
ApplicationManager.getApplication().invokeAndWait {
KeyHandler.getInstance().fullReset(editor.vim) KeyHandler.getInstance().fullReset(editor.vim)
}
try { try {
moveCaretToRandomPlace(env, editor) moveCaretToRandomPlace(env, editor)
env.executeCommands(Generator.sampledFrom(AvailableActions(editor))) env.executeCommands(Generator.sampledFrom(AvailableActions(editor)))
} finally { } finally {
ApplicationManager.getApplication().invokeAndWait {
reset(editor) reset(editor)
} }
} }
} }
} }
}
@Test @Test
fun testRandomActionsOnLoremIpsum() { fun testRandomActionsOnLoremIpsum() {
PropertyChecker.checkScenarios { PropertyChecker.checkScenarios {
ImperativeCommand { env -> ImperativeCommand { env ->
val editor = configureByText(loremText) val editor = configureByText(loremText)
ApplicationManager.getApplication().invokeAndWait {
KeyHandler.getInstance().fullReset(editor.vim) KeyHandler.getInstance().fullReset(editor.vim)
}
try { try {
moveCaretToRandomPlace(env, editor) moveCaretToRandomPlace(env, editor)
env.executeCommands(Generator.sampledFrom(AvailableActions(editor))) env.executeCommands(Generator.sampledFrom(AvailableActions(editor)))
} finally { } finally {
ApplicationManager.getApplication().invokeAndWait {
reset(editor) reset(editor)
} }
} }
} }
} }
}
@Test @Test
fun testRandomActionsOnJavaCode() { fun testRandomActionsOnJavaCode() {
PropertyChecker.checkScenarios { PropertyChecker.checkScenarios {
ImperativeCommand { env -> ImperativeCommand { env ->
val editor = configureByJavaText(javaText) val editor = configureByJavaText(javaText)
ApplicationManager.getApplication().invokeAndWait {
KeyHandler.getInstance().fullReset(editor.vim) KeyHandler.getInstance().fullReset(editor.vim)
}
try { try {
moveCaretToRandomPlace(env, editor) moveCaretToRandomPlace(env, editor)
env.executeCommands(Generator.sampledFrom(AvailableActions(editor))) env.executeCommands(Generator.sampledFrom(AvailableActions(editor)))
} finally { } finally {
ApplicationManager.getApplication().invokeAndWait {
reset(editor) reset(editor)
} }
} }
} }
} }
}
companion object { companion object {
private val text = """ private val text = """
@ -110,9 +123,11 @@ private class AvailableActions(private val editor: Editor) : ImperativeCommand {
env.logMessage("Use command: ${injector.parser.toKeyNotation(currentKeys + usedKey)}. ${if (node?.data != null) "Action: ${node.data!!.actionId}" else ""}") env.logMessage("Use command: ${injector.parser.toKeyNotation(currentKeys + usedKey)}. ${if (node?.data != null) "Action: ${node.data!!.actionId}" else ""}")
VimTestCase.typeText(listOf(usedKey), editor, editor.project) VimTestCase.typeText(listOf(usedKey), editor, editor.project)
ApplicationManager.getApplication().invokeAndWait {
IdeEventQueue.getInstance().flushQueue() IdeEventQueue.getInstance().flushQueue()
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue() PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
} }
}
private val esc = key("<Esc>") private val esc = key("<Esc>")
} }

View File

@ -9,6 +9,7 @@
package org.jetbrains.plugins.ideavim.propertybased package org.jetbrains.plugins.ideavim.propertybased
import com.intellij.ide.highlighter.JavaFileType import com.intellij.ide.highlighter.JavaFileType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.KeyHandler import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.VimPlugin import com.maddyhome.idea.vim.VimPlugin
@ -20,8 +21,12 @@ import org.jetbrains.plugins.ideavim.VimTestCase
abstract class VimPropertyTestBase : VimTestCase() { abstract class VimPropertyTestBase : VimTestCase() {
protected fun moveCaretToRandomPlace(env: ImperativeCommand.Environment, editor: Editor) { protected fun moveCaretToRandomPlace(env: ImperativeCommand.Environment, editor: Editor) {
val pos = env.generateValue(Generator.integers(0, editor.document.textLength - 1), "Put caret at position %s") val pos = env.generateValue(Generator.integers(0, editor.document.textLength - 1), "Put caret at position %s")
ApplicationManager.getApplication().invokeAndWait {
ApplicationManager.getApplication().runReadAction {
editor.caretModel.currentCaret.vim.moveToOffset(pos) editor.caretModel.currentCaret.vim.moveToOffset(pos)
} }
}
}
protected fun reset(editor: Editor) { protected fun reset(editor: Editor) {
val keyState = KeyHandler.getInstance().keyHandlerState val keyState = KeyHandler.getInstance().keyHandlerState

View File

@ -9,6 +9,7 @@
package org.jetbrains.plugins.ideavim.propertybased package org.jetbrains.plugins.ideavim.propertybased
import com.intellij.ide.IdeEventQueue import com.intellij.ide.IdeEventQueue
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.testFramework.PlatformTestUtil import com.intellij.testFramework.PlatformTestUtil
import com.maddyhome.idea.vim.api.injector import com.maddyhome.idea.vim.api.injector
@ -29,12 +30,14 @@ class YankDeletePropertyTest : VimPropertyTestBase() {
moveCaretToRandomPlace(env, editor) moveCaretToRandomPlace(env, editor)
env.executeCommands(Generator.sampledFrom(YankDeleteActions(editor))) env.executeCommands(Generator.sampledFrom(YankDeleteActions(editor)))
} finally { } finally {
ApplicationManager.getApplication().invokeAndWait {
reset(editor) reset(editor)
} }
} }
} }
} }
} }
}
private class YankDeleteActions(private val editor: Editor) : ImperativeCommand { private class YankDeleteActions(private val editor: Editor) : ImperativeCommand {
override fun performCommand(env: ImperativeCommand.Environment) { override fun performCommand(env: ImperativeCommand.Environment) {
@ -43,10 +46,12 @@ private class YankDeleteActions(private val editor: Editor) : ImperativeCommand
env.logMessage("Use command: $key") env.logMessage("Use command: $key")
VimTestCase.typeText(injector.parser.parseKeys(key), editor, editor.project) VimTestCase.typeText(injector.parser.parseKeys(key), editor, editor.project)
ApplicationManager.getApplication().invokeAndWait {
IdeEventQueue.getInstance().flushQueue() IdeEventQueue.getInstance().flushQueue()
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue() PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
} }
} }
}
private val keysList = private val keysList =
arrayListOf("v", "V", "<C-V>", "h", "j", "k", "l", "w", "e", "b", "y", "Y", "_", "d", "D", "c", "C", "p", "P") arrayListOf("v", "V", "<C-V>", "h", "j", "k", "l", "w", "e", "b", "y", "Y", "_", "d", "D", "c", "C", "p", "P")