summaryrefslogtreecommitdiffstats
path: root/devtools/client/scratchpad/test/browser_scratchpad_revert_to_saved.js
blob: 92c6c37200d658d744ed49aaf360ca71c57cf992 (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
/* 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>";
}