summaryrefslogtreecommitdiff
path: root/browser/devtools/app-manager/test/browser_manifest_editor.js
blob: 676eff6f108e10e79a8c4054bdc65614b053f7c1 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

const {Services} = Cu.import("resource://gre/modules/Services.jsm");

const MANIFEST_EDITOR_ENABLED = "devtools.appmanager.manifestEditor.enabled";

let gManifestWindow, gManifestEditor;

function test() {
  waitForExplicitFinish();

  Task.spawn(function() {
    Services.prefs.setBoolPref(MANIFEST_EDITOR_ENABLED, true);
    let tab = yield openAppManager();
    yield selectProjectsPanel();
    yield addSamplePackagedApp();
    yield showSampleProjectDetails();

    gManifestWindow = getManifestWindow();
    gManifestEditor = getProjectsWindow().UI.manifestEditor;
    yield changeManifestValue("name", "the best app");
    yield changeManifestValueBad("name", "the worst app");
    yield addNewManifestProperty("developer", "foo", "bar");

    // add duplicate property in the same parent doesn't create duplicates
    yield addNewManifestProperty("developer", "foo", "bar2");

    // add propery with same key in other parent is allowed
    yield addNewManifestProperty("tester", "foo", "new");

    yield addNewManifestPropertyBad("developer", "blob", "bob");
    yield removeManifestProperty("developer", "foo");
    gManifestWindow = null;
    gManifestEditor = null;

    yield removeSamplePackagedApp();
    yield removeTab(tab);
    Services.prefs.setBoolPref(MANIFEST_EDITOR_ENABLED, false);
    finish();
  });
}

// Wait until the animation from commitHierarchy has completed
function waitForUpdate() {
  return waitForTime(gManifestEditor.editor.lazyEmptyDelay + 1);
}

function changeManifestValue(key, value) {
  return Task.spawn(function() {
    let propElem = gManifestWindow.document
                   .querySelector("[id ^= '" + key + "']");
    is(propElem.querySelector(".name").value, key,
       "Key doesn't match expected value");

    let valueElem = propElem.querySelector(".value");
    EventUtils.sendMouseEvent({ type: "mousedown" }, valueElem, gManifestWindow);

    let valueInput = propElem.querySelector(".element-value-input");
    valueInput.value = '"' + value + '"';
    EventUtils.sendKey("RETURN", gManifestWindow);

    yield waitForUpdate();
    // Elements have all been replaced, re-select them
    propElem = gManifestWindow.document.querySelector("[id ^= '" + key + "']");
    valueElem = propElem.querySelector(".value");
    is(valueElem.value, '"' + value + '"',
       "Value doesn't match expected value");

    is(gManifestEditor.manifest[key], value,
       "Manifest doesn't contain expected value");
  });
}

function changeManifestValueBad(key, value) {
  return Task.spawn(function() {
    let propElem = gManifestWindow.document
                   .querySelector("[id ^= '" + key + "']");
    is(propElem.querySelector(".name").value, key,
       "Key doesn't match expected value");

    let valueElem = propElem.querySelector(".value");
    EventUtils.sendMouseEvent({ type: "mousedown" }, valueElem, gManifestWindow);

    let valueInput = propElem.querySelector(".element-value-input");
    // Leaving out quotes will result in an error, so no change should be made.
    valueInput.value = value;
    EventUtils.sendKey("RETURN", gManifestWindow);

    yield waitForUpdate();
    // Elements have all been replaced, re-select them
    propElem = gManifestWindow.document.querySelector("[id ^= '" + key + "']");
    valueElem = propElem.querySelector(".value");
    isnot(valueElem.value, '"' + value + '"',
       "Value was changed, but it should not have been");

    isnot(gManifestEditor.manifest[key], value,
       "Manifest was changed, but it should not have been");
  });
}

function addNewManifestProperty(parent, key, value) {
  info("Adding new property - parent: " + parent + "; key: " + key + "; value: " + value + "\n\n");
  return Task.spawn(function() {
    let parentElem = gManifestWindow.document
                     .querySelector("[id ^= '" + parent + "']");
    ok(parentElem, "Found parent element: " + parentElem.id);

    let addPropertyElem = parentElem.querySelector(".variables-view-add-property");
    ok(addPropertyElem, "Found add-property button");

    EventUtils.sendMouseEvent({ type: "mousedown" }, addPropertyElem, gManifestWindow);

    let nameInput = parentElem.querySelector(".element-name-input");
    nameInput.value = key;
    EventUtils.sendKey("TAB", gManifestWindow);

    let valueInput = parentElem.querySelector(".element-value-input");
    valueInput.value = '"' + value + '"';
    EventUtils.sendKey("RETURN", gManifestWindow);

    yield waitForUpdate();

    parentElem = gManifestWindow.document.querySelector("[id ^= '" + parent + "']");
    let elems = parentElem.querySelectorAll("[id ^= '" + key + "']");
    is(elems.length, 1, "No duplicate property is added");

    let newElem = elems[0];
    let nameElem = newElem.querySelector(".name");
    is(nameElem.value, key, "Key doesn't match expected Key");

    ok(key in gManifestEditor.manifest[parent],
       "Manifest doesn't contain expected key");

    let valueElem = newElem.querySelector(".value");
    is(valueElem.value, '"' + value + '"',
       "Value doesn't match expected value");

    is(gManifestEditor.manifest[parent][key], value,
       "Manifest doesn't contain expected value");
  });
}

function addNewManifestPropertyBad(parent, key, value) {
  return Task.spawn(function() {
    let parentElem = gManifestWindow.document
                     .querySelector("[id ^= '" + parent + "']");
    ok(parentElem,
      "Found parent element");
    let addPropertyElem = parentElem
                          .querySelector(".variables-view-add-property");
    ok(addPropertyElem,
      "Found add-property button");

    EventUtils.sendMouseEvent({ type: "mousedown" }, addPropertyElem, gManifestWindow);

    let nameInput = parentElem.querySelector(".element-name-input");
    nameInput.value = key;
    EventUtils.sendKey("TAB", gManifestWindow);

    let valueInput = parentElem.querySelector(".element-value-input");
    // Leaving out quotes will result in an error, so no change should be made.
    valueInput.value = value;
    EventUtils.sendKey("RETURN", gManifestWindow);

    yield waitForUpdate();

    let newElem = gManifestWindow.document.querySelector("[id ^= '" + key + "']");
    ok(!newElem, "Key was added, but it should not have been");
    ok(!(key in gManifestEditor.manifest[parent]),
       "Manifest contains key, but it should not");
  });
}

function removeManifestProperty(parent, key) {
  info("*** Remove property test ***");

  return Task.spawn(function() {
    let parentElem = gManifestWindow.document
                     .querySelector("[id ^= '" + parent + "']");
    ok(parentElem, "Found parent element");

    let keyExists = key in gManifestEditor.manifest[parent];
    ok(keyExists,
       "The manifest contains the key under the expected parent");

    let newElem = gManifestWindow.document.querySelector("[id ^= '" + key + "']");
    let removePropertyButton = newElem.querySelector(".variables-view-delete");
    ok(removePropertyButton, "The remove property button was found");
    removePropertyButton.click();

    yield waitForUpdate();

    ok(!(key in gManifestEditor.manifest[parent]), "Property was successfully removed");
  });
}