michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ 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: * Check for correct functionality of bookmarks backups michael@0: */ michael@0: michael@0: const NUMBER_OF_BACKUPS = 10; michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function () { michael@0: // Generate random dates. michael@0: let dateObj = new Date(); michael@0: let dates = []; michael@0: while (dates.length < NUMBER_OF_BACKUPS) { michael@0: // Use last year to ensure today's backup is the newest. michael@0: let randomDate = new Date(dateObj.getFullYear() - 1, michael@0: Math.floor(12 * Math.random()), michael@0: Math.floor(28 * Math.random())); michael@0: let dateString = randomDate.toLocaleFormat("%Y-%m-%d"); michael@0: if (dates.indexOf(dateString) == -1) michael@0: dates.push(dateString); michael@0: } michael@0: // Sort dates from oldest to newest. michael@0: dates.sort(); michael@0: michael@0: // Get and cleanup the backups folder. michael@0: let backupFolderPath = yield PlacesBackups.getBackupFolder(); michael@0: let bookmarksBackupDir = new FileUtils.File(backupFolderPath); michael@0: michael@0: // Fake backups are created backwards to ensure we won't consider file michael@0: // creation time. michael@0: // Create fake backups for the newest dates. michael@0: for (let i = dates.length - 1; i >= 0; i--) { michael@0: let backupFilename = PlacesBackups.getFilenameForDate(new Date(dates[i])); michael@0: let backupFile = bookmarksBackupDir.clone(); michael@0: backupFile.append(backupFilename); michael@0: backupFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0666", 8)); michael@0: do_log_info("Creating fake backup " + backupFile.leafName); michael@0: if (!backupFile.exists()) michael@0: do_throw("Unable to create fake backup " + backupFile.leafName); michael@0: } michael@0: michael@0: yield PlacesBackups.create(NUMBER_OF_BACKUPS); michael@0: // Add today's backup. michael@0: dates.push(dateObj.toLocaleFormat("%Y-%m-%d")); michael@0: michael@0: // Check backups. We have 11 dates but we the max number is 10 so the michael@0: // oldest backup should have been removed. michael@0: for (let i = 0; i < dates.length; i++) { michael@0: let backupFilename; michael@0: let shouldExist; michael@0: let backupFile; michael@0: if (i > 0) { michael@0: let files = bookmarksBackupDir.directoryEntries; michael@0: while (files.hasMoreElements()) { michael@0: let entry = files.getNext().QueryInterface(Ci.nsIFile); michael@0: if (PlacesBackups.filenamesRegex.test(entry.leafName)) { michael@0: backupFilename = entry.leafName; michael@0: backupFile = entry; michael@0: break; michael@0: } michael@0: } michael@0: shouldExist = true; michael@0: } michael@0: else { michael@0: backupFilename = PlacesBackups.getFilenameForDate(new Date(dates[i])); michael@0: backupFile = bookmarksBackupDir.clone(); michael@0: backupFile.append(backupFilename); michael@0: shouldExist = false; michael@0: } michael@0: if (backupFile.exists() != shouldExist) michael@0: do_throw("Backup should " + (shouldExist ? "" : "not") + " exist: " + backupFilename); michael@0: } michael@0: michael@0: // Cleanup backups folder. michael@0: // XXX: Can't use bookmarksBackupDir.remove(true) because file lock happens michael@0: // on WIN XP. michael@0: let files = bookmarksBackupDir.directoryEntries; michael@0: while (files.hasMoreElements()) { michael@0: let entry = files.getNext().QueryInterface(Ci.nsIFile); michael@0: entry.remove(false); michael@0: } michael@0: do_check_false(bookmarksBackupDir.directoryEntries.hasMoreElements()); michael@0: });