1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_upgrade_backup.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,49 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://gre/modules/Services.jsm", this); 1.8 +Cu.import("resource://gre/modules/osfile.jsm", this); 1.9 +Cu.import("resource://gre/modules/Task.jsm", this); 1.10 + 1.11 +function test() { 1.12 + waitForExplicitFinish(); 1.13 + 1.14 + Task.spawn(function task() { 1.15 + try { 1.16 + // Wait until initialization is complete 1.17 + yield SessionStore.promiseInitialized; 1.18 + 1.19 + const PREF_UPGRADE = "browser.sessionstore.upgradeBackup.latestBuildID"; 1.20 + let buildID = Services.appinfo.platformBuildID; 1.21 + 1.22 + // Write state once before starting the test to 1.23 + // ensure sessionstore.js writes won't happen in between. 1.24 + yield forceSaveState(); 1.25 + 1.26 + // Force backup to take place with a file decided by us 1.27 + Services.prefs.setCharPref(PREF_UPGRADE, ""); 1.28 + let contents = "browser_upgrade_backup.js"; 1.29 + let pathStore = OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.js"); 1.30 + yield OS.File.writeAtomic(pathStore, contents, { tmpPath: pathStore + ".tmp" }); 1.31 + yield SessionStore._internal._performUpgradeBackup(); 1.32 + is(Services.prefs.getCharPref(PREF_UPGRADE), buildID, "upgrade backup should be set (again)"); 1.33 + 1.34 + let pathBackup = OS.Path.join(OS.Constants.Path.profileDir, "sessionstore.bak-" + Services.appinfo.platformBuildID); 1.35 + is((yield OS.File.exists(pathBackup)), true, "upgrade backup file has been created"); 1.36 + 1.37 + let data = yield OS.File.read(pathBackup); 1.38 + is(new TextDecoder().decode(data), contents, "upgrade backup contains the expected contents"); 1.39 + 1.40 + // Ensure that we don't re-backup by accident 1.41 + yield OS.File.writeAtomic(pathStore, "something else entirely", { tmpPath: pathStore + ".tmp" }); 1.42 + yield SessionStore._internal._performUpgradeBackup(); 1.43 + data = yield OS.File.read(pathBackup); 1.44 + is(new TextDecoder().decode(data), contents, "upgrade backup hasn't changed"); 1.45 + 1.46 + } catch (ex) { 1.47 + ok(false, "Uncaught error: " + ex + " at " + ex.stack); 1.48 + } finally { 1.49 + finish(); 1.50 + } 1.51 + }); 1.52 +}