[M120 Migration][MM] Support W3C EME
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / unittest / utc_blink_ewk_context_application_cache_origins_get_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 #include "utc_blink_ewk_base.h"
6
7
8 class utc_blink_ewk_context_application_cache_origins_get : public utc_blink_ewk_base {
9 protected:
10   utc_blink_ewk_context_application_cache_origins_get()
11     : utc_blink_ewk_base()
12     , ctx(NULL)
13     , origins(NULL)
14   {
15   }
16
17   ~utc_blink_ewk_context_application_cache_origins_get() override {
18     SetOrigins(NULL);
19   }
20
21   void PostSetUp() override {
22     ctx = ewk_view_context_get(GetEwkWebView());
23     ASSERT_TRUE(ctx != NULL);
24   }
25
26   void LoadFinished(Evas_Object* webview) override {
27     utc_message("LoadFinished");
28     EventLoopStop(Success);
29   }
30
31   Eina_Bool FindSecurityOrigin(const char* origin_host, const char* origin_protocol)
32   {
33     utc_message("FindSecurityOrigin: %s in %p", origin_host, origins);
34
35     if(origins && origin_host) {
36       Eina_List* it = NULL;
37       void* list_data = NULL;
38       EINA_LIST_FOREACH(origins, it, list_data) {
39         Ewk_Security_Origin* origin = static_cast<Ewk_Security_Origin*>(list_data);
40
41         if (!strcmp(ewk_security_origin_protocol_get(origin), origin_protocol) &&
42             !strcmp(ewk_security_origin_host_get(origin), origin_host)) {
43           return EINA_TRUE;
44         }
45       }
46     }
47     return EINA_FALSE;
48   }
49
50   void GetOrigins(unsigned int expected_count)
51   {
52     utc_message("GetOrigins: %d", expected_count);
53     // origins operations are async, we must wait for changes to propagate
54     for (int i = 0; i < 3; ++i) {
55       if (EINA_TRUE != ewk_context_application_cache_origins_get(ctx, applicationCacheOriginsGet, this)) {
56         break;
57       }
58
59       if (Success != EventLoopStart()) {
60         break;
61       }
62
63       if (expected_count == eina_list_count(origins)) {
64         break;
65       }
66
67       if (!EventLoopWait(3.0)) {
68         break;
69       }
70     }
71   }
72
73   void SetOrigins(Eina_List* new_origins)
74   {
75     utc_message("SetOrigins");
76     if (new_origins != origins) {
77       if (origins) {
78         ewk_context_origins_free(origins);
79       }
80
81       origins = new_origins;
82     }
83   }
84
85   static void applicationCacheOriginsGet(Eina_List* origins, void* data)
86   {
87     utc_message("[applicationCacheOriginsGet] : %p, %p", origins, data);
88
89     if (!data) {
90        ewk_context_origins_free(origins);
91        FAIL();
92     }
93
94     utc_blink_ewk_context_application_cache_origins_get* owner =
95         static_cast<utc_blink_ewk_context_application_cache_origins_get*>(data);
96
97     owner->SetOrigins(origins);
98     owner->EventLoopStop(Success);
99   }
100
101 protected:
102   static const char* const appCacheURL;
103   Ewk_Context* ctx;
104   Eina_List* origins;
105 };
106
107 #define SAMPLE_ORIGIN_HOST "appcache.offline.technology"
108 #define SAMPLE_ORIGIN_PROTOCOL "http"
109 const char* const utc_blink_ewk_context_application_cache_origins_get::appCacheURL = SAMPLE_ORIGIN_PROTOCOL "://" SAMPLE_ORIGIN_HOST "/demo/";
110
111 /**
112 * @brief Load page and check if the origins is there
113 */
114 TEST_F(utc_blink_ewk_context_application_cache_origins_get, POS_TEST)
115 {
116   // Load page, so all related APIs are initialized
117   ASSERT_TRUE(ewk_view_url_set(GetEwkWebView(), "http://www.google.pl"));
118   ASSERT_EQ(utc_blink_ewk_base::Success, EventLoopStart());
119
120   // we want empty cache at test start
121   ASSERT_EQ(EINA_TRUE, ewk_context_application_cache_delete_all(ctx));
122   GetOrigins(0);
123   ASSERT_TRUE(origins == NULL);
124   ASSERT_EQ(0, eina_list_count(origins));
125
126   utc_message("Loading web page: %s", appCacheURL);
127   ASSERT_EQ(EINA_TRUE, ewk_view_url_set(GetEwkWebView(), appCacheURL));
128
129   ASSERT_EQ(Success, EventLoopStart());
130
131   GetOrigins(1);
132   ASSERT_TRUE(origins != NULL);
133   ASSERT_EQ(1, eina_list_count(origins));
134
135   ASSERT_TRUE(FindSecurityOrigin(SAMPLE_ORIGIN_HOST, SAMPLE_ORIGIN_PROTOCOL));
136 }
137
138 /**
139  * @brief Tests in case of bad parameters
140  */
141 TEST_F(utc_blink_ewk_context_application_cache_origins_get, INVALID_ARGS)
142 {
143   EXPECT_EQ(EINA_FALSE, ewk_context_application_cache_origins_get(NULL, applicationCacheOriginsGet, NULL));
144   EXPECT_EQ(EINA_FALSE, ewk_context_application_cache_origins_get(ctx, NULL, NULL));
145 }