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 "components/policy/core/common/policy_types.h"
11 #include "policy/policy_constants.h"
15 // Tests Incognito mode availability preference setting.
16 class IncognitoModePolicyHandlerTest
17 : public ConfigurationPolicyPrefStoreTest
{
19 void SetUp() override
{
20 handler_list_
.AddHandler(make_scoped_ptr
<ConfigurationPolicyHandler
>(
21 new IncognitoModePolicyHandler
));
24 static const int kIncognitoModeAvailabilityNotSet
= -1;
26 enum ObsoleteIncognitoEnabledValue
{
27 INCOGNITO_ENABLED_UNKNOWN
,
28 INCOGNITO_ENABLED_TRUE
,
29 INCOGNITO_ENABLED_FALSE
32 void SetPolicies(ObsoleteIncognitoEnabledValue incognito_enabled
,
35 if (incognito_enabled
!= INCOGNITO_ENABLED_UNKNOWN
) {
36 policy
.Set(key::kIncognitoEnabled
,
37 POLICY_LEVEL_MANDATORY
,
40 new base::FundamentalValue(incognito_enabled
==
41 INCOGNITO_ENABLED_TRUE
),
44 if (availability
>= 0) {
45 policy
.Set(key::kIncognitoModeAvailability
,
46 POLICY_LEVEL_MANDATORY
,
49 new base::FundamentalValue(availability
),
52 UpdateProviderPolicy(policy
);
55 void VerifyValues(IncognitoModePrefs::Availability availability
) {
56 const base::Value
* value
= NULL
;
57 EXPECT_TRUE(store_
->GetValue(prefs::kIncognitoModeAvailability
, &value
));
58 EXPECT_TRUE(base::FundamentalValue(availability
).Equals(value
));
62 // The following testcases verify that if the obsolete IncognitoEnabled
63 // policy is not set, the IncognitoModeAvailability values should be copied
64 // from IncognitoModeAvailability policy to pref "as is".
65 TEST_F(IncognitoModePolicyHandlerTest
,
66 NoObsoletePolicyAndIncognitoEnabled
) {
67 SetPolicies(INCOGNITO_ENABLED_UNKNOWN
, IncognitoModePrefs::ENABLED
);
68 VerifyValues(IncognitoModePrefs::ENABLED
);
71 TEST_F(IncognitoModePolicyHandlerTest
,
72 NoObsoletePolicyAndIncognitoDisabled
) {
73 SetPolicies(INCOGNITO_ENABLED_UNKNOWN
, IncognitoModePrefs::DISABLED
);
74 VerifyValues(IncognitoModePrefs::DISABLED
);
77 TEST_F(IncognitoModePolicyHandlerTest
,
78 NoObsoletePolicyAndIncognitoForced
) {
79 SetPolicies(INCOGNITO_ENABLED_UNKNOWN
, IncognitoModePrefs::FORCED
);
80 VerifyValues(IncognitoModePrefs::FORCED
);
83 TEST_F(IncognitoModePolicyHandlerTest
,
84 NoObsoletePolicyAndNoIncognitoAvailability
) {
85 SetPolicies(INCOGNITO_ENABLED_UNKNOWN
, kIncognitoModeAvailabilityNotSet
);
86 const base::Value
* value
= NULL
;
87 EXPECT_FALSE(store_
->GetValue(prefs::kIncognitoModeAvailability
, &value
));
90 // Checks that if the obsolete IncognitoEnabled policy is set, if sets
91 // the IncognitoModeAvailability preference only in case
92 // the IncognitoModeAvailability policy is not specified.
93 TEST_F(IncognitoModePolicyHandlerTest
,
94 ObsoletePolicyDoesNotAffectAvailabilityEnabled
) {
95 SetPolicies(INCOGNITO_ENABLED_FALSE
, IncognitoModePrefs::ENABLED
);
96 VerifyValues(IncognitoModePrefs::ENABLED
);
99 TEST_F(IncognitoModePolicyHandlerTest
,
100 ObsoletePolicyDoesNotAffectAvailabilityForced
) {
101 SetPolicies(INCOGNITO_ENABLED_TRUE
, IncognitoModePrefs::FORCED
);
102 VerifyValues(IncognitoModePrefs::FORCED
);
105 TEST_F(IncognitoModePolicyHandlerTest
,
106 ObsoletePolicySetsPreferenceToEnabled
) {
107 SetPolicies(INCOGNITO_ENABLED_TRUE
, kIncognitoModeAvailabilityNotSet
);
108 VerifyValues(IncognitoModePrefs::ENABLED
);
111 TEST_F(IncognitoModePolicyHandlerTest
,
112 ObsoletePolicySetsPreferenceToDisabled
) {
113 SetPolicies(INCOGNITO_ENABLED_FALSE
, kIncognitoModeAvailabilityNotSet
);
114 VerifyValues(IncognitoModePrefs::DISABLED
);
117 } // namespace policy