summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_nsMsgCompose2.js
blob: 5f234444b88633f8aa18dc1b38a03a2708a127a2 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * Test suite for nsMsgCompose functions relating to send listeners.
 */

let gMsgCompose = null;
let numSendListenerFunctions = 7;

let gSLAll = new Array(numSendListenerFunctions + 1);

function sendListener() {}

sendListener.prototype = {
  mReceived: 0,
  mAutoRemoveItem: 0,

  onStartSending(aMsgID, aMsgSize) {
    this.mReceived |= 0x01;
    if (this.mAutoRemoveItem == 0x01) {
      gMsgCompose.removeMsgSendListener(this);
    }
  },
  onProgress(aMsgID, aProgress, aProgressMax) {
    this.mReceived |= 0x02;
    if (this.mAutoRemoveItem == 0x02) {
      gMsgCompose.removeMsgSendListener(this);
    }
  },
  onStatus(aMsgID, aMsg) {
    this.mReceived |= 0x04;
    if (this.mAutoRemoveItem == 0x04) {
      gMsgCompose.removeMsgSendListener(this);
    }
  },
  onStopSending(aMsgID, aStatus, aMsg, aReturnFile) {
    this.mReceived |= 0x08;
    if (this.mAutoRemoveItem == 0x08) {
      gMsgCompose.removeMsgSendListener(this);
    }
  },
  onGetDraftFolderURI(aMsgID, aFolderURI) {
    this.mReceived |= 0x10;
    if (this.mAutoRemoveItem == 0x10) {
      gMsgCompose.removeMsgSendListener(this);
    }
  },
  onSendNotPerformed(aMsgID, aStatus) {
    this.mReceived |= 0x20;
    if (this.mAutoRemoveItem == 0x20) {
      gMsgCompose.removeMsgSendListener(this);
    }
  },
  onTransportSecurityError(msgID, status, secInfo, location) {
    this.mReceived |= 0x40;
    if (this.mAutoRemoveItem == 0x40) {
      gMsgCompose.removeMsgSendListener(this);
    }
  },
};

function NotifySendListeners() {
  gMsgCompose.onStartSending(null, null);
  gMsgCompose.onProgress(null, null, null);
  gMsgCompose.onStatus(null, null);
  gMsgCompose.onStopSending(null, null, null, null);
  gMsgCompose.onGetDraftFolderURI(null, null);
  gMsgCompose.onSendNotPerformed(null, null);
  gMsgCompose.onTransportSecurityError(null, null, null, "");
}

function run_test() {
  gMsgCompose = Cc["@mozilla.org/messengercompose/compose;1"].createInstance(
    Ci.nsIMsgCompose
  );
  let params = Cc[
    "@mozilla.org/messengercompose/composeparams;1"
  ].createInstance(Ci.nsIMsgComposeParams);
  gMsgCompose.initialize(params);

  Assert.ok(gMsgCompose != null);

  // Test - Add a listener

  for (let i = 0; i < numSendListenerFunctions + 1; ++i) {
    gSLAll[i] = new sendListener();
    gMsgCompose.addMsgSendListener(gSLAll[i]);
  }

  // Test - Notify all listeners

  NotifySendListeners();

  const bitMask = (1 << numSendListenerFunctions) - 1;
  for (let i = 0; i < numSendListenerFunctions + 1; ++i) {
    Assert.equal(gSLAll[i].mReceived, bitMask);
    gSLAll[i].mReceived = 0;

    // And prepare for test 3.
    gSLAll[i].mAutoRemoveItem = 1 << i;
  }

  // Test - Remove some listeners as we go

  NotifySendListeners();

  let currentReceived = 0;

  for (let i = 0; i < numSendListenerFunctions + 1; ++i) {
    if (i < numSendListenerFunctions) {
      currentReceived += 1 << i;
    }

    Assert.equal(gSLAll[i].mReceived, currentReceived);
    gSLAll[i].mReceived = 0;
  }

  // Test - Ensure the listeners have been removed.

  NotifySendListeners();

  for (let i = 0; i < numSendListenerFunctions + 1; ++i) {
    if (i < numSendListenerFunctions) {
      Assert.equal(gSLAll[i].mReceived, 0);
    } else {
      Assert.equal(gSLAll[i].mReceived, bitMask);
    }
  }

  // Test - Remove main listener

  gMsgCompose.removeMsgSendListener(gSLAll[numSendListenerFunctions]);
}