forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Settings People Revamp: Easy Unlock: Add unit test for C++ handler.
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
Showing
5 changed files
with
183 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters