Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / frame_host / render_frame_host_impl_browsertest.cc
blobf80f7eea4bfb06bcd122f03d81362d1699919a09
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"
6 #include "content/public/browser/render_frame_host.h"
7 #include "content/public/browser/web_contents.h"
8 #include "content/public/common/content_client.h"
9 #include "content/public/test/content_browser_test.h"
10 #include "content/public/test/content_browser_test_utils.h"
11 #include "content/public/test/test_utils.h"
12 #include "content/shell/browser/shell.h"
13 #include "content/test/test_content_browser_client.h"
15 namespace content {
17 namespace {
19 RenderFrameHostImpl* ToRFHI(RenderFrameHost* render_frame_host) {
20 return static_cast<RenderFrameHostImpl*>(render_frame_host);
23 // Implementation of ContentBrowserClient that overrides
24 // OverridePageVisibilityState() and allows consumers to set a value.
25 class PrerenderTestContentBrowserClient : public TestContentBrowserClient {
26 public:
27 PrerenderTestContentBrowserClient()
28 : override_enabled_(false),
29 visibility_override_(blink::WebPageVisibilityStateVisible)
31 ~PrerenderTestContentBrowserClient() override {}
33 void EnableVisibilityOverride(
34 blink::WebPageVisibilityState visibility_override) {
35 override_enabled_ = true;
36 visibility_override_ = visibility_override;
39 void OverridePageVisibilityState(
40 RenderFrameHost* render_frame_host,
41 blink::WebPageVisibilityState* visibility_state) override {
42 if (override_enabled_)
43 *visibility_state = visibility_override_;
46 private:
47 bool override_enabled_;
48 blink::WebPageVisibilityState visibility_override_;
50 DISALLOW_COPY_AND_ASSIGN(PrerenderTestContentBrowserClient);
53 } // anonymous namespace
55 using RenderFrameHostImplBrowserTest = ContentBrowserTest;
57 // Test that when creating a new window, the main frame is correctly focused.
58 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, IsFocused_AtLoad) {
59 EXPECT_TRUE(
60 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html")));
62 // The main frame should be focused.
63 WebContents* web_contents = shell()->web_contents();
64 EXPECT_TRUE(ToRFHI(web_contents->GetMainFrame())->IsFocused());
67 // Test that if the content changes the focused frame, it is correctly exposed.
68 // Disabled to to flaky failures: crbug.com/452631
69 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
70 DISABLED_IsFocused_Change) {
71 EXPECT_TRUE(
72 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html")));
74 WebContents* web_contents = shell()->web_contents();
76 std::string frames[2] = { "frame1", "frame2" };
77 for (const std::string& frame : frames) {
78 ExecuteScriptAndGetValue(
79 web_contents->GetMainFrame(), "focus" + frame + "()");
81 // The main frame is not the focused frame in the frame tree but the main
82 // frame is focused per RFHI rules because one of its descendant is focused.
83 EXPECT_NE(web_contents->GetMainFrame(), web_contents->GetFocusedFrame());
84 EXPECT_TRUE(ToRFHI(web_contents->GetFocusedFrame())->IsFocused());
85 EXPECT_TRUE(ToRFHI(web_contents->GetMainFrame())->IsFocused());
86 EXPECT_EQ(frame, web_contents->GetFocusedFrame()->GetFrameName());
90 // Test that even if the frame is focused in the frame tree but its
91 // RenderWidgetHost is not focused, it is not considered as focused.
92 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, IsFocused_Widget) {
93 EXPECT_TRUE(
94 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html")));
95 WebContents* web_contents = shell()->web_contents();
97 // A second window is created and navigated. It takes the focus.
98 Shell* new_shell = CreateBrowser();
99 EXPECT_TRUE(
100 NavigateToURL(new_shell, GetTestUrl("render_frame_host", "focus.html")));
101 EXPECT_TRUE(ToRFHI(new_shell->web_contents()->GetMainFrame())->IsFocused());
103 // The first opened window is no longer focused. The main frame is still the
104 // focused frame in the frame tree but as far as RFH is concerned, the frame
105 // is not focused.
106 EXPECT_EQ(web_contents->GetMainFrame(), web_contents->GetFocusedFrame());
108 #if defined(OS_MACOSX)
109 EXPECT_TRUE(ToRFHI(web_contents->GetMainFrame())->IsFocused());
110 #else
111 EXPECT_FALSE(ToRFHI(web_contents->GetMainFrame())->IsFocused());
112 #endif
115 // Test that a frame is visible/hidden depending on its WebContents visibility
116 // state.
117 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
118 GetVisibilityState_Basic) {
119 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
120 WebContents* web_contents = shell()->web_contents();
122 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
123 web_contents->GetMainFrame()->GetVisibilityState());
125 web_contents->WasHidden();
126 EXPECT_EQ(blink::WebPageVisibilityStateHidden,
127 web_contents->GetMainFrame()->GetVisibilityState());
130 // Test that a frame visibility can be overridden by the ContentBrowserClient.
131 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
132 GetVisibilityState_Override) {
133 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
134 WebContents* web_contents = shell()->web_contents();
136 PrerenderTestContentBrowserClient new_client;
137 ContentBrowserClient* old_client = SetBrowserClientForTesting(&new_client);
139 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
140 web_contents->GetMainFrame()->GetVisibilityState());
142 new_client.EnableVisibilityOverride(blink::WebPageVisibilityStatePrerender);
143 EXPECT_EQ(blink::WebPageVisibilityStatePrerender,
144 web_contents->GetMainFrame()->GetVisibilityState());
146 SetBrowserClientForTesting(old_client);
149 } // namespace content