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 void SetUp() override
{
23 ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file_
));
26 void TearDown() override
{
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 " \"welcome_page_on_os_upgrade_enabled\": true,\n"
61 " \"do_not_create_any_shortcuts\": true,\n"
62 " \"do_not_create_desktop_shortcut\": true,\n"
63 " \"do_not_create_quick_launch_shortcut\": true,\n"
64 " \"do_not_create_taskbar_shortcut\": true,\n"
65 " \"do_not_create_start_pin\": true,\n"
66 " \"do_not_launch_chrome\": true,\n"
67 " \"make_chrome_default\": true,\n"
68 " \"make_chrome_default_for_user\": true,\n"
69 " \"system_level\": true,\n"
70 " \"verbose_logging\": true,\n"
71 " \"require_eula\": true,\n"
72 " \"alternate_shortcut_text\": true,\n"
73 " \"chrome_shortcut_icon_index\": 1,\n"
74 " \"ping_delay\": 40\n"
77 " \"import_history\": false\n"
81 EXPECT_TRUE(base::WriteFile(prefs_file(), text
,
82 static_cast<int>(strlen(text
))));
83 installer::MasterPreferences
prefs(prefs_file());
84 EXPECT_TRUE(prefs
.read_from_file());
86 const char* const expected_true
[] = {
87 installer::master_preferences::kDistroImportSearchPref
,
88 installer::master_preferences::kDistroImportHistoryPref
,
89 installer::master_preferences::kDistroImportBookmarksPref
,
90 installer::master_preferences::kDistroImportHomePagePref
,
91 installer::master_preferences::kDistroWelcomePageOnOSUpgradeEnabled
,
92 installer::master_preferences::kDoNotCreateAnyShortcuts
,
93 installer::master_preferences::kDoNotCreateDesktopShortcut
,
94 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
95 installer::master_preferences::kDoNotCreateTaskbarShortcut
,
96 installer::master_preferences::kDoNotCreateStartPin
,
97 installer::master_preferences::kDoNotLaunchChrome
,
98 installer::master_preferences::kMakeChromeDefault
,
99 installer::master_preferences::kMakeChromeDefaultForUser
,
100 installer::master_preferences::kSystemLevel
,
101 installer::master_preferences::kVerboseLogging
,
102 installer::master_preferences::kRequireEula
,
103 installer::master_preferences::kAltShortcutText
,
106 for (int i
= 0; i
< arraysize(expected_true
); ++i
) {
108 EXPECT_TRUE(prefs
.GetBool(expected_true
[i
], &value
));
109 EXPECT_TRUE(value
) << expected_true
[i
];
112 std::string str_value
;
113 EXPECT_TRUE(prefs
.GetString(
114 installer::master_preferences::kDistroImportBookmarksFromFilePref
,
116 EXPECT_STREQ("c:\\foo", str_value
.c_str());
119 EXPECT_TRUE(prefs
.GetInt(
120 installer::master_preferences::kChromeShortcutIconIndex
,
122 EXPECT_EQ(icon_index
, 1);
124 EXPECT_TRUE(prefs
.GetInt(installer::master_preferences::kDistroPingDelay
,
126 EXPECT_EQ(ping_delay
, 40);
129 TEST_F(MasterPreferencesTest
, ParseMissingDistroParams
) {
132 " \"distribution\": { \n"
133 " \"import_search_engine\": true,\n"
134 " \"import_bookmarks\": false,\n"
135 " \"import_bookmarks_from_file\": \"\",\n"
136 " \"do_not_create_desktop_shortcut\": true,\n"
137 " \"do_not_create_quick_launch_shortcut\": true,\n"
138 " \"do_not_launch_chrome\": true,\n"
139 " \"chrome_shortcut_icon_index\": \"bac\"\n"
143 EXPECT_TRUE(base::WriteFile(prefs_file(), text
,
144 static_cast<int>(strlen(text
))));
145 installer::MasterPreferences
prefs(prefs_file());
146 EXPECT_TRUE(prefs
.read_from_file());
148 ExpectedBooleans expected_bool
[] = {
149 { installer::master_preferences::kDistroImportSearchPref
, true },
150 { installer::master_preferences::kDistroImportBookmarksPref
, false },
151 { installer::master_preferences::kDoNotCreateDesktopShortcut
, true },
152 { installer::master_preferences::kDoNotCreateQuickLaunchShortcut
, true },
153 { installer::master_preferences::kDoNotLaunchChrome
, true },
157 for (int i
= 0; i
< arraysize(expected_bool
); ++i
) {
158 EXPECT_TRUE(prefs
.GetBool(expected_bool
[i
].name
, &value
));
159 EXPECT_EQ(value
, expected_bool
[i
].expected_value
) << expected_bool
[i
].name
;
162 const char* const missing_bools
[] = {
163 installer::master_preferences::kDistroImportHomePagePref
,
164 installer::master_preferences::kDistroWelcomePageOnOSUpgradeEnabled
,
165 installer::master_preferences::kDoNotRegisterForUpdateLaunch
,
166 installer::master_preferences::kMakeChromeDefault
,
167 installer::master_preferences::kMakeChromeDefaultForUser
,
170 for (int i
= 0; i
< arraysize(missing_bools
); ++i
) {
171 EXPECT_FALSE(prefs
.GetBool(missing_bools
[i
], &value
)) << missing_bools
[i
];
174 std::string str_value
;
175 EXPECT_FALSE(prefs
.GetString(
176 installer::master_preferences::kDistroImportBookmarksFromFilePref
,
180 EXPECT_FALSE(prefs
.GetInt(
181 installer::master_preferences::kChromeShortcutIconIndex
,
183 EXPECT_EQ(icon_index
, 0);
186 EXPECT_FALSE(prefs
.GetInt(
187 installer::master_preferences::kDistroPingDelay
, &ping_delay
));
188 EXPECT_EQ(ping_delay
, 90);
191 TEST_F(MasterPreferencesTest
, FirstRunTabs
) {
194 " \"distribution\": { \n"
195 " \"something here\": true\n"
197 " \"first_run_tabs\": [\n"
198 " \"http://google.com/f1\",\n"
199 " \"https://google.com/f2\",\n"
200 " \"new_tab_page\"\n"
204 EXPECT_TRUE(base::WriteFile(prefs_file(), text
,
205 static_cast<int>(strlen(text
))));
206 installer::MasterPreferences
prefs(prefs_file());
207 typedef std::vector
<std::string
> TabsVector
;
208 TabsVector tabs
= prefs
.GetFirstRunTabs();
209 ASSERT_EQ(3, tabs
.size());
210 EXPECT_EQ("http://google.com/f1", tabs
[0]);
211 EXPECT_EQ("https://google.com/f2", tabs
[1]);
212 EXPECT_EQ("new_tab_page", tabs
[2]);
215 // In this test instead of using our synthetic json file, we use an
216 // actual test case from the extensions unittest. The hope here is that if
217 // they change something in the manifest this test will break, but in
218 // general it is expected the extension format to be backwards compatible.
219 TEST(MasterPrefsExtension
, ValidateExtensionJSON
) {
220 base::FilePath prefs_path
;
221 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &prefs_path
));
222 prefs_path
= prefs_path
.AppendASCII("extensions")
223 .AppendASCII("good").AppendASCII("Preferences");
225 installer::MasterPreferences
prefs(prefs_path
);
226 base::DictionaryValue
* extensions
= NULL
;
227 EXPECT_TRUE(prefs
.GetExtensionsBlock(&extensions
));
229 EXPECT_TRUE(extensions
->GetInteger(
230 "behllobkkfkfnphdnhnkndlbkcpglgmj.location", &location
));
232 EXPECT_TRUE(extensions
->GetInteger(
233 "behllobkkfkfnphdnhnkndlbkcpglgmj.state", &state
));
235 EXPECT_TRUE(extensions
->GetString(
236 "behllobkkfkfnphdnhnkndlbkcpglgmj.path", &path
));
238 EXPECT_TRUE(extensions
->GetString(
239 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.key", &key
));
241 EXPECT_TRUE(extensions
->GetString(
242 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.name", &name
));
244 EXPECT_TRUE(extensions
->GetString(
245 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.version", &version
));
248 // Test that we are parsing master preferences correctly.
249 TEST_F(MasterPreferencesTest
, GetInstallPreferencesTest
) {
250 // Create a temporary prefs file.
251 base::FilePath prefs_file
;
252 ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file
));
255 " \"distribution\": { \n"
256 " \"do_not_create_desktop_shortcut\": false,\n"
257 " \"do_not_create_quick_launch_shortcut\": false,\n"
258 " \"do_not_launch_chrome\": true,\n"
259 " \"system_level\": true,\n"
260 " \"verbose_logging\": false\n"
263 EXPECT_TRUE(base::WriteFile(prefs_file
, text
,
264 static_cast<int>(strlen(text
))));
266 // Make sure command line values override the values in master preferences.
267 std::wstring
cmd_str(
268 L
"setup.exe --installerdata=\"" + prefs_file
.value() + L
"\"");
269 cmd_str
.append(L
" --do-not-launch-chrome");
270 base::CommandLine cmd_line
= base::CommandLine::FromString(cmd_str
);
271 installer::MasterPreferences
prefs(cmd_line
);
273 // Check prefs that do not have any equivalent command line option.
274 ExpectedBooleans expected_bool
[] = {
275 { installer::master_preferences::kDoNotLaunchChrome
, true },
276 { installer::master_preferences::kSystemLevel
, true },
277 { installer::master_preferences::kVerboseLogging
, false },
280 // Now check that prefs got merged correctly.
282 for (int i
= 0; i
< arraysize(expected_bool
); ++i
) {
283 EXPECT_TRUE(prefs
.GetBool(expected_bool
[i
].name
, &value
));
284 EXPECT_EQ(value
, expected_bool
[i
].expected_value
) << expected_bool
[i
].name
;
287 // Delete temporary prefs file.
288 EXPECT_TRUE(base::DeleteFile(prefs_file
, false));
290 // Check that if master prefs doesn't exist, we can still parse the common
292 cmd_str
= L
"setup.exe --do-not-launch-chrome";
293 cmd_line
.ParseFromString(cmd_str
);
294 installer::MasterPreferences
prefs2(cmd_line
);
295 ExpectedBooleans expected_bool2
[] = {
296 { installer::master_preferences::kDoNotLaunchChrome
, true },
299 for (int i
= 0; i
< arraysize(expected_bool2
); ++i
) {
300 EXPECT_TRUE(prefs2
.GetBool(expected_bool2
[i
].name
, &value
));
301 EXPECT_EQ(value
, expected_bool2
[i
].expected_value
)
302 << expected_bool2
[i
].name
;
305 EXPECT_FALSE(prefs2
.GetBool(
306 installer::master_preferences::kSystemLevel
, &value
));
307 EXPECT_FALSE(prefs2
.GetBool(
308 installer::master_preferences::kVerboseLogging
, &value
));
311 TEST_F(MasterPreferencesTest
, TestDefaultInstallConfig
) {
312 std::wstringstream chrome_cmd
;
313 chrome_cmd
<< "setup.exe";
315 base::CommandLine
chrome_install(
316 base::CommandLine::FromString(chrome_cmd
.str()));
318 installer::MasterPreferences
pref_chrome(chrome_install
);
320 EXPECT_FALSE(pref_chrome
.is_multi_install());
321 EXPECT_TRUE(pref_chrome
.install_chrome());
324 TEST_F(MasterPreferencesTest
, TestMultiInstallConfig
) {
325 using installer::switches::kMultiInstall
;
326 using installer::switches::kChrome
;
328 std::wstringstream chrome_cmd
, cf_cmd
, chrome_cf_cmd
;
329 chrome_cmd
<< "setup.exe --" << kMultiInstall
<< " --" << kChrome
;
331 base::CommandLine
chrome_install(
332 base::CommandLine::FromString(chrome_cmd
.str()));
334 installer::MasterPreferences
pref_chrome(chrome_install
);
336 EXPECT_TRUE(pref_chrome
.is_multi_install());
337 EXPECT_TRUE(pref_chrome
.install_chrome());
340 TEST_F(MasterPreferencesTest
, EnforceLegacyCreateAllShortcutsFalse
) {
341 static const char kCreateAllShortcutsFalsePrefs
[] =
343 " \"distribution\": {"
344 " \"create_all_shortcuts\": false"
348 installer::MasterPreferences
prefs(kCreateAllShortcutsFalsePrefs
);
350 bool do_not_create_desktop_shortcut
= false;
351 bool do_not_create_quick_launch_shortcut
= false;
352 bool do_not_create_taskbar_shortcut
= false;
353 bool do_not_create_start_pin
= false;
355 installer::master_preferences::kDoNotCreateDesktopShortcut
,
356 &do_not_create_desktop_shortcut
);
358 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
359 &do_not_create_quick_launch_shortcut
);
361 installer::master_preferences::kDoNotCreateTaskbarShortcut
,
362 &do_not_create_taskbar_shortcut
);
364 installer::master_preferences::kDoNotCreateStartPin
,
365 &do_not_create_start_pin
);
366 // create_all_shortcuts is a legacy preference that should only enforce
367 // do_not_create_desktop_shortcut and do_not_create_quick_launch_shortcut
368 // when set to false.
369 EXPECT_TRUE(do_not_create_desktop_shortcut
);
370 EXPECT_TRUE(do_not_create_quick_launch_shortcut
);
371 EXPECT_FALSE(do_not_create_taskbar_shortcut
);
372 EXPECT_FALSE(do_not_create_start_pin
);
375 TEST_F(MasterPreferencesTest
, DontEnforceLegacyCreateAllShortcutsTrue
) {
376 static const char kCreateAllShortcutsFalsePrefs
[] =
378 " \"distribution\": {"
379 " \"create_all_shortcuts\": true"
383 installer::MasterPreferences
prefs(kCreateAllShortcutsFalsePrefs
);
385 bool do_not_create_desktop_shortcut
= false;
386 bool do_not_create_quick_launch_shortcut
= false;
387 bool do_not_create_taskbar_shortcut
= false;
388 bool do_not_create_start_pin
= false;
390 installer::master_preferences::kDoNotCreateDesktopShortcut
,
391 &do_not_create_desktop_shortcut
);
393 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
394 &do_not_create_quick_launch_shortcut
);
396 installer::master_preferences::kDoNotCreateTaskbarShortcut
,
397 &do_not_create_taskbar_shortcut
);
399 installer::master_preferences::kDoNotCreateStartPin
,
400 &do_not_create_start_pin
);
401 EXPECT_FALSE(do_not_create_desktop_shortcut
);
402 EXPECT_FALSE(do_not_create_quick_launch_shortcut
);
403 EXPECT_FALSE(do_not_create_taskbar_shortcut
);
404 EXPECT_FALSE(do_not_create_start_pin
);
407 TEST_F(MasterPreferencesTest
, DontEnforceLegacyCreateAllShortcutsNotSpecified
) {
408 static const char kCreateAllShortcutsFalsePrefs
[] =
410 " \"distribution\": {"
411 " \"some_other_pref\": true"
415 installer::MasterPreferences
prefs(kCreateAllShortcutsFalsePrefs
);
417 bool do_not_create_desktop_shortcut
= false;
418 bool do_not_create_quick_launch_shortcut
= false;
419 bool do_not_create_taskbar_shortcut
= false;
420 bool do_not_create_start_pin
= false;
422 installer::master_preferences::kDoNotCreateDesktopShortcut
,
423 &do_not_create_desktop_shortcut
);
425 installer::master_preferences::kDoNotCreateQuickLaunchShortcut
,
426 &do_not_create_quick_launch_shortcut
);
428 installer::master_preferences::kDoNotCreateTaskbarShortcut
,
429 &do_not_create_taskbar_shortcut
);
431 installer::master_preferences::kDoNotCreateStartPin
,
432 &do_not_create_start_pin
);
433 EXPECT_FALSE(do_not_create_desktop_shortcut
);
434 EXPECT_FALSE(do_not_create_quick_launch_shortcut
);
435 EXPECT_FALSE(do_not_create_taskbar_shortcut
);
436 EXPECT_FALSE(do_not_create_start_pin
);
439 TEST_F(MasterPreferencesTest
, MigrateOldStartupUrlsPref
) {
440 static const char kOldMasterPrefs
[] =
442 " \"distribution\": { \n"
443 " \"show_welcome_page\": true,\n"
444 " \"import_search_engine\": true,\n"
445 " \"import_history\": true,\n"
446 " \"import_bookmarks\": true\n"
449 " \"urls_to_restore_on_startup\": [\"http://www.google.com\"]\n"
453 const installer::MasterPreferences
prefs(kOldMasterPrefs
);
454 const base::DictionaryValue
& master_dictionary
=
455 prefs
.master_dictionary();
457 const base::ListValue
* old_startup_urls_list
= NULL
;
458 EXPECT_TRUE(master_dictionary
.GetList(prefs::kURLsToRestoreOnStartupOld
,
459 &old_startup_urls_list
));
460 EXPECT_TRUE(old_startup_urls_list
!= NULL
);
462 // The MasterPreferences dictionary should also conjure up the new setting
463 // as per EnforceLegacyPreferences.
464 const base::ListValue
* new_startup_urls_list
= NULL
;
465 EXPECT_TRUE(master_dictionary
.GetList(prefs::kURLsToRestoreOnStartup
,
466 &new_startup_urls_list
));
467 EXPECT_TRUE(new_startup_urls_list
!= NULL
);
470 TEST_F(MasterPreferencesTest
, DontMigrateOldStartupUrlsPrefWhenNewExists
) {
471 static const char kOldAndNewMasterPrefs
[] =
473 " \"distribution\": { \n"
474 " \"show_welcome_page\": true,\n"
475 " \"import_search_engine\": true,\n"
476 " \"import_history\": true,\n"
477 " \"import_bookmarks\": true\n"
480 " \"urls_to_restore_on_startup\": [\"http://www.google.com\"],\n"
481 " \"startup_urls\": [\"http://www.example.com\"]\n"
485 const installer::MasterPreferences
prefs(kOldAndNewMasterPrefs
);
486 const base::DictionaryValue
& master_dictionary
=
487 prefs
.master_dictionary();
489 const base::ListValue
* old_startup_urls_list
= NULL
;
490 EXPECT_TRUE(master_dictionary
.GetList(prefs::kURLsToRestoreOnStartupOld
,
491 &old_startup_urls_list
));
492 ASSERT_TRUE(old_startup_urls_list
!= NULL
);
493 std::string url_value
;
494 EXPECT_TRUE(old_startup_urls_list
->GetString(0, &url_value
));
495 EXPECT_EQ("http://www.google.com", url_value
);
497 // The MasterPreferences dictionary should also conjure up the new setting
498 // as per EnforceLegacyPreferences.
499 const base::ListValue
* new_startup_urls_list
= NULL
;
500 EXPECT_TRUE(master_dictionary
.GetList(prefs::kURLsToRestoreOnStartup
,
501 &new_startup_urls_list
));
502 ASSERT_TRUE(new_startup_urls_list
!= NULL
);
503 std::string new_url_value
;
504 EXPECT_TRUE(new_startup_urls_list
->GetString(0, &new_url_value
));
505 EXPECT_EQ("http://www.example.com", new_url_value
);