[M120 Migration][MM] Support W3C EME
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / unittest / utc_blink_cb_popup_blocked.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 #define URL  ("http://google.com")
8
9 class utc_blink_cb_popup_blocked : public utc_blink_ewk_base
10 {
11 protected:
12   utc_blink_cb_popup_blocked()
13     : target_uri(NULL)
14     , old_can_open_windows_setting_(false)
15   {
16   }
17
18   void PostSetUp() override {
19     Ewk_Settings* settings = ewk_view_settings_get(GetEwkWebView());
20     old_can_open_windows_setting_ = ewk_settings_scripts_can_open_windows_get(settings);
21
22     evas_object_smart_callback_add(GetEwkWebView(), "popup,blocked", ToSmartCallback(popup_blocked_cb), this);
23   }
24
25   void PreTearDown() override {
26     evas_object_smart_callback_del(GetEwkWebView(), "popup,blocked", ToSmartCallback(popup_blocked_cb));
27
28     Ewk_Settings* settings = ewk_view_settings_get(GetEwkWebView());
29     ewk_settings_scripts_can_open_windows_set(settings, old_can_open_windows_setting_);
30   }
31
32   static void popup_blocked_cb(utc_blink_cb_popup_blocked* owner, Evas_Object*, Eina_Stringshare* uri)
33   {
34     ASSERT_TRUE(owner);
35     owner->target_uri = eina_stringshare_add(uri);
36     owner->EventLoopStop(Success);
37   }
38
39   static void window_open_script_executed(Evas_Object* o, const char* result_value, utc_blink_cb_popup_blocked* owner)
40   {
41     ASSERT_TRUE(owner);
42     owner->EventLoopStop(Failure);
43   }
44
45 protected:
46   Eina_Stringshare* target_uri;
47   bool old_can_open_windows_setting_;
48 };
49
50 /**
51  * @brief Tests "popup,blocked" callback
52  *
53  * By default chromium-efl will block all window.open JS requests that were not produced by "user gesture"
54  */
55 TEST_F(utc_blink_cb_popup_blocked, callback)
56 {
57   char htmlBuffer[] = "<html>"
58                         "<head></head>"
59                         "<body onload=\"window.open('http://www.google.com/')\"></body>"
60                       "</html>";
61
62   Ewk_Settings* settings = ewk_view_settings_get(GetEwkWebView());
63   ewk_settings_scripts_can_open_windows_set(settings, false);
64
65   ASSERT_EQ(EINA_TRUE, ewk_view_html_string_load(GetEwkWebView(), htmlBuffer, NULL, NULL));
66   ASSERT_EQ(Success, EventLoopStart());
67   ASSERT_STREQ("http://www.google.com/", target_uri);
68
69   ewk_settings_scripts_can_open_windows_set(settings, true);
70
71   // ewk_view_script_execute works like "user gesture" so window.open should not be blocked
72   ASSERT_EQ(EINA_TRUE, ewk_view_script_execute(GetEwkWebView(), "window.open('http://www.google.com/');", reinterpret_cast<Ewk_View_Script_Execute_Cb>(window_open_script_executed), this));
73   ASSERT_EQ(Failure, EventLoopStart());
74 }