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: // Since PlacesBackups.getbackupFiles() is a lazy getter, these tests must michael@0: // run in the given order, to avoid making it out-of-sync. michael@0: michael@0: add_task(function check_max_backups_is_respected() { michael@0: // Get bookmarkBackups directory michael@0: let backupFolder = yield PlacesBackups.getBackupFolder(); michael@0: michael@0: // Create an html dummy backup in the past. michael@0: let htmlPath = OS.Path.join(backupFolder, "bookmarks-2008-01-01.html"); michael@0: let htmlFile = yield OS.File.open(htmlPath, { truncate: true }); michael@0: htmlFile.close(); michael@0: do_check_true(yield OS.File.exists(htmlPath)); michael@0: michael@0: // Create a json dummy backup in the past. michael@0: let jsonPath = OS.Path.join(backupFolder, "bookmarks-2008-01-31.json"); michael@0: let jsonFile = yield OS.File.open(jsonPath, { truncate: true }); michael@0: jsonFile.close(); michael@0: do_check_true(yield OS.File.exists(jsonPath)); michael@0: michael@0: // Export bookmarks to JSON. michael@0: // Allow 2 backups, the older one should be removed. michael@0: yield PlacesBackups.create(2); michael@0: let backupFilename = PlacesBackups.getFilenameForDate(); michael@0: michael@0: let count = 0; michael@0: let lastBackupPath = null; michael@0: let iterator = new OS.File.DirectoryIterator(backupFolder); michael@0: try { michael@0: yield iterator.forEach(aEntry => { michael@0: count++; michael@0: if (PlacesBackups.filenamesRegex.test(aEntry.name)) michael@0: lastBackupPath = aEntry.path; michael@0: }); michael@0: } finally { michael@0: iterator.close(); michael@0: } michael@0: michael@0: do_check_eq(count, 2); michael@0: do_check_neq(lastBackupPath, null); michael@0: do_check_false(yield OS.File.exists(htmlPath)); michael@0: do_check_true(yield OS.File.exists(jsonPath)); michael@0: }); michael@0: michael@0: add_task(function check_max_backups_greater_than_backups() { michael@0: // Get bookmarkBackups directory michael@0: let backupFolder = yield PlacesBackups.getBackupFolder(); michael@0: michael@0: // Export bookmarks to JSON. michael@0: // Allow 3 backups, none should be removed. michael@0: yield PlacesBackups.create(3); michael@0: let backupFilename = PlacesBackups.getFilenameForDate(); michael@0: michael@0: let count = 0; michael@0: let lastBackupPath = null; michael@0: let iterator = new OS.File.DirectoryIterator(backupFolder); michael@0: try { michael@0: yield iterator.forEach(aEntry => { michael@0: count++; michael@0: if (PlacesBackups.filenamesRegex.test(aEntry.name)) michael@0: lastBackupPath = aEntry.path; michael@0: }); michael@0: } finally { michael@0: iterator.close(); michael@0: } michael@0: do_check_eq(count, 2); michael@0: do_check_neq(lastBackupPath, null); michael@0: }); michael@0: michael@0: add_task(function check_max_backups_null() { michael@0: // Get bookmarkBackups directory michael@0: let backupFolder = yield PlacesBackups.getBackupFolder(); michael@0: michael@0: // Export bookmarks to JSON. michael@0: // Allow infinite backups, none should be removed, a new one is not created michael@0: // since one for today already exists. michael@0: yield PlacesBackups.create(null); michael@0: let backupFilename = PlacesBackups.getFilenameForDate(); michael@0: michael@0: let count = 0; michael@0: let lastBackupPath = null; michael@0: let iterator = new OS.File.DirectoryIterator(backupFolder); michael@0: try { michael@0: yield iterator.forEach(aEntry => { michael@0: count++; michael@0: if (PlacesBackups.filenamesRegex.test(aEntry.name)) michael@0: lastBackupPath = aEntry.path; michael@0: }); michael@0: } finally { michael@0: iterator.close(); michael@0: } michael@0: do_check_eq(count, 2); michael@0: do_check_neq(lastBackupPath, null); michael@0: }); michael@0: michael@0: add_task(function check_max_backups_undefined() { michael@0: // Get bookmarkBackups directory michael@0: let backupFolder = yield PlacesBackups.getBackupFolder(); michael@0: michael@0: // Export bookmarks to JSON. michael@0: // Allow infinite backups, none should be removed, a new one is not created michael@0: // since one for today already exists. michael@0: yield PlacesBackups.create(); michael@0: let backupFilename = PlacesBackups.getFilenameForDate(); michael@0: michael@0: let count = 0; michael@0: let lastBackupPath = null; michael@0: let iterator = new OS.File.DirectoryIterator(backupFolder); michael@0: try { michael@0: yield iterator.forEach(aEntry => { michael@0: count++; michael@0: if (PlacesBackups.filenamesRegex.test(aEntry.name)) michael@0: lastBackupPath = aEntry.path; michael@0: }); michael@0: } finally { michael@0: iterator.close(); michael@0: } michael@0: do_check_eq(count, 2); michael@0: do_check_neq(lastBackupPath, null); michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }