test_browserGlue_distribution.js (4120B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /** 5 * Tests that distribution correctly imports bookmarks from distribution.ini. 6 */ 7 8 const PREF_BMPROCESSED = "distribution.516444.bookmarksProcessed"; 9 const PREF_DISTRIBUTION_ID = "distribution.id"; 10 11 const TOPIC_CUSTOMIZATION_COMPLETE = "distribution-customization-complete"; 12 13 function run_test() { 14 // Set special pref to load distribution.ini from the profile folder. 15 Services.prefs.setBoolPref("distribution.testing.loadFromProfile", true); 16 17 // Copy distribution.ini file to the profile dir. 18 let distroDir = gProfD.clone(); 19 distroDir.leafName = "distribution"; 20 let iniFile = distroDir.clone(); 21 iniFile.append("distribution.ini"); 22 if (iniFile.exists()) { 23 iniFile.remove(false); 24 print("distribution.ini already exists, did some test forget to cleanup?"); 25 } 26 27 let testDistributionFile = gTestDir.clone(); 28 testDistributionFile.append("distribution.ini"); 29 testDistributionFile.copyTo(distroDir, "distribution.ini"); 30 Assert.ok(testDistributionFile.exists()); 31 32 run_next_test(); 33 } 34 35 registerCleanupFunction(function () { 36 // Remove the distribution file, even if the test failed, otherwise all 37 // next tests will import it. 38 let iniFile = gProfD.clone(); 39 iniFile.leafName = "distribution"; 40 iniFile.append("distribution.ini"); 41 if (iniFile.exists()) { 42 iniFile.remove(false); 43 } 44 Assert.ok(!iniFile.exists()); 45 }); 46 47 add_task(async function () { 48 let { DistributionManagement } = ChromeUtils.importESModule( 49 "resource:///modules/distribution.sys.mjs" 50 ); 51 52 // Ensure browser glue is running so it notices places initializing. 53 Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver); 54 55 // Initialize Places through the History Service and check that a new 56 // database has been created. 57 Assert.equal( 58 PlacesUtils.history.databaseStatus, 59 PlacesUtils.history.DATABASE_STATUS_CREATE 60 ); 61 // Force distribution. 62 DistributionManagement.applyCustomizations(); 63 64 // Test will continue on customization complete notification. 65 await promiseTopicObserved(TOPIC_CUSTOMIZATION_COMPLETE); 66 67 // Check the custom bookmarks exist on menu. 68 let menuItem = await PlacesUtils.bookmarks.fetch({ 69 parentGuid: PlacesUtils.bookmarks.menuGuid, 70 index: 0, 71 }); 72 Assert.equal(menuItem.title, "Menu Link Before"); 73 Assert.ok( 74 menuItem.guid.startsWith(DistributionManagement.BOOKMARK_GUID_PREFIX), 75 "Guid of this bookmark has expected prefix" 76 ); 77 78 menuItem = await PlacesUtils.bookmarks.fetch({ 79 parentGuid: PlacesUtils.bookmarks.menuGuid, 80 index: 1 + DEFAULT_BOOKMARKS_ON_MENU, 81 }); 82 Assert.equal(menuItem.title, "Menu Link After"); 83 84 // Check no favicon exists for this bookmark 85 let favicon = await PlacesUtils.favicons.getFaviconForPage(menuItem.url.URI); 86 Assert.equal(favicon, null, "Favicon should not be found"); 87 88 // Check the custom bookmarks exist on toolbar. 89 let toolbarItem = await PlacesUtils.bookmarks.fetch({ 90 parentGuid: PlacesUtils.bookmarks.toolbarGuid, 91 index: 0, 92 }); 93 Assert.equal(toolbarItem.title, "Toolbar Link Before"); 94 95 // Check the custom favicon exist for this bookmark 96 favicon = await PlacesUtils.favicons.getFaviconForPage(toolbarItem.url.URI); 97 Assert.ok(favicon, "Favicon should be found"); 98 Assert.equal(favicon.uri.spec, "https://example.org/favicon.png"); 99 Assert.greater(favicon.rawData.length, 0); 100 Assert.equal(favicon.mimeType, "image/png"); 101 Assert.equal(favicon.dataURI.spec, SMALLPNG_DATA_URI.spec); 102 103 toolbarItem = await PlacesUtils.bookmarks.fetch({ 104 parentGuid: PlacesUtils.bookmarks.toolbarGuid, 105 index: 1, 106 }); 107 Assert.equal(toolbarItem.title, "Toolbar Folder After"); 108 Assert.ok( 109 toolbarItem.guid.startsWith(DistributionManagement.FOLDER_GUID_PREFIX), 110 "Guid of this folder has expected prefix" 111 ); 112 113 // Check the bmprocessed pref has been created. 114 Assert.ok(Services.prefs.getBoolPref(PREF_BMPROCESSED)); 115 116 // Check distribution prefs have been created. 117 Assert.equal(Services.prefs.getCharPref(PREF_DISTRIBUTION_ID), "516444"); 118 });