[M120 Migration][MM] Support W3C EME
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / unittest / utc_blink_ewk_context_web_storage_usage_for_origin_get_func.cpp
1 // Copyright 2015 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 #define SAMPLE_ORIGIN "jmajnert.github.io"
10 const char* const WEB_STORAGE_SAMPLE_URL =
11     "http://" SAMPLE_ORIGIN
12     "/tests/"
13     "localStorage_test/web_storage_create_simple.html";
14
15 class utc_blink_ewk_context_web_storage_usage_for_origin_get
16     : public utc_blink_ewk_base {
17  protected:
18   utc_blink_ewk_context_web_storage_usage_for_origin_get()
19       : utc_blink_ewk_base(), context_(nullptr), usage_(0), neg_test_(false) {}
20
21   void PostSetUp() override {
22     ASSERT_TRUE(context_ = ewk_view_context_get(GetEwkWebView()));
23     // we want empty web storage at test start
24     ASSERT_TRUE(ewk_context_web_storage_delete_all(context_));
25   }
26
27   void LoadFinished(Evas_Object*) override { EventLoopStop(Success); }
28
29   void NegTest(Eina_List* origins) {
30     utc_message("NegTest: %p", origins);
31
32     Ewk_Security_Origin* origin = nullptr;
33     Ewk_Security_Origin* chosen_origin = nullptr;
34     Eina_List* it;
35     void* vorigin = nullptr;
36     EINA_LIST_FOREACH(origins, it, vorigin) {
37       origin = static_cast<Ewk_Security_Origin*>(vorigin);
38
39       const char* host = ewk_security_origin_host_get(origin);
40       if (host && !strcmp(host, SAMPLE_ORIGIN)) {
41         chosen_origin = origin;
42         break;
43       }
44     }
45     if (!chosen_origin)
46       EventLoopStop(Failure);
47     ASSERT_FALSE(ewk_context_web_storage_usage_for_origin_get(
48         nullptr, origin, usage_get_cb, this));
49     ASSERT_FALSE(ewk_context_web_storage_usage_for_origin_get(
50         context_, nullptr, usage_get_cb, this));
51     ASSERT_FALSE(ewk_context_web_storage_usage_for_origin_get(context_, origin,
52                                                               nullptr, this));
53     EventLoopStop(Success);
54   }
55
56   void FindSecurityOrigin(Eina_List* origins) {
57     utc_message("FindSecurityOrigin: %p", origins);
58
59     Ewk_Security_Origin* origin = nullptr;
60     Eina_List* it;
61     void* vorigin = nullptr;
62     EINA_LIST_FOREACH(origins, it, vorigin) {
63       origin = static_cast<Ewk_Security_Origin*>(vorigin);
64
65       const char* host = ewk_security_origin_host_get(origin);
66       if (host && !strcmp(host, SAMPLE_ORIGIN)) {
67         ASSERT_TRUE(ewk_context_web_storage_usage_for_origin_get(
68             context_, origin, usage_get_cb, this));
69         return;
70       }
71     }
72     EventLoopStop(Failure);
73   }
74
75   static void origins_get_cb(Eina_List* origins, void* user_data) {
76     utc_message("origins_get_cb: %p, %p", origins, user_data);
77     if (!user_data) {
78       ewk_context_origins_free(origins);
79       FAIL();
80     }
81
82     auto* self =
83         static_cast<utc_blink_ewk_context_web_storage_usage_for_origin_get*>(
84             user_data);
85
86     if (!origins) {
87       self->EventLoopStop(Failure);
88       return;
89     }
90
91     if (self->neg_test_)
92       self->NegTest(origins);
93     else
94       self->FindSecurityOrigin(origins);
95   }
96
97   static void usage_get_cb(uint64_t usage, void* user_data) {
98     utc_message("usage_get_cb: %lu, %p", usage, user_data);
99     if (!user_data) {
100       FAIL();
101       return;
102     }
103
104     auto* self =
105         static_cast<utc_blink_ewk_context_web_storage_usage_for_origin_get*>(
106             user_data);
107
108     self->usage_ = usage;
109     self->EventLoopStop(Success);
110   }
111
112  protected:
113   Ewk_Context* context_;
114   uint64_t usage_;
115   bool neg_test_;
116 };
117
118 /**
119  * @brief Tests if usage can be obtained when localstorage is used
120  */
121 TEST_F(utc_blink_ewk_context_web_storage_usage_for_origin_get, POS_TEST) {
122   utc_message("Loading web page: %s", WEB_STORAGE_SAMPLE_URL);
123   ASSERT_TRUE(ewk_view_url_set(GetEwkWebView(), WEB_STORAGE_SAMPLE_URL));
124   ASSERT_EQ(Success, EventLoopStart());
125
126   EventLoopWait(10.0);
127   ASSERT_TRUE(
128       ewk_context_web_storage_origins_get(context_, origins_get_cb, this));
129   ASSERT_EQ(Success, EventLoopStart());
130
131   ASSERT_TRUE(usage_);
132 }
133
134 /**
135  * @brief Tests if returns false when parameters are null.
136  */
137 TEST_F(utc_blink_ewk_context_web_storage_usage_for_origin_get, NEG_TEST) {
138   utc_message("Loading web page: %s", WEB_STORAGE_SAMPLE_URL);
139   ASSERT_TRUE(ewk_view_url_set(GetEwkWebView(), WEB_STORAGE_SAMPLE_URL));
140   ASSERT_EQ(Success, EventLoopStart());
141
142   EventLoopWait(10.0);
143   neg_test_ = true;
144   ASSERT_TRUE(
145       ewk_context_web_storage_origins_get(context_, origins_get_cb, this));
146   ASSERT_EQ(Success, EventLoopStart());
147 }