8eaf8e02f7f2834bd3372ef7ea9d69545d228169
[WebKit-https.git] / Tools / TestWebKitAPI / gtk / WebKit2Gtk / TestMain.h
1 /*
2  * Copyright (C) 2011 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 #ifndef TestMain_h
21 #define TestMain_h
22
23 #include <cairo.h>
24 #include <glib-object.h>
25 #include <webkit2/webkit2.h>
26 #include <wtf/HashSet.h>
27 #include <wtf/glib/GRefPtr.h>
28 #include <wtf/glib/GUniquePtr.h>
29 #include <wtf/text/CString.h>
30
31 #define MAKE_GLIB_TEST_FIXTURE(ClassName) \
32     static void setUp(ClassName* fixture, gconstpointer data) \
33     { \
34         new (fixture) ClassName; \
35     } \
36     static void tearDown(ClassName* fixture, gconstpointer data) \
37     { \
38         fixture->~ClassName(); \
39     } \
40     static void add(const char* suiteName, const char* testName, void (*testFunc)(ClassName*, const void*)) \
41     { \
42         GUniquePtr<gchar> testPath(g_strdup_printf("/webkit2/%s/%s", suiteName, testName)); \
43         g_test_add(testPath.get(), ClassName, 0, ClassName::setUp, testFunc, ClassName::tearDown); \
44     }
45
46 #define ASSERT_CMP_CSTRING(s1, cmp, s2) \
47     do {                                                                 \
48         CString __s1 = (s1);                                             \
49         CString __s2 = (s2);                                             \
50         if (g_strcmp0(__s1.data(), __s2.data()) cmp 0) ;                 \
51         else                                                             \
52             g_assertion_message_cmpstr(G_LOG_DOMAIN, __FILE__, __LINE__, \
53                 G_STRFUNC, #s1 " " #cmp " " #s2, __s1.data(), #cmp, __s2.data()); \
54     } while (0)
55
56
57 class Test {
58 public:
59     MAKE_GLIB_TEST_FIXTURE(Test);
60
61     static const char* dataDirectory();
62
63     static void initializeWebExtensionsCallback(WebKitWebContext* context, Test* test)
64     {
65         test->initializeWebExtensions();
66     }
67
68     Test()
69     {
70         GUniquePtr<char> localStorageDirectory(g_build_filename(dataDirectory(), "local-storage", nullptr));
71         GUniquePtr<char> indexedDBDirectory(g_build_filename(dataDirectory(), "indexeddb", nullptr));
72         m_webContext = adoptGRef(WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT,
73             "local-storage-directory", localStorageDirectory.get(),
74             "indexed-db-directory", indexedDBDirectory.get(),
75             nullptr)));
76
77         g_signal_connect(m_webContext.get(), "initialize-web-extensions", G_CALLBACK(initializeWebExtensionsCallback), this);
78         GUniquePtr<char> diskCacheDirectory(g_build_filename(dataDirectory(), "disk-cache", nullptr));
79         webkit_web_context_set_disk_cache_directory(m_webContext.get(), diskCacheDirectory.get());
80     }
81
82     ~Test()
83     {
84         g_signal_handlers_disconnect_matched(m_webContext.get(), G_SIGNAL_MATCH_DATA, 0, 0, nullptr, nullptr, this);
85         if (m_watchedObjects.isEmpty())
86             return;
87
88         g_print("Leaked objects:");
89         HashSet<GObject*>::const_iterator end = m_watchedObjects.end();
90         for (HashSet<GObject*>::const_iterator it = m_watchedObjects.begin(); it != end; ++it)
91             g_print(" %s(%p)", g_type_name_from_instance(reinterpret_cast<GTypeInstance*>(*it)), *it);
92         g_print("\n");
93
94         g_assert(m_watchedObjects.isEmpty());
95     }
96
97     virtual void initializeWebExtensions()
98     {
99         webkit_web_context_set_web_extensions_directory(m_webContext.get(), WEBKIT_TEST_WEB_EXTENSIONS_DIR);
100         webkit_web_context_set_web_extensions_initialization_user_data(m_webContext.get(), g_variant_new_uint32(++s_webExtensionID));
101     }
102
103     static void objectFinalized(Test* test, GObject* finalizedObject)
104     {
105         test->m_watchedObjects.remove(finalizedObject);
106     }
107
108     void assertObjectIsDeletedWhenTestFinishes(GObject* object)
109     {
110         m_watchedObjects.add(object);
111         g_object_weak_ref(object, reinterpret_cast<GWeakNotify>(objectFinalized), this);
112     }
113
114
115     enum ResourcesDir {
116         WebKit2GTKResources,
117         WebKit2Resources,
118     };
119
120     static CString getResourcesDir(ResourcesDir resourcesDir = WebKit2GTKResources)
121     {
122         switch (resourcesDir) {
123         case WebKit2GTKResources: {
124             GUniquePtr<char> resourcesDir(g_build_filename(WEBKIT_SRC_DIR, "Tools", "TestWebKitAPI", "Tests", "WebKit2Gtk", "resources", nullptr));
125             return resourcesDir.get();
126         }
127         case WebKit2Resources: {
128             GUniquePtr<char> resourcesDir(g_build_filename(WEBKIT_SRC_DIR, "Tools", "TestWebKitAPI", "Tests", "WebKit2", nullptr));
129             return resourcesDir.get();
130         }
131         }
132     }
133
134     void addLogFatalFlag(unsigned flag)
135     {
136         unsigned fatalMask = g_log_set_always_fatal(static_cast<GLogLevelFlags>(G_LOG_FATAL_MASK));
137         fatalMask |= flag;
138         g_log_set_always_fatal(static_cast<GLogLevelFlags>(fatalMask));
139     }
140
141     void removeLogFatalFlag(unsigned flag)
142     {
143         unsigned fatalMask = g_log_set_always_fatal(static_cast<GLogLevelFlags>(G_LOG_FATAL_MASK));
144         fatalMask &= ~flag;
145         g_log_set_always_fatal(static_cast<GLogLevelFlags>(fatalMask));
146     }
147
148     static bool cairoSurfacesEqual(cairo_surface_t* s1, cairo_surface_t* s2)
149     {
150         return (cairo_image_surface_get_format(s1) == cairo_image_surface_get_format(s2)
151             && cairo_image_surface_get_width(s1) == cairo_image_surface_get_width(s2)
152             && cairo_image_surface_get_height(s1) == cairo_image_surface_get_height(s2)
153             && cairo_image_surface_get_stride(s1) == cairo_image_surface_get_stride(s2)
154             && !memcmp(const_cast<const void*>(reinterpret_cast<void*>(cairo_image_surface_get_data(s1))),
155                 const_cast<const void*>(reinterpret_cast<void*>(cairo_image_surface_get_data(s2))),
156                 cairo_image_surface_get_height(s1)*cairo_image_surface_get_stride(s1)));
157     }
158
159     HashSet<GObject*> m_watchedObjects;
160     GRefPtr<WebKitWebContext> m_webContext;
161     static uint32_t s_webExtensionID;
162 };
163
164 #endif // TestMain_h