summaryrefslogtreecommitdiffstats
path: root/devtools/client/scratchpad/test/browser_scratchpad_revert_to_saved.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /devtools/client/scratchpad/test/browser_scratchpad_revert_to_saved.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'devtools/client/scratchpad/test/browser_scratchpad_revert_to_saved.js')
-rw-r--r--devtools/client/scratchpad/test/browser_scratchpad_revert_to_saved.js134
1 files changed, 134 insertions, 0 deletions
diff --git a/devtools/client/scratchpad/test/browser_scratchpad_revert_to_saved.js b/devtools/client/scratchpad/test/browser_scratchpad_revert_to_saved.js
new file mode 100644
index 000000000..92c6c3720
--- /dev/null
+++ b/devtools/client/scratchpad/test/browser_scratchpad_revert_to_saved.js
@@ -0,0 +1,134 @@
+/* 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 751744 */
+
+// Reference to the Scratchpad object.
+var gScratchpad;
+
+// Reference to the temporary nsIFiles.
+var gFile;
+
+// Temporary file name.
+var gFileName = "testFileForBug751744.tmp";
+
+
+// Content for the temporary file.
+var gFileContent = "/* this file is already saved */\n" +
+ "function foo() { alert('bar') }";
+var gLength = gFileContent.length;
+
+// Reference to the menu entry.
+var menu;
+
+function startTest()
+{
+ gScratchpad = gScratchpadWindow.Scratchpad;
+ menu = gScratchpadWindow.document.getElementById("sp-cmd-revert");
+ createAndLoadTemporaryFile();
+}
+
+function testAfterSaved() {
+ // Check if the revert menu is disabled as the file is at saved state.
+ ok(menu.hasAttribute("disabled"), "The revert menu entry is disabled.");
+
+ // chancging the text in the file
+ gScratchpad.setText(gScratchpad.getText() + "\nfoo();");
+ // Checking the text got changed
+ is(gScratchpad.getText(), gFileContent + "\nfoo();",
+ "The text changed the first time.");
+
+ // Revert menu now should be enabled.
+ ok(!menu.hasAttribute("disabled"),
+ "The revert menu entry is enabled after changing text first time");
+
+ // reverting back to last saved state.
+ gScratchpad.revertFile(testAfterRevert);
+}
+
+function testAfterRevert() {
+ // Check if the file's text got reverted
+ is(gScratchpad.getText(), gFileContent,
+ "The text reverted back to original text.");
+ // The revert menu should be disabled again.
+ ok(menu.hasAttribute("disabled"),
+ "The revert menu entry is disabled after reverting.");
+
+ // chancging the text in the file again
+ gScratchpad.setText(gScratchpad.getText() + "\nalert(foo.toSource());");
+ // Saving the file.
+ gScratchpad.saveFile(testAfterSecondSave);
+}
+
+function testAfterSecondSave() {
+ // revert menu entry should be disabled.
+ ok(menu.hasAttribute("disabled"),
+ "The revert menu entry is disabled after saving.");
+
+ // changing the text.
+ gScratchpad.setText(gScratchpad.getText() + "\nfoo();");
+
+ // revert menu entry should get enabled yet again.
+ ok(!menu.hasAttribute("disabled"),
+ "The revert menu entry is enabled after changing text third time");
+
+ // reverting back to last saved state.
+ gScratchpad.revertFile(testAfterSecondRevert);
+}
+
+function testAfterSecondRevert() {
+ // Check if the file's text got reverted
+ is(gScratchpad.getText(), gFileContent + "\nalert(foo.toSource());",
+ "The text reverted back to the changed saved text.");
+ // The revert menu should be disabled again.
+ ok(menu.hasAttribute("disabled"),
+ "Revert menu entry is disabled after reverting to changed saved state.");
+ gFile.remove(false);
+ gFile = gScratchpad = menu = null;
+ finish();
+}
+
+function createAndLoadTemporaryFile()
+{
+ // Create a temporary file.
+ gFile = FileUtils.getFile("TmpD", [gFileName]);
+ gFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+
+ // Write the temporary file.
+ let fout = Cc["@mozilla.org/network/file-output-stream;1"].
+ createInstance(Ci.nsIFileOutputStream);
+ fout.init(gFile.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(gFileContent);
+
+ NetUtil.asyncCopy(fileContentStream, fout, tempFileSaved);
+}
+
+function tempFileSaved(aStatus)
+{
+ ok(Components.isSuccessCode(aStatus),
+ "the temporary file was saved successfully");
+
+ // Import the file into Scratchpad.
+ gScratchpad.setFilename(gFile.path);
+ gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
+ testAfterSaved);
+}
+
+function test()
+{
+ waitForExplicitFinish();
+
+ gBrowser.selectedTab = gBrowser.addTab();
+ gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
+ gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
+ openScratchpad(startTest);
+ }, true);
+
+ content.location = "data:text/html,<p>test reverting to last saved state of" +
+ " a file </p>";
+}