1/*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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/EWK2UnitTestServer.h"
30#include "WKEinaSharedString.h"
31
32using namespace EWK2UnitTest;
33
34extern EWK2UnitTestEnvironment* environment;
35
36class EWK2FaviconDatabaseTest : public EWK2UnitTestBase {
37public:
38    struct IconRequestData {
39        Evas_Object* view;
40        Evas_Object* icon;
41    };
42
43    static void serverCallback(SoupServer* httpServer, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
44    {
45        if (message->method != SOUP_METHOD_GET) {
46            soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
47        return;
48        }
49
50        if (!strcmp(path, "/favicon.ico")) {
51            CString faviconPath = environment->pathForResource("blank.ico");
52            Eina_File* f = eina_file_open(faviconPath.data(), false);
53            if (!f) {
54                soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);
55                soup_message_body_complete(message->response_body);
56                return;
57            }
58
59            size_t fileSize = eina_file_size_get(f);
60
61            void* contents = eina_file_map_all(f, EINA_FILE_POPULATE);
62            if (!contents) {
63                soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);
64                soup_message_body_complete(message->response_body);
65                return;
66            }
67
68            soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, contents, fileSize);
69            soup_message_set_status(message, SOUP_STATUS_OK);
70            soup_message_body_complete(message->response_body);
71
72            eina_file_map_free(f, contents);
73            eina_file_close(f);
74            return;
75        }
76
77        const char contents[] = "<html><body>favicon test</body></html>";
78        soup_message_set_status(message, SOUP_STATUS_OK);
79        soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, contents, strlen(contents));
80        soup_message_body_complete(message->response_body);
81    }
82
83    static void requestFaviconData(Ewk_Favicon_Database *faviconDatabase, const char *url, void *userdata)
84    {
85        IconRequestData* data = static_cast<IconRequestData*>(userdata);
86
87        Evas* evas = evas_object_evas_get(data->view);
88        data->icon = ewk_favicon_database_icon_get(faviconDatabase, url, evas);
89    }
90};
91
92TEST_F(EWK2FaviconDatabaseTest, ewk_favicon_database_async_icon_get)
93{
94    std::unique_ptr<EWK2UnitTestServer> httpServer = std::make_unique<EWK2UnitTestServer>();
95    httpServer->run(serverCallback);
96
97    // Set favicon database path and enable functionality.
98    Ewk_Context* context = ewk_view_context_get(webView());
99    ewk_context_favicon_database_directory_set(context, 0);
100    Ewk_Favicon_Database* database = ewk_context_favicon_database_get(context);
101
102    IconRequestData data = { webView(), 0 };
103    ewk_favicon_database_icon_change_callback_add(database, requestFaviconData, &data);
104
105    // We need to load the page first to ensure the icon data will be
106    // in the database in case there's an associated favicon.
107    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/").data()));
108
109    while (!data.icon)
110        ecore_main_loop_iterate();
111
112    ASSERT_TRUE(data.icon);
113
114    // It is a 16x16 favicon.
115    int width, height;
116    evas_object_image_size_get(data.icon, &width, &height);
117    EXPECT_EQ(16, width);
118    EXPECT_EQ(16, height);
119    evas_object_unref(data.icon);
120
121    ewk_favicon_database_icon_change_callback_del(database, requestFaviconData);
122}
123