1 // Copyright 2015 The Chromium Authors. 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.
5 #include "content/browser/frame_host/render_frame_host_impl.h"
7 #include "content/browser/web_contents/web_contents_impl.h"
8 #include "content/public/browser/render_frame_host.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/common/content_client.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/public/test/test_utils.h"
14 #include "content/shell/browser/shell.h"
15 #include "content/test/test_content_browser_client.h"
21 // Implementation of ContentBrowserClient that overrides
22 // OverridePageVisibilityState() and allows consumers to set a value.
23 class PrerenderTestContentBrowserClient
: public TestContentBrowserClient
{
25 PrerenderTestContentBrowserClient()
26 : override_enabled_(false),
27 visibility_override_(blink::WebPageVisibilityStateVisible
)
29 ~PrerenderTestContentBrowserClient() override
{}
31 void EnableVisibilityOverride(
32 blink::WebPageVisibilityState visibility_override
) {
33 override_enabled_
= true;
34 visibility_override_
= visibility_override
;
37 void OverridePageVisibilityState(
38 RenderFrameHost
* render_frame_host
,
39 blink::WebPageVisibilityState
* visibility_state
) override
{
40 if (override_enabled_
)
41 *visibility_state
= visibility_override_
;
45 bool override_enabled_
;
46 blink::WebPageVisibilityState visibility_override_
;
48 DISALLOW_COPY_AND_ASSIGN(PrerenderTestContentBrowserClient
);
51 } // anonymous namespace
53 // TODO(mlamouri): part of these tests were removed because they were dependent
54 // on an environment were focus is guaranteed. This is only for
55 // interactive_ui_tests so these bits need to move there.
56 // See https://crbug.com/491535
57 using RenderFrameHostImplBrowserTest
= ContentBrowserTest
;
59 // Test that when creating a new window, the main frame is correctly focused.
60 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest
,
63 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html")));
65 // The main frame should be focused.
66 WebContents
* web_contents
= shell()->web_contents();
67 EXPECT_EQ(web_contents
->GetMainFrame(), web_contents
->GetFocusedFrame());
70 // Test that if the content changes the focused frame, it is correctly exposed.
71 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest
,
74 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html")));
76 WebContents
* web_contents
= shell()->web_contents();
78 std::string frames
[2] = { "frame1", "frame2" };
79 for (const std::string
& frame
: frames
) {
80 ExecuteScriptAndGetValue(
81 web_contents
->GetMainFrame(), "focus" + frame
+ "()");
83 // The main frame is not the focused frame in the frame tree but the main
84 // frame is focused per RFHI rules because one of its descendant is focused.
85 // TODO(mlamouri): we should check the frame focus state per RFHI, see the
86 // general comment at the beginning of this test file.
87 EXPECT_NE(web_contents
->GetMainFrame(), web_contents
->GetFocusedFrame());
88 EXPECT_EQ(frame
, web_contents
->GetFocusedFrame()->GetFrameName());
92 // Tests focus behavior when the focused frame is removed from the frame tree.
93 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest
, RemoveFocusedFrame
) {
95 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html")));
97 WebContentsImpl
* web_contents
=
98 static_cast<WebContentsImpl
*>(shell()->web_contents());
100 ExecuteScriptAndGetValue(web_contents
->GetMainFrame(), "focusframe4()");
102 EXPECT_NE(web_contents
->GetMainFrame(), web_contents
->GetFocusedFrame());
103 EXPECT_EQ("frame4", web_contents
->GetFocusedFrame()->GetFrameName());
105 web_contents
->GetFocusedFrame()->GetParent()->GetFrameName());
106 EXPECT_NE(-1, web_contents
->GetFrameTree()->focused_frame_tree_node_id_
);
108 ExecuteScriptAndGetValue(web_contents
->GetMainFrame(), "detachframe(3)");
109 EXPECT_EQ(nullptr, web_contents
->GetFocusedFrame());
110 EXPECT_EQ(-1, web_contents
->GetFrameTree()->focused_frame_tree_node_id_
);
112 ExecuteScriptAndGetValue(web_contents
->GetMainFrame(), "focusframe2()");
113 EXPECT_NE(nullptr, web_contents
->GetFocusedFrame());
114 EXPECT_NE(web_contents
->GetMainFrame(), web_contents
->GetFocusedFrame());
115 EXPECT_NE(-1, web_contents
->GetFrameTree()->focused_frame_tree_node_id_
);
117 ExecuteScriptAndGetValue(web_contents
->GetMainFrame(), "detachframe(2)");
118 EXPECT_EQ(nullptr, web_contents
->GetFocusedFrame());
119 EXPECT_EQ(-1, web_contents
->GetFrameTree()->focused_frame_tree_node_id_
);
122 // Test that a frame is visible/hidden depending on its WebContents visibility
124 // Flaky on Mac. http://crbug.com/467670
125 #if defined(OS_MACOSX)
126 #define MAYBE_GetVisibilityState_Basic DISABLED_GetVisibilityState_Basic
128 #define MAYBE_GetVisibilityState_Basic GetVisibilityState_Basic
130 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest
,
131 MAYBE_GetVisibilityState_Basic
) {
132 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
133 WebContents
* web_contents
= shell()->web_contents();
135 EXPECT_EQ(blink::WebPageVisibilityStateVisible
,
136 web_contents
->GetMainFrame()->GetVisibilityState());
138 web_contents
->WasHidden();
139 EXPECT_EQ(blink::WebPageVisibilityStateHidden
,
140 web_contents
->GetMainFrame()->GetVisibilityState());
143 // Test that a frame visibility can be overridden by the ContentBrowserClient.
144 // Flaky on Mac. http://crbug.com/525592
145 #if defined(OS_MACOSX)
146 #define MAYBE_GetVisibilityState_Override DISABLED_GetVisibilityState_Override
148 #define MAYBE_GetVisibilityState_Override GetVisibilityState_Override
150 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest
,
151 MAYBE_GetVisibilityState_Override
) {
152 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
153 WebContents
* web_contents
= shell()->web_contents();
155 PrerenderTestContentBrowserClient new_client
;
156 ContentBrowserClient
* old_client
= SetBrowserClientForTesting(&new_client
);
158 EXPECT_EQ(blink::WebPageVisibilityStateVisible
,
159 web_contents
->GetMainFrame()->GetVisibilityState());
161 new_client
.EnableVisibilityOverride(blink::WebPageVisibilityStatePrerender
);
162 EXPECT_EQ(blink::WebPageVisibilityStatePrerender
,
163 web_contents
->GetMainFrame()->GetVisibilityState());
165 SetBrowserClientForTesting(old_client
);
168 } // namespace content