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 "components/policy/core/common/policy_types.h"
20 #include "grit/components_strings.h"
21 #include "policy/policy_constants.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/base/l10n/l10n_util.h"
27 class RestoreOnStartupPolicyHandlerTest
: public testing::Test
{
29 void SetPolicyValue(const std::string
& policy
, base::Value
* value
) {
31 policy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
, POLICY_SOURCE_CLOUD
,
34 bool CheckPolicySettings() {
35 return handler_
.CheckPolicySettings(policies_
, &errors_
);
37 void ApplyPolicySettings() {
38 handler_
.ApplyPolicySettings(policies_
, &prefs_
);
40 PolicyErrorMap
& errors() { return errors_
; }
41 PrefValueMap
& prefs() { return prefs_
; }
45 PolicyErrorMap errors_
;
47 RestoreOnStartupPolicyHandler handler_
;
50 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_FailsTypeCheck
) {
51 // Handler expects an int; pass it a bool.
52 SetPolicyValue(key::kRestoreOnStartup
, new base::FundamentalValue(false));
53 // Checking should fail and add an error to the error map.
54 EXPECT_FALSE(CheckPolicySettings());
55 EXPECT_EQ(1U, errors().size());
56 EXPECT_EQ(l10n_util::GetStringFUTF16(
57 IDS_POLICY_TYPE_ERROR
,
59 ConfigurationPolicyHandler::ValueTypeToString(
60 base::Value::TYPE_INTEGER
))),
61 errors().begin()->second
);
64 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_Unspecified
) {
65 // Don't specify a value for the policy.
66 // Checking should succeed with no errors.
67 EXPECT_TRUE(CheckPolicySettings());
68 EXPECT_EQ(0U, errors().size());
71 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_UnknownValue
) {
72 // Specify an unknown value for the policy.
73 int impossible_value
= SessionStartupPref::kPrefValueHomePage
+
74 SessionStartupPref::kPrefValueLast
+
75 SessionStartupPref::kPrefValueURLs
+
76 SessionStartupPref::kPrefValueNewTab
;
77 SetPolicyValue(key::kRestoreOnStartup
,
78 new base::FundamentalValue(impossible_value
));
79 // Checking should succeed but add an error to the error map.
80 EXPECT_TRUE(CheckPolicySettings());
81 EXPECT_EQ(1U, errors().size());
82 EXPECT_EQ(l10n_util::GetStringFUTF16(
83 IDS_POLICY_OUT_OF_RANGE_ERROR
,
84 base::ASCIIToUTF16(base::IntToString(impossible_value
))),
85 errors().begin()->second
);
88 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_HomePage
) {
89 // Specify the HomePage value.
91 key::kRestoreOnStartup
,
92 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
93 // Checking should succeed but add an error to the error map.
94 EXPECT_TRUE(CheckPolicySettings());
95 EXPECT_EQ(1U, errors().size());
96 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_POLICY_VALUE_DEPRECATED
),
97 errors().begin()->second
);
100 TEST_F(RestoreOnStartupPolicyHandlerTest
,
101 CheckPolicySettings_RestoreLastSession_SessionCookies
) {
102 // Specify the Last value and the Session-Only Cookies value.
104 key::kRestoreOnStartup
,
105 new base::FundamentalValue(SessionStartupPref::kPrefValueLast
));
106 scoped_ptr
<base::ListValue
> urls(new base::ListValue
);
107 urls
->AppendString("http://foo.com");
108 SetPolicyValue(key::kCookiesSessionOnlyForUrls
, urls
.release());
109 // Checking should succeed but add an error to the error map.
110 EXPECT_TRUE(CheckPolicySettings());
111 EXPECT_EQ(1U, errors().size());
112 EXPECT_TRUE(key::kCookiesSessionOnlyForUrls
== errors().begin()->first
);
113 EXPECT_EQ(l10n_util::GetStringFUTF16(
114 IDS_POLICY_OVERRIDDEN
,
115 base::ASCIIToUTF16(key::kRestoreOnStartup
)),
116 errors().begin()->second
);
119 TEST_F(RestoreOnStartupPolicyHandlerTest
,
120 CheckPolicySettings_RestoreLastSession_ClearDataOnExit
) {
121 // Specify the Last value and the Clear-Data-On-Exit value.
123 key::kRestoreOnStartup
,
124 new base::FundamentalValue(SessionStartupPref::kPrefValueLast
));
125 SetPolicyValue(key::kClearSiteDataOnExit
, new base::FundamentalValue(true));
126 // Checking should succeed but add an error to the error map.
127 EXPECT_TRUE(CheckPolicySettings());
128 EXPECT_EQ(1U, errors().size());
129 EXPECT_TRUE(key::kClearSiteDataOnExit
== errors().begin()->first
);
130 EXPECT_EQ(l10n_util::GetStringFUTF16(
131 IDS_POLICY_OVERRIDDEN
,
132 base::ASCIIToUTF16(key::kRestoreOnStartup
)),
133 errors().begin()->second
);
136 TEST_F(RestoreOnStartupPolicyHandlerTest
,
137 CheckPolicySettings_RestoreLastSession
) {
138 // Specify the Last value without the conflicts.
140 key::kRestoreOnStartup
,
141 new base::FundamentalValue(SessionStartupPref::kPrefValueLast
));
142 // Checking should succeed with no errors.
143 EXPECT_TRUE(CheckPolicySettings());
144 EXPECT_EQ(0U, errors().size());
147 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_URLs
) {
148 // Specify the URLs value.
150 key::kRestoreOnStartup
,
151 new base::FundamentalValue(SessionStartupPref::kPrefValueURLs
));
152 // Checking should succeed with no errors.
153 EXPECT_TRUE(CheckPolicySettings());
154 EXPECT_EQ(0U, errors().size());
157 TEST_F(RestoreOnStartupPolicyHandlerTest
, CheckPolicySettings_NewTab
) {
158 // Specify the NewTab value.
160 key::kRestoreOnStartup
,
161 new base::FundamentalValue(SessionStartupPref::kPrefValueNewTab
));
162 // Checking should succeed with no errors.
163 EXPECT_TRUE(CheckPolicySettings());
164 EXPECT_EQ(0U, errors().size());
167 TEST_F(RestoreOnStartupPolicyHandlerTest
, ApplyPolicySettings_NoValue
) {
168 // Don't specify a value for the policy.
169 ApplyPolicySettings();
170 // The resulting prefs should be empty.
171 EXPECT_TRUE(prefs().begin() == prefs().end());
174 TEST_F(RestoreOnStartupPolicyHandlerTest
, ApplyPolicySettings_WrongType
) {
175 // Handler expects an int; pass it a bool.
176 SetPolicyValue(key::kRestoreOnStartup
, new base::FundamentalValue(false));
177 // The resulting prefs should be empty.
178 EXPECT_TRUE(prefs().begin() == prefs().end());
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.
189 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup
, &result
));
190 EXPECT_EQ(not_home_page
, result
);
193 TEST_F(RestoreOnStartupPolicyHandlerTest
,
194 ApplyPolicySettings_HomePage_NoHomePageValue
) {
195 // Specify the HomePage value but no HomePageIsNewTabPage value.
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());
204 TEST_F(RestoreOnStartupPolicyHandlerTest
,
205 ApplyPolicySettings_HomePage_HomePageValueIsWrongType
) {
206 // Specify the HomePage value and an integer for the home page value.
208 key::kRestoreOnStartup
,
209 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
211 key::kHomepageIsNewTabPage
,
212 new base::FundamentalValue(314159));
213 ApplyPolicySettings();
214 // The resulting prefs should be empty.
215 EXPECT_TRUE(prefs().begin() == prefs().end());
218 TEST_F(RestoreOnStartupPolicyHandlerTest
,
219 ApplyPolicySettings_HomePage_HomePageIsNewTabPage
) {
220 // Specify the HomePage value and the home page as the new tab page.
222 key::kRestoreOnStartup
,
223 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
225 key::kHomepageIsNewTabPage
,
226 new base::FundamentalValue(true));
227 ApplyPolicySettings();
228 // The resulting prefs should have the restore value as NTP.
230 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup
, &result
));
231 int expected
= SessionStartupPref::kPrefValueNewTab
;
232 EXPECT_EQ(expected
, result
);
235 TEST_F(RestoreOnStartupPolicyHandlerTest
,
236 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage_NotDefined
) {
237 // Specify the HomePage value but don't specify the home page to use.
239 key::kRestoreOnStartup
,
240 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
242 key::kHomepageIsNewTabPage
,
243 new base::FundamentalValue(false));
244 ApplyPolicySettings();
245 // The resulting prefs should be empty.
246 EXPECT_TRUE(prefs().begin() == prefs().end());
249 TEST_F(RestoreOnStartupPolicyHandlerTest
,
250 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage_WrongType
) {
251 // Specify the HomePage value but specify a boolean as the home page.
253 key::kRestoreOnStartup
,
254 new base::FundamentalValue(SessionStartupPref::kPrefValueHomePage
));
256 key::kHomepageIsNewTabPage
,
257 new base::FundamentalValue(false));
259 key::kHomepageLocation
,
260 new base::FundamentalValue(false));
261 ApplyPolicySettings();
262 // The resulting prefs should be empty.
263 EXPECT_TRUE(prefs().begin() == prefs().end());
266 TEST_F(RestoreOnStartupPolicyHandlerTest
,
267 ApplyPolicySettings_HomePage_HomePageIsNotNewTabPage
) {
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();
276 // The resulting prefs should have have URLs specified for startup.
278 EXPECT_TRUE(prefs().GetInteger(prefs::kRestoreOnStartup
, &result
));
279 int expected
= SessionStartupPref::kPrefValueURLs
;
280 EXPECT_EQ(expected
, result
);
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
);
293 } // namespace policy