This source file includes following definitions.
- PopFirstRequest
- CreateStub
- AppendRequest
- SetUp
- CallFunction
- UnloadExtension
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/extensions/api/power/power_api.h"
#include <deque>
#include <string>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/power/power_api_manager.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/power_save_blocker.h"
#include "extensions/common/extension.h"
namespace utils = extension_function_test_utils;
namespace extensions {
namespace {
const char kDisplayArgs[] = "[\"display\"]";
const char kSystemArgs[] = "[\"system\"]";
const char kEmptyArgs[] = "[]";
enum Request {
BLOCK_APP_SUSPENSION,
UNBLOCK_APP_SUSPENSION,
BLOCK_DISPLAY_SLEEP,
UNBLOCK_DISPLAY_SLEEP,
NONE,
};
class PowerSaveBlockerStub : public content::PowerSaveBlocker {
public:
explicit PowerSaveBlockerStub(base::Closure unblock_callback)
: unblock_callback_(unblock_callback) {
}
virtual ~PowerSaveBlockerStub() {
unblock_callback_.Run();
}
private:
base::Closure unblock_callback_;
DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerStub);
};
class PowerSaveBlockerStubManager {
public:
PowerSaveBlockerStubManager() : weak_ptr_factory_(this) {
PowerApiManager::GetInstance()->SetCreateBlockerFunctionForTesting(
base::Bind(&PowerSaveBlockerStubManager::CreateStub,
base::Unretained(this)));
}
~PowerSaveBlockerStubManager() {
PowerApiManager::GetInstance()->SetCreateBlockerFunctionForTesting(
PowerApiManager::CreateBlockerFunction());
}
Request PopFirstRequest() {
if (requests_.empty())
return NONE;
Request request = requests_.front();
requests_.pop_front();
return request;
}
private:
scoped_ptr<content::PowerSaveBlocker> CreateStub(
content::PowerSaveBlocker::PowerSaveBlockerType type,
const std::string& reason) {
Request unblock_request = NONE;
switch (type) {
case content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension:
requests_.push_back(BLOCK_APP_SUSPENSION);
unblock_request = UNBLOCK_APP_SUSPENSION;
break;
case content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep:
requests_.push_back(BLOCK_DISPLAY_SLEEP);
unblock_request = UNBLOCK_DISPLAY_SLEEP;
break;
}
return scoped_ptr<content::PowerSaveBlocker>(
new PowerSaveBlockerStub(
base::Bind(&PowerSaveBlockerStubManager::AppendRequest,
weak_ptr_factory_.GetWeakPtr(),
unblock_request)));
}
void AppendRequest(Request request) {
requests_.push_back(request);
}
std::deque<Request> requests_;
base::WeakPtrFactory<PowerSaveBlockerStubManager> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerStubManager);
};
}
class PowerApiTest : public BrowserWithTestWindowTest {
public:
virtual void SetUp() OVERRIDE {
BrowserWithTestWindowTest::SetUp();
manager_.reset(new PowerSaveBlockerStubManager);
extension_ = utils::CreateEmptyExtensionWithLocation(
extensions::Manifest::UNPACKED);
}
protected:
enum FunctionType {
REQUEST,
RELEASE,
};
bool CallFunction(FunctionType type,
const std::string& args,
extensions::Extension* extension) {
scoped_refptr<UIThreadExtensionFunction> function(
type == REQUEST ?
static_cast<UIThreadExtensionFunction*>(
new PowerRequestKeepAwakeFunction) :
static_cast<UIThreadExtensionFunction*>(
new PowerReleaseKeepAwakeFunction));
function->set_extension(extension);
return utils::RunFunction(function.get(), args, browser(), utils::NONE);
}
void UnloadExtension(extensions::Extension* extension) {
UnloadedExtensionInfo details(
extension, UnloadedExtensionInfo::REASON_UNINSTALL);
PowerApiManager::GetInstance()->Observe(
chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<Profile>(browser()->profile()),
content::Details<UnloadedExtensionInfo>(&details));
}
scoped_ptr<PowerSaveBlockerStubManager> manager_;
scoped_refptr<extensions::Extension> extension_;
};
TEST_F(PowerApiTest, RequestAndRelease) {
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get()));
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get()));
EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get()));
EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
}
TEST_F(PowerApiTest, RequestWithoutRelease) {
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
UnloadExtension(extension_.get());
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
}
TEST_F(PowerApiTest, ReleaseWithoutRequest) {
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get()));
EXPECT_EQ(NONE, manager_->PopFirstRequest());
}
TEST_F(PowerApiTest, UpgradeRequest) {
ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get()));
EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get()));
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
}
TEST_F(PowerApiTest, DowngradeRequest) {
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(REQUEST, kSystemArgs, extension_.get()));
EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(RELEASE, kEmptyArgs, extension_.get()));
EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
}
TEST_F(PowerApiTest, MultipleExtensions) {
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
scoped_ptr<base::DictionaryValue> extension_value(
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()));
EXPECT_EQ(NONE, manager_->PopFirstRequest());
UnloadExtension(extension_.get());
EXPECT_EQ(BLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(UNBLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
ASSERT_TRUE(CallFunction(REQUEST, kDisplayArgs, extension_.get()));
EXPECT_EQ(BLOCK_DISPLAY_SLEEP, manager_->PopFirstRequest());
EXPECT_EQ(UNBLOCK_APP_SUSPENSION, manager_->PopFirstRequest());
EXPECT_EQ(NONE, manager_->PopFirstRequest());
}
}