void () { runPythonTest(new PyConsoleTask("/debug") { @Override public void testing() { addToHistory("a = 1"); addToHistory(""" def foo(): x = 1 y = 2 return x + y"""); runHistoryAction(true); checkCaretPosition(true); runHistoryAction(true); checkCaretPosition(true); runHistoryAction(false); checkCaretPosition(false); } private void addToHistory(String command) { WriteCommandAction.runWriteCommandAction(getProject(), () -> { ConsoleHistoryController.getController(getConsoleView()).addToHistory(command); }); } private void runHistoryAction(boolean isNext) { ConsoleHistoryController controller = ConsoleHistoryController.getController(getConsoleView()); EdtTestUtil.runInEdtAndWait(() -> { AnAction action = isNext ? controller.getHistoryNext() : controller.getHistoryPrev(); action.actionPerformed(TestActionEvent.createTestEvent()); }); } private void checkCaretPosition(boolean isNext) { EditorEx consoleEditor = getConsoleView().getConsoleEditor(); ApplicationManager.getApplication().runReadAction(() -> { int offset = consoleEditor.getCaretModel().getOffset(); int caretLine = consoleEditor.getDocument().getLineNumber(offset); int linesCount = consoleEditor.getDocument().getLineCount(); if (linesCount > 1) { if (isNext) { assertEquals(0, caretLine); } else { assertEquals(linesCount - 1, caretLine); } } else { assertEquals(0, caretLine); } }); } }); }