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