1 // Copyright (c) 2012 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.
5 // Unit tests for master preferences related methods.
7 #include "base/files/file_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/path_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/values.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/installer/util/master_preferences.h"
15 #include "chrome/installer/util/master_preferences_constants.h"
16 #include "chrome/installer/util/util_constants.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 class MasterPreferencesTest
: public testing::Test
{
22 virtual void SetUp() {
23 ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file_
));
26 virtual void TearDown() {
27 EXPECT_TRUE(base::DeleteFile(prefs_file_
, false));
30 const base::FilePath
& prefs_file() const { return prefs_file_
; }
33 base::FilePath prefs_file_
;
36 // Used to specify an expected value for a set boolean preference variable.
37 struct ExpectedBooleans
{
44 TEST_F(MasterPreferencesTest
, NoFileToParse
) {
45 EXPECT_TRUE(base::DeleteFile(prefs_file(), false));
46 installer::MasterPreferences
prefs(prefs_file());
47 EXPECT_FALSE(prefs
.read_from_file());
50 TEST_F(MasterPreferencesTest
, ParseDistroParams
) {
53 " \"distribution\": { \n"
54 " \"show_welcome_page\": true,\n"
55 " \"import_search_engine\": true,\n"
56 " \"import_history\": true,\n"
57 " \"import_bookmarks\": true,\n"
58 " \"import_bookmarks_from_file\": \"c:\\\\foo\",\n"
59 " \"import_home_page\": true,\n"
60 " \"do_not_create_any_shortcuts\": true,\n"
61 " \"do_not_create_desktop_shortcut\": true,\n"
62 " \"do_not_create_quick_launch_shortcut\": true,\n"
63 " \"do_not_create_taskbar_shortcut\": true,\n"
64 " \"do_not_launch_chrome\": true,\n"
65 " \"make_chrome_default\": true,\n"
66 " \"make_chrome_default_for_user\": true,\n"
67 " \"system_level\": true,\n"
68 " \"verbose_logging\": true,\n"
69 " \"require_eula\": true,\n"
70 " \"alternate_shortcut_text\": true,\n"
71 " \"chrome_shortcut_icon_index\": 1,\n"
72 " \"ping_delay\": 40\n"
75 " \"import_history\": false\n"
79 EXPECT_TRUE(base::WriteFile(prefs_file(), text
, strlen(text
)));
80 installer::MasterPreferences
prefs(prefs_file());
81 EXPECT_TRUE(prefs
.read_from_file());
83 const char* const expected_true
[] = {
84 installer::master_preferences::kDistroImportSearchPref
,
85 installer::master_preferences::kDistroImportHistoryPref
,
86 installer::master_preferences::kDistroImportBookmarksPref
,
87 installer::master_preferences::kDistroImportHomePagePref
,
88 installer::master_preferences::kDoNotCreateAnyShortcuts
,
89 installer::master_preferences::kDoNotCreateDesktopShortcut
,
90 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
91 installer::master_preferences::kDoNotCreateTaskbarShortcut
,
92 installer::master_preferences::kDoNotLaunchChrome
,
93 installer::master_preferences::kMakeChromeDefault
,
94 installer::master_preferences::kMakeChromeDefaultForUser
,
95 installer::master_preferences::kSystemLevel
,
96 installer::master_preferences::kVerboseLogging
,
97 installer::master_preferences::kRequireEula
,
98 installer::master_preferences::kAltShortcutText
,
101 for (int i
= 0; i
< arraysize(expected_true
); ++i
) {
103 EXPECT_TRUE(prefs
.GetBool(expected_true
[i
], &value
));
104 EXPECT_TRUE(value
) << expected_true
[i
];
107 std::string str_value
;
108 EXPECT_TRUE(prefs
.GetString(
109 installer::master_preferences::kDistroImportBookmarksFromFilePref
,
111 EXPECT_STREQ("c:\\foo", str_value
.c_str());
114 EXPECT_TRUE(prefs
.GetInt(
115 installer::master_preferences::kChromeShortcutIconIndex
,
117 EXPECT_EQ(icon_index
, 1);
119 EXPECT_TRUE(prefs
.GetInt(installer::master_preferences::kDistroPingDelay
,
121 EXPECT_EQ(ping_delay
, 40);
124 TEST_F(MasterPreferencesTest
, ParseMissingDistroParams
) {
127 " \"distribution\": { \n"
128 " \"import_search_engine\": true,\n"
129 " \"import_bookmarks\": false,\n"
130 " \"import_bookmarks_from_file\": \"\",\n"
131 " \"do_not_create_desktop_shortcut\": true,\n"
132 " \"do_not_create_quick_launch_shortcut\": true,\n"
133 " \"do_not_launch_chrome\": true,\n"
134 " \"chrome_shortcut_icon_index\": \"bac\"\n"
138 EXPECT_TRUE(base::WriteFile(prefs_file(), text
, strlen(text
)));
139 installer::MasterPreferences
prefs(prefs_file());
140 EXPECT_TRUE(prefs
.read_from_file());
142 ExpectedBooleans expected_bool
[] = {
143 { installer::master_preferences::kDistroImportSearchPref
, true },
144 { installer::master_preferences::kDistroImportBookmarksPref
, false },
145 { installer::master_preferences::kDoNotCreateDesktopShortcut
, true },
146 { installer::master_preferences::kDoNotCreateQuickLaunchShortcut
, true },
147 { installer::master_preferences::kDoNotLaunchChrome
, true },
151 for (int i
= 0; i
< arraysize(expected_bool
); ++i
) {
152 EXPECT_TRUE(prefs
.GetBool(expected_bool
[i
].name
, &value
));
153 EXPECT_EQ(value
, expected_bool
[i
].expected_value
) << expected_bool
[i
].name
;
156 const char* const missing_bools
[] = {
157 installer::master_preferences::kDistroImportHomePagePref
,
158 installer::master_preferences::kDoNotRegisterForUpdateLaunch
,
159 installer::master_preferences::kMakeChromeDefault
,
160 installer::master_preferences::kMakeChromeDefaultForUser
,
163 for (int i
= 0; i
< arraysize(missing_bools
); ++i
) {
164 EXPECT_FALSE(prefs
.GetBool(missing_bools
[i
], &value
)) << missing_bools
[i
];
167 std::string str_value
;
168 EXPECT_FALSE(prefs
.GetString(
169 installer::master_preferences::kDistroImportBookmarksFromFilePref
,
173 EXPECT_FALSE(prefs
.GetInt(
174 installer::master_preferences::kChromeShortcutIconIndex
,
176 EXPECT_EQ(icon_index
, 0);
179 EXPECT_FALSE(prefs
.GetInt(
180 installer::master_preferences::kDistroPingDelay
, &ping_delay
));
181 EXPECT_EQ(ping_delay
, 90);
184 TEST_F(MasterPreferencesTest
, FirstRunTabs
) {
187 " \"distribution\": { \n"
188 " \"something here\": true\n"
190 " \"first_run_tabs\": [\n"
191 " \"http://google.com/f1\",\n"
192 " \"https://google.com/f2\",\n"
193 " \"new_tab_page\"\n"
197 EXPECT_TRUE(base::WriteFile(prefs_file(), text
, strlen(text
)));
198 installer::MasterPreferences
prefs(prefs_file());
199 typedef std::vector
<std::string
> TabsVector
;
200 TabsVector tabs
= prefs
.GetFirstRunTabs();
201 ASSERT_EQ(3, tabs
.size());
202 EXPECT_EQ("http://google.com/f1", tabs
[0]);
203 EXPECT_EQ("https://google.com/f2", tabs
[1]);
204 EXPECT_EQ("new_tab_page", tabs
[2]);
207 // In this test instead of using our synthetic json file, we use an
208 // actual test case from the extensions unittest. The hope here is that if
209 // they change something in the manifest this test will break, but in
210 // general it is expected the extension format to be backwards compatible.
211 TEST(MasterPrefsExtension
, ValidateExtensionJSON
) {
212 base::FilePath prefs_path
;
213 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &prefs_path
));
214 prefs_path
= prefs_path
.AppendASCII("extensions")
215 .AppendASCII("good").AppendASCII("Preferences");
217 installer::MasterPreferences
prefs(prefs_path
);
218 base::DictionaryValue
* extensions
= NULL
;
219 EXPECT_TRUE(prefs
.GetExtensionsBlock(&extensions
));
221 EXPECT_TRUE(extensions
->GetInteger(
222 "behllobkkfkfnphdnhnkndlbkcpglgmj.location", &location
));
224 EXPECT_TRUE(extensions
->GetInteger(
225 "behllobkkfkfnphdnhnkndlbkcpglgmj.state", &state
));
227 EXPECT_TRUE(extensions
->GetString(
228 "behllobkkfkfnphdnhnkndlbkcpglgmj.path", &path
));
230 EXPECT_TRUE(extensions
->GetString(
231 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.key", &key
));
233 EXPECT_TRUE(extensions
->GetString(
234 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.name", &name
));
236 EXPECT_TRUE(extensions
->GetString(
237 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.version", &version
));
240 // Test that we are parsing master preferences correctly.
241 TEST_F(MasterPreferencesTest
, GetInstallPreferencesTest
) {
242 // Create a temporary prefs file.
243 base::FilePath prefs_file
;
244 ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file
));
247 " \"distribution\": { \n"
248 " \"do_not_create_desktop_shortcut\": false,\n"
249 " \"do_not_create_quick_launch_shortcut\": false,\n"
250 " \"do_not_launch_chrome\": true,\n"
251 " \"system_level\": true,\n"
252 " \"verbose_logging\": false\n"
255 EXPECT_TRUE(base::WriteFile(prefs_file
, text
, strlen(text
)));
257 // Make sure command line values override the values in master preferences.
258 std::wstring
cmd_str(
259 L
"setup.exe --installerdata=\"" + prefs_file
.value() + L
"\"");
260 cmd_str
.append(L
" --do-not-launch-chrome");
261 base::CommandLine cmd_line
= base::CommandLine::FromString(cmd_str
);
262 installer::MasterPreferences
prefs(cmd_line
);
264 // Check prefs that do not have any equivalent command line option.
265 ExpectedBooleans expected_bool
[] = {
266 { installer::master_preferences::kDoNotLaunchChrome
, true },
267 { installer::master_preferences::kSystemLevel
, true },
268 { installer::master_preferences::kVerboseLogging
, false },
271 // Now check that prefs got merged correctly.
273 for (int i
= 0; i
< arraysize(expected_bool
); ++i
) {
274 EXPECT_TRUE(prefs
.GetBool(expected_bool
[i
].name
, &value
));
275 EXPECT_EQ(value
, expected_bool
[i
].expected_value
) << expected_bool
[i
].name
;
278 // Delete temporary prefs file.
279 EXPECT_TRUE(base::DeleteFile(prefs_file
, false));
281 // Check that if master prefs doesn't exist, we can still parse the common
283 cmd_str
= L
"setup.exe --do-not-launch-chrome";
284 cmd_line
.ParseFromString(cmd_str
);
285 installer::MasterPreferences
prefs2(cmd_line
);
286 ExpectedBooleans expected_bool2
[] = {
287 { installer::master_preferences::kDoNotLaunchChrome
, true },
290 for (int i
= 0; i
< arraysize(expected_bool2
); ++i
) {
291 EXPECT_TRUE(prefs2
.GetBool(expected_bool2
[i
].name
, &value
));
292 EXPECT_EQ(value
, expected_bool2
[i
].expected_value
)
293 << expected_bool2
[i
].name
;
296 EXPECT_FALSE(prefs2
.GetBool(
297 installer::master_preferences::kSystemLevel
, &value
));
298 EXPECT_FALSE(prefs2
.GetBool(
299 installer::master_preferences::kVerboseLogging
, &value
));
302 TEST_F(MasterPreferencesTest
, TestDefaultInstallConfig
) {
303 std::wstringstream chrome_cmd
;
304 chrome_cmd
<< "setup.exe";
306 base::CommandLine
chrome_install(
307 base::CommandLine::FromString(chrome_cmd
.str()));
309 installer::MasterPreferences
pref_chrome(chrome_install
);
311 EXPECT_FALSE(pref_chrome
.is_multi_install());
312 EXPECT_TRUE(pref_chrome
.install_chrome());
315 TEST_F(MasterPreferencesTest
, TestMultiInstallConfig
) {
316 using installer::switches::kMultiInstall
;
317 using installer::switches::kChrome
;
319 std::wstringstream chrome_cmd
, cf_cmd
, chrome_cf_cmd
;
320 chrome_cmd
<< "setup.exe --" << kMultiInstall
<< " --" << kChrome
;
322 base::CommandLine
chrome_install(
323 base::CommandLine::FromString(chrome_cmd
.str()));
325 installer::MasterPreferences
pref_chrome(chrome_install
);
327 EXPECT_TRUE(pref_chrome
.is_multi_install());
328 EXPECT_TRUE(pref_chrome
.install_chrome());
331 TEST_F(MasterPreferencesTest
, EnforceLegacyCreateAllShortcutsFalse
) {
332 static const char kCreateAllShortcutsFalsePrefs
[] =
334 " \"distribution\": {"
335 " \"create_all_shortcuts\": false"
339 installer::MasterPreferences
prefs(kCreateAllShortcutsFalsePrefs
);
341 bool do_not_create_desktop_shortcut
= false;
342 bool do_not_create_quick_launch_shortcut
= false;
343 bool do_not_create_taskbar_shortcut
= false;
345 installer::master_preferences::kDoNotCreateDesktopShortcut
,
346 &do_not_create_desktop_shortcut
);
348 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
349 &do_not_create_quick_launch_shortcut
);
351 installer::master_preferences::kDoNotCreateTaskbarShortcut
,
352 &do_not_create_taskbar_shortcut
);
353 // create_all_shortcuts is a legacy preference that should only enforce
354 // do_not_create_desktop_shortcut and do_not_create_quick_launch_shortcut
355 // when set to false.
356 EXPECT_TRUE(do_not_create_desktop_shortcut
);
357 EXPECT_TRUE(do_not_create_quick_launch_shortcut
);
358 EXPECT_FALSE(do_not_create_taskbar_shortcut
);
361 TEST_F(MasterPreferencesTest
, DontEnforceLegacyCreateAllShortcutsTrue
) {
362 static const char kCreateAllShortcutsFalsePrefs
[] =
364 " \"distribution\": {"
365 " \"create_all_shortcuts\": true"
369 installer::MasterPreferences
prefs(kCreateAllShortcutsFalsePrefs
);
371 bool do_not_create_desktop_shortcut
= false;
372 bool do_not_create_quick_launch_shortcut
= false;
373 bool do_not_create_taskbar_shortcut
= false;
375 installer::master_preferences::kDoNotCreateDesktopShortcut
,
376 &do_not_create_desktop_shortcut
);
378 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
379 &do_not_create_quick_launch_shortcut
);
381 installer::master_preferences::kDoNotCreateTaskbarShortcut
,
382 &do_not_create_taskbar_shortcut
);
383 EXPECT_FALSE(do_not_create_desktop_shortcut
);
384 EXPECT_FALSE(do_not_create_quick_launch_shortcut
);
385 EXPECT_FALSE(do_not_create_taskbar_shortcut
);
388 TEST_F(MasterPreferencesTest
, DontEnforceLegacyCreateAllShortcutsNotSpecified
) {
389 static const char kCreateAllShortcutsFalsePrefs
[] =
391 " \"distribution\": {"
392 " \"some_other_pref\": true"
396 installer::MasterPreferences
prefs(kCreateAllShortcutsFalsePrefs
);
398 bool do_not_create_desktop_shortcut
= false;
399 bool do_not_create_quick_launch_shortcut
= false;
400 bool do_not_create_taskbar_shortcut
= false;
402 installer::master_preferences::kDoNotCreateDesktopShortcut
,
403 &do_not_create_desktop_shortcut
);
405 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
406 &do_not_create_quick_launch_shortcut
);
408 installer::master_preferences::kDoNotCreateTaskbarShortcut
,
409 &do_not_create_taskbar_shortcut
);
410 EXPECT_FALSE(do_not_create_desktop_shortcut
);
411 EXPECT_FALSE(do_not_create_quick_launch_shortcut
);
412 EXPECT_FALSE(do_not_create_taskbar_shortcut
);
415 TEST_F(MasterPreferencesTest
, MigrateOldStartupUrlsPref
) {
416 static const char kOldMasterPrefs
[] =
418 " \"distribution\": { \n"
419 " \"show_welcome_page\": true,\n"
420 " \"import_search_engine\": true,\n"
421 " \"import_history\": true,\n"
422 " \"import_bookmarks\": true\n"
425 " \"urls_to_restore_on_startup\": [\"http://www.google.com\"]\n"
429 const installer::MasterPreferences
prefs(kOldMasterPrefs
);
430 const base::DictionaryValue
& master_dictionary
=
431 prefs
.master_dictionary();
433 const base::ListValue
* old_startup_urls_list
= NULL
;
434 EXPECT_TRUE(master_dictionary
.GetList(prefs::kURLsToRestoreOnStartupOld
,
435 &old_startup_urls_list
));
436 EXPECT_TRUE(old_startup_urls_list
!= NULL
);
438 // The MasterPreferences dictionary should also conjure up the new setting
439 // as per EnforceLegacyPreferences.
440 const base::ListValue
* new_startup_urls_list
= NULL
;
441 EXPECT_TRUE(master_dictionary
.GetList(prefs::kURLsToRestoreOnStartup
,
442 &new_startup_urls_list
));
443 EXPECT_TRUE(new_startup_urls_list
!= NULL
);
446 TEST_F(MasterPreferencesTest
, DontMigrateOldStartupUrlsPrefWhenNewExists
) {
447 static const char kOldAndNewMasterPrefs
[] =
449 " \"distribution\": { \n"
450 " \"show_welcome_page\": true,\n"
451 " \"import_search_engine\": true,\n"
452 " \"import_history\": true,\n"
453 " \"import_bookmarks\": true\n"
456 " \"urls_to_restore_on_startup\": [\"http://www.google.com\"],\n"
457 " \"startup_urls\": [\"http://www.example.com\"]\n"
461 const installer::MasterPreferences
prefs(kOldAndNewMasterPrefs
);
462 const base::DictionaryValue
& master_dictionary
=
463 prefs
.master_dictionary();
465 const base::ListValue
* old_startup_urls_list
= NULL
;
466 EXPECT_TRUE(master_dictionary
.GetList(prefs::kURLsToRestoreOnStartupOld
,
467 &old_startup_urls_list
));
468 ASSERT_TRUE(old_startup_urls_list
!= NULL
);
469 std::string url_value
;
470 EXPECT_TRUE(old_startup_urls_list
->GetString(0, &url_value
));
471 EXPECT_EQ("http://www.google.com", url_value
);
473 // The MasterPreferences dictionary should also conjure up the new setting
474 // as per EnforceLegacyPreferences.
475 const base::ListValue
* new_startup_urls_list
= NULL
;
476 EXPECT_TRUE(master_dictionary
.GetList(prefs::kURLsToRestoreOnStartup
,
477 &new_startup_urls_list
));
478 ASSERT_TRUE(new_startup_urls_list
!= NULL
);
479 std::string new_url_value
;
480 EXPECT_TRUE(new_startup_urls_list
->GetString(0, &new_url_value
));
481 EXPECT_EQ("http://www.example.com", new_url_value
);