browser/devtools/scratchpad/test/browser_scratchpad_files.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
michael@0 5 let tempScope = {};
michael@0 6 Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
michael@0 7 let NetUtil = tempScope.NetUtil;
michael@0 8
michael@0 9 // Reference to the Scratchpad object.
michael@0 10 let gScratchpad;
michael@0 11
michael@0 12 // Reference to the temporary nsIFile we will work with.
michael@0 13 let gFile;
michael@0 14
michael@0 15 // The temporary file content.
michael@0 16 let gFileContent = "hello.world('bug636725');";
michael@0 17
michael@0 18 function test()
michael@0 19 {
michael@0 20 waitForExplicitFinish();
michael@0 21
michael@0 22 gBrowser.selectedTab = gBrowser.addTab();
michael@0 23 gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
michael@0 24 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
michael@0 25 openScratchpad(runTests);
michael@0 26 }, true);
michael@0 27
michael@0 28 content.location = "data:text/html,<p>test file open and save in Scratchpad";
michael@0 29 }
michael@0 30
michael@0 31 function runTests()
michael@0 32 {
michael@0 33 gScratchpad = gScratchpadWindow.Scratchpad;
michael@0 34
michael@0 35 createTempFile("fileForBug636725.tmp", gFileContent, function(aStatus, aFile) {
michael@0 36 ok(Components.isSuccessCode(aStatus),
michael@0 37 "The temporary file was saved successfully");
michael@0 38
michael@0 39 gFile = aFile;
michael@0 40 gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
michael@0 41 fileImported);
michael@0 42 });
michael@0 43 }
michael@0 44
michael@0 45 function fileImported(aStatus, aFileContent)
michael@0 46 {
michael@0 47 ok(Components.isSuccessCode(aStatus),
michael@0 48 "the temporary file was imported successfully with Scratchpad");
michael@0 49
michael@0 50 is(aFileContent, gFileContent,
michael@0 51 "received data is correct");
michael@0 52
michael@0 53 is(gScratchpad.getText(), gFileContent,
michael@0 54 "the editor content is correct");
michael@0 55
michael@0 56 is(gScratchpad.dirty, false,
michael@0 57 "the editor marks imported file as saved");
michael@0 58
michael@0 59 // Save the file after changes.
michael@0 60 gFileContent += "// omg, saved!";
michael@0 61 gScratchpad.editor.setText(gFileContent);
michael@0 62
michael@0 63 gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), true, true,
michael@0 64 fileExported);
michael@0 65 }
michael@0 66
michael@0 67 function fileExported(aStatus)
michael@0 68 {
michael@0 69 ok(Components.isSuccessCode(aStatus),
michael@0 70 "the temporary file was exported successfully with Scratchpad");
michael@0 71
michael@0 72 let oldContent = gFileContent;
michael@0 73
michael@0 74 // Attempt another file save, with confirmation which returns false.
michael@0 75 gFileContent += "// omg, saved twice!";
michael@0 76 gScratchpad.editor.setText(gFileContent);
michael@0 77
michael@0 78 let oldConfirm = gScratchpadWindow.confirm;
michael@0 79 let askedConfirmation = false;
michael@0 80 gScratchpadWindow.confirm = function() {
michael@0 81 askedConfirmation = true;
michael@0 82 return false;
michael@0 83 };
michael@0 84
michael@0 85 gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), false, true,
michael@0 86 fileExported2);
michael@0 87
michael@0 88 gScratchpadWindow.confirm = oldConfirm;
michael@0 89
michael@0 90 ok(askedConfirmation, "exportToFile() asked for overwrite confirmation");
michael@0 91
michael@0 92 gFileContent = oldContent;
michael@0 93
michael@0 94 let channel = NetUtil.newChannel(gFile);
michael@0 95 channel.contentType = "application/javascript";
michael@0 96
michael@0 97 // Read back the temporary file.
michael@0 98 NetUtil.asyncFetch(channel, fileRead);
michael@0 99 }
michael@0 100
michael@0 101 function fileExported2()
michael@0 102 {
michael@0 103 ok(false, "exportToFile() did not cancel file overwrite");
michael@0 104 }
michael@0 105
michael@0 106 function fileRead(aInputStream, aStatus)
michael@0 107 {
michael@0 108 ok(Components.isSuccessCode(aStatus),
michael@0 109 "the temporary file was read back successfully");
michael@0 110
michael@0 111 let updatedContent =
michael@0 112 NetUtil.readInputStreamToString(aInputStream, aInputStream.available());;
michael@0 113
michael@0 114 is(updatedContent, gFileContent, "file properly updated");
michael@0 115
michael@0 116 // Done!
michael@0 117 gFile.remove(false);
michael@0 118 gFile = null;
michael@0 119 gScratchpad = null;
michael@0 120 finish();
michael@0 121 }

mercurial