Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / profiles / incognito_mode_policy_handler_unittest.cc
blob5fac6d6fd25326e2bbdbd90b8f6fc3c3009fb474
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/prefs/incognito_mode_prefs.h"
6 #include "chrome/browser/profiles/incognito_mode_policy_handler.h"
7 #include "chrome/common/pref_names.h"
8 #include "components/policy/core/browser/configuration_policy_pref_store.h"
9 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
10 #include "policy/policy_constants.h"
12 namespace policy {
14 // Tests Incognito mode availability preference setting.
15 class IncognitoModePolicyHandlerTest
16 : public ConfigurationPolicyPrefStoreTest {
17 public:
18 void SetUp() override {
19 handler_list_.AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>(
20 new IncognitoModePolicyHandler));
22 protected:
23 static const int kIncognitoModeAvailabilityNotSet = -1;
25 enum ObsoleteIncognitoEnabledValue {
26 INCOGNITO_ENABLED_UNKNOWN,
27 INCOGNITO_ENABLED_TRUE,
28 INCOGNITO_ENABLED_FALSE
31 void SetPolicies(ObsoleteIncognitoEnabledValue incognito_enabled,
32 int availability) {
33 PolicyMap policy;
34 if (incognito_enabled != INCOGNITO_ENABLED_UNKNOWN) {
35 policy.Set(key::kIncognitoEnabled,
36 POLICY_LEVEL_MANDATORY,
37 POLICY_SCOPE_USER,
38 new base::FundamentalValue(incognito_enabled ==
39 INCOGNITO_ENABLED_TRUE),
40 NULL);
42 if (availability >= 0) {
43 policy.Set(key::kIncognitoModeAvailability,
44 POLICY_LEVEL_MANDATORY,
45 POLICY_SCOPE_USER,
46 new base::FundamentalValue(availability),
47 NULL);
49 UpdateProviderPolicy(policy);
52 void VerifyValues(IncognitoModePrefs::Availability availability) {
53 const base::Value* value = NULL;
54 EXPECT_TRUE(store_->GetValue(prefs::kIncognitoModeAvailability, &value));
55 EXPECT_TRUE(base::FundamentalValue(availability).Equals(value));
59 // The following testcases verify that if the obsolete IncognitoEnabled
60 // policy is not set, the IncognitoModeAvailability values should be copied
61 // from IncognitoModeAvailability policy to pref "as is".
62 TEST_F(IncognitoModePolicyHandlerTest,
63 NoObsoletePolicyAndIncognitoEnabled) {
64 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, IncognitoModePrefs::ENABLED);
65 VerifyValues(IncognitoModePrefs::ENABLED);
68 TEST_F(IncognitoModePolicyHandlerTest,
69 NoObsoletePolicyAndIncognitoDisabled) {
70 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, IncognitoModePrefs::DISABLED);
71 VerifyValues(IncognitoModePrefs::DISABLED);
74 TEST_F(IncognitoModePolicyHandlerTest,
75 NoObsoletePolicyAndIncognitoForced) {
76 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, IncognitoModePrefs::FORCED);
77 VerifyValues(IncognitoModePrefs::FORCED);
80 TEST_F(IncognitoModePolicyHandlerTest,
81 NoObsoletePolicyAndNoIncognitoAvailability) {
82 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, kIncognitoModeAvailabilityNotSet);
83 const base::Value* value = NULL;
84 EXPECT_FALSE(store_->GetValue(prefs::kIncognitoModeAvailability, &value));
87 // Checks that if the obsolete IncognitoEnabled policy is set, if sets
88 // the IncognitoModeAvailability preference only in case
89 // the IncognitoModeAvailability policy is not specified.
90 TEST_F(IncognitoModePolicyHandlerTest,
91 ObsoletePolicyDoesNotAffectAvailabilityEnabled) {
92 SetPolicies(INCOGNITO_ENABLED_FALSE, IncognitoModePrefs::ENABLED);
93 VerifyValues(IncognitoModePrefs::ENABLED);
96 TEST_F(IncognitoModePolicyHandlerTest,
97 ObsoletePolicyDoesNotAffectAvailabilityForced) {
98 SetPolicies(INCOGNITO_ENABLED_TRUE, IncognitoModePrefs::FORCED);
99 VerifyValues(IncognitoModePrefs::FORCED);
102 TEST_F(IncognitoModePolicyHandlerTest,
103 ObsoletePolicySetsPreferenceToEnabled) {
104 SetPolicies(INCOGNITO_ENABLED_TRUE, kIncognitoModeAvailabilityNotSet);
105 VerifyValues(IncognitoModePrefs::ENABLED);
108 TEST_F(IncognitoModePolicyHandlerTest,
109 ObsoletePolicySetsPreferenceToDisabled) {
110 SetPolicies(INCOGNITO_ENABLED_FALSE, kIncognitoModeAvailabilityNotSet);
111 VerifyValues(IncognitoModePrefs::DISABLED);
114 } // namespace policy