-
Notifications
You must be signed in to change notification settings - Fork 0
/
head.js
192 lines (155 loc) · 5.57 KB
/
head.js
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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const {utils: Cu, classes: Cc, interfaces: Ci} = Components;
const {Promise: promise} =
Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js", {});
const {devtools} =
Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
const {require} = devtools;
const {AppProjects} = require("devtools/app-manager/app-projects");
const APP_MANAGER_URL = "about:app-manager";
const TEST_BASE =
"chrome://mochitests/content/browser/browser/devtools/app-manager/test/";
const HOSTED_APP_MANIFEST = TEST_BASE + "hosted_app.manifest";
const PACKAGED_APP_DIR_PATH = getTestFilePath(".");
gDevTools.testing = true;
SimpleTest.registerCleanupFunction(() => {
gDevTools.testing = false;
});
function addTab(url, targetWindow = window) {
info("Adding tab: " + url);
let deferred = promise.defer();
let targetBrowser = targetWindow.gBrowser;
targetWindow.focus();
let tab = targetBrowser.selectedTab = targetBrowser.addTab(url);
let linkedBrowser = tab.linkedBrowser;
linkedBrowser.addEventListener("load", function onLoad() {
linkedBrowser.removeEventListener("load", onLoad, true);
info("Tab added and finished loading: " + url);
deferred.resolve(tab);
}, true);
return deferred.promise;
}
function removeTab(tab, targetWindow = window) {
info("Removing tab.");
let deferred = promise.defer();
let targetBrowser = targetWindow.gBrowser;
let tabContainer = targetBrowser.tabContainer;
tabContainer.addEventListener("TabClose", function onClose(aEvent) {
tabContainer.removeEventListener("TabClose", onClose, false);
info("Tab removed and finished closing.");
deferred.resolve();
}, false);
targetBrowser.removeTab(tab);
return deferred.promise;
}
function openAppManager() {
return addTab(APP_MANAGER_URL);
}
function addSampleHostedApp() {
info("Adding sample hosted app");
let projectsWindow = getProjectsWindow();
let projectsDocument = projectsWindow.document;
let url = projectsDocument.querySelector("#url-input");
url.value = HOSTED_APP_MANIFEST;
return projectsWindow.UI.addHosted();
}
function removeSampleHostedApp() {
info("Removing sample hosted app");
return AppProjects.remove(HOSTED_APP_MANIFEST);
}
function addSamplePackagedApp() {
info("Adding sample packaged app");
let appDir = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
appDir.initWithPath(PACKAGED_APP_DIR_PATH);
return getProjectsWindow().UI.addPackaged(appDir);
}
function removeSamplePackagedApp() {
info("Removing sample packaged app");
return AppProjects.remove(PACKAGED_APP_DIR_PATH);
}
function getProjectsWindow() {
return content.document.querySelector(".projects-panel").contentWindow;
}
function getManifestWindow() {
return getProjectsWindow().document.querySelector(".variables-view")
.contentWindow;
}
function waitForProjectsPanel(deferred = promise.defer()) {
info("Wait for projects panel");
let projectsWindow = getProjectsWindow();
let projectsUI = projectsWindow.UI;
if (!projectsUI) {
info("projectsUI false");
projectsWindow.addEventListener("load", function onLoad() {
info("got load event");
projectsWindow.removeEventListener("load", onLoad);
waitForProjectsPanel(deferred);
});
return deferred.promise;
}
if (projectsUI.isReady) {
info("projectsUI ready");
deferred.resolve();
return deferred.promise;
}
info("projectsUI not ready");
projectsUI.once("ready", deferred.resolve);
return deferred.promise;
}
function selectProjectsPanel() {
return Task.spawn(function() {
let projectsButton = content.document.querySelector(".projects-button");
EventUtils.sendMouseEvent({ type: "click" }, projectsButton, content);
yield waitForProjectsPanel();
});
}
function waitForProjectSelection() {
info("Wait for project selection");
let deferred = promise.defer();
getProjectsWindow().UI.once("project-selected", deferred.resolve);
return deferred.promise;
}
function selectFirstProject() {
return Task.spawn(function() {
let projectsFrame = content.document.querySelector(".projects-panel");
let projectsWindow = projectsFrame.contentWindow;
let projectsDoc = projectsWindow.document;
let projectItem = projectsDoc.querySelector(".project-item");
EventUtils.sendMouseEvent({ type: "click" }, projectItem, projectsWindow);
yield waitForProjectSelection();
});
}
function showSampleProjectDetails() {
return Task.spawn(function() {
yield selectProjectsPanel();
yield selectFirstProject();
});
}
function waitForTick() {
let deferred = promise.defer();
executeSoon(deferred.resolve);
return deferred.promise;
}
function waitForTime(aDelay) {
let deferred = promise.defer();
setTimeout(deferred.resolve, aDelay);
return deferred.promise;
}
exports.addTab = addTab;
exports.removeTab = removeTab;
exports.openAppManager = openAppManager;
exports.addSampleHostedApp = addSampleHostedApp;
exports.removeSampleHostedApp = removeSampleHostedApp;
exports.addSamplePackagedApp = addSamplePackagedApp;
exports.removeSamplePackagedApp = removeSamplePackagedApp;
exports.getProjectsWindow = getProjectsWindow;
exports.getManifestWindow = getManifestWindow;
exports.waitForProjectsPanel = waitForProjectsPanel;
exports.selectProjectsPanel = selectProjectsPanel;
exports.waitForProjectSelection = waitForProjectSelection;
exports.selectFirstProject = selectFirstProject;
exports.showSampleProjectDetails = showSampleProjectDetails;
exports.waitForTick = waitForTick;
exports.waitForTime = waitForTime;