| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sessions/restore_on_startup_policy_handler.h" | 5 #include "chrome/browser/sessions/restore_on_startup_policy_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 private: | 46 private: |
| 47 PolicyMap policies_; | 47 PolicyMap policies_; |
| 48 PolicyErrorMap errors_; | 48 PolicyErrorMap errors_; |
| 49 PrefValueMap prefs_; | 49 PrefValueMap prefs_; |
| 50 RestoreOnStartupPolicyHandler handler_; | 50 RestoreOnStartupPolicyHandler handler_; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_FailsTypeCheck) { | 53 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_FailsTypeCheck) { |
| 54 // Handler expects an int; pass it a bool. | 54 // Handler expects an int; pass it a bool. |
| 55 SetPolicyValue(key::kRestoreOnStartup, | 55 SetPolicyValue(key::kRestoreOnStartup, base::MakeUnique<base::Value>(false)); |
| 56 base::MakeUnique<base::FundamentalValue>(false)); | |
| 57 // Checking should fail and add an error to the error map. | 56 // Checking should fail and add an error to the error map. |
| 58 EXPECT_FALSE(CheckPolicySettings()); | 57 EXPECT_FALSE(CheckPolicySettings()); |
| 59 ASSERT_EQ(1U, errors().size()); | 58 ASSERT_EQ(1U, errors().size()); |
| 60 EXPECT_EQ( | 59 EXPECT_EQ( |
| 61 l10n_util::GetStringFUTF16(IDS_POLICY_TYPE_ERROR, | 60 l10n_util::GetStringFUTF16(IDS_POLICY_TYPE_ERROR, |
| 62 base::ASCIIToUTF16(base::Value::GetTypeName( | 61 base::ASCIIToUTF16(base::Value::GetTypeName( |
| 63 base::Value::Type::INTEGER))), | 62 base::Value::Type::INTEGER))), |
| 64 errors().begin()->second); | 63 errors().begin()->second); |
| 65 } | 64 } |
| 66 | 65 |
| 67 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_Unspecified) { | 66 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_Unspecified) { |
| 68 // Don't specify a value for the policy. | 67 // Don't specify a value for the policy. |
| 69 // Checking should succeed with no errors. | 68 // Checking should succeed with no errors. |
| 70 EXPECT_TRUE(CheckPolicySettings()); | 69 EXPECT_TRUE(CheckPolicySettings()); |
| 71 EXPECT_TRUE(errors().empty()); | 70 EXPECT_TRUE(errors().empty()); |
| 72 } | 71 } |
| 73 | 72 |
| 74 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_UnknownValue) { | 73 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_UnknownValue) { |
| 75 // Specify an unknown value for the policy. | 74 // Specify an unknown value for the policy. |
| 76 int impossible_value = SessionStartupPref::kPrefValueLast + | 75 int impossible_value = SessionStartupPref::kPrefValueLast + |
| 77 SessionStartupPref::kPrefValueURLs + | 76 SessionStartupPref::kPrefValueURLs + |
| 78 SessionStartupPref::kPrefValueNewTab; | 77 SessionStartupPref::kPrefValueNewTab; |
| 79 SetPolicyValue(key::kRestoreOnStartup, | 78 SetPolicyValue(key::kRestoreOnStartup, |
| 80 base::MakeUnique<base::FundamentalValue>(impossible_value)); | 79 base::MakeUnique<base::Value>(impossible_value)); |
| 81 // Checking should succeed but add an error to the error map. | 80 // Checking should succeed but add an error to the error map. |
| 82 EXPECT_TRUE(CheckPolicySettings()); | 81 EXPECT_TRUE(CheckPolicySettings()); |
| 83 ASSERT_EQ(1U, errors().size()); | 82 ASSERT_EQ(1U, errors().size()); |
| 84 EXPECT_EQ(l10n_util::GetStringFUTF16( | 83 EXPECT_EQ(l10n_util::GetStringFUTF16( |
| 85 IDS_POLICY_OUT_OF_RANGE_ERROR, | 84 IDS_POLICY_OUT_OF_RANGE_ERROR, |
| 86 base::ASCIIToUTF16(base::IntToString(impossible_value))), | 85 base::ASCIIToUTF16(base::IntToString(impossible_value))), |
| 87 errors().begin()->second); | 86 errors().begin()->second); |
| 88 } | 87 } |
| 89 | 88 |
| 90 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_HomePage) { | 89 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_HomePage) { |
| 91 // Specify the HomePage value. | 90 // Specify the HomePage value. |
| 92 SetPolicyValue(key::kRestoreOnStartup, | 91 SetPolicyValue(key::kRestoreOnStartup, |
| 93 // kPrefValueHomePage, deprecated. | 92 // kPrefValueHomePage, deprecated. |
| 94 base::MakeUnique<base::FundamentalValue>(0)); | 93 base::MakeUnique<base::Value>(0)); |
| 95 // Checking should succeed but add an error to the error map. | 94 // Checking should succeed but add an error to the error map. |
| 96 EXPECT_TRUE(CheckPolicySettings()); | 95 EXPECT_TRUE(CheckPolicySettings()); |
| 97 ASSERT_EQ(1U, errors().size()); | 96 ASSERT_EQ(1U, errors().size()); |
| 98 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_POLICY_VALUE_DEPRECATED), | 97 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_POLICY_VALUE_DEPRECATED), |
| 99 errors().begin()->second); | 98 errors().begin()->second); |
| 100 } | 99 } |
| 101 | 100 |
| 102 TEST_F(RestoreOnStartupPolicyHandlerTest, | 101 TEST_F(RestoreOnStartupPolicyHandlerTest, |
| 103 CheckPolicySettings_RestoreLastSession_SessionCookies) { | 102 CheckPolicySettings_RestoreLastSession_SessionCookies) { |
| 104 // Specify the Last value and the Session-Only Cookies value. | 103 // Specify the Last value and the Session-Only Cookies value. |
| 105 SetPolicyValue(key::kRestoreOnStartup, | 104 SetPolicyValue( |
| 106 base::WrapUnique(new base::FundamentalValue( | 105 key::kRestoreOnStartup, |
| 107 SessionStartupPref::kPrefValueLast))); | 106 base::WrapUnique(new base::Value(SessionStartupPref::kPrefValueLast))); |
| 108 std::unique_ptr<base::ListValue> urls(new base::ListValue); | 107 std::unique_ptr<base::ListValue> urls(new base::ListValue); |
| 109 urls->AppendString("http://foo.com"); | 108 urls->AppendString("http://foo.com"); |
| 110 SetPolicyValue(key::kCookiesSessionOnlyForUrls, std::move(urls)); | 109 SetPolicyValue(key::kCookiesSessionOnlyForUrls, std::move(urls)); |
| 111 // Checking should succeed but add an error to the error map. | 110 // Checking should succeed but add an error to the error map. |
| 112 EXPECT_TRUE(CheckPolicySettings()); | 111 EXPECT_TRUE(CheckPolicySettings()); |
| 113 ASSERT_EQ(1U, errors().size()); | 112 ASSERT_EQ(1U, errors().size()); |
| 114 EXPECT_EQ(key::kCookiesSessionOnlyForUrls, errors().begin()->first); | 113 EXPECT_EQ(key::kCookiesSessionOnlyForUrls, errors().begin()->first); |
| 115 EXPECT_EQ(l10n_util::GetStringFUTF16( | 114 EXPECT_EQ(l10n_util::GetStringFUTF16( |
| 116 IDS_POLICY_OVERRIDDEN, | 115 IDS_POLICY_OVERRIDDEN, |
| 117 base::ASCIIToUTF16(key::kRestoreOnStartup)), | 116 base::ASCIIToUTF16(key::kRestoreOnStartup)), |
| 118 errors().begin()->second); | 117 errors().begin()->second); |
| 119 } | 118 } |
| 120 | 119 |
| 121 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_NotHomePage) { | 120 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_NotHomePage) { |
| 122 // Specify anything except the HomePage value. | 121 // Specify anything except the HomePage value. |
| 123 int not_home_page = 1; // kPrefValueHomePage + 1, deprecated. | 122 int not_home_page = 1; // kPrefValueHomePage + 1, deprecated. |
| 124 SetPolicyValue(key::kRestoreOnStartup, | 123 SetPolicyValue(key::kRestoreOnStartup, |
| 125 base::MakeUnique<base::FundamentalValue>(not_home_page)); | 124 base::MakeUnique<base::Value>(not_home_page)); |
| 126 ApplyPolicySettings(); | 125 ApplyPolicySettings(); |
| 127 // The resulting prefs should have the value we specified. | 126 // The resulting prefs should have the value we specified. |
| 128 int result; | 127 int result; |
| 129 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); | 128 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); |
| 130 EXPECT_EQ(not_home_page, result); | 129 EXPECT_EQ(not_home_page, result); |
| 131 } | 130 } |
| 132 | 131 |
| 133 TEST_F(RestoreOnStartupPolicyHandlerTest, | 132 TEST_F(RestoreOnStartupPolicyHandlerTest, |
| 134 CheckPolicySettings_RestoreLastSession_ClearDataOnExit) { | 133 CheckPolicySettings_RestoreLastSession_ClearDataOnExit) { |
| 135 // Specify the Last value and the Clear-Data-On-Exit value. | 134 // Specify the Last value and the Clear-Data-On-Exit value. |
| 136 SetPolicyValue(key::kRestoreOnStartup, | 135 SetPolicyValue( |
| 137 base::WrapUnique(new base::FundamentalValue( | 136 key::kRestoreOnStartup, |
| 138 SessionStartupPref::kPrefValueLast))); | 137 base::WrapUnique(new base::Value(SessionStartupPref::kPrefValueLast))); |
| 139 SetPolicyValue(key::kClearSiteDataOnExit, | 138 SetPolicyValue(key::kClearSiteDataOnExit, |
| 140 base::MakeUnique<base::FundamentalValue>(true)); | 139 base::MakeUnique<base::Value>(true)); |
| 141 // Checking should succeed but add an error to the error map. | 140 // Checking should succeed but add an error to the error map. |
| 142 EXPECT_TRUE(CheckPolicySettings()); | 141 EXPECT_TRUE(CheckPolicySettings()); |
| 143 ASSERT_EQ(1U, errors().size()); | 142 ASSERT_EQ(1U, errors().size()); |
| 144 EXPECT_EQ(key::kClearSiteDataOnExit, errors().begin()->first); | 143 EXPECT_EQ(key::kClearSiteDataOnExit, errors().begin()->first); |
| 145 EXPECT_EQ(l10n_util::GetStringFUTF16( | 144 EXPECT_EQ(l10n_util::GetStringFUTF16( |
| 146 IDS_POLICY_OVERRIDDEN, | 145 IDS_POLICY_OVERRIDDEN, |
| 147 base::ASCIIToUTF16(key::kRestoreOnStartup)), | 146 base::ASCIIToUTF16(key::kRestoreOnStartup)), |
| 148 errors().begin()->second); | 147 errors().begin()->second); |
| 149 } | 148 } |
| 150 | 149 |
| 151 TEST_F(RestoreOnStartupPolicyHandlerTest, | 150 TEST_F(RestoreOnStartupPolicyHandlerTest, |
| 152 CheckPolicySettings_RestoreLastSession) { | 151 CheckPolicySettings_RestoreLastSession) { |
| 153 // Specify the Last value without the conflicts. | 152 // Specify the Last value without the conflicts. |
| 154 SetPolicyValue(key::kRestoreOnStartup, | 153 SetPolicyValue( |
| 155 base::WrapUnique(new base::FundamentalValue( | 154 key::kRestoreOnStartup, |
| 156 SessionStartupPref::kPrefValueLast))); | 155 base::WrapUnique(new base::Value(SessionStartupPref::kPrefValueLast))); |
| 157 // Checking should succeed with no errors. | 156 // Checking should succeed with no errors. |
| 158 EXPECT_TRUE(CheckPolicySettings()); | 157 EXPECT_TRUE(CheckPolicySettings()); |
| 159 EXPECT_TRUE(errors().empty()); | 158 EXPECT_TRUE(errors().empty()); |
| 160 } | 159 } |
| 161 | 160 |
| 162 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_URLs) { | 161 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_URLs) { |
| 163 // Specify the URLs value. | 162 // Specify the URLs value. |
| 164 SetPolicyValue(key::kRestoreOnStartup, | 163 SetPolicyValue( |
| 165 base::WrapUnique(new base::FundamentalValue( | 164 key::kRestoreOnStartup, |
| 166 SessionStartupPref::kPrefValueURLs))); | 165 base::WrapUnique(new base::Value(SessionStartupPref::kPrefValueURLs))); |
| 167 // Checking should succeed with no errors. | 166 // Checking should succeed with no errors. |
| 168 EXPECT_TRUE(CheckPolicySettings()); | 167 EXPECT_TRUE(CheckPolicySettings()); |
| 169 EXPECT_TRUE(errors().empty()); | 168 EXPECT_TRUE(errors().empty()); |
| 170 } | 169 } |
| 171 | 170 |
| 172 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_NewTab) { | 171 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_NewTab) { |
| 173 // Specify the NewTab value. | 172 // Specify the NewTab value. |
| 174 SetPolicyValue(key::kRestoreOnStartup, | 173 SetPolicyValue( |
| 175 base::WrapUnique(new base::FundamentalValue( | 174 key::kRestoreOnStartup, |
| 176 SessionStartupPref::kPrefValueNewTab))); | 175 base::WrapUnique(new base::Value(SessionStartupPref::kPrefValueNewTab))); |
| 177 // Checking should succeed with no errors. | 176 // Checking should succeed with no errors. |
| 178 EXPECT_TRUE(CheckPolicySettings()); | 177 EXPECT_TRUE(CheckPolicySettings()); |
| 179 EXPECT_TRUE(errors().empty()); | 178 EXPECT_TRUE(errors().empty()); |
| 180 } | 179 } |
| 181 | 180 |
| 182 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_NoValue) { | 181 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_NoValue) { |
| 183 // Don't specify a value for the policy. | 182 // Don't specify a value for the policy. |
| 184 ApplyPolicySettings(); | 183 ApplyPolicySettings(); |
| 185 // The resulting prefs should be empty. | 184 // The resulting prefs should be empty. |
| 186 EXPECT_TRUE(prefs().empty()); | 185 EXPECT_TRUE(prefs().empty()); |
| 187 } | 186 } |
| 188 | 187 |
| 189 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_WrongType) { | 188 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_WrongType) { |
| 190 // Handler expects an int; pass it a bool. | 189 // Handler expects an int; pass it a bool. |
| 191 SetPolicyValue(key::kRestoreOnStartup, | 190 SetPolicyValue(key::kRestoreOnStartup, base::MakeUnique<base::Value>(false)); |
| 192 base::MakeUnique<base::FundamentalValue>(false)); | |
| 193 // The resulting prefs should be empty. | 191 // The resulting prefs should be empty. |
| 194 EXPECT_TRUE(prefs().empty()); | 192 EXPECT_TRUE(prefs().empty()); |
| 195 } | 193 } |
| 196 | 194 |
| 197 } // namespace policy | 195 } // namespace policy |
| OLD | NEW |