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/test/render_view_test.h"
9 #include "content/renderer/pepper/plugin_power_saver_helper_impl.h"
10 #include "content/renderer/render_frame_impl.h"
11 #include "content/renderer/render_view_impl.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/web/WebDocument.h"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
15 #include "third_party/WebKit/public/web/WebPluginParams.h"
20 class PluginPowerSaverHelperTest
: public RenderViewTest
{
22 PluginPowerSaverHelperTest() : sink_(NULL
) {}
24 RenderFrameImpl
* frame() {
25 return static_cast<RenderFrameImpl
*>(view_
->GetMainRenderFrame());
28 PluginPowerSaverHelperImpl
* power_saver_helper() {
29 return frame()->GetPluginPowerSaverHelper();
32 void SetUp() override
{
33 RenderViewTest::SetUp();
34 sink_
= &render_thread_
->sink();
40 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest
);
43 TEST_F(PluginPowerSaverHelperTest
, PosterImage
) {
45 blink::WebVector
<blink::WebString
> names(size
);
46 blink::WebVector
<blink::WebString
> values(size
);
48 blink::WebPluginParams params
;
49 params
.url
= GURL("http://b.com/foo.swf");
51 params
.attributeNames
.swap(names
);
52 params
.attributeValues
.swap(values
);
54 params
.attributeNames
[0] = "poster";
55 params
.attributeNames
[1] = "height";
56 params
.attributeNames
[2] = "width";
57 params
.attributeValues
[0] = "poster.jpg";
58 params
.attributeValues
[1] = "100";
59 params
.attributeValues
[2] = "100";
61 EXPECT_EQ(GURL("http://a.com/poster.jpg"),
62 power_saver_helper()->GetPluginInstancePosterImage(
63 params
, GURL("http://a.com/page.html")));
65 // Ignore empty poster paramaters.
66 params
.attributeValues
[0] = "";
67 EXPECT_EQ(GURL(), power_saver_helper()->GetPluginInstancePosterImage(
68 params
, GURL("http://a.com/page.html")));
70 // Ignore poster parameter when plugin is big (shouldn't be throttled).
71 params
.attributeValues
[0] = "poster.jpg";
72 params
.attributeValues
[1] = "500";
73 params
.attributeValues
[2] = "500";
74 EXPECT_EQ(GURL(), power_saver_helper()->GetPluginInstancePosterImage(
75 params
, GURL("http://a.com/page.html")));
78 TEST_F(PluginPowerSaverHelperTest
, AllowSameOrigin
) {
80 power_saver_helper()->ShouldThrottleContent(GURL(), 100, 100, nullptr));
83 power_saver_helper()->ShouldThrottleContent(GURL(), 1000, 1000, nullptr));
86 TEST_F(PluginPowerSaverHelperTest
, DisallowCrossOriginUnlessLarge
) {
87 bool is_main_attraction
= false;
88 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent(
89 GURL("http://other.com"), 100, 100, &is_main_attraction
));
90 EXPECT_FALSE(is_main_attraction
);
92 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent(
93 GURL("http://other.com"), 1000, 1000, &is_main_attraction
));
94 EXPECT_TRUE(is_main_attraction
);
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
);
103 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent(
104 GURL("http://other.com"), 1, 1, &is_main_attraction
));
105 EXPECT_FALSE(is_main_attraction
);
107 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent(
108 GURL("http://other.com"), 5, 5, &is_main_attraction
));
109 EXPECT_FALSE(is_main_attraction
);
111 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent(
112 GURL("http://other.com"), 10, 10, &is_main_attraction
));
113 EXPECT_FALSE(is_main_attraction
);
116 TEST_F(PluginPowerSaverHelperTest
, TemporaryOriginWhitelist
) {
117 bool is_main_attraction
= false;
118 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent(
119 GURL("http://other.com"), 100, 100, &is_main_attraction
));
120 EXPECT_FALSE(is_main_attraction
);
122 // Clear out other messages so we find just the plugin power saver IPCs.
123 sink_
->ClearMessages();
125 power_saver_helper()->WhitelistContentOrigin(GURL("http://other.com"));
126 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent(
127 GURL("http://other.com"), 100, 100, &is_main_attraction
));
128 EXPECT_FALSE(is_main_attraction
);
130 // Test that we've sent an IPC to the browser.
131 ASSERT_EQ(1u, sink_
->message_count());
132 const IPC::Message
* msg
= sink_
->GetMessageAt(0);
133 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID
, msg
->type());
134 FrameHostMsg_PluginContentOriginAllowed::Param params
;
135 FrameHostMsg_PluginContentOriginAllowed::Read(msg
, ¶ms
);
136 EXPECT_EQ(GURL("http://other.com"), params
.a
);
139 TEST_F(PluginPowerSaverHelperTest
, UnthrottleOnExPostFactoWhitelist
) {
141 power_saver_helper()->RegisterPeripheralPlugin(GURL("http://other.com"),
144 std::set
<GURL
> origin_whitelist
;
145 origin_whitelist
.insert(GURL("http://other.com"));
146 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist(
147 frame()->GetRoutingID(), origin_whitelist
));
149 // Runs until the unthrottle closure is run.
153 TEST_F(PluginPowerSaverHelperTest
, ClearWhitelistOnNavigate
) {
154 power_saver_helper()->WhitelistContentOrigin(GURL("http://other.com"));
156 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent(
157 GURL("http://other.com"), 100, 100, nullptr));
159 LoadHTML("<html></html>");
161 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent(
162 GURL("http://other.com"), 100, 100, nullptr));
165 } // namespace content