Merge branch 'webview_runscript_improvements' of https://github.com/TcT2k/wxWidgets

Add WebView::RunScriptAsync() for running scripts asynchronously.

See https://github.com/wxWidgets/wxWidgets/pull/2316
This commit is contained in:
Vadim Zeitlin
2021-11-16 17:30:55 +01:00
13 changed files with 390 additions and 324 deletions

View File

@@ -35,6 +35,17 @@ public:
: m_browser(wxWebView::New()),
m_loaded(new EventCounter(m_browser, wxEVT_WEBVIEW_LOADED))
{
#ifdef __WXMSW__
if (wxWebView::IsBackendAvailable(wxWebViewBackendEdge))
{
// The blank page does not have an empty title with edge
m_blankTitle = "about:blank";
// Edge does not support about: url use a different URL instead
m_alternateHistoryURL = "about:blank";
}
else
#endif
m_alternateHistoryURL = "about:";
}
~WebViewTestCase()
@@ -53,13 +64,33 @@ protected:
if(i % 2 == 1)
m_browser->LoadURL("about:blank");
else
m_browser->LoadURL("about:");
m_browser->LoadURL(m_alternateHistoryURL);
ENSURE_LOADED;
}
}
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]")
@@ -88,7 +119,7 @@ TEST_CASE_METHOD(WebViewTestCase, "WebView", "[wxWebView]")
//Test title after loading a url, we yield to let events process
LoadUrl();
CHECK(m_browser->GetCurrentTitle() == "");
CHECK(m_browser->GetCurrentTitle() == m_blankTitle);
}
SECTION("URL")
@@ -97,7 +128,7 @@ TEST_CASE_METHOD(WebViewTestCase, "WebView", "[wxWebView]")
//After first loading about:blank the next in the sequence is about:
LoadUrl();
CHECK(m_browser->GetCurrentURL() == "about:");
CHECK(m_browser->GetCurrentURL() == m_alternateHistoryURL);
}
SECTION("History")
@@ -336,9 +367,9 @@ TEST_CASE_METHOD(WebViewTestCase, "WebView", "[wxWebView]")
CHECK(m_browser->RunScript("function f(a){return a;}f(false);", &result));
CHECK(result == "false");
CHECK(m_browser->RunScript("function f(){var person = new Object();person.name = 'Foo'; \
person.lastName = 'Bar';return person;}f();", &result));
CHECK(result == "{\"name\":\"Foo\",\"lastName\":\"Bar\"}");
CHECK(m_browser->RunScript("function f(){var person = new Object();person.lastName = 'Bar'; \
person.name = 'Foo';return person;}f();", &result));
CHECK(result == "{\"lastName\":\"Bar\",\"name\":\"Foo\"}");
CHECK(m_browser->RunScript("function f(){ return [\"foo\", \"bar\"]; }f();", &result));
CHECK(result == "[\"foo\",\"bar\"]");
@@ -381,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("<html><body>text</body></html>", "");