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