browser/devtools/scratchpad/test/browser_scratchpad_files.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_files.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +/* vim: set ts=2 et sw=2 tw=80: */
     1.5 +/* Any copyright is dedicated to the Public Domain.
     1.6 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.7 +
     1.8 +let tempScope = {};
     1.9 +Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
    1.10 +let NetUtil = tempScope.NetUtil;
    1.11 +
    1.12 +// Reference to the Scratchpad object.
    1.13 +let gScratchpad;
    1.14 +
    1.15 +// Reference to the temporary nsIFile we will work with.
    1.16 +let gFile;
    1.17 +
    1.18 +// The temporary file content.
    1.19 +let gFileContent = "hello.world('bug636725');";
    1.20 +
    1.21 +function test()
    1.22 +{
    1.23 +  waitForExplicitFinish();
    1.24 +
    1.25 +  gBrowser.selectedTab = gBrowser.addTab();
    1.26 +  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    1.27 +    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    1.28 +    openScratchpad(runTests);
    1.29 +  }, true);
    1.30 +
    1.31 +  content.location = "data:text/html,<p>test file open and save in Scratchpad";
    1.32 +}
    1.33 +
    1.34 +function runTests()
    1.35 +{
    1.36 +  gScratchpad = gScratchpadWindow.Scratchpad;
    1.37 +
    1.38 +  createTempFile("fileForBug636725.tmp", gFileContent, function(aStatus, aFile) {
    1.39 +    ok(Components.isSuccessCode(aStatus),
    1.40 +      "The temporary file was saved successfully");
    1.41 +
    1.42 +      gFile = aFile;
    1.43 +      gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
    1.44 +        fileImported);
    1.45 +  });
    1.46 +}
    1.47 +
    1.48 +function fileImported(aStatus, aFileContent)
    1.49 +{
    1.50 +  ok(Components.isSuccessCode(aStatus),
    1.51 +     "the temporary file was imported successfully with Scratchpad");
    1.52 +
    1.53 +  is(aFileContent, gFileContent,
    1.54 +     "received data is correct");
    1.55 +
    1.56 +  is(gScratchpad.getText(), gFileContent,
    1.57 +     "the editor content is correct");
    1.58 +
    1.59 +  is(gScratchpad.dirty, false,
    1.60 +     "the editor marks imported file as saved");
    1.61 +
    1.62 +  // Save the file after changes.
    1.63 +  gFileContent += "// omg, saved!";
    1.64 +  gScratchpad.editor.setText(gFileContent);
    1.65 +
    1.66 +  gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), true, true,
    1.67 +                          fileExported);
    1.68 +}
    1.69 +
    1.70 +function fileExported(aStatus)
    1.71 +{
    1.72 +  ok(Components.isSuccessCode(aStatus),
    1.73 +     "the temporary file was exported successfully with Scratchpad");
    1.74 +
    1.75 +  let oldContent = gFileContent;
    1.76 +
    1.77 +  // Attempt another file save, with confirmation which returns false.
    1.78 +  gFileContent += "// omg, saved twice!";
    1.79 +  gScratchpad.editor.setText(gFileContent);
    1.80 +
    1.81 +  let oldConfirm = gScratchpadWindow.confirm;
    1.82 +  let askedConfirmation = false;
    1.83 +  gScratchpadWindow.confirm = function() {
    1.84 +    askedConfirmation = true;
    1.85 +    return false;
    1.86 +  };
    1.87 +
    1.88 +  gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), false, true,
    1.89 +                          fileExported2);
    1.90 +
    1.91 +  gScratchpadWindow.confirm = oldConfirm;
    1.92 +
    1.93 +  ok(askedConfirmation, "exportToFile() asked for overwrite confirmation");
    1.94 +
    1.95 +  gFileContent = oldContent;
    1.96 +
    1.97 +  let channel = NetUtil.newChannel(gFile);
    1.98 +  channel.contentType = "application/javascript";
    1.99 +
   1.100 +  // Read back the temporary file.
   1.101 +  NetUtil.asyncFetch(channel, fileRead);
   1.102 +}
   1.103 +
   1.104 +function fileExported2()
   1.105 +{
   1.106 +  ok(false, "exportToFile() did not cancel file overwrite");
   1.107 +}
   1.108 +
   1.109 +function fileRead(aInputStream, aStatus)
   1.110 +{
   1.111 +  ok(Components.isSuccessCode(aStatus),
   1.112 +     "the temporary file was read back successfully");
   1.113 +
   1.114 +  let updatedContent =
   1.115 +    NetUtil.readInputStreamToString(aInputStream, aInputStream.available());;
   1.116 +
   1.117 +  is(updatedContent, gFileContent, "file properly updated");
   1.118 +
   1.119 +  // Done!
   1.120 +  gFile.remove(false);
   1.121 +  gFile = null;
   1.122 +  gScratchpad = null;
   1.123 +  finish();
   1.124 +}

mercurial