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" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/prefs/session_startup_pref.h" | 13 #include "chrome/browser/prefs/session_startup_pref.h" |
14 #include "chrome/browser/sessions/restore_on_startup_policy_handler.h" | 14 #include "chrome/browser/sessions/restore_on_startup_policy_handler.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
bartfab (slow)
2015/08/31 15:36:43
Nit: No longer used.
sdefresne
2015/09/02 11:30:19
Removed.
| |
16 #include "components/policy/core/browser/configuration_policy_handler.h" | 16 #include "components/policy/core/browser/configuration_policy_handler.h" |
17 #include "components/policy/core/browser/policy_error_map.h" | 17 #include "components/policy/core/browser/policy_error_map.h" |
18 #include "components/policy/core/common/policy_map.h" | 18 #include "components/policy/core/common/policy_map.h" |
19 #include "grit/components_strings.h" | 19 #include "grit/components_strings.h" |
20 #include "policy/policy_constants.h" | 20 #include "policy/policy_constants.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
23 | 23 |
24 namespace policy { | 24 namespace policy { |
25 | 25 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 | 61 |
62 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_Unspecified) { | 62 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_Unspecified) { |
63 // Don't specify a value for the policy. | 63 // Don't specify a value for the policy. |
64 // Checking should succeed with no errors. | 64 // Checking should succeed with no errors. |
65 EXPECT_TRUE(CheckPolicySettings()); | 65 EXPECT_TRUE(CheckPolicySettings()); |
66 EXPECT_EQ(0U, errors().size()); | 66 EXPECT_EQ(0U, errors().size()); |
67 } | 67 } |
68 | 68 |
69 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_UnknownValue) { | 69 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_UnknownValue) { |
70 // Specify an unknown value for the policy. | 70 // Specify an unknown value for the policy. |
71 int impossible_value = SessionStartupPref::kPrefValueHomePage + | 71 int impossible_value = SessionStartupPref::kPrefValueLast + |
72 SessionStartupPref::kPrefValueLast + | |
73 SessionStartupPref::kPrefValueURLs + | 72 SessionStartupPref::kPrefValueURLs + |
74 SessionStartupPref::kPrefValueNewTab; | 73 SessionStartupPref::kPrefValueNewTab; |
75 SetPolicyValue(key::kRestoreOnStartup, | 74 SetPolicyValue(key::kRestoreOnStartup, |
76 new base::FundamentalValue(impossible_value)); | 75 new base::FundamentalValue(impossible_value)); |
77 // Checking should succeed but add an error to the error map. | 76 // Checking should succeed but add an error to the error map. |
78 EXPECT_TRUE(CheckPolicySettings()); | 77 EXPECT_TRUE(CheckPolicySettings()); |
79 EXPECT_EQ(1U, errors().size()); | 78 EXPECT_EQ(1U, errors().size()); |
80 EXPECT_EQ(l10n_util::GetStringFUTF16( | 79 EXPECT_EQ(l10n_util::GetStringFUTF16( |
81 IDS_POLICY_OUT_OF_RANGE_ERROR, | 80 IDS_POLICY_OUT_OF_RANGE_ERROR, |
82 base::ASCIIToUTF16(base::IntToString(impossible_value))), | 81 base::ASCIIToUTF16(base::IntToString(impossible_value))), |
83 errors().begin()->second); | 82 errors().begin()->second); |
84 } | 83 } |
85 | 84 |
86 TEST_F(RestoreOnStartupPolicyHandlerTest, CheckPolicySettings_HomePage) { | |
87 // Specify the HomePage value. | |
88 SetPolicyValue( | |
89 key::kRestoreOnStartup, | |
90 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
91 // Checking should succeed but add an error to the error map. | |
92 EXPECT_TRUE(CheckPolicySettings()); | |
93 EXPECT_EQ(1U, errors().size()); | |
94 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_POLICY_VALUE_DEPRECATED), | |
95 errors().begin()->second); | |
96 } | |
97 | |
98 TEST_F(RestoreOnStartupPolicyHandlerTest, | 85 TEST_F(RestoreOnStartupPolicyHandlerTest, |
99 CheckPolicySettings_RestoreLastSession_SessionCookies) { | 86 CheckPolicySettings_RestoreLastSession_SessionCookies) { |
100 // Specify the Last value and the Session-Only Cookies value. | 87 // Specify the Last value and the Session-Only Cookies value. |
101 SetPolicyValue( | 88 SetPolicyValue( |
102 key::kRestoreOnStartup, | 89 key::kRestoreOnStartup, |
103 new base::FundamentalValue(SessionStartupPref::kPrefValueLast)); | 90 new base::FundamentalValue(SessionStartupPref::kPrefValueLast)); |
104 scoped_ptr<base::ListValue> urls(new base::ListValue); | 91 scoped_ptr<base::ListValue> urls(new base::ListValue); |
105 urls->AppendString("http://foo.com"); | 92 urls->AppendString("http://foo.com"); |
106 SetPolicyValue(key::kCookiesSessionOnlyForUrls, urls.release()); | 93 SetPolicyValue(key::kCookiesSessionOnlyForUrls, urls.release()); |
107 // Checking should succeed but add an error to the error map. | 94 // Checking should succeed but add an error to the error map. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 EXPECT_TRUE(prefs().begin() == prefs().end()); | 156 EXPECT_TRUE(prefs().begin() == prefs().end()); |
170 } | 157 } |
171 | 158 |
172 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_WrongType) { | 159 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_WrongType) { |
173 // Handler expects an int; pass it a bool. | 160 // Handler expects an int; pass it a bool. |
174 SetPolicyValue(key::kRestoreOnStartup, new base::FundamentalValue(false)); | 161 SetPolicyValue(key::kRestoreOnStartup, new base::FundamentalValue(false)); |
175 // The resulting prefs should be empty. | 162 // The resulting prefs should be empty. |
176 EXPECT_TRUE(prefs().begin() == prefs().end()); | 163 EXPECT_TRUE(prefs().begin() == prefs().end()); |
177 } | 164 } |
178 | 165 |
179 TEST_F(RestoreOnStartupPolicyHandlerTest, ApplyPolicySettings_NotHomePage) { | |
180 // Specify anything except the HomePage value. | |
181 int not_home_page = SessionStartupPref::kPrefValueHomePage + 1; | |
182 SetPolicyValue(key::kRestoreOnStartup, | |
183 new base::FundamentalValue(not_home_page)); | |
184 ApplyPolicySettings(); | |
185 // The resulting prefs should have the value we specified. | |
186 int result; | |
187 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); | |
188 EXPECT_EQ(not_home_page, result); | |
189 } | |
190 | |
191 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
192 ApplyPolicySettings_HomePage_NoHomePageValue) { | |
193 // Specify the HomePage value but no HomePageIsNewTabPage value. | |
194 SetPolicyValue( | |
195 key::kRestoreOnStartup, | |
196 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
197 ApplyPolicySettings(); | |
198 // The resulting prefs should be empty. | |
199 EXPECT_TRUE(prefs().begin() == prefs().end()); | |
200 } | |
201 | |
202 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
203 ApplyPolicySettings_HomePage_HomePageValueIsWrongType) { | |
204 // Specify the HomePage value and an integer for the home page value. | |
205 SetPolicyValue( | |
206 key::kRestoreOnStartup, | |
207 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
208 SetPolicyValue( | |
209 key::kHomepageIsNewTabPage, | |
210 new base::FundamentalValue(314159)); | |
211 ApplyPolicySettings(); | |
212 // The resulting prefs should be empty. | |
213 EXPECT_TRUE(prefs().begin() == prefs().end()); | |
214 } | |
215 | |
216 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
217 ApplyPolicySettings_HomePage_HomePageIsNewTabPage) { | |
218 // Specify the HomePage value and the home page as the new tab page. | |
219 SetPolicyValue( | |
220 key::kRestoreOnStartup, | |
221 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
222 SetPolicyValue( | |
223 key::kHomepageIsNewTabPage, | |
224 new base::FundamentalValue(true)); | |
225 ApplyPolicySettings(); | |
226 // The resulting prefs should have the restore value as NTP. | |
227 int result; | |
228 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); | |
229 int expected = SessionStartupPref::kPrefValueNewTab; | |
230 EXPECT_EQ(expected, result); | |
231 } | |
232 | |
233 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
234 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage_NotDefined) { | |
235 // Specify the HomePage value but don't specify the home page to use. | |
236 SetPolicyValue( | |
237 key::kRestoreOnStartup, | |
238 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
239 SetPolicyValue( | |
240 key::kHomepageIsNewTabPage, | |
241 new base::FundamentalValue(false)); | |
242 ApplyPolicySettings(); | |
243 // The resulting prefs should be empty. | |
244 EXPECT_TRUE(prefs().begin() == prefs().end()); | |
245 } | |
246 | |
247 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
248 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage_WrongType) { | |
249 // Specify the HomePage value but specify a boolean as the home page. | |
250 SetPolicyValue( | |
251 key::kRestoreOnStartup, | |
252 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
253 SetPolicyValue( | |
254 key::kHomepageIsNewTabPage, | |
255 new base::FundamentalValue(false)); | |
256 SetPolicyValue( | |
257 key::kHomepageLocation, | |
258 new base::FundamentalValue(false)); | |
259 ApplyPolicySettings(); | |
260 // The resulting prefs should be empty. | |
261 EXPECT_TRUE(prefs().begin() == prefs().end()); | |
262 } | |
263 | |
264 TEST_F(RestoreOnStartupPolicyHandlerTest, | |
265 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage) { | |
266 SetPolicyValue( | |
267 key::kRestoreOnStartup, | |
268 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage)); | |
269 SetPolicyValue(key::kHomepageIsNewTabPage, new base::FundamentalValue(false)); | |
270 SetPolicyValue(key::kHomepageLocation, | |
271 new base::StringValue("http://foo.com")); | |
272 ApplyPolicySettings(); | |
273 | |
274 // The resulting prefs should have have URLs specified for startup. | |
275 int result; | |
276 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup, &result)); | |
277 int expected = SessionStartupPref::kPrefValueURLs; | |
278 EXPECT_EQ(expected, result); | |
279 | |
280 // The resulting prefs should have the URL we specified as the home page. | |
281 base::Value* url_result; | |
282 EXPECT_TRUE(prefs().GetValue(prefs::kURLsToRestoreOnStartup, &url_result)); | |
283 base::ListValue* url_list_result; | |
284 EXPECT_TRUE(url_result->GetAsList(&url_list_result)); | |
285 EXPECT_EQ(1U, url_list_result->GetSize()); | |
286 std::string expected_url; | |
287 EXPECT_TRUE(url_list_result->GetString(0, &expected_url)); | |
288 EXPECT_EQ(std::string("http://foo.com"), expected_url); | |
289 } | |
290 | |
291 } // namespace policy | 166 } // namespace policy |
OLD | NEW |