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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Checks that automatically created bookmark backups are discarded if they are michael@0: * duplicate of an existing ones. michael@0: */ michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function() { michael@0: // Create a backup for yesterday in the backups folder. michael@0: let backupFolder = yield PlacesBackups.getBackupFolder(); michael@0: let dateObj = new Date(); michael@0: dateObj.setDate(dateObj.getDate() - 1); michael@0: let oldBackupName = PlacesBackups.getFilenameForDate(dateObj); michael@0: let oldBackup = OS.Path.join(backupFolder, oldBackupName); michael@0: let {count: count, hash: hash} = yield BookmarkJSONUtils.exportToFile(oldBackup); michael@0: do_check_true(count > 0); michael@0: do_check_eq(hash.length, 24); michael@0: oldBackupName = oldBackupName.replace(/\.json/, "_" + count + "_" + hash + ".json"); michael@0: yield OS.File.move(oldBackup, OS.Path.join(backupFolder, oldBackupName)); michael@0: michael@0: // Create a backup. michael@0: // This should just rename the existing backup, so in the end there should be michael@0: // only one backup with today's date. michael@0: yield PlacesBackups.create(); michael@0: michael@0: // Get the hash of the generated backup michael@0: let backupFiles = yield PlacesBackups.getBackupFiles(); michael@0: do_check_eq(backupFiles.length, 1); michael@0: michael@0: let matches = OS.Path.basename(backupFiles[0]).match(PlacesBackups.filenamesRegex); michael@0: do_check_eq(matches[1], new Date().toLocaleFormat("%Y-%m-%d")); michael@0: do_check_eq(matches[2], count); michael@0: do_check_eq(matches[3], hash); michael@0: michael@0: // Add a bookmark and create another backup. michael@0: let bookmarkId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.bookmarksMenuFolder, michael@0: uri("http://foo.com"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "foo"); michael@0: // We must enforce a backup since one for today already exists. The forced michael@0: // backup will replace the existing one. michael@0: yield PlacesBackups.create(undefined, true); michael@0: do_check_eq(backupFiles.length, 1); michael@0: recentBackup = yield PlacesBackups.getMostRecentBackup(); michael@0: do_check_neq(recentBackup, OS.Path.join(backupFolder, oldBackupName)); michael@0: matches = OS.Path.basename(recentBackup).match(PlacesBackups.filenamesRegex); michael@0: do_check_eq(matches[1], new Date().toLocaleFormat("%Y-%m-%d")); michael@0: do_check_eq(matches[2], count + 1); michael@0: do_check_neq(matches[3], hash); michael@0: michael@0: // Clean up michael@0: PlacesUtils.bookmarks.removeItem(bookmarkId); michael@0: yield PlacesBackups.create(0); michael@0: });