summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/composition/browser_signatureUpdating.js
blob: 1555703e27db5e556eab9fd0e1a27b6636408482 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* 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/. */

/**
 * Tests that the signature updates properly when switching identities.
 */

// mail.identity.id1.htmlSigFormat = false
// mail.identity.id1.htmlSigText   = "Tinderbox is soo 90ies"

// mail.identity.id2.htmlSigFormat = true
// mail.identity.id2.htmlSigText   = "Tinderboxpushlog is the new <b>hotness!</b>"

"use strict";

var { close_compose_window, open_compose_new_mail, setup_msg_contents } =
  ChromeUtils.import("resource://testing-common/mozmill/ComposeHelpers.jsm");
var { be_in_folder, FAKE_SERVER_HOSTNAME, get_special_folder } =
  ChromeUtils.import(
    "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
  );

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

var cwc = null; // compose window controller

add_setup(async function () {
  requestLongerTimeout(2);

  // These prefs can't be set in the manifest as they contain white-space.
  Services.prefs.setStringPref(
    "mail.identity.id1.htmlSigText",
    "Tinderbox is soo 90ies"
  );
  Services.prefs.setStringPref(
    "mail.identity.id2.htmlSigText",
    "Tinderboxpushlog is the new <b>hotness!</b>"
  );

  // Ensure we're in the tinderbox account as that has the right identities set
  // up for this test.
  let server = MailServices.accounts.findServer(
    "tinderbox",
    FAKE_SERVER_HOSTNAME,
    "pop3"
  );
  let inbox = await get_special_folder(
    Ci.nsMsgFolderFlags.Inbox,
    false,
    server
  );
  await be_in_folder(inbox);
});

registerCleanupFunction(function () {
  Services.prefs.clearUserPref("mail.compose.default_to_paragraph");
  Services.prefs.clearUserPref("mail.identity.id1.compose_html");
  Services.prefs.clearUserPref("mail.identity.id1.htmlSigText");
  Services.prefs.clearUserPref("mail.identity.id2.htmlSigText");
  Services.prefs.clearUserPref(
    "mail.identity.id1.suppress_signature_separator"
  );
  Services.prefs.clearUserPref(
    "mail.identity.id2.suppress_signature_separator"
  );
});

/**
 * Test that the plaintext compose window has a signature initially,
 * and has the correct signature after switching to another identity.
 */
async function plaintextComposeWindowSwitchSignatures(suppressSigSep) {
  Services.prefs.setBoolPref("mail.identity.id1.compose_html", false);
  Services.prefs.setBoolPref(
    "mail.identity.id1.suppress_signature_separator",
    suppressSigSep
  );
  Services.prefs.setBoolPref(
    "mail.identity.id2.suppress_signature_separator",
    suppressSigSep
  );
  cwc = open_compose_new_mail();

  let contentFrame = cwc.window.document.getElementById("messageEditor");
  let mailBody = contentFrame.contentDocument.body;

  // The first node in the body should be a BR node, which allows the user
  // to insert text before / outside of the signature.
  Assert.equal(mailBody.firstChild.localName, "br");

  setup_msg_contents(cwc, "", "Plaintext compose window", "Body, first line.");

  let node = mailBody.lastChild;

  // The last node is a BR - this allows users to put text after the
  // signature without it being styled like the signature.
  Assert.equal(node.localName, "br");
  node = node.previousSibling;

  // Now we should have the DIV node that contains the signature, with
  // the class moz-signature.
  Assert.equal(node.localName, "div");

  const kSeparator = "-- ";
  const kSigClass = "moz-signature";
  Assert.equal(node.className, kSigClass);

  let sigNode = node.firstChild;

  if (!suppressSigSep) {
    Assert.equal(sigNode.textContent, kSeparator);
    let brNode = sigNode.nextSibling;
    Assert.equal(brNode.localName, "br");
    sigNode = brNode.nextSibling;
  }

  let expectedText = "Tinderbox is soo 90ies";
  Assert.equal(sigNode.textContent, expectedText);

  // Now switch identities!
  await chooseIdentity(cwc.window, "id2");

  node = contentFrame.contentDocument.body.lastChild;

  // The last node is a BR - this allows users to put text after the
  // signature without it being styled like the signature.
  Assert.equal(node.localName, "br");
  node = node.previousSibling;

  Assert.equal(node.localName, "div");
  Assert.equal(node.className, kSigClass);

  sigNode = node.firstChild;

  if (!suppressSigSep) {
    expectedText = "-- ";
    Assert.equal(sigNode.textContent, kSeparator);
    let brNode = sigNode.nextSibling;
    Assert.equal(brNode.localName, "br");
    sigNode = brNode.nextSibling;
  }

  expectedText = "Tinderboxpushlog is the new *hotness!*";
  Assert.equal(sigNode.textContent, expectedText);

  // Now check that the original signature has been removed by ensuring
  // that there's only one node with class moz-signature.
  let sigs = contentFrame.contentDocument.querySelectorAll("." + kSigClass);
  Assert.equal(sigs.length, 1);

  // And ensure that the text we wrote wasn't altered
  let bodyFirstChild = contentFrame.contentDocument.body.firstChild;

  while (node != bodyFirstChild) {
    node = node.previousSibling;
  }

  Assert.equal(node.nodeValue, "Body, first line.");

  close_compose_window(cwc);
}

add_task(async function testPlaintextComposeWindowSwitchSignatures() {
  await plaintextComposeWindowSwitchSignatures(false);
});

add_task(
  async function testPlaintextComposeWindowSwitchSignaturesWithSuppressedSeparator() {
    await plaintextComposeWindowSwitchSignatures(true);
  }
);

/**
 * Same test, but with an HTML compose window
 */
async function HTMLComposeWindowSwitchSignatures(
  suppressSigSep,
  paragraphFormat
) {
  Services.prefs.setBoolPref(
    "mail.compose.default_to_paragraph",
    paragraphFormat
  );

  Services.prefs.setBoolPref("mail.identity.id1.compose_html", true);
  Services.prefs.setBoolPref(
    "mail.identity.id1.suppress_signature_separator",
    suppressSigSep
  );
  Services.prefs.setBoolPref(
    "mail.identity.id2.suppress_signature_separator",
    suppressSigSep
  );
  cwc = open_compose_new_mail();

  setup_msg_contents(cwc, "", "HTML compose window", "Body, first line.");

  let contentFrame = cwc.window.document.getElementById("messageEditor");
  let node = contentFrame.contentDocument.body.lastChild;

  // In html compose, the signature is inside the last node, which has a
  // class="moz-signature".
  Assert.equal(node.className, "moz-signature");
  node = node.firstChild; // text node containing the signature divider
  if (suppressSigSep) {
    Assert.equal(node.nodeValue, "Tinderbox is soo 90ies");
  } else {
    Assert.equal(node.nodeValue, "-- \nTinderbox is soo 90ies");
  }

  // Now switch identities!
  await chooseIdentity(cwc.window, "id2");

  node = contentFrame.contentDocument.body.lastChild;

  // In html compose, the signature is inside the last node
  // with class="moz-signature".
  Assert.equal(node.className, "moz-signature");
  node = node.firstChild; // text node containing the signature divider
  if (!suppressSigSep) {
    Assert.equal(node.nodeValue, "-- ");
    node = node.nextSibling;
    Assert.equal(node.localName, "br");
    node = node.nextSibling;
  }
  Assert.equal(node.nodeValue, "Tinderboxpushlog is the new ");
  node = node.nextSibling;
  Assert.equal(node.localName, "b");
  node = node.firstChild;
  Assert.equal(node.nodeValue, "hotness!");

  // Now check that the original signature has been removed,
  // and no blank lines got added!
  node = contentFrame.contentDocument.body.firstChild;
  let textNode;
  if (paragraphFormat) {
    textNode = node.firstChild;
  } else {
    textNode = node;
  }
  Assert.equal(textNode.nodeValue, "Body, first line.");
  if (!paragraphFormat) {
    node = node.nextSibling;
    Assert.equal(node.localName, "br");
  }
  node = node.nextSibling;
  // check that the signature is immediately after the message text.
  Assert.equal(node.className, "moz-signature");
  // check that that the signature is the last node.
  Assert.equal(node, contentFrame.contentDocument.body.lastChild);

  close_compose_window(cwc);
}

add_task(async function testHTMLComposeWindowSwitchSignatures() {
  await HTMLComposeWindowSwitchSignatures(false, false);
});

add_task(
  async function testHTMLComposeWindowSwitchSignaturesWithSuppressedSeparator() {
    await HTMLComposeWindowSwitchSignatures(true, false);
  }
);

add_task(async function testHTMLComposeWindowSwitchSignaturesParagraphFormat() {
  await HTMLComposeWindowSwitchSignatures(false, true);
});

add_task(
  async function testHTMLComposeWindowSwitchSignaturesWithSuppressedSeparatorParagraphFormat() {
    await HTMLComposeWindowSwitchSignatures(true, true);
  }
);