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.
7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_value_map.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/prefs/session_startup_pref.h"
14 #include "chrome/browser/sessions/restore_on_startup_policy_handler.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/policy/core/browser/configuration_policy_handler.h"
17 #include "components/policy/core/browser/policy_error_map.h"
18 #include "components/policy/core/common/policy_map.h"
19 #include "grit/component_strings.h"
20 #include "policy/policy_constants.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/base/l10n/l10n_util.h"
26 class RestoreOnStartupPolicyHandlerTest
: public testing::Test
{
28 void SetPolicyValue(const std::string
& policy
, base::Value
* value
) {
30 policy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
, value
, NULL
);
32 bool CheckPolicySettings() {
33 return handler_
.CheckPolicySettings(policies_
, &errors_
);
35 void ApplyPolicySettings() {
36 handler_
.ApplyPolicySettings(policies_
, &prefs_
);
38 PolicyErrorMap
& errors() { return errors_
; }
39 PrefValueMap
& prefs() { return prefs_
; }
43 PolicyErrorMap errors_
;
45 RestoreOnStartupPolicyHandler handler_
;
48 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_FailsTypeCheck
) {
49 // Handler expects an int; pass it a bool.
50 SetPolicyValue(key::kRestoreOnStartup
, new base::FundamentalValue(false));
51 // Checking should fail and add an error to the error map.
52 EXPECT_FALSE(CheckPolicySettings());
53 EXPECT_EQ(1U, errors().size());
54 EXPECT_EQ(l10n_util::GetStringFUTF16(
55 IDS_POLICY_TYPE_ERROR
,
57 ConfigurationPolicyHandler::ValueTypeToString(
58 base::Value::TYPE_INTEGER
))),
59 errors().begin()->second
);
62 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_Unspecified
) {
63 // Don't specify a value for the policy.
64 // Checking should succeed with no errors.
65 EXPECT_TRUE(CheckPolicySettings());
66 EXPECT_EQ(0U, errors().size());
69 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_UnknownValue
) {
70 // Specify an unknown value for the policy.
71 int impossible_value
= SessionStartupPref::kPrefValueHomePage
+
72 SessionStartupPref::kPrefValueLast
+
73 SessionStartupPref::kPrefValueURLs
+
74 SessionStartupPref::kPrefValueNewTab
;
75 SetPolicyValue(key::kRestoreOnStartup
,
76 new base::FundamentalValue(impossible_value
));
77 // Checking should succeed but add an error to the error map.
78 EXPECT_TRUE(CheckPolicySettings());
79 EXPECT_EQ(1U, errors().size());
80 EXPECT_EQ(l10n_util::GetStringFUTF16(
81 IDS_POLICY_OUT_OF_RANGE_ERROR
,
82 base::ASCIIToUTF16(base::IntToString(impossible_value
))),
83 errors().begin()->second
);
86 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_HomePage
) {
87 // Specify the HomePage value.
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
);
98 TEST_F(RestoreOnStartupPolicyHandlerTest
,
99 CheckPolicySettings_RestoreLastSession_SessionCookies
) {
100 // Specify the Last value and the Session-Only Cookies value.
102 key::kRestoreOnStartup
,
103 new base::FundamentalValue(SessionStartupPref::kPrefValueLast
));
104 scoped_ptr
<base::ListValue
> urls(new base::ListValue
);
105 urls
->AppendString("http://foo.com");
106 SetPolicyValue(key::kCookiesSessionOnlyForUrls
, urls
.release());
107 // Checking should succeed but add an error to the error map.
108 EXPECT_TRUE(CheckPolicySettings());
109 EXPECT_EQ(1U, errors().size());
110 EXPECT_TRUE(key::kCookiesSessionOnlyForUrls
== errors().begin()->first
);
111 EXPECT_EQ(l10n_util::GetStringFUTF16(
112 IDS_POLICY_OVERRIDDEN
,
113 base::ASCIIToUTF16(key::kRestoreOnStartup
)),
114 errors().begin()->second
);
117 TEST_F(RestoreOnStartupPolicyHandlerTest
,
118 CheckPolicySettings_RestoreLastSession_ClearDataOnExit
) {
119 // Specify the Last value and the Clear-Data-On-Exit value.
121 key::kRestoreOnStartup
,
122 new base::FundamentalValue(SessionStartupPref::kPrefValueLast
));
123 SetPolicyValue(key::kClearSiteDataOnExit
, new base::FundamentalValue(true));
124 // Checking should succeed but add an error to the error map.
125 EXPECT_TRUE(CheckPolicySettings());
126 EXPECT_EQ(1U, errors().size());
127 EXPECT_TRUE(key::kClearSiteDataOnExit
== errors().begin()->first
);
128 EXPECT_EQ(l10n_util::GetStringFUTF16(
129 IDS_POLICY_OVERRIDDEN
,
130 base::ASCIIToUTF16(key::kRestoreOnStartup
)),
131 errors().begin()->second
);
134 TEST_F(RestoreOnStartupPolicyHandlerTest
,
135 CheckPolicySettings_RestoreLastSession
) {
136 // Specify the Last value without the conflicts.
138 key::kRestoreOnStartup
,
139 new base::FundamentalValue(SessionStartupPref::kPrefValueLast
));
140 // Checking should succeed with no errors.
141 EXPECT_TRUE(CheckPolicySettings());
142 EXPECT_EQ(0U, errors().size());
145 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_URLs
) {
146 // Specify the URLs value.
148 key::kRestoreOnStartup
,
149 new base::FundamentalValue(SessionStartupPref::kPrefValueURLs
));
150 // Checking should succeed with no errors.
151 EXPECT_TRUE(CheckPolicySettings());
152 EXPECT_EQ(0U, errors().size());
155 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_NewTab
) {
156 // Specify the NewTab value.
158 key::kRestoreOnStartup
,
159 new base::FundamentalValue(SessionStartupPref::kPrefValueNewTab
));
160 // Checking should succeed with no errors.
161 EXPECT_TRUE(CheckPolicySettings());
162 EXPECT_EQ(0U, errors().size());
165 TEST_F(RestoreOnStartupPolicyHandlerTest
, ApplyPolicySettings_NoValue
) {
166 // Don't specify a value for the policy.
167 ApplyPolicySettings();
168 // The resulting prefs should be empty.
169 EXPECT_TRUE(prefs().begin() == prefs().end());
172 TEST_F(RestoreOnStartupPolicyHandlerTest
, ApplyPolicySettings_WrongType
) {
173 // Handler expects an int; pass it a bool.
174 SetPolicyValue(key::kRestoreOnStartup
, new base::FundamentalValue(false));
175 // The resulting prefs should be empty.
176 EXPECT_TRUE(prefs().begin() == prefs().end());
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.
187 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup
, &result
));
188 EXPECT_EQ(not_home_page
, result
);
191 TEST_F(RestoreOnStartupPolicyHandlerTest
,
192 ApplyPolicySettings_HomePage_NoHomePageValue
) {
193 // Specify the HomePage value but no HomePageIsNewTabPage value.
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());
202 TEST_F(RestoreOnStartupPolicyHandlerTest
,
203 ApplyPolicySettings_HomePage_HomePageValueIsWrongType
) {
204 // Specify the HomePage value and an integer for the home page value.
206 key::kRestoreOnStartup
,
207 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
209 key::kHomepageIsNewTabPage
,
210 new base::FundamentalValue(314159));
211 ApplyPolicySettings();
212 // The resulting prefs should be empty.
213 EXPECT_TRUE(prefs().begin() == prefs().end());
216 TEST_F(RestoreOnStartupPolicyHandlerTest
,
217 ApplyPolicySettings_HomePage_HomePageIsNewTabPage
) {
218 // Specify the HomePage value and the home page as the new tab page.
220 key::kRestoreOnStartup
,
221 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
223 key::kHomepageIsNewTabPage
,
224 new base::FundamentalValue(true));
225 ApplyPolicySettings();
226 // The resulting prefs should have the restore value as NTP.
228 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup
, &result
));
229 int expected
= SessionStartupPref::kPrefValueNewTab
;
230 EXPECT_EQ(expected
, result
);
233 TEST_F(RestoreOnStartupPolicyHandlerTest
,
234 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage_NotDefined
) {
235 // Specify the HomePage value but don't specify the home page to use.
237 key::kRestoreOnStartup
,
238 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
240 key::kHomepageIsNewTabPage
,
241 new base::FundamentalValue(false));
242 ApplyPolicySettings();
243 // The resulting prefs should be empty.
244 EXPECT_TRUE(prefs().begin() == prefs().end());
247 TEST_F(RestoreOnStartupPolicyHandlerTest
,
248 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage_WrongType
) {
249 // Specify the HomePage value but specify a boolean as the home page.
251 key::kRestoreOnStartup
,
252 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
254 key::kHomepageIsNewTabPage
,
255 new base::FundamentalValue(false));
257 key::kHomepageLocation
,
258 new base::FundamentalValue(false));
259 ApplyPolicySettings();
260 // The resulting prefs should be empty.
261 EXPECT_TRUE(prefs().begin() == prefs().end());
264 TEST_F(RestoreOnStartupPolicyHandlerTest
,
265 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage
) {
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();
274 // The resulting prefs should have have URLs specified for startup.
276 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup
, &result
));
277 int expected
= SessionStartupPref::kPrefValueURLs
;
278 EXPECT_EQ(expected
, result
);
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
);
291 } // namespace policy