toolkit/components/places/tests/bookmarks/test_818584-discard-duplicate-backups.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/bookmarks/test_818584-discard-duplicate-backups.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,59 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + * Checks that automatically created bookmark backups are discarded if they are
    1.10 + * duplicate of an existing ones.
    1.11 + */
    1.12 +function run_test() {
    1.13 +  run_next_test();
    1.14 +}
    1.15 +
    1.16 +add_task(function() {
    1.17 +  // Create a backup for yesterday in the backups folder.
    1.18 +  let backupFolder = yield PlacesBackups.getBackupFolder();
    1.19 +  let dateObj = new Date();
    1.20 +  dateObj.setDate(dateObj.getDate() - 1);
    1.21 +  let oldBackupName = PlacesBackups.getFilenameForDate(dateObj);
    1.22 +  let oldBackup = OS.Path.join(backupFolder, oldBackupName);
    1.23 +  let {count: count, hash: hash} = yield BookmarkJSONUtils.exportToFile(oldBackup);
    1.24 +  do_check_true(count > 0);
    1.25 +  do_check_eq(hash.length, 24);
    1.26 +  oldBackupName = oldBackupName.replace(/\.json/, "_" + count + "_" + hash + ".json");
    1.27 +  yield OS.File.move(oldBackup, OS.Path.join(backupFolder, oldBackupName));
    1.28 +
    1.29 +  // Create a backup.
    1.30 +  // This should just rename the existing backup, so in the end there should be
    1.31 +  // only one backup with today's date.
    1.32 +  yield PlacesBackups.create();
    1.33 +
    1.34 +  // Get the hash of the generated backup
    1.35 +  let backupFiles = yield PlacesBackups.getBackupFiles();
    1.36 +  do_check_eq(backupFiles.length, 1);
    1.37 +  
    1.38 +  let matches = OS.Path.basename(backupFiles[0]).match(PlacesBackups.filenamesRegex);
    1.39 +  do_check_eq(matches[1], new Date().toLocaleFormat("%Y-%m-%d"));
    1.40 +  do_check_eq(matches[2], count);
    1.41 +  do_check_eq(matches[3], hash);
    1.42 +
    1.43 +  // Add a bookmark and create another backup.
    1.44 +  let bookmarkId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.bookmarksMenuFolder,
    1.45 +                                                        uri("http://foo.com"),
    1.46 +                                                        PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.47 +                                                        "foo");
    1.48 +  // We must enforce a backup since one for today already exists.  The forced
    1.49 +  // backup will replace the existing one.
    1.50 +  yield PlacesBackups.create(undefined, true);
    1.51 +  do_check_eq(backupFiles.length, 1);
    1.52 +  recentBackup = yield PlacesBackups.getMostRecentBackup();
    1.53 +  do_check_neq(recentBackup, OS.Path.join(backupFolder, oldBackupName));
    1.54 +  matches = OS.Path.basename(recentBackup).match(PlacesBackups.filenamesRegex);
    1.55 +  do_check_eq(matches[1], new Date().toLocaleFormat("%Y-%m-%d"));
    1.56 +  do_check_eq(matches[2], count + 1);
    1.57 +  do_check_neq(matches[3], hash);
    1.58 +
    1.59 +  // Clean up
    1.60 +  PlacesUtils.bookmarks.removeItem(bookmarkId);
    1.61 +  yield PlacesBackups.create(0);
    1.62 +});

mercurial