Bug 762594 - Prompts to save unmodified message forward draft. r=mconley,a=Standard8
authorMagnus Melin <mkmelin+mozilla@iki.fi>
Sun, 09 Dec 2012 22:36:48 +0200
changeset 13587 3546a1cdce583d1cc5602583afeeada25b44b2f5
parent 13586 3bea464deb12753c1e1168e31f58f4f3b79a738d
child 13588 ac8795b126f1ca7194413e858483a628e91fff06
push id18
push userbugzilla@standard8.plus.com
push dateSun, 30 Dec 2012 19:50:19 +0000
reviewersmconley, Standard8
bugs762594
Bug 762594 - Prompts to save unmodified message forward draft. r=mconley,a=Standard8
mail/components/compose/content/cloudAttachmentLinkManager.js
mail/test/mozmill/composition/test-forwarded-eml-actions.js
mail/test/mozmill/composition/test-save-changes-on-quit.js
mail/test/mozmill/shared-modules/test-compose-helpers.js
--- a/mail/components/compose/content/cloudAttachmentLinkManager.js
+++ b/mail/components/compose/content/cloudAttachmentLinkManager.js
@@ -40,31 +40,35 @@ var gCloudAttachmentLinkManager = {
     if (gComposeType != Components.interfaces.nsIMsgCompType.ForwardInline)
       return;
 
     let mailDoc = document.getElementById("content-frame").contentDocument;
     let mailBody = mailDoc.getElementsByTagName("body")[0];
     let editor = GetCurrentEditor();
     let selection = editor.selection;
 
-    let childNodes = mailBody.childNodes;
     let container = editor.createElementWithDefaults("div");
     container.setAttribute("class", "moz-forward-container");
 
+    editor.enableUndo(false);
+
     if (mailBody.hasChildNodes()) {
       while (mailBody.childNodes.length > 0) {
         let removedChild = mailBody.removeChild(mailBody.firstChild);
         container.appendChild(removedChild);
       }
     }
     editor.insertLineBreak();
     selection.collapse(mailBody, 1);
     editor.insertElementAtSelection(container, false);
     editor.insertLineBreak();
     editor.beginningOfDocument();
+
+    editor.enableUndo(true);
+    editor.resetModificationCount();
   },
   ComposeProcessDone: function() {},
   SaveInFolderDone: function() {},
 
   handleEvent: function(event) {
     let mailDoc = document.getElementById("content-frame").contentDocument;
 
     if (event.type == "attachment-uploaded") {
--- a/mail/test/mozmill/composition/test-forwarded-eml-actions.js
+++ b/mail/test/mozmill/composition/test-forwarded-eml-actions.js
@@ -109,17 +109,17 @@ function setupWindowAndTest(hotkeyToHit,
     throw new Error("body text didn't contain the body text; msgbodyA=" +
                     msgbodyB + ", bodyText=" + bodyText);
 
   let subjectText = compWin.e("msgSubject").value;
   if (subjectText.indexOf(msgsubject) == -1)
     throw new Error("subject text didn't contain the original subject; " +
                     "msgsubject=" +  msgsubject + ", subjectText=" + subjectText);
 
-  close_compose_window(compWin);
+  close_compose_window(compWin, false);
   close_window(msgWin);
 }
 
 /**
  * Test that replying to an attached .eml contains the expected texts.
  */
 function test_reply_to_attached_eml() {
   setupWindowAndTest("R", {shiftKey: false, accelKey: true});
--- a/mail/test/mozmill/composition/test-save-changes-on-quit.js
+++ b/mail/test/mozmill/composition/test-save-changes-on-quit.js
@@ -1,44 +1,46 @@
 /* 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 we prompt the user if they'd like to save their message when they
- * try to quit with an open compose window with unsaved changes.
+ * try to quit/close with an open compose window with unsaved changes, and
+ * that we don't prompt if there are no changes.
  */
 
+// make SOLO_TEST=composition/test-save-changes-on-quit.js mozmill-one
+
 const MODULE_NAME = "test-save-changes-on-close";
 
 const RELATIVE_ROOT = "../shared-modules";
 const MODULE_REQUIRES = ["folder-display-helpers", "compose-helpers",
-                         "prompt-helpers"];
+                         "prompt-helpers", "window-helpers"];
 const SAVE = 0
 const CANCEL = 1
 const DONT_SAVE = 2;
 
 var jumlib = {};
 Components.utils.import("resource://mozmill/modules/jum.js", jumlib);
 var elib = {};
 Components.utils.import("resource://mozmill/modules/elementslib.js", elib);
 Components.utils.import("resource://gre/modules/Services.jsm");
 
-var composeHelper = null;
 var cwc = null; // compose window controller
+var folder = null;
 
 var setupModule = function (module) {
-  let fdh = collector.getModule("folder-display-helpers");
-  fdh.installInto(module);
+  collector.getModule("folder-display-helpers").installInto(module);
+  collector.getModule("compose-helpers").installInto(module);
+  collector.getModule("prompt-helpers").installInto(module);
+  collector.getModule("window-helpers").installInto(module);
 
-  let composeHelper = collector.getModule("compose-helpers");
-  composeHelper.installInto(module);
-
-  let ph = collector.getModule("prompt-helpers");
-  ph.installInto(module);
+  folder = create_folder("PromptToSaveTest");
+  add_message_to_folder(folder, create_message());
 };
 
 /**
  * Test that when a compose window is open with changes, and
  * a Quit is requested (for example, from File > Quit from the
  * 3pane), that the user gets a confirmation dialog to discard
  * the changes. This also tests that the user can cancel the
  * quit request.
@@ -173,8 +175,49 @@ function test_window_quit_state_reset_on
   cwc2.click(cwc2.eid("menu_close"));
 
   let promptState = gMockPromptService.promptState;
   assert_not_equals(null, promptState, "Expected a confirmEx prompt");
 
   gMockPromptService.unregister();
 }
 
+/**
+ * Tests that we don't get a prompt to save if there has been no user input
+ * into the message yet, when trying to close.
+ */
+function test_no_prompt_on_close_for_unmodified() {
+  be_in_folder(folder);
+  let msg = select_click_row(0);
+  assert_selected_and_displayed(mc, msg);
+
+  let nwc = open_compose_new_mail();
+  close_compose_window(nwc, false);
+
+  let rwc = open_compose_with_reply();
+  close_compose_window(rwc, false);
+
+  let fwc = open_compose_with_forward();
+  close_compose_window(fwc, false);
+}
+
+/**
+ * Tests that we get a prompt to save if the user made changes to the message
+ * before trying to close it.
+ */
+function test_prompt_on_close_for_modified() {
+  be_in_folder(folder);
+  let msg = select_click_row(0);
+  assert_selected_and_displayed(mc, msg);
+
+  let nwc = open_compose_new_mail();
+  nwc.type(nwc.eid("content-frame"), "Hey hey hey!");
+  close_compose_window(nwc, true);
+
+  let rwc = open_compose_with_reply();
+  rwc.type(rwc.eid("content-frame"), "Howdy!");
+  close_compose_window(rwc, true);
+
+  let fwc = open_compose_with_forward();
+  fwc.type(fwc.eid("content-frame"), "Greetings!");
+  close_compose_window(fwc, true);
+}
+
--- a/mail/test/mozmill/shared-modules/test-compose-helpers.js
+++ b/mail/test/mozmill/shared-modules/test-compose-helpers.js
@@ -163,26 +163,41 @@ function open_compose_with_element_click
 
   windowHelper.plan_for_new_window("msgcompose");
   aController.click(new elib.ID(mc.window.document, aElement));
 
   return wait_for_compose_window();
 }
 
 /**
- * Closes the requested compose window. This function currently just forwards to
- * windowHelper but means we don't have to include windowHelper elsewhere if we
- * don't want it.
- *
- * In future it could also be expanded to handle prompting on close.
+ * Closes the requested compose window.
  *
  * @param aController the controller whose window is to be closed.
+ * @param aShouldPrompt (optional) true: check that the prompt to save appears
+ *                                 false: check there's no prompt to save
  */
-function close_compose_window(aController) {
-  windowHelper.close_window(aController);
+function close_compose_window(aController, aShouldPrompt) {
+  if (aShouldPrompt === undefined) { // caller doesn't care if we get a prompt
+    windowHelper.close_window(aController);
+    return;
+  }
+
+  windowHelper.plan_for_window_close(aController);
+  if (aShouldPrompt) {
+    windowHelper.plan_for_modal_dialog("commonDialog", function clickDontSave(controller) {
+       controller.window.document.documentElement.getButton("extra1").doCommand();
+    });
+    // Try to close, we should get a prompt to save.
+    aController.window.goDoCommand("cmd_close");
+    windowHelper.wait_for_modal_dialog();
+  }
+  else {
+    aController.window.goDoCommand("cmd_close");
+  }
+  windowHelper.wait_for_window_close();
 }
 
 /**
  * Waits for a new compose window to open. This assumes you have already called
  * "windowHelper.plan_for_new_window("msgcompose");" and the command to open
  * the compose window itself.
  *
  * @return The loaded window of type "msgcompose" wrapped in a MozmillController