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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "content/public/common/content_switches.h" | 10 #include "content/public/common/content_switches.h" |
11 #include "content/public/renderer/render_frame.h" | 11 #include "content/public/renderer/render_frame.h" |
12 #include "content/renderer/pepper/pepper_plugin_instance_throttler.h" | 12 #include "content/renderer/pepper/plugin_instance_throttler_impl.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/WebKit/public/web/WebInputEvent.h" | 15 #include "third_party/WebKit/public/web/WebInputEvent.h" |
16 #include "third_party/WebKit/public/web/WebPluginParams.h" | 16 #include "third_party/WebKit/public/web/WebPluginParams.h" |
17 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
18 | 18 |
19 using testing::_; | 19 using testing::_; |
20 using testing::Return; | 20 using testing::Return; |
21 | 21 |
22 class GURL; | 22 class GURL; |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
26 class PepperPluginInstanceThrottlerTest : public testing::Test { | 26 class PluginInstanceThrottlerImplTest |
| 27 : public testing::Test, |
| 28 public PluginInstanceThrottler::Observer { |
27 protected: | 29 protected: |
28 PepperPluginInstanceThrottlerTest() : change_callback_calls_(0) {} | 30 PluginInstanceThrottlerImplTest() : change_callback_calls_(0) {} |
| 31 ~PluginInstanceThrottlerImplTest() override { |
| 32 throttler_->RemoveObserver(this); |
| 33 } |
29 | 34 |
30 void SetUp() override { | 35 void SetUp() override { |
31 blink::WebRect rect; | 36 blink::WebRect rect; |
32 rect.width = 100; | 37 rect.width = 100; |
33 rect.height = 100; | 38 rect.height = 100; |
34 throttler_.reset(new PepperPluginInstanceThrottler( | 39 throttler_.reset(new PluginInstanceThrottlerImpl( |
35 nullptr, rect, true /* is_flash_plugin */, GURL("http://example.com"), | 40 nullptr, GURL("http://example.com"), |
36 content::RenderFrame::POWER_SAVER_MODE_PERIPHERAL_THROTTLED, | 41 PluginInstanceThrottlerImpl::POWER_SAVER_MODE_PERIPHERAL_THROTTLED)); |
37 base::Bind(&PepperPluginInstanceThrottlerTest::ChangeCallback, | 42 throttler_->AddObserver(this); |
38 base::Unretained(this)))); | |
39 } | 43 } |
40 | 44 |
41 PepperPluginInstanceThrottler* throttler() { | 45 PluginInstanceThrottlerImpl* throttler() { |
42 DCHECK(throttler_.get()); | 46 DCHECK(throttler_.get()); |
43 return throttler_.get(); | 47 return throttler_.get(); |
44 } | 48 } |
45 | 49 |
46 void DisablePowerSaverByRetroactiveWhitelist() { | 50 void DisablePowerSaverByRetroactiveWhitelist() { |
47 throttler()->DisablePowerSaver( | 51 throttler()->DisablePowerSaver( |
48 PepperPluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST); | 52 PluginInstanceThrottlerImpl::UNTHROTTLE_METHOD_BY_WHITELIST); |
49 } | 53 } |
50 | 54 |
51 int change_callback_calls() { return change_callback_calls_; } | 55 int change_callback_calls() { return change_callback_calls_; } |
52 | 56 |
53 void EngageThrottle() { | 57 void EngageThrottle() { throttler_->SetPluginThrottled(true); } |
54 throttler_->SetPluginThrottled(true); | |
55 } | |
56 | 58 |
57 void SendEventAndTest(blink::WebInputEvent::Type event_type, | 59 void SendEventAndTest(blink::WebInputEvent::Type event_type, |
58 bool expect_consumed, | 60 bool expect_consumed, |
59 bool expect_throttled, | 61 bool expect_throttled, |
60 int expect_change_callback_count) { | 62 int expect_change_callback_count) { |
61 blink::WebMouseEvent event; | 63 blink::WebMouseEvent event; |
62 event.type = event_type; | 64 event.type = event_type; |
63 event.modifiers = blink::WebInputEvent::Modifiers::LeftButtonDown; | 65 event.modifiers = blink::WebInputEvent::Modifiers::LeftButtonDown; |
64 EXPECT_EQ(expect_consumed, throttler()->ConsumeInputEvent(event)); | 66 EXPECT_EQ(expect_consumed, throttler()->ConsumeInputEvent(event)); |
65 EXPECT_EQ(expect_throttled, throttler()->is_throttled()); | 67 EXPECT_EQ(expect_throttled, throttler()->IsThrottled()); |
66 EXPECT_EQ(expect_change_callback_count, change_callback_calls()); | 68 EXPECT_EQ(expect_change_callback_count, change_callback_calls()); |
67 } | 69 } |
68 | 70 |
69 private: | 71 private: |
70 void ChangeCallback() { ++change_callback_calls_; } | 72 // PluginInstanceThrottlerImpl::Observer |
| 73 void OnThrottleStateChange() override { ++change_callback_calls_; } |
71 | 74 |
72 scoped_ptr<PepperPluginInstanceThrottler> throttler_; | 75 scoped_ptr<PluginInstanceThrottlerImpl> throttler_; |
73 | 76 |
74 int change_callback_calls_; | 77 int change_callback_calls_; |
75 | 78 |
76 base::MessageLoop loop_; | 79 base::MessageLoop loop_; |
77 }; | 80 }; |
78 | 81 |
79 TEST_F(PepperPluginInstanceThrottlerTest, ThrottleAndUnthrottleByClick) { | 82 TEST_F(PluginInstanceThrottlerImplTest, ThrottleAndUnthrottleByClick) { |
80 EXPECT_FALSE(throttler()->is_throttled()); | 83 EXPECT_FALSE(throttler()->IsThrottled()); |
81 EXPECT_EQ(0, change_callback_calls()); | 84 EXPECT_EQ(0, change_callback_calls()); |
82 | 85 |
83 EngageThrottle(); | 86 EngageThrottle(); |
84 EXPECT_TRUE(throttler()->is_throttled()); | 87 EXPECT_TRUE(throttler()->IsThrottled()); |
85 EXPECT_EQ(1, change_callback_calls()); | 88 EXPECT_EQ(1, change_callback_calls()); |
86 | 89 |
87 // MouseUp while throttled should be consumed and disengage throttling. | 90 // MouseUp while throttled should be consumed and disengage throttling. |
88 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, true, false, 2); | 91 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, true, false, 2); |
89 } | 92 } |
90 | 93 |
91 TEST_F(PepperPluginInstanceThrottlerTest, ThrottleByKeyframe) { | 94 TEST_F(PluginInstanceThrottlerImplTest, ThrottleByKeyframe) { |
92 EXPECT_FALSE(throttler()->is_throttled()); | 95 EXPECT_FALSE(throttler()->IsThrottled()); |
93 EXPECT_EQ(0, change_callback_calls()); | 96 EXPECT_EQ(0, change_callback_calls()); |
94 | 97 |
95 SkBitmap boring_bitmap; | 98 SkBitmap boring_bitmap; |
96 gfx::Canvas canvas(gfx::Size(20, 10), 1.0f, true); | 99 gfx::Canvas canvas(gfx::Size(20, 10), 1.0f, true); |
97 canvas.FillRect(gfx::Rect(20, 10), SK_ColorBLACK); | 100 canvas.FillRect(gfx::Rect(20, 10), SK_ColorBLACK); |
98 canvas.FillRect(gfx::Rect(10, 10), SK_ColorWHITE); | 101 canvas.FillRect(gfx::Rect(10, 10), SK_ColorWHITE); |
99 SkBitmap interesting_bitmap = | 102 SkBitmap interesting_bitmap = |
100 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 103 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
101 | 104 |
102 // Don't throttle for a boring frame. | 105 // Don't throttle for a boring frame. |
103 throttler()->OnImageFlush(&boring_bitmap); | 106 throttler()->OnImageFlush(&boring_bitmap); |
104 EXPECT_FALSE(throttler()->is_throttled()); | 107 EXPECT_FALSE(throttler()->IsThrottled()); |
105 EXPECT_EQ(0, change_callback_calls()); | 108 EXPECT_EQ(0, change_callback_calls()); |
106 | 109 |
107 // Don't throttle for non-consecutive interesting frames. | 110 // Don't throttle for non-consecutive interesting frames. |
108 throttler()->OnImageFlush(&interesting_bitmap); | 111 throttler()->OnImageFlush(&interesting_bitmap); |
109 throttler()->OnImageFlush(&boring_bitmap); | 112 throttler()->OnImageFlush(&boring_bitmap); |
110 throttler()->OnImageFlush(&interesting_bitmap); | 113 throttler()->OnImageFlush(&interesting_bitmap); |
111 throttler()->OnImageFlush(&boring_bitmap); | 114 throttler()->OnImageFlush(&boring_bitmap); |
112 throttler()->OnImageFlush(&interesting_bitmap); | 115 throttler()->OnImageFlush(&interesting_bitmap); |
113 throttler()->OnImageFlush(&boring_bitmap); | 116 throttler()->OnImageFlush(&boring_bitmap); |
114 EXPECT_FALSE(throttler()->is_throttled()); | 117 EXPECT_FALSE(throttler()->IsThrottled()); |
115 EXPECT_EQ(0, change_callback_calls()); | 118 EXPECT_EQ(0, change_callback_calls()); |
116 | 119 |
117 // Throttle after consecutive interesting frames. | 120 // Throttle after consecutive interesting frames. |
118 throttler()->OnImageFlush(&interesting_bitmap); | 121 throttler()->OnImageFlush(&interesting_bitmap); |
119 throttler()->OnImageFlush(&interesting_bitmap); | 122 throttler()->OnImageFlush(&interesting_bitmap); |
120 throttler()->OnImageFlush(&interesting_bitmap); | 123 throttler()->OnImageFlush(&interesting_bitmap); |
121 throttler()->OnImageFlush(&interesting_bitmap); | 124 throttler()->OnImageFlush(&interesting_bitmap); |
122 EXPECT_TRUE(throttler()->is_throttled()); | 125 EXPECT_TRUE(throttler()->IsThrottled()); |
123 EXPECT_EQ(1, change_callback_calls()); | 126 EXPECT_EQ(1, change_callback_calls()); |
124 } | 127 } |
125 | 128 |
126 TEST_F(PepperPluginInstanceThrottlerTest, IgnoreThrottlingAfterMouseUp) { | 129 TEST_F(PluginInstanceThrottlerImplTest, IgnoreThrottlingAfterMouseUp) { |
127 EXPECT_FALSE(throttler()->is_throttled()); | 130 EXPECT_FALSE(throttler()->IsThrottled()); |
128 EXPECT_EQ(0, change_callback_calls()); | 131 EXPECT_EQ(0, change_callback_calls()); |
129 | 132 |
130 // MouseUp before throttling engaged should not be consumed, but should | 133 // MouseUp before throttling engaged should not be consumed, but should |
131 // prevent subsequent throttling from engaging. | 134 // prevent subsequent throttling from engaging. |
132 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, false, false, 0); | 135 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, false, false, 0); |
133 | 136 |
134 EngageThrottle(); | 137 EngageThrottle(); |
135 EXPECT_FALSE(throttler()->is_throttled()); | 138 EXPECT_FALSE(throttler()->IsThrottled()); |
136 EXPECT_EQ(0, change_callback_calls()); | 139 EXPECT_EQ(0, change_callback_calls()); |
137 } | 140 } |
138 | 141 |
139 TEST_F(PepperPluginInstanceThrottlerTest, FastWhitelisting) { | 142 TEST_F(PluginInstanceThrottlerImplTest, FastWhitelisting) { |
140 EXPECT_FALSE(throttler()->is_throttled()); | 143 EXPECT_FALSE(throttler()->IsThrottled()); |
141 EXPECT_EQ(0, change_callback_calls()); | 144 EXPECT_EQ(0, change_callback_calls()); |
142 | 145 |
143 DisablePowerSaverByRetroactiveWhitelist(); | 146 DisablePowerSaverByRetroactiveWhitelist(); |
144 | 147 |
145 EngageThrottle(); | 148 EngageThrottle(); |
146 EXPECT_FALSE(throttler()->is_throttled()); | 149 EXPECT_FALSE(throttler()->IsThrottled()); |
147 EXPECT_EQ(1, change_callback_calls()); | 150 EXPECT_EQ(1, change_callback_calls()); |
148 } | 151 } |
149 | 152 |
150 TEST_F(PepperPluginInstanceThrottlerTest, SlowWhitelisting) { | 153 TEST_F(PluginInstanceThrottlerImplTest, SlowWhitelisting) { |
151 EXPECT_FALSE(throttler()->is_throttled()); | 154 EXPECT_FALSE(throttler()->IsThrottled()); |
152 EXPECT_EQ(0, change_callback_calls()); | 155 EXPECT_EQ(0, change_callback_calls()); |
153 | 156 |
154 EngageThrottle(); | 157 EngageThrottle(); |
155 EXPECT_TRUE(throttler()->is_throttled()); | 158 EXPECT_TRUE(throttler()->IsThrottled()); |
156 EXPECT_EQ(1, change_callback_calls()); | 159 EXPECT_EQ(1, change_callback_calls()); |
157 | 160 |
158 DisablePowerSaverByRetroactiveWhitelist(); | 161 DisablePowerSaverByRetroactiveWhitelist(); |
159 EXPECT_FALSE(throttler()->is_throttled()); | 162 EXPECT_FALSE(throttler()->IsThrottled()); |
160 EXPECT_EQ(2, change_callback_calls()); | 163 EXPECT_EQ(2, change_callback_calls()); |
161 } | 164 } |
162 | 165 |
163 TEST_F(PepperPluginInstanceThrottlerTest, EventConsumption) { | 166 TEST_F(PluginInstanceThrottlerImplTest, EventConsumption) { |
164 EXPECT_FALSE(throttler()->is_throttled()); | 167 EXPECT_FALSE(throttler()->IsThrottled()); |
165 EXPECT_EQ(0, change_callback_calls()); | 168 EXPECT_EQ(0, change_callback_calls()); |
166 | 169 |
167 EngageThrottle(); | 170 EngageThrottle(); |
168 EXPECT_TRUE(throttler()->is_throttled()); | 171 EXPECT_TRUE(throttler()->IsThrottled()); |
169 EXPECT_EQ(1, change_callback_calls()); | 172 EXPECT_EQ(1, change_callback_calls()); |
170 | 173 |
171 // Consume but don't unthrottle on a variety of other events. | 174 // Consume but don't unthrottle on a variety of other events. |
172 SendEventAndTest(blink::WebInputEvent::Type::MouseDown, true, true, 1); | 175 SendEventAndTest(blink::WebInputEvent::Type::MouseDown, true, true, 1); |
173 SendEventAndTest(blink::WebInputEvent::Type::MouseWheel, true, true, 1); | 176 SendEventAndTest(blink::WebInputEvent::Type::MouseWheel, true, true, 1); |
174 SendEventAndTest(blink::WebInputEvent::Type::MouseMove, true, true, 1); | 177 SendEventAndTest(blink::WebInputEvent::Type::MouseMove, true, true, 1); |
175 SendEventAndTest(blink::WebInputEvent::Type::KeyDown, true, true, 1); | 178 SendEventAndTest(blink::WebInputEvent::Type::KeyDown, true, true, 1); |
176 SendEventAndTest(blink::WebInputEvent::Type::KeyUp, true, true, 1); | 179 SendEventAndTest(blink::WebInputEvent::Type::KeyUp, true, true, 1); |
177 | 180 |
178 // Consume and unthrottle on MouseUp | 181 // Consume and unthrottle on MouseUp |
179 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, true, false, 2); | 182 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, true, false, 2); |
180 | 183 |
181 // Don't consume events after unthrottle. | 184 // Don't consume events after unthrottle. |
182 SendEventAndTest(blink::WebInputEvent::Type::MouseDown, false, false, 2); | 185 SendEventAndTest(blink::WebInputEvent::Type::MouseDown, false, false, 2); |
183 SendEventAndTest(blink::WebInputEvent::Type::MouseWheel, false, false, 2); | 186 SendEventAndTest(blink::WebInputEvent::Type::MouseWheel, false, false, 2); |
184 SendEventAndTest(blink::WebInputEvent::Type::MouseMove, false, false, 2); | 187 SendEventAndTest(blink::WebInputEvent::Type::MouseMove, false, false, 2); |
185 SendEventAndTest(blink::WebInputEvent::Type::KeyDown, false, false, 2); | 188 SendEventAndTest(blink::WebInputEvent::Type::KeyDown, false, false, 2); |
186 SendEventAndTest(blink::WebInputEvent::Type::KeyUp, false, false, 2); | 189 SendEventAndTest(blink::WebInputEvent::Type::KeyUp, false, false, 2); |
187 | 190 |
188 // Subsequent MouseUps should also not be consumed. | 191 // Subsequent MouseUps should also not be consumed. |
189 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, false, false, 2); | 192 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, false, false, 2); |
190 } | 193 } |
191 | 194 |
192 TEST_F(PepperPluginInstanceThrottlerTest, ThrottleOnLeftClickOnly) { | 195 TEST_F(PluginInstanceThrottlerImplTest, ThrottleOnLeftClickOnly) { |
193 EXPECT_FALSE(throttler()->is_throttled()); | 196 EXPECT_FALSE(throttler()->IsThrottled()); |
194 EXPECT_EQ(0, change_callback_calls()); | 197 EXPECT_EQ(0, change_callback_calls()); |
195 | 198 |
196 EngageThrottle(); | 199 EngageThrottle(); |
197 EXPECT_TRUE(throttler()->is_throttled()); | 200 EXPECT_TRUE(throttler()->IsThrottled()); |
198 EXPECT_EQ(1, change_callback_calls()); | 201 EXPECT_EQ(1, change_callback_calls()); |
199 | 202 |
200 blink::WebMouseEvent event; | 203 blink::WebMouseEvent event; |
201 event.type = blink::WebInputEvent::Type::MouseUp; | 204 event.type = blink::WebInputEvent::Type::MouseUp; |
202 | 205 |
203 event.modifiers = blink::WebInputEvent::Modifiers::RightButtonDown; | 206 event.modifiers = blink::WebInputEvent::Modifiers::RightButtonDown; |
204 EXPECT_FALSE(throttler()->ConsumeInputEvent(event)); | 207 EXPECT_FALSE(throttler()->ConsumeInputEvent(event)); |
205 EXPECT_TRUE(throttler()->is_throttled()); | 208 EXPECT_TRUE(throttler()->IsThrottled()); |
206 | 209 |
207 event.modifiers = blink::WebInputEvent::Modifiers::MiddleButtonDown; | 210 event.modifiers = blink::WebInputEvent::Modifiers::MiddleButtonDown; |
208 EXPECT_TRUE(throttler()->ConsumeInputEvent(event)); | 211 EXPECT_TRUE(throttler()->ConsumeInputEvent(event)); |
209 EXPECT_TRUE(throttler()->is_throttled()); | 212 EXPECT_TRUE(throttler()->IsThrottled()); |
210 | 213 |
211 event.modifiers = blink::WebInputEvent::Modifiers::LeftButtonDown; | 214 event.modifiers = blink::WebInputEvent::Modifiers::LeftButtonDown; |
212 EXPECT_TRUE(throttler()->ConsumeInputEvent(event)); | 215 EXPECT_TRUE(throttler()->ConsumeInputEvent(event)); |
213 EXPECT_FALSE(throttler()->is_throttled()); | 216 EXPECT_FALSE(throttler()->IsThrottled()); |
214 } | 217 } |
215 | 218 |
216 } // namespace content | 219 } // namespace content |
OLD | NEW |