1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/*
* Copyright (C) 2017 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2,1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "TestMain.h"
#include <gio/gio.h>
#include <wtf/UUID.h>
#include <wtf/glib/SocketConnection.h>
#include <wtf/text/StringBuilder.h>
class AutomationTest: public Test {
public:
MAKE_GLIB_TEST_FIXTURE(AutomationTest);
static const SocketConnection::MessageHandlers s_messageHandlers;
AutomationTest()
: m_mainLoop(adoptGRef(g_main_loop_new(nullptr, TRUE)))
{
GRefPtr<GSocketClient> socketClient = adoptGRef(g_socket_client_new());
g_socket_client_connect_to_host_async(socketClient.get(), "127.0.0.1:2229", 0, nullptr, [](GObject* client, GAsyncResult* result, gpointer userData) {
GRefPtr<GSocketConnection> connection = adoptGRef(g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(client), result, nullptr));
static_cast<AutomationTest*>(userData)->setConnection(SocketConnection::create(WTFMove(connection), s_messageHandlers, userData));
}, this);
g_main_loop_run(m_mainLoop.get());
}
~AutomationTest()
{
}
struct Target {
Target() = default;
Target(guint64 id, CString name, bool isPaired)
: id(id)
, name(name)
, isPaired(isPaired)
{
}
guint64 id { 0 };
CString name;
bool isPaired { false };
};
void setConnection(Ref<SocketConnection>&& connection)
{
m_connection = WTFMove(connection);
g_main_loop_quit(m_mainLoop.get());
}
void setTarget(guint64 connectionID, Target&& target)
{
bool newConnection = !m_connectionID;
bool wasPaired = m_target.isPaired;
m_connectionID = connectionID;
m_target = WTFMove(target);
if (newConnection || (!wasPaired && m_target.isPaired))
g_main_loop_quit(m_mainLoop.get());
}
void receivedMessage(guint64 connectionID, guint64 targetID, const char* message)
{
g_assert_cmpuint(connectionID, ==, m_connectionID);
g_assert_cmpuint(targetID, ==, m_target.id);
m_message = message;
g_main_loop_quit(m_mainLoop.get());
}
void sendCommandToBackend(const String& command, const String& parameters = String())
{
static long sequenceID = 0;
StringBuilder messageBuilder;
messageBuilder.append("{\"id\":", ++sequenceID, ",\"method\":\"Automation.", command, '"');
if (!parameters.isNull())
messageBuilder.append(",\"params\":", parameters);
messageBuilder.append('}');
m_connection->sendMessage("SendMessageToBackend", g_variant_new("(tts)", m_connectionID, m_target.id, messageBuilder.toString().utf8().data()));
}
static WebKitWebView* createWebViewCallback(WebKitAutomationSession* session, AutomationTest* test)
{
test->m_createWebViewWasCalled = true;
return test->m_webViewForAutomation;
}
static WebKitWebView* createWebViewInWindowCallback(WebKitAutomationSession* session, AutomationTest* test)
{
test->m_createWebViewInWindowWasCalled = true;
return test->m_webViewForAutomation;
}
static WebKitWebView* createWebViewInTabCallback(WebKitAutomationSession* session, AutomationTest* test)
{
test->m_createWebViewInTabWasCalled = true;
return test->m_webViewForAutomation;
}
void automationStarted(WebKitAutomationSession* session)
{
m_session = session;
assertObjectIsDeletedWhenTestFinishes(G_OBJECT(m_session));
g_assert_null(webkit_automation_session_get_application_info(session));
WebKitApplicationInfo* info = webkit_application_info_new();
webkit_application_info_set_name(info, "AutomationTestBrowser");
webkit_application_info_set_version(info, WEBKIT_MAJOR_VERSION, WEBKIT_MINOR_VERSION, WEBKIT_MICRO_VERSION);
webkit_automation_session_set_application_info(session, info);
webkit_application_info_unref(info);
g_assert_true(webkit_automation_session_get_application_info(session) == info);
}
static void automationStartedCallback(WebKitWebContext* webContext, WebKitAutomationSession* session, AutomationTest* test)
{
g_assert_true(webContext == test->m_webContext.get());
g_assert_true(WEBKIT_IS_AUTOMATION_SESSION(session));
test->automationStarted(session);
}
static GUniquePtr<char> toVersionString(unsigned major, unsigned minor, unsigned micro)
{
if (!micro && !minor)
return GUniquePtr<char>(g_strdup_printf("%u", major));
if (!micro)
return GUniquePtr<char>(g_strdup_printf("%u.%u", major, minor));
return GUniquePtr<char>(g_strdup_printf("%u.%u.%u", major, minor, micro));
}
void didStartAutomationSession(GVariant* capabilities)
{
if (!m_session)
return;
g_assert_nonnull(capabilities);
const char* browserName;
const char* browserVersion;
g_variant_get(capabilities, "(&s&s)", &browserName, &browserVersion);
g_assert_cmpstr(browserName, ==, "AutomationTestBrowser");
GUniquePtr<char> versionString = toVersionString(WEBKIT_MAJOR_VERSION, WEBKIT_MINOR_VERSION, WEBKIT_MICRO_VERSION);
g_assert_cmpstr(browserVersion, ==, versionString.get());
}
WebKitAutomationSession* requestSession(const char* sessionID)
{
auto signalID = g_signal_connect(m_webContext.get(), "automation-started", G_CALLBACK(automationStartedCallback), this);
m_connection->sendMessage("StartAutomationSession", g_variant_new("(sa{sv})", sessionID, nullptr));
auto timeoutID = g_timeout_add(1000, [](gpointer userData) -> gboolean {
g_main_loop_quit(static_cast<GMainLoop*>(userData));
return G_SOURCE_REMOVE;
}, m_mainLoop.get());
g_main_loop_run(m_mainLoop.get());
if (!m_connectionID)
m_session = nullptr;
if (m_session && m_connectionID)
g_source_remove(timeoutID);
g_signal_handler_disconnect(m_webContext.get(), signalID);
return m_session;
}
void setupIfNeeded()
{
if (m_target.isPaired)
return;
g_assert_cmpuint(m_target.id, !=, 0);
m_connection->sendMessage("Setup", g_variant_new("(tt)", m_connectionID, m_target.id));
g_main_loop_run(m_mainLoop.get());
g_assert_true(m_target.isPaired);
}
bool createTopLevelBrowsingContext(WebKitWebView* webView)
{
setupIfNeeded();
m_webViewForAutomation = webView;
m_createWebViewWasCalled = false;
m_message = CString();
auto signalID = g_signal_connect(m_session, "create-web-view", G_CALLBACK(createWebViewCallback), this);
sendCommandToBackend("createBrowsingContext"_s);
g_main_loop_run(m_mainLoop.get());
g_signal_handler_disconnect(m_session, signalID);
g_assert_true(m_createWebViewWasCalled);
g_assert_false(m_message.isNull());
m_webViewForAutomation = nullptr;
if (strstr(m_message.data(), "The remote session failed to create a new browsing context"))
return false;
if (strstr(m_message.data(), "handle"))
return true;
return false;
}
bool createNewWindow(WebKitWebView* webView)
{
m_webViewForAutomation = webView;
m_createWebViewInWindowWasCalled = false;
m_message = { };
auto signalID = g_signal_connect(m_session, "create-web-view::window", G_CALLBACK(createWebViewInWindowCallback), this);
sendCommandToBackend("createBrowsingContext"_s, "{\"presentationHint\":\"Window\"}"_s);
g_main_loop_run(m_mainLoop.get());
g_signal_handler_disconnect(m_session, signalID);
g_assert_true(m_createWebViewInWindowWasCalled);
g_assert_false(m_message.isNull());
m_webViewForAutomation = nullptr;
if (strstr(m_message.data(), "\"presentation\":\"Window\""))
return true;
return false;
}
bool createNewTab(WebKitWebView* webView)
{
m_webViewForAutomation = webView;
m_createWebViewInTabWasCalled = false;
m_message = { };
auto signalID = g_signal_connect(m_session, "create-web-view::tab", G_CALLBACK(createWebViewInTabCallback), this);
sendCommandToBackend("createBrowsingContext"_s, "{\"presentationHint\":\"Tab\"}"_s);
g_main_loop_run(m_mainLoop.get());
g_signal_handler_disconnect(m_session, signalID);
g_assert_true(m_createWebViewInTabWasCalled);
g_assert_false(m_message.isNull());
m_webViewForAutomation = nullptr;
if (strstr(m_message.data(), "\"presentation\":\"Tab\""))
return true;
return false;
}
GRefPtr<GMainLoop> m_mainLoop;
RefPtr<SocketConnection> m_connection;
WebKitAutomationSession* m_session;
guint64 m_connectionID { 0 };
Target m_target;
WebKitWebView* m_webViewForAutomation { nullptr };
bool m_createWebViewWasCalled { false };
bool m_createWebViewInWindowWasCalled { false };
bool m_createWebViewInTabWasCalled { false };
CString m_message;
};
const SocketConnection::MessageHandlers AutomationTest::s_messageHandlers = {
{ "DidClose", std::pair<CString, SocketConnection::MessageCallback> { { },
[](SocketConnection&, GVariant*, gpointer userData) {
auto& test = *static_cast<AutomationTest*>(userData);
test.m_connection = nullptr;
}}
},
{ "DidStartAutomationSession", std::pair<CString, SocketConnection::MessageCallback> { "(ss)",
[](SocketConnection&, GVariant* parameters, gpointer userData) {
auto& test = *static_cast<AutomationTest*>(userData);
test.didStartAutomationSession(parameters);
}}
},
{ "SetTargetList", std::pair<CString, SocketConnection::MessageCallback> { "(ta(tsssb))",
[](SocketConnection&, GVariant* parameters, gpointer userData) {
auto& test = *static_cast<AutomationTest*>(userData);
guint64 connectionID;
GUniqueOutPtr<GVariantIter> iter;
g_variant_get(parameters, "(ta(tsssb))", &connectionID, &iter.outPtr());
guint64 targetID;
const char* type;
const char* name;
const char* dummy;
gboolean isPaired;
while (g_variant_iter_loop(iter.get(), "(t&s&s&sb)", &targetID, &type, &name, &dummy, &isPaired)) {
if (!g_strcmp0(type, "Automation")) {
test.setTarget(connectionID, Target(targetID, name, isPaired));
break;
}
}
}}
},
{ "SendMessageToFrontend", std::pair<CString, SocketConnection::MessageCallback> { "(tts)",
[](SocketConnection&, GVariant* parameters, gpointer userData) {
auto& test = *static_cast<AutomationTest*>(userData);
guint64 connectionID, targetID;
const char* message;
g_variant_get(parameters, "(tt&s)", &connectionID, &targetID, &message);
test.receivedMessage(connectionID, targetID, message);
}}
}
};
static void testAutomationSessionRequestSession(AutomationTest* test, gconstpointer)
{
String sessionID = createVersion4UUIDString();
// WebKitAutomationSession::automation-started is never emitted if automation is not enabled.
g_assert_false(webkit_web_context_is_automation_allowed(test->m_webContext.get()));
auto* session = test->requestSession(sessionID.utf8().data());
g_assert_null(session);
webkit_web_context_set_automation_allowed(test->m_webContext.get(), TRUE);
g_assert_true(webkit_web_context_is_automation_allowed(test->m_webContext.get()));
// There can't be more than one context with automation enabled
GRefPtr<WebKitWebContext> otherContext = adoptGRef(webkit_web_context_new());
Test::removeLogFatalFlag(G_LOG_LEVEL_WARNING);
webkit_web_context_set_automation_allowed(otherContext.get(), TRUE);
Test::addLogFatalFlag(G_LOG_LEVEL_WARNING);
g_assert_false(webkit_web_context_is_automation_allowed(otherContext.get()));
session = test->requestSession(sessionID.utf8().data());
g_assert_cmpstr(webkit_automation_session_get_id(session), ==, sessionID.utf8().data());
g_assert_cmpuint(test->m_target.id, >, 0);
ASSERT_CMP_CSTRING(test->m_target.name, ==, sessionID.utf8());
g_assert_false(test->m_target.isPaired);
// Will fail to create a browsing context when not creating a web view (or not handling the signal).
g_assert_false(test->createTopLevelBrowsingContext(nullptr));
// Will also fail if the web view is not controlled by automation.
auto webView = Test::adoptView(Test::createWebView(test->m_webContext.get()));
g_assert_false(webkit_web_view_is_controlled_by_automation(webView.get()));
g_assert_false(test->createTopLevelBrowsingContext(webView.get()));
// And will work with a proper web view.
webView = Test::adoptView(g_object_new(WEBKIT_TYPE_WEB_VIEW,
#if PLATFORM(WPE)
"backend", Test::createWebViewBackend(),
#endif
"web-context", test->m_webContext.get(),
"is-controlled-by-automation", TRUE,
nullptr));
g_assert_true(webkit_web_view_is_controlled_by_automation(webView.get()));
g_assert_cmpuint(webkit_web_view_get_automation_presentation_type(webView.get()), ==, WEBKIT_AUTOMATION_BROWSING_CONTEXT_PRESENTATION_WINDOW);
g_assert_true(test->createTopLevelBrowsingContext(webView.get()));
auto newWebViewInWindow = Test::adoptView(g_object_new(WEBKIT_TYPE_WEB_VIEW,
#if PLATFORM(WPE)
"backend", Test::createWebViewBackend(),
#endif
"web-context", test->m_webContext.get(),
"is-controlled-by-automation", TRUE,
nullptr));
g_assert_true(webkit_web_view_is_controlled_by_automation(newWebViewInWindow.get()));
g_assert_cmpuint(webkit_web_view_get_automation_presentation_type(newWebViewInWindow.get()), ==, WEBKIT_AUTOMATION_BROWSING_CONTEXT_PRESENTATION_WINDOW);
g_assert_true(test->createNewWindow(newWebViewInWindow.get()));
auto newWebViewInTab = Test::adoptView(g_object_new(WEBKIT_TYPE_WEB_VIEW,
#if PLATFORM(WPE)
"backend", Test::createWebViewBackend(),
#endif
"web-context", test->m_webContext.get(),
"is-controlled-by-automation", TRUE,
"automation-presentation-type", WEBKIT_AUTOMATION_BROWSING_CONTEXT_PRESENTATION_TAB,
nullptr));
g_assert_true(webkit_web_view_is_controlled_by_automation(newWebViewInTab.get()));
g_assert_cmpuint(webkit_web_view_get_automation_presentation_type(newWebViewInTab.get()), ==, WEBKIT_AUTOMATION_BROWSING_CONTEXT_PRESENTATION_TAB);
g_assert_true(test->createNewTab(newWebViewInTab.get()));
webkit_web_context_set_automation_allowed(test->m_webContext.get(), FALSE);
}
static void testAutomationSessionApplicationInfo(Test* test, gconstpointer)
{
WebKitApplicationInfo* info = webkit_application_info_new();
g_assert_cmpstr(webkit_application_info_get_name(info), ==, g_get_prgname());
webkit_application_info_set_name(info, "WebKitGTKBrowser");
g_assert_cmpstr(webkit_application_info_get_name(info), ==, "WebKitGTKBrowser");
webkit_application_info_set_name(info, nullptr);
g_assert_cmpstr(webkit_application_info_get_name(info), ==, g_get_prgname());
guint64 major, minor, micro;
webkit_application_info_get_version(info, &major, nullptr, nullptr);
g_assert_cmpuint(major, ==, 0);
webkit_application_info_set_version(info, 1, 2, 3);
webkit_application_info_get_version(info, &major, &minor, µ);
g_assert_cmpuint(major, ==, 1);
g_assert_cmpuint(minor, ==, 2);
g_assert_cmpuint(micro, ==, 3);
webkit_application_info_unref(info);
}
void beforeAll()
{
g_setenv("WEBKIT_INSPECTOR_SERVER", "127.0.0.1:2229", TRUE);
AutomationTest::add("WebKitAutomationSession", "request-session", testAutomationSessionRequestSession);
Test::add("WebKitAutomationSession", "application-info", testAutomationSessionApplicationInfo);
}
void afterAll()
{
g_unsetenv("WEBKIT_INSPECTOR_SERVER");
}
|