aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-08-04 22:36:56 +0530
committerAkshay Nair <phenax5@gmail.com>2025-08-04 22:36:56 +0530
commitc7e105ee4254d00f591c4c002f32c223f4c2448a (patch)
tree17ebadd5dc1a5e148c4b2f5ed1c70b7c7e54fe1a /spec
parent0d1258aee3f39c8d5348588a87e804ec8cd27bd5 (diff)
downloadnull-browser-c7e105ee4254d00f591c4c002f32c223f4c2448a.tar.gz
null-browser-c7e105ee4254d00f591c4c002f32c223f4c2448a.zip
Add on_result callback for web.view.run_js
Diffstat (limited to 'spec')
-rw-r--r--spec/LuaRuntimeApiSpec.cpp57
-rw-r--r--spec/LuaRuntimeSpec.cpp54
2 files changed, 110 insertions, 1 deletions
diff --git a/spec/LuaRuntimeApiSpec.cpp b/spec/LuaRuntimeApiSpec.cpp
index 123eaa1..028ceb6 100644
--- a/spec/LuaRuntimeApiSpec.cpp
+++ b/spec/LuaRuntimeApiSpec.cpp
@@ -401,7 +401,7 @@ private slots:
}
}
- void test_lua_api_decorations_set_enabled() {
+ void test_lua_api_decorations_enable() {
describe("web.decorations.*.enable");
context("when called without view id");
@@ -460,7 +460,9 @@ private slots:
QCOMPARE(call[2].value<std::optional<WindowId>>(), std::make_optional(42));
}
}
+ }
+ void test_lua_api_decorations_disable() {
describe("web.decorations.*.disable");
context("when called without view id");
@@ -520,6 +522,59 @@ private slots:
}
}
}
+
+ void test_lua_api_view_run_js() {
+ describe("web.view.run_js");
+
+ context("when called without opts");
+ it("runs js in current view") {
+ auto &lua = LuaRuntime::instance();
+ QSignalSpy view_run_js(&lua, &LuaRuntime::webview_js_eval_requested);
+
+ lua.evaluate(R"LUA( web.view.run_js("dostuff(200)") )LUA");
+
+ QVERIFY(view_run_js.wait());
+ auto call = view_run_js.first();
+ QCOMPARE(call[0], "dostuff(200)");
+ QCOMPARE(call[1], 0);
+ QVERIFY(call[2].canConvert<JsOnResultFunc>()); // Noop function
+ }
+
+ context("when called with opts.view");
+ it("runs js in given view") {
+ auto &lua = LuaRuntime::instance();
+ QSignalSpy view_run_js(&lua, &LuaRuntime::webview_js_eval_requested);
+
+ lua.evaluate(R"LUA( web.view.run_js("dostuff(200)", { view = 42 }) )LUA");
+
+ QVERIFY(view_run_js.wait());
+ auto call = view_run_js.first();
+ QCOMPARE(call[0], "dostuff(200)");
+ QCOMPARE(call[1], 42);
+ QVERIFY(call[2].canConvert<JsOnResultFunc>());
+ }
+
+ context("when called with opts.on_result");
+ it("runs js in given view and calls on_result with the result") {
+ auto &lua = LuaRuntime::instance();
+ QSignalSpy view_run_js(&lua, &LuaRuntime::webview_js_eval_requested);
+
+ lua.evaluate(R"LUA(
+ _G.on_result_called_with = null
+ web.view.run_js("dostuff(200)", { view = 42, on_result = function(value)
+ _G.on_result_called_with = value
+ end })
+ )LUA");
+
+ QVERIFY(view_run_js.wait());
+ auto call = view_run_js.first();
+ QCOMPARE(call[0], "dostuff(200)");
+ QCOMPARE(call[1], 42);
+ QVERIFY(call[2].canConvert<JsOnResultFunc>());
+ call[2].value<JsOnResultFunc>()(9921.29);
+ QVERIFY(wait_for_lua_to_be_true(R"LUA( return _G.on_result_called_with == 9921.29 )LUA"));
+ }
+ }
};
QTEST_REGISTER(LuaRuntimeApiSpec)
diff --git a/spec/LuaRuntimeSpec.cpp b/spec/LuaRuntimeSpec.cpp
index aeaaab2..4d9270c 100644
--- a/spec/LuaRuntimeSpec.cpp
+++ b/spec/LuaRuntimeSpec.cpp
@@ -1,7 +1,9 @@
#include <QtCore>
+#include <qtestcase.h>
#include <uv.h>
#include "LuaRuntime.hpp"
+#include "lua.h"
#include "testUtils.h"
// NOLINTBEGIN
@@ -124,6 +126,58 @@ private slots:
QVERIFY(wait_for_lua_to_be_true("return _G.spawn_exit_code == 0"));
}
}
+
+ void test_push_qvariant() {
+ describe("LuaRuntime::push_qvariant");
+
+ context("when called with a QString");
+ it("pushes lua string") {
+ auto &lua = LuaRuntime::instance();
+ auto *state = lua.get_state();
+
+ LuaRuntime::push_qvariant(state, QString("hello"));
+
+ QVERIFY(lua_isstring(state, 1));
+ QCOMPARE(lua_tostring(state, 1), "hello");
+ lua_pop(state, 1);
+ }
+
+ context("when called with a QVariant int");
+ it("pushes lua number") {
+ auto &lua = LuaRuntime::instance();
+ auto *state = lua.get_state();
+
+ LuaRuntime::push_qvariant(state, QVariant(42));
+
+ QVERIFY(lua_isnumber(state, 1));
+ QCOMPARE(lua_tointeger(state, 1), 42);
+ lua_pop(state, 1);
+ }
+
+ context("when called with a QVariant double");
+ it("pushes lua number") {
+ auto &lua = LuaRuntime::instance();
+ auto *state = lua.get_state();
+
+ LuaRuntime::push_qvariant(state, QVariant(42.69));
+
+ QVERIFY(lua_isnumber(state, 1));
+ QCOMPARE(lua_tonumber(state, 1), 42.69);
+ lua_pop(state, 1);
+ }
+
+ context("when called with a QVariant bool");
+ it("pushes lua boolean") {
+ auto &lua = LuaRuntime::instance();
+ auto *state = lua.get_state();
+
+ LuaRuntime::push_qvariant(state, QVariant(true));
+
+ QVERIFY(lua_isboolean(state, 1));
+ QCOMPARE(lua_toboolean(state, 1), true);
+ lua_pop(state, 1);
+ }
+ }
};
QTEST_REGISTER(LuaRuntimeSpec)