browser_scratchpad_confirm_close.js 5.9 KB

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