toolkit/components/places/tests/unit/test_utils_backups_create.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_utils_backups_create.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     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 + /**
    1.11 +  * Check for correct functionality of bookmarks backups
    1.12 +  */
    1.13 +
    1.14 +const NUMBER_OF_BACKUPS = 10;
    1.15 +
    1.16 +function run_test() {
    1.17 +  run_next_test();
    1.18 +}
    1.19 +
    1.20 +add_task(function () {
    1.21 +  // Generate random dates.
    1.22 +  let dateObj = new Date();
    1.23 +  let dates = [];
    1.24 +  while (dates.length < NUMBER_OF_BACKUPS) {
    1.25 +    // Use last year to ensure today's backup is the newest.
    1.26 +    let randomDate = new Date(dateObj.getFullYear() - 1,
    1.27 +                              Math.floor(12 * Math.random()),
    1.28 +                              Math.floor(28 * Math.random()));
    1.29 +    let dateString = randomDate.toLocaleFormat("%Y-%m-%d");
    1.30 +    if (dates.indexOf(dateString) == -1)
    1.31 +      dates.push(dateString);
    1.32 +  }
    1.33 +  // Sort dates from oldest to newest.
    1.34 +  dates.sort();
    1.35 +
    1.36 +  // Get and cleanup the backups folder.
    1.37 +  let backupFolderPath = yield PlacesBackups.getBackupFolder();
    1.38 +  let bookmarksBackupDir = new FileUtils.File(backupFolderPath);
    1.39 +
    1.40 +  // Fake backups are created backwards to ensure we won't consider file
    1.41 +  // creation time.
    1.42 +  // Create fake backups for the newest dates.
    1.43 +  for (let i = dates.length - 1; i >= 0; i--) {
    1.44 +    let backupFilename = PlacesBackups.getFilenameForDate(new Date(dates[i]));
    1.45 +    let backupFile = bookmarksBackupDir.clone();
    1.46 +    backupFile.append(backupFilename);
    1.47 +    backupFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0666", 8));
    1.48 +    do_log_info("Creating fake backup " + backupFile.leafName);
    1.49 +    if (!backupFile.exists())
    1.50 +      do_throw("Unable to create fake backup " + backupFile.leafName);
    1.51 +  }
    1.52 +
    1.53 +  yield PlacesBackups.create(NUMBER_OF_BACKUPS);
    1.54 +  // Add today's backup.
    1.55 +  dates.push(dateObj.toLocaleFormat("%Y-%m-%d"));
    1.56 +
    1.57 +  // Check backups.  We have 11 dates but we the max number is 10 so the
    1.58 +  // oldest backup should have been removed.
    1.59 +  for (let i = 0; i < dates.length; i++) {
    1.60 +    let backupFilename;
    1.61 +    let shouldExist;
    1.62 +    let backupFile;
    1.63 +    if (i > 0) {
    1.64 +      let files = bookmarksBackupDir.directoryEntries;
    1.65 +      while (files.hasMoreElements()) {
    1.66 +        let entry = files.getNext().QueryInterface(Ci.nsIFile);
    1.67 +        if (PlacesBackups.filenamesRegex.test(entry.leafName)) {
    1.68 +          backupFilename = entry.leafName;
    1.69 +          backupFile = entry;
    1.70 +          break;
    1.71 +        }
    1.72 +      }
    1.73 +      shouldExist = true;
    1.74 +    }
    1.75 +    else {
    1.76 +      backupFilename = PlacesBackups.getFilenameForDate(new Date(dates[i]));
    1.77 +      backupFile = bookmarksBackupDir.clone();
    1.78 +      backupFile.append(backupFilename);
    1.79 +      shouldExist = false;
    1.80 +    }
    1.81 +    if (backupFile.exists() != shouldExist)
    1.82 +      do_throw("Backup should " + (shouldExist ? "" : "not") + " exist: " + backupFilename);
    1.83 +  }
    1.84 +
    1.85 +  // Cleanup backups folder.
    1.86 +  // XXX: Can't use bookmarksBackupDir.remove(true) because file lock happens
    1.87 +  // on WIN XP.
    1.88 +  let files = bookmarksBackupDir.directoryEntries;
    1.89 +  while (files.hasMoreElements()) {
    1.90 +    let entry = files.getNext().QueryInterface(Ci.nsIFile);
    1.91 +    entry.remove(false);
    1.92 +  }
    1.93 +  do_check_false(bookmarksBackupDir.directoryEntries.hasMoreElements());
    1.94 +});

mercurial