530f9fcb9dd07e5a88b72721ad60dab82d9c0611
[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 schemeRequestCallback(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
61 class EWK2ContextTestMultipleProcesses : public EWK2UnitTestBase {
62 protected:
63     EWK2ContextTestMultipleProcesses()
64     {
65         m_multipleProcesses = true;
66     }
67 };
68
69 class EWK2ContextTestWithExtension : public EWK2UnitTestBase {
70 public:
71     static void messageReceivedCallback(const char* name, const Eina_Value* body, void* userData)
72     {
73         bool* handled = static_cast<bool*>(userData);
74         ASSERT_STREQ("pong", name);
75
76         *handled = true;
77     }
78
79 protected:
80     EWK2ContextTestWithExtension()
81     {
82         m_withExtension = true;
83     }
84 };
85
86 TEST_F(EWK2ContextTest, ewk_context_default_get)
87 {
88     Ewk_Context* defaultContext = ewk_context_default_get();
89     ASSERT_TRUE(defaultContext);
90     ASSERT_EQ(defaultContext, ewk_context_default_get());
91 }
92
93 TEST_F(EWK2UnitTestBase, ewk_context_application_cache_manager_get)
94 {
95     Ewk_Context* context = ewk_view_context_get(webView());
96     Ewk_Application_Cache_Manager* applicationCacheManager = ewk_context_application_cache_manager_get(context);
97     ASSERT_TRUE(applicationCacheManager);
98     ASSERT_EQ(applicationCacheManager, ewk_context_application_cache_manager_get(context));
99 }
100
101 TEST_F(EWK2ContextTest, ewk_context_cookie_manager_get)
102 {
103     Ewk_Context* context = ewk_view_context_get(webView());
104     Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(context);
105     ASSERT_TRUE(cookieManager);
106     ASSERT_EQ(cookieManager, ewk_context_cookie_manager_get(context));
107 }
108
109 TEST_F(EWK2ContextTest, ewk_context_database_manager_get)
110 {
111     Ewk_Context* context = ewk_view_context_get(webView());
112     Ewk_Database_Manager* databaseManager = ewk_context_database_manager_get(context);
113     ASSERT_TRUE(databaseManager);
114     ASSERT_EQ(databaseManager, ewk_context_database_manager_get(context));
115 }
116
117 TEST_F(EWK2ContextTest, ewk_context_favicon_database_get)
118 {
119     Ewk_Context* context = ewk_view_context_get(webView());
120     Ewk_Favicon_Database* faviconDatabase = ewk_context_favicon_database_get(context);
121     ASSERT_TRUE(faviconDatabase);
122     ASSERT_EQ(faviconDatabase, ewk_context_favicon_database_get(context));
123 }
124
125 TEST_F(EWK2ContextTest, ewk_context_storage_manager_get)
126 {
127     Ewk_Context* context = ewk_view_context_get(webView());
128     Ewk_Storage_Manager* storageManager = ewk_context_storage_manager_get(context);
129     ASSERT_TRUE(storageManager);
130     ASSERT_EQ(storageManager, ewk_context_storage_manager_get(context));
131 }
132
133 TEST_F(EWK2ContextTest, ewk_context_url_scheme_register)
134 {
135     ewk_context_url_scheme_register(ewk_view_context_get(webView()), "fooscheme", schemeRequestCallback, nullptr);
136     ewk_view_url_set(webView(), "fooscheme:MyPath");
137
138     ASSERT_TRUE(waitUntilTrue(finishTest, testTimeoutSeconds));
139 }
140
141 TEST_F(EWK2ContextTest, ewk_context_cache_model)
142 {
143     Ewk_Context* context = ewk_view_context_get(webView());
144
145     ASSERT_EQ(EWK_CACHE_MODEL_DOCUMENT_VIEWER, ewk_context_cache_model_get(context));
146
147     ASSERT_TRUE(ewk_context_cache_model_set(context, EWK_CACHE_MODEL_DOCUMENT_BROWSER));
148     ASSERT_EQ(EWK_CACHE_MODEL_DOCUMENT_BROWSER, ewk_context_cache_model_get(context));
149
150     ASSERT_TRUE(ewk_context_cache_model_set(context, EWK_CACHE_MODEL_PRIMARY_WEBBROWSER));
151     ASSERT_EQ(EWK_CACHE_MODEL_PRIMARY_WEBBROWSER, ewk_context_cache_model_get(context));
152
153     ASSERT_TRUE(ewk_context_cache_model_set(context, EWK_CACHE_MODEL_DOCUMENT_VIEWER));
154     ASSERT_EQ(EWK_CACHE_MODEL_DOCUMENT_VIEWER, ewk_context_cache_model_get(context));
155 }
156
157 TEST_F(EWK2ContextTest, ewk_context_web_process_model)
158 {
159     Ewk_Context* context = ewk_view_context_get(webView());
160
161     ASSERT_EQ(EWK_PROCESS_MODEL_SHARED_SECONDARY, ewk_context_process_model_get(context));
162
163     Ewk_Page_Group* pageGroup = ewk_view_page_group_get(webView());
164     Evas* evas = ecore_evas_get(backingStore());
165     Evas_Smart* smart = evas_smart_class_new(&(ewkViewClass()->sc));
166
167     Evas_Object* webView1 = ewk_view_smart_add(evas, smart, context, pageGroup);
168     Evas_Object* webView2 = ewk_view_smart_add(evas, smart, context, pageGroup);
169
170     PlatformProcessIdentifier webView1WebProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processIdentifier();
171     PlatformProcessIdentifier webView2WebProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processIdentifier();
172
173     ASSERT_EQ(webView1WebProcessID, webView2WebProcessID);
174 }
175
176 TEST_F(EWK2ContextTestMultipleProcesses, ewk_context_web_process_model)
177 {
178     Ewk_Context* context = ewk_view_context_get(webView());
179
180     ASSERT_EQ(EWK_PROCESS_MODEL_MULTIPLE_SECONDARY, ewk_context_process_model_get(context));
181
182     Ewk_Page_Group* pageGroup = ewk_view_page_group_get(webView());
183     Evas* evas = ecore_evas_get(backingStore());
184     Evas_Smart* smart = evas_smart_class_new(&(ewkViewClass()->sc));
185
186     Evas_Object* webView1 = ewk_view_smart_add(evas, smart, context, pageGroup);
187     Evas_Object* webView2 = ewk_view_smart_add(evas, smart, context, pageGroup);
188
189     PlatformProcessIdentifier webView1WebProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processIdentifier();
190     PlatformProcessIdentifier webView2WebProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processIdentifier();
191
192     ASSERT_NE(webView1WebProcessID, webView2WebProcessID);
193 }
194
195 TEST_F(EWK2ContextTest, ewk_context_network_process_model)
196 {
197     Ewk_Context* context = ewk_view_context_get(webView());
198
199     ASSERT_EQ(EWK_PROCESS_MODEL_SHARED_SECONDARY, ewk_context_process_model_get(context));
200
201     Ewk_Page_Group* pageGroup = ewk_view_page_group_get(webView());
202     Evas* evas = ecore_evas_get(backingStore());
203     Evas_Smart* smart = evas_smart_class_new(&(ewkViewClass()->sc));
204
205     Evas_Object* webView1 = ewk_view_smart_add(evas, smart, context, pageGroup);
206     Evas_Object* webView2 = ewk_view_smart_add(evas, smart, context, pageGroup);
207
208     PlatformProcessIdentifier webView1WebProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processIdentifier();
209     PlatformProcessIdentifier webView2WebProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processIdentifier();
210
211     ASSERT_EQ(webView1WebProcessID, webView2WebProcessID);
212
213     PlatformProcessIdentifier webView1NetworkProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processPool().networkProcess()->processIdentifier();
214     PlatformProcessIdentifier webView2NetworkProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processPool().networkProcess()->processIdentifier();
215
216     ASSERT_EQ(webView1NetworkProcessID, webView2NetworkProcessID);
217 }
218
219 TEST_F(EWK2ContextTestMultipleProcesses, ewk_context_network_process_model)
220 {
221     Ewk_Context* context = ewk_view_context_get(webView());
222
223     ASSERT_EQ(EWK_PROCESS_MODEL_MULTIPLE_SECONDARY, ewk_context_process_model_get(context));
224
225     Ewk_Page_Group* pageGroup = ewk_view_page_group_get(webView());
226     Evas* evas = ecore_evas_get(backingStore());
227     Evas_Smart* smart = evas_smart_class_new(&(ewkViewClass()->sc));
228
229     Evas_Object* webView1 = ewk_view_smart_add(evas, smart, context, pageGroup);
230     Evas_Object* webView2 = ewk_view_smart_add(evas, smart, context, pageGroup);
231
232     PlatformProcessIdentifier webView1WebProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processIdentifier();
233     PlatformProcessIdentifier webView2WebProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processIdentifier();
234     PlatformProcessIdentifier webView1NetworkProcessID = toImpl(EWKViewGetWKView(webView1))->page()->process().processPool().networkProcess()->processIdentifier();
235     PlatformProcessIdentifier webView2NetworkProcessID = toImpl(EWKViewGetWKView(webView2))->page()->process().processPool().networkProcess()->processIdentifier();
236
237     ASSERT_NE(webView1WebProcessID, webView2WebProcessID);
238     ASSERT_NE(webView1WebProcessID, webView1NetworkProcessID);
239     ASSERT_NE(webView1WebProcessID, webView2NetworkProcessID);
240 }
241
242 TEST_F(EWK2ContextTest, ewk_context_new)
243 {
244     Ewk_Context* context = ewk_context_new();
245     ASSERT_TRUE(context);
246     ewk_object_unref(context);
247 }
248
249 TEST_F(EWK2ContextTestWithExtension, ewk_context_new_with_extensions_path)
250 {
251     bool received = false;
252     Ewk_Context* context = ewk_view_context_get(webView());
253
254     ewk_context_message_from_extensions_callback_set(context, messageReceivedCallback, &received);
255
256     ewk_view_url_set(webView(), environment->defaultTestPageUrl());
257     ASSERT_TRUE(waitUntilLoadFinished());
258
259     Eina_Value* body = eina_value_new(EINA_VALUE_TYPE_STRING);
260     eina_value_set(body, "From UIProcess");
261     ewk_context_message_post_to_extensions(context, "ping", body);
262     eina_value_free(body);
263
264     ASSERT_TRUE(waitUntilTrue(received, testTimeoutSeconds));
265 }
266
267 TEST_F(EWK2ContextTest, ewk_context_additional_plugin_path_set)
268 {
269     Ewk_Context* context = ewk_view_context_get(webView());
270
271     ASSERT_FALSE(ewk_context_additional_plugin_path_set(context, 0));
272
273     ASSERT_TRUE(ewk_context_additional_plugin_path_set(context, "/plugins"));
274
275     /* FIXME: Get additional plugin path and compare with the path. */
276 }
277
278 static char* s_acceptLanguages = nullptr;
279
280 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
281 {
282     if (message->method != SOUP_METHOD_GET) {
283         soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
284         return;
285     }
286
287     soup_message_set_status(message, SOUP_STATUS_OK);
288     s_acceptLanguages = strdup(soup_message_headers_get_one(message->request_headers, "Accept-Language"));
289
290     soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, s_acceptLanguages, strlen(s_acceptLanguages));
291     soup_message_body_complete(message->response_body);
292 }
293
294 TEST_F(EWK2UnitTestBase, ewk_context_preferred_languages)
295 {
296     Eina_List* acceptLanguages;
297     acceptLanguages = eina_list_append(acceptLanguages, "ko_kr");
298     acceptLanguages = eina_list_append(acceptLanguages, "fr");
299     acceptLanguages = eina_list_append(acceptLanguages, "en");
300
301     ewk_context_preferred_languages_set(acceptLanguages);
302
303     std::unique_ptr<EWK2UnitTestServer> httpServer = std::make_unique<EWK2UnitTestServer>();
304     httpServer->run(serverCallback);
305
306     ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
307     ASSERT_STREQ("ko-kr, fr;q=0.90, en;q=0.80", s_acceptLanguages);
308     free(s_acceptLanguages);
309
310     ewk_context_preferred_languages_set(nullptr);
311     ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
312     ASSERT_STREQ("en-US", s_acceptLanguages);
313     free(s_acceptLanguages);
314 }