[EFL][CustomProtocol] Do not add duplicated custom scheme
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / tests / test_ewk2_context.cpp
1 /*
2  * Copyright (C) 2012 Intel Corporation. All rights reserved.
3  * Copyright (C) 2012 Samsung Electronics
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28
29 #include "EwkView.h"
30 #include "UnitTestUtils/EWK2UnitTestBase.h"
31 #include "UnitTestUtils/EWK2UnitTestServer.h"
32 #include "ewk_context_private.h"
33 #include "ewk_view_private.h"
34 #include <wtf/text/CString.h>
35
36 using namespace EWK2UnitTest;
37
38 extern EWK2UnitTestEnvironment* environment;
39
40 static const char htmlReply[] = "<html><head><title>Foo</title></head><body>Bar</body></html>";
41
42 static bool finishTest = false;
43 static constexpr double testTimeoutSeconds = 2.0;
44
45 class EWK2ContextTest : public EWK2UnitTestBase {
46 public:
47     static void schemeRequestCallback1(Ewk_Url_Scheme_Request* request, void* userData)
48     {
49         const char* scheme = ewk_url_scheme_request_scheme_get(request);
50         ASSERT_STREQ("fooscheme", scheme);
51         const char* url = ewk_url_scheme_request_url_get(request);
52         ASSERT_STREQ("fooscheme:MyPath", url);
53         const char* path = ewk_url_scheme_request_path_get(request);
54         ASSERT_STREQ("MyPath", path);
55         ASSERT_TRUE(ewk_url_scheme_request_finish(request, htmlReply, strlen(htmlReply), "text/html"));
56
57         finishTest = true;
58     }
59
60     static void schemeRequestCallback2(Ewk_Url_Scheme_Request* request, void* userData)
61     {
62         const char* scheme = ewk_url_scheme_request_scheme_get(request);
63         ASSERT_STREQ("fooscheme", scheme);
64         const char* url = ewk_url_scheme_request_url_get(request);
65         ASSERT_STREQ("fooscheme:MyNewPath", url);
66         const char* path = ewk_url_scheme_request_path_get(request);
67         ASSERT_STREQ("MyNewPath", path);
68         ASSERT_TRUE(ewk_url_scheme_request_finish(request, htmlReply, strlen(htmlReply), "text/html"));
69
70         finishTest = true;
71     }
72 };
73
74 class EWK2ContextTestMultipleProcesses : public EWK2UnitTestBase {
75 protected:
76     EWK2ContextTestMultipleProcesses()
77     {
78         m_multipleProcesses = true;
79     }
80 };
81
82 class EWK2ContextTestWithExtension : public EWK2UnitTestBase {
83 public:
84     static void messageReceivedCallback(const char* name, const Eina_Value* body, void* userData)
85     {
86         bool* handled = static_cast<bool*>(userData);
87         ASSERT_STREQ("pong", name);
88
89         *handled = true;
90     }
91
92 protected:
93     EWK2ContextTestWithExtension()
94     {
95         m_withExtension = true;
96     }
97 };
98
99 TEST_F(EWK2ContextTest, ewk_context_default_get)
100 {
101     Ewk_Context* defaultContext = ewk_context_default_get();
102     ASSERT_TRUE(defaultContext);
103     ASSERT_EQ(defaultContext, ewk_context_default_get());
104 }
105
106 TEST_F(EWK2UnitTestBase, ewk_context_application_cache_manager_get)
107 {
108     Ewk_Context* context = ewk_view_context_get(webView());
109     Ewk_Application_Cache_Manager* applicationCacheManager = ewk_context_application_cache_manager_get(context);
110     ASSERT_TRUE(applicationCacheManager);
111     ASSERT_EQ(applicationCacheManager, ewk_context_application_cache_manager_get(context));
112 }
113
114 TEST_F(EWK2ContextTest, ewk_context_cookie_manager_get)
115 {
116     Ewk_Context* context = ewk_view_context_get(webView());
117     Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(context);
118     ASSERT_TRUE(cookieManager);
119     ASSERT_EQ(cookieManager, ewk_context_cookie_manager_get(context));
120 }
121
122 TEST_F(EWK2ContextTest, ewk_context_database_manager_get)
123 {
124     Ewk_Context* context = ewk_view_context_get(webView());
125     Ewk_Database_Manager* databaseManager = ewk_context_database_manager_get(context);
126     ASSERT_TRUE(databaseManager);
127     ASSERT_EQ(databaseManager, ewk_context_database_manager_get(context));
128 }
129
130 TEST_F(EWK2ContextTest, ewk_context_favicon_database_get)
131 {
132     Ewk_Context* context = ewk_view_context_get(webView());
133     Ewk_Favicon_Database* faviconDatabase = ewk_context_favicon_database_get(context);
134     ASSERT_TRUE(faviconDatabase);
135     ASSERT_EQ(faviconDatabase, ewk_context_favicon_database_get(context));
136 }
137
138 TEST_F(EWK2ContextTest, ewk_context_storage_manager_get)
139 {
140     Ewk_Context* context = ewk_view_context_get(webView());
141     Ewk_Storage_Manager* storageManager = ewk_context_storage_manager_get(context);
142     ASSERT_TRUE(storageManager);
143     ASSERT_EQ(storageManager, ewk_context_storage_manager_get(context));
144 }
145
146 TEST_F(EWK2ContextTest, ewk_context_url_scheme_register)
147 {
148     ewk_context_url_scheme_register(ewk_view_context_get(webView()), "fooscheme", schemeRequestCallback1, nullptr);
149     ewk_view_url_set(webView(), "fooscheme:MyPath");
150     ASSERT_TRUE(waitUntilTrue(finishTest, testTimeoutSeconds));
151
152     finishTest = false;
153     ewk_context_url_scheme_register(ewk_view_context_get(webView()), "fooscheme", schemeRequestCallback2, nullptr);
154     ewk_view_url_set(webView(), "fooscheme:MyNewPath");
155     ASSERT_TRUE(waitUntilTrue(finishTest, testTimeoutSeconds));
156 }
157
158 TEST_F(EWK2ContextTest, ewk_context_cache_model)
159 {
160     Ewk_Context* context = ewk_view_context_get(webView());
161
162     ASSERT_EQ(EWK_CACHE_MODEL_DOCUMENT_VIEWER, ewk_context_cache_model_get(context));
163
164     ASSERT_TRUE(ewk_context_cache_model_set(context, EWK_CACHE_MODEL_DOCUMENT_BROWSER));
165     ASSERT_EQ(EWK_CACHE_MODEL_DOCUMENT_BROWSER, ewk_context_cache_model_get(context));
166
167     ASSERT_TRUE(ewk_context_cache_model_set(context, EWK_CACHE_MODEL_PRIMARY_WEBBROWSER));
168     ASSERT_EQ(EWK_CACHE_MODEL_PRIMARY_WEBBROWSER, ewk_context_cache_model_get(context));
169
170     ASSERT_TRUE(ewk_context_cache_model_set(context, EWK_CACHE_MODEL_DOCUMENT_VIEWER));
171     ASSERT_EQ(EWK_CACHE_MODEL_DOCUMENT_VIEWER, ewk_context_cache_model_get(context));
172 }
173
174 TEST_F(EWK2ContextTest, ewk_context_web_process_model)
175 {
176     Ewk_Context* context = ewk_view_context_get(webView());
177
178     ASSERT_EQ(EWK_PROCESS_MODEL_SHARED_SECONDARY, ewk_context_process_model_get(context));
179
180     Ewk_Page_Group* pageGroup = ewk_view_page_group_get(webView());
181     Evas* evas = ecore_evas_get(backingStore());
182     Evas_Smart* smart = evas_smart_class_new(&(ewkViewClass()->sc));
183
184     Evas_Object* webView1 = ewk_view_smart_add(evas, smart, context, pageGroup);
185     Evas_Object* webView2 = ewk_view_smart_add(evas, smart, context, pageGroup);
186
187     PlatformProcessIdentifier webView1WebProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processIdentifier();
188     PlatformProcessIdentifier webView2WebProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processIdentifier();
189
190     ASSERT_EQ(webView1WebProcessID, webView2WebProcessID);
191 }
192
193 TEST_F(EWK2ContextTestMultipleProcesses, ewk_context_web_process_model)
194 {
195     Ewk_Context* context = ewk_view_context_get(webView());
196
197     ASSERT_EQ(EWK_PROCESS_MODEL_MULTIPLE_SECONDARY, ewk_context_process_model_get(context));
198
199     Ewk_Page_Group* pageGroup = ewk_view_page_group_get(webView());
200     Evas* evas = ecore_evas_get(backingStore());
201     Evas_Smart* smart = evas_smart_class_new(&(ewkViewClass()->sc));
202
203     Evas_Object* webView1 = ewk_view_smart_add(evas, smart, context, pageGroup);
204     Evas_Object* webView2 = ewk_view_smart_add(evas, smart, context, pageGroup);
205
206     PlatformProcessIdentifier webView1WebProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processIdentifier();
207     PlatformProcessIdentifier webView2WebProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processIdentifier();
208
209     ASSERT_NE(webView1WebProcessID, webView2WebProcessID);
210 }
211
212 TEST_F(EWK2ContextTest, ewk_context_network_process_model)
213 {
214     Ewk_Context* context = ewk_view_context_get(webView());
215
216     ASSERT_EQ(EWK_PROCESS_MODEL_SHARED_SECONDARY, ewk_context_process_model_get(context));
217
218     Ewk_Page_Group* pageGroup = ewk_view_page_group_get(webView());
219     Evas* evas = ecore_evas_get(backingStore());
220     Evas_Smart* smart = evas_smart_class_new(&(ewkViewClass()->sc));
221
222     Evas_Object* webView1 = ewk_view_smart_add(evas, smart, context, pageGroup);
223     Evas_Object* webView2 = ewk_view_smart_add(evas, smart, context, pageGroup);
224
225     PlatformProcessIdentifier webView1WebProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processIdentifier();
226     PlatformProcessIdentifier webView2WebProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processIdentifier();
227
228     ASSERT_EQ(webView1WebProcessID, webView2WebProcessID);
229
230     PlatformProcessIdentifier webView1NetworkProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processPool().networkProcess()->processIdentifier();
231     PlatformProcessIdentifier webView2NetworkProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processPool().networkProcess()->processIdentifier();
232
233     ASSERT_EQ(webView1NetworkProcessID, webView2NetworkProcessID);
234 }
235
236 TEST_F(EWK2ContextTestMultipleProcesses, ewk_context_network_process_model)
237 {
238     Ewk_Context* context = ewk_view_context_get(webView());
239
240     ASSERT_EQ(EWK_PROCESS_MODEL_MULTIPLE_SECONDARY, ewk_context_process_model_get(context));
241
242     Ewk_Page_Group* pageGroup = ewk_view_page_group_get(webView());
243     Evas* evas = ecore_evas_get(backingStore());
244     Evas_Smart* smart = evas_smart_class_new(&(ewkViewClass()->sc));
245
246     Evas_Object* webView1 = ewk_view_smart_add(evas, smart, context, pageGroup);
247     Evas_Object* webView2 = ewk_view_smart_add(evas, smart, context, pageGroup);
248
249     PlatformProcessIdentifier webView1WebProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processIdentifier();
250     PlatformProcessIdentifier webView2WebProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processIdentifier();
251     PlatformProcessIdentifier webView1NetworkProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processPool().networkProcess()->processIdentifier();
252     PlatformProcessIdentifier webView2NetworkProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processPool().networkProcess()->processIdentifier();
253
254     ASSERT_NE(webView1WebProcessID, webView2WebProcessID);
255     ASSERT_NE(webView1WebProcessID, webView1NetworkProcessID);
256     ASSERT_NE(webView1WebProcessID, webView2NetworkProcessID);
257 }
258
259 TEST_F(EWK2ContextTest, ewk_context_new)
260 {
261     Ewk_Context* context = ewk_context_new();
262     ASSERT_TRUE(context);
263     ewk_object_unref(context);
264 }
265
266 TEST_F(EWK2ContextTestWithExtension, ewk_context_new_with_extensions_path)
267 {
268     bool received = false;
269     Ewk_Context* context = ewk_view_context_get(webView());
270
271     ewk_context_message_from_extensions_callback_set(context, messageReceivedCallback, &received);
272
273     ewk_view_url_set(webView(), environment->defaultTestPageUrl());
274     ASSERT_TRUE(waitUntilLoadFinished());
275
276     Eina_Value* body = eina_value_new(EINA_VALUE_TYPE_STRING);
277     eina_value_set(body, "From UIProcess");
278     ewk_context_message_post_to_extensions(context, "ping", body);
279     eina_value_free(body);
280
281     ASSERT_TRUE(waitUntilTrue(received, testTimeoutSeconds));
282 }
283
284 TEST_F(EWK2ContextTest, ewk_context_additional_plugin_path_set)
285 {
286     Ewk_Context* context = ewk_view_context_get(webView());
287
288     ASSERT_FALSE(ewk_context_additional_plugin_path_set(context, 0));
289
290     ASSERT_TRUE(ewk_context_additional_plugin_path_set(context, "/plugins"));
291
292     /* FIXME: Get additional plugin path and compare with the path. */
293 }
294
295 static char* s_acceptLanguages = nullptr;
296
297 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
298 {
299     if (message->method != SOUP_METHOD_GET) {
300         soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
301         return;
302     }
303
304     soup_message_set_status(message, SOUP_STATUS_OK);
305     s_acceptLanguages = strdup(soup_message_headers_get_one(message->request_headers, "Accept-Language"));
306
307     soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, s_acceptLanguages, strlen(s_acceptLanguages));
308     soup_message_body_complete(message->response_body);
309 }
310
311 TEST_F(EWK2UnitTestBase, ewk_context_preferred_languages)
312 {
313     Eina_List* acceptLanguages;
314     acceptLanguages = eina_list_append(acceptLanguages, "ko_kr");
315     acceptLanguages = eina_list_append(acceptLanguages, "fr");
316     acceptLanguages = eina_list_append(acceptLanguages, "en");
317
318     ewk_context_preferred_languages_set(acceptLanguages);
319
320     std::unique_ptr<EWK2UnitTestServer> httpServer = std::make_unique<EWK2UnitTestServer>();
321     httpServer->run(serverCallback);
322
323     ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
324     ASSERT_STREQ("ko-kr, fr;q=0.90, en;q=0.80", s_acceptLanguages);
325     free(s_acceptLanguages);
326
327     ewk_context_preferred_languages_set(nullptr);
328     ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
329     ASSERT_STREQ("en-US", s_acceptLanguages);
330     free(s_acceptLanguages);
331 }