diff --git a/chrome/browser/signin/easy_unlock_service.h b/chrome/browser/signin/easy_unlock_service.h index 8711a53667b5b9..5009b4f5313a3a 100644 --- a/chrome/browser/signin/easy_unlock_service.h +++ b/chrome/browser/signin/easy_unlock_service.h @@ -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; diff --git a/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.cc b/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.cc index 34c8f9758c88ff..686a85a2f5fc3b 100644 --- a/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.cc +++ b/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.cc @@ -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)); } diff --git a/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.h b/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.h index 47fe1974971012..b0c3f5672c3ff2 100644 --- a/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.h +++ b/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.h @@ -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. diff --git a/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler_unittest.cc b/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler_unittest.cc new file mode 100644 index 00000000000000..481fa2b15bc2bc --- /dev/null +++ b/chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler_unittest.cc @@ -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 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( + 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 profile_; + content::TestWebUI web_ui_; +}; + +TEST_F(EasyUnlockSettingsHandlerTest, OnlyCreatedWhenEasyUnlockAllowed) { + scoped_ptr handler; + + scoped_ptr 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 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 diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index fae9d59b18d624..1d9143474f1b1f 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -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', ],