Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / installer / util / master_preferences_unittest.cc
blobf077176a9c11d716d8bca543ad6331d01bcaa943
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.
4 //
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"
19 namespace {
20 class MasterPreferencesTest : public testing::Test {
21 protected:
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_; }
32 private:
33 base::FilePath prefs_file_;
36 // Used to specify an expected value for a set boolean preference variable.
37 struct ExpectedBooleans {
38 const char* name;
39 bool expected_value;
42 } // namespace
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) {
51 const char text[] =
52 "{ \n"
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"
75 " },\n"
76 " \"blah\": {\n"
77 " \"import_history\": false\n"
78 " }\n"
79 "} \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) {
107 bool value = false;
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,
115 &str_value));
116 EXPECT_STREQ("c:\\foo", str_value.c_str());
118 int icon_index = 0;
119 EXPECT_TRUE(prefs.GetInt(
120 installer::master_preferences::kChromeShortcutIconIndex,
121 &icon_index));
122 EXPECT_EQ(icon_index, 1);
123 int ping_delay = 90;
124 EXPECT_TRUE(prefs.GetInt(installer::master_preferences::kDistroPingDelay,
125 &ping_delay));
126 EXPECT_EQ(ping_delay, 40);
129 TEST_F(MasterPreferencesTest, ParseMissingDistroParams) {
130 const char text[] =
131 "{ \n"
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"
140 " }\n"
141 "} \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 },
156 bool value = false;
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,
177 &str_value));
179 int icon_index = 0;
180 EXPECT_FALSE(prefs.GetInt(
181 installer::master_preferences::kChromeShortcutIconIndex,
182 &icon_index));
183 EXPECT_EQ(icon_index, 0);
185 int ping_delay = 90;
186 EXPECT_FALSE(prefs.GetInt(
187 installer::master_preferences::kDistroPingDelay, &ping_delay));
188 EXPECT_EQ(ping_delay, 90);
191 TEST_F(MasterPreferencesTest, FirstRunTabs) {
192 const char text[] =
193 "{ \n"
194 " \"distribution\": { \n"
195 " \"something here\": true\n"
196 " },\n"
197 " \"first_run_tabs\": [\n"
198 " \"http://google.com/f1\",\n"
199 " \"https://google.com/f2\",\n"
200 " \"new_tab_page\"\n"
201 " ]\n"
202 "} \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));
228 int location = 0;
229 EXPECT_TRUE(extensions->GetInteger(
230 "behllobkkfkfnphdnhnkndlbkcpglgmj.location", &location));
231 int state = 0;
232 EXPECT_TRUE(extensions->GetInteger(
233 "behllobkkfkfnphdnhnkndlbkcpglgmj.state", &state));
234 std::string path;
235 EXPECT_TRUE(extensions->GetString(
236 "behllobkkfkfnphdnhnkndlbkcpglgmj.path", &path));
237 std::string key;
238 EXPECT_TRUE(extensions->GetString(
239 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.key", &key));
240 std::string name;
241 EXPECT_TRUE(extensions->GetString(
242 "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.name", &name));
243 std::string version;
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));
253 const char text[] =
254 "{ \n"
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"
261 " }\n"
262 "} \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.
281 bool value = false;
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
291 // prefs.
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"
345 " }"
346 "}";
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;
354 prefs.GetBool(
355 installer::master_preferences::kDoNotCreateDesktopShortcut,
356 &do_not_create_desktop_shortcut);
357 prefs.GetBool(
358 installer::master_preferences::kDoNotCreateQuickLaunchShortcut,
359 &do_not_create_quick_launch_shortcut);
360 prefs.GetBool(
361 installer::master_preferences::kDoNotCreateTaskbarShortcut,
362 &do_not_create_taskbar_shortcut);
363 prefs.GetBool(
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"
380 " }"
381 "}";
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;
389 prefs.GetBool(
390 installer::master_preferences::kDoNotCreateDesktopShortcut,
391 &do_not_create_desktop_shortcut);
392 prefs.GetBool(
393 installer::master_preferences::kDoNotCreateQuickLaunchShortcut,
394 &do_not_create_quick_launch_shortcut);
395 prefs.GetBool(
396 installer::master_preferences::kDoNotCreateTaskbarShortcut,
397 &do_not_create_taskbar_shortcut);
398 prefs.GetBool(
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"
412 " }"
413 "}";
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;
421 prefs.GetBool(
422 installer::master_preferences::kDoNotCreateDesktopShortcut,
423 &do_not_create_desktop_shortcut);
424 prefs.GetBool(
425 installer::master_preferences::kDoNotCreateQuickLaunchShortcut,
426 &do_not_create_quick_launch_shortcut);
427 prefs.GetBool(
428 installer::master_preferences::kDoNotCreateTaskbarShortcut,
429 &do_not_create_taskbar_shortcut);
430 prefs.GetBool(
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[] =
441 "{ \n"
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"
447 " },\n"
448 " \"session\": {\n"
449 " \"urls_to_restore_on_startup\": [\"http://www.google.com\"]\n"
450 " }\n"
451 "} \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[] =
472 "{ \n"
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"
478 " },\n"
479 " \"session\": {\n"
480 " \"urls_to_restore_on_startup\": [\"http://www.google.com\"],\n"
481 " \"startup_urls\": [\"http://www.example.com\"]\n"
482 " }\n"
483 "} \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);