Commit 681645cc authored by Andrey Zaytsev's avatar Andrey Zaytsev Committed by Commit Bot

Safety check: added parsing displayed strings on the backend and split status updates

Bug: 1015841
Change-Id: I2b9db68ada58615108c459f33bcca258fb5a88d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2089868
Commit-Queue: Andrey Zaytsev <andzaytsev@google.com>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#748646}
parent 342db58f
...@@ -7,18 +7,32 @@ ...@@ -7,18 +7,32 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/metrics/user_metrics.h" #include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h" #include "base/metrics/user_metrics_action.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/password_manager/bulk_leak_check_service_factory.h" #include "chrome/browser/password_manager/bulk_leak_check_service_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h" #include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "ui/base/l10n/l10n_util.h"
#if defined(OS_CHROMEOS)
#include "ui/chromeos/devicetype_utils.h"
#endif
namespace { namespace {
// Constants for communication with JS. // Constants for communication with JS.
constexpr char kStatusChanged[] = "safety-check-status-changed"; constexpr char kUpdatesEvent[] = "safety-check-updates-status-changed";
constexpr char kPasswordsEvent[] = "safety-check-passwords-status-changed";
constexpr char kSafeBrowsingEvent[] =
"safety-check-safe-browsing-status-changed";
constexpr char kPerformSafetyCheck[] = "performSafetyCheck"; constexpr char kPerformSafetyCheck[] = "performSafetyCheck";
constexpr char kSafetyCheckComponent[] = "safetyCheckComponent";
constexpr char kNewState[] = "newState"; constexpr char kNewState[] = "newState";
constexpr char kDisplayedString[] = "displayedString";
constexpr char kPasswordsCompromised[] = "passwordsCompromised"; constexpr char kPasswordsCompromised[] = "passwordsCompromised";
// Converts the VersionUpdater::Status to the UpdateStatus enum to be passed // Converts the VersionUpdater::Status to the UpdateStatus enum to be passed
...@@ -125,31 +139,111 @@ void SafetyCheckHandler::OnUpdateCheckResult(VersionUpdater::Status status, ...@@ -125,31 +139,111 @@ void SafetyCheckHandler::OnUpdateCheckResult(VersionUpdater::Status status,
const base::string16& message) { const base::string16& message) {
UpdateStatus update_status = ConvertToUpdateStatus(status); UpdateStatus update_status = ConvertToUpdateStatus(status);
base::DictionaryValue event; base::DictionaryValue event;
event.SetIntKey(kSafetyCheckComponent,
static_cast<int>(SafetyCheckComponent::kUpdates));
event.SetIntKey(kNewState, static_cast<int>(update_status)); event.SetIntKey(kNewState, static_cast<int>(update_status));
FireWebUIListener(kStatusChanged, event); event.SetStringKey(kDisplayedString, GetStringForUpdates(update_status));
FireWebUIListener(kUpdatesEvent, event);
} }
void SafetyCheckHandler::OnSafeBrowsingCheckResult( void SafetyCheckHandler::OnSafeBrowsingCheckResult(
SafetyCheckHandler::SafeBrowsingStatus status) { SafetyCheckHandler::SafeBrowsingStatus status) {
base::DictionaryValue event; base::DictionaryValue event;
event.SetIntKey(kSafetyCheckComponent,
static_cast<int>(SafetyCheckComponent::kSafeBrowsing));
event.SetIntKey(kNewState, static_cast<int>(status)); event.SetIntKey(kNewState, static_cast<int>(status));
FireWebUIListener(kStatusChanged, event); event.SetStringKey(kDisplayedString, GetStringForSafeBrowsing(status));
FireWebUIListener(kSafeBrowsingEvent, event);
} }
void SafetyCheckHandler::OnPasswordsCheckResult(PasswordsStatus status, void SafetyCheckHandler::OnPasswordsCheckResult(PasswordsStatus status,
int num_compromised) { int num_compromised) {
base::DictionaryValue event; base::DictionaryValue event;
event.SetIntKey(kSafetyCheckComponent,
static_cast<int>(SafetyCheckComponent::kPasswords));
event.SetIntKey(kNewState, static_cast<int>(status)); event.SetIntKey(kNewState, static_cast<int>(status));
if (status == PasswordsStatus::kCompromisedExist) { if (status == PasswordsStatus::kCompromisedExist) {
event.SetIntKey(kPasswordsCompromised, num_compromised); event.SetIntKey(kPasswordsCompromised, num_compromised);
} }
FireWebUIListener(kStatusChanged, event); event.SetStringKey(kDisplayedString,
GetStringForPasswords(status, num_compromised));
FireWebUIListener(kPasswordsEvent, event);
}
base::string16 SafetyCheckHandler::GetStringForUpdates(UpdateStatus status) {
switch (status) {
case UpdateStatus::kChecking:
return l10n_util::GetStringUTF16(IDS_SETTINGS_SAFETY_CHECK_RUNNING);
case UpdateStatus::kUpdated:
#if defined(OS_CHROMEOS)
return ui::SubstituteChromeOSDeviceType(IDS_SETTINGS_UPGRADE_UP_TO_DATE);
#else
return l10n_util::GetStringUTF16(IDS_SETTINGS_UPGRADE_UP_TO_DATE);
#endif
case UpdateStatus::kUpdating:
return l10n_util::GetStringUTF16(IDS_SETTINGS_UPGRADE_UPDATING);
case UpdateStatus::kRelaunch:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_UPGRADE_SUCCESSFUL_RELAUNCH);
case UpdateStatus::kDisabledByAdmin:
return l10n_util::GetStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_UPDATES_DISABLED_BY_ADMIN,
base::ASCIIToUTF16(chrome::kWhoIsMyAdministratorHelpURL));
case UpdateStatus::kFailedOffline:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_UPDATES_FAILED_OFFLINE);
case UpdateStatus::kFailed:
return l10n_util::GetStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_UPDATES_FAILED,
base::ASCIIToUTF16(chrome::kChromeFixUpdateProblems));
}
}
base::string16 SafetyCheckHandler::GetStringForSafeBrowsing(
SafeBrowsingStatus status) {
switch (status) {
case SafeBrowsingStatus::kChecking:
return l10n_util::GetStringUTF16(IDS_SETTINGS_SAFETY_CHECK_RUNNING);
case SafeBrowsingStatus::kEnabled:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_SAFE_BROWSING_ENABLED);
case SafeBrowsingStatus::kDisabled:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_SAFE_BROWSING_DISABLED);
case SafeBrowsingStatus::kDisabledByAdmin:
return l10n_util::GetStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_SAFE_BROWSING_DISABLED_BY_ADMIN,
base::ASCIIToUTF16(chrome::kWhoIsMyAdministratorHelpURL));
case SafeBrowsingStatus::kDisabledByExtension:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_SAFE_BROWSING_DISABLED_BY_EXTENSION);
}
}
base::string16 SafetyCheckHandler::GetStringForPasswords(PasswordsStatus status,
int num_compromised) {
switch (status) {
case PasswordsStatus::kChecking:
return l10n_util::GetStringUTF16(IDS_SETTINGS_SAFETY_CHECK_RUNNING);
case PasswordsStatus::kSafe:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PASSWORDS_SAFE);
case PasswordsStatus::kCompromisedExist:
return l10n_util::GetPluralStringFUTF16(
IDS_SETTINGS_SAFETY_CHECK_PASSWORDS_COMPROMISED, num_compromised);
case PasswordsStatus::kOffline:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PASSWORDS_OFFLINE);
case PasswordsStatus::kNoPasswords:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PASSWORDS_NO_PASSWORDS);
case PasswordsStatus::kSignedOut:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PASSWORDS_SIGNED_OUT);
case PasswordsStatus::kQuotaLimit:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PASSWORDS_QUOTA_LIMIT);
case PasswordsStatus::kTooManyPasswords:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PASSWORDS_TOO_MANY_PASSWORDS);
case PasswordsStatus::kError:
return l10n_util::GetStringUTF16(
IDS_SETTINGS_SAFETY_CHECK_PASSWORDS_ERROR);
}
} }
void SafetyCheckHandler::OnStateChanged( void SafetyCheckHandler::OnStateChanged(
......
...@@ -25,14 +25,6 @@ class SafetyCheckHandler ...@@ -25,14 +25,6 @@ class SafetyCheckHandler
: public settings::SettingsPageUIHandler, : public settings::SettingsPageUIHandler,
public password_manager::BulkLeakCheckService::Observer { public password_manager::BulkLeakCheckService::Observer {
public: public:
// Used in communication with the frontend to indicate which component the
// communicated status update applies to.
enum class SafetyCheckComponent {
kUpdates,
kPasswords,
kSafeBrowsing,
kExtensions,
};
// The following enums represent the state of each component of the safety // The following enums represent the state of each component of the safety
// check and should be kept in sync with the JS frontend // check and should be kept in sync with the JS frontend
// (safety_check_browser_proxy.js). // (safety_check_browser_proxy.js).
...@@ -100,11 +92,16 @@ class SafetyCheckHandler ...@@ -100,11 +92,16 @@ class SafetyCheckHandler
const std::string& version, const std::string& version,
int64_t update_size, int64_t update_size,
const base::string16& message); const base::string16& message);
void OnSafeBrowsingCheckResult(SafeBrowsingStatus status); void OnSafeBrowsingCheckResult(SafeBrowsingStatus status);
void OnPasswordsCheckResult(PasswordsStatus status, int num_compromised); void OnPasswordsCheckResult(PasswordsStatus status, int num_compromised);
// Methods for building user-visible strings based on the safety check
// state.
base::string16 GetStringForUpdates(UpdateStatus status);
base::string16 GetStringForSafeBrowsing(SafeBrowsingStatus status);
base::string16 GetStringForPasswords(PasswordsStatus status,
int num_compromised);
// BulkLeakCheckService::Observer implementation. // BulkLeakCheckService::Observer implementation.
void OnStateChanged( void OnStateChanged(
password_manager::BulkLeakCheckService::State state) override; password_manager::BulkLeakCheckService::State state) override;
......
...@@ -4,9 +4,14 @@ ...@@ -4,9 +4,14 @@
#include "chrome/browser/ui/webui/settings/safety_check_handler.h" #include "chrome/browser/ui/webui/settings/safety_check_handler.h"
#include <string>
#include "base/bind.h" #include "base/bind.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/user_action_tester.h" #include "base/test/metrics/user_action_tester.h"
#include "build/build_config.h"
#include "chrome/browser/ui/webui/help/test_version_updater.h" #include "chrome/browser/ui/webui/help/test_version_updater.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/password_manager/core/browser/bulk_leak_check_service.h" #include "components/password_manager/core/browser/bulk_leak_check_service.h"
...@@ -17,6 +22,15 @@ ...@@ -17,6 +22,15 @@
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_CHROMEOS)
#include "ui/chromeos/devicetype_utils.h"
#endif
// Components for building event strings.
constexpr char kUpdates[] = "updates";
constexpr char kPasswords[] = "passwords";
constexpr char kSafeBrowsing[] = "safe-browsing";
class TestingSafetyCheckHandler : public SafetyCheckHandler { class TestingSafetyCheckHandler : public SafetyCheckHandler {
public: public:
using SafetyCheckHandler::AllowJavascript; using SafetyCheckHandler::AllowJavascript;
...@@ -33,15 +47,28 @@ class SafetyCheckHandlerTest : public ChromeRenderViewHostTestHarness { ...@@ -33,15 +47,28 @@ class SafetyCheckHandlerTest : public ChromeRenderViewHostTestHarness {
public: public:
void SetUp() override; void SetUp() override;
// Returns whether one of the WebUI callbacks for safety check status changes // Returns a |base::DictionaryValue| for safety check status update that
// has the specified |safetyCheckComponent| and |newState|. // has the specified |component| and |new_state| if it exists; nullptr
bool HasSafetyCheckStatusChangedWithData(int component, int new_state); // otherwise.
const base::DictionaryValue* GetSafetyCheckStatusChangedWithDataIfExists(
const std::string& component,
int new_state);
void VerifyDisplayedString(const base::DictionaryValue* event,
const base::string16& expected);
void VerifyDisplayedString(const base::DictionaryValue* event,
const std::string& expected);
protected: protected:
TestVersionUpdater* version_updater_ = nullptr; TestVersionUpdater* version_updater_ = nullptr;
std::unique_ptr<password_manager::BulkLeakCheckService> test_leak_service_; std::unique_ptr<password_manager::BulkLeakCheckService> test_leak_service_;
content::TestWebUI test_web_ui_; content::TestWebUI test_web_ui_;
std::unique_ptr<TestingSafetyCheckHandler> safety_check_; std::unique_ptr<TestingSafetyCheckHandler> safety_check_;
private:
// Replaces any instances of browser name (e.g. Google Chrome, Chromium, etc)
// with "browser" to make sure tests work both on Chromium and Google Chrome.
void ReplaceBrowserName(base::string16* s);
}; };
void SafetyCheckHandlerTest::SetUp() { void SafetyCheckHandlerTest::SetUp() {
...@@ -62,8 +89,9 @@ void SafetyCheckHandlerTest::SetUp() { ...@@ -62,8 +89,9 @@ void SafetyCheckHandlerTest::SetUp() {
safety_check_->AllowJavascript(); safety_check_->AllowJavascript();
} }
bool SafetyCheckHandlerTest::HasSafetyCheckStatusChangedWithData( const base::DictionaryValue*
int component, SafetyCheckHandlerTest::GetSafetyCheckStatusChangedWithDataIfExists(
const std::string& component,
int new_state) { int new_state) {
for (const auto& it : test_web_ui_.call_data()) { for (const auto& it : test_web_ui_.call_data()) {
const content::TestWebUI::CallData& data = *it; const content::TestWebUI::CallData& data = *it;
...@@ -72,22 +100,49 @@ bool SafetyCheckHandlerTest::HasSafetyCheckStatusChangedWithData( ...@@ -72,22 +100,49 @@ bool SafetyCheckHandlerTest::HasSafetyCheckStatusChangedWithData(
} }
std::string event; std::string event;
if ((!data.arg1()->GetAsString(&event)) || if ((!data.arg1()->GetAsString(&event)) ||
event != "safety-check-status-changed") { event != "safety-check-" + component + "-status-changed") {
continue; continue;
} }
const base::DictionaryValue* dictionary = nullptr; const base::DictionaryValue* dictionary = nullptr;
if (!data.arg2()->GetAsDictionary(&dictionary)) { if (!data.arg2()->GetAsDictionary(&dictionary)) {
continue; continue;
} }
int cur_component, cur_new_state; int cur_new_state;
if (dictionary->GetInteger("safetyCheckComponent", &cur_component) && if (dictionary->GetInteger("newState", &cur_new_state) &&
cur_component == component &&
dictionary->GetInteger("newState", &cur_new_state) &&
cur_new_state == new_state) { cur_new_state == new_state) {
return true; return dictionary;
} }
} }
return false; return nullptr;
}
void SafetyCheckHandlerTest::VerifyDisplayedString(
const base::DictionaryValue* event,
const base::string16& expected) {
base::string16 displayed;
ASSERT_TRUE(event->GetString("displayedString", &displayed));
ReplaceBrowserName(&displayed);
// Need to also replace any instances of Chrome and Chromium in the expected
// string due to an edge case on ChromeOS, where a device name is "Chrome",
// which gets replaced in the displayed string.
base::string16 expected_replaced = expected;
ReplaceBrowserName(&expected_replaced);
EXPECT_EQ(expected_replaced, displayed);
}
void SafetyCheckHandlerTest::VerifyDisplayedString(
const base::DictionaryValue* event,
const std::string& expected) {
VerifyDisplayedString(event, base::ASCIIToUTF16(expected));
}
void SafetyCheckHandlerTest::ReplaceBrowserName(base::string16* s) {
base::ReplaceSubstringsAfterOffset(s, 0, base::ASCIIToUTF16("Google Chrome"),
base::ASCIIToUTF16("Browser"));
base::ReplaceSubstringsAfterOffset(s, 0, base::ASCIIToUTF16("Chrome"),
base::ASCIIToUTF16("Browser"));
base::ReplaceSubstringsAfterOffset(s, 0, base::ASCIIToUTF16("Chromium"),
base::ASCIIToUTF16("Browser"));
} }
TEST_F(SafetyCheckHandlerTest, PerformSafetyCheck_MetricsRecorded) { TEST_F(SafetyCheckHandlerTest, PerformSafetyCheck_MetricsRecorded) {
...@@ -96,21 +151,110 @@ TEST_F(SafetyCheckHandlerTest, PerformSafetyCheck_MetricsRecorded) { ...@@ -96,21 +151,110 @@ TEST_F(SafetyCheckHandlerTest, PerformSafetyCheck_MetricsRecorded) {
EXPECT_EQ(1, user_action_tester.GetActionCount("SafetyCheck.Started")); EXPECT_EQ(1, user_action_tester.GetActionCount("SafetyCheck.Started"));
} }
TEST_F(SafetyCheckHandlerTest, CheckUpdates_Checking) {
version_updater_->SetReturnedStatus(VersionUpdater::Status::CHECKING);
safety_check_->PerformSafetyCheck();
const base::DictionaryValue* event =
GetSafetyCheckStatusChangedWithDataIfExists(
kUpdates,
static_cast<int>(SafetyCheckHandler::UpdateStatus::kChecking));
ASSERT_TRUE(event);
VerifyDisplayedString(event, base::UTF8ToUTF16("Running…"));
}
TEST_F(SafetyCheckHandlerTest, CheckUpdates_Updated) { TEST_F(SafetyCheckHandlerTest, CheckUpdates_Updated) {
version_updater_->SetReturnedStatus(VersionUpdater::Status::UPDATED); version_updater_->SetReturnedStatus(VersionUpdater::Status::UPDATED);
safety_check_->PerformSafetyCheck(); safety_check_->PerformSafetyCheck();
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kUpdates), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::UpdateStatus::kUpdated))); kUpdates,
static_cast<int>(SafetyCheckHandler::UpdateStatus::kUpdated));
ASSERT_TRUE(event);
#if defined(OS_CHROMEOS)
base::string16 expected = base::ASCIIToUTF16("Your ") +
ui::GetChromeOSDeviceName() +
base::ASCIIToUTF16(" is up to date");
VerifyDisplayedString(event, expected);
#else
VerifyDisplayedString(event, "Browser is up to date");
#endif
} }
TEST_F(SafetyCheckHandlerTest, CheckUpdates_NotUpdated) { TEST_F(SafetyCheckHandlerTest, CheckUpdates_Updating) {
version_updater_->SetReturnedStatus(VersionUpdater::Status::UPDATING);
safety_check_->PerformSafetyCheck();
const base::DictionaryValue* event =
GetSafetyCheckStatusChangedWithDataIfExists(
kUpdates,
static_cast<int>(SafetyCheckHandler::UpdateStatus::kUpdating));
ASSERT_TRUE(event);
#if defined(OS_CHROMEOS)
VerifyDisplayedString(event, "Updating your device");
#else
VerifyDisplayedString(event, "Updating Browser");
#endif
}
TEST_F(SafetyCheckHandlerTest, CheckUpdates_Relaunch) {
version_updater_->SetReturnedStatus(VersionUpdater::Status::NEARLY_UPDATED);
safety_check_->PerformSafetyCheck();
const base::DictionaryValue* event =
GetSafetyCheckStatusChangedWithDataIfExists(
kUpdates,
static_cast<int>(SafetyCheckHandler::UpdateStatus::kRelaunch));
ASSERT_TRUE(event);
#if defined(OS_CHROMEOS)
VerifyDisplayedString(
event, "Nearly up to date! Restart your device to finish updating.");
#else
VerifyDisplayedString(event,
"Nearly up to date! Relaunch Browser to finish "
"updating. Incognito windows won't reopen.");
#endif
}
TEST_F(SafetyCheckHandlerTest, CheckUpdates_DisabledByAdmin) {
version_updater_->SetReturnedStatus( version_updater_->SetReturnedStatus(
VersionUpdater::Status::DISABLED_BY_ADMIN); VersionUpdater::Status::DISABLED_BY_ADMIN);
safety_check_->PerformSafetyCheck(); safety_check_->PerformSafetyCheck();
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kUpdates), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::UpdateStatus::kDisabledByAdmin))); kUpdates,
static_cast<int>(SafetyCheckHandler::UpdateStatus::kDisabledByAdmin));
ASSERT_TRUE(event);
VerifyDisplayedString(
event,
"Updates are managed by <a target=\"_blank\" "
"href=\"https://support.google.com/accounts/answer/6208960\">your "
"administrator</a>");
}
TEST_F(SafetyCheckHandlerTest, CheckUpdates_FailedOffline) {
version_updater_->SetReturnedStatus(VersionUpdater::Status::FAILED_OFFLINE);
safety_check_->PerformSafetyCheck();
const base::DictionaryValue* event =
GetSafetyCheckStatusChangedWithDataIfExists(
kUpdates,
static_cast<int>(SafetyCheckHandler::UpdateStatus::kFailedOffline));
ASSERT_TRUE(event);
VerifyDisplayedString(event,
"Browser can't check for updates. Try checking your "
"internet connection.");
}
TEST_F(SafetyCheckHandlerTest, CheckUpdates_Failed) {
version_updater_->SetReturnedStatus(VersionUpdater::Status::FAILED);
safety_check_->PerformSafetyCheck();
const base::DictionaryValue* event =
GetSafetyCheckStatusChangedWithDataIfExists(
kUpdates,
static_cast<int>(SafetyCheckHandler::UpdateStatus::kFailed));
ASSERT_TRUE(event);
VerifyDisplayedString(
event,
"Browser didn't update, something went wrong. <a target=\"_blank\" "
"href=\"https://support.google.com/chrome/answer/111996\">Fix Browser "
"update problems and failed updates.</a>");
} }
TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_Enabled) { TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_Enabled) {
...@@ -118,9 +262,14 @@ TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_Enabled) { ...@@ -118,9 +262,14 @@ TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_Enabled) {
->GetPrefs() ->GetPrefs()
->SetBoolean(prefs::kSafeBrowsingEnabled, true); ->SetBoolean(prefs::kSafeBrowsingEnabled, true);
safety_check_->PerformSafetyCheck(); safety_check_->PerformSafetyCheck();
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kSafeBrowsing), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::SafeBrowsingStatus::kEnabled))); kSafeBrowsing,
static_cast<int>(SafetyCheckHandler::SafeBrowsingStatus::kEnabled));
ASSERT_TRUE(event);
VerifyDisplayedString(event,
"Safe Browsing is up to date and protecting you from "
"harmful sites and downloads");
} }
TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_Disabled) { TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_Disabled) {
...@@ -128,9 +277,13 @@ TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_Disabled) { ...@@ -128,9 +277,13 @@ TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_Disabled) {
->GetPrefs() ->GetPrefs()
->SetBoolean(prefs::kSafeBrowsingEnabled, false); ->SetBoolean(prefs::kSafeBrowsingEnabled, false);
safety_check_->PerformSafetyCheck(); safety_check_->PerformSafetyCheck();
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kSafeBrowsing), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::SafeBrowsingStatus::kDisabled))); kSafeBrowsing,
static_cast<int>(SafetyCheckHandler::SafeBrowsingStatus::kDisabled));
ASSERT_TRUE(event);
VerifyDisplayedString(
event, "Safe Browsing is off. To stay safe on the web, turn it on.");
} }
TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_DisabledByAdmin) { TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_DisabledByAdmin) {
...@@ -140,10 +293,17 @@ TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_DisabledByAdmin) { ...@@ -140,10 +293,17 @@ TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_DisabledByAdmin) {
->SetManagedPref(prefs::kSafeBrowsingEnabled, ->SetManagedPref(prefs::kSafeBrowsingEnabled,
std::make_unique<base::Value>(false)); std::make_unique<base::Value>(false));
safety_check_->PerformSafetyCheck(); safety_check_->PerformSafetyCheck();
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kSafeBrowsing), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>( kSafeBrowsing,
SafetyCheckHandler::SafeBrowsingStatus::kDisabledByAdmin))); static_cast<int>(
SafetyCheckHandler::SafeBrowsingStatus::kDisabledByAdmin));
ASSERT_TRUE(event);
VerifyDisplayedString(
event,
"<a target=\"_blank\" "
"href=\"https://support.google.com/accounts/answer/6208960\">Your "
"administrator</a> has turned off Safe Browsing");
} }
TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_DisabledByExtension) { TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_DisabledByExtension) {
...@@ -153,10 +313,13 @@ TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_DisabledByExtension) { ...@@ -153,10 +313,13 @@ TEST_F(SafetyCheckHandlerTest, CheckSafeBrowsing_DisabledByExtension) {
->SetExtensionPref(prefs::kSafeBrowsingEnabled, ->SetExtensionPref(prefs::kSafeBrowsingEnabled,
std::make_unique<base::Value>(false)); std::make_unique<base::Value>(false));
safety_check_->PerformSafetyCheck(); safety_check_->PerformSafetyCheck();
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kSafeBrowsing), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>( kSafeBrowsing,
SafetyCheckHandler::SafeBrowsingStatus::kDisabledByExtension))); static_cast<int>(
SafetyCheckHandler::SafeBrowsingStatus::kDisabledByExtension));
ASSERT_TRUE(event);
VerifyDisplayedString(event, "An extension has turned off Safe Browsing");
} }
TEST_F(SafetyCheckHandlerTest, CheckPasswords_ObserverRemovedAfterError) { TEST_F(SafetyCheckHandlerTest, CheckPasswords_ObserverRemovedAfterError) {
...@@ -164,22 +327,32 @@ TEST_F(SafetyCheckHandlerTest, CheckPasswords_ObserverRemovedAfterError) { ...@@ -164,22 +327,32 @@ TEST_F(SafetyCheckHandlerTest, CheckPasswords_ObserverRemovedAfterError) {
// First, a "running" change of state. // First, a "running" change of state.
test_leak_service_->set_state_and_notify( test_leak_service_->set_state_and_notify(
password_manager::BulkLeakCheckService::State::kRunning); password_manager::BulkLeakCheckService::State::kRunning);
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kPasswords), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kChecking))); kPasswords,
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kChecking));
ASSERT_TRUE(event);
VerifyDisplayedString(event, base::UTF8ToUTF16("Running…"));
// Second, an "offline" state. // Second, an "offline" state.
test_leak_service_->set_state_and_notify( test_leak_service_->set_state_and_notify(
password_manager::BulkLeakCheckService::State::kNetworkError); password_manager::BulkLeakCheckService::State::kNetworkError);
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event2 =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kPasswords), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kOffline))); kPasswords,
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kOffline));
ASSERT_TRUE(event2);
VerifyDisplayedString(event2,
"Browser can't check your passwords. Try checking your "
"internet connection.");
// Another error, but since the previous state is terminal, the handler should // Another error, but since the previous state is terminal, the handler should
// no longer be observing the BulkLeakCheckService state. // no longer be observing the BulkLeakCheckService state.
test_leak_service_->set_state_and_notify( test_leak_service_->set_state_and_notify(
password_manager::BulkLeakCheckService::State::kServiceError); password_manager::BulkLeakCheckService::State::kServiceError);
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event3 =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kPasswords), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kOffline))); kPasswords,
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kOffline));
ASSERT_TRUE(event3);
} }
TEST_F(SafetyCheckHandlerTest, CheckPasswords_InterruptedAndRefreshed) { TEST_F(SafetyCheckHandlerTest, CheckPasswords_InterruptedAndRefreshed) {
...@@ -187,9 +360,12 @@ TEST_F(SafetyCheckHandlerTest, CheckPasswords_InterruptedAndRefreshed) { ...@@ -187,9 +360,12 @@ TEST_F(SafetyCheckHandlerTest, CheckPasswords_InterruptedAndRefreshed) {
// Password check running. // Password check running.
test_leak_service_->set_state_and_notify( test_leak_service_->set_state_and_notify(
password_manager::BulkLeakCheckService::State::kRunning); password_manager::BulkLeakCheckService::State::kRunning);
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kPasswords), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kChecking))); kPasswords,
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kChecking));
ASSERT_TRUE(event);
VerifyDisplayedString(event, base::UTF8ToUTF16("Running…"));
// The check gets interrupted and the page is refreshed. // The check gets interrupted and the page is refreshed.
safety_check_->DisallowJavascript(); safety_check_->DisallowJavascript();
safety_check_->AllowJavascript(); safety_check_->AllowJavascript();
...@@ -197,14 +373,21 @@ TEST_F(SafetyCheckHandlerTest, CheckPasswords_InterruptedAndRefreshed) { ...@@ -197,14 +373,21 @@ TEST_F(SafetyCheckHandlerTest, CheckPasswords_InterruptedAndRefreshed) {
safety_check_->PerformSafetyCheck(); safety_check_->PerformSafetyCheck();
test_leak_service_->set_state_and_notify( test_leak_service_->set_state_and_notify(
password_manager::BulkLeakCheckService::State::kRunning); password_manager::BulkLeakCheckService::State::kRunning);
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event2 =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kPasswords), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kChecking))); kPasswords,
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kChecking));
ASSERT_TRUE(event2);
test_leak_service_->set_state_and_notify( test_leak_service_->set_state_and_notify(
password_manager::BulkLeakCheckService::State::kNetworkError); password_manager::BulkLeakCheckService::State::kSignedOut);
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event3 =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kPasswords), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kOffline))); kPasswords,
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kSignedOut));
ASSERT_TRUE(event3);
VerifyDisplayedString(
event3,
"Browser can't check your passwords because you're not signed in");
} }
TEST_F(SafetyCheckHandlerTest, CheckPasswords_StartedTwice) { TEST_F(SafetyCheckHandlerTest, CheckPasswords_StartedTwice) {
...@@ -213,13 +396,19 @@ TEST_F(SafetyCheckHandlerTest, CheckPasswords_StartedTwice) { ...@@ -213,13 +396,19 @@ TEST_F(SafetyCheckHandlerTest, CheckPasswords_StartedTwice) {
// First, a "running" change of state. // First, a "running" change of state.
test_leak_service_->set_state_and_notify( test_leak_service_->set_state_and_notify(
password_manager::BulkLeakCheckService::State::kRunning); password_manager::BulkLeakCheckService::State::kRunning);
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kPasswords), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kChecking))); kPasswords,
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kChecking));
ASSERT_TRUE(event);
// Second, an "offline" state. // Second, an "offline" state.
test_leak_service_->set_state_and_notify( test_leak_service_->set_state_and_notify(
password_manager::BulkLeakCheckService::State::kNetworkError); password_manager::BulkLeakCheckService::State::kServiceError);
EXPECT_TRUE(HasSafetyCheckStatusChangedWithData( const base::DictionaryValue* event2 =
static_cast<int>(SafetyCheckHandler::SafetyCheckComponent::kPasswords), GetSafetyCheckStatusChangedWithDataIfExists(
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kOffline))); kPasswords,
static_cast<int>(SafetyCheckHandler::PasswordsStatus::kError));
ASSERT_TRUE(event2);
VerifyDisplayedString(event2,
"Browser can't check your passwords. Try again later.");
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment