25f62fcf8612830161b0818efd19891ff840b54d
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / tests / test_ewk2_view.cpp
1 /*
2     Copyright (C) 2012 Samsung Electronics
3     Copyright (C) 2012 Intel Corporation. All rights reserved.
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public License
16     along with this library; if not, write to the Free Software Foundation,
17     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "config.h"
21
22 #include "UnitTestUtils/EWK2UnitTestBase.h"
23 #include "UnitTestUtils/EWK2UnitTestEnvironment.h"
24 #include "UnitTestUtils/EWK2UnitTestServer.h"
25 #include <EWebKit2.h>
26 #include <Ecore.h>
27 #include <Eina.h>
28 #include <gtest/gtest.h>
29 #include <wtf/OwnPtr.h>
30 #include <wtf/PassOwnPtr.h>
31 #include <wtf/UnusedParam.h>
32 #include <wtf/text/CString.h>
33 #include <wtf/text/WTFString.h>
34
35 using namespace EWK2UnitTest;
36
37 extern EWK2UnitTestEnvironment* environment;
38 bool fullScreenCallbackCalled;
39
40 static void onLoadFinishedForRedirection(void* userData, Evas_Object*, void*)
41 {
42     int* countLoadFinished = static_cast<int*>(userData);
43     (*countLoadFinished)--;
44 }
45
46 TEST_F(EWK2UnitTestBase, ewk_view_uri_get)
47 {
48     loadUrlSync(environment->defaultTestPageUrl());
49     EXPECT_STREQ(ewk_view_uri_get(webView()), environment->defaultTestPageUrl());
50
51     int countLoadFinished = 2;
52     evas_object_smart_callback_add(webView(), "load,finished", onLoadFinishedForRedirection, &countLoadFinished);
53     ewk_view_uri_set(webView(), environment->urlForResource("redirect_uri_to_default.html").data());
54     while (countLoadFinished)
55         ecore_main_loop_iterate();
56     evas_object_smart_callback_del(webView(), "load,finished", onLoadFinishedForRedirection);
57     EXPECT_STREQ(ewk_view_uri_get(webView()), environment->defaultTestPageUrl());
58 }
59
60 TEST_F(EWK2UnitTestBase, ewk_view_device_pixel_ratio)
61 {
62     loadUrlSync(environment->defaultTestPageUrl());
63
64     // Default pixel ratio is 1.0
65     ASSERT_FLOAT_EQ(ewk_view_device_pixel_ratio_get(webView()), 1);
66
67     ASSERT_TRUE(ewk_view_device_pixel_ratio_set(webView(), 1.2));
68     ASSERT_FLOAT_EQ(ewk_view_device_pixel_ratio_get(webView()), 1.2);
69
70     ASSERT_TRUE(ewk_view_device_pixel_ratio_set(webView(), 1));
71     ASSERT_FLOAT_EQ(ewk_view_device_pixel_ratio_get(webView()), 1);
72 }
73
74 TEST_F(EWK2UnitTestBase, ewk_view_html_string_load)
75 {
76     ewk_view_html_string_load(webView(), "<html><head><title>Foo</title></head><body>Bar</body></html>", 0, 0);
77     ASSERT_TRUE(waitUntilTitleChangedTo("Foo"));
78     ASSERT_STREQ(ewk_view_title_get(webView()), "Foo");
79     ewk_view_html_string_load(webView(), "<html><head><title>Bar</title></head><body>Foo</body></html>", 0, 0);
80     ASSERT_TRUE(waitUntilTitleChangedTo("Bar"));
81     ASSERT_STREQ(ewk_view_title_get(webView()), "Bar");
82 }
83
84 static void serverCallbackNavigation(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
85 {
86     if (message->method != SOUP_METHOD_GET) {
87         soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
88         return;
89     }
90
91     soup_message_set_status(message, SOUP_STATUS_OK);
92
93     Eina_Strbuf* body = eina_strbuf_new();
94     eina_strbuf_append_printf(body, "<html><title>%s</title><body>%s</body></html>", path + 1, path + 1);
95     const size_t bodyLength = eina_strbuf_length_get(body);
96     soup_message_body_append(message->response_body, SOUP_MEMORY_TAKE, eina_strbuf_string_steal(body), bodyLength);
97     eina_strbuf_free(body);
98
99     soup_message_body_complete(message->response_body);
100 }
101
102 TEST_F(EWK2UnitTestBase, ewk_view_navigation)
103 {
104     OwnPtr<EWK2UnitTestServer> httpServer = adoptPtr(new EWK2UnitTestServer);
105     httpServer->run(serverCallbackNavigation);
106
107     // Visit Page1
108     loadUrlSync(httpServer->getURIForPath("/Page1").data());
109     ASSERT_STREQ(ewk_view_title_get(webView()), "Page1");
110     ASSERT_FALSE(ewk_view_back_possible(webView()));
111     ASSERT_FALSE(ewk_view_forward_possible(webView()));
112
113     // Visit Page2
114     loadUrlSync(httpServer->getURIForPath("/Page2").data());
115     ASSERT_STREQ(ewk_view_title_get(webView()), "Page2");
116     ASSERT_TRUE(ewk_view_back_possible(webView()));
117     ASSERT_FALSE(ewk_view_forward_possible(webView()));
118
119     // Go back to Page1
120     ewk_view_back(webView());
121     ASSERT_TRUE(waitUntilTitleChangedTo("Page1"));
122     ASSERT_STREQ(ewk_view_title_get(webView()), "Page1");
123     ASSERT_FALSE(ewk_view_back_possible(webView()));
124     ASSERT_TRUE(ewk_view_forward_possible(webView()));
125
126     // Go forward to Page2
127     ewk_view_forward(webView());
128     ASSERT_TRUE(waitUntilTitleChangedTo("Page2"));
129     ASSERT_STREQ(ewk_view_title_get(webView()), "Page2");
130     ASSERT_TRUE(ewk_view_back_possible(webView()));
131     ASSERT_FALSE(ewk_view_forward_possible(webView()));
132 }
133
134 TEST_F(EWK2UnitTestBase, ewk_view_setting_encoding_custom)
135 {
136     ASSERT_FALSE(ewk_view_setting_encoding_custom_get(webView()));
137     ASSERT_TRUE(ewk_view_setting_encoding_custom_set(webView(), "UTF-8"));
138     ASSERT_STREQ(ewk_view_setting_encoding_custom_get(webView()), "UTF-8");
139     // Set the default charset.
140     ASSERT_TRUE(ewk_view_setting_encoding_custom_set(webView(), 0));
141     ASSERT_FALSE(ewk_view_setting_encoding_custom_get(webView()));
142 }
143
144 static void onFormAboutToBeSubmitted(void* userData, Evas_Object*, void* eventInfo)
145 {
146     Ewk_Form_Submission_Request* request = static_cast<Ewk_Form_Submission_Request*>(eventInfo);
147     bool* handled = static_cast<bool*>(userData);
148
149     ASSERT_TRUE(request);
150
151     Eina_List* fieldNames = ewk_form_submission_request_field_names_get(request);
152     ASSERT_TRUE(fieldNames);
153     ASSERT_EQ(eina_list_count(fieldNames), 3);
154     void* data;
155     EINA_LIST_FREE(fieldNames, data)
156         eina_stringshare_del(static_cast<char*>(data));
157
158     const char* value1 = ewk_form_submission_request_field_value_get(request, "text1");
159     ASSERT_STREQ(value1, "value1");
160     eina_stringshare_del(value1);
161     const char* value2 = ewk_form_submission_request_field_value_get(request, "text2");
162     ASSERT_STREQ(value2, "value2");
163     eina_stringshare_del(value2);
164     const char* password = ewk_form_submission_request_field_value_get(request, "password");
165     ASSERT_STREQ(password, "secret");
166     eina_stringshare_del(password);
167
168     *handled = true;
169 }
170
171 TEST_F(EWK2UnitTestBase, ewk_view_form_submission_request)
172 {
173     const char* formHTML =
174         "<html><head><script type='text/javascript'>function submitForm() { document.getElementById('myform').submit(); }</script></head>"
175         "<body onload='submitForm()'>"
176         " <form id='myform' action='#'>"
177         "  <input type='text' name='text1' value='value1'>"
178         "  <input type='text' name='text2' value='value2'>"
179         "  <input type='password' name='password' value='secret'>"
180         "  <textarea cols='5' rows='5' name='textarea'>Text</textarea>"
181         "  <input type='hidden' name='hidden1' value='hidden1'>"
182         "  <input type='submit' value='Submit'>"
183         " </form>"
184         "</body></html>";
185
186     ewk_view_html_string_load(webView(), formHTML, "file:///", 0);
187     bool handled = false;
188     evas_object_smart_callback_add(webView(), "form,submission,request", onFormAboutToBeSubmitted, &handled);
189     while (!handled)
190         ecore_main_loop_iterate();
191     ASSERT_TRUE(handled);
192     evas_object_smart_callback_del(webView(), "form,submission,request", onFormAboutToBeSubmitted);
193 }
194
195 static inline void checkBasicPopupMenuItem(Ewk_Popup_Menu_Item* item, const char* title, bool enabled)
196 {
197     EXPECT_EQ(ewk_popup_menu_item_type_get(item), EWK_POPUP_MENU_ITEM);
198     EXPECT_STREQ(ewk_popup_menu_item_text_get(item), title);
199     EXPECT_EQ(ewk_popup_menu_item_enabled_get(item), enabled);
200 }
201
202 static Eina_Bool selectItemAfterDelayed(void* data)
203 {
204     EXPECT_TRUE(ewk_view_popup_menu_select(static_cast<Evas_Object*>(data), 0));
205     return ECORE_CALLBACK_CANCEL;
206 }
207
208 static Eina_Bool showPopupMenu(Ewk_View_Smart_Data* smartData, Eina_Rectangle, Ewk_Text_Direction, double, Eina_List* list, int selectedIndex)
209 {
210     EXPECT_EQ(selectedIndex, 2);
211
212     Ewk_Popup_Menu_Item* item = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(list, 0));
213     checkBasicPopupMenuItem(item, "first", true);
214     EXPECT_EQ(ewk_popup_menu_item_text_direction_get(item), EWK_TEXT_DIRECTION_LEFT_TO_RIGHT);
215     EXPECT_STREQ(ewk_popup_menu_item_tooltip_get(item), "");
216     EXPECT_STREQ(ewk_popup_menu_item_accessibility_text_get(item), "");
217     EXPECT_FALSE(ewk_popup_menu_item_is_label_get(item));
218     EXPECT_FALSE(ewk_popup_menu_item_selected_get(item));
219
220     item = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(list, 1));
221     checkBasicPopupMenuItem(item, "second", false);
222     EXPECT_EQ(ewk_popup_menu_item_enabled_get(item), false);
223
224     item = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(list, 2));
225     checkBasicPopupMenuItem(item, "third", true);
226     EXPECT_EQ(ewk_popup_menu_item_text_direction_get(item), EWK_TEXT_DIRECTION_RIGHT_TO_LEFT);
227     EXPECT_STREQ(ewk_popup_menu_item_tooltip_get(item), "tooltip");
228     EXPECT_STREQ(ewk_popup_menu_item_accessibility_text_get(item), "aria");
229     EXPECT_TRUE(ewk_popup_menu_item_selected_get(item));
230
231     item = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(list, 3));
232     checkBasicPopupMenuItem(item, "label", false);
233     EXPECT_TRUE(ewk_popup_menu_item_is_label_get(item));
234
235     item = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(list, 4));
236     checkBasicPopupMenuItem(item, "    forth", true);
237
238     item = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(list, 5));
239     EXPECT_EQ(ewk_popup_menu_item_type_get(item), EWK_POPUP_MENU_UNKNOWN);
240     EXPECT_STREQ(ewk_popup_menu_item_text_get(item), 0);
241
242     ecore_timer_add(0, selectItemAfterDelayed, smartData->self);
243     return true;
244 }
245
246 TEST_F(EWK2UnitTestBase, ewk_view_popup_menu_select)
247 {
248     const char* selectHTML =
249         "<!doctype html><body><select onchange=\"document.title=this.value;\">"
250         "<option>first</option><option disabled>second</option><option selected dir=\"rtl\" title=\"tooltip\" aria-label=\"aria\">third</option>"
251         "<optgroup label=\"label\"><option>forth</option></optgroup>"
252         "</select></body>";
253
254     ewkViewClass()->popup_menu_show = showPopupMenu;
255
256     ewk_view_html_string_load(webView(), selectHTML, "file:///", 0);
257     ASSERT_TRUE(waitUntilLoadFinished());
258     mouseClick(30, 20);
259     ASSERT_TRUE(waitUntilTitleChangedTo("first"));
260
261     EXPECT_TRUE(ewk_view_popup_menu_close(webView()));
262     EXPECT_FALSE(ewk_view_popup_menu_select(webView(), 0));
263 }
264
265 TEST_F(EWK2UnitTestBase, ewk_view_settings_get)
266 {
267     Ewk_Settings* settings = ewk_view_settings_get(webView());
268     ASSERT_TRUE(settings);
269     ASSERT_EQ(settings, ewk_view_settings_get(webView()));
270 }
271
272 TEST_F(EWK2UnitTestBase, ewk_view_theme_set)
273 {
274     const char* buttonHTML = "<html><body><input type='button' id='btn'>"
275         "<script>document.title=document.getElementById('btn').clientWidth;</script>"
276         "</body></html>";
277
278     ewk_view_html_string_load(webView(), buttonHTML, "file:///", 0);
279     EXPECT_TRUE(waitUntilTitleChangedTo("30")); // button of default theme has 30px as padding (15 to -16)
280
281     ewk_view_theme_set(webView(), environment->pathForResource("it_does_not_exist.edj").data());
282     ewk_view_html_string_load(webView(), buttonHTML, "file:///", 0);
283     EXPECT_TRUE(waitUntilTitleChangedTo("30")); // the result should be same as default theme
284
285     ewk_view_theme_set(webView(), environment->pathForResource("empty_theme.edj").data());
286     ewk_view_html_string_load(webView(), buttonHTML, "file:///", 0);
287     EXPECT_TRUE(waitUntilTitleChangedTo("30")); // the result should be same as default theme
288
289     ewk_view_theme_set(webView(), environment->pathForResource("big_button_theme.edj").data());
290     ewk_view_html_string_load(webView(), buttonHTML, "file:///", 0);
291     EXPECT_TRUE(waitUntilTitleChangedTo("299")); // button of big button theme has 299px as padding (150 to -150)
292 }
293
294 TEST_F(EWK2UnitTestBase, ewk_view_mouse_events_enabled)
295 {
296     ASSERT_TRUE(ewk_view_mouse_events_enabled_set(webView(), EINA_TRUE));
297     ASSERT_TRUE(ewk_view_mouse_events_enabled_get(webView()));
298
299     ASSERT_TRUE(ewk_view_mouse_events_enabled_set(webView(), 2));
300     ASSERT_TRUE(ewk_view_mouse_events_enabled_get(webView()));
301
302     ASSERT_TRUE(ewk_view_mouse_events_enabled_set(webView(), EINA_FALSE));
303     ASSERT_FALSE(ewk_view_mouse_events_enabled_get(webView()));
304 }
305
306 static Eina_Bool fullScreenCallback(Ewk_View_Smart_Data* smartData)
307 {
308     fullScreenCallbackCalled = true;
309     return false;
310 }
311
312 static void checkFullScreenProperty(Evas_Object* webView, bool expectedState)
313 {
314     if (environment->useX11Window()) {
315         Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(webView));
316         Ecore_Evas* ecoreEvas = ecore_evas_ecore_evas_get(smartData->base.evas);
317         bool windowState = false;
318         while (((windowState = ecore_evas_fullscreen_get(ecoreEvas)) != expectedState))
319             ecore_main_loop_iterate();
320         ASSERT_TRUE(expectedState == windowState);
321     }
322 }
323
324 TEST_F(EWK2UnitTestBase, ewk_view_full_screen_enter)
325 {
326     const char fullscreenHTML[] =
327         "<!doctype html><head><script>function makeFullScreen(){"
328         "var div = document.getElementById(\"fullscreen\");"
329         "div.webkitRequestFullScreen();"
330         "document.title = \"fullscreen entered\";"
331         "}</script></head>"
332         "<body><div id=\"fullscreen\" style=\"width:100px; height:100px\" onclick=\"makeFullScreen()\"></div></body>";
333
334     ewkViewClass()->fullscreen_enter = fullScreenCallback;
335
336     ewk_view_html_string_load(webView(), fullscreenHTML, "file:///", 0);
337     ASSERT_TRUE(waitUntilLoadFinished());
338     mouseClick(50, 50);
339     ASSERT_TRUE(waitUntilTitleChangedTo("fullscreen entered"));
340     ASSERT_TRUE(fullScreenCallbackCalled);
341     checkFullScreenProperty(webView(), true);
342 }
343
344 TEST_F(EWK2UnitTestBase, ewk_view_full_screen_exit)
345 {
346     const char fullscreenHTML[] =
347         "<!doctype html><head><script>function makeFullScreenAndExit(){"
348         "var div = document.getElementById(\"fullscreen\");"
349         "div.webkitRequestFullScreen();"
350         "document.webkitCancelFullScreen();"
351         "document.title = \"fullscreen exited\";"
352         "}</script></head>"
353         "<body><div id=\"fullscreen\" style=\"width:100px; height:100px\" onclick=\"makeFullScreenAndExit()\"></div></body>";
354
355     ewkViewClass()->fullscreen_exit = fullScreenCallback;
356
357     ewk_view_html_string_load(webView(), fullscreenHTML, "file:///", 0);
358     ASSERT_TRUE(waitUntilLoadFinished());
359     mouseClick(50, 50);
360     ASSERT_TRUE(waitUntilTitleChangedTo("fullscreen exited"));
361     ASSERT_TRUE(fullScreenCallbackCalled);
362     checkFullScreenProperty(webView(), false);
363 }
364
365 TEST_F(EWK2UnitTestBase, ewk_view_same_page_navigation)
366 {
367     // Tests that same page navigation updates the page URI.
368     String testUrl = environment->urlForResource("same_page_navigation.html").data();
369     loadUrlSync(testUrl.utf8().data());
370     ASSERT_STREQ(testUrl.utf8().data(), ewk_view_uri_get(webView()));
371     mouseClick(50, 50);
372     testUrl = testUrl + '#';
373     ASSERT_TRUE(waitUntilURIChangedTo(testUrl.utf8().data()));
374 }
375
376 TEST_F(EWK2UnitTestBase, ewk_view_title_changed)
377 {
378     const char* titleChangedHTML =
379         "<!doctype html><head><title>Title before changed</title></head>"
380         "<body onload=\"document.title='Title after changed';\"></body>";
381     ewk_view_html_string_load(webView(), titleChangedHTML, 0, 0);
382     EXPECT_TRUE(waitUntilTitleChangedTo("Title after changed"));
383     EXPECT_STREQ(ewk_view_title_get(webView()), "Title after changed");
384
385     titleChangedHTML =
386         "<!doctype html><head><title>Title before changed</title></head>"
387         "<body onload=\"document.title='';\"></body>";
388     ewk_view_html_string_load(webView(), titleChangedHTML, 0, 0);
389     EXPECT_TRUE(waitUntilTitleChangedTo(""));
390     EXPECT_STREQ(ewk_view_title_get(webView()), "");
391
392     titleChangedHTML =
393         "<!doctype html><head><title>Title before changed</title></head>"
394         "<body onload=\"document.title=null;\"></body>";
395     ewk_view_html_string_load(webView(), titleChangedHTML, 0, 0);
396     EXPECT_TRUE(waitUntilTitleChangedTo(""));
397     EXPECT_STREQ(ewk_view_title_get(webView()), "");
398 }
399
400 static struct {
401     const char* expectedMessage;
402     bool called;
403 } alertCallbackData;
404
405 static struct {
406     const char* expectedMessage;
407     bool result;
408     bool called;
409 } confirmCallbackData;
410
411 static struct {
412     const char* expectedMessage;
413     const char* expectedDefaultValue;
414     const char* result;
415     bool called;
416 } promptCallbackData;
417
418 static void checkAlert(Ewk_View_Smart_Data*, const char* message)
419 {
420     alertCallbackData.called = true;
421     EXPECT_STREQ(message, alertCallbackData.expectedMessage);
422 }
423
424 TEST_F(EWK2UnitTestBase, ewk_view_run_javascript_alert)
425 {
426     ewkViewClass()->run_javascript_alert = checkAlert;
427
428     const char* alertHTML = "<!doctype html><body onload=\"alert('Alert message');\"></body>";
429     alertCallbackData.expectedMessage = "Alert message";
430     alertCallbackData.called = false;
431     ewk_view_html_string_load(webView(), alertHTML, 0, 0);
432     EXPECT_TRUE(waitUntilLoadFinished());
433     EXPECT_EQ(alertCallbackData.called, true);
434
435     alertHTML = "<!doctype html><body onload=\"alert('');\"></body>";
436     alertCallbackData.expectedMessage = "";
437     alertCallbackData.called = false;
438     ewk_view_html_string_load(webView(), alertHTML, 0, 0);
439     EXPECT_TRUE(waitUntilLoadFinished());
440     EXPECT_EQ(alertCallbackData.called, true);
441
442     alertHTML = "<!doctype html><body onload=\"alert(null);\"></body>";
443     alertCallbackData.expectedMessage = "null";
444     alertCallbackData.called = false;
445     ewk_view_html_string_load(webView(), alertHTML, 0, 0);
446     EXPECT_TRUE(waitUntilLoadFinished());
447     EXPECT_EQ(alertCallbackData.called, true);
448
449     alertHTML = "<!doctype html><body onload=\"alert();\"></body>";
450     alertCallbackData.expectedMessage = "undefined";
451     alertCallbackData.called = false;
452     ewk_view_html_string_load(webView(), alertHTML, 0, 0);
453     EXPECT_TRUE(waitUntilLoadFinished());
454     EXPECT_EQ(alertCallbackData.called, true);
455
456     ewkViewClass()->run_javascript_alert = 0;
457
458     alertCallbackData.called = false;
459     ewk_view_html_string_load(webView(), alertHTML, 0, 0);
460     EXPECT_TRUE(waitUntilLoadFinished());
461     EXPECT_EQ(alertCallbackData.called, false);
462 }
463
464 static Eina_Bool checkConfirm(Ewk_View_Smart_Data*, const char* message)
465 {
466     confirmCallbackData.called = true;
467     EXPECT_STREQ(message, confirmCallbackData.expectedMessage);
468     return confirmCallbackData.result;
469 }
470
471 TEST_F(EWK2UnitTestBase, ewk_view_run_javascript_confirm)
472 {
473     ewkViewClass()->run_javascript_confirm = checkConfirm;
474
475     const char* confirmHTML = "<!doctype html><body onload=\"document.title = confirm('Confirm message');\"></body>";
476     confirmCallbackData.expectedMessage = "Confirm message";
477     confirmCallbackData.result = true;
478     confirmCallbackData.called = false;
479     ewk_view_html_string_load(webView(), confirmHTML, 0, 0);
480     EXPECT_TRUE(waitUntilTitleChangedTo("true"));
481     EXPECT_STREQ(ewk_view_title_get(webView()), "true");
482     EXPECT_EQ(confirmCallbackData.called, true);
483
484     confirmCallbackData.expectedMessage = "Confirm message";
485     confirmCallbackData.result = false;
486     confirmCallbackData.called = false;
487     ewk_view_html_string_load(webView(), confirmHTML, 0, 0);
488     EXPECT_TRUE(waitUntilTitleChangedTo("false"));
489     EXPECT_STREQ(ewk_view_title_get(webView()), "false");
490     EXPECT_EQ(confirmCallbackData.called, true);
491
492     confirmHTML = "<!doctype html><body onload=\"document.title = confirm('');\"></body>";
493     confirmCallbackData.expectedMessage = "";
494     confirmCallbackData.result = true;
495     confirmCallbackData.called = false;
496     ewk_view_html_string_load(webView(), confirmHTML, 0, 0);
497     EXPECT_TRUE(waitUntilTitleChangedTo("true"));
498     EXPECT_STREQ(ewk_view_title_get(webView()), "true");
499     EXPECT_EQ(confirmCallbackData.called, true);
500
501     confirmHTML = "<!doctype html><body onload=\"document.title = confirm(null);\"></body>";
502     confirmCallbackData.expectedMessage = "null";
503     confirmCallbackData.result = true;
504     confirmCallbackData.called = false;
505     ewk_view_html_string_load(webView(), confirmHTML, 0, 0);
506     EXPECT_TRUE(waitUntilTitleChangedTo("true"));
507     EXPECT_STREQ(ewk_view_title_get(webView()), "true");
508     EXPECT_EQ(confirmCallbackData.called, true);
509
510     confirmHTML = "<!doctype html><body onload=\"document.title = confirm();\"></body>";
511     confirmCallbackData.expectedMessage = "undefined";
512     confirmCallbackData.result = true;
513     confirmCallbackData.called = false;
514     ewk_view_html_string_load(webView(), confirmHTML, 0, 0);
515     EXPECT_TRUE(waitUntilTitleChangedTo("true"));
516     EXPECT_STREQ(ewk_view_title_get(webView()), "true");
517     EXPECT_EQ(confirmCallbackData.called, true);
518
519     ewkViewClass()->run_javascript_confirm = 0;
520
521     confirmCallbackData.called = false;
522     ewk_view_html_string_load(webView(), confirmHTML, 0, 0);
523     EXPECT_TRUE(waitUntilTitleChangedTo("false"));
524     EXPECT_STREQ(ewk_view_title_get(webView()), "false");
525     EXPECT_EQ(confirmCallbackData.called, false);
526 }
527
528 static const char* checkPrompt(Ewk_View_Smart_Data*, const char* message, const char* defaultValue)
529 {
530     promptCallbackData.called = true;
531     EXPECT_STREQ(message, promptCallbackData.expectedMessage);
532     EXPECT_STREQ(defaultValue, promptCallbackData.expectedDefaultValue);
533
534     if (!promptCallbackData.result)
535         return 0;
536
537     return eina_stringshare_add(promptCallbackData.result);
538 }
539
540 TEST_F(EWK2UnitTestBase, ewk_view_run_javascript_prompt)
541 {
542     static const char promptMessage[] = "Prompt message";
543     static const char promptResult[] = "Prompt result";
544
545     ewkViewClass()->run_javascript_prompt = checkPrompt;
546
547     const char* promptHTML = "<!doctype html><body onload=\"document.title = prompt('Prompt message', 'Prompt default value');\"></body>";
548     promptCallbackData.expectedMessage = promptMessage;
549     promptCallbackData.expectedDefaultValue = "Prompt default value";
550     promptCallbackData.result = promptResult;
551     promptCallbackData.called = false;
552     ewk_view_html_string_load(webView(), promptHTML, 0, 0);
553     EXPECT_TRUE(waitUntilTitleChangedTo(promptResult));
554     EXPECT_STREQ(ewk_view_title_get(webView()), promptResult);
555     EXPECT_EQ(promptCallbackData.called, true);
556
557     promptHTML = "<!doctype html><body onload=\"document.title = prompt('Prompt message', '');\"></body>";
558     promptCallbackData.expectedMessage = promptMessage;
559     promptCallbackData.expectedDefaultValue = "";
560     promptCallbackData.result = promptResult;
561     promptCallbackData.called = false;
562     ewk_view_html_string_load(webView(), promptHTML, 0, 0);
563     EXPECT_TRUE(waitUntilTitleChangedTo(promptResult));
564     EXPECT_STREQ(ewk_view_title_get(webView()), promptResult);
565     EXPECT_EQ(promptCallbackData.called, true);
566
567     promptHTML = "<!doctype html><body onload=\"document.title = prompt('Prompt message');\"></body>";
568     promptCallbackData.expectedMessage = promptMessage;
569     promptCallbackData.expectedDefaultValue = "";
570     promptCallbackData.result = promptResult;
571     promptCallbackData.called = false;
572     ewk_view_html_string_load(webView(), promptHTML, 0, 0);
573     EXPECT_TRUE(waitUntilTitleChangedTo(promptResult));
574     EXPECT_STREQ(ewk_view_title_get(webView()), promptResult);
575     EXPECT_EQ(promptCallbackData.called, true);
576
577     promptHTML = "<!doctype html><body onload=\"document.title = prompt('');\"></body>";
578     promptCallbackData.expectedMessage = "";
579     promptCallbackData.expectedDefaultValue = "";
580     promptCallbackData.result = promptResult;
581     promptCallbackData.called = false;
582     ewk_view_html_string_load(webView(), promptHTML, 0, 0);
583     EXPECT_TRUE(waitUntilTitleChangedTo(promptResult));
584     EXPECT_STREQ(ewk_view_title_get(webView()), promptResult);
585     EXPECT_EQ(promptCallbackData.called, true);
586
587     promptHTML = "<!doctype html><body onload=\"document.title = prompt();\"></body>";
588     promptCallbackData.expectedMessage = "undefined";
589     promptCallbackData.expectedDefaultValue = "";
590     promptCallbackData.result = promptResult;
591     promptCallbackData.called = false;
592     ewk_view_html_string_load(webView(), promptHTML, 0, 0);
593     EXPECT_TRUE(waitUntilTitleChangedTo(promptResult));
594     EXPECT_STREQ(ewk_view_title_get(webView()), promptResult);
595     EXPECT_EQ(promptCallbackData.called, true);
596
597     promptHTML = "<html><head><title>Default title</title></head>"
598                  "<body onload=\"var promptResult = prompt('Prompt message');"
599                  "if (promptResult == null) document.title='null';"
600                  "else document.title = promptResult;\"></body></html>";
601     promptCallbackData.expectedMessage = promptMessage;
602     promptCallbackData.expectedDefaultValue = "";
603     promptCallbackData.result = "";
604     promptCallbackData.called = false;
605     ewk_view_html_string_load(webView(), promptHTML, 0, 0);
606     EXPECT_TRUE(waitUntilTitleChangedTo(""));
607     EXPECT_STREQ(ewk_view_title_get(webView()), "");
608     EXPECT_EQ(promptCallbackData.called, true);
609
610     promptCallbackData.expectedMessage = promptMessage;
611     promptCallbackData.expectedDefaultValue = "";
612     promptCallbackData.result = 0;
613     promptCallbackData.called = false;
614     ewk_view_html_string_load(webView(), promptHTML, 0, 0);
615     EXPECT_TRUE(waitUntilTitleChangedTo("null"));
616     EXPECT_STREQ(ewk_view_title_get(webView()), "null");
617     EXPECT_EQ(promptCallbackData.called, true);
618
619     ewkViewClass()->run_javascript_prompt = 0;
620
621     promptCallbackData.called = false;
622     ewk_view_html_string_load(webView(), promptHTML, 0, 0);
623     EXPECT_TRUE(waitUntilTitleChangedTo("null"));
624     EXPECT_STREQ(ewk_view_title_get(webView()), "null");
625     EXPECT_EQ(promptCallbackData.called, false);
626 }
627
628 #if ENABLE(INPUT_TYPE_COLOR)
629 static const int initialRed = 0x12;
630 static const int initialGreen = 0x34;
631 static const int initialBlue = 0x56;
632 static const int initialAlpha = 0xff;
633 static const int changedRed = 0x98;
634 static const int changedGreen = 0x76;
635 static const int changedBlue = 0x54;
636 static const int changedAlpha = 0xff;
637
638 static bool isColorPickerShown = false;
639
640 static void onColorPickerDone(void* userData, Evas_Object*, void*)
641 {
642     bool* handled = static_cast<bool*>(userData);
643
644     *handled = true;
645 }
646
647 static unsigned char setColorPickerColor(void* data)
648 {
649     Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(data);
650
651     // 3. Change color to changed color.
652     EXPECT_TRUE(ewk_view_color_picker_color_set(smartData->self, changedRed, changedGreen, changedBlue, changedAlpha));
653
654     evas_object_smart_callback_call(smartData->self, "input,type,color,request", 0);
655
656     return 0;
657 }
658
659 static Eina_Bool showColorPicker(Ewk_View_Smart_Data* smartData, int r, int g, int b, int a)
660 {
661     static bool isFirstRun = true;
662
663     isColorPickerShown = true;
664
665     if (isFirstRun) {
666         // 1. Check initial value from html file.
667         EXPECT_EQ(r, initialRed);
668         EXPECT_EQ(g, initialGreen);
669         EXPECT_EQ(b, initialBlue);
670         EXPECT_EQ(a, initialAlpha);
671
672         isFirstRun = false;
673     } else {
674         // 4. Input values should be same as changed color.
675         EXPECT_EQ(r, changedRed);
676         EXPECT_EQ(g, changedGreen);
677         EXPECT_EQ(b, changedBlue);
678         EXPECT_EQ(a, changedAlpha);
679     }
680
681     // 2. Return after making a color picker.
682     ecore_timer_add(0.0, setColorPickerColor, smartData);
683     return true;
684 }
685
686 static Eina_Bool hideColorPicker(Ewk_View_Smart_Data*)
687 {
688     // Test color picker is shown.
689     EXPECT_TRUE(isColorPickerShown);
690     isColorPickerShown = false;
691 }
692
693 TEST_F(EWK2UnitTestBase, ewk_view_color_picker_color_set)
694 {
695     Ewk_View_Smart_Class* api = ewkViewClass();
696     api->input_picker_color_request = showColorPicker;
697     api->input_picker_color_dismiss = hideColorPicker;
698
699     loadUrlSync("data:text/html,<input type='color' value='#123456'>");
700
701     // Click input element.
702     mouseClick(30, 20);
703
704     bool handled = false;
705     evas_object_smart_callback_add(webView(), "input,type,color,request", onColorPickerDone, &handled);
706     while (!handled)
707         ecore_main_loop_iterate();
708
709     // Click input element again.
710     mouseClick(30, 20);
711
712     handled = false;
713     while (!handled)
714         ecore_main_loop_iterate();
715     evas_object_smart_callback_del(webView(), "input,type,color,request", onColorPickerDone);
716 }
717 #endif // ENABLE(INPUT_TYPE_COLOR)