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