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_constants.h" |
| 11 #include "content/public/common/content_switches.h" |
| 12 #include "content/renderer/pepper/pepper_plugin_instance_throttler.h" |
| 13 #include "content/renderer/pepper/plugin_power_saver_helper.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 17 |
| 18 using testing::_; |
| 19 using testing::Return; |
| 20 |
| 21 namespace content { |
| 22 |
| 23 namespace { |
| 24 |
| 25 class MockPluginPowerSaverHelper : public PluginPowerSaverHelper { |
| 26 public: |
| 27 MockPluginPowerSaverHelper() : PluginPowerSaverHelper(NULL) { |
| 28 EXPECT_CALL(*this, ShouldThrottleContent(_, _, _, _)) |
| 29 .WillRepeatedly(Return(true)); |
| 30 } |
| 31 |
| 32 MOCK_CONST_METHOD4(ShouldThrottleContent, bool(const GURL&, int, int, bool*)); |
| 33 |
| 34 void RegisterPeripheralPlugin( |
| 35 const GURL& content_origin, |
| 36 const base::Closure& unthrottle_callback) override { |
| 37 unthrottle_callback_ = unthrottle_callback; |
| 38 } |
| 39 |
| 40 void WhitelistContentOrigin(const GURL& content_origin) override { |
| 41 DCHECK(!unthrottle_callback_.is_null()); |
| 42 unthrottle_callback_.Run(); |
| 43 } |
| 44 |
| 45 base::Closure& unthrottle_callback() { return unthrottle_callback_; } |
| 46 |
| 47 private: |
| 48 base::Closure unthrottle_callback_; |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 class PepperPluginInstanceThrottlerTest : public testing::Test { |
| 54 protected: |
| 55 PepperPluginInstanceThrottlerTest() : change_callback_calls_(0) {} |
| 56 |
| 57 void SetUp() override { |
| 58 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 59 switches::kEnablePluginPowerSaver); |
| 60 |
| 61 blink::WebRect rect; |
| 62 rect.width = 100; |
| 63 rect.height = 100; |
| 64 throttler_.reset(new PepperPluginInstanceThrottler( |
| 65 &power_saver_helper_, rect, kFlashPluginName, |
| 66 GURL("http://example.com"), |
| 67 base::Bind(&PepperPluginInstanceThrottlerTest::ChangeCallback, |
| 68 base::Unretained(this)))); |
| 69 } |
| 70 |
| 71 PepperPluginInstanceThrottler* throttler() { |
| 72 DCHECK(throttler_.get()); |
| 73 return throttler_.get(); |
| 74 } |
| 75 |
| 76 MockPluginPowerSaverHelper& power_saver_helper() { |
| 77 return power_saver_helper_; |
| 78 } |
| 79 |
| 80 int change_callback_calls() { return change_callback_calls_; } |
| 81 |
| 82 void EngageThrottle() { |
| 83 throttler_->SetPluginThrottled(true); |
| 84 } |
| 85 |
| 86 void SendEventAndTest(blink::WebInputEvent::Type event_type, |
| 87 bool expect_consumed, |
| 88 bool expect_throttled, |
| 89 int expect_change_callback_count) { |
| 90 blink::WebMouseEvent event; |
| 91 event.type = event_type; |
| 92 EXPECT_EQ(expect_consumed, throttler()->ConsumeInputEvent(event)); |
| 93 EXPECT_EQ(expect_throttled, throttler()->is_throttled()); |
| 94 EXPECT_EQ(expect_change_callback_count, change_callback_calls()); |
| 95 } |
| 96 |
| 97 private: |
| 98 void ChangeCallback() { ++change_callback_calls_; } |
| 99 |
| 100 scoped_ptr<PepperPluginInstanceThrottler> throttler_; |
| 101 MockPluginPowerSaverHelper power_saver_helper_; |
| 102 |
| 103 int change_callback_calls_; |
| 104 |
| 105 base::MessageLoop loop_; |
| 106 }; |
| 107 |
| 108 TEST_F(PepperPluginInstanceThrottlerTest, ThrottleAndUnthrottleByClick) { |
| 109 EXPECT_FALSE(throttler()->is_throttled()); |
| 110 EXPECT_EQ(0, change_callback_calls()); |
| 111 |
| 112 EngageThrottle(); |
| 113 EXPECT_TRUE(throttler()->is_throttled()); |
| 114 EXPECT_EQ(1, change_callback_calls()); |
| 115 |
| 116 // MouseUp while throttled should be consumed and disengage throttling. |
| 117 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, true, false, 2); |
| 118 } |
| 119 |
| 120 TEST_F(PepperPluginInstanceThrottlerTest, IgnoreThrottlingAfterMouseUp) { |
| 121 EXPECT_FALSE(throttler()->is_throttled()); |
| 122 EXPECT_EQ(0, change_callback_calls()); |
| 123 |
| 124 // MouseUp before throttling engaged should not be consumed, but should |
| 125 // prevent subsequent throttling from engaging. |
| 126 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, false, false, 0); |
| 127 |
| 128 EngageThrottle(); |
| 129 EXPECT_FALSE(throttler()->is_throttled()); |
| 130 EXPECT_EQ(0, change_callback_calls()); |
| 131 } |
| 132 |
| 133 TEST_F(PepperPluginInstanceThrottlerTest, FastWhitelisting) { |
| 134 EXPECT_FALSE(throttler()->is_throttled()); |
| 135 EXPECT_EQ(0, change_callback_calls()); |
| 136 |
| 137 power_saver_helper().WhitelistContentOrigin(GURL("http://example.com")); |
| 138 |
| 139 EngageThrottle(); |
| 140 EXPECT_FALSE(throttler()->is_throttled()); |
| 141 EXPECT_EQ(1, change_callback_calls()); |
| 142 } |
| 143 |
| 144 TEST_F(PepperPluginInstanceThrottlerTest, SlowWhitelisting) { |
| 145 EXPECT_FALSE(throttler()->is_throttled()); |
| 146 EXPECT_EQ(0, change_callback_calls()); |
| 147 |
| 148 EngageThrottle(); |
| 149 EXPECT_TRUE(throttler()->is_throttled()); |
| 150 EXPECT_EQ(1, change_callback_calls()); |
| 151 |
| 152 power_saver_helper().WhitelistContentOrigin(GURL("http://example.com")); |
| 153 EXPECT_FALSE(throttler()->is_throttled()); |
| 154 EXPECT_EQ(2, change_callback_calls()); |
| 155 } |
| 156 |
| 157 TEST_F(PepperPluginInstanceThrottlerTest, EventConsumption) { |
| 158 EXPECT_FALSE(throttler()->is_throttled()); |
| 159 EXPECT_EQ(0, change_callback_calls()); |
| 160 |
| 161 EngageThrottle(); |
| 162 EXPECT_TRUE(throttler()->is_throttled()); |
| 163 EXPECT_EQ(1, change_callback_calls()); |
| 164 |
| 165 // Consume but don't unthrottle on a variety of other events. |
| 166 SendEventAndTest(blink::WebInputEvent::Type::MouseDown, true, true, 1); |
| 167 SendEventAndTest(blink::WebInputEvent::Type::MouseWheel, true, true, 1); |
| 168 SendEventAndTest(blink::WebInputEvent::Type::MouseMove, true, true, 1); |
| 169 SendEventAndTest(blink::WebInputEvent::Type::KeyDown, true, true, 1); |
| 170 SendEventAndTest(blink::WebInputEvent::Type::KeyUp, true, true, 1); |
| 171 |
| 172 // Consume and unthrottle on MouseUp |
| 173 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, true, false, 2); |
| 174 |
| 175 // Don't consume events after unthrottle. |
| 176 SendEventAndTest(blink::WebInputEvent::Type::MouseDown, false, false, 2); |
| 177 SendEventAndTest(blink::WebInputEvent::Type::MouseWheel, false, false, 2); |
| 178 SendEventAndTest(blink::WebInputEvent::Type::MouseMove, false, false, 2); |
| 179 SendEventAndTest(blink::WebInputEvent::Type::KeyDown, false, false, 2); |
| 180 SendEventAndTest(blink::WebInputEvent::Type::KeyUp, false, false, 2); |
| 181 |
| 182 // Subsequent MouseUps should also not be consumed. |
| 183 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, false, false, 2); |
| 184 } |
| 185 |
| 186 } // namespace content |
OLD | NEW |