Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: content/browser/frame_host/render_frame_host_impl_browsertest.cc

Issue 871443004: Expose the visibility state of a frame to the browser process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@rfh_isfocused
Patch Set: remove from RFH Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/frame_host/render_frame_host_impl.h" 5 #include "content/browser/frame_host/render_frame_host_impl.h"
6 #include "content/public/browser/render_frame_host.h" 6 #include "content/public/browser/render_frame_host.h"
7 #include "content/public/browser/web_contents.h" 7 #include "content/public/browser/web_contents.h"
8 #include "content/public/common/content_client.h"
8 #include "content/public/test/content_browser_test.h" 9 #include "content/public/test/content_browser_test.h"
9 #include "content/public/test/content_browser_test_utils.h" 10 #include "content/public/test/content_browser_test_utils.h"
10 #include "content/public/test/test_utils.h" 11 #include "content/public/test/test_utils.h"
11 #include "content/shell/browser/shell.h" 12 #include "content/shell/browser/shell.h"
13 #include "content/test/test_content_browser_client.h"
12 14
13 namespace content { 15 namespace content {
14 16
15 namespace { 17 namespace {
16 18
17 RenderFrameHostImpl* ToRFHI(RenderFrameHost* render_frame_host) { 19 RenderFrameHostImpl* ToRFHI(RenderFrameHost* render_frame_host) {
18 return static_cast<RenderFrameHostImpl*>(render_frame_host); 20 return static_cast<RenderFrameHostImpl*>(render_frame_host);
19 } 21 }
20 22
23 // Implementation of ContentBrowserClient that overrides IsPrerendering() and
24 // allows consumers to set an arbitrary value. Note that by default,
25 // ContentBrowserClient returns false for IsPrerendering() so unless
26 // set_prerender_override() is called, this will have no effect.
27 class PrerenderTestContentBrowserClient : public TestContentBrowserClient {
28 public:
29 PrerenderTestContentBrowserClient()
30 : prerender_override_(false)
31 {}
32 ~PrerenderTestContentBrowserClient() override {}
33
34 void set_prerender_override(bool prerender_override) {
35 prerender_override_ = prerender_override;
36 }
37
38 bool IsPrerendering(RenderFrameHost* render_frame_host) override {
39 return prerender_override_;
40 }
41
42 private:
43 bool prerender_override_;
44 };
45
21 } // anonymous namespace 46 } // anonymous namespace
22 47
23 using RenderFrameHostImplBrowserTest = ContentBrowserTest; 48 using RenderFrameHostImplBrowserTest = ContentBrowserTest;
24 49
25 // Test that when creating a new window, the main frame is correctly focused. 50 // Test that when creating a new window, the main frame is correctly focused.
26 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, IsFocused_AtLoad) { 51 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, IsFocused_AtLoad) {
27 EXPECT_TRUE( 52 EXPECT_TRUE(
28 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html"))); 53 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html")));
29 54
30 // The main frame should be the focused frame according to WebContents. 55 // The main frame should be the focused frame according to WebContents.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 NavigateToURL(new_shell, GetTestUrl("render_frame_host", "focus.html"))); 93 NavigateToURL(new_shell, GetTestUrl("render_frame_host", "focus.html")));
69 EXPECT_TRUE(ToRFHI(new_shell->web_contents()->GetMainFrame())->IsFocused()); 94 EXPECT_TRUE(ToRFHI(new_shell->web_contents()->GetMainFrame())->IsFocused());
70 95
71 // The first opened window is no longer focused. The main frame is still the 96 // The first opened window is no longer focused. The main frame is still the
72 // focused frame in the frame tree but as far as RFH is concerned, the frame 97 // focused frame in the frame tree but as far as RFH is concerned, the frame
73 // is not focused. 98 // is not focused.
74 EXPECT_EQ(web_contents->GetMainFrame(), web_contents->GetFocusedFrame()); 99 EXPECT_EQ(web_contents->GetMainFrame(), web_contents->GetFocusedFrame());
75 EXPECT_FALSE(ToRFHI(web_contents->GetMainFrame())->IsFocused()); 100 EXPECT_FALSE(ToRFHI(web_contents->GetMainFrame())->IsFocused());
76 } 101 }
77 102
103 // Test that a frame is visible/hidden depending on its WebContents visibility
104 // state.
105 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
106 GetVisibilityState_Basic) {
107 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
108 WebContents* web_contents = shell()->web_contents();
109
110 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
111 ToRFHI(web_contents->GetMainFrame())->GetVisibilityState());
112
113 web_contents->WasHidden();
114 EXPECT_EQ(blink::WebPageVisibilityStateHidden,
115 ToRFHI(web_contents->GetMainFrame())->GetVisibilityState());
116 }
117
118 // Test that a frame is marked as prerendered if
119 // ContentBrowserClient::IsPrerendering() returns true.
120 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
121 GetVisibilityState_Prerender) {
122 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
123 WebContents* web_contents = shell()->web_contents();
124
125 PrerenderTestContentBrowserClient new_client;
126 ContentBrowserClient* old_client = SetBrowserClientForTesting(&new_client);
127
128 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
129 ToRFHI(web_contents->GetMainFrame())->GetVisibilityState());
130
131 new_client.set_prerender_override(true);
132 EXPECT_EQ(blink::WebPageVisibilityStatePrerender,
133 ToRFHI(web_contents->GetMainFrame())->GetVisibilityState());
134
135 SetBrowserClientForTesting(old_client);
136 }
137
78 } // namespace content 138 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698