browser/devtools/scratchpad/test/browser_scratchpad_confirm_close.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 2 /* Any copyright is dedicated to the Public Domain.
michael@0 3 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 4 /* Bug 653427 */
michael@0 5
michael@0 6 let tempScope = {};
michael@0 7 Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
michael@0 8 Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
michael@0 9 let NetUtil = tempScope.NetUtil;
michael@0 10 let FileUtils = tempScope.FileUtils;
michael@0 11
michael@0 12 // only finish() when correct number of tests are done
michael@0 13 const expected = 9;
michael@0 14 var count = 0;
michael@0 15 function done()
michael@0 16 {
michael@0 17 if (++count == expected) {
michael@0 18 cleanup();
michael@0 19 finish();
michael@0 20 }
michael@0 21 }
michael@0 22
michael@0 23 var gFile;
michael@0 24
michael@0 25 var oldPrompt = Services.prompt;
michael@0 26 var promptButton = -1;
michael@0 27
michael@0 28 function test()
michael@0 29 {
michael@0 30 waitForExplicitFinish();
michael@0 31
michael@0 32 gFile = createTempFile("fileForBug653427.tmp");
michael@0 33 writeFile(gFile, "text", testUnsaved.call(this));
michael@0 34
michael@0 35 Services.prompt = {
michael@0 36 confirmEx: function() {
michael@0 37 return promptButton;
michael@0 38 }
michael@0 39 };
michael@0 40
michael@0 41 testNew();
michael@0 42 testSavedFile();
michael@0 43
michael@0 44 gBrowser.selectedTab = gBrowser.addTab();
michael@0 45 content.location = "data:text/html,<p>test scratchpad save file prompt on closing";
michael@0 46 }
michael@0 47
michael@0 48 function testNew()
michael@0 49 {
michael@0 50 openScratchpad(function(win) {
michael@0 51 win.Scratchpad.close(function() {
michael@0 52 ok(win.closed, "new scratchpad window should close without prompting")
michael@0 53 done();
michael@0 54 });
michael@0 55 }, {noFocus: true});
michael@0 56 }
michael@0 57
michael@0 58 function testSavedFile()
michael@0 59 {
michael@0 60 openScratchpad(function(win) {
michael@0 61 win.Scratchpad.filename = "test.js";
michael@0 62 win.Scratchpad.editor.dirty = false;
michael@0 63 win.Scratchpad.close(function() {
michael@0 64 ok(win.closed, "scratchpad from file with no changes should close")
michael@0 65 done();
michael@0 66 });
michael@0 67 }, {noFocus: true});
michael@0 68 }
michael@0 69
michael@0 70 function testUnsaved()
michael@0 71 {
michael@0 72 function setFilename(aScratchpad, aFile) {
michael@0 73 aScratchpad.setFilename(aFile);
michael@0 74 }
michael@0 75
michael@0 76 testUnsavedFileCancel(setFilename);
michael@0 77 testUnsavedFileSave(setFilename);
michael@0 78 testUnsavedFileDontSave(setFilename);
michael@0 79 testCancelAfterLoad();
michael@0 80
michael@0 81 function mockSaveFile(aScratchpad) {
michael@0 82 let SaveFileStub = function (aCallback) {
michael@0 83 /*
michael@0 84 * An argument for aCallback must pass Components.isSuccessCode
michael@0 85 *
michael@0 86 * A version of isSuccessCode in JavaScript:
michael@0 87 * function isSuccessCode(returnCode) {
michael@0 88 * return (returnCode & 0x80000000) == 0;
michael@0 89 * }
michael@0 90 */
michael@0 91 aCallback(1);
michael@0 92 };
michael@0 93
michael@0 94 aScratchpad.saveFile = SaveFileStub;
michael@0 95 }
michael@0 96
michael@0 97 // Run these tests again but this time without setting a filename to
michael@0 98 // test that Scratchpad always asks for confirmation on dirty editor.
michael@0 99 testUnsavedFileCancel(mockSaveFile);
michael@0 100 testUnsavedFileSave(mockSaveFile);
michael@0 101 testUnsavedFileDontSave();
michael@0 102 }
michael@0 103
michael@0 104 function testUnsavedFileCancel(aCallback=function () {})
michael@0 105 {
michael@0 106 openScratchpad(function(win) {
michael@0 107 aCallback(win.Scratchpad, "test.js");
michael@0 108 win.Scratchpad.editor.dirty = true;
michael@0 109
michael@0 110 promptButton = win.BUTTON_POSITION_CANCEL;
michael@0 111
michael@0 112 win.Scratchpad.close(function() {
michael@0 113 ok(!win.closed, "cancelling dialog shouldn't close scratchpad");
michael@0 114 win.close();
michael@0 115 done();
michael@0 116 });
michael@0 117 }, {noFocus: true});
michael@0 118 }
michael@0 119
michael@0 120 // Test a regression where our confirmation dialog wasn't appearing
michael@0 121 // after openFile calls. See bug 801982.
michael@0 122 function testCancelAfterLoad()
michael@0 123 {
michael@0 124 openScratchpad(function(win) {
michael@0 125 win.Scratchpad.setRecentFile(gFile);
michael@0 126 win.Scratchpad.openFile(0);
michael@0 127 win.Scratchpad.editor.dirty = true;
michael@0 128 promptButton = win.BUTTON_POSITION_CANCEL;
michael@0 129
michael@0 130 let EventStub = {
michael@0 131 called: false,
michael@0 132 preventDefault: function() {
michael@0 133 EventStub.called = true;
michael@0 134 }
michael@0 135 };
michael@0 136
michael@0 137 win.Scratchpad.onClose(EventStub, function() {
michael@0 138 ok(!win.closed, "cancelling dialog shouldn't close scratchpad");
michael@0 139 ok(EventStub.called, "aEvent.preventDefault was called");
michael@0 140
michael@0 141 win.Scratchpad.editor.dirty = false;
michael@0 142 win.close();
michael@0 143 done();
michael@0 144 });
michael@0 145 }, {noFocus: true});
michael@0 146 }
michael@0 147
michael@0 148 function testUnsavedFileSave(aCallback=function () {})
michael@0 149 {
michael@0 150 openScratchpad(function(win) {
michael@0 151 win.Scratchpad.importFromFile(gFile, true, function(status, content) {
michael@0 152 aCallback(win.Scratchpad, gFile.path);
michael@0 153
michael@0 154 let text = "new text";
michael@0 155 win.Scratchpad.setText(text);
michael@0 156
michael@0 157 promptButton = win.BUTTON_POSITION_SAVE;
michael@0 158
michael@0 159 win.Scratchpad.close(function() {
michael@0 160 ok(win.closed, 'pressing "Save" in dialog should close scratchpad');
michael@0 161 readFile(gFile, function(savedContent) {
michael@0 162 is(savedContent, text, 'prompted "Save" worked when closing scratchpad');
michael@0 163 done();
michael@0 164 });
michael@0 165 });
michael@0 166 });
michael@0 167 }, {noFocus: true});
michael@0 168 }
michael@0 169
michael@0 170 function testUnsavedFileDontSave(aCallback=function () {})
michael@0 171 {
michael@0 172 openScratchpad(function(win) {
michael@0 173 aCallback(win.Scratchpad, gFile.path);
michael@0 174 win.Scratchpad.editor.dirty = true;
michael@0 175
michael@0 176 promptButton = win.BUTTON_POSITION_DONT_SAVE;
michael@0 177
michael@0 178 win.Scratchpad.close(function() {
michael@0 179 ok(win.closed, 'pressing "Don\'t Save" in dialog should close scratchpad');
michael@0 180 done();
michael@0 181 });
michael@0 182 }, {noFocus: true});
michael@0 183 }
michael@0 184
michael@0 185 function cleanup()
michael@0 186 {
michael@0 187 Services.prompt = oldPrompt;
michael@0 188 gFile.remove(false);
michael@0 189 gFile = null;
michael@0 190 }
michael@0 191
michael@0 192 function createTempFile(name)
michael@0 193 {
michael@0 194 let file = FileUtils.getFile("TmpD", [name]);
michael@0 195 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 196 file.QueryInterface(Ci.nsILocalFile)
michael@0 197 return file;
michael@0 198 }
michael@0 199
michael@0 200 function writeFile(file, content, callback)
michael@0 201 {
michael@0 202 let fout = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 203 createInstance(Ci.nsIFileOutputStream);
michael@0 204 fout.init(file.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
michael@0 205 0644, fout.DEFER_OPEN);
michael@0 206
michael@0 207 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
michael@0 208 createInstance(Ci.nsIScriptableUnicodeConverter);
michael@0 209 converter.charset = "UTF-8";
michael@0 210 let fileContentStream = converter.convertToInputStream(content);
michael@0 211
michael@0 212 NetUtil.asyncCopy(fileContentStream, fout, callback);
michael@0 213 }
michael@0 214
michael@0 215 function readFile(file, callback)
michael@0 216 {
michael@0 217 let channel = NetUtil.newChannel(file);
michael@0 218 channel.contentType = "application/javascript";
michael@0 219
michael@0 220 NetUtil.asyncFetch(channel, function(inputStream, status) {
michael@0 221 ok(Components.isSuccessCode(status),
michael@0 222 "file was read successfully");
michael@0 223
michael@0 224 let content = NetUtil.readInputStreamToString(inputStream,
michael@0 225 inputStream.available());
michael@0 226 callback(content);
michael@0 227 });
michael@0 228 }

mercurial