[GTK] Add methods to add a user style sheet to the WebKit2 GTK+ API
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / gtk / tests / TestWebKitWebViewGroup.cpp
index cbc1d23..026c5c5 100644 (file)
 
 #include "config.h"
 
+#include "WebKitTestServer.h"
 #include "WebViewTest.h"
+#include <cstdarg>
 #include <gtk/gtk.h>
 #include <webkit2/webkit2.h>
 #include <wtf/gobject/GRefPtr.h>
 
+static WebKitTestServer* kServer;
+
+// These are all here so that they can be changed easily, if necessary.
+static const char* kStyleSheetHTML = "<html><div id=\"styledElement\">Sweet stylez!</div></html>";
+static const char* kInjectedStyleSheet = "#styledElement { font-weight: bold; }";
+static const char* kStyleSheetTestScript = "getComputedStyle(document.getElementById('styledElement'))['font-weight']";
+static const char* kStyleSheetTestScriptResult = "bold";
+
 static void testWebViewGroupDefault(Test* test, gconstpointer)
 {
     // Default group is shared by all WebViews by default.
@@ -88,15 +98,109 @@ static void testWebViewGroupSettings(Test* test, gconstpointer)
     g_assert(webkit_web_view_group_get_settings(webkit_web_view_get_group(webView1.get())) == webView2Settings);
 }
 
+static bool isStyleSheetInjectedForURLAtPath(WebViewTest* test, const char* path)
+{
+    test->loadURI(kServer->getURIForPath(path).data());
+    test->waitUntilLoadFinished();
+
+    GOwnPtr<GError> error;
+    WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished(kStyleSheetTestScript, &error.outPtr());
+    g_assert(javascriptResult);
+    g_assert(!error.get());
+
+    GOwnPtr<char> resultString(WebViewTest::javascriptResultToCString(javascriptResult));
+    return !g_strcmp0(resultString.get(), kStyleSheetTestScriptResult);
+}
+
+static void fillURLListFromPaths(char** list, const char* path, ...)
+{
+    va_list argumentList;
+    va_start(argumentList, path);
+
+    int i = 0;
+    while (path) {
+        // FIXME: We must use a wildcard for the host here until http://wkbug.com/112476 is fixed.
+        // Until that time patterns with port numbers in them will not properly match URLs with port numbers.
+        list[i++] = g_strdup_printf("http://*/%s*", path);
+        path = va_arg(argumentList, const char*);
+    }
+}
+
+static void removeOldInjectedStyleSheetsAndResetLists(WebKitWebViewGroup* group, char** whitelist, char** blacklist)
+{
+    webkit_web_view_group_remove_all_user_style_sheets(group);
+
+    while (*whitelist) {
+        g_free(*whitelist);
+        *whitelist = 0;
+        whitelist++;
+    }
+
+    while (*blacklist) {
+        g_free(*blacklist);
+        *blacklist = 0;
+        blacklist++;
+    }
+}
+
+static void testWebViewGroupInjectedStyleSheet(WebViewTest* test, gconstpointer)
+{
+    WebKitWebViewGroup* group = webkit_web_view_get_group(test->m_webView);
+    char* whitelist[3] = { 0, 0, 0 };
+    char* blacklist[3] = { 0, 0, 0 };
+
+    removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist);
+
+    // Without a whitelist or a blacklist all URLs should have the injected style sheet.
+    static const char* randomPath = "somerandompath";
+    g_assert(!isStyleSheetInjectedForURLAtPath(test, randomPath));
+    webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, 0, 0, WEBKIT_INJECTED_CONTENT_FRAMES_ALL);
+    g_assert(isStyleSheetInjectedForURLAtPath(test, randomPath));
+
+    removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist);
+
+    fillURLListFromPaths(blacklist, randomPath, 0);
+    webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, 0, blacklist, WEBKIT_INJECTED_CONTENT_FRAMES_ALL);
+    g_assert(!isStyleSheetInjectedForURLAtPath(test, randomPath));
+    g_assert(isStyleSheetInjectedForURLAtPath(test, "someotherrandompath"));
+
+    removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist);
+
+    static const char* inTheWhiteList = "inthewhitelist";
+    static const char* notInWhitelist = "notinthewhitelist";
+    static const char* inTheWhiteListAndBlackList = "inthewhitelistandblacklist";
+
+    fillURLListFromPaths(whitelist, inTheWhiteList, inTheWhiteListAndBlackList, 0);
+    fillURLListFromPaths(blacklist, inTheWhiteListAndBlackList, 0);
+    webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, whitelist, blacklist, WEBKIT_INJECTED_CONTENT_FRAMES_ALL);
+    g_assert(isStyleSheetInjectedForURLAtPath(test, inTheWhiteList));
+    g_assert(!isStyleSheetInjectedForURLAtPath(test, inTheWhiteListAndBlackList));
+    g_assert(!isStyleSheetInjectedForURLAtPath(test, notInWhitelist));
+
+    // It's important to clean up the environment before other tests.
+    removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist);
+}
+
+static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
+{
+    soup_message_set_status(message, SOUP_STATUS_OK);
+    soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, kStyleSheetHTML, strlen(kStyleSheetHTML));
+    soup_message_body_complete(message->response_body);
+}
+
 void beforeAll()
 {
+    kServer = new WebKitTestServer();
+    kServer->run(serverCallback);
+
     Test::add("WebKitWebViewGroup", "default-group", testWebViewGroupDefault);
     Test::add("WebKitWebViewGroup", "new-group", testWebViewGroupNewGroup);
     Test::add("WebKitWebView", "new-with-group", testWebViewNewWithGroup);
     Test::add("WebKitWebViewGroup", "settings", testWebViewGroupSettings);
+    WebViewTest::add("WebKitWebViewGroup", "injected-style-sheet", testWebViewGroupInjectedStyleSheet);
 }
 
 void afterAll()
 {
-
+    delete kServer;
 }