/* 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/. */ let account = createAccount(); let defaultIdentity = addIdentity(account); let nonDefaultIdentity = addIdentity(account); let gRootFolder = account.incomingServer.rootFolder; gRootFolder.createSubfolder("test", null); let gTestFolder = gRootFolder.getChildNamed("test"); createMessages(gTestFolder, 4); add_task(async function testPlainTextBody() { let files = { "background.js": async () => { async function checkWindow(expected) { let state = await browser.compose.getComposeDetails(createdTab.id); for (let field of ["isPlainText"]) { if (field in expected) { browser.test.assertEq( expected[field], state[field], `Check value for ${field}` ); } } for (let field of ["plainTextBody"]) { if (field in expected) { browser.test.assertEq( JSON.stringify(expected[field]), JSON.stringify(state[field]), `Check value for ${field}` ); } } } // Start a new message. let createdWindowPromise = window.waitForEvent("windows.onCreated"); await browser.compose.beginNew({ isPlainText: true }); let [createdWindow] = await createdWindowPromise; let [createdTab] = await browser.tabs.query({ windowId: createdWindow.id, }); await checkWindow({ isPlainText: true }); let tests = [ { // Set plaintextBody with Windows style newlines. The return value of // the API is independent of the used OS and only returns LF endings. input: { isPlainText: true, plainTextBody: "123\r\n456\r\n789" }, expected: { isPlainText: true, plainTextBody: "123\n456\n789" }, }, { // Set plaintextBody with Linux style newlines. The return value of // the API is independent of the used OS and only returns LF endings. input: { isPlainText: true, plainTextBody: "ABC\nDEF\nGHI" }, expected: { isPlainText: true, plainTextBody: "ABC\nDEF\nGHI" }, }, { // Bug 1792551 without newline at the end. input: { isPlainText: true, plainTextBody: "123456 \n Hello " }, expected: { isPlainText: true, plainTextBody: "123456 \n Hello " }, }, { // Bug 1792551 without newline at the end. input: { isPlainText: true, plainTextBody: "123456 \n " }, expected: { isPlainText: true, plainTextBody: "123456 \n " }, }, { // Bug 1792551 with a newline at the end. input: { isPlainText: true, plainTextBody: "123456 \n Hello \n" }, expected: { isPlainText: true, plainTextBody: "123456 \n Hello \n" }, }, ]; for (let test of tests) { browser.test.log(`Checking input: ${JSON.stringify(test.input)}`); await browser.compose.setComposeDetails(createdTab.id, test.input); await checkWindow(test.expected); } browser.test.log("Replace plainTextBody with empty string"); await browser.compose.setComposeDetails(createdTab.id, { isPlainText: true, plainTextBody: "Lorem ipsum", }); await checkWindow({ isPlainText: true, plainTextBody: "Lorem ipsum" }); await browser.compose.setComposeDetails(createdTab.id, { isPlainText: true, plainTextBody: "", }); await checkWindow({ isPlainText: true, plainTextBody: "" }); // Clean up. let removedWindowPromise = window.waitForEvent("windows.onRemoved"); browser.windows.remove(createdWindow.id); await removedWindowPromise; browser.test.notifyPass("finished"); }, "utils.js": await getUtilsJS(), }; let extension = ExtensionTestUtils.loadExtension({ files, manifest: { background: { scripts: ["utils.js", "background.js"] }, permissions: ["accountsRead", "compose"], }, }); await extension.startup(); await extension.awaitFinish("finished"); await extension.unload(); }); add_task(async function testBody() { // Open an compose window with HTML body. let params = Cc[ "@mozilla.org/messengercompose/composeparams;1" ].createInstance(Ci.nsIMsgComposeParams); params.composeFields = Cc[ "@mozilla.org/messengercompose/composefields;1" ].createInstance(Ci.nsIMsgCompFields); params.composeFields.body = "
This is some HTML text.
"; let htmlWindowPromise = BrowserTestUtils.domWindowOpened(); MailServices.compose.OpenComposeWindowWithParams(null, params); let htmlWindow = await htmlWindowPromise; await BrowserTestUtils.waitForEvent(htmlWindow, "load"); // Open another compose window with plain text body. params = Cc["@mozilla.org/messengercompose/composeparams;1"].createInstance( Ci.nsIMsgComposeParams ); params.composeFields = Cc[ "@mozilla.org/messengercompose/composefields;1" ].createInstance(Ci.nsIMsgCompFields); params.format = Ci.nsIMsgCompFormat.PlainText; params.composeFields.body = "This is some plain text."; let plainTextComposeWindowPromise = BrowserTestUtils.domWindowOpened(); MailServices.compose.OpenComposeWindowWithParams(null, params); let plainTextWindow = await plainTextComposeWindowPromise; await BrowserTestUtils.waitForEvent(plainTextWindow, "load"); // Run the extension. let extension = ExtensionTestUtils.loadExtension({ background: async () => { let windows = await browser.windows.getAll({ populate: true, windowTypes: ["messageCompose"], }); let [htmlTabId, plainTextTabId] = windows.map(w => w.tabs[0].id); let plainTextBodyTag = ''; // Get details, HTML message. let htmlDetails = await browser.compose.getComposeDetails(htmlTabId); browser.test.log(JSON.stringify(htmlDetails)); browser.test.assertTrue(!htmlDetails.isPlainText); browser.test.assertTrue( htmlDetails.body.includes("This is some HTML text.
") ); browser.test.assertEq( "This is some /HTML/ text.", htmlDetails.plainTextBody ); // Set details, HTML message. await browser.compose.setComposeDetails(htmlTabId, { body: htmlDetails.body.replace("HTML", "HTML
"),
});
htmlDetails = await browser.compose.getComposeDetails(htmlTabId);
browser.test.log(JSON.stringify(htmlDetails));
browser.test.assertTrue(!htmlDetails.isPlainText);
browser.test.assertTrue(
htmlDetails.body.includes("This is some HTML
text.