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
3 // found in the LICENSE file.
5 #include "base/run_loop.h"
6 #include "content/common/frame_messages.h"
7 #include "content/common/view_message_enums.h"
8 #include "content/public/common/content_constants.h"
9 #include "content/public/test/render_view_test.h"
10 #include "content/renderer/pepper/plugin_power_saver_helper.h"
11 #include "content/renderer/render_frame_impl.h"
12 #include "content/renderer/render_view_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/web/WebDocument.h"
15 #include "third_party/WebKit/public/web/WebLocalFrame.h"
16 #include "third_party/WebKit/public/web/WebPluginParams.h"
21 class PluginPowerSaverHelperTest
: public RenderViewTest
{
23 PluginPowerSaverHelperTest() : sink_(NULL
) {}
25 RenderFrameImpl
* frame() {
26 return static_cast<RenderFrameImpl
*>(view_
->GetMainRenderFrame());
29 PluginPowerSaverHelper
* helper() {
30 return frame()->plugin_power_saver_helper();
33 void SetUp() override
{
34 RenderViewTest::SetUp();
35 sink_
= &render_thread_
->sink();
38 blink::WebPluginParams
MakeParams(const std::string
& url
,
39 const std::string
& poster
,
40 const std::string
& width
,
41 const std::string
& height
) {
42 const size_t size
= 3;
43 blink::WebVector
<blink::WebString
> names(size
);
44 blink::WebVector
<blink::WebString
> values(size
);
46 blink::WebPluginParams params
;
47 params
.url
= GURL(url
);
49 params
.attributeNames
.swap(names
);
50 params
.attributeValues
.swap(values
);
52 params
.attributeNames
[0] = "poster";
53 params
.attributeNames
[1] = "height";
54 params
.attributeNames
[2] = "width";
55 params
.attributeValues
[0] = blink::WebString::fromUTF8(poster
);
56 params
.attributeValues
[1] = blink::WebString::fromUTF8(height
);
57 params
.attributeValues
[2] = blink::WebString::fromUTF8(width
);
65 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest
);
68 TEST_F(PluginPowerSaverHelperTest
, AllowSameOrigin
) {
69 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL(), kFlashPluginName
, 100,
71 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL(), kFlashPluginName
, 1000,
75 TEST_F(PluginPowerSaverHelperTest
, DisallowCrossOriginUnlessLarge
) {
76 bool cross_origin_main_content
= false;
77 EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"),
78 kFlashPluginName
, 100, 100,
79 &cross_origin_main_content
));
80 EXPECT_FALSE(cross_origin_main_content
);
82 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"),
83 kFlashPluginName
, 1000, 1000,
84 &cross_origin_main_content
));
85 EXPECT_TRUE(cross_origin_main_content
);
88 TEST_F(PluginPowerSaverHelperTest
, AlwaysAllowTinyContent
) {
89 bool cross_origin_main_content
= false;
91 helper()->ShouldThrottleContent(GURL(), kFlashPluginName
, 1, 1, nullptr));
92 EXPECT_FALSE(cross_origin_main_content
);
94 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"),
95 kFlashPluginName
, 1, 1,
96 &cross_origin_main_content
));
97 EXPECT_FALSE(cross_origin_main_content
);
99 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"),
100 kFlashPluginName
, 5, 5,
101 &cross_origin_main_content
));
102 EXPECT_FALSE(cross_origin_main_content
);
104 EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"),
105 kFlashPluginName
, 10, 10,
106 &cross_origin_main_content
));
107 EXPECT_FALSE(cross_origin_main_content
);
110 TEST_F(PluginPowerSaverHelperTest
, TemporaryOriginWhitelist
) {
111 bool cross_origin_main_content
= false;
112 EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"),
113 kFlashPluginName
, 100, 100,
114 &cross_origin_main_content
));
115 EXPECT_FALSE(cross_origin_main_content
);
117 // Clear out other messages so we find just the plugin power saver IPCs.
118 sink_
->ClearMessages();
120 helper()->WhitelistContentOrigin(GURL("http://b.com"));
121 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"),
122 kFlashPluginName
, 100, 100,
123 &cross_origin_main_content
));
124 EXPECT_FALSE(cross_origin_main_content
);
126 // Test that we've sent an IPC to the browser.
127 ASSERT_EQ(1u, sink_
->message_count());
128 const IPC::Message
* msg
= sink_
->GetMessageAt(0);
129 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID
, msg
->type());
130 FrameHostMsg_PluginContentOriginAllowed::Param params
;
131 FrameHostMsg_PluginContentOriginAllowed::Read(msg
, ¶ms
);
132 EXPECT_EQ(GURL("http://b.com"), get
<0>(params
));
135 TEST_F(PluginPowerSaverHelperTest
, UnthrottleOnExPostFactoWhitelist
) {
137 frame()->RegisterPeripheralPlugin(GURL("http://other.com"),
140 std::set
<GURL
> origin_whitelist
;
141 origin_whitelist
.insert(GURL("http://other.com"));
142 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist(
143 frame()->GetRoutingID(), origin_whitelist
));
145 // Runs until the unthrottle closure is run.
149 TEST_F(PluginPowerSaverHelperTest
, ClearWhitelistOnNavigate
) {
150 helper()->WhitelistContentOrigin(GURL("http://b.com"));
152 EXPECT_FALSE(helper()->ShouldThrottleContent(
153 GURL("http://b.com"), kFlashPluginName
, 100, 100, nullptr));
155 LoadHTML("<html></html>");
157 EXPECT_TRUE(helper()->ShouldThrottleContent(
158 GURL("http://b.com"), kFlashPluginName
, 100, 100, nullptr));
161 } // namespace content