summaryrefslogtreecommitdiffstats
path: root/devtools/client/scratchpad/test/browser_scratchpad_reset_undo.js
blob: a1b60cd336cec54857afc2b4f5d45a19e5167d02 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* 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 684546 */

// Reference to the Scratchpad chrome window object.
var gScratchpadWindow;

// Reference to the Scratchpad object.
var gScratchpad;

// Reference to the temporary nsIFile we will work with.
var gFileA;
var gFileB;

// The temporary file content.
var gFileAContent = "// File A ** Hello World!";
var gFileBContent = "// File B ** Goodbye All";

// Help track if one or both files are saved
var gFirstFileSaved = false;

function test()
{
  waitForExplicitFinish();

  gBrowser.selectedTab = gBrowser.addTab();
  gBrowser.selectedBrowser.addEventListener("load", function browserLoad() {
    gBrowser.selectedBrowser.removeEventListener("load", browserLoad, true);
    openScratchpad(runTests);
  }, true);

  content.location = "data:text/html,<p>test that undo get's reset after file load in Scratchpad";
}

function runTests()
{
  gScratchpad = gScratchpadWindow.Scratchpad;

  // Create a temporary file.
  gFileA = FileUtils.getFile("TmpD", ["fileAForBug684546.tmp"]);
  gFileA.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);

  gFileB = FileUtils.getFile("TmpD", ["fileBForBug684546.tmp"]);
  gFileB.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);

  // Write the temporary file.
  let foutA = Cc["@mozilla.org/network/file-output-stream;1"].
             createInstance(Ci.nsIFileOutputStream);
  foutA.init(gFileA.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
            0o644, foutA.DEFER_OPEN);

  let foutB = Cc["@mozilla.org/network/file-output-stream;1"].
             createInstance(Ci.nsIFileOutputStream);
  foutB.init(gFileB.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
            0o644, foutB.DEFER_OPEN);

  let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
                  createInstance(Ci.nsIScriptableUnicodeConverter);
  converter.charset = "UTF-8";
  let fileContentStreamA = converter.convertToInputStream(gFileAContent);
  let fileContentStreamB = converter.convertToInputStream(gFileBContent);

  NetUtil.asyncCopy(fileContentStreamA, foutA, tempFileSaved);
  NetUtil.asyncCopy(fileContentStreamB, foutB, tempFileSaved);
}

function tempFileSaved(aStatus)
{
  let success = Components.isSuccessCode(aStatus);

  ok(success, "a temporary file was saved successfully");

  if (!success)
  {
    finish();
    return;
  }

  if (gFirstFileSaved && success)
  {
    ok((gFirstFileSaved && success), "Both files loaded");
    // Import the file A into Scratchpad.
    gScratchpad.importFromFile(gFileA.QueryInterface(Ci.nsILocalFile), true,
                              fileAImported);
  }
  gFirstFileSaved = success;
}

function fileAImported(aStatus, aFileContent)
{
  ok(Components.isSuccessCode(aStatus),
     "the temporary file A was imported successfully with Scratchpad");

  is(aFileContent, gFileAContent, "received data is correct");

  is(gScratchpad.getText(), gFileAContent, "the editor content is correct");

  gScratchpad.editor.replaceText("new text",
    gScratchpad.editor.getPosition(gScratchpad.getText().length));

  is(gScratchpad.getText(), gFileAContent + "new text", "text updated correctly");
  gScratchpad.undo();
  is(gScratchpad.getText(), gFileAContent, "undo works");
  gScratchpad.redo();
  is(gScratchpad.getText(), gFileAContent + "new text", "redo works");

  // Import the file B into Scratchpad.
  gScratchpad.importFromFile(gFileB.QueryInterface(Ci.nsILocalFile), true,
                            fileBImported);
}

function fileBImported(aStatus, aFileContent)
{
  ok(Components.isSuccessCode(aStatus),
     "the temporary file B was imported successfully with Scratchpad");

  is(aFileContent, gFileBContent, "received data is correct");

  is(gScratchpad.getText(), gFileBContent, "the editor content is correct");

  ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load");

  gScratchpad.undo();
  is(gScratchpad.getText(), gFileBContent,
      "the editor content is still correct after undo");

  gScratchpad.editor.replaceText("new text",
    gScratchpad.editor.getPosition(gScratchpad.getText().length));
  is(gScratchpad.getText(), gFileBContent + "new text", "text updated correctly");

  gScratchpad.undo();
  is(gScratchpad.getText(), gFileBContent, "undo works");
  ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load (again)");

  gScratchpad.redo();
  is(gScratchpad.getText(), gFileBContent + "new text", "redo works");

  // Done!
  finish();
}

registerCleanupFunction(function () {
  if (gFileA && gFileA.exists())
  {
    gFileA.remove(false);
    gFileA = null;
  }
  if (gFileB && gFileB.exists())
  {
    gFileB.remove(false);
    gFileB = null;
  }
  gScratchpad = null;
});