browser/devtools/scratchpad/test/browser_scratchpad_revert_to_saved.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 751744 */
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 // Reference to the Scratchpad object.
michael@0 13 let gScratchpad;
michael@0 14
michael@0 15 // Reference to the temporary nsIFiles.
michael@0 16 let gFile;
michael@0 17
michael@0 18 // Temporary file name.
michael@0 19 let gFileName = "testFileForBug751744.tmp"
michael@0 20
michael@0 21
michael@0 22 // Content for the temporary file.
michael@0 23 let gFileContent = "/* this file is already saved */\n" +
michael@0 24 "function foo() { alert('bar') }";
michael@0 25 let gLength = gFileContent.length;
michael@0 26
michael@0 27 // Reference to the menu entry.
michael@0 28 let menu;
michael@0 29
michael@0 30 function startTest()
michael@0 31 {
michael@0 32 gScratchpad = gScratchpadWindow.Scratchpad;
michael@0 33 menu = gScratchpadWindow.document.getElementById("sp-cmd-revert");
michael@0 34 createAndLoadTemporaryFile();
michael@0 35 }
michael@0 36
michael@0 37 function testAfterSaved() {
michael@0 38 // Check if the revert menu is disabled as the file is at saved state.
michael@0 39 ok(menu.hasAttribute("disabled"), "The revert menu entry is disabled.");
michael@0 40
michael@0 41 // chancging the text in the file
michael@0 42 gScratchpad.setText(gScratchpad.getText() + "\nfoo();");
michael@0 43 // Checking the text got changed
michael@0 44 is(gScratchpad.getText(), gFileContent + "\nfoo();",
michael@0 45 "The text changed the first time.");
michael@0 46
michael@0 47 // Revert menu now should be enabled.
michael@0 48 ok(!menu.hasAttribute("disabled"),
michael@0 49 "The revert menu entry is enabled after changing text first time");
michael@0 50
michael@0 51 // reverting back to last saved state.
michael@0 52 gScratchpad.revertFile(testAfterRevert);
michael@0 53 }
michael@0 54
michael@0 55 function testAfterRevert() {
michael@0 56 // Check if the file's text got reverted
michael@0 57 is(gScratchpad.getText(), gFileContent,
michael@0 58 "The text reverted back to original text.");
michael@0 59 // The revert menu should be disabled again.
michael@0 60 ok(menu.hasAttribute("disabled"),
michael@0 61 "The revert menu entry is disabled after reverting.");
michael@0 62
michael@0 63 // chancging the text in the file again
michael@0 64 gScratchpad.setText(gScratchpad.getText() + "\nalert(foo.toSource());");
michael@0 65 // Saving the file.
michael@0 66 gScratchpad.saveFile(testAfterSecondSave);
michael@0 67 }
michael@0 68
michael@0 69 function testAfterSecondSave() {
michael@0 70 // revert menu entry should be disabled.
michael@0 71 ok(menu.hasAttribute("disabled"),
michael@0 72 "The revert menu entry is disabled after saving.");
michael@0 73
michael@0 74 // changing the text.
michael@0 75 gScratchpad.setText(gScratchpad.getText() + "\nfoo();");
michael@0 76
michael@0 77 // revert menu entry should get enabled yet again.
michael@0 78 ok(!menu.hasAttribute("disabled"),
michael@0 79 "The revert menu entry is enabled after changing text third time");
michael@0 80
michael@0 81 // reverting back to last saved state.
michael@0 82 gScratchpad.revertFile(testAfterSecondRevert);
michael@0 83 }
michael@0 84
michael@0 85 function testAfterSecondRevert() {
michael@0 86 // Check if the file's text got reverted
michael@0 87 is(gScratchpad.getText(), gFileContent + "\nalert(foo.toSource());",
michael@0 88 "The text reverted back to the changed saved text.");
michael@0 89 // The revert menu should be disabled again.
michael@0 90 ok(menu.hasAttribute("disabled"),
michael@0 91 "Revert menu entry is disabled after reverting to changed saved state.");
michael@0 92 gFile.remove(false);
michael@0 93 gFile = gScratchpad = menu = null;
michael@0 94 finish();
michael@0 95 }
michael@0 96
michael@0 97 function createAndLoadTemporaryFile()
michael@0 98 {
michael@0 99 // Create a temporary file.
michael@0 100 gFile = FileUtils.getFile("TmpD", [gFileName]);
michael@0 101 gFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
michael@0 102
michael@0 103 // Write the temporary file.
michael@0 104 let fout = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 105 createInstance(Ci.nsIFileOutputStream);
michael@0 106 fout.init(gFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
michael@0 107 0o644, fout.DEFER_OPEN);
michael@0 108
michael@0 109 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
michael@0 110 createInstance(Ci.nsIScriptableUnicodeConverter);
michael@0 111 converter.charset = "UTF-8";
michael@0 112 let fileContentStream = converter.convertToInputStream(gFileContent);
michael@0 113
michael@0 114 NetUtil.asyncCopy(fileContentStream, fout, tempFileSaved);
michael@0 115 }
michael@0 116
michael@0 117 function tempFileSaved(aStatus)
michael@0 118 {
michael@0 119 ok(Components.isSuccessCode(aStatus),
michael@0 120 "the temporary file was saved successfully");
michael@0 121
michael@0 122 // Import the file into Scratchpad.
michael@0 123 gScratchpad.setFilename(gFile.path);
michael@0 124 gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
michael@0 125 testAfterSaved);
michael@0 126 }
michael@0 127
michael@0 128 function test()
michael@0 129 {
michael@0 130 waitForExplicitFinish();
michael@0 131
michael@0 132 gBrowser.selectedTab = gBrowser.addTab();
michael@0 133 gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
michael@0 134 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
michael@0 135 openScratchpad(startTest);
michael@0 136 }, true);
michael@0 137
michael@0 138 content.location = "data:text/html,<p>test reverting to last saved state of" +
michael@0 139 " a file </p>";
michael@0 140 }

mercurial