| 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/test/render_view_test.h" | 8 #include "content/public/test/render_view_test.h" |
| 9 #include "content/renderer/pepper/plugin_power_saver_helper_impl.h" | 9 #include "content/renderer/pepper/plugin_power_saver_helper_impl.h" |
| 10 #include "content/renderer/render_frame_impl.h" | 10 #include "content/renderer/render_frame_impl.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 69 |
| 70 // Ignore poster parameter when plugin is big (shouldn't be throttled). | 70 // Ignore poster parameter when plugin is big (shouldn't be throttled). |
| 71 params.attributeValues[0] = "poster.jpg"; | 71 params.attributeValues[0] = "poster.jpg"; |
| 72 params.attributeValues[1] = "500"; | 72 params.attributeValues[1] = "500"; |
| 73 params.attributeValues[2] = "500"; | 73 params.attributeValues[2] = "500"; |
| 74 EXPECT_EQ(GURL(), power_saver_helper()->GetPluginInstancePosterImage( | 74 EXPECT_EQ(GURL(), power_saver_helper()->GetPluginInstancePosterImage( |
| 75 params, GURL("http://a.com/page.html"))); | 75 params, GURL("http://a.com/page.html"))); |
| 76 } | 76 } |
| 77 | 77 |
| 78 TEST_F(PluginPowerSaverHelperTest, AllowSameOrigin) { | 78 TEST_F(PluginPowerSaverHelperTest, AllowSameOrigin) { |
| 79 bool cross_origin = false; | 79 EXPECT_FALSE( |
| 80 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | 80 power_saver_helper()->ShouldThrottleContent(GURL(), 100, 100, nullptr)); |
| 81 GURL(), 100, 100, &cross_origin)); | |
| 82 EXPECT_FALSE(cross_origin); | |
| 83 | 81 |
| 84 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | 82 EXPECT_FALSE( |
| 85 GURL(), 1000, 1000, &cross_origin)); | 83 power_saver_helper()->ShouldThrottleContent(GURL(), 1000, 1000, nullptr)); |
| 86 EXPECT_FALSE(cross_origin); | |
| 87 } | 84 } |
| 88 | 85 |
| 89 TEST_F(PluginPowerSaverHelperTest, DisallowCrossOriginUnlessLarge) { | 86 TEST_F(PluginPowerSaverHelperTest, DisallowCrossOriginUnlessLarge) { |
| 90 bool cross_origin = false; | 87 bool is_main_attraction = false; |
| 91 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( | 88 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( |
| 92 GURL("http://other.com"), 100, 100, &cross_origin)); | 89 GURL("http://other.com"), 100, 100, &is_main_attraction)); |
| 93 EXPECT_TRUE(cross_origin); | 90 EXPECT_FALSE(is_main_attraction); |
| 94 | 91 |
| 95 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | 92 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( |
| 96 GURL("http://other.com"), 1000, 1000, &cross_origin)); | 93 GURL("http://other.com"), 1000, 1000, &is_main_attraction)); |
| 97 EXPECT_TRUE(cross_origin); | 94 EXPECT_TRUE(is_main_attraction); |
| 95 } |
| 96 |
| 97 TEST_F(PluginPowerSaverHelperTest, AlwaysAllowTinyContent) { |
| 98 bool is_main_attraction = false; |
| 99 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( |
| 100 GURL(), 1, 1, &is_main_attraction)); |
| 101 EXPECT_FALSE(is_main_attraction); |
| 102 |
| 103 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( |
| 104 GURL("http://other.com"), 1, 1, &is_main_attraction)); |
| 105 EXPECT_FALSE(is_main_attraction); |
| 106 |
| 107 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( |
| 108 GURL("http://other.com"), 5, 5, &is_main_attraction)); |
| 109 EXPECT_FALSE(is_main_attraction); |
| 110 |
| 111 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( |
| 112 GURL("http://other.com"), 10, 10, &is_main_attraction)); |
| 113 EXPECT_FALSE(is_main_attraction); |
| 98 } | 114 } |
| 99 | 115 |
| 100 TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) { | 116 TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) { |
| 101 bool cross_origin = false; | 117 bool is_main_attraction = false; |
| 102 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( | 118 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( |
| 103 GURL("http://other.com"), 100, 100, &cross_origin)); | 119 GURL("http://other.com"), 100, 100, &is_main_attraction)); |
| 104 EXPECT_TRUE(cross_origin); | 120 EXPECT_FALSE(is_main_attraction); |
| 105 | 121 |
| 106 // Clear out other messages so we find just the plugin power saver IPCs. | 122 // Clear out other messages so we find just the plugin power saver IPCs. |
| 107 sink_->ClearMessages(); | 123 sink_->ClearMessages(); |
| 108 | 124 |
| 109 power_saver_helper()->WhitelistContentOrigin(GURL("http://other.com")); | 125 power_saver_helper()->WhitelistContentOrigin(GURL("http://other.com")); |
| 110 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | 126 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( |
| 111 GURL("http://other.com"), 100, 100, &cross_origin)); | 127 GURL("http://other.com"), 100, 100, &is_main_attraction)); |
| 112 EXPECT_TRUE(cross_origin); | 128 EXPECT_FALSE(is_main_attraction); |
| 113 | 129 |
| 114 // Test that we've sent an IPC to the browser. | 130 // Test that we've sent an IPC to the browser. |
| 115 ASSERT_EQ(1u, sink_->message_count()); | 131 ASSERT_EQ(1u, sink_->message_count()); |
| 116 const IPC::Message* msg = sink_->GetMessageAt(0); | 132 const IPC::Message* msg = sink_->GetMessageAt(0); |
| 117 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID, msg->type()); | 133 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID, msg->type()); |
| 118 FrameHostMsg_PluginContentOriginAllowed::Param params; | 134 FrameHostMsg_PluginContentOriginAllowed::Param params; |
| 119 FrameHostMsg_PluginContentOriginAllowed::Read(msg, ¶ms); | 135 FrameHostMsg_PluginContentOriginAllowed::Read(msg, ¶ms); |
| 120 EXPECT_EQ(GURL("http://other.com"), params.a); | 136 EXPECT_EQ(GURL("http://other.com"), params.a); |
| 121 } | 137 } |
| 122 | 138 |
| 123 TEST_F(PluginPowerSaverHelperTest, UnthrottleOnExPostFactoWhitelist) { | 139 TEST_F(PluginPowerSaverHelperTest, UnthrottleOnExPostFactoWhitelist) { |
| 124 base::RunLoop loop; | 140 base::RunLoop loop; |
| 125 power_saver_helper()->RegisterPeripheralPlugin(GURL("http://other.com"), | 141 power_saver_helper()->RegisterPeripheralPlugin(GURL("http://other.com"), |
| 126 loop.QuitClosure()); | 142 loop.QuitClosure()); |
| 127 | 143 |
| 128 std::set<GURL> origin_whitelist; | 144 std::set<GURL> origin_whitelist; |
| 129 origin_whitelist.insert(GURL("http://other.com")); | 145 origin_whitelist.insert(GURL("http://other.com")); |
| 130 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist( | 146 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist( |
| 131 frame()->GetRoutingID(), origin_whitelist)); | 147 frame()->GetRoutingID(), origin_whitelist)); |
| 132 | 148 |
| 133 // Runs until the unthrottle closure is run. | 149 // Runs until the unthrottle closure is run. |
| 134 loop.Run(); | 150 loop.Run(); |
| 135 } | 151 } |
| 136 | 152 |
| 137 TEST_F(PluginPowerSaverHelperTest, ClearWhitelistOnNavigate) { | 153 TEST_F(PluginPowerSaverHelperTest, ClearWhitelistOnNavigate) { |
| 138 power_saver_helper()->WhitelistContentOrigin(GURL("http://other.com")); | 154 power_saver_helper()->WhitelistContentOrigin(GURL("http://other.com")); |
| 139 | 155 |
| 140 bool cross_origin = false; | |
| 141 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | 156 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( |
| 142 GURL("http://other.com"), 100, 100, &cross_origin)); | 157 GURL("http://other.com"), 100, 100, nullptr)); |
| 143 EXPECT_TRUE(cross_origin); | |
| 144 | 158 |
| 145 LoadHTML("<html></html>"); | 159 LoadHTML("<html></html>"); |
| 146 | 160 |
| 147 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( | 161 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( |
| 148 GURL("http://other.com"), 100, 100, &cross_origin)); | 162 GURL("http://other.com"), 100, 100, nullptr)); |
| 149 EXPECT_TRUE(cross_origin); | |
| 150 } | 163 } |
| 151 | 164 |
| 152 } // namespace content | 165 } // namespace content |
| OLD | NEW |