browser/devtools/app-manager/test/head.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3 "use strict";
michael@0 4
michael@0 5 const {utils: Cu, classes: Cc, interfaces: Ci} = Components;
michael@0 6
michael@0 7 const {Promise: promise} =
michael@0 8 Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js", {});
michael@0 9 const {devtools} =
michael@0 10 Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
michael@0 11 const {require} = devtools;
michael@0 12
michael@0 13 const {AppProjects} = require("devtools/app-manager/app-projects");
michael@0 14
michael@0 15 const APP_MANAGER_URL = "about:app-manager";
michael@0 16 const TEST_BASE =
michael@0 17 "chrome://mochitests/content/browser/browser/devtools/app-manager/test/";
michael@0 18 const HOSTED_APP_MANIFEST = TEST_BASE + "hosted_app.manifest";
michael@0 19
michael@0 20 const PACKAGED_APP_DIR_PATH = getTestFilePath(".");
michael@0 21
michael@0 22 gDevTools.testing = true;
michael@0 23 SimpleTest.registerCleanupFunction(() => {
michael@0 24 gDevTools.testing = false;
michael@0 25 });
michael@0 26
michael@0 27 function addTab(url, targetWindow = window) {
michael@0 28 info("Adding tab: " + url);
michael@0 29
michael@0 30 let deferred = promise.defer();
michael@0 31 let targetBrowser = targetWindow.gBrowser;
michael@0 32
michael@0 33 targetWindow.focus();
michael@0 34 let tab = targetBrowser.selectedTab = targetBrowser.addTab(url);
michael@0 35 let linkedBrowser = tab.linkedBrowser;
michael@0 36
michael@0 37 linkedBrowser.addEventListener("load", function onLoad() {
michael@0 38 linkedBrowser.removeEventListener("load", onLoad, true);
michael@0 39 info("Tab added and finished loading: " + url);
michael@0 40 deferred.resolve(tab);
michael@0 41 }, true);
michael@0 42
michael@0 43 return deferred.promise;
michael@0 44 }
michael@0 45
michael@0 46 function removeTab(tab, targetWindow = window) {
michael@0 47 info("Removing tab.");
michael@0 48
michael@0 49 let deferred = promise.defer();
michael@0 50 let targetBrowser = targetWindow.gBrowser;
michael@0 51 let tabContainer = targetBrowser.tabContainer;
michael@0 52
michael@0 53 tabContainer.addEventListener("TabClose", function onClose(aEvent) {
michael@0 54 tabContainer.removeEventListener("TabClose", onClose, false);
michael@0 55 info("Tab removed and finished closing.");
michael@0 56 deferred.resolve();
michael@0 57 }, false);
michael@0 58
michael@0 59 targetBrowser.removeTab(tab);
michael@0 60
michael@0 61 return deferred.promise;
michael@0 62 }
michael@0 63
michael@0 64 function openAppManager() {
michael@0 65 return addTab(APP_MANAGER_URL);
michael@0 66 }
michael@0 67
michael@0 68 function addSampleHostedApp() {
michael@0 69 info("Adding sample hosted app");
michael@0 70 let projectsWindow = getProjectsWindow();
michael@0 71 let projectsDocument = projectsWindow.document;
michael@0 72 let url = projectsDocument.querySelector("#url-input");
michael@0 73 url.value = HOSTED_APP_MANIFEST;
michael@0 74 return projectsWindow.UI.addHosted();
michael@0 75 }
michael@0 76
michael@0 77 function removeSampleHostedApp() {
michael@0 78 info("Removing sample hosted app");
michael@0 79 return AppProjects.remove(HOSTED_APP_MANIFEST);
michael@0 80 }
michael@0 81
michael@0 82 function addSamplePackagedApp() {
michael@0 83 info("Adding sample packaged app");
michael@0 84 let appDir = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsIFile);
michael@0 85 appDir.initWithPath(PACKAGED_APP_DIR_PATH);
michael@0 86 return getProjectsWindow().UI.addPackaged(appDir);
michael@0 87 }
michael@0 88
michael@0 89 function removeSamplePackagedApp() {
michael@0 90 info("Removing sample packaged app");
michael@0 91 return AppProjects.remove(PACKAGED_APP_DIR_PATH);
michael@0 92 }
michael@0 93
michael@0 94 function getProjectsWindow() {
michael@0 95 return content.document.querySelector(".projects-panel").contentWindow;
michael@0 96 }
michael@0 97
michael@0 98 function getManifestWindow() {
michael@0 99 return getProjectsWindow().document.querySelector(".variables-view")
michael@0 100 .contentWindow;
michael@0 101 }
michael@0 102
michael@0 103 function waitForProjectsPanel(deferred = promise.defer()) {
michael@0 104 info("Wait for projects panel");
michael@0 105
michael@0 106 let projectsWindow = getProjectsWindow();
michael@0 107 let projectsUI = projectsWindow.UI;
michael@0 108 if (!projectsUI) {
michael@0 109 info("projectsUI false");
michael@0 110 projectsWindow.addEventListener("load", function onLoad() {
michael@0 111 info("got load event");
michael@0 112 projectsWindow.removeEventListener("load", onLoad);
michael@0 113 waitForProjectsPanel(deferred);
michael@0 114 });
michael@0 115 return deferred.promise;
michael@0 116 }
michael@0 117
michael@0 118 if (projectsUI.isReady) {
michael@0 119 info("projectsUI ready");
michael@0 120 deferred.resolve();
michael@0 121 return deferred.promise;
michael@0 122 }
michael@0 123
michael@0 124 info("projectsUI not ready");
michael@0 125 projectsUI.once("ready", deferred.resolve);
michael@0 126 return deferred.promise;
michael@0 127 }
michael@0 128
michael@0 129 function selectProjectsPanel() {
michael@0 130 return Task.spawn(function() {
michael@0 131 let projectsButton = content.document.querySelector(".projects-button");
michael@0 132 EventUtils.sendMouseEvent({ type: "click" }, projectsButton, content);
michael@0 133
michael@0 134 yield waitForProjectsPanel();
michael@0 135 });
michael@0 136 }
michael@0 137
michael@0 138 function waitForProjectSelection() {
michael@0 139 info("Wait for project selection");
michael@0 140
michael@0 141 let deferred = promise.defer();
michael@0 142 getProjectsWindow().UI.once("project-selected", deferred.resolve);
michael@0 143 return deferred.promise;
michael@0 144 }
michael@0 145
michael@0 146 function selectFirstProject() {
michael@0 147 return Task.spawn(function() {
michael@0 148 let projectsFrame = content.document.querySelector(".projects-panel");
michael@0 149 let projectsWindow = projectsFrame.contentWindow;
michael@0 150 let projectsDoc = projectsWindow.document;
michael@0 151 let projectItem = projectsDoc.querySelector(".project-item");
michael@0 152 EventUtils.sendMouseEvent({ type: "click" }, projectItem, projectsWindow);
michael@0 153
michael@0 154 yield waitForProjectSelection();
michael@0 155 });
michael@0 156 }
michael@0 157
michael@0 158 function showSampleProjectDetails() {
michael@0 159 return Task.spawn(function() {
michael@0 160 yield selectProjectsPanel();
michael@0 161 yield selectFirstProject();
michael@0 162 });
michael@0 163 }
michael@0 164
michael@0 165 function waitForTick() {
michael@0 166 let deferred = promise.defer();
michael@0 167 executeSoon(deferred.resolve);
michael@0 168 return deferred.promise;
michael@0 169 }
michael@0 170
michael@0 171 function waitForTime(aDelay) {
michael@0 172 let deferred = promise.defer();
michael@0 173 setTimeout(deferred.resolve, aDelay);
michael@0 174 return deferred.promise;
michael@0 175 }

mercurial