summaryrefslogtreecommitdiffstats
path: root/devtools/client/projecteditor/test/browser_projecteditor_menubar_02.js
blob: d0d41f743d8e87a34b6062cca5053e3656a789ad (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
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

loadHelperScript("helper_edits.js");

// Test menu bar enabled / disabled state.

add_task(function* () {
  let projecteditor = yield addProjectEditorTabForTempDirectory();
  let menubar = projecteditor.menubar;

  // Update menu items for a clean slate, so previous tests cannot
  // affect paste, and possibly other side effects
  projecteditor._updateMenuItems();

  // let projecteditor = yield addProjectEditorTabForTempDirectory();
  ok(projecteditor, "ProjectEditor has loaded");

  let fileMenu = menubar.querySelector("#file-menu");
  let editMenu = menubar.querySelector("#edit-menu");
  ok(fileMenu, "The menu has loaded in the projecteditor document");
  ok(editMenu, "The menu has loaded in the projecteditor document");

  let cmdNew = fileMenu.querySelector("[command=cmd-new]");
  let cmdSave = fileMenu.querySelector("[command=cmd-save]");
  let cmdSaveas = fileMenu.querySelector("[command=cmd-saveas]");

  let cmdUndo = editMenu.querySelector("[command=cmd_undo]");
  let cmdRedo = editMenu.querySelector("[command=cmd_redo]");
  let cmdCut = editMenu.querySelector("[command=cmd_cut]");
  let cmdCopy = editMenu.querySelector("[command=cmd_copy]");
  let cmdPaste = editMenu.querySelector("[command=cmd_paste]");

  info("Checking initial state of menus");
  yield openAndCloseMenu(fileMenu);
  yield openAndCloseMenu(editMenu);

  is(cmdNew.getAttribute("disabled"), "", "File menu item is enabled");
  is(cmdSave.getAttribute("disabled"), "true", "File menu item is disabled");
  is(cmdSaveas.getAttribute("disabled"), "true", "File menu item is disabled");

  is(cmdUndo.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdRedo.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdCut.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdCopy.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdPaste.getAttribute("disabled"), "true", "Edit menu item is disabled");

  projecteditor.menuEnabled = false;

  info("Checking with menuEnabled = false");
  yield openAndCloseMenu(fileMenu);
  yield openAndCloseMenu(editMenu);

  is(cmdNew.getAttribute("disabled"), "true", "File menu item is disabled");
  is(cmdSave.getAttribute("disabled"), "true", "File menu item is disabled");
  is(cmdSaveas.getAttribute("disabled"), "true", "File menu item is disabled");

  is(cmdUndo.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdRedo.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdCut.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdCopy.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdPaste.getAttribute("disabled"), "true", "Edit menu item is disabled");

  info("Checking with menuEnabled=true");
  projecteditor.menuEnabled = true;

  yield openAndCloseMenu(fileMenu);
  yield openAndCloseMenu(editMenu);

  is(cmdNew.getAttribute("disabled"), "", "File menu item is enabled");
  is(cmdSave.getAttribute("disabled"), "true", "File menu item is disabled");
  is(cmdSaveas.getAttribute("disabled"), "true", "File menu item is disabled");

  is(cmdUndo.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdRedo.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdCut.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdCopy.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdPaste.getAttribute("disabled"), "true", "Edit menu item is disabled");

  info("Checking with resource selected");
  let resource = projecteditor.project.allResources()[2];
  yield selectFile(projecteditor, resource);
  let editor = projecteditor.currentEditor;

  let onChange = promise.defer();

  projecteditor.on("onEditorChange", () => {
    info("onEditorChange has been detected");
    onChange.resolve();
  });
  editor.editor.focus();
  EventUtils.synthesizeKey("f", { }, projecteditor.window);

  yield onChange;
  yield openAndCloseMenu(fileMenu);
  yield openAndCloseMenu(editMenu);

  is(cmdNew.getAttribute("disabled"), "", "File menu item is enabled");
  is(cmdSave.getAttribute("disabled"), "", "File menu item is enabled");
  is(cmdSaveas.getAttribute("disabled"), "", "File menu item is enabled");

  // Use editor.canUndo() to see if this is failing - the menu disabled property
  // should be in sync with this because of isCommandEnabled in editor.js.
  info('cmdUndo.getAttribute("disabled") is: "' + cmdUndo.getAttribute("disabled") + '"');
  ok(editor.editor.canUndo(), "Edit menu item is enabled");

  is(cmdRedo.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdCut.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdCopy.getAttribute("disabled"), "true", "Edit menu item is disabled");
  is(cmdPaste.getAttribute("disabled"), "", "Edit menu item is enabled");
});

function* openAndCloseMenu(menu) {
  let shown = onPopupShow(menu);
  EventUtils.synthesizeMouseAtCenter(menu, {}, menu.ownerDocument.defaultView);
  yield shown;
  let hidden = onPopupHidden(menu);
  EventUtils.synthesizeMouseAtCenter(menu, {}, menu.ownerDocument.defaultView);
  yield hidden;
}