Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/run_loop.h" | 5 #include "base/run_loop.h" |
| 6 #include "content/common/frame_messages.h" | 6 #include "content/common/frame_messages.h" |
| 7 #include "content/common/view_message_enums.h" | 7 #include "content/common/view_message_enums.h" |
| 8 #include "content/public/common/content_constants.h" | 8 #include "content/public/common/content_constants.h" |
| 9 #include "content/public/test/frame_load_waiter.h" | 9 #include "content/public/test/frame_load_waiter.h" |
| 10 #include "content/public/test/render_view_test.h" | 10 #include "content/public/test/render_view_test.h" |
| 11 #include "content/renderer/pepper/plugin_power_saver_helper.h" | 11 #include "content/renderer/pepper/plugin_power_saver_helper.h" |
| 12 #include "content/renderer/render_frame_impl.h" | 12 #include "content/renderer/render_frame_impl.h" |
| 13 #include "content/renderer/render_view_impl.h" | 13 #include "content/renderer/render_view_impl.h" |
| 14 #include "content/test/test_render_frame.h" | 14 #include "content/test/test_render_frame.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/WebKit/public/web/WebDocument.h" | 16 #include "third_party/WebKit/public/web/WebDocument.h" |
| 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 18 #include "third_party/WebKit/public/web/WebPluginParams.h" | 18 #include "third_party/WebKit/public/web/WebPluginParams.h" |
| 19 #include "ui/gfx/geometry/size.h" | |
| 19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 | 23 |
| 23 class PluginPowerSaverHelperTest : public RenderViewTest { | 24 class PluginPowerSaverHelperTest : public RenderViewTest { |
| 24 public: | 25 public: |
| 25 PluginPowerSaverHelperTest() : sink_(NULL) {} | 26 PluginPowerSaverHelperTest() : sink_(NULL) {} |
| 26 | 27 |
| 27 void SetUp() override { | 28 void SetUp() override { |
| 28 RenderViewTest::SetUp(); | 29 RenderViewTest::SetUp(); |
| 29 sink_ = &render_thread_->sink(); | 30 sink_ = &render_thread_->sink(); |
| 30 } | 31 } |
| 31 | 32 |
| 32 RenderFrameImpl* frame() { | 33 RenderFrameImpl* frame() { |
| 33 return static_cast<RenderFrameImpl*>(view_->GetMainRenderFrame()); | 34 return static_cast<RenderFrameImpl*>(view_->GetMainRenderFrame()); |
| 34 } | 35 } |
| 35 | 36 |
| 36 blink::WebPluginParams MakeParams(const std::string& url, | |
| 37 const std::string& poster, | |
| 38 const std::string& width, | |
| 39 const std::string& height) { | |
| 40 const size_t size = 3; | |
| 41 blink::WebVector<blink::WebString> names(size); | |
| 42 blink::WebVector<blink::WebString> values(size); | |
| 43 | |
| 44 blink::WebPluginParams params; | |
| 45 params.url = GURL(url); | |
| 46 | |
| 47 params.attributeNames.swap(names); | |
| 48 params.attributeValues.swap(values); | |
| 49 | |
| 50 params.attributeNames[0] = "poster"; | |
| 51 params.attributeNames[1] = "height"; | |
| 52 params.attributeNames[2] = "width"; | |
| 53 params.attributeValues[0] = blink::WebString::fromUTF8(poster); | |
| 54 params.attributeValues[1] = blink::WebString::fromUTF8(height); | |
| 55 params.attributeValues[2] = blink::WebString::fromUTF8(width); | |
| 56 | |
| 57 return params; | |
| 58 } | |
| 59 | |
| 60 protected: | 37 protected: |
| 61 IPC::TestSink* sink_; | 38 IPC::TestSink* sink_; |
| 62 | 39 |
| 63 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest); | 40 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest); |
| 64 }; | 41 }; |
| 65 | 42 |
| 66 TEST_F(PluginPowerSaverHelperTest, AllowSameOrigin) { | |
| 67 EXPECT_FALSE(frame()->ShouldThrottleContent( | |
| 68 url::Origin(GURL("http://same.com")), | |
| 69 url::Origin(GURL("http://same.com")), 100, 100, nullptr)); | |
| 70 EXPECT_FALSE(frame()->ShouldThrottleContent( | |
| 71 url::Origin(GURL("http://same.com")), | |
| 72 url::Origin(GURL("http://same.com")), 1000, 1000, nullptr)); | |
| 73 } | |
| 74 | |
| 75 TEST_F(PluginPowerSaverHelperTest, DisallowCrossOriginUnlessLarge) { | |
| 76 bool cross_origin_main_content = false; | |
| 77 EXPECT_TRUE( | |
| 78 frame()->ShouldThrottleContent(url::Origin(GURL("http://same.com")), | |
| 79 url::Origin(GURL("http://other.com")), 100, | |
| 80 100, &cross_origin_main_content)); | |
| 81 EXPECT_FALSE(cross_origin_main_content); | |
| 82 | |
| 83 EXPECT_FALSE( | |
| 84 frame()->ShouldThrottleContent(url::Origin(GURL("http://same.com")), | |
| 85 url::Origin(GURL("http://other.com")), | |
| 86 1000, 1000, &cross_origin_main_content)); | |
| 87 EXPECT_TRUE(cross_origin_main_content); | |
| 88 } | |
| 89 | |
| 90 TEST_F(PluginPowerSaverHelperTest, AlwaysAllowTinyContent) { | |
| 91 bool cross_origin_main_content = false; | |
| 92 EXPECT_FALSE(frame()->ShouldThrottleContent( | |
| 93 url::Origin(GURL("http://same.com")), | |
| 94 url::Origin(GURL("http://same.com")), 1, 1, nullptr)); | |
| 95 EXPECT_FALSE(cross_origin_main_content); | |
| 96 | |
| 97 EXPECT_FALSE(frame()->ShouldThrottleContent( | |
| 98 url::Origin(GURL("http://same.com")), | |
| 99 url::Origin(GURL("http://other.com")), 1, 1, &cross_origin_main_content)); | |
| 100 EXPECT_FALSE(cross_origin_main_content); | |
| 101 | |
| 102 EXPECT_FALSE(frame()->ShouldThrottleContent( | |
| 103 url::Origin(GURL("http://same.com")), | |
| 104 url::Origin(GURL("http://other.com")), 5, 5, &cross_origin_main_content)); | |
| 105 EXPECT_FALSE(cross_origin_main_content); | |
| 106 | |
| 107 EXPECT_TRUE( | |
| 108 frame()->ShouldThrottleContent(url::Origin(GURL("http://same.com")), | |
| 109 url::Origin(GURL("http://other.com")), 10, | |
| 110 10, &cross_origin_main_content)); | |
| 111 EXPECT_FALSE(cross_origin_main_content); | |
| 112 } | |
| 113 | |
| 114 TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) { | 43 TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) { |
| 115 bool cross_origin_main_content = false; | 44 EXPECT_EQ(RenderFrame::CONTENT_STATUS_PERIPHERAL, |
|
groby-ooo-7-16
2015/12/09 02:17:15
It bugs me these have to be browser tests. Any cha
tommycli
2015/12/09 23:25:45
For this particular test, I think it needs to be a
| |
| 116 EXPECT_TRUE( | 45 frame()->GetPeripheralContentStatus( |
| 117 frame()->ShouldThrottleContent(url::Origin(GURL("http://same.com")), | 46 url::Origin(GURL("http://same.com")), |
| 118 url::Origin(GURL("http://other.com")), 100, | 47 url::Origin(GURL("http://other.com")), gfx::Size(100, 100))); |
| 119 100, &cross_origin_main_content)); | |
| 120 EXPECT_FALSE(cross_origin_main_content); | |
| 121 | 48 |
| 122 // Clear out other messages so we find just the plugin power saver IPCs. | 49 // Clear out other messages so we find just the plugin power saver IPCs. |
| 123 sink_->ClearMessages(); | 50 sink_->ClearMessages(); |
| 124 | 51 |
| 125 frame()->WhitelistContentOrigin(url::Origin(GURL("http://other.com"))); | 52 frame()->WhitelistContentOrigin(url::Origin(GURL("http://other.com"))); |
| 126 EXPECT_FALSE( | 53 |
| 127 frame()->ShouldThrottleContent(url::Origin(GURL("http://same.com")), | 54 EXPECT_EQ(RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_WHITELISTED, |
| 128 url::Origin(GURL("http://other.com")), 100, | 55 frame()->GetPeripheralContentStatus( |
| 129 100, &cross_origin_main_content)); | 56 url::Origin(GURL("http://same.com")), |
| 130 EXPECT_FALSE(cross_origin_main_content); | 57 url::Origin(GURL("http://other.com")), gfx::Size(100, 100))); |
| 131 | 58 |
| 132 // Test that we've sent an IPC to the browser. | 59 // Test that we've sent an IPC to the browser. |
| 133 ASSERT_EQ(1u, sink_->message_count()); | 60 ASSERT_EQ(1u, sink_->message_count()); |
| 134 const IPC::Message* msg = sink_->GetMessageAt(0); | 61 const IPC::Message* msg = sink_->GetMessageAt(0); |
| 135 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID, msg->type()); | 62 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID, msg->type()); |
| 136 FrameHostMsg_PluginContentOriginAllowed::Param params; | 63 FrameHostMsg_PluginContentOriginAllowed::Param params; |
| 137 FrameHostMsg_PluginContentOriginAllowed::Read(msg, ¶ms); | 64 FrameHostMsg_PluginContentOriginAllowed::Read(msg, ¶ms); |
| 138 EXPECT_TRUE(url::Origin(GURL("http://other.com")) | 65 EXPECT_TRUE(url::Origin(GURL("http://other.com")) |
| 139 .IsSameOriginWith(base::get<0>(params))); | 66 .IsSameOriginWith(base::get<0>(params))); |
| 140 } | 67 } |
| 141 | 68 |
| 142 TEST_F(PluginPowerSaverHelperTest, UnthrottleOnExPostFactoWhitelist) { | 69 TEST_F(PluginPowerSaverHelperTest, UnthrottleOnExPostFactoWhitelist) { |
| 143 base::RunLoop loop; | 70 base::RunLoop loop; |
| 144 frame()->RegisterPeripheralPlugin(url::Origin(GURL("http://other.com")), | 71 frame()->RegisterPeripheralPlugin(url::Origin(GURL("http://other.com")), |
| 145 loop.QuitClosure()); | 72 loop.QuitClosure()); |
| 146 | 73 |
| 147 std::set<url::Origin> origin_whitelist; | 74 std::set<url::Origin> origin_whitelist; |
| 148 origin_whitelist.insert(url::Origin(GURL("http://other.com"))); | 75 origin_whitelist.insert(url::Origin(GURL("http://other.com"))); |
| 149 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist( | 76 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist( |
| 150 frame()->GetRoutingID(), origin_whitelist)); | 77 frame()->GetRoutingID(), origin_whitelist)); |
| 151 | 78 |
| 152 // Runs until the unthrottle closure is run. | 79 // Runs until the unthrottle closure is run. |
| 153 loop.Run(); | 80 loop.Run(); |
| 154 } | 81 } |
| 155 | 82 |
| 156 TEST_F(PluginPowerSaverHelperTest, ClearWhitelistOnNavigate) { | 83 TEST_F(PluginPowerSaverHelperTest, ClearWhitelistOnNavigate) { |
| 157 frame()->WhitelistContentOrigin(url::Origin(GURL("http://other.com"))); | 84 frame()->WhitelistContentOrigin(url::Origin(GURL("http://other.com"))); |
| 158 | 85 |
| 159 EXPECT_FALSE(frame()->ShouldThrottleContent( | 86 EXPECT_EQ(RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_WHITELISTED, |
| 160 url::Origin(GURL("http://same.com")), | 87 frame()->GetPeripheralContentStatus( |
| 161 url::Origin(GURL("http://other.com")), 100, 100, nullptr)); | 88 url::Origin(GURL("http://same.com")), |
| 89 url::Origin(GURL("http://other.com")), gfx::Size(100, 100))); | |
| 162 | 90 |
| 163 LoadHTML("<html></html>"); | 91 LoadHTML("<html></html>"); |
| 164 | 92 |
| 165 EXPECT_TRUE(frame()->ShouldThrottleContent( | 93 EXPECT_EQ(RenderFrame::CONTENT_STATUS_PERIPHERAL, |
| 166 url::Origin(GURL("http://same.com")), | 94 frame()->GetPeripheralContentStatus( |
| 167 url::Origin(GURL("http://other.com")), 100, 100, nullptr)); | 95 url::Origin(GURL("http://same.com")), |
| 96 url::Origin(GURL("http://other.com")), gfx::Size(100, 100))); | |
| 168 } | 97 } |
| 169 | 98 |
| 170 } // namespace content | 99 } // namespace content |
| OLD | NEW |