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/bind.h" | |
6 #include "base/command_line.h" | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "content/public/common/content_constants.h" | |
11 #include "content/public/common/content_switches.h" | |
12 #include "content/renderer/pepper/pepper_plugin_instance_throttler.h" | |
13 #include "content/renderer/pepper/plugin_power_saver_helper.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
17 | |
18 using testing::_; | |
19 using testing::Return; | |
20 | |
21 namespace content { | |
22 | |
23 namespace { | |
24 | |
25 class MockPluginPowerSaverHelper : public PluginPowerSaverHelper { | |
26 public: | |
27 MockPluginPowerSaverHelper() : PluginPowerSaverHelper(NULL) { | |
28 EXPECT_CALL(*this, ShouldThrottleContent(_, _, _, _)) | |
29 .WillRepeatedly(Return(true)); | |
30 } | |
31 | |
32 MOCK_CONST_METHOD4(ShouldThrottleContent, bool(const GURL&, int, int, bool*)); | |
33 MOCK_METHOD2(RegisterPeripheralPlugin, | |
34 void(const GURL&, const base::Closure&)); | |
35 MOCK_METHOD1(WhitelistContentOrigin, void(const GURL&)); | |
36 }; | |
37 | |
38 } // namespace | |
39 | |
40 class PepperPluginInstanceThrottlerTest : public testing::Test { | |
41 protected: | |
42 PepperPluginInstanceThrottlerTest() : change_callback_calls_(0) {} | |
43 | |
44 void SetUp() override { | |
45 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
46 switches::kEnablePluginPowerSaver); | |
47 | |
48 blink::WebRect rect; | |
49 rect.width = 100; | |
50 rect.height = 100; | |
51 throttler_.reset(new PepperPluginInstanceThrottler( | |
52 &power_saver_helper_, rect, kFlashPluginName, | |
53 GURL("http://example.com"), | |
54 base::Bind(&PepperPluginInstanceThrottlerTest::ChangeCallback, | |
55 base::Unretained(this)))); | |
56 } | |
57 | |
58 PepperPluginInstanceThrottler* throttler() { | |
59 DCHECK(throttler_.get()); | |
60 return throttler_.get(); | |
61 } | |
62 | |
63 int change_callback_calls() { return change_callback_calls_; } | |
64 | |
65 void SetPluginThrottledForTest(bool throttled) { | |
66 throttler_->SetPluginThrottled(throttled); | |
67 } | |
68 | |
69 void DisablePowerSaverByRetroactiveWhitelistForTest() { | |
70 throttler_->DisablePowerSaverByRetroactiveWhitelist(); | |
raymes
2014/11/13 00:05:16
You can avoid needing to make this public without
tommycli
2014/11/13 02:33:36
Done.
| |
71 } | |
72 | |
73 private: | |
74 void ChangeCallback() { ++change_callback_calls_; } | |
75 | |
76 scoped_ptr<PepperPluginInstanceThrottler> throttler_; | |
77 MockPluginPowerSaverHelper power_saver_helper_; | |
78 | |
79 int change_callback_calls_; | |
80 | |
81 base::MessageLoop loop_; | |
82 }; | |
83 | |
84 TEST_F(PepperPluginInstanceThrottlerTest, ThrottleAndUnthrottle) { | |
85 EXPECT_FALSE(throttler()->is_throttled()); | |
86 EXPECT_EQ(0, change_callback_calls()); | |
87 | |
88 SetPluginThrottledForTest(true); | |
raymes
2014/11/13 00:05:16
Rather than calling SetPluginThrottledForTest(true
tommycli
2014/11/13 02:33:36
The current 'real' behavior is to simply wait 5 se
| |
89 EXPECT_TRUE(throttler()->is_throttled()); | |
90 EXPECT_EQ(1, change_callback_calls()); | |
91 | |
92 SetPluginThrottledForTest(false); | |
raymes
2014/11/13 00:05:16
Can we use ConsumeInputEvent here instead?
tommycli
2014/11/13 02:33:36
Done. I consolidated this with UnthrottleByClick n
| |
93 EXPECT_FALSE(throttler()->is_throttled()); | |
94 EXPECT_EQ(2, change_callback_calls()); | |
95 } | |
96 | |
97 TEST_F(PepperPluginInstanceThrottlerTest, FastWhitelisting) { | |
98 EXPECT_FALSE(throttler()->is_throttled()); | |
99 EXPECT_EQ(0, change_callback_calls()); | |
100 | |
101 DisablePowerSaverByRetroactiveWhitelistForTest(); | |
102 | |
103 SetPluginThrottledForTest(true); | |
104 EXPECT_FALSE(throttler()->is_throttled()); | |
105 EXPECT_EQ(1, change_callback_calls()); | |
106 } | |
107 | |
108 TEST_F(PepperPluginInstanceThrottlerTest, SlowWhitelisting) { | |
109 EXPECT_FALSE(throttler()->is_throttled()); | |
110 EXPECT_EQ(0, change_callback_calls()); | |
111 | |
112 SetPluginThrottledForTest(true); | |
113 EXPECT_TRUE(throttler()->is_throttled()); | |
114 EXPECT_EQ(1, change_callback_calls()); | |
115 | |
116 DisablePowerSaverByRetroactiveWhitelistForTest(); | |
117 EXPECT_FALSE(throttler()->is_throttled()); | |
118 EXPECT_EQ(2, change_callback_calls()); | |
119 } | |
120 | |
121 TEST_F(PepperPluginInstanceThrottlerTest, UnthrottleByClick) { | |
122 EXPECT_FALSE(throttler()->is_throttled()); | |
123 EXPECT_EQ(0, change_callback_calls()); | |
124 | |
125 SetPluginThrottledForTest(true); | |
126 EXPECT_TRUE(throttler()->is_throttled()); | |
127 EXPECT_EQ(1, change_callback_calls()); | |
128 | |
129 blink::WebMouseEvent event; | |
130 event.type = blink::WebInputEvent::Type::MouseUp; | |
131 EXPECT_TRUE(throttler()->ConsumeInputEvent(event)); | |
raymes
2014/11/13 00:05:16
A few more tests for ConumeInputEvent could be goo
tommycli
2014/11/13 02:33:36
Done. Added a few more tests. Actually this caught
| |
132 | |
133 EXPECT_FALSE(throttler()->is_throttled()); | |
134 EXPECT_EQ(2, change_callback_calls()); | |
135 } | |
136 | |
137 } // namespace content | |
OLD | NEW |