1/*
2 * Copyright (C) 2012 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
30using namespace EWK2UnitTest;
31
32class EWK2DatabaseManagerTest : public EWK2UnitTestBase {
33public:
34    struct OriginData {
35        Eina_List* originList;
36        Ewk_Database_Manager* manager;
37        bool didReceiveOriginsCallback;
38        bool isSynchronized;
39        unsigned timeoutSeconds;
40
41        OriginData()
42            : originList(0)
43            , manager(0)
44            , didReceiveOriginsCallback(false)
45            , isSynchronized(false)
46            , timeoutSeconds(10)
47        { }
48    };
49
50    static void databaseOriginsCallback(Eina_List* origins, void* userData)
51    {
52        OriginData* originData = static_cast<OriginData*>(userData);
53        originData->didReceiveOriginsCallback = true;
54
55        Eina_List* l;
56        void* data;
57        EINA_LIST_FOREACH(origins, l, data) {
58            Ewk_Security_Origin* origin = static_cast<Ewk_Security_Origin*>(data);
59            if (!strcmp(ewk_security_origin_protocol_get(origin), "http")
60                && !strcmp(ewk_security_origin_host_get(origin), "www.databasetest.com")
61                && !ewk_security_origin_port_get(origin)) {
62                    originData->originList = origins;
63                    originData->isSynchronized = true;
64                    return;
65            }
66        }
67        void* originItem;
68        EINA_LIST_FREE(origins, originItem)
69            ewk_object_unref(static_cast<Ewk_Object*>(originItem));
70    }
71
72    static Eina_Bool timerCallback(void* userData)
73    {
74        OriginData* originData = static_cast<OriginData*>(userData);
75
76        if (originData->isSynchronized || !--(originData->timeoutSeconds)) {
77            ecore_main_loop_quit();
78            return ECORE_CALLBACK_CANCEL;
79        }
80
81        if (originData->didReceiveOriginsCallback) {
82            originData->didReceiveOriginsCallback = false;
83            ewk_database_manager_origins_async_get(originData->manager, databaseOriginsCallback, originData);
84        }
85
86        return ECORE_CALLBACK_RENEW;
87    }
88};
89
90TEST_F(EWK2DatabaseManagerTest, ewk_database_manager_origins_async_get)
91{
92    Evas_Object* view = webView();
93    const char* databaseHTML =
94        "<html><head><title>original title</title></head>"
95        "<body>"
96        "<script type='text/javascript'>"
97        " var db = openDatabase(\"DBTest\", \"1.0\", \"HTML5 Database example\", 200000);"
98        "</script>"
99        "</body></html>";
100
101    ASSERT_TRUE(ewk_view_html_string_load(view, databaseHTML, "http://www.databasetest.com", 0));
102    ASSERT_TRUE(waitUntilLoadFinished());
103
104    OriginData originData;
105    originData.manager = ewk_context_database_manager_get(ewk_view_context_get(view));
106    ASSERT_TRUE(ewk_database_manager_origins_async_get(originData.manager, databaseOriginsCallback, &originData));
107    Ecore_Timer* databaseTimer = ecore_timer_add(1, timerCallback, &originData);
108
109    ecore_main_loop_begin();
110    databaseTimer = nullptr;
111
112    ASSERT_TRUE(originData.isSynchronized);
113    ASSERT_LE(1, eina_list_count(originData.originList));
114
115    void* originItem;
116    EINA_LIST_FREE(originData.originList, originItem)
117        ewk_object_unref(static_cast<Ewk_Object*>(originItem));
118}
119