[M120 Migration][MM] Support W3C EME
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / unittest / utc_blink_ewk_policy_decision_suspend_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 class utc_blink_ewk_policy_decision_suspend : public utc_blink_ewk_base {
8 protected:
9   utc_blink_ewk_policy_decision_suspend()
10     : policy_decision(NULL)
11   {
12   }
13
14   void LoadFinished(Evas_Object* webview) override {
15     EventLoopStop(utc_blink_ewk_base::Failure); // will noop if EventLoopStop was already called
16   }
17
18   static void policy_decision_suspend(utc_blink_ewk_policy_decision_suspend* owner, Evas_Object* webview, Ewk_Policy_Decision* policy_decision)
19   {
20     utc_message("[policy_decision_suspend] :: \n");
21     ASSERT_TRUE(owner);
22     EXPECT_TRUE(policy_decision);
23
24     if (!owner->policy_decision) {
25       // We need to handle suspend here, as if suspend fails then policy_decision is treated as not decided and deleted
26       if (EINA_TRUE == ewk_policy_decision_suspend(policy_decision)) {
27         owner->policy_decision = policy_decision;
28       }
29
30       owner->EventLoopStop(utc_blink_ewk_base::Success);
31     }
32   }
33
34   static void fail_event_loop(utc_blink_ewk_policy_decision_suspend* owner, Evas_Object* webview, void*)
35   {
36     ASSERT_TRUE(owner);
37     owner->policy_decision = NULL;
38     owner->EventLoopStop(Failure);
39   }
40
41 protected:
42   Ewk_Policy_Decision *policy_decision;
43 };
44
45 /**
46  * @brief Tests if suspend operation for policy decision is set properly
47  */
48 TEST_F(utc_blink_ewk_policy_decision_suspend, SuspendNavigation)
49 {
50   evas_object_smart_callback_auto sc(GetEwkWebView(), "policy,navigation,decide", ToSmartCallback(policy_decision_suspend), this);
51
52   ASSERT_EQ(EINA_TRUE, ewk_view_url_set(GetEwkWebView(), "http://www.google.com"));
53   // Wait for policy decision smart callback
54   ASSERT_EQ(Success, EventLoopStart());
55
56   ASSERT_FALSE(policy_decision);
57   // TODO: enable path below if we manage to support suspend for navigation
58   /*
59   ASSERT_TRUE(policy_decision) << "suspend failed - suspend for navigation may be not supported";
60
61   // we've suspended navigation - try waiting for load finished. It should timeout
62   ASSERT_EQ(Timeout, EventLoopStart(5.0));
63
64   // now resume it
65   ASSERT_EQ(EINA_TRUE, ewk_policy_decision_use(policy_decision));
66
67   // and wait till request finished - LoadFinished sets error to Failure
68   ASSERT_EQ(Failure, EventLoopStart());
69   */
70 }
71
72 TEST_F(utc_blink_ewk_policy_decision_suspend, SuspendNewWindow)
73 {
74   evas_object_smart_callback_auto sc(GetEwkWebView(), "policy,newwindow,decide", ToSmartCallback(policy_decision_suspend), this);
75   evas_object_smart_callback_auto popup_blocked(GetEwkWebView(), "popup,blocked", ToSmartCallback(fail_event_loop), this);
76   evas_object_smart_callback_auto create_window(GetEwkWebView(), "create,window", ToSmartCallback(fail_event_loop), this);
77
78   ASSERT_EQ(EINA_TRUE, ewk_view_html_string_load(GetEwkWebView(), "<html><body>Page</body></html>", NULL, NULL));
79
80   // Wait for page to load
81   ASSERT_EQ(Failure, EventLoopStart());
82
83   // execute window.open
84   ewk_view_script_execute(GetEwkWebView(), "window.open('www.google.com');", NULL, NULL);
85   // Wait for policy,newwindow,decide callback
86   ASSERT_EQ(Success, EventLoopStart());
87
88   ASSERT_TRUE(policy_decision) << "suspend failed - suspend for newwindow may be not supported";
89
90   // we've suspended decision - we should not get create,window or popup,blocked smart callback
91   ASSERT_EQ(Timeout, EventLoopStart(5.0));
92
93   // Allow new window
94   ASSERT_EQ(EINA_TRUE, ewk_policy_decision_use(policy_decision));
95
96   // window,create will NULLify policy_decision to was called - it will be called synchronously from ewk_policy_decision_use
97   ASSERT_FALSE(policy_decision);
98 }
99
100 TEST_F(utc_blink_ewk_policy_decision_suspend, SuspendResponse)
101 {
102   evas_object_smart_callback_auto sc(GetEwkWebView(), "policy,response,decide", ToSmartCallback(policy_decision_suspend), this);
103
104   ASSERT_EQ(EINA_TRUE, ewk_view_url_set(GetEwkWebView(), "http://www.google.com"));
105   // Wait for policy decision smart callback
106   ASSERT_EQ(Success, EventLoopStart());
107
108   ASSERT_TRUE(policy_decision) << "suspend failed - suspend for response may be not supported";
109
110   // we've suspended navigation - try waiting for load finished. It should timeout
111   ASSERT_EQ(Timeout, EventLoopStart(5.0));
112
113   // now resume it
114   ASSERT_EQ(EINA_TRUE, ewk_policy_decision_use(policy_decision));
115
116   // and wait till request finished - LoadFinished sets error to Failure
117   ASSERT_EQ(Failure, EventLoopStart());
118 }
119
120 /**
121  * @brief Tests if function works properly in case of NULL of a webview
122  */
123 TEST_F(utc_blink_ewk_policy_decision_suspend, InvalidArgs)
124 {
125   ASSERT_EQ(EINA_FALSE, ewk_policy_decision_suspend(NULL));
126 }