Bug 801982 - Scratchpad doesn't show confirmation dialog after opening a file; r=harth
authorAnton Kovalyov <anton@mozilla.com>
Thu, 18 Oct 2012 11:00:33 -0700
changeset 111011 4067355bbc56340308ba1c29aa60d870799497f2
parent 111010 f39343285fc1c7cda5ceab320a24c2af3b53258f
child 111012 0743f910f5a5851c28155141bd8611a673bbb53a
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersharth
bugs801982
milestone19.0a1
Bug 801982 - Scratchpad doesn't show confirmation dialog after opening a file; r=harth
browser/devtools/scratchpad/scratchpad.js
browser/devtools/scratchpad/test/browser_scratchpad_bug_653427_confirm_close.js
--- a/browser/devtools/scratchpad/scratchpad.js
+++ b/browser/devtools/scratchpad/scratchpad.js
@@ -671,18 +671,16 @@ var Scratchpad = {
     let promptCallback = function(aFile) {
       this.promptSave(function(aCloseFile, aSaved, aStatus) {
         let shouldOpen = aCloseFile;
         if (aSaved && !Components.isSuccessCode(aStatus)) {
           shouldOpen = false;
         }
 
         if (shouldOpen) {
-          this._skipClosePrompt = true;
-
           let file;
           if (aFile) {
             file = aFile;
           } else {
             file = Components.classes["@mozilla.org/file/local;1"].
                    createInstance(Components.interfaces.nsILocalFile);
             let filePath = this.getRecentFiles()[aIndex];
             file.initWithPath(filePath);
@@ -1308,35 +1306,24 @@ var Scratchpad = {
     return true;
   },
 
   /**
    * Handler for window close event. Prompts to save scratchpad if
    * there are unsaved changes.
    *
    * @param nsIDOMEvent aEvent
+   * @param function aCallback
+   *        Optional function you want to call when file is saved/closed.
+   *        Used mainly for tests.
    */
-  onClose: function SP_onClose(aEvent)
+  onClose: function SP_onClose(aEvent, aCallback)
   {
-    if (this._skipClosePrompt) {
-      return;
-    }
-
-    this.promptSave(function(aShouldClose, aSaved, aStatus) {
-      let shouldClose = aShouldClose;
-      if (aSaved && !Components.isSuccessCode(aStatus)) {
-        shouldClose = false;
-      }
-
-      if (shouldClose) {
-        this._skipClosePrompt = true;
-        window.close();
-      }
-    }.bind(this));
     aEvent.preventDefault();
+    this.close(aCallback);
   },
 
   /**
    * Close the scratchpad window. Prompts before closing if the scratchpad
    * has unsaved changes.
    *
    * @param function aCallback
    *        Optional function you want to call when file is saved
@@ -1345,17 +1332,16 @@ var Scratchpad = {
   {
     this.promptSave(function(aShouldClose, aSaved, aStatus) {
       let shouldClose = aShouldClose;
       if (aSaved && !Components.isSuccessCode(aStatus)) {
         shouldClose = false;
       }
 
       if (shouldClose) {
-        this._skipClosePrompt = true;
         window.close();
       }
       if (aCallback) {
         aCallback();
       }
     }.bind(this));
   },
 
--- a/browser/devtools/scratchpad/test/browser_scratchpad_bug_653427_confirm_close.js
+++ b/browser/devtools/scratchpad/test/browser_scratchpad_bug_653427_confirm_close.js
@@ -4,17 +4,17 @@
 
 let tempScope = {};
 Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
 Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
 let NetUtil = tempScope.NetUtil;
 let FileUtils = tempScope.FileUtils;
 
 // only finish() when correct number of tests are done
-const expected = 5;
+const expected = 6;
 var count = 0;
 function done()
 {
   if (++count == expected) {
     cleanup();
     finish();
   }
 }
@@ -64,16 +64,17 @@ function testSavedFile()
       done();
     });
   }, {noFocus: true});
 }
 
 function testUnsaved()
 {
   testUnsavedFileCancel();
+  testCancelAfterLoad();
   testUnsavedFileSave();
   testUnsavedFileDontSave();
 }
 
 function testUnsavedFileCancel()
 {
   openScratchpad(function(win) {
     win.Scratchpad.setFilename("test.js");
@@ -84,16 +85,44 @@ function testUnsavedFileCancel()
     win.Scratchpad.close(function() {
       ok(!win.closed, "cancelling dialog shouldn't close scratchpad");
       win.close();
       done();
     });
   }, {noFocus: true});
 }
 
+// Test a regression where our confirmation dialog wasn't appearing
+// after openFile calls. See bug 801982.
+function testCancelAfterLoad()
+{
+  openScratchpad(function(win) {
+    win.Scratchpad.setRecentFile(gFile);
+    win.Scratchpad.openFile(0);
+    win.Scratchpad.editor.dirty = true;
+    promptButton = win.BUTTON_POSITION_CANCEL;
+
+    let EventStub = {
+      called: false,
+      preventDefault: function() {
+        EventStub.called = true;
+      }
+    };
+
+    win.Scratchpad.onClose(EventStub, function() {
+      ok(!win.closed, "cancelling dialog shouldn't close scratchpad");
+      ok(EventStub.called, "aEvent.preventDefault was called");
+
+      win.Scratchpad.editor.dirty = false;
+      win.close();
+      done();
+    });
+  }, {noFocus: true});
+}
+
 function testUnsavedFileSave()
 {
   openScratchpad(function(win) {
     win.Scratchpad.importFromFile(gFile, true, function(status, content) {
       win.Scratchpad.setFilename(gFile.path);
 
       let text = "new text";
       win.Scratchpad.setText(text);