[M120 Migration][MM] Support W3C EME
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / unittest / utc_blink_ewk_context_web_storage_origin_delete_func.cpp
1 // Copyright 2014 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 /* Define those macros _before_ you include the utc_blink_ewk.h header file. */
6
7 #include "utc_blink_ewk_base.h"
8
9 class utc_blink_ewk_context_web_storage_origin_delete: public utc_blink_ewk_base {
10 protected:
11   utc_blink_ewk_context_web_storage_origin_delete()
12     : utc_blink_ewk_base()
13     , origins(NULL)
14     , ctx(NULL)
15   {
16   }
17
18   ~utc_blink_ewk_context_web_storage_origin_delete() override {
19     SetOrigins(NULL);
20   }
21
22   void PostSetUp() override {
23     ctx = ewk_view_context_get(GetEwkWebView());
24     ASSERT_TRUE(ctx != NULL);
25
26     // we want empty web storage at test start
27     ASSERT_EQ(EINA_TRUE, ewk_context_web_storage_delete_all(ctx));
28
29     GetOrigins(0);
30     ASSERT_TRUE(origins == NULL);
31   }
32
33   void LoadFinished(Evas_Object* webview) override { EventLoopStop(Success); }
34
35   static void origins_get_cb(Eina_List* origins, void* user_data)
36   {
37     utc_message("origins_get_cb: %p, %p", origins, user_data);
38     if (!user_data) {
39       ewk_context_origins_free(origins);
40       FAIL();
41     }
42
43     utc_blink_ewk_context_web_storage_origin_delete* owner = static_cast<utc_blink_ewk_context_web_storage_origin_delete*>(user_data);
44
45     owner->SetOrigins(origins);
46     owner->EventLoopStop(Success);
47   }
48
49   Ewk_Security_Origin* FindSecurityOrigin(const char* origin_host)
50   {
51     utc_message("FindSecurityOrigin: %s in %p", origin_host, origins);
52     Ewk_Security_Origin* retval = NULL;
53
54     if(origins && origin_host) {
55       Eina_List* it;
56       void *vorigin = NULL;
57       EINA_LIST_FOREACH(origins, it, vorigin) {
58         Ewk_Security_Origin* origin = static_cast<Ewk_Security_Origin*>(vorigin);
59
60         const char* host = ewk_security_origin_host_get(origin);
61         if (host && strcmp(host, origin_host) == 0) {
62           retval = origin;
63           break;
64         }
65       }
66     }
67
68     return retval;
69   }
70
71   void SetOrigins(Eina_List* new_origins)
72   {
73     if (new_origins != origins) {
74       if (origins) {
75         ewk_context_origins_free(origins);
76       }
77
78       origins = new_origins;
79     }
80   }
81
82   void GetOrigins(int expected_count)
83   {
84     // web storage operations are async, we must wait for changes to propagate
85     for (int i = 0; i < 3; ++i) {
86       if (EINA_TRUE != ewk_context_web_storage_origins_get(ctx, origins_get_cb, this)) {
87         break;
88       }
89
90       if (Success != EventLoopStart()) {
91         break;
92       }
93
94       if (expected_count == eina_list_count(origins)) {
95         break;
96       }
97
98       if (!EventLoopWait(3.0)) {
99         break;
100       }
101     }
102   }
103
104 protected:
105   static const char* const web_storage_sample_resource;
106   static const char* const web_storage_sample_url;
107   static const char* const sample_origin;
108
109 protected:
110   Eina_List* origins;
111   Ewk_Context* ctx;
112 };
113
114 const char* const utc_blink_ewk_context_web_storage_origin_delete::web_storage_sample_resource = "ewk_context_web_storage/web_storage_create_simple.html";
115 const char* const utc_blink_ewk_context_web_storage_origin_delete::web_storage_sample_url = "http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_webstorage_local";
116 const char* const utc_blink_ewk_context_web_storage_origin_delete::sample_origin = "www.w3schools.com";
117
118 /**
119 * @brief Tests if can delete all web storages
120 */
121 TEST_F(utc_blink_ewk_context_web_storage_origin_delete, POS_TEST)
122 {
123   // load some pages with web storage
124   std::string resurl = GetResourceUrl(web_storage_sample_resource);
125   utc_message("Loading resource: %s", resurl.c_str());
126   ASSERT_EQ(EINA_TRUE, ewk_view_url_set(GetEwkWebView(), resurl.c_str()));
127   ASSERT_EQ(Success, EventLoopStart());
128
129   utc_message("Loading web page: %s", web_storage_sample_url);
130   ASSERT_EQ(EINA_TRUE, ewk_view_url_set(GetEwkWebView(), web_storage_sample_url));
131   ASSERT_EQ(Success, EventLoopStart());
132
133   GetOrigins(2);
134   ASSERT_TRUE(origins != NULL);
135   ASSERT_EQ(2, eina_list_count(origins));
136
137   // remove sample origin
138   Ewk_Security_Origin* origin = FindSecurityOrigin(sample_origin);
139   ASSERT_TRUE(origin != NULL);
140
141   ASSERT_EQ(EINA_TRUE, ewk_context_web_storage_origin_delete(ctx, origin));
142
143   GetOrigins(1);
144   ASSERT_TRUE(origins != NULL);
145   ASSERT_EQ(1, eina_list_count(origins));
146
147   // verify security origin was removed
148   origin = FindSecurityOrigin(sample_origin);
149   ASSERT_TRUE(origin == NULL);
150 }
151
152 /**
153 * @brief Tests if returns false when context is null.
154 */
155 TEST_F(utc_blink_ewk_context_web_storage_origin_delete, invalid_args)
156 {
157   EXPECT_EQ(EINA_FALSE, ewk_context_web_storage_origin_delete(NULL, NULL));
158   EXPECT_EQ(EINA_FALSE, ewk_context_web_storage_origin_delete(ctx, NULL));
159 }