From 31872258b18e81d5f973080e5130ebf49db3271f Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 20 Apr 2025 17:40:09 +0530 Subject: Some more lua api specs --- spec/LuaRuntimeApiSpec.cpp | 125 ++++++++++++++++++++++++++++++++++++++++----- spec/LuaRuntimeSpec.cpp | 8 +++ spec/WebViewStackSpec.cpp | 8 +++ spec/testUtils.h | 11 ++-- 4 files changed, 135 insertions(+), 17 deletions(-) (limited to 'spec') diff --git a/spec/LuaRuntimeApiSpec.cpp b/spec/LuaRuntimeApiSpec.cpp index 936244f..c340a03 100644 --- a/spec/LuaRuntimeApiSpec.cpp +++ b/spec/LuaRuntimeApiSpec.cpp @@ -25,7 +25,9 @@ private slots: void cleanupTestCase() { LuaRuntime::instance().stop_event_loop(); } - void test_web_event_add_event() { + void test_web_event_add_listener() { + describe("web.event.add_listener"); + context("when events, patterns and callback are specified correctly"); it("returns true and registers event") { auto &lua = LuaRuntime::instance(); @@ -116,6 +118,8 @@ private slots: } void test_web_event_dispatching() { + describe("web.event.add_listener (event dispatch)"); + context("when dispatching a registered event (without pattern)"); it("calls the registered event handler") { auto &lua = LuaRuntime::instance(); @@ -146,6 +150,8 @@ private slots: } void lua_api_view_set_url() { + describe("web.search.set_url"); + context("when called with a url and view id"); it("emits url_opened for the given view id") { auto &lua = LuaRuntime::instance(); @@ -192,23 +198,114 @@ private slots: } } - void lua_api_view_current() { - context("when called without a window id"); - it("emits url_opened for the given view id") { + void lua_api_search_set_text() { + describe("web.search.set_text"); + + context("when called with just the search text"); + it("emits search_requested with search text and view id 0") { auto &lua = LuaRuntime::instance(); - QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); + QSignalSpy search_requested(&lua, &LuaRuntime::search_requested); - lua.evaluate(R"( - return web.view.current() - )"); - QVERIFY(evaluation_completed_spy.wait()); + lua.evaluate("web.search.set_text('search text')"); + + QVERIFY(search_requested.wait()); + QCOMPARE(search_requested.first()[0], "search text"); + QCOMPARE(search_requested.first()[1], 0); + } + + context("when called with search text and view id"); + it("emits search_requested with search text and given view id") { + auto &lua = LuaRuntime::instance(); + QSignalSpy search_requested(&lua, &LuaRuntime::search_requested); + + lua.evaluate("web.search.set_text('search text', 2)"); + + QVERIFY(search_requested.wait()); + QCOMPARE(search_requested.first()[0], "search text"); + QCOMPARE(search_requested.first()[1], 2); + } + } + + void lua_api_search_next() { + describe("web.search.next"); + + context("when called without view id"); + it("emits search_next_requested with view id 0") { + auto &lua = LuaRuntime::instance(); + QSignalSpy search_next_requested(&lua, &LuaRuntime::search_next_requested); + + lua.evaluate("web.search.next()"); + + QVERIFY(search_next_requested.wait()); + QCOMPARE(search_next_requested.first().first(), 0); + } + + context("when called with view id"); + it("emits search_next_requested with given view id") { + auto &lua = LuaRuntime::instance(); + QSignalSpy search_next_requested(&lua, &LuaRuntime::search_next_requested); + + lua.evaluate("web.search.next(2)"); + + QVERIFY(search_next_requested.wait()); + QCOMPARE(search_next_requested.first().first(), 2); + } + } + + void lua_api_search_previous() { + describe("web.search.previous"); + + context("when called without view id"); + it("emits search_previous_requested with view id 0") { + auto &lua = LuaRuntime::instance(); + QSignalSpy search_previous_requested(&lua, &LuaRuntime::search_previous_requested); + + lua.evaluate("web.search.previous()"); + + QVERIFY(search_previous_requested.wait()); + QCOMPARE(search_previous_requested.first().first(), 0); + } + + context("when called with view id"); + it("emits search_previous_requested with given view id") { + auto &lua = LuaRuntime::instance(); + QSignalSpy search_previous_requested(&lua, &LuaRuntime::search_previous_requested); + + lua.evaluate("web.search.previous(2)"); + + QVERIFY(search_previous_requested.wait()); + QCOMPARE(search_previous_requested.first().first(), 2); + } + } + + void lua_api_search_get_text() { + describe("web.search.get_text"); + + it("returns the current search text") { + auto &lua = LuaRuntime::instance(); + auto &router = WindowActionRouter::instance(); + router.set_current_search_text("some search text"); + QSignalSpy evaluation_completed(&lua, &LuaRuntime::evaluation_completed); + + lua.evaluate("return web.search.get_text()"); + + QVERIFY(evaluation_completed.wait()); + QCOMPARE(evaluation_completed.first().first(), "some search text"); + } + } + + void lua_api_view_open_devtools() { + describe("web.view.open_devtools"); + + context("when called with view id"); + it("emits devtools_requested with view id") { + auto &lua = LuaRuntime::instance(); + QSignalSpy devtools_requested(&lua, &LuaRuntime::devtools_requested); - qDebug() << evaluation_completed_spy.first().first(); + lua.evaluate("web.view.open_devtools(42)"); - // QCOMPARE(evaluation_completed_spy.first().first(), 1); - // QCOMPARE(url_opened.first()[0], "https://updated-url.com"); - // QCOMPARE(url_opened.first()[1], OpenType::OpenUrl); - // QCOMPARE(url_opened.first()[2], 42); + QVERIFY(devtools_requested.wait()); + QCOMPARE(devtools_requested.first().first(), 42); } } }; diff --git a/spec/LuaRuntimeSpec.cpp b/spec/LuaRuntimeSpec.cpp index 97864b2..aeaaab2 100644 --- a/spec/LuaRuntimeSpec.cpp +++ b/spec/LuaRuntimeSpec.cpp @@ -14,6 +14,8 @@ private slots: void cleanupTestCase() { LuaRuntime::instance().stop_event_loop(); } void test_evaluate() { + describe("#evaluate"); + context("when given an expression returning a number"); it("emits evaluation_completed with the result") { auto &lua = LuaRuntime::instance(); @@ -68,6 +70,8 @@ private slots: } void test_queue_task() { + describe("#queue_task"); + context("when task is queued"); it("evaluates task asynchronously") { auto &lua = LuaRuntime::instance(); @@ -81,6 +85,8 @@ private slots: } void test_sanity_check_uv_timer() { + describe("uv timer"); + context("when a 1 second timer is set"); it("calls callback after 1 second") { auto &lua = LuaRuntime::instance(); @@ -101,6 +107,8 @@ private slots: } void test_sanity_check_uv_spawn() { + describe("uv spawn"); + it("calls exit callback when process exists") { auto &lua = LuaRuntime::instance(); QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed); diff --git a/spec/WebViewStackSpec.cpp b/spec/WebViewStackSpec.cpp index 155218e..a63eca6 100644 --- a/spec/WebViewStackSpec.cpp +++ b/spec/WebViewStackSpec.cpp @@ -23,6 +23,8 @@ private slots: void cleanupTestCase() { LuaRuntime::instance().stop_event_loop(); } void test_initial_state() { + describe("constructor"); + context("when initialized"); it("opens an empty stack maintaining current url") { Configuration configuration; @@ -34,6 +36,8 @@ private slots: } void test_open_url() { + describe("#open_url"); + context("when openUrl is called with an empty stack"); it("opens a new webview") { Configuration configuration; @@ -115,6 +119,8 @@ private slots: } void test_close() { + describe("#close"); + context("when close is called"); context("- with invalid id"); it("does nothing") { @@ -189,6 +195,8 @@ private slots: } void test_new_window_request_signal() { + describe("signal newWindowRequested"); + context("when webview emits a newWindowRequested signal"); context("- of type new view"); it("opens a new web view and focuses it") { diff --git a/spec/testUtils.h b/spec/testUtils.h index 37bc9f9..a20291c 100644 --- a/spec/testUtils.h +++ b/spec/testUtils.h @@ -7,22 +7,27 @@ #define NOT ! #define ANSI_BOLD "\x1b[1m" +#define COLOR_DESCRIBE ANSI_BOLD #define COLOR_CONTEXT "\x1b[32m" ANSI_BOLD #define COLOR_IT "\x1b[36m" ANSI_BOLD #define COLOR_SKIP "\x1b[33m" ANSI_BOLD #define ANSI_RESET "\x1b[0m" +#define describe(msg) \ + printf(COLOR_DESCRIBE "%s" ANSI_RESET "\n", msg); \ + fflush(stdout); + #define context(msg) \ - printf("⚪" COLOR_CONTEXT "%s" ANSI_RESET "\n", msg); \ + printf("│ " COLOR_CONTEXT "%s" ANSI_RESET "\n", msg); \ fflush(stdout); #define it(msg) \ - printf(" ⚪" ANSI_BOLD COLOR_IT "it " ANSI_RESET COLOR_IT "%s" ANSI_RESET "\n", msg); \ + printf("└─⚪" ANSI_BOLD COLOR_IT "it " ANSI_RESET COLOR_IT "%s" ANSI_RESET "\n", msg); \ fflush(stdout); \ if (1) #define xit(msg) \ - printf(" ⚪" COLOR_SKIP "SKIPPED it %s" ANSI_RESET "\n", msg); \ + printf("└─⚪" COLOR_SKIP "SKIPPED it %s" ANSI_RESET "\n", msg); \ fflush(stdout); \ if (0) -- cgit v1.3.1