master_preferences_unittest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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.
6
7#include "base/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"
18
19namespace {
20class MasterPreferencesTest : public testing::Test {
21 protected:
22  virtual void SetUp() {
23    ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file_));
24  }
25
26  virtual void TearDown() {
27    EXPECT_TRUE(base::DeleteFile(prefs_file_, false));
28  }
29
30  const base::FilePath& prefs_file() const { return prefs_file_; }
31
32 private:
33  base::FilePath prefs_file_;
34};
35
36// Used to specify an expected value for a set boolean preference variable.
37struct ExpectedBooleans {
38  const char* name;
39  bool expected_value;
40};
41
42}  // namespace
43
44TEST_F(MasterPreferencesTest, NoFileToParse) {
45  EXPECT_TRUE(base::DeleteFile(prefs_file(), false));
46  installer::MasterPreferences prefs(prefs_file());
47  EXPECT_FALSE(prefs.read_from_file());
48}
49
50TEST_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    "     \"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"
73    "  },\n"
74    "  \"blah\": {\n"
75    "     \"import_history\": false\n"
76    "  }\n"
77    "} \n";
78
79  EXPECT_TRUE(file_util::WriteFile(prefs_file(), text, strlen(text)));
80  installer::MasterPreferences prefs(prefs_file());
81  EXPECT_TRUE(prefs.read_from_file());
82
83  const char* 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,
99  };
100
101  for (int i = 0; i < arraysize(expected_true); ++i) {
102    bool value = false;
103    EXPECT_TRUE(prefs.GetBool(expected_true[i], &value));
104    EXPECT_TRUE(value) << expected_true[i];
105  }
106
107  std::string str_value;
108  EXPECT_TRUE(prefs.GetString(
109      installer::master_preferences::kDistroImportBookmarksFromFilePref,
110      &str_value));
111  EXPECT_STREQ("c:\\foo", str_value.c_str());
112
113  int icon_index = 0;
114  EXPECT_TRUE(prefs.GetInt(
115      installer::master_preferences::kChromeShortcutIconIndex,
116      &icon_index));
117  EXPECT_EQ(icon_index, 1);
118  int ping_delay = 90;
119  EXPECT_TRUE(prefs.GetInt(installer::master_preferences::kDistroPingDelay,
120                           &ping_delay));
121  EXPECT_EQ(ping_delay, 40);
122}
123
124TEST_F(MasterPreferencesTest, ParseMissingDistroParams) {
125  const char text[] =
126    "{ \n"
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"
135    "  }\n"
136    "} \n";
137
138  EXPECT_TRUE(file_util::WriteFile(prefs_file(), text, strlen(text)));
139  installer::MasterPreferences prefs(prefs_file());
140  EXPECT_TRUE(prefs.read_from_file());
141
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 },
148  };
149
150  bool value = false;
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;
154  }
155
156  const char* missing_bools[] = {
157    installer::master_preferences::kDistroImportHomePagePref,
158    installer::master_preferences::kDoNotRegisterForUpdateLaunch,
159    installer::master_preferences::kMakeChromeDefault,
160    installer::master_preferences::kMakeChromeDefaultForUser,
161  };
162
163  for (int i = 0; i < arraysize(missing_bools); ++i) {
164    EXPECT_FALSE(prefs.GetBool(missing_bools[i], &value)) << missing_bools[i];
165  }
166
167  std::string str_value;
168  EXPECT_FALSE(prefs.GetString(
169      installer::master_preferences::kDistroImportBookmarksFromFilePref,
170      &str_value));
171
172  int icon_index = 0;
173  EXPECT_FALSE(prefs.GetInt(
174      installer::master_preferences::kChromeShortcutIconIndex,
175      &icon_index));
176  EXPECT_EQ(icon_index, 0);
177
178  int ping_delay = 90;
179  EXPECT_FALSE(prefs.GetInt(
180      installer::master_preferences::kDistroPingDelay, &ping_delay));
181  EXPECT_EQ(ping_delay, 90);
182}
183
184TEST_F(MasterPreferencesTest, FirstRunTabs) {
185  const char text[] =
186    "{ \n"
187    "  \"distribution\": { \n"
188    "     \"something here\": true\n"
189    "  },\n"
190    "  \"first_run_tabs\": [\n"
191    "     \"http://google.com/f1\",\n"
192    "     \"https://google.com/f2\",\n"
193    "     \"new_tab_page\"\n"
194    "  ]\n"
195    "} \n";
196
197  EXPECT_TRUE(file_util::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]);
205}
206
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.
211TEST(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");
216
217  installer::MasterPreferences prefs(prefs_path);
218  DictionaryValue* extensions = NULL;
219  EXPECT_TRUE(prefs.GetExtensionsBlock(&extensions));
220  int location = 0;
221  EXPECT_TRUE(extensions->GetInteger(
222      "behllobkkfkfnphdnhnkndlbkcpglgmj.location", &location));
223  int state = 0;
224  EXPECT_TRUE(extensions->GetInteger(
225      "behllobkkfkfnphdnhnkndlbkcpglgmj.state", &state));
226  std::string path;
227  EXPECT_TRUE(extensions->GetString(
228      "behllobkkfkfnphdnhnkndlbkcpglgmj.path", &path));
229  std::string key;
230  EXPECT_TRUE(extensions->GetString(
231      "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.key", &key));
232  std::string name;
233  EXPECT_TRUE(extensions->GetString(
234      "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.name", &name));
235  std::string version;
236  EXPECT_TRUE(extensions->GetString(
237      "behllobkkfkfnphdnhnkndlbkcpglgmj.manifest.version", &version));
238}
239
240// Test that we are parsing master preferences correctly.
241TEST_F(MasterPreferencesTest, GetInstallPreferencesTest) {
242  // Create a temporary prefs file.
243  base::FilePath prefs_file;
244  ASSERT_TRUE(base::CreateTemporaryFile(&prefs_file));
245  const char text[] =
246    "{ \n"
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"
253    "  }\n"
254    "} \n";
255  EXPECT_TRUE(file_util::WriteFile(prefs_file, text, strlen(text)));
256
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  CommandLine cmd_line = CommandLine::FromString(cmd_str);
262  installer::MasterPreferences prefs(cmd_line);
263
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 },
269  };
270
271  // Now check that prefs got merged correctly.
272  bool value = false;
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;
276  }
277
278  // Delete temporary prefs file.
279  EXPECT_TRUE(base::DeleteFile(prefs_file, false));
280
281  // Check that if master prefs doesn't exist, we can still parse the common
282  // prefs.
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 },
288  };
289
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;
294  }
295
296  EXPECT_FALSE(prefs2.GetBool(
297      installer::master_preferences::kSystemLevel, &value));
298  EXPECT_FALSE(prefs2.GetBool(
299      installer::master_preferences::kVerboseLogging, &value));
300}
301
302TEST_F(MasterPreferencesTest, TestDefaultInstallConfig) {
303  std::wstringstream chrome_cmd, cf_cmd;
304  chrome_cmd << "setup.exe";
305  cf_cmd << "setup.exe --" << installer::switches::kChromeFrame;
306
307  CommandLine chrome_install(CommandLine::FromString(chrome_cmd.str()));
308  CommandLine cf_install(CommandLine::FromString(cf_cmd.str()));
309
310  installer::MasterPreferences pref_chrome(chrome_install);
311  installer::MasterPreferences pref_cf(cf_install);
312
313  EXPECT_FALSE(pref_chrome.is_multi_install());
314  EXPECT_TRUE(pref_chrome.install_chrome());
315  EXPECT_FALSE(pref_chrome.install_chrome_frame());
316
317  EXPECT_FALSE(pref_cf.is_multi_install());
318  EXPECT_FALSE(pref_cf.install_chrome());
319  EXPECT_TRUE(pref_cf.install_chrome_frame());
320}
321
322TEST_F(MasterPreferencesTest, TestMultiInstallConfig) {
323  using installer::switches::kMultiInstall;
324  using installer::switches::kChrome;
325  using installer::switches::kChromeFrame;
326
327  std::wstringstream chrome_cmd, cf_cmd, chrome_cf_cmd;
328  chrome_cmd << "setup.exe --" << kMultiInstall << " --" << kChrome;
329  cf_cmd << "setup.exe --" << kMultiInstall << " --" << kChromeFrame;
330  chrome_cf_cmd << "setup.exe --" << kMultiInstall << " --" << kChrome <<
331      " --" << kChromeFrame;
332
333  CommandLine chrome_install(CommandLine::FromString(chrome_cmd.str()));
334  CommandLine cf_install(CommandLine::FromString(cf_cmd.str()));
335  CommandLine chrome_cf_install(CommandLine::FromString(chrome_cf_cmd.str()));
336
337  installer::MasterPreferences pref_chrome(chrome_install);
338  installer::MasterPreferences pref_cf(cf_install);
339  installer::MasterPreferences pref_chrome_cf(chrome_cf_install);
340
341  EXPECT_TRUE(pref_chrome.is_multi_install());
342  EXPECT_TRUE(pref_chrome.install_chrome());
343  EXPECT_FALSE(pref_chrome.install_chrome_frame());
344
345  EXPECT_TRUE(pref_cf.is_multi_install());
346  EXPECT_FALSE(pref_cf.install_chrome());
347  EXPECT_TRUE(pref_cf.install_chrome_frame());
348
349  EXPECT_TRUE(pref_chrome_cf.is_multi_install());
350  EXPECT_TRUE(pref_chrome_cf.install_chrome());
351  EXPECT_TRUE(pref_chrome_cf.install_chrome_frame());
352}
353
354TEST_F(MasterPreferencesTest, EnforceLegacyCreateAllShortcutsFalse) {
355  static const char kCreateAllShortcutsFalsePrefs[] =
356      "{"
357      "  \"distribution\": {"
358      "     \"create_all_shortcuts\": false"
359      "  }"
360      "}";
361
362    installer::MasterPreferences prefs(kCreateAllShortcutsFalsePrefs);
363
364    bool do_not_create_desktop_shortcut = false;
365    bool do_not_create_quick_launch_shortcut = false;
366    bool do_not_create_taskbar_shortcut = false;
367    prefs.GetBool(
368        installer::master_preferences::kDoNotCreateDesktopShortcut,
369        &do_not_create_desktop_shortcut);
370    prefs.GetBool(
371        installer::master_preferences::kDoNotCreateQuickLaunchShortcut,
372        &do_not_create_quick_launch_shortcut);
373    prefs.GetBool(
374        installer::master_preferences::kDoNotCreateTaskbarShortcut,
375        &do_not_create_taskbar_shortcut);
376    // create_all_shortcuts is a legacy preference that should only enforce
377    // do_not_create_desktop_shortcut and do_not_create_quick_launch_shortcut
378    // when set to false.
379    EXPECT_TRUE(do_not_create_desktop_shortcut);
380    EXPECT_TRUE(do_not_create_quick_launch_shortcut);
381    EXPECT_FALSE(do_not_create_taskbar_shortcut);
382}
383
384TEST_F(MasterPreferencesTest, DontEnforceLegacyCreateAllShortcutsTrue) {
385  static const char kCreateAllShortcutsFalsePrefs[] =
386      "{"
387      "  \"distribution\": {"
388      "     \"create_all_shortcuts\": true"
389      "  }"
390      "}";
391
392    installer::MasterPreferences prefs(kCreateAllShortcutsFalsePrefs);
393
394    bool do_not_create_desktop_shortcut = false;
395    bool do_not_create_quick_launch_shortcut = false;
396    bool do_not_create_taskbar_shortcut = false;
397    prefs.GetBool(
398        installer::master_preferences::kDoNotCreateDesktopShortcut,
399        &do_not_create_desktop_shortcut);
400    prefs.GetBool(
401        installer::master_preferences::kDoNotCreateQuickLaunchShortcut,
402        &do_not_create_quick_launch_shortcut);
403    prefs.GetBool(
404        installer::master_preferences::kDoNotCreateTaskbarShortcut,
405        &do_not_create_taskbar_shortcut);
406    EXPECT_FALSE(do_not_create_desktop_shortcut);
407    EXPECT_FALSE(do_not_create_quick_launch_shortcut);
408    EXPECT_FALSE(do_not_create_taskbar_shortcut);
409}
410
411TEST_F(MasterPreferencesTest, DontEnforceLegacyCreateAllShortcutsNotSpecified) {
412  static const char kCreateAllShortcutsFalsePrefs[] =
413      "{"
414      "  \"distribution\": {"
415      "     \"some_other_pref\": true"
416      "  }"
417      "}";
418
419    installer::MasterPreferences prefs(kCreateAllShortcutsFalsePrefs);
420
421    bool do_not_create_desktop_shortcut = false;
422    bool do_not_create_quick_launch_shortcut = false;
423    bool do_not_create_taskbar_shortcut = false;
424    prefs.GetBool(
425        installer::master_preferences::kDoNotCreateDesktopShortcut,
426        &do_not_create_desktop_shortcut);
427    prefs.GetBool(
428        installer::master_preferences::kDoNotCreateQuickLaunchShortcut,
429        &do_not_create_quick_launch_shortcut);
430    prefs.GetBool(
431        installer::master_preferences::kDoNotCreateTaskbarShortcut,
432        &do_not_create_taskbar_shortcut);
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}
437
438TEST_F(MasterPreferencesTest, MigrateOldStartupUrlsPref) {
439  static const char kOldMasterPrefs[] =
440      "{ \n"
441      "  \"distribution\": { \n"
442      "     \"show_welcome_page\": true,\n"
443      "     \"import_search_engine\": true,\n"
444      "     \"import_history\": true,\n"
445      "     \"import_bookmarks\": true\n"
446      "  },\n"
447      "  \"session\": {\n"
448      "     \"urls_to_restore_on_startup\": [\"http://www.google.com\"]\n"
449      "  }\n"
450      "} \n";
451
452  const installer::MasterPreferences prefs(kOldMasterPrefs);
453  const base::DictionaryValue& master_dictionary =
454      prefs.master_dictionary();
455
456  const base::ListValue* old_startup_urls_list = NULL;
457  EXPECT_TRUE(master_dictionary.GetList(prefs::kURLsToRestoreOnStartupOld,
458                                        &old_startup_urls_list));
459  EXPECT_TRUE(old_startup_urls_list != NULL);
460
461  // The MasterPreferences dictionary should also conjure up the new setting
462  // as per EnforceLegacyPreferences.
463  const base::ListValue* new_startup_urls_list = NULL;
464  EXPECT_TRUE(master_dictionary.GetList(prefs::kURLsToRestoreOnStartup,
465                                        &new_startup_urls_list));
466  EXPECT_TRUE(new_startup_urls_list != NULL);
467}
468
469TEST_F(MasterPreferencesTest, DontMigrateOldStartupUrlsPrefWhenNewExists) {
470  static const char kOldAndNewMasterPrefs[] =
471      "{ \n"
472      "  \"distribution\": { \n"
473      "     \"show_welcome_page\": true,\n"
474      "     \"import_search_engine\": true,\n"
475      "     \"import_history\": true,\n"
476      "     \"import_bookmarks\": true\n"
477      "  },\n"
478      "  \"session\": {\n"
479      "     \"urls_to_restore_on_startup\": [\"http://www.google.com\"],\n"
480      "     \"startup_urls\": [\"http://www.example.com\"]\n"
481      "  }\n"
482      "} \n";
483
484  const installer::MasterPreferences prefs(kOldAndNewMasterPrefs);
485  const base::DictionaryValue& master_dictionary =
486      prefs.master_dictionary();
487
488  const base::ListValue* old_startup_urls_list = NULL;
489  EXPECT_TRUE(master_dictionary.GetList(prefs::kURLsToRestoreOnStartupOld,
490                                        &old_startup_urls_list));
491  ASSERT_TRUE(old_startup_urls_list != NULL);
492  std::string url_value;
493  EXPECT_TRUE(old_startup_urls_list->GetString(0, &url_value));
494  EXPECT_EQ("http://www.google.com", url_value);
495
496  // The MasterPreferences dictionary should also conjure up the new setting
497  // as per EnforceLegacyPreferences.
498  const base::ListValue* new_startup_urls_list = NULL;
499  EXPECT_TRUE(master_dictionary.GetList(prefs::kURLsToRestoreOnStartup,
500                                        &new_startup_urls_list));
501  ASSERT_TRUE(new_startup_urls_list != NULL);
502  std::string new_url_value;
503  EXPECT_TRUE(new_startup_urls_list->GetString(0, &new_url_value));
504  EXPECT_EQ("http://www.example.com", new_url_value);
505}
506