summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_copyChaining.js
blob: b014049ed3ed2a87186ed71539b7f206b51e7ce5 (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
/* 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 of chaining copies between the same folders

/* import-globals-from ../../../test/resources/MessageGenerator.jsm */
load("../../../resources/MessageGenerator.jsm");

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

var gCopySource;
var gCopyDest;
var gMessages;
var gCurTestNum = 1;

// main test

var gTestArray = [
  function copyMsg1() {
    gMessages = [...gCopySource.msgDatabase.enumerateMessages()];
    CopyNextMessage();
  },
  function copyMsg2() {
    CopyNextMessage();
  },
  function copyMsg3() {
    CopyNextMessage();
  },
  function copyMsg4() {
    CopyNextMessage();
  },
];

function CopyNextMessage() {
  if (gMessages.length > 0) {
    let msgHdr = gMessages.shift();
    MailServices.copy.copyMessages(
      gCopySource,
      [msgHdr],
      gCopyDest,
      true,
      copyListener,
      null,
      false
    );
  } else {
    do_throw("TEST FAILED - out of messages");
  }
}

function run_test() {
  localAccountUtils.loadLocalMailAccount();
  let messageGenerator = new MessageGenerator();
  let scenarioFactory = new MessageScenarioFactory(messageGenerator);

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

  gCopyDest = localAccountUtils.inboxFolder.createLocalSubfolder("copyDest");
  // build up a diverse list of messages
  let messages = [];
  messages = messages.concat(scenarioFactory.directReply(10));
  gCopySource = localAccountUtils.rootFolder.createLocalSubfolder("copySource");
  addMessagesToFolder(messages, gCopySource);

  mailTestUtils.updateFolderAndNotify(gCopySource, doTest);
  return true;
}

function doTest() {
  var test = gCurTestNum;
  if (test <= gTestArray.length) {
    var testFn = gTestArray[test - 1];
    dump("Doing test " + test + " " + testFn.name + "\n");

    try {
      testFn();
    } catch (ex) {
      do_throw("TEST FAILED " + ex);
    }
  } else {
    endTest();
  }
}

function endTest() {
  // Cleanup, null out everything
  dump(" Exiting mail tests\n");
  gMessages = null;
  do_test_finished(); // for the one in run_test()
}

// nsIMsgCopyServiceListener implementation
var copyListener = {
  OnStartCopy() {},
  OnProgress(aProgress, aProgressMax) {},
  SetMessageKey(aKey) {},
  SetMessageId(aMessageId) {},
  OnStopCopy(aStatus) {
    // Check: message successfully copied.
    Assert.equal(aStatus, 0);
    ++gCurTestNum;
    doTest();
  },
};