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