Index: content/renderer/pepper/pepper_plugin_instance_throttler_unittest.cc |
diff --git a/content/renderer/pepper/pepper_plugin_instance_throttler_unittest.cc b/content/renderer/pepper/pepper_plugin_instance_throttler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e6739b65857667e195a7536b625fe911d3847da8 |
--- /dev/null |
+++ b/content/renderer/pepper/pepper_plugin_instance_throttler_unittest.cc |
@@ -0,0 +1,137 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/command_line.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "content/public/common/content_constants.h" |
+#include "content/public/common/content_switches.h" |
+#include "content/renderer/pepper/pepper_plugin_instance_throttler.h" |
+#include "content/renderer/pepper/plugin_power_saver_helper.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/WebKit/public/web/WebInputEvent.h" |
+ |
+using testing::_; |
+using testing::Return; |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+class MockPluginPowerSaverHelper : public PluginPowerSaverHelper { |
+ public: |
+ MockPluginPowerSaverHelper() : PluginPowerSaverHelper(NULL) { |
+ EXPECT_CALL(*this, ShouldThrottleContent(_, _, _, _)) |
+ .WillRepeatedly(Return(true)); |
+ } |
+ |
+ MOCK_CONST_METHOD4(ShouldThrottleContent, bool(const GURL&, int, int, bool*)); |
+ MOCK_METHOD2(RegisterPeripheralPlugin, |
+ void(const GURL&, const base::Closure&)); |
+ MOCK_METHOD1(WhitelistContentOrigin, void(const GURL&)); |
+}; |
+ |
+} // namespace |
+ |
+class PepperPluginInstanceThrottlerTest : public testing::Test { |
+ protected: |
+ PepperPluginInstanceThrottlerTest() : change_callback_calls_(0) {} |
+ |
+ void SetUp() override { |
+ base::CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kEnablePluginPowerSaver); |
+ |
+ blink::WebRect rect; |
+ rect.width = 100; |
+ rect.height = 100; |
+ throttler_.reset(new PepperPluginInstanceThrottler( |
+ &power_saver_helper_, rect, kFlashPluginName, |
+ GURL("http://example.com"), |
+ base::Bind(&PepperPluginInstanceThrottlerTest::ChangeCallback, |
+ base::Unretained(this)))); |
+ } |
+ |
+ PepperPluginInstanceThrottler* throttler() { |
+ DCHECK(throttler_.get()); |
+ return throttler_.get(); |
+ } |
+ |
+ int change_callback_calls() { return change_callback_calls_; } |
+ |
+ void SetPluginThrottledForTest(bool throttled) { |
+ throttler_->SetPluginThrottled(throttled); |
+ } |
+ |
+ void DisablePowerSaverByRetroactiveWhitelistForTest() { |
+ throttler_->DisablePowerSaverByRetroactiveWhitelist(); |
raymes
2014/11/13 00:05:16
You can avoid needing to make this public without
tommycli
2014/11/13 02:33:36
Done.
|
+ } |
+ |
+ private: |
+ void ChangeCallback() { ++change_callback_calls_; } |
+ |
+ scoped_ptr<PepperPluginInstanceThrottler> throttler_; |
+ MockPluginPowerSaverHelper power_saver_helper_; |
+ |
+ int change_callback_calls_; |
+ |
+ base::MessageLoop loop_; |
+}; |
+ |
+TEST_F(PepperPluginInstanceThrottlerTest, ThrottleAndUnthrottle) { |
+ EXPECT_FALSE(throttler()->is_throttled()); |
+ EXPECT_EQ(0, change_callback_calls()); |
+ |
+ SetPluginThrottledForTest(true); |
raymes
2014/11/13 00:05:16
Rather than calling SetPluginThrottledForTest(true
tommycli
2014/11/13 02:33:36
The current 'real' behavior is to simply wait 5 se
|
+ EXPECT_TRUE(throttler()->is_throttled()); |
+ EXPECT_EQ(1, change_callback_calls()); |
+ |
+ SetPluginThrottledForTest(false); |
raymes
2014/11/13 00:05:16
Can we use ConsumeInputEvent here instead?
tommycli
2014/11/13 02:33:36
Done. I consolidated this with UnthrottleByClick n
|
+ EXPECT_FALSE(throttler()->is_throttled()); |
+ EXPECT_EQ(2, change_callback_calls()); |
+} |
+ |
+TEST_F(PepperPluginInstanceThrottlerTest, FastWhitelisting) { |
+ EXPECT_FALSE(throttler()->is_throttled()); |
+ EXPECT_EQ(0, change_callback_calls()); |
+ |
+ DisablePowerSaverByRetroactiveWhitelistForTest(); |
+ |
+ SetPluginThrottledForTest(true); |
+ EXPECT_FALSE(throttler()->is_throttled()); |
+ EXPECT_EQ(1, change_callback_calls()); |
+} |
+ |
+TEST_F(PepperPluginInstanceThrottlerTest, SlowWhitelisting) { |
+ EXPECT_FALSE(throttler()->is_throttled()); |
+ EXPECT_EQ(0, change_callback_calls()); |
+ |
+ SetPluginThrottledForTest(true); |
+ EXPECT_TRUE(throttler()->is_throttled()); |
+ EXPECT_EQ(1, change_callback_calls()); |
+ |
+ DisablePowerSaverByRetroactiveWhitelistForTest(); |
+ EXPECT_FALSE(throttler()->is_throttled()); |
+ EXPECT_EQ(2, change_callback_calls()); |
+} |
+ |
+TEST_F(PepperPluginInstanceThrottlerTest, UnthrottleByClick) { |
+ EXPECT_FALSE(throttler()->is_throttled()); |
+ EXPECT_EQ(0, change_callback_calls()); |
+ |
+ SetPluginThrottledForTest(true); |
+ EXPECT_TRUE(throttler()->is_throttled()); |
+ EXPECT_EQ(1, change_callback_calls()); |
+ |
+ blink::WebMouseEvent event; |
+ event.type = blink::WebInputEvent::Type::MouseUp; |
+ EXPECT_TRUE(throttler()->ConsumeInputEvent(event)); |
raymes
2014/11/13 00:05:16
A few more tests for ConumeInputEvent could be goo
tommycli
2014/11/13 02:33:36
Done. Added a few more tests. Actually this caught
|
+ |
+ EXPECT_FALSE(throttler()->is_throttled()); |
+ EXPECT_EQ(2, change_callback_calls()); |
+} |
+ |
+} // namespace content |