diff --git a/browser/components/migration/tests/unit/head_migration.js b/browser/components/migration/tests/unit/head_migration.js index 740ad1a95a3..2f84eac2bb0 100644 --- a/browser/components/migration/tests/unit/head_migration.js +++ b/browser/components/migration/tests/unit/head_migration.js @@ -5,20 +5,60 @@ const Cc = Components.classes; const Ci = Components.interfaces; const Cu = Components.utils; - -const IMIGRATOR = Ci.nsIBrowserProfileMigrator; +const Cr = Components.results; Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils", "resource://gre/modules/PlacesUtils.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "FileUtils", "resource://gre/modules/FileUtils.jsm"); - +XPCOMUtils.defineLazyModuleGetter(this, "MigrationUtils", + "resource:///modules/MigrationUtils.jsm"); // Initialize profile. let gProfD = do_get_profile(); -function newMigratorFor(aKey) { - let cid = "@mozilla.org/profile/migrator;1?app=browser&type=" + aKey; - return Cc[cid].createInstance(Ci.nsIBrowserProfileMigrator); +// Create a fake XULAppInfo to satisfy the eventual needs of the migrators. +let (XULAppInfo = { + // nsIXUlAppInfo + get vendor() "Mozilla", + get name() "XPCShell", + get ID() "xpcshell@tests.mozilla.org", + get version() "1", + get appBuildID() "2007010101", + get platformVersion() "1.0", + get platformBuildID() "2007010101", + + // nsIXUlRuntime (partial) + get inSafeMode() false, + logConsoleErrors: true, + get OS() "XPCShell", + get XPCOMABI() "noarch-spidermonkey", + invalidateCachesOnRestart: function () {}, + + // nsIWinAppHelper + get userCanElevate() false, + + QueryInterface: function (aIID) { + let interfaces = [Ci.nsIXULAppInfo, Ci.nsIXULRuntime]; + if ("nsIWinAppHelper" in Ci) + interfaces.push(Ci.nsIWinAppHelper); + if (!interfaces.some(function (v) aIID.equals(v))) + throw Cr.NS_ERROR_NO_INTERFACE; + return this; + } +}) { + const CONTRACT_ID = "@mozilla.org/xre/app-info;1"; + const CID = Components.ID("7685dac8-3637-4660-a544-928c5ec0e714}"); + + let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); + registrar.registerFactory(CID, "XULAppInfo", CONTRACT_ID, { + createInstance: function (aOuter, aIID) { + if (aOuter != null) + throw Cr.NS_ERROR_NO_AGGREGATION; + return XULAppInfo.QueryInterface(aIID); + }, + QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory) + }); } diff --git a/browser/components/migration/tests/unit/test_IE_bookmarks.js b/browser/components/migration/tests/unit/test_IE_bookmarks.js index 6a3fd8a9b9b..407c1f3a2a6 100644 --- a/browser/components/migration/tests/unit/test_IE_bookmarks.js +++ b/browser/components/migration/tests/unit/test_IE_bookmarks.js @@ -3,26 +3,55 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ function run_test() { - let migrator = newMigratorFor("ie"); + do_test_pending(); + + let migrator = MigrationUtils.getMigrator("ie"); // Sanity check for the source. do_check_true(migrator.sourceExists); // Ensure bookmarks migration is available. let availableSources = migrator.getMigrateData("FieldOfFlowers", false); - do_check_true((availableSources & IMIGRATOR.BOOKMARKS) > 0); + do_check_true((availableSources & MigrationUtils.resourceTypes.BOOKMARKS) > 0); - // Needed to enforce bookmarks replacing. - let startup = { - doStartup: function () {}, - get directory() do_get_profile() - } - migrator.migrate(IMIGRATOR.BOOKMARKS, startup, "FieldOfFlowers"); + // Wait for the imported bookmarks. Check that "From Internet Explorer" + // folders are created in the menu and on the toolbar. + let source = MigrationUtils.getLocalizedString("sourceNameIE"); + let label = MigrationUtils.getLocalizedString("importedBookmarksFolder", [source]); - // Check that at least two bookmark have been added to the menu and the - // toolbar. The first one comes from bookmarks.html, the others from IE. - do_check_true(PlacesUtils.bookmarks - .getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1) > 0); - do_check_true(PlacesUtils.bookmarks - .getIdForItemAt(PlacesUtils.toolbarFolderId, 1) > 0); + let expectedParents = [ PlacesUtils.bookmarksMenuFolderId, + PlacesUtils.toolbarFolderId ]; + + PlacesUtils.bookmarks.addObserver({ + onItemAdded: function onItemAdded(aItemId, aParentId, aIndex, aItemType, + aURI, aTitle) { + if (aTitle == label) { + let index = expectedParents.indexOf(aParentId); + do_check_neq(index, -1); + expectedParents.splice(index, 1); + if (expectedParents.length == 0) + PlacesUtils.bookmarks.removeObserver(this); + } + }, + onBeginUpdateBatch: function () {}, + onEndUpdateBatch: function () {}, + onBeforeItemRemoved: function () {}, + onItemRemoved: function () {}, + onItemChanged: function () {}, + onItemVisited: function () {}, + onItemMoved: function () {}, + }, false); + + // Wait for migration. + Services.obs.addObserver(function onMigrationEnded() { + Services.obs.removeObserver(onMigrationEnded, "Migration:Ended"); + + // Check the bookmarks have been imported to all the expected parents. + do_check_eq(expectedParents.length, 0); + + do_test_finished(); + }, "Migration:Ended", false); + + migrator.migrate(MigrationUtils.resourceTypes.BOOKMARKS, null, + "FieldOfFlowers"); } diff --git a/browser/components/migration/tests/unit/xpcshell.ini b/browser/components/migration/tests/unit/xpcshell.ini index 04d272e8f2a..2704bc2cfbc 100644 --- a/browser/components/migration/tests/unit/xpcshell.ini +++ b/browser/components/migration/tests/unit/xpcshell.ini @@ -3,4 +3,4 @@ head = head_migration.js tail = [test_IE_bookmarks.js] -skip-if = true +skip-if = os != "win"