/* 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 choosing "Don't Save" when closing a compose window removes the * draft that autosave created, and nothing else. */ "use strict"; var { close_compose_window, compose_window_ready, open_compose_new_mail, setup_msg_contents, } = ChromeUtils.importESModule( "resource://testing-common/mail/ComposeHelpers.sys.mjs" ); var { be_in_folder, get_about_3pane, get_special_folder, select_click_row } = ChromeUtils.importESModule( "resource://testing-common/mail/FolderDisplayHelpers.sys.mjs" ); var { make_message_sets_in_folders } = ChromeUtils.importESModule( "resource://testing-common/mail/MessageInjectionHelpers.sys.mjs" ); var { promise_new_window } = ChromeUtils.importESModule( "resource://testing-common/mail/WindowHelpers.sys.mjs" ); let draftsFolder, templatesSubfolder; add_setup(async function () { draftsFolder = await get_special_folder(Ci.nsMsgFolderFlags.Drafts, true); const templatesFolder = await get_special_folder( Ci.nsMsgFolderFlags.Templates, true ); templatesSubfolder = templatesFolder .QueryInterface(Ci.nsIMsgLocalMailFolder) .createLocalSubfolder("Discard Project"); registerCleanupFunction(() => { templatesSubfolder.deleteSelf(null); }); }); /** * Autosave the message in the compose window and wait until the draft is in * the Drafts folder and the compose window has finished saving it. * * @param {Window} cwc * @returns {integer} The key of the autosaved draft. */ async function autosave(cwc) { const count = draftsFolder.getTotalMessages(false); await cwc.AutoSave(); await TestUtils.waitForCondition( () => draftsFolder.getTotalMessages(false) == count + 1, "autosave should create a draft" ); // The draft may be in the folder before the compose window has caught up. // Closing the window while a save is in progress shows a different prompt. let key; await TestUtils.waitForCondition(() => { if (cwc.gSaveOperationInProgress) { return false; } const draftId = cwc.gMsgCompose.compFields.draftId; key = parseInt(draftId.substr(draftId.indexOf("#") + 1)); return ( draftsFolder.msgDatabase.containsKey(key) && draftsFolder.generateMessageURI(key) == draftId ); }, "the compose window should finish saving the draft"); return key; } add_task(async function test_discardNewMessage() { const cwc = await open_compose_new_mail(); await setup_msg_contents( cwc, "test@example.invalid", "Discarded autosave", "Some text" ); await autosave(cwc); await close_compose_window(cwc, true); await TestUtils.waitForCondition( () => draftsFolder.getTotalMessages(false) == 0, "Don't Save should remove the autosaved draft" ); }); add_task(async function test_discardKeepsMessageWithSameKey() { // If the draft ID points to another folder, the key it carries means nothing // in the Drafts folder, even if a message there happens to have that key. const otherFolder = draftsFolder.rootFolder .QueryInterface(Ci.nsIMsgLocalMailFolder) .createLocalSubfolder("Discard Other"); registerCleanupFunction(() => { otherFolder.deleteSelf(null); }); const cwc = await open_compose_new_mail(); await setup_msg_contents( cwc, "test@example.invalid", "Discarded autosave, other folder", "Some text" ); const key = await autosave(cwc); cwc.gMsgCompose.compFields.draftId = otherFolder.generateMessageURI(key); await close_compose_window(cwc, true); // Nothing should be deleted, so there is nothing to wait for. await TestUtils.waitForTick(); Assert.ok( draftsFolder.msgDatabase.containsKey(key), "the message in the Drafts folder with the same key should be kept" ); // Clean up the draft. const draft = draftsFolder.msgDatabase.getMsgHdrForKey(key); draftsFolder.deleteMessages([draft], null, true, false, null, false); await TestUtils.waitForCondition( () => draftsFolder.getTotalMessages(false) == 0, "the draft should be cleaned up" ); }); add_task(async function test_discardWhileEditingTemplate() { await make_message_sets_in_folders([templatesSubfolder], [{ count: 1 }]); await be_in_folder(templatesSubfolder); await select_click_row(0, AccessibilityUtils); const composePromise = promise_new_window("msgcompose"); get_about_3pane().commandController.doCommand("cmd_editTemplateMsg"); const cwc = await compose_window_ready(composePromise); cwc.document.getElementById("messageEditor").focus(); EventUtils.sendString("More text", cwc); await autosave(cwc); await close_compose_window(cwc, true); await TestUtils.waitForCondition( () => draftsFolder.getTotalMessages(false) == 0, "Don't Save should remove the autosaved draft" ); Assert.equal( templatesSubfolder.getTotalMessages(false), 1, "the template should be kept" ); });