summaryrefslogtreecommitdiffstats
path: root/devtools/client/scratchpad/test/browser_scratchpad_edit_ui_updates.js
blob: ade87eaace3c5403f996e670d5664124810a0981 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* 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 699130 */

"use strict";

var WebConsoleUtils = require("devtools/client/webconsole/utils").Utils;
var DEVTOOLS_CHROME_ENABLED = "devtools.chrome.enabled";

function test()
{
  waitForExplicitFinish();
  gBrowser.selectedTab = gBrowser.addTab();
  Services.prefs.setBoolPref(DEVTOOLS_CHROME_ENABLED, false);
  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    openScratchpad(runTests);
  }, true);

  content.location = "data:text/html,test Edit menu updates Scratchpad - bug 699130";
}

function runTests()
{
  let sp = gScratchpadWindow.Scratchpad;
  let doc = gScratchpadWindow.document;
  let winUtils = gScratchpadWindow.QueryInterface(Ci.nsIInterfaceRequestor).
                 getInterface(Ci.nsIDOMWindowUtils);
  let OS = Services.appinfo.OS;

  info("will test the Edit menu");

  let pass = 0;

  sp.setText("bug 699130: hello world! (edit menu)");

  let editMenu = doc.getElementById("sp-edit-menu");
  ok(editMenu, "the Edit menu");
  let menubar = editMenu.parentNode;
  ok(menubar, "menubar found");

  let editMenuIndex = -1;
  for (let i = 0; i < menubar.children.length; i++) {
    if (menubar.children[i] === editMenu) {
      editMenuIndex = i;
      break;
    }
  }
  isnot(editMenuIndex, -1, "Edit menu index is correct");

  let menuPopup = editMenu.menupopup;
  ok(menuPopup, "the Edit menupopup");
  let cutItem = doc.getElementById("menu_cut");
  ok(cutItem, "the Cut menuitem");
  let pasteItem = doc.getElementById("menu_paste");
  ok(pasteItem, "the Paste menuitem");

  let anchor = doc.documentElement;
  let isContextMenu = false;

  let oldVal = sp.editor.getText();

  let testSelfXss = function (oldVal) {
    // Self xss prevention tests (bug 994134)
    info("Self xss paste tests");
    is(WebConsoleUtils.usageCount, 0, "Test for usage count getter");
    let notificationbox = doc.getElementById("scratchpad-notificationbox");
    let notification = notificationbox.getNotificationWithValue("selfxss-notification");
    ok(notification, "Self-xss notification shown");
    is(oldVal, sp.editor.getText(), "Paste blocked by self-xss prevention");
    Services.prefs.setIntPref("devtools.selfxss.count", 10);
    notificationbox.removeAllNotifications(true);
    openMenu(10, 10, firstShow);
  };

  let openMenu = function (aX, aY, aCallback) {
    if (!editMenu || OS != "Darwin") {
      menuPopup.addEventListener("popupshown", function onPopupShown() {
        menuPopup.removeEventListener("popupshown", onPopupShown, false);
        executeSoon(aCallback);
      }, false);
    }

    executeSoon(function () {
      if (editMenu) {
        if (OS == "Darwin") {
          winUtils.forceUpdateNativeMenuAt(editMenuIndex);
          executeSoon(aCallback);
        } else {
          editMenu.open = true;
        }
      } else {
        menuPopup.openPopup(anchor, "overlap", aX, aY, isContextMenu, false);
      }
    });
  };

  let closeMenu = function (aCallback) {
    if (!editMenu || OS != "Darwin") {
      menuPopup.addEventListener("popuphidden", function onPopupHidden() {
        menuPopup.removeEventListener("popuphidden", onPopupHidden, false);
        executeSoon(aCallback);
      }, false);
    }

    executeSoon(function () {
      if (editMenu) {
        if (OS == "Darwin") {
          winUtils.forceUpdateNativeMenuAt(editMenuIndex);
          executeSoon(aCallback);
        } else {
          editMenu.open = false;
        }
      } else {
        menuPopup.hidePopup();
      }
    });
  };

  let firstShow = function () {
    ok(!cutItem.hasAttribute("disabled"), "cut menuitem is enabled");
    closeMenu(firstHide);
  };

  let firstHide = function () {
    sp.editor.setSelection({ line: 0, ch: 0 }, { line: 0, ch: 10 });
    openMenu(11, 11, showAfterSelect);
  };

  let showAfterSelect = function () {
    ok(!cutItem.hasAttribute("disabled"), "cut menuitem is enabled after select");
    closeMenu(hideAfterSelect);
  };

  let hideAfterSelect = function () {
    sp.editor.on("change", onCut);
    waitForFocus(function () {
      let selectedText = sp.editor.getSelection();
      ok(selectedText.length > 0, "non-empty selected text will be cut");

      EventUtils.synthesizeKey("x", {accelKey: true}, gScratchpadWindow);
    }, gScratchpadWindow);
  };

  let onCut = function () {
    sp.editor.off("change", onCut);
    openMenu(12, 12, showAfterCut);
  };

  let showAfterCut = function () {
    ok(!cutItem.hasAttribute("disabled"), "cut menuitem is enabled after cut");
    ok(!pasteItem.hasAttribute("disabled"), "paste menuitem is enabled after cut");
    closeMenu(hideAfterCut);
  };

  let hideAfterCut = function () {
    waitForFocus(function () {
      sp.editor.on("change", onPaste);
      EventUtils.synthesizeKey("v", {accelKey: true}, gScratchpadWindow);
    }, gScratchpadWindow);
  };

  let onPaste = function () {
    sp.editor.off("change", onPaste);
    openMenu(13, 13, showAfterPaste);
  };

  let showAfterPaste = function () {
    ok(!cutItem.hasAttribute("disabled"), "cut menuitem is enabled after paste");
    ok(!pasteItem.hasAttribute("disabled"), "paste menuitem is enabled after paste");
    closeMenu(hideAfterPaste);
  };

  let hideAfterPaste = function () {
    if (pass == 0) {
      pass++;
      testContextMenu();
    } else {
      Services.prefs.clearUserPref(DEVTOOLS_CHROME_ENABLED);
      finish();
    }
  };

  let testContextMenu = function () {
    info("will test the context menu");

    editMenu = null;
    isContextMenu = true;

    menuPopup = doc.getElementById("scratchpad-text-popup");
    ok(menuPopup, "the context menupopup");
    cutItem = doc.getElementById("cMenu_cut");
    ok(cutItem, "the Cut menuitem");
    pasteItem = doc.getElementById("cMenu_paste");
    ok(pasteItem, "the Paste menuitem");

    sp.setText("bug 699130: hello world! (context menu)");
    openMenu(10, 10, firstShow);
  };
  waitForFocus(function () {
    WebConsoleUtils.usageCount = 0;
    EventUtils.synthesizeKey("v", {accelKey: true}, gScratchpadWindow);
    testSelfXss(oldVal);
  }, gScratchpadWindow);
}