1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/places/tests/unit/test_browserGlue_distribution.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * Tests that nsBrowserGlue correctly imports bookmarks from distribution.ini. 1.9 + */ 1.10 + 1.11 +const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion"; 1.12 +const PREF_BMPROCESSED = "distribution.516444.bookmarksProcessed"; 1.13 +const PREF_DISTRIBUTION_ID = "distribution.id"; 1.14 + 1.15 +const TOPICDATA_DISTRIBUTION_CUSTOMIZATION = "force-distribution-customization"; 1.16 +const TOPIC_CUSTOMIZATION_COMPLETE = "distribution-customization-complete"; 1.17 +const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; 1.18 + 1.19 +function run_test() 1.20 +{ 1.21 + do_test_pending(); 1.22 + 1.23 + // Set special pref to load distribution.ini from the profile folder. 1.24 + Services.prefs.setBoolPref("distribution.testing.loadFromProfile", true); 1.25 + // Copy distribution.ini file to the profile dir. 1.26 + let distroDir = gProfD.clone(); 1.27 + distroDir.leafName = "distribution"; 1.28 + let iniFile = distroDir.clone(); 1.29 + iniFile.append("distribution.ini"); 1.30 + if (iniFile.exists()) { 1.31 + iniFile.remove(false); 1.32 + print("distribution.ini already exists, did some test forget to cleanup?"); 1.33 + } 1.34 + let testDistributionFile = gTestDir.clone(); 1.35 + testDistributionFile.append("distribution.ini"); 1.36 + testDistributionFile.copyTo(distroDir, "distribution.ini"); 1.37 + do_check_true(testDistributionFile.exists()); 1.38 + 1.39 + // Disable Smart Bookmarks creation. 1.40 + Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1); 1.41 + 1.42 + // Initialize Places through the History Service and check that a new 1.43 + // database has been created. 1.44 + do_check_eq(PlacesUtils.history.databaseStatus, 1.45 + PlacesUtils.history.DATABASE_STATUS_CREATE); 1.46 + 1.47 + // Force distribution. 1.48 + Cc["@mozilla.org/browser/browserglue;1"]. 1.49 + getService(Ci.nsIObserver).observe(null, 1.50 + TOPIC_BROWSERGLUE_TEST, 1.51 + TOPICDATA_DISTRIBUTION_CUSTOMIZATION); 1.52 + 1.53 + // Test will continue on customization complete notification. 1.54 + Services.obs.addObserver(function(aSubject, aTopic, aData) { 1.55 + Services.obs.removeObserver(arguments.callee, 1.56 + TOPIC_CUSTOMIZATION_COMPLETE, 1.57 + false); 1.58 + do_execute_soon(onCustomizationComplete); 1.59 + }, TOPIC_CUSTOMIZATION_COMPLETE, false); 1.60 +} 1.61 + 1.62 +function onCustomizationComplete() 1.63 +{ 1.64 + // Check the custom bookmarks exist on menu. 1.65 + let menuItemId = 1.66 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); 1.67 + do_check_neq(menuItemId, -1); 1.68 + do_check_eq(PlacesUtils.bookmarks.getItemTitle(menuItemId), 1.69 + "Menu Link Before"); 1.70 + menuItemId = 1.71 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1.72 + 1 + DEFAULT_BOOKMARKS_ON_MENU); 1.73 + do_check_neq(menuItemId, -1); 1.74 + do_check_eq(PlacesUtils.bookmarks.getItemTitle(menuItemId), 1.75 + "Menu Link After"); 1.76 + 1.77 + // Check the custom bookmarks exist on toolbar. 1.78 + let toolbarItemId = 1.79 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.80 + do_check_neq(toolbarItemId, -1); 1.81 + do_check_eq(PlacesUtils.bookmarks.getItemTitle(toolbarItemId), 1.82 + "Toolbar Link Before"); 1.83 + toolbarItemId = 1.84 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 1.85 + 1 + DEFAULT_BOOKMARKS_ON_TOOLBAR); 1.86 + do_check_neq(toolbarItemId, -1); 1.87 + do_check_eq(PlacesUtils.bookmarks.getItemTitle(toolbarItemId), 1.88 + "Toolbar Link After"); 1.89 + 1.90 + // Check the bmprocessed pref has been created. 1.91 + do_check_true(Services.prefs.getBoolPref(PREF_BMPROCESSED)); 1.92 + 1.93 + // Check distribution prefs have been created. 1.94 + do_check_eq(Services.prefs.getCharPref(PREF_DISTRIBUTION_ID), "516444"); 1.95 + 1.96 + do_test_finished(); 1.97 +} 1.98 + 1.99 +do_register_cleanup(function() { 1.100 + // Remove the distribution file, even if the test failed, otherwise all 1.101 + // next tests will import it. 1.102 + let iniFile = gProfD.clone(); 1.103 + iniFile.leafName = "distribution"; 1.104 + iniFile.append("distribution.ini"); 1.105 + if (iniFile.exists()) 1.106 + iniFile.remove(false); 1.107 + do_check_false(iniFile.exists()); 1.108 +});