From 05ae8976a8e1ab5411058de244c4e2fcc87ec369 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Wed, 12 Mar 2025 20:47:57 +0530 Subject: Refactor MainWindow into InputMediator --- spec/InputMediatorSpec.cpp | 162 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 spec/InputMediatorSpec.cpp (limited to 'spec/InputMediatorSpec.cpp') diff --git a/spec/InputMediatorSpec.cpp b/spec/InputMediatorSpec.cpp new file mode 100644 index 0000000..464e156 --- /dev/null +++ b/spec/InputMediatorSpec.cpp @@ -0,0 +1,162 @@ +#include "InputMediator.hpp" +#include "LuaRuntime.hpp" +#include "testUtils.h" +#include "widgets/InputLine.hpp" +#include "widgets/WebViewStack.hpp" + +class InputMediatorSpec : public QObject { + Q_OBJECT + +private slots: + void testInputTypes() { + context("when showCommandInput is called"); + it("shows url input with default command") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + QCOMPARE(inputLine.isHidden(), true); + + inputMediator.showCommandInput("open"); + + QCOMPARE(inputLine.isHidden(), false); + QCOMPARE(inputLine.prompt(), "[exec]"); + QCOMPARE(inputLine.getInputText(), "open"); + } + + context("when showURLInput is called"); + it("shows url input with default url") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + QCOMPARE(inputLine.isHidden(), true); + + inputMediator.showURLInput("http://a.com"); + + QCOMPARE(inputLine.isHidden(), false); + QCOMPARE(inputLine.prompt(), "[url]"); + QCOMPARE(inputLine.getInputText(), "http://a.com"); + } + } + + void testCommandEvaluationOpen() { + context("when command open is executed"); + context("- without args"); + it("opens url input") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + + inputMediator.showCommandInput(); + emit inputLine.submitted("open"); + + QCOMPARE(inputLine.prompt(), "[url]"); + } + + context("when command open is executed"); + context("- with url as an arg"); + it("opens url in current tab") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + + inputMediator.showCommandInput(); + emit inputLine.submitted("open http://a.com"); + + std::vector urls = {QUrl("http://a.com")}; + QCOMPARE(webViewStack.urls(), urls); + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + } + } + + void testCommandEvaluationTabOpen() { + context("when command tabopen is run"); + context("- without args"); + it("opens url in a new tab") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + + inputMediator.showCommandInput(); + emit inputLine.submitted("tabopen"); + + QCOMPARE(inputLine.prompt(), "[url]"); + } + + context("when command tabopen is executed"); + context("- with url as an arg"); + it("opens url in a new tab") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + + inputMediator.showCommandInput(); + emit inputLine.submitted("tabopen http://a.com"); + + std::vector urls = {QUrl(WebViewStack::NewtabURL), + QUrl("http://a.com")}; + QCOMPARE(webViewStack.urls(), urls); + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + } + } + + void testCommandEvaluationTabNextPrev() { + context("when command tabnext is executed"); + it("jumps to next tab") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + webViewStack.openUrl(QUrl("https://a1.com"), OpenType::OpenUrl); + webViewStack.openUrl(QUrl("https://a2.com"), OpenType::OpenUrlInTab); + webViewStack.openUrl(QUrl("https://a2.com"), OpenType::OpenUrlInBgTab); + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + + inputMediator.showCommandInput(); + emit inputLine.submitted("tabnext"); + + QCOMPARE(webViewStack.currentWebViewIndex(), 2); + } + + context("when command tabprev is executed"); + it("jumps to previous tab") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + webViewStack.openUrl(QUrl("https://a1.com"), OpenType::OpenUrl); + webViewStack.openUrl(QUrl("https://a2.com"), OpenType::OpenUrlInTab); + webViewStack.openUrl(QUrl("https://a2.com"), OpenType::OpenUrlInBgTab); + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + + inputMediator.showCommandInput(); + emit inputLine.submitted("tabprev"); + + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + } + } + + void testHideInputLine() { + context("when hideInputLine is called"); + it("hides input") { + auto inputLine = InputLine(); + auto webViewStack = WebViewStack(); + auto luaRuntime = LuaRuntime::instance(); + InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime); + inputMediator.showURLInput(); + QCOMPARE(inputLine.isHidden(), false); + + inputMediator.hideInputLine(); + + QCOMPARE(inputLine.isHidden(), true); + } + } +}; + +QTEST_REGISTER(InputMediatorSpec) +#include "InputMediatorSpec.moc" -- cgit v1.3.1