Skip to content

Commit

Permalink
Adding some tests for easy_unlock_service
Browse files Browse the repository at this point in the history
Extracted parts that deal with extension system from EasyUnlockService
to a separate class (mainly to make setting up unittests easier).

Added tests for EasyUnlockAuthAttempt

Converted existing browser tests to unit tests and removed official
build ifdefs.

BUG=414829

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

Cr-Commit-Position: refs/heads/master@{#315826}
  • Loading branch information
tbarzic authored and Commit bot committed Feb 11, 2015
1 parent 6a030f5 commit 5242236
Show file tree
Hide file tree
Showing 21 changed files with 1,956 additions and 479 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "chrome/browser/extensions/extension_system_factory.h"
#include "chrome/browser/extensions/test_extension_prefs.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/signin/easy_unlock_app_manager.h"
#include "chrome/browser/signin/easy_unlock_service_factory.h"
#include "chrome/browser/signin/easy_unlock_service_regular.h"
#include "chrome/common/extensions/api/easy_unlock_private.h"
Expand Down Expand Up @@ -417,7 +418,12 @@ struct AutoPairingResult {

// Test factory to register EasyUnlockService.
KeyedService* BuildTestEasyUnlockService(content::BrowserContext* context) {
return new EasyUnlockServiceRegular(static_cast<Profile*>(context));
EasyUnlockService* service =
new EasyUnlockServiceRegular(static_cast<Profile*>(context));
service->Initialize(
EasyUnlockAppManager::Create(extensions::ExtensionSystem::Get(context),
-1 /* manifest id*/, base::FilePath()));
return service;
}

// A fake EventRouter that logs event it dispatches for testing.
Expand Down Expand Up @@ -469,15 +475,16 @@ KeyedService* BuildFakeExtensionSystem(content::BrowserContext* profile) {
}

TEST_F(EasyUnlockPrivateApiTest, AutoPairing) {
EasyUnlockServiceFactory::GetInstance()->SetTestingFactoryAndUse(
profile(), &BuildTestEasyUnlockService);
FakeExtensionSystem* fake_extension_system =
static_cast<FakeExtensionSystem*>(
extensions::ExtensionSystemFactory::GetInstance()
->SetTestingFactoryAndUse(profile(), &BuildFakeExtensionSystem));
FakeEventRouter* event_router =
static_cast<FakeEventRouter*>(fake_extension_system->event_router());

EasyUnlockServiceFactory::GetInstance()->SetTestingFactoryAndUse(
profile(), &BuildTestEasyUnlockService);

AutoPairingResult result;

// Dispatch OnStartAutoPairing event on EasyUnlockService::StartAutoPairing.
Expand Down
7 changes: 6 additions & 1 deletion chrome/browser/extensions/test_extension_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/info_map.h"
#include "extensions/browser/lazy_background_task_queue.h"
#include "extensions/browser/management_policy.h"
#include "extensions/browser/quota_service.h"
#include "extensions/browser/runtime_data.h"
Expand All @@ -50,6 +51,10 @@ void TestExtensionSystem::Shutdown() {
extension_service_->Shutdown();
}

void TestExtensionSystem::CreateLazyBackgroundTaskQueue() {
lazy_background_task_queue_.reset(new LazyBackgroundTaskQueue(profile_));
}

ExtensionPrefs* TestExtensionSystem::CreateExtensionPrefs(
const base::CommandLine* command_line,
const base::FilePath& install_directory) {
Expand Down Expand Up @@ -143,7 +148,7 @@ InfoMap* TestExtensionSystem::info_map() { return info_map_.get(); }

LazyBackgroundTaskQueue*
TestExtensionSystem::lazy_background_task_queue() {
return NULL;
return lazy_background_task_queue_.get();
}

void TestExtensionSystem::SetEventRouter(scoped_ptr<EventRouter> event_router) {
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/extensions/test_extension_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class TestExtensionSystem : public ExtensionSystem {

void CreateSocketManager();

// Creates a LazyBackgroundTaskQueue. If not invoked, the
// LazyBackgroundTaskQueue is NULL.
void CreateLazyBackgroundTaskQueue();

void InitForRegularProfile(bool extensions_enabled) override {}
void SetExtensionService(ExtensionService* service);
ExtensionService* extension_service() override;
Expand Down Expand Up @@ -94,6 +98,7 @@ class TestExtensionSystem : public ExtensionSystem {
scoped_ptr<RuntimeData> runtime_data_;
scoped_ptr<ExtensionService> extension_service_;
scoped_refptr<InfoMap> info_map_;
scoped_ptr<LazyBackgroundTaskQueue> lazy_background_task_queue_;
scoped_ptr<EventRouter> event_router_;
scoped_ptr<ErrorConsole> error_console_;
scoped_ptr<InstallVerifier> install_verifier_;
Expand Down
201 changes: 201 additions & 0 deletions chrome/browser/signin/easy_unlock_app_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// Copyright 2015 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 "chrome/browser/signin/easy_unlock_app_manager.h"

#include "base/command_line.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/common/extensions/api/easy_unlock_private.h"
#include "chrome/common/extensions/api/screenlock_private.h"
#include "chrome/common/extensions/extension_constants.h"
#include "components/proximity_auth/switches.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/one_shot_event.h"

#if defined(OS_CHROMEOS)
#include "base/sys_info.h"
#endif

namespace {

class EasyUnlockAppManagerImpl : public EasyUnlockAppManager {
public:
EasyUnlockAppManagerImpl(extensions::ExtensionSystem* extension_system,
int manifest_id,
const base::FilePath& app_path);
~EasyUnlockAppManagerImpl() override;

// EasyUnlockAppManager overrides.
void EnsureReady(const base::Closure& ready_callback) override;
void LaunchSetup() override;
void LoadApp() override;
void DisableAppIfLoaded() override;
void ReloadApp() override;
bool SendUserUpdatedEvent(const std::string& user_id,
bool is_logged_in,
bool data_ready) override;
bool SendAuthAttemptEvent() override;

private:
extensions::ExtensionSystem* extension_system_;

// The Easy Unlock app id.
std::string app_id_;
int manifest_id_;

// The path from which Easy Unlock app should be loaded.
base::FilePath app_path_;

DISALLOW_COPY_AND_ASSIGN(EasyUnlockAppManagerImpl);
};

EasyUnlockAppManagerImpl::EasyUnlockAppManagerImpl(
extensions::ExtensionSystem* extension_system,
int manifest_id,
const base::FilePath& app_path)
: extension_system_(extension_system),
app_id_(extension_misc::kEasyUnlockAppId),
manifest_id_(manifest_id),
app_path_(app_path) {
}

EasyUnlockAppManagerImpl::~EasyUnlockAppManagerImpl() {
}

void EasyUnlockAppManagerImpl::EnsureReady(
const base::Closure& ready_callback) {
extension_system_->ready().Post(FROM_HERE, ready_callback);
}

void EasyUnlockAppManagerImpl::LaunchSetup() {
ExtensionService* extension_service = extension_system_->extension_service();
if (!extension_service)
return;

const extensions::Extension* extension =
extension_service->GetExtensionById(app_id_, false);
if (!extension)
return;

OpenApplication(AppLaunchParams(extension_service->profile(), extension,
extensions::LAUNCH_CONTAINER_WINDOW,
NEW_WINDOW,
extensions::SOURCE_CHROME_INTERNAL));
}

void EasyUnlockAppManagerImpl::LoadApp() {
ExtensionService* extension_service = extension_system_->extension_service();
if (!extension_service)
return;

#if defined(OS_CHROMEOS)
// TODO(xiyuan): Remove this when the app is bundled with chrome.
if (!base::SysInfo::IsRunningOnChromeOS() &&
!base::CommandLine::ForCurrentProcess()->HasSwitch(
proximity_auth::switches::kForceLoadEasyUnlockAppInTests)) {
return;
}
#endif

if (app_path_.empty())
return;

extensions::ComponentLoader* loader = extension_service->component_loader();
if (!loader->Exists(app_id_))
app_id_ = loader->Add(manifest_id_, app_path_);

extension_service->EnableExtension(app_id_);
}

void EasyUnlockAppManagerImpl::DisableAppIfLoaded() {
ExtensionService* extension_service = extension_system_->extension_service();
if (!extension_service)
return;

if (!extension_service->component_loader()->Exists(app_id_))
return;

extension_service->DisableExtension(app_id_,
extensions::Extension::DISABLE_RELOAD);
}

void EasyUnlockAppManagerImpl::ReloadApp() {
ExtensionService* extension_service = extension_system_->extension_service();
if (!extension_service)
return;

if (!extension_service->component_loader()->Exists(app_id_))
return;

extension_service->ReloadExtension(app_id_);
}

bool EasyUnlockAppManagerImpl::SendUserUpdatedEvent(const std::string& user_id,
bool is_logged_in,
bool data_ready) {
extensions::EventRouter* event_router = extension_system_->event_router();
if (!event_router)
return false;

std::string event_name =
extensions::api::easy_unlock_private::OnUserInfoUpdated::kEventName;

if (!event_router->ExtensionHasEventListener(app_id_, event_name))
return false;

extensions::api::easy_unlock_private::UserInfo info;
info.user_id = user_id;
info.logged_in = is_logged_in;
info.data_ready = data_ready;

scoped_ptr<base::ListValue> args(new base::ListValue());
args->Append(info.ToValue().release());

scoped_ptr<extensions::Event> event(
new extensions::Event(event_name, args.Pass()));

event_router->DispatchEventToExtension(app_id_, event.Pass());
return true;
}

bool EasyUnlockAppManagerImpl::SendAuthAttemptEvent() {
extensions::EventRouter* event_router = extension_system_->event_router();
if (!event_router)
return false;

std::string event_name =
extensions::api::screenlock_private::OnAuthAttempted::kEventName;

if (!event_router->HasEventListener(event_name))
return false;

scoped_ptr<base::ListValue> args(new base::ListValue());
args->AppendString(extensions::api::screenlock_private::ToString(
extensions::api::screenlock_private::AUTH_TYPE_USERCLICK));
args->AppendString(std::string());

scoped_ptr<extensions::Event> event(
new extensions::Event(event_name, args.Pass()));

// TODO(tbarzic): Restrict this to EasyUnlock app.
event_router->BroadcastEvent(event.Pass());
return true;
}

} // namespace

EasyUnlockAppManager::~EasyUnlockAppManager() {
}

// static
scoped_ptr<EasyUnlockAppManager> EasyUnlockAppManager::Create(
extensions::ExtensionSystem* extension_system,
int manifest_id,
const base::FilePath& app_path) {
return scoped_ptr<EasyUnlockAppManager>(
new EasyUnlockAppManagerImpl(extension_system, manifest_id, app_path));
}
57 changes: 57 additions & 0 deletions chrome/browser/signin/easy_unlock_app_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2015 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.

#ifndef CHROME_BROWSER_SIGNIN_EASY_UNLOCK_APP_MANAGER_H_
#define CHROME_BROWSER_SIGNIN_EASY_UNLOCK_APP_MANAGER_H_

#include <string>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"

namespace extensions {
class ExtensionSystem;
}

// Used to manage Easy Unlock app's lifetime and to dispatch events to the app.
// It's main purpose is to abstract extension system from the rest of easy
// unlock code.
class EasyUnlockAppManager {
public:
virtual ~EasyUnlockAppManager();

// Creates EasyUnlockAppManager object that should be used in production.
static scoped_ptr<EasyUnlockAppManager> Create(
extensions::ExtensionSystem* extension_system,
int manifest_id,
const base::FilePath& app_path);

// Wait for the extension system to get ready and invokes |ready_callback|
// when that happens.
// Note that the callback may be triggered after |this| is deleted.
virtual void EnsureReady(const base::Closure& ready_callback) = 0;

// Launches Easy Unlock setup app, if the setup app is loaded.
virtual void LaunchSetup() = 0;

// Loads Easy Unlock app.
virtual void LoadApp() = 0;

// Disables Easy Unlock app.
virtual void DisableAppIfLoaded() = 0;

// Reloads Easy Unlock app.
virtual void ReloadApp() = 0;

// Sends easyUnlockPrivate.onUserInfoUpdate event to Easy Unlock app.
virtual bool SendUserUpdatedEvent(const std::string& user_id,
bool is_logged_in,
bool data_ready) = 0;

// Sends screenlockPrivate.onAuthAttempted event to Easy Unlock app.
virtual bool SendAuthAttemptEvent() = 0;
};

#endif // CHROME_BROWSER_SIGNIN_EASY_UNLOCK_APP_MANAGER_H_
Loading

0 comments on commit 5242236

Please sign in to comment.