[M120 Migration][MM] Support W3C EME
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / unittest / utc_blink_ewk_context_local_file_system_delete_func.cpp
1 // Copyright 2014-2016 Samsung Electronics. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "utc_blink_ewk_base.h"
6 #include <inttypes.h>
7
8 class utc_blink_ewk_context_local_file_system_delete
9     : public utc_blink_ewk_base {
10  protected:
11   utc_blink_ewk_context_local_file_system_delete()
12       : utc_blink_ewk_base(),
13         ctx(nullptr),
14         origins(nullptr),
15         origin_to_delete(nullptr) {}
16
17   ~utc_blink_ewk_context_local_file_system_delete() override {
18     SetOrigins(nullptr);
19   }
20
21   void PreSetUp() override { AllowFileAccessFromFiles(); }
22
23   void PostSetUp() override {
24     ctx = ewk_view_context_get(GetEwkWebView());
25     evas_object_smart_callback_add(GetEwkWebView(), "title,changed",
26                                    title_changed_cb, this);
27   }
28
29   void PreTearDown() override {
30     evas_object_smart_callback_del(GetEwkWebView(), "title,changed",
31                                    title_changed_cb);
32   }
33
34   static void origins_get_cb(Eina_List* origins, void* user_data)
35   {
36     utc_message("[origins_get_cb] :: %p, %p", origins, user_data);
37     ASSERT_TRUE(user_data);
38     utc_blink_ewk_context_local_file_system_delete* owner = static_cast<utc_blink_ewk_context_local_file_system_delete*>(user_data);
39     owner->SetOrigins(origins);
40     owner->EventLoopStop(Success);
41   }
42
43   void SetOrigins(Eina_List* new_origins) {
44     utc_message("[SetOrigins] :: ");
45     if (new_origins != origins) {
46       if (origins) {
47         ewk_context_origins_free(origins);
48       }
49       origins = new_origins;
50     }
51   }
52
53   Eina_Bool FindOriginToDelete(const char* expected_protocol,
54                                const char* expected_host,
55                                uint16_t expected_port) {
56     Eina_List* list = nullptr;
57     void* list_data = nullptr;
58     EINA_LIST_FOREACH(origins, list, list_data) {
59       Ewk_Security_Origin* origin = (Ewk_Security_Origin*)(list_data);
60       const char* protocol = ewk_security_origin_protocol_get(origin);
61       const char* host = ewk_security_origin_host_get(origin);
62       uint16_t port = ewk_security_origin_port_get(origin);
63       utc_message(
64           "[FindOriginToDelete] :: "
65           "protocol: %s :: host: %s :: port: %" PRIu16,
66           protocol, host, port);
67       if (!strcmp(protocol, expected_protocol) &&
68           !strcmp(host, expected_host) && (port == expected_port)) {
69         origin_to_delete = origin;
70         return true;
71       }
72     }
73     return false;
74   }
75
76   void GetOrigins(int expected_count) {
77     utc_message("[GetOrigins] :: %d", eina_list_count(origins));
78     // web database operations are async, we must wait for changes to propagate
79     ASSERT_EQ(EINA_TRUE, ewk_context_local_file_system_origins_get(
80                              ctx, origins_get_cb, this));
81     ASSERT_EQ(Success, EventLoopStart());
82     ASSERT_EQ(expected_count, eina_list_count(origins));
83   }
84
85   static void origins_get_cb(Eina_List* origins, void* data) {
86     utc_message("[origins_get_cb] :: origins: %p, data: %p", origins, data);
87     if (data) {
88       auto owner =
89           static_cast<utc_blink_ewk_context_local_file_system_delete*>(data);
90       owner->SetOrigins(origins);
91       owner->EventLoopStop(Success);
92     }
93   }
94
95   static void title_changed_cb(void* data,
96                                Evas_Object* webView,
97                                void* event_info) {
98     utc_message("[title_changed_cb] :: data: %p", data);
99     if (data) {
100       auto owner =
101           static_cast<utc_blink_ewk_context_local_file_system_delete*>(data);
102       if (!strcmp("SUCCESS", static_cast<char*>(event_info))) {
103         owner->EventLoopStop(Success);
104       } else if (!strcmp("FAILURE", static_cast<char*>(event_info))) {
105         owner->EventLoopStop(Failure);
106       }
107     }
108   }
109
110   Ewk_Context* ctx;
111   Eina_List* origins;
112   Ewk_Security_Origin* origin_to_delete;
113 };
114
115 /**
116  * @brief Tests if there is possibility to get local file system origins
117 **/
118 TEST_F(utc_blink_ewk_context_local_file_system_delete, POS) {
119   std::string url = GetResourceUrl(
120       "ewk_context_local_file_system/"
121       "sample_context_local_file_system_write.html");
122   const char* const expected_protocol = "file";
123   const char* const expected_host = "";
124   const uint16_t expected_port = 0;
125   // Delete the local file system and check if there are no origins left
126   ASSERT_EQ(EINA_TRUE, ewk_context_local_file_system_all_delete(ctx));
127   GetOrigins(0);
128
129   // Create a local file system
130   ASSERT_EQ(EINA_TRUE, ewk_view_url_set(GetEwkWebView(), url.c_str()));
131   ASSERT_EQ(Success, EventLoopStart());
132   EventLoopWait(2);
133
134   // Check if origin is loaded
135   GetOrigins(1);
136   ASSERT_TRUE(
137       FindOriginToDelete(expected_protocol, expected_host, expected_port));
138   ASSERT_TRUE(origin_to_delete);
139
140   // Delete selected origin
141   ASSERT_EQ(EINA_TRUE,
142             ewk_context_local_file_system_delete(ctx, origin_to_delete));
143   EventLoopWait(2);
144
145   // Check if there is no expected origin
146   GetOrigins(0);
147   ASSERT_FALSE(
148       FindOriginToDelete(expected_protocol, expected_host, expected_port));
149 }
150
151 /**
152  * @brief Negative test for ewk_context_local_file_system_delete(). Checking
153  *        whether function works properly with null origin value.
154 **/
155 TEST_F(utc_blink_ewk_context_local_file_system_delete, NEG_ORIGIN_NULL) {
156   ASSERT_EQ(EINA_FALSE, ewk_context_local_file_system_delete(ctx, nullptr));
157 }
158
159 /**
160  * @brief Negative test for ewk_context_local_file_system_delete(). Checking
161  *        whether function works properly with null context and origin value.
162 **/
163 TEST_F(utc_blink_ewk_context_local_file_system_delete,
164        NEG_ORIGIN_AND_CONTEXT_NULL) {
165   ASSERT_EQ(EINA_FALSE, ewk_context_local_file_system_delete(nullptr, nullptr));
166 }