Source/WebKit2/ChangeLog

112012-07-18 Christophe Dumez <christophe.dumez@intel.com>
22
 3 [EFL][WK2] Add unit tests for Ewk_Cookie_Manager
 4 https://bugs.webkit.org/show_bug.cgi?id=91639
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 Add unit tests for Ewk_Cookie_Manager.
 9
 10 * PlatformEfl.cmake:
 11 * UIProcess/API/efl/ewk_cookie_manager.h: Fix "accept policy" documentation
 12 to indicate that only cookies for the main page are accepted by default.
 13 * UIProcess/API/efl/tests/UnitTestUtils/EWK2UnitTestServer.cpp: Added.
 14 (EWK2UnitTestServer::EWK2UnitTestServer): Add HTTP server similar to the one
 15 used by GTK port unit tests.
 16 (EWK2UnitTestServer::~EWK2UnitTestServer):
 17 (EWK2UnitTestServer::run):
 18 (EWK2UnitTestServer::getURIForPath):
 19 * UIProcess/API/efl/tests/UnitTestUtils/EWK2UnitTestServer.h: Added.
 20 (EWK2UnitTestServer):
 21 (EWK2UnitTestServer::baseURI):
 22 * UIProcess/API/efl/tests/test_ewk2_cookie_manager.cpp: Added.
 23 (serverCallback):
 24 (getAcceptPolicyCallback):
 25 (getAcceptPolicy):
 26 (getHostnamesWithCookiesCallback):
 27 (getHostnamesWithCookies):
 28 (freeHostNames):
 29 (countHostnamesWithCookies):
 30 (TEST_F):
 31 (cleanUpCookieFiles):
 32
 332012-07-18 Christophe Dumez <christophe.dumez@intel.com>
 34
335 [EFL][WK2] EFL should use DownloadSoup instead of defining DownloadEfl
436 https://bugs.webkit.org/show_bug.cgi?id=91602
537

Source/WebKit2/PlatformEfl.cmake

