[GLIB] Use GUniquePtr instead of GOwnPtr
[WebKit-https.git] / Tools / TestWebKitAPI / gtk / WebKit2Gtk / WebProcessTestRunner.cpp
1 /*
2  * Copyright (C) 2013 Igalia S.L.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "WebProcessTestRunner.h"
22
23 #include <wtf/gobject/GUniquePtr.h>
24
25 WebProcessTestRunner::WebProcessTestRunner()
26     : m_mainLoop(g_main_loop_new(0, TRUE))
27     , m_bus(adoptGRef(g_test_dbus_new(G_TEST_DBUS_NONE)))
28 {
29     // Save the DISPLAY env var to restore it after calling g_test_dbus_up() that unsets it.
30     // See https://bugs.webkit.org/show_bug.cgi?id=125621.
31     const char* display = g_getenv("DISPLAY");
32     g_test_dbus_up(m_bus.get());
33     g_setenv("DISPLAY", display, FALSE);
34     m_connection = adoptGRef(g_bus_get_sync(G_BUS_TYPE_SESSION, 0, 0));
35 }
36
37 WebProcessTestRunner::~WebProcessTestRunner()
38 {
39     g_main_loop_unref(m_mainLoop);
40
41     // g_test_dbus_down waits until the connection is freed, so release our refs explicitly before calling it.
42     m_connection = 0;
43     m_proxy = 0;
44     g_test_dbus_down(m_bus.get());
45 }
46
47 void WebProcessTestRunner::proxyCreatedCallback(GObject*, GAsyncResult* result, WebProcessTestRunner* testRunner)
48 {
49     testRunner->m_proxy = adoptGRef(g_dbus_proxy_new_finish(result, 0));
50     g_main_loop_quit(testRunner->m_mainLoop);
51 }
52
53 GDBusProxy* WebProcessTestRunner::proxy()
54 {
55     if (m_proxy)
56         return m_proxy.get();
57
58     g_dbus_proxy_new(m_connection.get(), G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, 0,
59         "org.webkit.gtk.WebProcessTest", "/org/webkit/gtk/WebProcessTest", "org.webkit.gtk.WebProcessTest",
60         0, reinterpret_cast<GAsyncReadyCallback>(WebProcessTestRunner::proxyCreatedCallback), this);
61     g_main_loop_run(m_mainLoop);
62     g_assert(m_proxy.get());
63
64     return m_proxy.get();
65 }
66
67 void WebProcessTestRunner::onNameAppeared(GDBusConnection*, const char*, const char*, gpointer userData)
68 {
69     WebProcessTestRunner* testRunner = static_cast<WebProcessTestRunner*>(userData);
70     g_main_loop_quit(testRunner->m_mainLoop);
71 }
72
73 void WebProcessTestRunner::onNameVanished(GDBusConnection*, const char* name, gpointer userData)
74 {
75     _exit(1);
76 }
77
78 void WebProcessTestRunner::testFinishedCallback(GDBusProxy* proxy, GAsyncResult* result, WebProcessTestRunner* testRunner)
79 {
80     GRefPtr<GVariant> returnValue = adoptGRef(g_dbus_proxy_call_finish(proxy, result, 0));
81     g_assert(returnValue.get());
82     gboolean testResult;
83     g_variant_get(returnValue.get(), "(b)", &testResult);
84     testRunner->finishTest(testResult);
85 }
86
87 bool WebProcessTestRunner::runTest(const char* suiteName, const char* testName, GVariant* args)
88 {
89     g_assert(g_variant_is_of_type(args, G_VARIANT_TYPE_VARDICT));
90
91     unsigned watcherID = g_bus_watch_name_on_connection(m_connection.get(), "org.webkit.gtk.WebProcessTest", G_BUS_NAME_WATCHER_FLAGS_NONE,
92         WebProcessTestRunner::onNameAppeared, WebProcessTestRunner::onNameVanished, this, 0);
93     g_main_loop_run(m_mainLoop);
94
95     m_testResult = false;
96     GUniquePtr<char> testPath(g_strdup_printf("%s/%s", suiteName, testName));
97     g_dbus_proxy_call(
98         proxy(),
99         "RunTest",
100         g_variant_new("(s@a{sv})", testPath.get(), args),
101         G_DBUS_CALL_FLAGS_NONE,
102         -1, 0,
103         reinterpret_cast<GAsyncReadyCallback>(WebProcessTestRunner::testFinishedCallback),
104         this);
105     g_main_loop_run(m_mainLoop);
106     g_bus_unwatch_name(watcherID);
107
108     return m_testResult;
109 }
110
111 void WebProcessTestRunner::finishTest(bool result)
112 {
113     m_testResult = result;
114     g_main_loop_quit(m_mainLoop);
115 }