summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_staleTemporaryFileCleanup.js
blob: 427c10191441327db9fdc5c5d62921f508d6fad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Test that stale temporary files are cleaned up when the msg compose service
 * is initialized.
 */

var gExpectedFiles;

function create_temporary_files_for(name) {
  let file = Services.dirsvc.get("TmpD", Ci.nsIFile);
  file.append(name);
  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);

  return file;
}

function collect_expected_temporary_files() {
  let files = [];

  files.push(create_temporary_files_for("nsmail.tmp"));
  files.push(create_temporary_files_for("nsmail.tmp"));
  files.push(create_temporary_files_for("nsmail.tmp"));
  files.push(create_temporary_files_for("nsemail.eml"));
  files.push(create_temporary_files_for("nsemail.tmp"));
  files.push(create_temporary_files_for("nsqmail.tmp"));
  files.push(create_temporary_files_for("nscopy.tmp"));
  files.push(create_temporary_files_for("nscopy.tmp"));

  return files;
}

function check_files_not_exist(files) {
  files.forEach(function (file) {
    Assert.ok(!file.exists());
  });
}

function run_test() {
  gExpectedFiles = collect_expected_temporary_files();
  registerCleanupFunction(function () {
    gExpectedFiles.forEach(function (file) {
      if (file.exists()) {
        file.remove(false);
      }
    });
  });

  // Ensure we have at least one mail account
  localAccountUtils.loadLocalMailAccount();
  MailServices.compose; // Initialise the compose service.
  do_test_pending();
  check_files_not_exist(gExpectedFiles);
  do_test_finished();
}