Chromium Code Reviews| Index: chrome/browser/safe_browsing/chrome_password_protection_service_unittest.cc |
| diff --git a/chrome/browser/safe_browsing/chrome_password_protection_service_unittest.cc b/chrome/browser/safe_browsing/chrome_password_protection_service_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d3dc732435a18e71d292d57fb0b5fb19a39472cf |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/chrome_password_protection_service_unittest.cc |
| @@ -0,0 +1,188 @@ |
| +// Copyright 2017 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/safe_browsing/chrome_password_protection_service.h" |
| + |
| +#include "base/test/scoped_feature_list.h" |
| +#include "components/variations/variations_params_manager.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace safe_browsing { |
| + |
| +class MockChromePasswordProtectionService |
| + : public ChromePasswordProtectionService { |
| + public: |
| + MockChromePasswordProtectionService() |
| + : ChromePasswordProtectionService(), |
| + is_incognito_(false), |
| + is_extended_reporting_(false), |
| + is_history_sync_enabled_(false) {} |
| + bool IsExtendedReporting() override { return is_extended_reporting_; } |
|
Nathan Parker
2017/05/05 20:58:22
nit: order these the same as how they're listed be
|
| + bool IsIncognito() override { return is_incognito_; } |
| + bool IsHistorySyncEnabled() override { return is_history_sync_enabled_; } |
| + |
| + // Configures the results returned by IsExtendedReporting(), IsIncognito(), |
| + // and IsHistorySyncEnabled(). |
| + void ConfigService(bool is_incognito, |
| + bool is_extended_reporting, |
| + bool is_history_sync_enabled) { |
| + is_incognito_ = is_incognito; |
| + is_extended_reporting_ = is_extended_reporting; |
| + is_history_sync_enabled_ = is_history_sync_enabled; |
| + } |
| + |
| + private: |
| + bool is_incognito_; |
| + bool is_extended_reporting_; |
| + bool is_history_sync_enabled_; |
| +}; |
| + |
| +class ChromePasswordProtectionServiceTest : public testing::Test { |
| + public: |
| + typedef std::map<std::string, std::string> Parameters; |
| + ChromePasswordProtectionServiceTest() {} |
| + |
| + // Sets up Finch trial feature parameters. |
| + void SetFeatureParams(const base::Feature& feature, |
| + const std::string& trial_name, |
| + const Parameters& params) { |
| + static std::set<std::string> features = {feature.name}; |
|
Nathan Parker
2017/05/05 20:58:22
Why is this static? This could cause tests to have
|
| + params_manager_.ClearAllVariationParams(); |
| + params_manager_.SetVariationParamsWithFeatureAssociations(trial_name, |
| + params, features); |
| + } |
| + |
| + // Creates Finch trial parameters. |
| + Parameters CreateParameters(bool allowed_for_incognito, |
| + bool allowed_for_extended_reporting, |
| + bool allowed_for_history_sync) { |
| + return {{"incognito", allowed_for_incognito ? "true" : "false"}, |
| + {"extended_reporting", |
| + allowed_for_extended_reporting ? "true" : "false"}, |
| + {"history_sync", allowed_for_history_sync ? "true" : "false"}}; |
| + } |
| + |
| + protected: |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + variations::testing::VariationParamsManager params_manager_; |
| + base::test::ScopedFeatureList scoped_feature_list_; |
| +}; |
| + |
| +TEST_F(ChromePasswordProtectionServiceTest, |
| + VerifyFinchControlForLowReputationPingSBEROnlyNoIncognito) { |
| + MockChromePasswordProtectionService service; |
| + // By default kLowReputationPinging feature is disabled. |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + |
| + // Enables kLowReputationPinging feature. |
| + scoped_feature_list_.InitAndEnableFeature(kLowReputationPinging); |
| + // Creates finch trial parameters correspond to the following experiment: |
| + // "name": "LowReputationPingSBEROnlyNoIncognito", |
| + // "params": { |
| + // "incognito": "false", |
| + // "extended_reporting": "true", |
| + // "history_sync": "false" |
| + // }, |
| + // "enable_features": [ |
| + // "LowReputationPinging" |
| + // ] |
| + Parameters sber_and_no_incognito = CreateParameters(false, true, false); |
| + SetFeatureParams(kLowReputationPinging, "SBEROnlyNoIncognito", |
| + sber_and_no_incognito); |
| + service.ConfigService(false /*incognito*/, false /*SBER*/, false /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, false /*SBER*/, true /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, true /*SBER*/, false /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, true /*SBER*/, true /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, false /*SBER*/, false /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, false /*SBER*/, true /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, true /*SBER*/, false /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, true /*SBER*/, true /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| +} |
| + |
| +TEST_F(ChromePasswordProtectionServiceTest, |
| + VerifyFinchControlForLowReputationPingSBERAndHistorySyncNoIncognito) { |
| + MockChromePasswordProtectionService service; |
| + // By default kLowReputationPinging feature is disabled. |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + |
| + // Enables kLowReputationPinging feature. |
| + scoped_feature_list_.InitAndEnableFeature(kLowReputationPinging); |
| + // Creates finch trial parameters correspond to the following experiment: |
| + // "name": "LowReputationPingSBERAndHistorySyncNoIncognito", |
| + // "params": { |
| + // "incognito": "false", |
| + // "extended_reporting": "true", |
| + // "history_sync": "true" |
| + // }, |
| + // "enable_features": [ |
| + // "LowReputationPinging" |
| + // ] |
| + Parameters sber_and_sync_no_incognito = CreateParameters(false, true, true); |
| + SetFeatureParams(kLowReputationPinging, "SBERAndHistorySyncNoIncognito", |
| + sber_and_sync_no_incognito); |
| + service.ConfigService(false /*incognito*/, false /*SBER*/, false /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, false /*SBER*/, true /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, true /*SBER*/, false /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, true /*SBER*/, true /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, false /*SBER*/, false /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, false /*SBER*/, true /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, true /*SBER*/, false /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, true /*SBER*/, true /*sync*/); |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| +} |
| + |
| +TEST_F(ChromePasswordProtectionServiceTest, |
| + VerifyFinchControlForLowReputationPingAll) { |
| + MockChromePasswordProtectionService service; |
| + // By default kLowReputationPinging feature is disabled. |
| + EXPECT_FALSE(service.IsPingingEnabled(kLowReputationPinging)); |
| + |
| + // Enables kLowReputationPinging feature. |
| + scoped_feature_list_.InitAndEnableFeature(kLowReputationPinging); |
| + // Creates finch trial parameters correspond to the following experiment: |
| + // "name": "LowReputationPingAll", |
| + // "params": { |
| + // "incognito": "true", |
| + // "extended_reporting": "true", |
| + // "history_sync": "true" |
| + // }, |
| + // "enable_features": [ |
| + // "LowReputationPinging" |
| + // ] |
| + Parameters all_users = CreateParameters(true, true, true); |
| + SetFeatureParams(kLowReputationPinging, "All", all_users); |
| + service.ConfigService(false /*incognito*/, false /*SBER*/, false /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, false /*SBER*/, true /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, true /*SBER*/, false /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(false /*incognito*/, true /*SBER*/, true /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, false /*SBER*/, false /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, false /*SBER*/, true /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, true /*SBER*/, false /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| + service.ConfigService(true /*incognito*/, true /*SBER*/, true /*sync*/); |
| + EXPECT_TRUE(service.IsPingingEnabled(kLowReputationPinging)); |
| +} |
| + |
| +} // namespace safe_browsing |