michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: */ michael@0: michael@0: const PREF_APP_UPDATE_MIGRATE_APP_DIR = "app.update.migrated.updateDir"; michael@0: michael@0: function clearTaskbarIDHash(exePath, appInfoName) { michael@0: let registry = AUS_Cc["@mozilla.org/windows-registry-key;1"]. michael@0: createInstance(AUS_Ci.nsIWindowsRegKey); michael@0: try { michael@0: registry.open(AUS_Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, michael@0: "Software\\Mozilla\\" + appInfoName + "\\TaskBarIDs", michael@0: AUS_Ci.nsIWindowsRegKey.ACCESS_ALL); michael@0: registry.removeValue(exePath); michael@0: } catch (e) { michael@0: } michael@0: finally { michael@0: registry.close(); michael@0: } michael@0: } michael@0: michael@0: function setTaskbarIDHash(exePath, hash, appInfoName) { michael@0: let registry = AUS_Cc["@mozilla.org/windows-registry-key;1"]. michael@0: createInstance(AUS_Ci.nsIWindowsRegKey); michael@0: try { michael@0: registry.create(AUS_Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, michael@0: "Software\\Mozilla\\" + appInfoName + "\\TaskBarIDs", michael@0: AUS_Ci.nsIWindowsRegKey.ACCESS_WRITE); michael@0: registry.writeStringValue(exePath, hash); michael@0: } catch (e) { michael@0: } michael@0: finally { michael@0: registry.close(); michael@0: } michael@0: }; michael@0: michael@0: function getMigrated() { michael@0: var migrated = 0; michael@0: try { michael@0: migrated = Services.prefs.getBoolPref(PREF_APP_UPDATE_MIGRATE_APP_DIR); michael@0: } catch (e) { michael@0: } michael@0: return migrated; michael@0: } michael@0: michael@0: /* General Update Manager Tests */ michael@0: michael@0: function run_test() { michael@0: setupTestCommon(); michael@0: michael@0: standardInit(); michael@0: michael@0: var appinfo = AUS_Cc["@mozilla.org/xre/app-info;1"]. michael@0: getService(AUS_Ci.nsIXULAppInfo). michael@0: QueryInterface(AUS_Ci.nsIXULRuntime); michael@0: michael@0: // Obtain the old update root leaf michael@0: var updateLeafName; michael@0: var exeFile = FileUtils.getFile(XRE_EXECUTABLE_FILE, []); michael@0: var programFiles = FileUtils.getFile("ProgF", []); michael@0: if (exeFile.path.substring(0, programFiles.path.length).toLowerCase() == michael@0: programFiles.path.toLowerCase()) { michael@0: updateLeafName = exeFile.parent.leafName; michael@0: } else { michael@0: updateLeafName = appinfo.name; michael@0: } michael@0: michael@0: // Obtain the old update root michael@0: var oldUpdateRoot; michael@0: if (appinfo.vendor) { michael@0: oldUpdateRoot = FileUtils.getDir("LocalAppData", [appinfo.vendor, michael@0: appinfo.name, michael@0: updateLeafName], false); michael@0: } else { michael@0: oldUpdateRoot = FileUtils.getDir("LocalAppData", [appinfo.name, michael@0: updateLeafName], false); michael@0: } michael@0: // Obtain the new update root michael@0: var newUpdateRoot = FileUtils.getDir("UpdRootD", [], false); michael@0: michael@0: /////////////////////////////////////////////////////////// michael@0: // Setting a taskbar ID without the old update dir existing should set the michael@0: // pref so a migration isn't retried. michael@0: michael@0: // Remove the old and new update root directories michael@0: try { michael@0: oldUpdateRoot.remove(true); michael@0: } catch (e) { michael@0: } michael@0: try { michael@0: newUpdateRoot.remove(true); michael@0: } catch (e) { michael@0: } michael@0: Services.prefs.setBoolPref(PREF_APP_UPDATE_MIGRATE_APP_DIR, false); michael@0: setTaskbarIDHash(exeFile.parent.path, "AAAAAAAA", appinfo.name); michael@0: initUpdateServiceStub(); michael@0: do_check_eq(getMigrated(), 1); michael@0: michael@0: /////////////////////////////////////////////////////////// michael@0: // An application without a taskbar ID should bail early without a pref michael@0: // being set, so that if a taskbar is set for the application, the migration michael@0: // will be retried. michael@0: michael@0: Services.prefs.setBoolPref(PREF_APP_UPDATE_MIGRATE_APP_DIR, false); michael@0: clearTaskbarIDHash(exeFile.parent.path, appinfo.name); michael@0: initUpdateServiceStub(); michael@0: do_check_eq(getMigrated(), 0); michael@0: michael@0: /////////////////////////////////////////////////////////// michael@0: // Migrating files should work michael@0: michael@0: Services.prefs.setBoolPref(PREF_APP_UPDATE_MIGRATE_APP_DIR, false); michael@0: setTaskbarIDHash(exeFile.parent.path, "AAAAAAAA", appinfo.name); michael@0: var oldUpdateDirs = oldUpdateRoot.clone(); michael@0: oldUpdateDirs.append("updates"); michael@0: oldUpdateDirs.append("0"); michael@0: oldUpdateDirs.create(AUS_Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY); michael@0: michael@0: // Get an array of all of the files we want to migrate. michael@0: // We do this to create them in the old update directory. michael@0: var filesToMigrate = [FILE_UPDATES_DB, FILE_UPDATE_ACTIVE, michael@0: ["updates", FILE_LAST_LOG], ["updates", FILE_BACKUP_LOG], michael@0: ["updates", "0", FILE_UPDATE_STATUS]]; michael@0: // Move each of those files to the new directory michael@0: filesToMigrate.forEach(relPath => { michael@0: let oldFile = oldUpdateRoot.clone(); michael@0: if (relPath instanceof Array) { michael@0: relPath.forEach(relPathPart => { michael@0: oldFile.append(relPathPart); michael@0: }); michael@0: } else { michael@0: oldFile.append(relPath); michael@0: } michael@0: oldFile.create(AUS_Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE); michael@0: }); michael@0: // Do the migration michael@0: initUpdateServiceStub(); michael@0: doTestFinish(); michael@0: return; michael@0: // Now verify that each of the files exist in the new update directory michael@0: filesToMigrate.forEach(relPath => { michael@0: let newFile = newUpdateRoot.clone(); michael@0: let oldFile = oldUpdateRoot.clone(); michael@0: if (relPath instanceof Array) { michael@0: relPath.forEach(relPathPart => { michael@0: newFile.append(relPathPart); michael@0: oldFile.append(relPathPart); michael@0: }); michael@0: } else { michael@0: newFile.append(relPath); michael@0: oldFile.append(relPath); michael@0: } michael@0: // The file should be mimgrated, except for FILE_UPDATE_STATUS michael@0: // which gets consumed by post update after it is migrated.. michael@0: if (newFile.leafName != FILE_UPDATE_STATUS) { michael@0: do_check_true(newFile.exists()); michael@0: } michael@0: do_check_false(oldFile.exists()); michael@0: }); michael@0: michael@0: doTestFinish(); michael@0: } michael@0: michael@0: function end_test() { michael@0: var appinfo = AUS_Cc["@mozilla.org/xre/app-info;1"]. michael@0: getService(AUS_Ci.nsIXULAppInfo). michael@0: QueryInterface(AUS_Ci.nsIXULRuntime); michael@0: var exeFile = FileUtils.getFile(XRE_EXECUTABLE_FILE, []); michael@0: clearTaskbarIDHash(exeFile.parent.path, appinfo.name); michael@0: }