Merge branch 'dev/ewk_test_base' into dev/ewk_test
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / unittest / utc_blink_ewk_context_local_file_system_delete_func.cpp
1 /*
2  * chromium EFL
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation, Inc., 51
18  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21
22 #include "utc_blink_ewk_base.h"
23
24 class utc_blink_ewk_context_local_file_system_delete : public utc_blink_ewk_base
25 {
26 protected:
27   utc_blink_ewk_context_local_file_system_delete()
28     : utc_blink_ewk_base()
29     , ctx(NULL)
30     , origins(NULL)
31     , origin_to_delete(NULL)
32   {
33   }
34
35   ~utc_blink_ewk_context_local_file_system_delete()
36   {
37     origin_to_delete = NULL;
38     SetOrigins(NULL);
39   }
40
41   void PreSetUp()
42   {
43     static char* args[]={"--allow-file-access-from-files"};
44     ewk_set_arguments(1,args);
45   }
46
47   void PostSetUp()
48   {
49     utc_message("[PostSetUp] :: ");
50     ctx = ewk_view_context_get(GetEwkWebView());
51     evas_object_smart_callback_add(GetEwkWebView(), "title,changed", title_changed_cb, this);
52   }
53
54   void PreTearDown()
55   {
56     evas_object_smart_callback_del(GetEwkWebView(), "title,changed", title_changed_cb);
57   }
58
59   static void origins_get_cb(Eina_List* origins, void* user_data)
60   {
61     utc_message("[origins_get_cb] :: %p, %p", origins, user_data);
62     ASSERT_TRUE(user_data);
63     utc_blink_ewk_context_local_file_system_delete* owner = static_cast<utc_blink_ewk_context_local_file_system_delete*>(user_data);
64     owner->SetOrigins(origins);
65     owner->EventLoopStop(Success);
66   }
67
68   static void title_changed_cb(void* user_data, Evas_Object* webView, void* event_info)
69   {
70     utc_message("[title_changed_cb] :: \n");
71
72     ASSERT_TRUE(user_data);
73     utc_blink_ewk_context_local_file_system_delete* owner = static_cast<utc_blink_ewk_context_local_file_system_delete*>(user_data);
74
75     if (!strcmp("SUCCESS", (char*)event_info)) {
76       owner->EventLoopStop(Success);
77     }
78     else if (!strcmp("FAILURE", (char*)event_info)) {
79       owner->EventLoopStop(Failure);
80     }
81   }
82
83   void SetOrigins(Eina_List* new_origins)
84   {
85     utc_message("[SetOrigins] :: ");
86     if (new_origins != origins) {
87       if (origins) {
88         ewk_context_origins_free(origins);
89       }
90       origins = new_origins;
91     }
92   }
93
94   Eina_Bool HasOrigin(const char* protocol, const char* host, uint16_t port)
95   {
96     Eina_List* list = NULL;
97     void* list_data = NULL;
98     EINA_LIST_FOREACH(origins, list, list_data) {
99       Ewk_Security_Origin* origin = (Ewk_Security_Origin*)(list_data);
100       const char* protocol_ = ewk_security_origin_protocol_get(origin);
101       const char* host_ = ewk_security_origin_host_get(origin);
102       uint16_t port_ = ewk_security_origin_port_get(origin);
103       if (!strcmp(protocol_, protocol) && !strcmp(host_, host) && (port_ == port)) {
104         origin_to_delete = origin;
105         return EINA_TRUE;
106       }
107     }
108     return EINA_FALSE;
109   }
110
111   void GetOrigins(int expected_count)
112   {
113     utc_message("[GetOrigins] :: %d", eina_list_count(origins));
114     // web database operations are async, we must wait for changes to propagate
115     for (int i = 0; i < 3; ++i) {
116       if (EINA_TRUE != ewk_context_local_file_system_origins_get(ctx, origins_get_cb, this))
117         break;
118
119       if (Success != EventLoopStart())
120         break;
121
122       if (expected_count == eina_list_count(origins))
123         break;
124
125       if (!EventLoopWait(3.0))
126         break;
127     }
128   }
129
130 protected:
131   static const char* const URL;
132   static const char* const expected_protocol;
133   static const char* const expected_host;
134   static const uint16_t expected_port;
135
136 protected:
137   Eina_List* origins;
138   Ewk_Security_Origin* origin_to_delete;
139   Ewk_Context* ctx;
140 };
141
142 const char* const utc_blink_ewk_context_local_file_system_delete::URL = "ewk_context_local_file_system/sample_context_local_file_system_write.html";
143 const char* const utc_blink_ewk_context_local_file_system_delete::expected_protocol = "file";
144 const char* const utc_blink_ewk_context_local_file_system_delete::expected_host = "";
145 const uint16_t utc_blink_ewk_context_local_file_system_delete::expected_port = 65535;
146
147 /**
148 * @brief Tests if there is possibility to get local file system origins
149 */
150 TEST_F(utc_blink_ewk_context_local_file_system_delete, POS_TEST)
151 {
152   // Delete the local file system and check if there are no origins left
153   ASSERT_EQ(EINA_TRUE, ewk_context_local_file_system_all_delete(ctx));
154   GetOrigins(0);
155   ASSERT_EQ(0, eina_list_count(origins));
156   ASSERT_TRUE(origins == NULL);
157
158   // Create a local file system
159   ASSERT_EQ(EINA_TRUE, ewk_view_url_set(GetEwkWebView(), GetResourceUrl(URL).c_str()));
160   ASSERT_EQ(Success, EventLoopStart());
161
162   // Check if origin is loaded
163   GetOrigins(1);
164   ASSERT_EQ(EINA_TRUE, HasOrigin(expected_protocol, expected_host, expected_port));
165
166   // Delete selected origin
167   ASSERT_TRUE(origin_to_delete);
168   ASSERT_EQ(EINA_TRUE, ewk_context_local_file_system_delete(ctx, origin_to_delete));
169
170   // Check if there is no expected origin
171   GetOrigins(0);
172   ASSERT_EQ(EINA_FALSE, HasOrigin(expected_protocol, expected_host, expected_port));
173 }
174
175 /**
176 * @brief Tests if cannot delete local file systems when origin is null
177 */
178 TEST_F(utc_blink_ewk_context_local_file_system_delete, NEG_TEST1)
179 {
180   ASSERT_EQ(EINA_FALSE, ewk_context_local_file_system_delete(ctx, NULL));
181 }
182
183 /**
184 * @brief Tests if cannot delete local file systems when context is null
185 */
186 TEST_F(utc_blink_ewk_context_local_file_system_delete, NEG_TEST2)
187 {
188   ASSERT_EQ(EINA_FALSE, ewk_context_local_file_system_delete(NULL, NULL));
189 }