1/*
2 * Copyright (C) 2014 Samsung Electronics
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include "UnitTestUtils/EWK2UnitTestBase.h"
29#include "UnitTestUtils/EWK2UnitTestEnvironment.h"
30#include "UnitTestUtils/EWK2UnitTestServer.h"
31#include <EWebKit2.h>
32
33using namespace EWK2UnitTest;
34
35static const char applicationCacheHtml[] =
36    "<!doctype html>"
37    "<html manifest='test.appcache'>"
38    "<head><link rel='stylesheet' href='clock.css'></head>"
39    "<body>Application Cache Test</body>"
40    "</html>";
41static const char applicationCacheManifest[] = "CACHE MANIFEST\n"
42    "application_cache.html\n"
43    "clock.css";
44static const char applicationCacheCss[] = "output { font:2em sans-serif; }";
45static const char applicationCacheHtmlFile[] = "/application_cache.html";
46static const char applicationCacheManifestFile[] = "/test.appcache";
47static const char applicationCacheCssFile[] = "/clock.css";
48
49static void serverCallback(SoupServer*, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
50{
51    if (message->method != SOUP_METHOD_GET) {
52        soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
53        return;
54    }
55
56    soup_message_set_status(message, SOUP_STATUS_OK);
57    Eina_Strbuf* body = eina_strbuf_new();
58
59    if (!strcmp(path, applicationCacheHtmlFile))
60        eina_strbuf_append_printf(body, applicationCacheHtml);
61    else if (!strcmp(path, applicationCacheManifestFile))
62        eina_strbuf_append_printf(body, applicationCacheManifest);
63    else if (!strcmp(path, applicationCacheCssFile))
64        eina_strbuf_append_printf(body, applicationCacheCss);
65    else {
66        soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);
67        soup_message_body_complete(message->response_body);
68        eina_strbuf_free(body);
69        return;
70    }
71
72    const size_t bodyLength = eina_strbuf_length_get(body);
73    soup_message_body_append(message->response_body, SOUP_MEMORY_TAKE, eina_strbuf_string_steal(body), bodyLength);
74    eina_strbuf_free(body);
75
76    soup_message_body_complete(message->response_body);
77}
78
79struct OriginsData {
80    Eina_List* items;
81    Ewk_Application_Cache_Manager* manager;
82};
83
84static void getOriginsCallback(Eina_List* origins, void* event_info)
85{
86    Eina_List** ret = static_cast<Eina_List**>(event_info);
87    *ret = origins;
88    ecore_main_loop_quit();
89}
90
91static Eina_Bool timerCallback(void* userData)
92{
93    OriginsData* data = static_cast<OriginsData*>(userData);
94    ewk_application_cache_manager_origins_async_get(data->manager, getOriginsCallback, &data->items);
95
96    return ECORE_CALLBACK_CANCEL;
97}
98
99TEST_F(EWK2UnitTestBase, ewk_application_cache_manager)
100{
101    std::unique_ptr<EWK2UnitTestServer> httpServer = std::make_unique<EWK2UnitTestServer>();
102    httpServer->run(serverCallback);
103
104    Ecore_Timer* applicationCacheTimer;
105    OriginsData data;
106    data.manager = ewk_context_application_cache_manager_get(ewk_view_context_get(webView()));
107
108    // Before testing, remove all caches.
109    ewk_application_cache_manager_clear(data.manager);
110
111    applicationCacheTimer = ecore_timer_add(0.5, timerCallback, &data);
112    ecore_main_loop_begin();
113    ASSERT_EQ(0, eina_list_count(data.items));
114
115    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath(applicationCacheHtmlFile).data()));
116
117    applicationCacheTimer = ecore_timer_add(0.5, timerCallback, &data);
118    ecore_main_loop_begin();
119    ASSERT_EQ(1, eina_list_count(data.items));
120
121    ewk_application_cache_manager_entries_for_origin_del(data.manager, static_cast<Ewk_Security_Origin*>(eina_list_nth(data.items, 0)));
122
123    void* origin;
124    EINA_LIST_FREE(data.items, origin)
125        ewk_object_unref(static_cast<Ewk_Object*>(origin));
126
127    data.items = nullptr;
128
129    applicationCacheTimer = ecore_timer_add(0.5, timerCallback, &data);
130    ecore_main_loop_begin();
131    ASSERT_EQ(0, eina_list_count(data.items));
132}
133