Skip to content

Commit

Permalink
Implemented fake for HostPairingController.
Browse files Browse the repository at this point in the history
BUG=375191
TEST=none

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281428 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dzhioev@chromium.org committed Jul 4, 2014
1 parent 7ec0450 commit 7730e38
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 0 deletions.
2 changes: 2 additions & 0 deletions chromeos/chromeos.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@
'network/dhcp_proxy_script_fetcher_chromeos.h',
'pairing/fake_controller_pairing_controller.cc',
'pairing/fake_controller_pairing_controller.h',
'pairing/fake_host_pairing_controller.cc',
'pairing/fake_host_pairing_controller.h',
'pairing/controller_pairing_controller.cc',
'pairing/controller_pairing_controller.h',
'pairing/host_pairing_controller.cc',
Expand Down
192 changes: 192 additions & 0 deletions chromeos/pairing/fake_host_pairing_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// 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 "chromeos/pairing/fake_host_pairing_controller.h"

#include <map>
#include <vector>

#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"

namespace {

const int kUpdateStepsNumber = 10;
const int kDefaultAsyncDurationMs = 3000;
const size_t kCodeLength = 6;

} // namespace

namespace chromeos {

FakeHostPairingController::FakeHostPairingController(const std::string& config)
: current_stage_(STAGE_NONE),
enrollment_should_fail_(false),
start_after_update_(false) {
ApplyConfig(config);
AddObserver(this);
}

FakeHostPairingController::~FakeHostPairingController() {
RemoveObserver(this);
}

void FakeHostPairingController::ApplyConfig(const std::string& config) {
typedef std::vector<std::string> Tokens;

base::StringPairs kv_pairs;
CHECK(base::SplitStringIntoKeyValuePairs(config, ':', ',', &kv_pairs))
<< "Wrong config format.";
std::map<std::string, std::string> dict(kv_pairs.begin(), kv_pairs.end());

if (dict.count("async_duration")) {
int ms = 0;
CHECK(base::StringToInt(dict["async_duration"], &ms))
<< "Wrong 'async_duration' format.";
async_duration_ = base::TimeDelta::FromMilliseconds(ms);
} else {
async_duration_ =
base::TimeDelta::FromMilliseconds(kDefaultAsyncDurationMs);
}

start_after_update_ = dict["start_after_update"] == "1";

enrollment_should_fail_ = dict["fail_enrollment"] == "1";

if (dict.count("code")) {
confirmation_code_ = dict["code"];
} else {
confirmation_code_.clear();
for (size_t i = 0; i < kCodeLength; ++i)
confirmation_code_.push_back(base::RandInt('0', '9'));
}
CHECK(confirmation_code_.length() == kCodeLength &&
confirmation_code_.find_first_not_of("0123456789") == std::string::npos)
<< "Wrong 'code' format.";

device_name_ =
dict.count("device_name") ? dict["device_name"] : "Chromebox-01";

enrollment_domain_ = dict.count("domain") ? dict["domain"] : "example.com";
}

void FakeHostPairingController::ChangeStage(Stage new_stage) {
if (current_stage_ == new_stage)
return;
current_stage_ = new_stage;
FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage));
}

void FakeHostPairingController::ChangeStageLater(Stage new_stage) {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeHostPairingController::ChangeStage,
base::Unretained(this),
new_stage),
async_duration_);
}

void FakeHostPairingController::SetUpdateProgress(int step) {
UpdateProgress progress;
progress.progress = double(step) / kUpdateStepsNumber;
FOR_EACH_OBSERVER(Observer, observers_, UpdateAdvanced(progress));
base::Closure task;
if (step >= kUpdateStepsNumber) {
task = base::Bind(&FakeHostPairingController::ChangeStage,
base::Unretained(this),
STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE);
} else {
task = base::Bind(&FakeHostPairingController::SetUpdateProgress,
base::Unretained(this),
step + 1);
}
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE, task, async_duration_ / kUpdateStepsNumber);
}

void FakeHostPairingController::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}

void FakeHostPairingController::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}

HostPairingController::Stage FakeHostPairingController::GetCurrentStage() {
return current_stage_;
}

void FakeHostPairingController::StartPairing() {
CHECK(current_stage_ == STAGE_NONE);
if (start_after_update_) {
ChangeStage(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE);
} else {
ChangeStage(STAGE_WAITING_FOR_CONTROLLER);
}
}

std::string FakeHostPairingController::GetDeviceName() {
return device_name_;
}

std::string FakeHostPairingController::GetConfirmationCode() {
CHECK(current_stage_ == STAGE_WAITING_FOR_CODE_CONFIRMATION);
return confirmation_code_;
}

