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