Commit 30a1ccd6 authored by jamescook's avatar jamescook Committed by Commit bot

Move extensions power API unit test to extensions_unittests

It was running in Chrome's unit_tests suite for historical reasons.

BUG=397164
TEST=extensions_unittests

Review URL: https://codereview.chromium.org/554963002

Cr-Commit-Position: refs/heads/master@{#293946}
parent 46eac8f5
...@@ -533,7 +533,6 @@ ...@@ -533,7 +533,6 @@
# http://crbug.com/348066. They are duplicated here because we haven't # http://crbug.com/348066. They are duplicated here because we haven't
# extensions_unittests running in the bots yet. Until that happens, # extensions_unittests running in the bots yet. Until that happens,
# they should be kept here. # they should be kept here.
'../extensions/browser/api/power/power_api_unittest.cc',
'../extensions/common/extension_l10n_util_unittest.cc', '../extensions/common/extension_l10n_util_unittest.cc',
'../extensions/common/features/base_feature_provider_unittest.cc', '../extensions/common/features/base_feature_provider_unittest.cc',
'../extensions/common/features/complex_feature_unittest.cc', '../extensions/common/features/complex_feature_unittest.cc',
......
...@@ -11,13 +11,12 @@ ...@@ -11,13 +11,12 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "content/public/browser/power_save_blocker.h" #include "content/public/browser/power_save_blocker.h"
#include "extensions/browser/api/power/power_api_manager.h" #include "extensions/browser/api/power/power_api_manager.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/api_unittest.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
#include "extensions/common/test_util.h"
namespace utils = extension_function_test_utils;
namespace extensions { namespace extensions {
...@@ -129,19 +128,16 @@ class PowerSaveBlockerStubManager { ...@@ -129,19 +128,16 @@ class PowerSaveBlockerStubManager {
} // namespace } // namespace
class PowerApiTest : public BrowserWithTestWindowTest { class PowerApiTest : public ApiUnitTest {
public: public:
virtual void SetUp() OVERRIDE { virtual void SetUp() OVERRIDE {
BrowserWithTestWindowTest::SetUp(); ApiUnitTest::SetUp();
manager_.reset(new PowerSaveBlockerStubManager(profile())); manager_.reset(new PowerSaveBlockerStubManager(browser_context()));
extension_ = utils::CreateEmptyExtensionWithLocation(
extensions::Manifest::UNPACKED);
} }
virtual void TearDown() OVERRIDE { virtual void TearDown() OVERRIDE {
extension_ = NULL;
manager_.reset(); manager_.reset();
BrowserWithTestWindowTest::TearDown(); ApiUnitTest::TearDown();
} }
protected: protected:
...@@ -156,7 +152,7 @@ class PowerApiTest : public BrowserWithTestWindowTest { ...@@ -156,7 +152,7 @@ class PowerApiTest : public BrowserWithTestWindowTest {
// arguments, on behalf of |extension|. // arguments, on behalf of |extension|.
bool CallFunction(FunctionType type, bool CallFunction(FunctionType type,
const std::string& args, const std::string& args,
extensions::Extension* extension) { const extensions::Extension* extension) {
scoped_refptr<UIThreadExtensionFunction> function( scoped_refptr<UIThreadExtensionFunction> function(
type == REQUEST ? type == REQUEST ?
static_cast<UIThreadExtensionFunction*>( static_cast<UIThreadExtensionFunction*>(
...@@ -164,34 +160,33 @@ class PowerApiTest : public BrowserWithTestWindowTest { ...@@ -164,34 +160,33 @@ class PowerApiTest : public BrowserWithTestWindowTest {
static_cast<UIThreadExtensionFunction*>( static_cast<UIThreadExtensionFunction*>(
new PowerReleaseKeepAwakeFunction)); new PowerReleaseKeepAwakeFunction));
function->set_extension(extension); function->set_extension(extension);
return utils::RunFunction(function.get(), args, browser(), utils::NONE); return api_test_utils::RunFunction(function.get(), args, browser_context());
} }
// Send a notification to PowerApiManager saying that |extension| has // Send a notification to PowerApiManager saying that |extension| has
// been unloaded. // been unloaded.
void UnloadExtension(extensions::Extension* extension) { void UnloadExtension(const extensions::Extension* extension) {
PowerApiManager::Get(profile())->OnExtensionUnloaded( PowerApiManager::Get(browser_context())->OnExtensionUnloaded(
profile(), extension, UnloadedExtensionInfo::REASON_UNINSTALL); browser_context(), extension, UnloadedExtensionInfo::REASON_UNINSTALL);
} }
scoped_ptr<PowerSaveBlockerStubManager> manager_; scoped_ptr<PowerSaveBlockerStubManager> manager_;
scoped_refptr<extensions::Extension> extension_;
}; };
TEST_F(PowerApiTest, RequestAndRelease) { TEST_F(PowerApiTest, RequestAndRelease) {
// Simulate an extension making and releasing a "display" request and a // Simulate an extension making and releasing a "display" request and a
// "system" request. // "system" request.
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
...@@ -200,11 +195,11 @@ TEST_F(PowerApiTest, RequestWithoutRelease) { ...@@ -200,11 +195,11 @@ TEST_F(PowerApiTest, RequestWithoutRelease) {
// Simulate an extension calling requestKeepAwake() without calling // Simulate an extension calling requestKeepAwake() without calling
// releaseKeepAwake(). The override should be automatically removed when // releaseKeepAwake(). The override should be automatically removed when
// the extension is unloaded. // the extension is unloaded.
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
UnloadExtension(extension_.get()); UnloadExtension(extension());
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
...@@ -212,7 +207,7 @@ TEST_F(PowerApiTest, RequestWithoutRelease) { ...@@ -212,7 +207,7 @@ TEST_F(PowerApiTest, RequestWithoutRelease) {
TEST_F(PowerApiTest, ReleaseWithoutRequest) { TEST_F(PowerApiTest, ReleaseWithoutRequest) {
// Simulate an extension calling releaseKeepAwake() without having // Simulate an extension calling releaseKeepAwake() without having
// calling requestKeepAwake() earlier. The call should be ignored. // calling requestKeepAwake() earlier. The call should be ignored.
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
...@@ -221,16 +216,16 @@ TEST_F(PowerApiTest, UpgradeRequest) { ...@@ -221,16 +216,16 @@ TEST_F(PowerApiTest, UpgradeRequest) {
// requestKeepAwake("display"). When the second call is made, a // requestKeepAwake("display"). When the second call is made, a
// display-sleep-blocking request should be made before the initial // display-sleep-blocking request should be made before the initial
// app-suspension-blocking request is released. // app-suspension-blocking request is released.
ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
...@@ -240,46 +235,42 @@ TEST_F(PowerApiTest, DowngradeRequest) { ...@@ -240,46 +235,42 @@ TEST_F(PowerApiTest, DowngradeRequest) {
// requestKeepAwake("system"). When the second call is made, an // requestKeepAwake("system"). When the second call is made, an
// app-suspension-blocking request should be made before the initial // app-suspension-blocking request should be made before the initial
// display-sleep-blocking request is released. // display-sleep-blocking request is released.
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension()));
EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get())); ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension()));
EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
} }
TEST_F(PowerApiTest, MultipleExtensions) { TEST_F(PowerApiTest, MultipleExtensions) {
// Simulate an extension blocking the display from sleeping. // Simulate an extension blocking the display from sleeping.
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
// Create a second extension that blocks system suspend. No additional // Create a second extension that blocks system suspend. No additional
// PowerSaveBlocker is needed; the blocker from the first extension // PowerSaveBlocker is needed; the blocker from the first extension
// already covers the behavior requested by the second extension. // already covers the behavior requested by the second extension.
scoped_ptr<base::DictionaryValue> extension_value( scoped_refptr<Extension> extension2(test_util::CreateExtensionWithID("id2"));
utils::ParseDictionary("{\"name\": \"Test\", \"version\": \"1.0\"}"));
scoped_refptr<extensions::Extension> extension2(
utils::CreateExtension(extensions::Manifest::UNPACKED,
extension_value.get(), "second_extension"));
ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension2.get())); ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension2.get()));
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
// When the first extension is unloaded, a new app-suspension blocker // When the first extension is unloaded, a new app-suspension blocker
// should be created before the display-sleep blocker is destroyed. // should be created before the display-sleep blocker is destroyed.
UnloadExtension(extension_.get()); UnloadExtension(extension());
EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
// Make the first extension block display-sleep again. // Make the first extension block display-sleep again.
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get())); ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest()); EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest()); EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest()); EXPECT_EQ(NONE, manager_->PopFirstRequest());
......
...@@ -153,6 +153,15 @@ std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, ...@@ -153,6 +153,15 @@ std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
return function->GetError(); return function->GetError();
} }
bool RunFunction(UIThreadExtensionFunction* function,
const std::string& args,
content::BrowserContext* context) {
TestFunctionDispatcherDelegate delegate;
scoped_ptr<ExtensionFunctionDispatcher> dispatcher(
new ExtensionFunctionDispatcher(context, &delegate));
return RunFunction(function, args, context, dispatcher.Pass(), NONE);
}
bool RunFunction(UIThreadExtensionFunction* function, bool RunFunction(UIThreadExtensionFunction* function,
const std::string& args, const std::string& args,
content::BrowserContext* context, content::BrowserContext* context,
......
...@@ -80,6 +80,9 @@ std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, ...@@ -80,6 +80,9 @@ std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
// TODO(aa): I'm concerned that this style won't scale to all the bits and bobs // TODO(aa): I'm concerned that this style won't scale to all the bits and bobs
// we're going to need to frob for all the different extension functions. But // we're going to need to frob for all the different extension functions. But
// we can refactor when we see what is needed. // we can refactor when we see what is needed.
bool RunFunction(UIThreadExtensionFunction* function,
const std::string& args,
content::BrowserContext* context);
bool RunFunction(UIThreadExtensionFunction* function, bool RunFunction(UIThreadExtensionFunction* function,
const std::string& args, const std::string& args,
content::BrowserContext* context, content::BrowserContext* context,
......
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace content { namespace content {
class BrowserContext;
class ContentClient; class ContentClient;
class ContentBrowserClient; class ContentBrowserClient;
class TestBrowserContext;
} }
namespace extensions { namespace extensions {
...@@ -32,9 +32,12 @@ class ExtensionsTest : public testing::Test { ...@@ -32,9 +32,12 @@ class ExtensionsTest : public testing::Test {
ExtensionsTest(); ExtensionsTest();
virtual ~ExtensionsTest(); virtual ~ExtensionsTest();
content::TestBrowserContext* browser_context() { // Returned as a BrowserContext since most users don't need methods from
return browser_context_.get(); // TestBrowserContext.
} content::BrowserContext* browser_context() { return browser_context_.get(); }
// Returned as a TestExtensionsBrowserClient since most users need to call
// test-specific methods on it.
TestExtensionsBrowserClient* extensions_browser_client() { TestExtensionsBrowserClient* extensions_browser_client() {
return extensions_browser_client_.get(); return extensions_browser_client_.get();
} }
...@@ -46,7 +49,7 @@ class ExtensionsTest : public testing::Test { ...@@ -46,7 +49,7 @@ class ExtensionsTest : public testing::Test {
private: private:
scoped_ptr<content::ContentClient> content_client_; scoped_ptr<content::ContentClient> content_client_;
scoped_ptr<content::ContentBrowserClient> content_browser_client_; scoped_ptr<content::ContentBrowserClient> content_browser_client_;
scoped_ptr<content::TestBrowserContext> browser_context_; scoped_ptr<content::BrowserContext> browser_context_;
scoped_ptr<TestExtensionsBrowserClient> extensions_browser_client_; scoped_ptr<TestExtensionsBrowserClient> extensions_browser_client_;
DISALLOW_COPY_AND_ASSIGN(ExtensionsTest); DISALLOW_COPY_AND_ASSIGN(ExtensionsTest);
......
...@@ -1014,6 +1014,7 @@ ...@@ -1014,6 +1014,7 @@
'browser/api/cast_channel/cast_socket_unittest.cc', 'browser/api/cast_channel/cast_socket_unittest.cc',
'browser/api/cast_channel/logger_unittest.cc', 'browser/api/cast_channel/logger_unittest.cc',
'browser/api/declarative/deduping_factory_unittest.cc', 'browser/api/declarative/deduping_factory_unittest.cc',
'browser/api/power/power_api_unittest.cc',
'browser/api/sockets_tcp/sockets_tcp_api_unittest.cc', 'browser/api/sockets_tcp/sockets_tcp_api_unittest.cc',
'browser/api/storage/settings_quota_unittest.cc', 'browser/api/storage/settings_quota_unittest.cc',
'browser/api/storage/storage_api_unittest.cc', 'browser/api/storage/storage_api_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment