summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/folder-display/browser_viewSource.js
blob: 63ce81daa31558939aa8ad2a8b7fcbaeb541b9ed (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
/* 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 view-source content can be reloaded to change encoding.
 */

"use strict";

var utils = ChromeUtils.import("resource://testing-common/mozmill/utils.jsm");
var { be_in_folder, create_folder, get_about_message, mc, select_click_row } =
  ChromeUtils.import(
    "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
  );
var {
  click_menus_in_sequence,
  close_window,
  plan_for_new_window,
  wait_for_new_window,
} = ChromeUtils.import("resource://testing-common/mozmill/WindowHelpers.jsm");

var folder;

// Message content as stored in the message folder. Non-ASCII characters as
// escape codes for clarity.
var contentLatin1 = "Testar, ett tv\xE5 tre.";
var contentUTF8 = "Testar, ett tv\xC3\xA5 tre.";
// Message content as it should be displayed to the user.
var contentReadable = "Testar, ett två tre.";
// UTF-8 content displayed as Latin1.
var contentGarbled = "Testar, ett två tre.";
// Latin1 content displayed as UTF-8.
var contentReplaced = "Testar, ett tv� tre.";

add_setup(async function () {
  folder = await create_folder("viewsource");
  addToFolder("ISO-8859-1 header/ISO-8859-1 body", "ISO-8859-1", contentLatin1);
  addToFolder("ISO-8859-1 header/UTF-8 body", "ISO-8859-1", contentUTF8);
  addToFolder("UTF-8 header/ISO-8859-1 body", "UTF-8", contentLatin1);
  addToFolder("UTF-8 header/UTF-8 body", "UTF-8", contentUTF8);

  await be_in_folder(folder);
});

registerCleanupFunction(() => {
  folder.deleteSelf(null);
});

/** Header matches the body. Should be readable in both places. */
add_task(async function latin1Header_with_latin1Body() {
  await subtest(0, contentReadable, contentReadable);
});
/** Header doesn't match the body. Unicode characters should be displayed. */
add_task(async function latin1Header_with_utf8Body() {
  await subtest(1, contentGarbled, contentGarbled);
});
/**
 * Header doesn't match the body. Unreadable characters should be replaced
 * in both places, but the view-source display defaults to windows-1252.
 */
add_task(async function utf8Header_with_latin1Body() {
  await subtest(2, contentReplaced, contentReadable);
});
/**
 * Header matches the body. Should be readable in both places, but the
 * view-source display defaults to windows-1252.
 */
add_task(async function utf8Header_with_utf8Body() {
  await subtest(3, contentReadable, contentGarbled);
});

function addToFolder(subject, charset, body) {
  let msgId = Services.uuid.generateUUID() + "@invalid";

  let source =
    "From - Sat Nov  1 12:39:54 2008\n" +
    "X-Mozilla-Status: 0001\n" +
    "X-Mozilla-Status2: 00000000\n" +
    "Message-ID: <" +
    msgId +
    ">\n" +
    "Date: Wed, 11 Jun 2008 20:32:02 -0400\n" +
    "From: Tester <tests@mozillamessaging.invalid>\n" +
    "MIME-Version: 1.0\n" +
    "To: anna@example.com\n" +
    `Subject: ${subject}` +
    "\n" +
    `Content-Type: text/plain; charset=${charset}\n` +
    "Content-Transfer-Encoding: 8bit\n" +
    "\n" +
    body +
    "\n";

  folder.QueryInterface(Ci.nsIMsgLocalMailFolder);
  folder.addMessage(source);

  return folder.msgDatabase.getMsgHdrForMessageID(msgId);
}

async function subtest(row, expectedDisplayed, expectedSource) {
  select_click_row(row);

  let aboutMessage = get_about_message();
  let displayContent =
    aboutMessage.getMessagePaneBrowser().contentDocument.body.textContent;
  Assert.stringContains(
    displayContent,
    expectedDisplayed,
    "Message content must include the readable text"
  );
  Assert.equal(
    aboutMessage.document.getElementById("messagepane").docShell.charset,
    "UTF-8"
  );

  plan_for_new_window("navigator:view-source");
  EventUtils.synthesizeKey("U", { shiftKey: false, accelKey: true });
  let viewSourceController = wait_for_new_window("navigator:view-source");

  utils.waitFor(
    () =>
      viewSourceController.window.document
        .getElementById("content")
        .contentDocument.querySelector("pre") != null,
    "Timeout waiting for the latin1 view-source document to load."
  );

  let source =
    viewSourceController.window.document.getElementById("content")
      .contentDocument.body.textContent;
  Assert.stringContains(
    source,
    expectedSource,
    "View source must contain the readable text"
  );

  let popupshown;

  // We can't use the menu on macOS.
  if (AppConstants.platform != "macosx") {
    let theContent =
      viewSourceController.window.document.getElementById("content");
    // Keep a reference to the originally loaded document.
    let doc = theContent.contentDocument;

    // Click the new window to make it receive further events properly.
    EventUtils.synthesizeMouseAtCenter(theContent, {}, theContent.ownerGlobal);
    await new Promise(resolve => setTimeout(resolve));

    popupshown = BrowserTestUtils.waitForEvent(
      viewSourceController.window.document.getElementById("viewmenu-popup"),
      "popupshown"
    );
    let menuView =
      viewSourceController.window.document.getElementById("menu_view");
    EventUtils.synthesizeMouseAtCenter(menuView, {}, menuView.ownerGlobal);
    await popupshown;

    Assert.equal(
      viewSourceController.window.document.getElementById(
        "repair-text-encoding"
      ).disabled,
      expectedSource == contentReadable
    );

    await click_menus_in_sequence(
      viewSourceController.window.document.getElementById("viewmenu-popup"),
      [{ id: "repair-text-encoding" }]
    );

    if (expectedSource != contentReadable) {
      utils.waitFor(
        () =>
          viewSourceController.window.document.getElementById("content")
            .contentDocument != doc &&
          viewSourceController.window.document
            .getElementById("content")
            .contentDocument.querySelector("pre") != null,
        "Timeout waiting utf-8 encoded view-source document to load."
      );

      source =
        viewSourceController.window.document.getElementById("content")
          .contentDocument.body.textContent;
      Assert.stringContains(
        source,
        contentReadable,
        "View source must contain the readable text"
      );
    }
  }

  // Check the context menu while were here.
  let browser = viewSourceController.window.document.getElementById("content");
  let contextMenu = viewSourceController.window.document.getElementById(
    "viewSourceContextMenu"
  );
  popupshown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "body",
    { type: "contextmenu" },
    browser
  );
  await popupshown;

  let actualItems = [];
  for (let item of contextMenu.children) {
    if (item.localName == "menuitem" && !item.hidden) {
      actualItems.push(item.id);
    }
  }
  Assert.deepEqual(actualItems, [
    "cMenu_copy",
    "cMenu_selectAll",
    "cMenu_find",
    "cMenu_findAgain",
  ]);
  contextMenu.hidePopup();

  close_window(viewSourceController);
}