Skip to content

Commit

Permalink
Settings People Revamp: Easy Unlock: Add unit test for C++ handler.
Browse files Browse the repository at this point in the history
This implements unit tests for the C++ handler using a
FakeEasyUnlockService.

BUG=563721

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

Cr-Commit-Position: refs/heads/master@{#380013}
  • Loading branch information
tommycli authored and Commit bot committed Mar 9, 2016
1 parent 24a6b6d commit e9e89f0
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 4 deletions.
4 changes: 2 additions & 2 deletions chrome/browser/signin/easy_unlock_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ class EasyUnlockService : public KeyedService {

// Whether easy unlock is allowed to be used. If the controlling preference
// is set (from policy), this returns the preference value. Otherwise, it is
// permitted if the flag is enabled.
bool IsAllowed() const;
// permitted if the flag is enabled. Virtual to allow override for testing.
virtual bool IsAllowed() const;

// Whether Easy Unlock is currently enabled for this user.
bool IsEnabled() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void EasyUnlockSettingsHandler::SendTurnOffOperationStatus() {

web_ui()->CallJavascriptFunction(
"cr.webUIListenerCallback",
base::StringValue("easy-unlock-turn-off-dialog-update"),
base::StringValue("easy-unlock-turn-off-flow-status"),
base::StringValue(status_string));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ class EasyUnlockSettingsHandler : public content::WebUIMessageHandler,
// EasyUnlockServiceObserver
void OnTurnOffOperationStatusChanged() override;

private:
protected:
explicit EasyUnlockSettingsHandler(Profile* profile);

private:
FRIEND_TEST_ALL_PREFIXES(EasyUnlockSettingsHandlerTest, TurnOffStatus);

void SendTurnOffOperationStatus();

// JS callbacks.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2016 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/ui/webui/settings/chromeos/easy_unlock_settings_handler.h"

#include "chrome/browser/signin/easy_unlock_service.h"
#include "chrome/browser/signin/easy_unlock_service_factory.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {
namespace settings {

namespace {

class FakeEasyUnlockService : public EasyUnlockService {
public:
explicit FakeEasyUnlockService(Profile* profile)
: EasyUnlockService(profile), turn_off_status_(IDLE), is_allowed_(true) {}

TurnOffFlowStatus GetTurnOffFlowStatus() const override {
return turn_off_status_;
}

bool IsAllowed() const override { return is_allowed_; }

void set_is_allowed(bool is_allowed) { is_allowed_ = is_allowed; }

void RunTurnOffFlow() override {
turn_off_status_ = PENDING;
NotifyTurnOffOperationStatusChanged();
}

void ResetTurnOffFlow() override {
turn_off_status_ = IDLE;
NotifyTurnOffOperationStatusChanged();
}

void SetTurnOffFailForTest() {
turn_off_status_ = FAIL;
NotifyTurnOffOperationStatusChanged();
}

private:
Type GetType() const override { return TYPE_REGULAR; }
AccountId GetAccountId() const override { return EmptyAccountId(); }
void LaunchSetup() override {}
const base::DictionaryValue* GetPermitAccess() const override {
return nullptr;
}
void SetPermitAccess(const base::DictionaryValue& permit) override {}
void ClearPermitAccess() override {}

const base::ListValue* GetRemoteDevices() const override { return nullptr; }
void SetRemoteDevices(const base::ListValue& devices) override {}
void SetRemoteBleDevices(const base::ListValue& devices) override {}

std::string GetChallenge() const override { return std::string(); }
std::string GetWrappedSecret() const override { return std::string(); }
void RecordEasySignInOutcome(const AccountId& account_id,
bool success) const override {}
void RecordPasswordLoginEvent(const AccountId& account_id) const override {}
void StartAutoPairing(const AutoPairingResultCallback& callback) override {}
void SetAutoPairingResult(bool success, const std::string& error) override {}

void InitializeInternal() override {}
void ShutdownInternal() override {}
bool IsAllowedInternal() const override { return false; }
void OnWillFinalizeUnlock(bool success) override {}
void OnSuspendDoneInternal() override {}

TurnOffFlowStatus turn_off_status_;
bool is_allowed_;
};

class TestEasyUnlockSettingsHandler : public EasyUnlockSettingsHandler {
public:
explicit TestEasyUnlockSettingsHandler(Profile* profile)
: EasyUnlockSettingsHandler(profile) {}

using EasyUnlockSettingsHandler::set_web_ui;
};

scoped_ptr<KeyedService> CreateEasyUnlockServiceForTest(
content::BrowserContext* context) {
return make_scoped_ptr(
new FakeEasyUnlockService(Profile::FromBrowserContext(context)));
}

} // namespace

class EasyUnlockSettingsHandlerTest : public testing::Test {
public:
EasyUnlockSettingsHandlerTest() {}

void SetUp() override {
TestingProfile::Builder builder;
builder.AddTestingFactory(EasyUnlockServiceFactory::GetInstance(),
&CreateEasyUnlockServiceForTest);
profile_ = builder.Build();
}

Profile* profile() { return profile_.get(); }
content::TestWebUI* web_ui() { return &web_ui_; }
FakeEasyUnlockService* fake_easy_unlock_service() {
return static_cast<FakeEasyUnlockService*>(
EasyUnlockService::Get(profile_.get()));
}

void VerifyTurnOffStatusCallback(size_t expected_total_calls,
const std::string& expected_status) {
std::string event;
std::string status;

EXPECT_EQ(expected_total_calls, web_ui_.call_data().size());

const content::TestWebUI::CallData& data = *web_ui_.call_data().back();
EXPECT_EQ("cr.webUIListenerCallback", data.function_name());
ASSERT_TRUE(data.arg1()->GetAsString(&event));
EXPECT_EQ("easy-unlock-turn-off-flow-status", event);
ASSERT_TRUE(data.arg2()->GetAsString(&status));

EXPECT_EQ(expected_status, status);
}

private:
scoped_ptr<TestingProfile> profile_;
content::TestWebUI web_ui_;
};

TEST_F(EasyUnlockSettingsHandlerTest, OnlyCreatedWhenEasyUnlockAllowed) {
scoped_ptr<EasyUnlockSettingsHandler> handler;

scoped_ptr<content::WebUIDataSource> data_source(
content::WebUIDataSource::Create("test-data-source"));

handler.reset(
EasyUnlockSettingsHandler::Create(data_source.get(), profile()));
EXPECT_TRUE(handler.get());

fake_easy_unlock_service()->set_is_allowed(false);
handler.reset(
EasyUnlockSettingsHandler::Create(data_source.get(), profile()));
EXPECT_FALSE(handler.get());
}

TEST_F(EasyUnlockSettingsHandlerTest, TurnOffStatus) {
scoped_ptr<EasyUnlockSettingsHandler> handler;
handler.reset(new TestEasyUnlockSettingsHandler(profile()));
handler->set_web_ui(web_ui());

handler->HandleGetTurnOffFlowStatus(nullptr);
VerifyTurnOffStatusCallback(1U, "idle");

handler->HandleRequestTurnOff(nullptr);
VerifyTurnOffStatusCallback(2U, "pending");

handler->HandleGetTurnOffFlowStatus(nullptr);
VerifyTurnOffStatusCallback(3U, "pending");

handler->HandlePageDismissed(nullptr);
VerifyTurnOffStatusCallback(4U, "idle");

fake_easy_unlock_service()->SetTurnOffFailForTest();
VerifyTurnOffStatusCallback(5U, "server-error");

handler->HandleGetTurnOffFlowStatus(nullptr);
VerifyTurnOffStatusCallback(6U, "server-error");
}

} // namespace settings
} // namespace chromeos
1 change: 1 addition & 0 deletions chrome/chrome_tests_unit.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@
'browser/ui/webui/chromeos/login/l10n_util_unittest.cc',
'browser/ui/webui/chromeos/login/signin_userlist_unittest.cc',
'browser/ui/webui/options/chromeos/cros_language_options_handler_unittest.cc',
'browser/ui/webui/settings/chromeos/easy_unlock_settings_handler_unittest.cc',
'common/extensions/api/file_browser_handlers/file_browser_handler_manifest_unittest.cc',
'common/extensions/api/file_system_provider/file_system_provider_handler_unittest.cc',
],
Expand Down

0 comments on commit e9e89f0

Please sign in to comment.