@@LIST(APPEND WebKit2_LINK_FLAGS
44 ${EFLDEPS_LDFLAGS}
55 ${EVAS_LDFLAGS}
66 ${LIBSOUP24_LDFLAGS}
 7 ${CAIRO_LDFLAGS}
78)
89
910LIST(APPEND WebKit2_SOURCES

@@SET(EWK2UnitTests_LIBRARIES
197198 ${ECORE_LIBRARIES}
198199 ${ECORE_EVAS_LIBRARIES}
199200 ${EVAS_LIBRARIES}
 201 ${LIBSOUP24_LIBRARIES}
200202 gtest
201203)
202204

@@ADD_LIBRARY(ewk2UnitTestUtils
219221 ${WEBKIT2_EFL_TEST_DIR}/UnitTestUtils/EWK2UnitTestBase.cpp
220222 ${WEBKIT2_EFL_TEST_DIR}/UnitTestUtils/EWK2UnitTestEnvironment.cpp
221223 ${WEBKIT2_EFL_TEST_DIR}/UnitTestUtils/EWK2UnitTestMain.cpp
 224 ${WEBKIT2_EFL_TEST_DIR}/UnitTestUtils/EWK2UnitTestServer.cpp
222225)
223226
224227TARGET_LINK_LIBRARIES(ewk2UnitTestUtils ${EWK2UnitTests_LIBRARIES})

@@TARGET_LINK_LIBRARIES(ewk2UnitTestUtils ${EWK2UnitTests_LIBRARIES})
226229# The "ewk" on the test name needs to be suffixed with "2", otherwise it
227230# will clash with tests from the WebKit 1 test suite.
228231SET(EWK2UnitTests_BINARIES
 232 test_ewk2_cookie_manager
229233 test_ewk2_view
230234)
231235

Source/WebKit2/UIProcess/API/efl/ewk_cookie_manager.h

@@EAPI void ewk_cookie_manager_persistent_storage_set(Ewk_Cookie_Manager *manager,
107107/**
108108 * Set @a policy as the cookie acceptance policy for @a manager.
109109 *
110  * By default, cookies are always accepted.
 110 * By default, cookies are accepted only for the main page.
111111 *
112112 * @param manager The cookie manager to update.
113113 * @param policy a #Ewk_Cookie_Accept_Policy

@@EAPI void ewk_cookie_manager_accept_policy_set(Ewk_Cookie_Manager *manager, Ewk_
118118/**
119119 * Asynchronously get the cookie acceptance policy of @a manager.
120120 *
121  * By default, cookies are always accepted.
 121 * By default, cookies are accepted only for the main page.
122122 *
123123 * @param manager The cookie manager to query.
124124 * @param callback The function to call when the policy is received or an error occured.

Source/WebKit2/UIProcess/API/efl/tests/UnitTestUtils/EWK2UnitTestServer.cpp

 1/*
 2 * Copyright (C) 2011 Igalia S.L.
 3 * Copyright (C) 2012 Intel Corporation
 4 *
 5 * This library is free software; you can redistribute it and/or
 6 * modify it under the terms of the GNU Library General Public
 7 * License as published by the Free Software Foundation; either
 8 * version 2 of the License, or (at your option) any later version.
 9 *
 10 * This library is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13 * Library General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU Library General Public License
 16 * along with this library; see the file COPYING.LIB. If not, write to
 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 18 * Boston, MA 02110-1301, USA.
 19 */
 20
 21#include "config.h"
 22#include "EWK2UnitTestServer.h"
 23
 24EWK2UnitTestServer::EWK2UnitTestServer()
 25{
 26 SoupAddress* address = soup_address_new("127.0.0.1", SOUP_ADDRESS_ANY_PORT);
 27 soup_address_resolve_sync(address, 0);
 28
 29 m_soupServer = soup_server_new(SOUP_SERVER_INTERFACE, address, NULL);
 30 m_baseURI = soup_uri_new("http://127.0.0.1/");
 31 soup_uri_set_port(m_baseURI, soup_server_get_port(m_soupServer));
 32 g_object_unref(address);
 33}
 34
 35EWK2UnitTestServer::~EWK2UnitTestServer()
 36{
 37 soup_uri_free(m_baseURI);
 38 g_object_unref(m_soupServer);
 39}
 40
 41void EWK2UnitTestServer::run(SoupServerCallback serverCallback)
 42{
 43 soup_server_run_async(m_soupServer);
 44 soup_server_add_handler(m_soupServer, 0, serverCallback, 0, 0);
 45}
 46
 47CString EWK2UnitTestServer::getURIForPath(const char* path) const
 48{
 49 SoupURI* uri = soup_uri_new_with_base(m_baseURI, path);
 50 char* uriString = soup_uri_to_string(uri, FALSE);
 51 CString ret = uriString;
 52 free(uriString);
 53 soup_uri_free(uri);
 54
 55 return ret;
 56}
 57
 58

Source/WebKit2/UIProcess/API/efl/tests/UnitTestUtils/EWK2UnitTestServer.h

 1/*
 2 * Copyright (C) 2011 Igalia S.L.
 3 * Copyright (C) 2012 Intel Corporation
 4 *
 5 * This library is free software; you can redistribute it and/or
 6 * modify it under the terms of the GNU Library General Public
 7 * License as published by the Free Software Foundation; either
 8 * version 2 of the License, or (at your option) any later version.
 9 *
 10 * This library is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13 * Library General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU Library General Public License
 16 * along with this library; see the file COPYING.LIB. If not, write to
 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 18 * Boston, MA 02110-1301, USA.
 19 */
 20
 21#ifndef EWK2UnitTestServer_h
 22#define EWK2UnitTestServer_h
 23
 24#include <libsoup/soup.h>
 25#include <wtf/text/CString.h>
 26
 27class EWK2UnitTestServer {
 28public:
 29 EWK2UnitTestServer();
 30 virtual ~EWK2UnitTestServer();
 31
 32 SoupURI* baseURI() const { return m_baseURI; }
 33
 34 CString getURIForPath(const char* path) const;
 35 void run(SoupServerCallback);
 36
 37private:
 38 SoupServer* m_soupServer;
 39 SoupURI* m_baseURI;
 40};
 41
 42#endif // EWK2UnitTestServer_h
 43

Source/WebKit2/UIProcess/API/efl/tests/test_ewk2_cookie_manager.cpp

 1/*
 2 * Copyright (C) 2012 Igalia S.L.
 3 * Copyright (C) 2012 Intel Corporation
 4 *
 5 * This library is free software; you can redistribute it and/or
 6 * modify it under the terms of the GNU Library General Public
 7 * License as published by the Free Software Foundation; either
 8 * version 2 of the License, or (at your option) any later version.
 9 *
 10 * This library is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13 * Library General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU Library General Public License
 16 * along with this library; see the file COPYING.LIB. If not, write to
 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 18 * Boston, MA 02110-1301, USA.
 19 */
 20
 21#include "config.h"
 22
 23#include "UnitTestUtils/EWK2UnitTestBase.h"
 24#include "UnitTestUtils/EWK2UnitTestEnvironment.h"
 25#include "UnitTestUtils/EWK2UnitTestServer.h"
 26#include <EWebKit2.h>
 27#include <Ecore.h>
 28#include <Eina.h>
 29#include <unistd.h>
 30
 31using namespace EWK2UnitTest;
 32
 33extern EWK2UnitTestEnvironment* environment;
 34
 35static const char FIRST_PARTY_DOMAIN[] = "127.0.0.1";
 36static const char THIRD_PARTY_DOMAIN[] = "localhost";
 37static const char TMP_COOKIE_FILE_TXT[] = "/tmp/cookies.txt";
 38static const char TMP_COOKIE_FILE_SQLITE[] = "/tmp/cookies.db";
 39static const char INDEX_HTML_STRING[] =
 40 "<html><body>"
 41 " <p>EFLWebKit2 Cookie Manager test</p>"
 42 " <img src='http://localhost:%u/image.png' width=5 height=5></img>"
 43 "</body></html>";
 44
 45static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
 46{
 47 if (message->method != SOUP_METHOD_GET) {
 48 soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
 49 return;
 50 }
 51
 52 soup_message_set_status(message, SOUP_STATUS_OK);
 53 if (!strcmp(path, "/index.html")) {
 54 Eina_Strbuf* buffer = eina_strbuf_new();
 55 eina_strbuf_append_printf(buffer, INDEX_HTML_STRING, soup_server_get_port(server));
 56 soup_message_headers_replace(message->response_headers, "Set-Cookie", "foo=bar; Max-Age=60");
 57 soup_message_body_append(message->response_body, SOUP_MEMORY_TAKE, eina_strbuf_string_steal(buffer), eina_strbuf_length_get(buffer));
 58 eina_strbuf_string_free(buffer);
 59 } else if (!strcmp(path, "/image.png"))
 60 soup_message_headers_replace(message->response_headers, "Set-Cookie", "baz=qux; Max-Age=60");
 61 else
 62 FAIL();
 63 soup_message_body_complete(message->response_body);
 64}
 65
 66static void getAcceptPolicyCallback(Ewk_Cookie_Accept_Policy policy, Ewk_Web_Error* error, void* event_info)
 67{
 68 ASSERT(!error);
 69 Ewk_Cookie_Accept_Policy* ret = static_cast<Ewk_Cookie_Accept_Policy*>(event_info);
 70 *ret = policy;
 71 ecore_main_loop_quit();
 72}
 73
 74static Ewk_Cookie_Accept_Policy getAcceptPolicy(Ewk_Cookie_Manager* manager)
 75{
 76 Ewk_Cookie_Accept_Policy policy = EWK_COOKIE_ACCEPT_POLICY_ALWAYS;
 77 ewk_cookie_manager_async_accept_policy_get(manager, getAcceptPolicyCallback, &policy);
 78 ecore_main_loop_begin();
 79 return policy;
 80}
 81
 82static void getHostnamesWithCookiesCallback(Eina_List* hostnames, Ewk_Web_Error* error, void* event_info)
 83{
 84 ASSERT(!error);
 85
 86 Eina_List** ret = static_cast<Eina_List**>(event_info);
 87 Eina_List* l;
 88 void* data;
 89 EINA_LIST_FOREACH(hostnames, l, data)
 90 *ret = eina_list_append(*ret, eina_stringshare_ref(static_cast<char*>(data)));
 91 ecore_main_loop_quit();
 92}
 93
 94static Eina_List* getHostnamesWithCookies(Ewk_Cookie_Manager* manager)
 95{
 96 Eina_List* ret = 0;
 97 ewk_cookie_manager_async_hostnames_with_cookies_get(manager, getHostnamesWithCookiesCallback, &ret);
 98 ecore_main_loop_begin();
 99 return ret;
 100}
 101
 102static void freeHostNames(Eina_List* hostnames)
 103{
 104 void* data;
 105 EINA_LIST_FREE(hostnames, data)
 106 eina_stringshare_del(static_cast<char*>(data));
 107}
 108
 109static int countHostnamesWithCookies(Ewk_Cookie_Manager* manager)
 110{
 111 Eina_List* hostnames = getHostnamesWithCookies(manager);
 112 int count = eina_list_count(hostnames);
 113 freeHostNames(hostnames);
 114 return count;
 115}
 116
 117TEST_F(EWK2UnitTestBase, ewk_cookie_manager_accept_policy)
 118{
 119 g_type_init();
 120 ecore_main_loop_glib_integrate();
 121
 122 EWK2UnitTestServer* httpServer = new EWK2UnitTestServer();
 123 httpServer->run(serverCallback);
 124
 125 Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_context_default_get());
 126 ASSERT(cookieManager);
 127
 128 // Default policy is EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY.
 129 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY);
 130 loadUrlSync(httpServer->getURIForPath("/index.html").data());
 131
 132 Eina_List* hostnames = getHostnamesWithCookies(cookieManager);
 133 ASSERT_EQ(eina_list_count(hostnames), 1);
 134 ASSERT_STREQ(static_cast<char*>(eina_list_nth(hostnames, 0)), FIRST_PARTY_DOMAIN);
 135 freeHostNames(hostnames);
 136 ewk_cookie_manager_cookies_clear(cookieManager);
 137
 138 // Change policy to EWK_COOKIE_ACCEPT_POLICY_ALWAYS
 139 ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
 140 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
 141 loadUrlSync(httpServer->getURIForPath("/index.html").data());
 142
 143 hostnames = getHostnamesWithCookies(cookieManager);
 144 ASSERT_EQ(eina_list_count(hostnames), 2);
 145 ASSERT_STREQ(static_cast<char*>(eina_list_nth(hostnames, 0)), FIRST_PARTY_DOMAIN);
 146 ASSERT_STREQ(static_cast<char*>(eina_list_nth(hostnames, 1)), THIRD_PARTY_DOMAIN);
 147 freeHostNames(hostnames);
 148 ewk_cookie_manager_cookies_clear(cookieManager);
 149
 150 // Change policy to EWK_COOKIE_ACCEPT_POLICY_NEVER
 151 ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_NEVER);
 152 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_NEVER);
 153 loadUrlSync(httpServer->getURIForPath("/index.html").data());
 154 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
 155
 156 delete httpServer;
 157}
 158
 159TEST_F(EWK2UnitTestBase, ewk_cookie_manager_cookies_delete)
 160{
 161 g_type_init();
 162 ecore_main_loop_glib_integrate();
 163
 164 EWK2UnitTestServer* httpServer = new EWK2UnitTestServer();
 165 httpServer->run(serverCallback);
 166
 167 Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_context_default_get());
 168 ASSERT(cookieManager);
 169
 170 ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
 171 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
 172
 173 loadUrlSync(httpServer->getURIForPath("/index.html").data());
 174 Eina_List* hostnames = getHostnamesWithCookies(cookieManager);
 175 ASSERT_EQ(eina_list_count(hostnames), 2);
 176 freeHostNames(hostnames);
 177
 178 // Delete first party cookie
 179 ewk_cookie_manager_hostname_cookies_clear(cookieManager, FIRST_PARTY_DOMAIN);
 180 hostnames = getHostnamesWithCookies(cookieManager);
 181 ASSERT_EQ(eina_list_count(hostnames), 1);
 182 ASSERT_STREQ(static_cast<char*>(eina_list_nth(hostnames, 0)), THIRD_PARTY_DOMAIN);
 183 freeHostNames(hostnames);
 184
 185 // Delete third party cookie
 186 ewk_cookie_manager_hostname_cookies_clear(cookieManager, THIRD_PARTY_DOMAIN);
 187 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
 188
 189 // Get all cookies again
 190 loadUrlSync(httpServer->getURIForPath("/index.html").data());
 191 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
 192
 193 // Clear all cookies
 194 ewk_cookie_manager_cookies_clear(cookieManager);
 195 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
 196
 197 delete httpServer;
 198}
 199
 200static void cleanUpCookieFiles()
 201{
 202 unlink(TMP_COOKIE_FILE_TXT);
 203 unlink(TMP_COOKIE_FILE_SQLITE);
 204}
 205
 206TEST_F(EWK2UnitTestBase, ewk_cookie_manager_permanent_storage)
 207{
 208 g_type_init();
 209 ecore_main_loop_glib_integrate();
 210
 211 EWK2UnitTestServer* httpServer = new EWK2UnitTestServer();
 212 httpServer->run(serverCallback);
 213
 214 // initial clean up
 215 cleanUpCookieFiles();
 216
 217 Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_context_default_get());
 218 ASSERT(cookieManager);
 219
 220 ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
 221 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
 222
 223 // Text storage using a new file.
 224 ewk_cookie_manager_persistent_storage_set(cookieManager, TMP_COOKIE_FILE_TXT, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
 225 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
 226
 227 loadUrlSync(httpServer->getURIForPath("/index.html").data());
 228 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
 229
 230 // SQLite storage using a new file.
 231 ewk_cookie_manager_persistent_storage_set(cookieManager, TMP_COOKIE_FILE_SQLITE, EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
 232 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
 233
 234 loadUrlSync(httpServer->getURIForPath("/index.html").data());
 235 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
 236
 237 // Text storage using an existing file.
 238 ewk_cookie_manager_persistent_storage_set(cookieManager, TMP_COOKIE_FILE_TXT, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
 239 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
 240 ewk_cookie_manager_cookies_clear(cookieManager);
 241 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
 242
 243 // SQLite storage with an existing file.
 244 ewk_cookie_manager_persistent_storage_set(cookieManager, TMP_COOKIE_FILE_SQLITE, EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
 245 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
 246 ewk_cookie_manager_cookies_clear(cookieManager);
 247 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
 248
 249 // Final clean up
 250 cleanUpCookieFiles();
 251}
 252