1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/bookmarks/test_466303-json-remove-backups.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,129 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Since PlacesBackups.getbackupFiles() is a lazy getter, these tests must 1.11 +// run in the given order, to avoid making it out-of-sync. 1.12 + 1.13 +add_task(function check_max_backups_is_respected() { 1.14 + // Get bookmarkBackups directory 1.15 + let backupFolder = yield PlacesBackups.getBackupFolder(); 1.16 + 1.17 + // Create an html dummy backup in the past. 1.18 + let htmlPath = OS.Path.join(backupFolder, "bookmarks-2008-01-01.html"); 1.19 + let htmlFile = yield OS.File.open(htmlPath, { truncate: true }); 1.20 + htmlFile.close(); 1.21 + do_check_true(yield OS.File.exists(htmlPath)); 1.22 + 1.23 + // Create a json dummy backup in the past. 1.24 + let jsonPath = OS.Path.join(backupFolder, "bookmarks-2008-01-31.json"); 1.25 + let jsonFile = yield OS.File.open(jsonPath, { truncate: true }); 1.26 + jsonFile.close(); 1.27 + do_check_true(yield OS.File.exists(jsonPath)); 1.28 + 1.29 + // Export bookmarks to JSON. 1.30 + // Allow 2 backups, the older one should be removed. 1.31 + yield PlacesBackups.create(2); 1.32 + let backupFilename = PlacesBackups.getFilenameForDate(); 1.33 + 1.34 + let count = 0; 1.35 + let lastBackupPath = null; 1.36 + let iterator = new OS.File.DirectoryIterator(backupFolder); 1.37 + try { 1.38 + yield iterator.forEach(aEntry => { 1.39 + count++; 1.40 + if (PlacesBackups.filenamesRegex.test(aEntry.name)) 1.41 + lastBackupPath = aEntry.path; 1.42 + }); 1.43 + } finally { 1.44 + iterator.close(); 1.45 + } 1.46 + 1.47 + do_check_eq(count, 2); 1.48 + do_check_neq(lastBackupPath, null); 1.49 + do_check_false(yield OS.File.exists(htmlPath)); 1.50 + do_check_true(yield OS.File.exists(jsonPath)); 1.51 +}); 1.52 + 1.53 +add_task(function check_max_backups_greater_than_backups() { 1.54 + // Get bookmarkBackups directory 1.55 + let backupFolder = yield PlacesBackups.getBackupFolder(); 1.56 + 1.57 + // Export bookmarks to JSON. 1.58 + // Allow 3 backups, none should be removed. 1.59 + yield PlacesBackups.create(3); 1.60 + let backupFilename = PlacesBackups.getFilenameForDate(); 1.61 + 1.62 + let count = 0; 1.63 + let lastBackupPath = null; 1.64 + let iterator = new OS.File.DirectoryIterator(backupFolder); 1.65 + try { 1.66 + yield iterator.forEach(aEntry => { 1.67 + count++; 1.68 + if (PlacesBackups.filenamesRegex.test(aEntry.name)) 1.69 + lastBackupPath = aEntry.path; 1.70 + }); 1.71 + } finally { 1.72 + iterator.close(); 1.73 + } 1.74 + do_check_eq(count, 2); 1.75 + do_check_neq(lastBackupPath, null); 1.76 +}); 1.77 + 1.78 +add_task(function check_max_backups_null() { 1.79 + // Get bookmarkBackups directory 1.80 + let backupFolder = yield PlacesBackups.getBackupFolder(); 1.81 + 1.82 + // Export bookmarks to JSON. 1.83 + // Allow infinite backups, none should be removed, a new one is not created 1.84 + // since one for today already exists. 1.85 + yield PlacesBackups.create(null); 1.86 + let backupFilename = PlacesBackups.getFilenameForDate(); 1.87 + 1.88 + let count = 0; 1.89 + let lastBackupPath = null; 1.90 + let iterator = new OS.File.DirectoryIterator(backupFolder); 1.91 + try { 1.92 + yield iterator.forEach(aEntry => { 1.93 + count++; 1.94 + if (PlacesBackups.filenamesRegex.test(aEntry.name)) 1.95 + lastBackupPath = aEntry.path; 1.96 + }); 1.97 + } finally { 1.98 + iterator.close(); 1.99 + } 1.100 + do_check_eq(count, 2); 1.101 + do_check_neq(lastBackupPath, null); 1.102 +}); 1.103 + 1.104 +add_task(function check_max_backups_undefined() { 1.105 + // Get bookmarkBackups directory 1.106 + let backupFolder = yield PlacesBackups.getBackupFolder(); 1.107 + 1.108 + // Export bookmarks to JSON. 1.109 + // Allow infinite backups, none should be removed, a new one is not created 1.110 + // since one for today already exists. 1.111 + yield PlacesBackups.create(); 1.112 + let backupFilename = PlacesBackups.getFilenameForDate(); 1.113 + 1.114 + let count = 0; 1.115 + let lastBackupPath = null; 1.116 + let iterator = new OS.File.DirectoryIterator(backupFolder); 1.117 + try { 1.118 + yield iterator.forEach(aEntry => { 1.119 + count++; 1.120 + if (PlacesBackups.filenamesRegex.test(aEntry.name)) 1.121 + lastBackupPath = aEntry.path; 1.122 + }); 1.123 + } finally { 1.124 + iterator.close(); 1.125 + } 1.126 + do_check_eq(count, 2); 1.127 + do_check_neq(lastBackupPath, null); 1.128 +}); 1.129 + 1.130 +function run_test() { 1.131 + run_next_test(); 1.132 +}