From 0921d0508afdb465c5e47280c95dd22343e61890 Mon Sep 17 00:00:00 2001 From: Tobias Taschner Date: Wed, 3 Nov 2021 14:02:21 +0100 Subject: [PATCH] Add tests for wxWebView::RunScriptAsync() --- tests/controls/webtest.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/controls/webtest.cpp b/tests/controls/webtest.cpp index 28b25088a2..c0eb77f529 100644 --- a/tests/controls/webtest.cpp +++ b/tests/controls/webtest.cpp @@ -69,10 +69,28 @@ protected: } } + void OnScriptResult(const wxWebViewEvent& evt) + { + m_asyncScriptResult = (evt.IsError()) ? 0 : 1; + m_asyncScriptString = evt.GetString(); + } + + void RunAsyncScript(const wxString& javascript) + { + m_browser->Bind(wxEVT_WEBVIEW_SCRIPT_RESULT, &WebViewTestCase::OnScriptResult, this); + m_asyncScriptResult = -1; + m_browser->RunScriptAsync(javascript); + while (m_asyncScriptResult == -1) + wxYield(); + m_browser->Unbind(wxEVT_WEBVIEW_SCRIPT_RESULT, &WebViewTestCase::OnScriptResult, this); + } + wxWebView* const m_browser; EventCounter* const m_loaded; wxString m_blankTitle; wxString m_alternateHistoryURL; + int m_asyncScriptResult; + wxString m_asyncScriptString; }; TEST_CASE_METHOD(WebViewTestCase, "WebView", "[wxWebView]") @@ -394,6 +412,21 @@ TEST_CASE_METHOD(WebViewTestCase, "WebView", "[wxWebView]") CHECK(!m_browser->RunScript("x.y.z")); } + SECTION("RunScriptAsync") + { +#ifdef __WXMSW__ + // IE doesn't support async script execution + if (!wxWebView::IsBackendAvailable(wxWebViewBackendEdge)) + return; +#endif + RunAsyncScript("function f(a){return a;}f('Hello World!');"); + CHECK(m_asyncScriptResult == 1); + CHECK(m_asyncScriptString == "Hello World!"); + + RunAsyncScript("int main() { return 0; }"); + CHECK(m_asyncScriptResult == 0); + } + SECTION("SetPage") { m_browser->SetPage("text", "");