std::string FakeHostPairingController::GetEnrollmentDomain() {
return enrollment_domain_;
}

void FakeHostPairingController::PairingStageChanged(Stage new_stage) {
switch (new_stage) {
case STAGE_WAITING_FOR_CONTROLLER: {
ChangeStageLater(STAGE_WAITING_FOR_CODE_CONFIRMATION);
break;
}
case STAGE_WAITING_FOR_CODE_CONFIRMATION: {
ChangeStageLater(STAGE_UPDATING);
break;
}
case STAGE_UPDATING: {
SetUpdateProgress(0);
break;
}
case STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE: {
ChangeStageLater(STAGE_WAITING_FOR_CREDENTIALS);
break;
}
case STAGE_WAITING_FOR_CREDENTIALS: {
ChangeStageLater(STAGE_ENROLLING);
break;
}
case STAGE_ENROLLING: {
if (enrollment_should_fail_) {
enrollment_should_fail_ = false;
ChangeStageLater(STAGE_ENROLLMENT_ERROR);
} else {
ChangeStageLater(STAGE_PAIRING_DONE);
}
break;
}
case STAGE_ENROLLMENT_ERROR: {
ChangeStageLater(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE);
break;
}
case STAGE_PAIRING_DONE: {
ChangeStageLater(STAGE_FINISHED);
break;
}
default: { break; }
}
}

void FakeHostPairingController::UpdateAdvanced(const UpdateProgress& progress) {
}

} // namespace chromeos
74 changes: 74 additions & 0 deletions chromeos/pairing/fake_host_pairing_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.

#ifndef CHROMEOS_PAIRING_FAKE_HOST_PAIRING_CONTROLLER_H_
#define CHROMEOS_PAIRING_FAKE_HOST_PAIRING_CONTROLLER_H_

#include "base/macros.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/pairing/host_pairing_controller.h"

namespace chromeos {

class CHROMEOS_EXPORT FakeHostPairingController
: public HostPairingController,
public HostPairingController::Observer {
public:
typedef HostPairingController::Observer Observer;

// Config is a comma separated list of key-value pairs separated by colon.
// Supported options:
// * async_duration - integer. Default: 3000.
// * start_after_update - {0,1}. Default: 0.
// * fail_enrollment - {0,1}. Default: 0.
// * code - 6 digits or empty string. Default: empty string. If strings is
// empty, random code is generated.
// * device_name - string. Default: "Chromebox-01".
// * domain - string. Default: "example.com".
FakeHostPairingController(const std::string& config);
virtual ~FakeHostPairingController();

// Applies given |config| to flow.
void ApplyConfig(const std::string& config);

// Overridden from HostPairingFlow:
virtual void AddObserver(Observer* observer) OVERRIDE;
virtual void RemoveObserver(Observer* observer) OVERRIDE;
virtual Stage GetCurrentStage() OVERRIDE;
virtual void StartPairing() OVERRIDE;
virtual std::string GetDeviceName() OVERRIDE;
virtual std::string GetConfirmationCode() OVERRIDE;
virtual std::string GetEnrollmentDomain() OVERRIDE;

private:
void ChangeStage(Stage new_stage);
void ChangeStageLater(Stage new_stage);
void SetUpdateProgress(int step);

// Overridden from HostPairingFlow::Observer:
virtual void PairingStageChanged(Stage new_stage) OVERRIDE;
virtual void UpdateAdvanced(const UpdateProgress& progress) OVERRIDE;

ObserverList<Observer> observers_;
Stage current_stage_;
std::string device_name_;
std::string confirmation_code_;
base::TimeDelta async_duration_;

// If this flag is true error happens on |STAGE_ENROLLING| once.
bool enrollment_should_fail_;

// Controller starts its work like if update and reboot already happened.
bool start_after_update_;

std::string enrollment_domain_;

DISALLOW_COPY_AND_ASSIGN(FakeHostPairingController);
};

} // namespace chromeos

#endif // CHROMEOS_PAIRING_FAKE_HOST_PAIRING_CONTROLLER_H_
4 changes: 4 additions & 0 deletions chromeos/pairing/host_pairing_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class CHROMEOS_EXPORT HostPairingController {
// |STAGE_WAITING_FOR_CODE_CONFIRMATION| stage.
virtual std::string GetConfirmationCode() = 0;

// Returns an enrollment domain name. Can be called on stage
// |STAGE_ENROLLMENT| and later.
virtual std::string GetEnrollmentDomain() = 0;

private:
DISALLOW_COPY_AND_ASSIGN(HostPairingController);
};
Expand Down

0 comments on commit 7730e38

Please sign in to comment.