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

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: rebase 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
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)
30 {}
31 ~PrerenderTestContentBrowserClient() override {}
32
33 void EnableVisibilityOverride(
34 blink::WebPageVisibilityState visibility_override) {
35 override_enabled_ = true;
36 visibility_override_ = visibility_override;
37 }
38
39 void OverridePageVisibilityState(
40 RenderFrameHost* render_frame_host,
41 blink::WebPageVisibilityState* visibility_state) override {
42 if (override_enabled_)
43 *visibility_state = visibility_override_;
44 }
45
46 private:
47 bool override_enabled_;
48 blink::WebPageVisibilityState visibility_override_;
49 };
jochen (gone - plz use gerrit) 2015/01/26 16:18:51 disallow copy/assign
mlamouri (slow - plz ping) 2015/01/26 17:40:43 Done.
50
21 } // anonymous namespace 51 } // anonymous namespace
22 52
23 using RenderFrameHostImplBrowserTest = ContentBrowserTest; 53 using RenderFrameHostImplBrowserTest = ContentBrowserTest;
24 54
25 // Test that when creating a new window, the main frame is correctly focused. 55 // Test that when creating a new window, the main frame is correctly focused.
26 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, IsFocused_AtLoad) { 56 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, IsFocused_AtLoad) {
27 EXPECT_TRUE( 57 EXPECT_TRUE(
28 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html"))); 58 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html")));
29 59
30 // The main frame should be focused. 60 // The main frame should be focused.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 // is not focused. 101 // is not focused.
72 EXPECT_EQ(web_contents->GetMainFrame(), web_contents->GetFocusedFrame()); 102 EXPECT_EQ(web_contents->GetMainFrame(), web_contents->GetFocusedFrame());
73 103
74 #if defined(OS_MACOSX) 104 #if defined(OS_MACOSX)
75 EXPECT_TRUE(ToRFHI(web_contents->GetMainFrame())->IsFocused()); 105 EXPECT_TRUE(ToRFHI(web_contents->GetMainFrame())->IsFocused());
76 #else 106 #else
77 EXPECT_FALSE(ToRFHI(web_contents->GetMainFrame())->IsFocused()); 107 EXPECT_FALSE(ToRFHI(web_contents->GetMainFrame())->IsFocused());
78 #endif 108 #endif
79 } 109 }
80 110
111 // Test that a frame is visible/hidden depending on its WebContents visibility
112 // state.
113 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
114 GetVisibilityState_Basic) {
115 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
116 WebContents* web_contents = shell()->web_contents();
117
118 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
119 ToRFHI(web_contents->GetMainFrame())->GetVisibilityState());
120
121 web_contents->WasHidden();
122 EXPECT_EQ(blink::WebPageVisibilityStateHidden,
123 ToRFHI(web_contents->GetMainFrame())->GetVisibilityState());
124 }
125
126 // Test that a frame visibility can be overridden by the ContentBrowserClient.
127 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest,
128 GetVisibilityState_Override) {
129 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo")));
130 WebContents* web_contents = shell()->web_contents();
131
132 PrerenderTestContentBrowserClient new_client;
133 ContentBrowserClient* old_client = SetBrowserClientForTesting(&new_client);
134
135 EXPECT_EQ(blink::WebPageVisibilityStateVisible,
136 ToRFHI(web_contents->GetMainFrame())->GetVisibilityState());
137
138 new_client.EnableVisibilityOverride(blink::WebPageVisibilityStatePrerender);
139 EXPECT_EQ(blink::WebPageVisibilityStatePrerender,
140 ToRFHI(web_contents->GetMainFrame())->GetVisibilityState());
141
142 SetBrowserClientForTesting(old_client);
143 }
144
81 } // namespace content 145 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698