summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_emptyTrash.js
blob: 528691117ea82e7d939faa64b7215c8ed2d34ebd (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 *
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 *
 * ***** END LICENSE BLOCK ***** */

/*
 * Test suite for empty trash
 *
 * Currently tested:
 * - Empty local trash
 * TODO
 * - Empty imap trash
 */

// Globals
var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var gMsgFile1;
var gLocalTrashFolder;
var gCurTestNum;
var gMsgHdrs = [];
var gRootFolder;

var nsIMFNService = Ci.nsIMsgFolderNotificationService;

// nsIMsgCopyServiceListener implementation
var copyListener = {
  OnStartCopy() {},
  OnProgress(aProgress, aProgressMax) {},
  SetMessageKey(aKey) {
    let hdr = localAccountUtils.inboxFolder.GetMessageHeader(aKey);
    gMsgHdrs.push({ hdr, ID: hdr.messageId });
  },
  SetMessageId(aMessageId) {},
  OnStopCopy(aStatus) {
    // Check: message successfully copied.
    Assert.equal(aStatus, 0);
    // Ugly hack: make sure we don't get stuck in a JS->C++->JS->C++... call stack
    // This can happen with a bunch of synchronous functions grouped together, and
    // can even cause tests to fail because they're still waiting for the listener
    // to return
    do_timeout(0, function () {
      doTest(++gCurTestNum);
    });
  },
};

var urlListener = {
  OnStartRunningUrl(aUrl) {},
  OnStopRunningUrl(aUrl, aExitCode) {
    // Check: message successfully copied.
    Assert.equal(aExitCode, 0);
    // Ugly hack: make sure we don't get stuck in a JS->C++->JS->C++... call stack
    // This can happen with a bunch of synchronous functions grouped together, and
    // can even cause tests to fail because they're still waiting for the listener
    // to return
    do_timeout(0, function () {
      doTest(++gCurTestNum);
    });
  },
};

function copyFileMessage(file, destFolder, isDraftOrTemplate) {
  MailServices.copy.copyFileMessage(
    file,
    destFolder,
    null,
    isDraftOrTemplate,
    0,
    "",
    copyListener,
    null
  );
}

function deleteMessages(srcFolder, items) {
  srcFolder.deleteMessages(items, null, false, true, copyListener, true);
}

/*
 * TESTS
 */

// Beware before commenting out a test -- later tests might just depend on earlier ones
var gTestArray = [
  // Copying message from file
  function testCopyFileMessage1() {
    copyFileMessage(gMsgFile1, localAccountUtils.inboxFolder, false);
  },

  // Delete message
  function testDeleteMessage() {
    // delete to trash
    // Let's take a moment to re-initialize stuff that got moved
    let inboxDB = localAccountUtils.inboxFolder.msgDatabase;
    gMsgHdrs[0].hdr = inboxDB.getMsgHdrForMessageID(gMsgHdrs[0].ID);

    // Now delete the message
    deleteMessages(localAccountUtils.inboxFolder, [gMsgHdrs[0].hdr]);
  },
  function emptyTrash() {
    gRootFolder = localAccountUtils.incomingServer.rootMsgFolder;
    gLocalTrashFolder = gRootFolder.getChildNamed("Trash");
    // hold onto a db to make sure that empty trash deals with the case
    // of someone holding onto the db, but the trash folder has a null db.
    let gLocalTrashDB = gLocalTrashFolder.msgDatabase; // eslint-disable-line no-unused-vars
    gLocalTrashFolder.msgDatabase = null;
    // this is synchronous
    gLocalTrashFolder.emptyTrash(null);
    // check that the trash folder is 0 size, that the db has a 0 message count
    // and has no messages.
    Assert.equal(0, gLocalTrashFolder.filePath.fileSize);
    Assert.equal(0, gLocalTrashFolder.msgDatabase.dBFolderInfo.numMessages);
    let msgs = [...gLocalTrashFolder.msgDatabase.enumerateMessages()];
    Assert.equal(0, msgs.length);
    urlListener.OnStopRunningUrl(null, 0);
  },
];

// Our listener, which captures events.
function gMFListener() {}
gMFListener.prototype = {
  folderDeleted(aFolder) {
    aFolder.msgDatabase = null;
  },
};

function run_test() {
  localAccountUtils.loadLocalMailAccount();
  // Load up a message so that we can copy it in later.
  gMsgFile1 = do_get_file("../../../data/bugmail10");
  // our front end code clears the msg db when it gets told the folder for
  // an open view has been deleted - so simulate that.
  var folderDeletedListener = new gMFListener();
  MailServices.mfn.addListener(
    folderDeletedListener,
    nsIMFNService.folderDeleted
  );

  // "Master" do_test_pending(), paired with a do_test_finished() at the end of all the operations.
  do_test_pending();

  // Do the test.
  doTest(1);
}

function doTest(test) {
  if (test <= gTestArray.length) {
    gCurTestNum = test;

    var testFn = gTestArray[test - 1];
    // Set a limit of three seconds; if the notifications haven't arrived by then there's a problem.
    do_timeout(10000, function () {
      if (gCurTestNum == test) {
        do_throw(
          "Notifications not received in 10000 ms for operation " + testFn.name
        );
      }
    });
    try {
      testFn();
    } catch (ex) {
      dump(ex);
    }
  } else {
    gMsgHdrs = null;
    do_test_finished(); // for the one in run_test()
  }
}