| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/prefs/pref_value_map.h" | 9 #include "base/prefs/pref_value_map.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_Unspecified) { | 64 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_Unspecified) { |
| 65 // Don't specify a value for the policy. | 65 // Don't specify a value for the policy. |
| 66 // Checking should succeed with no errors. | 66 // Checking should succeed with no errors. |
| 67 EXPECT_TRUE(CheckPolicySettings()); | 67 EXPECT_TRUE(CheckPolicySettings()); |
| 68 EXPECT_EQ(0U, errors().size()); | 68 EXPECT_EQ(0U, errors().size()); |
| 69 } | 69 } |
| 70 | 70 |
| 71 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_UnknownValue) { | 71 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_UnknownValue) { |
| 72 // Specify an unknown value for the policy. | 72 // Specify an unknown value for the policy. |
| 73 int impossible_value = SessionStartupPref::kPrefValueHomePage + | 73 int impossible_value = SessionStartupPref::kPrefValueLast + |
| 74 SessionStartupPref::kPrefValueLast + | |
| 75 SessionStartupPref::kPrefValueURLs + | 74 SessionStartupPref::kPrefValueURLs + |
| 76 SessionStartupPref::kPrefValueNewTab; | 75 SessionStartupPref::kPrefValueNewTab; |
| 77 SetPolicyValue(key::kRestoreOnStartup, | 76 SetPolicyValue(key::kRestoreOnStartup, |
| 78 new base::FundamentalValue(impossible_value)); | 77 new base::FundamentalValue(impossible_value)); |
| 79 // Checking should succeed but add an error to the error map. | 78 // Checking should succeed but add an error to the error map. |
| 80 EXPECT_TRUE(CheckPolicySettings()); | 79 EXPECT_TRUE(CheckPolicySettings()); |
| 81 EXPECT_EQ(1U, errors().size()); | 80 EXPECT_EQ(1U, errors().size()); |
| 82 EXPECT_EQ(l10n_util::GetStringFUTF16( | 81 EXPECT_EQ(l10n_util::GetStringFUTF16( |
| 83 IDS_POLICY_OUT_OF_RANGE_ERROR, | 82 IDS_POLICY_OUT_OF_RANGE_ERROR, |
| 84 base::ASCIIToUTF16(base::IntToString(impossible_value))), | 83 base::ASCIIToUTF16(base::IntToString(impossible_value))), |
| 85 errors().begin()->second); | 84 errors().begin()->second); |
| 86 } | 85 } |
| 87 | 86 |
| 88 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_HomePage) { | 87 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_HomePage) { |
| 89 // Specify the HomePage value. | 88 // Specify the HomePage value. |
| 90 SetPolicyValue( | 89 SetPolicyValue( |
| 91 key::kRestoreOnStartup, | 90 key::kRestoreOnStartup, |
| 92 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | 91 new base::FundamentalValue(0)); // kPrefValueHomePage, deprecated. |
| 93 // Checking should succeed but add an error to the error map. | 92 // Checking should succeed but add an error to the error map. |
| 94 EXPECT_TRUE(CheckPolicySettings()); | 93 EXPECT_TRUE(CheckPolicySettings()); |
| 95 EXPECT_EQ(1U, errors().size()); | 94 EXPECT_EQ(1U, errors().size()); |
| 96 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_POLICY_VALUE_DEPRECATED), | 95 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_POLICY_VALUE_DEPRECATED), |
| 97 errors().begin()->second); | 96 errors().begin()->second); |
| 98 } | 97 } |
| 99 | 98 |
| 100 TEST_F(RestoreOnStartupPolicyHandlerTest, | 99 TEST_F(RestoreOnStartupPolicyHandlerTest, |
| 101 CheckPolicySettings_RestoreLastSession_SessionCookies) { | 100 CheckPolicySettings_RestoreLastSession_SessionCookies) { |
| 102 // Specify the Last value and the Session-Only Cookies value. | 101 // Specify the Last value and the Session-Only Cookies value. |
| 103 SetPolicyValue( | 102 SetPolicyValue( |
| 104 key::kRestoreOnStartup, | 103 key::kRestoreOnStartup, |
| 105 new base::FundamentalValue(SessionStartupPref::kPrefValueLast)); | 104 new base::FundamentalValue(SessionStartupPref::kPrefValueLast)); |
| 106 scoped_ptr<base::ListValue> urls(new base::ListValue); | 105 scoped_ptr<base::ListValue> urls(new base::ListValue); |
| 107 urls->AppendString("http://foo.com"); | 106 urls->AppendString("http://foo.com"); |
| 108 SetPolicyValue(key::kCookiesSessionOnlyForUrls, urls.release()); | 107 SetPolicyValue(key::kCookiesSessionOnlyForUrls, urls.release()); |
| 109 // Checking should succeed but add an error to the error map. | 108 // Checking should succeed but add an error to the error map. |
| 110 EXPECT_TRUE(CheckPolicySettings()); | 109 EXPECT_TRUE(CheckPolicySettings()); |
| 111 EXPECT_EQ(1U, errors().size()); | 110 EXPECT_EQ(1U, errors().size()); |
| 112 EXPECT_TRUE(key::kCookiesSessionOnlyForUrls == errors().begin()->first); | 111 EXPECT_TRUE(key::kCookiesSessionOnlyForUrls == errors().begin()->first); |
| 113 EXPECT_EQ(l10n_util::GetStringFUTF16( | 112 EXPECT_EQ(l10n_util::GetStringFUTF16( |
| 114 IDS_POLICY_OVERRIDDEN, | 113 IDS_POLICY_OVERRIDDEN, |
| 115 base::ASCIIToUTF16(key::kRestoreOnStartup)), | 114 base::ASCIIToUTF16(key::kRestoreOnStartup)), |
| 116 errors().begin()->second); | 115 errors().begin()->second); |
| 117 } | 116 } |
| 118 | 117 |
| 118 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_NotHomePage) { |
| 119 // Specify anything except the HomePage value. |
| 120 int not_home_page = 1; // kPrefValueHomePage + 1, deprecated. |
| 121 SetPolicyValue(key::kRestoreOnStartup, |
| 122 new base::FundamentalValue(not_home_page)); |
| 123 ApplyPolicySettings(); |
| 124 // The resulting prefs should have the value we specified. |
| 125 int result; |
| 126 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); |
| 127 EXPECT_EQ(not_home_page, result); |
| 128 } |
| 129 |
| 119 TEST_F(RestoreOnStartupPolicyHandlerTest, | 130 TEST_F(RestoreOnStartupPolicyHandlerTest, |
| 120 CheckPolicySettings_RestoreLastSession_ClearDataOnExit) { | 131 CheckPolicySettings_RestoreLastSession_ClearDataOnExit) { |
| 121 // Specify the Last value and the Clear-Data-On-Exit value. | 132 // Specify the Last value and the Clear-Data-On-Exit value. |
| 122 SetPolicyValue( | 133 SetPolicyValue( |
| 123 key::kRestoreOnStartup, | 134 key::kRestoreOnStartup, |
| 124 new base::FundamentalValue(SessionStartupPref::kPrefValueLast)); | 135 new base::FundamentalValue(SessionStartupPref::kPrefValueLast)); |
| 125 SetPolicyValue(key::kClearSiteDataOnExit, new base::FundamentalValue(true)); | 136 SetPolicyValue(key::kClearSiteDataOnExit, new base::FundamentalValue(true)); |
| 126 // Checking should succeed but add an error to the error map. | 137 // Checking should succeed but add an error to the error map. |
| 127 EXPECT_TRUE(CheckPolicySettings()); | 138 EXPECT_TRUE(CheckPolicySettings()); |
| 128 EXPECT_EQ(1U, errors().size()); | 139 EXPECT_EQ(1U, errors().size()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 EXPECT_TRUE(prefs().begin() == prefs().end()); | 182 EXPECT_TRUE(prefs().begin() == prefs().end()); |
| 172 } | 183 } |
| 173 | 184 |
| 174 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_WrongType) { | 185 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_WrongType) { |
| 175 // Handler expects an int; pass it a bool. | 186 // Handler expects an int; pass it a bool. |
| 176 SetPolicyValue(key::kRestoreOnStartup, new base::FundamentalValue(false)); | 187 SetPolicyValue(key::kRestoreOnStartup, new base::FundamentalValue(false)); |
| 177 // The resulting prefs should be empty. | 188 // The resulting prefs should be empty. |
| 178 EXPECT_TRUE(prefs().begin() == prefs().end()); | 189 EXPECT_TRUE(prefs().begin() == prefs().end()); |
| 179 } | 190 } |
| 180 | 191 |
| 181 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_NotHomePage) { | |
| 182 // Specify anything except the HomePage value. | |
| 183 int not_home_page = SessionStartupPref::kPrefValueHomePage + 1; | |
| 184 SetPolicyValue(key::kRestoreOnStartup, | |
| 185 new base::FundamentalValue(not_home_page)); | |
| 186 ApplyPolicySettings(); | |
| 187 // The resulting prefs should have the value we specified. | |
| 188 int result; | |
| 189 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); | |
| 190 EXPECT_EQ(not_home_page, result); | |
| 191 } | |
| 192 | |
| 193 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
| 194 ApplyPolicySettings_HomePage_NoHomePageValue) { | |
| 195 // Specify the HomePage value but no HomePageIsNewTabPage value. | |
| 196 SetPolicyValue( | |
| 197 key::kRestoreOnStartup, | |
| 198 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
| 199 ApplyPolicySettings(); | |
| 200 // The resulting prefs should be empty. | |
| 201 EXPECT_TRUE(prefs().begin() == prefs().end()); | |
| 202 } | |
| 203 | |
| 204 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
| 205 ApplyPolicySettings_HomePage_HomePageValueIsWrongType) { | |
| 206 // Specify the HomePage value and an integer for the home page value. | |
| 207 SetPolicyValue( | |
| 208 key::kRestoreOnStartup, | |
| 209 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
| 210 SetPolicyValue( | |
| 211 key::kHomepageIsNewTabPage, | |
| 212 new base::FundamentalValue(314159)); | |
| 213 ApplyPolicySettings(); | |
| 214 // The resulting prefs should be empty. | |
| 215 EXPECT_TRUE(prefs().begin() == prefs().end()); | |
| 216 } | |
| 217 | |
| 218 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
| 219 ApplyPolicySettings_HomePage_HomePageIsNewTabPage) { | |
| 220 // Specify the HomePage value and the home page as the new tab page. | |
| 221 SetPolicyValue( | |
| 222 key::kRestoreOnStartup, | |
| 223 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
| 224 SetPolicyValue( | |
| 225 key::kHomepageIsNewTabPage, | |
| 226 new base::FundamentalValue(true)); | |
| 227 ApplyPolicySettings(); | |
| 228 // The resulting prefs should have the restore value as NTP. | |
| 229 int result; | |
| 230 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); | |
| 231 int expected = SessionStartupPref::kPrefValueNewTab; | |
| 232 EXPECT_EQ(expected, result); | |
| 233 } | |
| 234 | |
| 235 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
| 236 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage_NotDefined) { | |
| 237 // Specify the HomePage value but don't specify the home page to use. | |
| 238 SetPolicyValue( | |
| 239 key::kRestoreOnStartup, | |
| 240 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
| 241 SetPolicyValue( | |
| 242 key::kHomepageIsNewTabPage, | |
| 243 new base::FundamentalValue(false)); | |
| 244 ApplyPolicySettings(); | |
| 245 // The resulting prefs should be empty. | |
| 246 EXPECT_TRUE(prefs().begin() == prefs().end()); | |
| 247 } | |
| 248 | |
| 249 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
| 250 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage_WrongType) { | |
| 251 // Specify the HomePage value but specify a boolean as the home page. | |
| 252 SetPolicyValue( | |
| 253 key::kRestoreOnStartup, | |
| 254 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
| 255 SetPolicyValue( | |
| 256 key::kHomepageIsNewTabPage, | |
| 257 new base::FundamentalValue(false)); | |
| 258 SetPolicyValue( | |
| 259 key::kHomepageLocation, | |
| 260 new base::FundamentalValue(false)); | |
| 261 ApplyPolicySettings(); | |
| 262 // The resulting prefs should be empty. | |
| 263 EXPECT_TRUE(prefs().begin() == prefs().end()); | |
| 264 } | |
| 265 | |
| 266 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
| 267 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage) { | |
| 268 SetPolicyValue( | |
| 269 key::kRestoreOnStartup, | |
| 270 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
| 271 SetPolicyValue(key::kHomepageIsNewTabPage, new base::FundamentalValue(false)); | |
| 272 SetPolicyValue(key::kHomepageLocation, | |
| 273 new base::StringValue("http://foo.com")); | |
| 274 ApplyPolicySettings(); | |
| 275 | |
| 276 // The resulting prefs should have have URLs specified for startup. | |
| 277 int result; | |
| 278 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); | |
| 279 int expected = SessionStartupPref::kPrefValueURLs; | |
| 280 EXPECT_EQ(expected, result); | |
| 281 | |
| 282 // The resulting prefs should have the URL we specified as the home page. | |
| 283 base::Value* url_result; | |
| 284 EXPECT_TRUE(prefs().GetValue(prefs::kURLsToRestoreOnStartup, &url_result)); | |
| 285 base::ListValue* url_list_result; | |
| 286 EXPECT_TRUE(url_result->GetAsList(&url_list_result)); | |
| 287 EXPECT_EQ(1U, url_list_result->GetSize()); | |
| 288 std::string expected_url; | |
| 289 EXPECT_TRUE(url_list_result->GetString(0, &expected_url)); | |
| 290 EXPECT_EQ(std::string("http://foo.com"), expected_url); | |
| 291 } | |
| 292 | |
| 293 } // namespace policy | 192 } // namespace policy |
| OLD | NEW |