OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
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.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 "url/gurl.h" | |
16 | |
17 namespace content { | |
18 | |
19 class PluginPowerSaverHelperTest : public RenderViewTest { | |
20 public: | |
21 PluginPowerSaverHelperTest() : sink_(NULL) {} | |
22 | |
23 RenderFrameImpl* frame() { | |
24 return static_cast<RenderFrameImpl*>(view_->GetMainRenderFrame()); | |
25 } | |
26 | |
27 PluginPowerSaverHelper* power_saver_helper() { | |
28 return frame()->plugin_power_saver_helper(); | |
29 } | |
30 | |
31 void SetUp() override { | |
32 RenderViewTest::SetUp(); | |
33 sink_ = &render_thread_->sink(); | |
34 } | |
35 | |
36 protected: | |
37 IPC::TestSink* sink_; | |
38 | |
39 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest); | |
40 }; | |
41 | |
42 TEST_F(PluginPowerSaverHelperTest, AllowSameOrigin) { | |
43 bool cross_origin = false; | |
44 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | |
45 GURL(), 100, 100, &cross_origin)); | |
46 EXPECT_FALSE(cross_origin); | |
47 | |
48 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | |
49 GURL(), 1000, 1000, &cross_origin)); | |
50 EXPECT_FALSE(cross_origin); | |
51 } | |
52 | |
53 TEST_F(PluginPowerSaverHelperTest, DisallowCrossOriginUnlessLarge) { | |
54 bool cross_origin = false; | |
55 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( | |
56 GURL("http://other.com"), 100, 100, &cross_origin)); | |
57 EXPECT_TRUE(cross_origin); | |
58 | |
59 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | |
60 GURL("http://other.com"), 1000, 1000, &cross_origin)); | |
61 EXPECT_TRUE(cross_origin); | |
62 } | |
63 | |
64 TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) { | |
65 bool cross_origin = false; | |
66 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( | |
67 GURL("http://other.com"), 100, 100, &cross_origin)); | |
68 EXPECT_TRUE(cross_origin); | |
69 | |
70 // Clear out other messages so we find just the plugin power saver IPCs. | |
71 sink_->ClearMessages(); | |
72 | |
73 power_saver_helper()->WhitelistContentOrigin(GURL("http://other.com")); | |
74 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | |
75 GURL("http://other.com"), 100, 100, &cross_origin)); | |
76 EXPECT_TRUE(cross_origin); | |
77 | |
78 // Test that we've sent an IPC to the browser. | |
79 ASSERT_EQ(1u, sink_->message_count()); | |
80 const IPC::Message* msg = sink_->GetMessageAt(0); | |
81 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID, msg->type()); | |
82 FrameHostMsg_PluginContentOriginAllowed::Param params; | |
83 FrameHostMsg_PluginContentOriginAllowed::Read(msg, ¶ms); | |
84 EXPECT_EQ(GURL("http://other.com"), params.a); | |
85 } | |
86 | |
87 TEST_F(PluginPowerSaverHelperTest, UnthrottleOnExPostFactoWhitelist) { | |
88 base::RunLoop loop; | |
89 power_saver_helper()->RegisterPeripheralPlugin(GURL("http://other.com"), | |
90 loop.QuitClosure()); | |
91 | |
92 std::set<GURL> origin_whitelist; | |
93 origin_whitelist.insert(GURL("http://other.com")); | |
94 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist( | |
95 frame()->GetRoutingID(), origin_whitelist)); | |
96 | |
97 // Runs until the unthrottle closure is run. | |
Charlie Reis
2014/10/31 16:33:40
Is there an expectation we should be testing here?
tommycli
2014/10/31 18:32:29
The unthrottle closure is the loop.QuitClosure.
I
| |
98 loop.Run(); | |
99 } | |
100 | |
101 TEST_F(PluginPowerSaverHelperTest, ClearWhitelistOnNavigate) { | |
102 power_saver_helper()->WhitelistContentOrigin(GURL("http://other.com")); | |
103 | |
104 bool cross_origin = false; | |
105 EXPECT_FALSE(power_saver_helper()->ShouldThrottleContent( | |
106 GURL("http://other.com"), 100, 100, &cross_origin)); | |
107 EXPECT_TRUE(cross_origin); | |
108 | |
109 LoadHTML("<html></html>"); | |
110 | |
111 EXPECT_TRUE(power_saver_helper()->ShouldThrottleContent( | |
112 GURL("http://other.com"), 100, 100, &cross_origin)); | |
113 EXPECT_TRUE(cross_origin); | |
114 } | |
115 | |
116 } // namespace content | |
OLD | NEW |