| OLD | NEW |
| 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 |
| 50 DISALLOW_COPY_AND_ASSIGN(PrerenderTestContentBrowserClient); |
| 51 }; |
| 52 |
| 21 } // anonymous namespace | 53 } // anonymous namespace |
| 22 | 54 |
| 23 using RenderFrameHostImplBrowserTest = ContentBrowserTest; | 55 using RenderFrameHostImplBrowserTest = ContentBrowserTest; |
| 24 | 56 |
| 25 // Test that when creating a new window, the main frame is correctly focused. | 57 // Test that when creating a new window, the main frame is correctly focused. |
| 26 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, IsFocused_AtLoad) { | 58 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, IsFocused_AtLoad) { |
| 27 EXPECT_TRUE( | 59 EXPECT_TRUE( |
| 28 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html"))); | 60 NavigateToURL(shell(), GetTestUrl("render_frame_host", "focus.html"))); |
| 29 | 61 |
| 30 // The main frame should be focused. | 62 // The main frame should be focused. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // is not focused. | 103 // is not focused. |
| 72 EXPECT_EQ(web_contents->GetMainFrame(), web_contents->GetFocusedFrame()); | 104 EXPECT_EQ(web_contents->GetMainFrame(), web_contents->GetFocusedFrame()); |
| 73 | 105 |
| 74 #if defined(OS_MACOSX) | 106 #if defined(OS_MACOSX) |
| 75 EXPECT_TRUE(ToRFHI(web_contents->GetMainFrame())->IsFocused()); | 107 EXPECT_TRUE(ToRFHI(web_contents->GetMainFrame())->IsFocused()); |
| 76 #else | 108 #else |
| 77 EXPECT_FALSE(ToRFHI(web_contents->GetMainFrame())->IsFocused()); | 109 EXPECT_FALSE(ToRFHI(web_contents->GetMainFrame())->IsFocused()); |
| 78 #endif | 110 #endif |
| 79 } | 111 } |
| 80 | 112 |
| 113 // Test that a frame is visible/hidden depending on its WebContents visibility |
| 114 // state. |
| 115 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, |
| 116 GetVisibilityState_Basic) { |
| 117 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo"))); |
| 118 WebContents* web_contents = shell()->web_contents(); |
| 119 |
| 120 EXPECT_EQ(blink::WebPageVisibilityStateVisible, |
| 121 web_contents->GetMainFrame()->GetVisibilityState()); |
| 122 |
| 123 web_contents->WasHidden(); |
| 124 EXPECT_EQ(blink::WebPageVisibilityStateHidden, |
| 125 web_contents->GetMainFrame()->GetVisibilityState()); |
| 126 } |
| 127 |
| 128 // Test that a frame visibility can be overridden by the ContentBrowserClient. |
| 129 IN_PROC_BROWSER_TEST_F(RenderFrameHostImplBrowserTest, |
| 130 GetVisibilityState_Override) { |
| 131 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,foo"))); |
| 132 WebContents* web_contents = shell()->web_contents(); |
| 133 |
| 134 PrerenderTestContentBrowserClient new_client; |
| 135 ContentBrowserClient* old_client = SetBrowserClientForTesting(&new_client); |
| 136 |
| 137 EXPECT_EQ(blink::WebPageVisibilityStateVisible, |
| 138 web_contents->GetMainFrame()->GetVisibilityState()); |
| 139 |
| 140 new_client.EnableVisibilityOverride(blink::WebPageVisibilityStatePrerender); |
| 141 EXPECT_EQ(blink::WebPageVisibilityStatePrerender, |
| 142 web_contents->GetMainFrame()->GetVisibilityState()); |
| 143 |
| 144 SetBrowserClientForTesting(old_client); |
| 145 } |
| 146 |
| 81 } // namespace content | 147 } // namespace content |
| OLD | NEW |