summaryrefslogtreecommitdiffstats
path: root/devtools/client/scratchpad/test/browser_scratchpad_confirm_close.js
blob: a6318fa7595a7bc0a265c5c60e296130f3db1600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
/* Bug 653427 */

var tempScope = {};
Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
var NetUtil = tempScope.NetUtil;
var FileUtils = tempScope.FileUtils;

// only finish() when correct number of tests are done
const expected = 9;
var count = 0;
function done()
{
  if (++count == expected) {
    cleanup();
    finish();
  }
}

var gFile;

var oldPrompt = Services.prompt;
var promptButton = -1;

function test()
{
  waitForExplicitFinish();

  gFile = createTempFile("fileForBug653427.tmp");
  writeFile(gFile, "text", testUnsaved.call(this));

  Services.prompt = {
    confirmEx: function () {
      return promptButton;
    }
  };

  testNew();
  testSavedFile();

  gBrowser.selectedTab = gBrowser.addTab();
  content.location = "data:text/html,<p>test scratchpad save file prompt on closing";
}

function testNew()
{
  openScratchpad(function (win) {
    win.Scratchpad.close(function () {
      ok(win.closed, "new scratchpad window should close without prompting");
      done();
    });
  }, {noFocus: true});
}

function testSavedFile()
{
  openScratchpad(function (win) {
    win.Scratchpad.filename = "test.js";
    win.Scratchpad.editor.dirty = false;
    win.Scratchpad.close(function () {
      ok(win.closed, "scratchpad from file with no changes should close");
      done();
    });
  }, {noFocus: true});
}

function testUnsaved()
{
  function setFilename(aScratchpad, aFile) {
    aScratchpad.setFilename(aFile);
  }

  testUnsavedFileCancel(setFilename);
  testUnsavedFileSave(setFilename);
  testUnsavedFileDontSave(setFilename);
  testCancelAfterLoad();

  function mockSaveFile(aScratchpad) {
    let SaveFileStub = function (aCallback) {
      /*
       * An argument for aCallback must pass Components.isSuccessCode
       *
       * A version of isSuccessCode in JavaScript:
       *  function isSuccessCode(returnCode) {
       *    return (returnCode & 0x80000000) == 0;
       *  }
       */
      aCallback(1);
    };

    aScratchpad.saveFile = SaveFileStub;
  }

  // Run these tests again but this time without setting a filename to
  // test that Scratchpad always asks for confirmation on dirty editor.
  testUnsavedFileCancel(mockSaveFile);
  testUnsavedFileSave(mockSaveFile);
  testUnsavedFileDontSave();
}

function testUnsavedFileCancel(aCallback = function () {})
{
  openScratchpad(function (win) {
    aCallback(win.Scratchpad, "test.js");
    win.Scratchpad.editor.dirty = true;

    promptButton = win.BUTTON_POSITION_CANCEL;

    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(aCallback = function () {})
{
  openScratchpad(function (win) {
    win.Scratchpad.importFromFile(gFile, true, function (status, content) {
      aCallback(win.Scratchpad, gFile.path);

      let text = "new text";
      win.Scratchpad.setText(text);

      promptButton = win.BUTTON_POSITION_SAVE;

      win.Scratchpad.close(function () {
        ok(win.closed, 'pressing "Save" in dialog should close scratchpad');
        readFile(gFile, function (savedContent) {
          is(savedContent, text, 'prompted "Save" worked when closing scratchpad');
          done();
        });
      });
    });
  }, {noFocus: true});
}

function testUnsavedFileDontSave(aCallback = function () {})
{
  openScratchpad(function (win) {
    aCallback(win.Scratchpad, gFile.path);
    win.Scratchpad.editor.dirty = true;

    promptButton = win.BUTTON_POSITION_DONT_SAVE;

    win.Scratchpad.close(function () {
      ok(win.closed, 'pressing "Don\'t Save" in dialog should close scratchpad');
      done();
    });
  }, {noFocus: true});
}

function cleanup()
{
  Services.prompt = oldPrompt;
  gFile.remove(false);
  gFile = null;
}

function createTempFile(name)
{
  let file = FileUtils.getFile("TmpD", [name]);
  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
  file.QueryInterface(Ci.nsILocalFile);
  return file;
}

function writeFile(file, content, callback)
{
  let fout = Cc["@mozilla.org/network/file-output-stream;1"].
             createInstance(Ci.nsIFileOutputStream);
  fout.init(file.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
            0o644, fout.DEFER_OPEN);

  let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
                  createInstance(Ci.nsIScriptableUnicodeConverter);
  converter.charset = "UTF-8";
  let fileContentStream = converter.convertToInputStream(content);

  NetUtil.asyncCopy(fileContentStream, fout, callback);
}

function readFile(file, callback)
{
  let channel = NetUtil.newChannel({
    uri: NetUtil.newURI(file),
    loadUsingSystemPrincipal: true});
  channel.contentType = "application/javascript";

  NetUtil.asyncFetch(channel, function (inputStream, status) {
    ok(Components.isSuccessCode(status),
       "file was read successfully");

    let content = NetUtil.readInputStreamToString(inputStream,
                                                  inputStream.available());
    callback(content);
  });
}