[EFL][WK2] Rename async APIs for better consistency with EFL
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / tests / test_ewk2_storage_manager.cpp
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
30 using namespace EWK2UnitTest;
31
32 class EWK2StorageManagerTest : public EWK2UnitTestBase {
33 public:
34     struct OriginData {
35         Eina_List* originList;
36         Ewk_Storage_Manager* manager;
37         bool didReceiveOriginsCallback;
38         bool isSynchronized;
39         unsigned timeToCheck;
40
41         OriginData()
42             : originList(0)
43             , manager(0)
44             , didReceiveOriginsCallback(false)
45             , isSynchronized(false)
46             , timeToCheck(10)
47         { }
48     };
49
50     static void getStorageOriginsCallback(Eina_List* origins, Ewk_Error* error, void* userData)
51     {
52         ASSERT_FALSE(error);
53
54         OriginData* originData = static_cast<OriginData*>(userData);
55         originData->didReceiveOriginsCallback = true;
56
57         Eina_List* l;
58         void* data;
59         EINA_LIST_FOREACH(origins, l, data) {
60             originData->originList = eina_list_append(originData->originList, data);
61             Ewk_Security_Origin* origin = static_cast<Ewk_Security_Origin*>(data);
62             if (!strcmp(ewk_security_origin_protocol_get(origin), "http")
63                 && !strcmp(ewk_security_origin_host_get(origin), "www.storagetest.com")
64                 && !ewk_security_origin_port_get(origin)) {
65                     originData->isSynchronized = true;
66                     ecore_main_loop_quit();
67             }
68         }
69     }
70
71     static bool timerCallback(void* userData)
72     {
73         OriginData* originData = static_cast<OriginData*>(userData);
74
75         if (originData->isSynchronized || !--(originData->timeToCheck)) {
76             ecore_main_loop_quit();
77             return ECORE_CALLBACK_CANCEL;
78         }
79
80         if (originData->didReceiveOriginsCallback) {
81             originData->didReceiveOriginsCallback = false;
82             ewk_storage_manager_origins_async_get(originData->manager, getStorageOriginsCallback, originData);
83         }
84
85         return ECORE_CALLBACK_RENEW;
86     }
87
88 protected:
89     bool checkOrigin(Eina_List* origins, Ewk_Security_Origin** origin)
90     {
91         Eina_List* l;
92         void* data;
93         EINA_LIST_FOREACH(origins, l, data) {
94             *origin = static_cast<Ewk_Security_Origin*>(data);
95             if (!strcmp(ewk_security_origin_protocol_get(*origin), "http")
96                 && !strcmp(ewk_security_origin_host_get(*origin), "www.storagetest.com")
97                 && !ewk_security_origin_port_get(*origin))
98                 return true;
99         }
100
101         return false;
102     }
103 };
104
105 TEST_F(EWK2StorageManagerTest, ewk_storage_manager_origins_async_get)
106 {
107     Evas_Object* view = webView();
108     const char* storageHTML =
109         "<html><head><title>original title</title></head>"
110         "<body>"
111         "<script type='text/javascript'>"
112         " localStorage.setItem('item', 'storage');"
113         "</script>"
114         "</body></html>";
115
116     ewk_view_html_string_load(view, storageHTML, "http://www.storagetest.com", 0);
117
118     OriginData originData;
119     originData.manager = ewk_context_storage_manager_get(ewk_view_context_get(view));
120     ASSERT_TRUE(ewk_storage_manager_origins_async_get(originData.manager, getStorageOriginsCallback, &originData));
121     Ecore_Timer* storage_timer = ecore_timer_add(1, reinterpret_cast<Ecore_Task_Cb>(timerCallback), &originData);
122
123     ecore_main_loop_begin();
124     if (storage_timer)
125         ecore_timer_del(storage_timer);
126
127     ASSERT_TRUE(originData.isSynchronized);
128     ASSERT_LE(1, eina_list_count(originData.originList));
129
130     Ewk_Security_Origin* origin = 0;
131     EXPECT_TRUE(checkOrigin(originData.originList, &origin));
132
133     EXPECT_EQ(origin, ewk_object_ref(origin));
134     ewk_object_unref(origin);
135 }