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. 4 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" 11 12 namespace policy { 13 14 // Tests Incognito mode availability preference setting. 15 class IncognitoModePolicyHandlerTest 16 : public ConfigurationPolicyPrefStoreTest { 17 public: 18 virtual void SetUp() OVERRIDE { 19 handler_list_.AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( 20 new IncognitoModePolicyHandler)); 21 } 22 protected: 23 static const int kIncognitoModeAvailabilityNotSet = -1; 24 25 enum ObsoleteIncognitoEnabledValue { 26 INCOGNITO_ENABLED_UNKNOWN, 27 INCOGNITO_ENABLED_TRUE, 28 INCOGNITO_ENABLED_FALSE 29 }; 30 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); 41 } 42 if (availability >= 0) { 43 policy.Set(key::kIncognitoModeAvailability, 44 POLICY_LEVEL_MANDATORY, 45 POLICY_SCOPE_USER, 46 new base::FundamentalValue(availability), 47 NULL); 48 } 49 UpdateProviderPolicy(policy); 50 } 51 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)); 56 } 57 }; 58 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); 66 } 67 68 TEST_F(IncognitoModePolicyHandlerTest, 69 NoObsoletePolicyAndIncognitoDisabled) { 70 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, IncognitoModePrefs::DISABLED); 71 VerifyValues(IncognitoModePrefs::DISABLED); 72 } 73 74 TEST_F(IncognitoModePolicyHandlerTest, 75 NoObsoletePolicyAndIncognitoForced) { 76 SetPolicies(INCOGNITO_ENABLED_UNKNOWN, IncognitoModePrefs::FORCED); 77 VerifyValues(IncognitoModePrefs::FORCED); 78 } 79 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)); 85 } 86 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); 94 } 95 96 TEST_F(IncognitoModePolicyHandlerTest, 97 ObsoletePolicyDoesNotAffectAvailabilityForced) { 98 SetPolicies(INCOGNITO_ENABLED_TRUE, IncognitoModePrefs::FORCED); 99 VerifyValues(IncognitoModePrefs::FORCED); 100 } 101 102 TEST_F(IncognitoModePolicyHandlerTest, 103 ObsoletePolicySetsPreferenceToEnabled) { 104 SetPolicies(INCOGNITO_ENABLED_TRUE, kIncognitoModeAvailabilityNotSet); 105 VerifyValues(IncognitoModePrefs::ENABLED); 106 } 107 108 TEST_F(IncognitoModePolicyHandlerTest, 109 ObsoletePolicySetsPreferenceToDisabled) { 110 SetPolicies(INCOGNITO_ENABLED_FALSE, kIncognitoModeAvailabilityNotSet); 111 VerifyValues(IncognitoModePrefs::DISABLED); 112 } 113 114 } // namespace policy 115