browser/devtools/shadereditor/test/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
michael@0 6
michael@0 7 let { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
michael@0 8
michael@0 9 let gEnableLogging = Services.prefs.getBoolPref("devtools.debugger.log");
michael@0 10 // To enable logging for try runs, just set the pref to true.
michael@0 11 Services.prefs.setBoolPref("devtools.debugger.log", false);
michael@0 12
michael@0 13 let { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
michael@0 14 let { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {});
michael@0 15 let { gDevTools } = Cu.import("resource:///modules/devtools/gDevTools.jsm", {});
michael@0 16 let { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
michael@0 17 let { DebuggerServer } = Cu.import("resource://gre/modules/devtools/dbg-server.jsm", {});
michael@0 18 let { DebuggerClient } = Cu.import("resource://gre/modules/devtools/dbg-client.jsm", {});
michael@0 19
michael@0 20 let { WebGLFront } = devtools.require("devtools/server/actors/webgl");
michael@0 21 let TiltGL = devtools.require("devtools/tilt/tilt-gl");
michael@0 22 let TargetFactory = devtools.TargetFactory;
michael@0 23 let Toolbox = devtools.Toolbox;
michael@0 24
michael@0 25 const EXAMPLE_URL = "http://example.com/browser/browser/devtools/shadereditor/test/";
michael@0 26 const SIMPLE_CANVAS_URL = EXAMPLE_URL + "doc_simple-canvas.html";
michael@0 27 const SHADER_ORDER_URL = EXAMPLE_URL + "doc_shader-order.html";
michael@0 28 const MULTIPLE_CONTEXTS_URL = EXAMPLE_URL + "doc_multiple-contexts.html";
michael@0 29 const OVERLAPPING_GEOMETRY_CANVAS_URL = EXAMPLE_URL + "doc_overlapping-geometry.html";
michael@0 30 const BLENDED_GEOMETRY_CANVAS_URL = EXAMPLE_URL + "doc_blended-geometry.html";
michael@0 31
michael@0 32 // All tests are asynchronous.
michael@0 33 waitForExplicitFinish();
michael@0 34
michael@0 35 let gToolEnabled = Services.prefs.getBoolPref("devtools.shadereditor.enabled");
michael@0 36
michael@0 37 registerCleanupFunction(() => {
michael@0 38 info("finish() was called, cleaning up...");
michael@0 39 Services.prefs.setBoolPref("devtools.debugger.log", gEnableLogging);
michael@0 40 Services.prefs.setBoolPref("devtools.shadereditor.enabled", gToolEnabled);
michael@0 41
michael@0 42 // These tests use a lot of memory due to GL contexts, so force a GC to help
michael@0 43 // fragmentation.
michael@0 44 info("Forcing GC after shadereditor test.");
michael@0 45 Cu.forceGC();
michael@0 46 });
michael@0 47
michael@0 48 function addTab(aUrl, aWindow) {
michael@0 49 info("Adding tab: " + aUrl);
michael@0 50
michael@0 51 let deferred = promise.defer();
michael@0 52 let targetWindow = aWindow || window;
michael@0 53 let targetBrowser = targetWindow.gBrowser;
michael@0 54
michael@0 55 targetWindow.focus();
michael@0 56 let tab = targetBrowser.selectedTab = targetBrowser.addTab(aUrl);
michael@0 57 let linkedBrowser = tab.linkedBrowser;
michael@0 58
michael@0 59 linkedBrowser.addEventListener("load", function onLoad() {
michael@0 60 linkedBrowser.removeEventListener("load", onLoad, true);
michael@0 61 info("Tab added and finished loading: " + aUrl);
michael@0 62 deferred.resolve(tab);
michael@0 63 }, true);
michael@0 64
michael@0 65 return deferred.promise;
michael@0 66 }
michael@0 67
michael@0 68 function removeTab(aTab, aWindow) {
michael@0 69 info("Removing tab.");
michael@0 70
michael@0 71 let deferred = promise.defer();
michael@0 72 let targetWindow = aWindow || window;
michael@0 73 let targetBrowser = targetWindow.gBrowser;
michael@0 74 let tabContainer = targetBrowser.tabContainer;
michael@0 75
michael@0 76 tabContainer.addEventListener("TabClose", function onClose(aEvent) {
michael@0 77 tabContainer.removeEventListener("TabClose", onClose, false);
michael@0 78 info("Tab removed and finished closing.");
michael@0 79 deferred.resolve();
michael@0 80 }, false);
michael@0 81
michael@0 82 targetBrowser.removeTab(aTab);
michael@0 83 return deferred.promise;
michael@0 84 }
michael@0 85
michael@0 86 function handleError(aError) {
michael@0 87 ok(false, "Got an error: " + aError.message + "\n" + aError.stack);
michael@0 88 finish();
michael@0 89 }
michael@0 90
michael@0 91 function ifWebGLSupported() {
michael@0 92 ok(false, "You need to define a 'ifWebGLSupported' function.");
michael@0 93 finish();
michael@0 94 }
michael@0 95
michael@0 96 function ifWebGLUnsupported() {
michael@0 97 todo(false, "Skipping test because WebGL isn't supported.");
michael@0 98 finish();
michael@0 99 }
michael@0 100
michael@0 101 function test() {
michael@0 102 let generator = isWebGLSupported() ? ifWebGLSupported : ifWebGLUnsupported;
michael@0 103 Task.spawn(generator).then(null, handleError);
michael@0 104 }
michael@0 105
michael@0 106 function createCanvas() {
michael@0 107 return document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
michael@0 108 }
michael@0 109
michael@0 110 function isWebGLSupported() {
michael@0 111 let supported =
michael@0 112 !TiltGL.isWebGLForceEnabled() &&
michael@0 113 TiltGL.isWebGLSupported() &&
michael@0 114 TiltGL.create3DContext(createCanvas());
michael@0 115
michael@0 116 info("Apparently, WebGL is" + (supported ? "" : " not") + " supported.");
michael@0 117 return supported;
michael@0 118 }
michael@0 119
michael@0 120 function once(aTarget, aEventName, aUseCapture = false) {
michael@0 121 info("Waiting for event: '" + aEventName + "' on " + aTarget + ".");
michael@0 122
michael@0 123 let deferred = promise.defer();
michael@0 124
michael@0 125 for (let [add, remove] of [
michael@0 126 ["on", "off"], // Use event emitter before DOM events for consistency
michael@0 127 ["addEventListener", "removeEventListener"],
michael@0 128 ["addListener", "removeListener"]
michael@0 129 ]) {
michael@0 130 if ((add in aTarget) && (remove in aTarget)) {
michael@0 131 aTarget[add](aEventName, function onEvent(...aArgs) {
michael@0 132 aTarget[remove](aEventName, onEvent, aUseCapture);
michael@0 133 deferred.resolve(...aArgs);
michael@0 134 }, aUseCapture);
michael@0 135 break;
michael@0 136 }
michael@0 137 }
michael@0 138
michael@0 139 return deferred.promise;
michael@0 140 }
michael@0 141
michael@0 142 // Hack around `once`, as that only resolves to a single (first) argument
michael@0 143 // and discards the rest. `onceSpread` is similar, except resolves to an
michael@0 144 // array of all of the arguments in the handler. These should be consolidated
michael@0 145 // into the same function, but many tests will need to be changed.
michael@0 146 function onceSpread(aTarget, aEvent) {
michael@0 147 let deferred = promise.defer();
michael@0 148 aTarget.once(aEvent, (...args) => deferred.resolve(args));
michael@0 149 return deferred.promise;
michael@0 150 }
michael@0 151
michael@0 152 function observe(aNotificationName, aOwnsWeak = false) {
michael@0 153 info("Waiting for observer notification: '" + aNotificationName + ".");
michael@0 154
michael@0 155 let deferred = promise.defer();
michael@0 156
michael@0 157 Services.obs.addObserver(function onNotification(...aArgs) {
michael@0 158 Services.obs.removeObserver(onNotification, aNotificationName);
michael@0 159 deferred.resolve.apply(deferred, aArgs);
michael@0 160 }, aNotificationName, aOwnsWeak);
michael@0 161
michael@0 162 return deferred.promise;
michael@0 163 }
michael@0 164
michael@0 165 function waitForFrame(aDebuggee) {
michael@0 166 let deferred = promise.defer();
michael@0 167 aDebuggee.requestAnimationFrame(deferred.resolve);
michael@0 168 return deferred.promise;
michael@0 169 }
michael@0 170
michael@0 171 function isApprox(aFirst, aSecond, aMargin = 1) {
michael@0 172 return Math.abs(aFirst - aSecond) <= aMargin;
michael@0 173 }
michael@0 174
michael@0 175 function isApproxColor(aFirst, aSecond, aMargin) {
michael@0 176 return isApprox(aFirst.r, aSecond.r, aMargin) &&
michael@0 177 isApprox(aFirst.g, aSecond.g, aMargin) &&
michael@0 178 isApprox(aFirst.b, aSecond.b, aMargin) &&
michael@0 179 isApprox(aFirst.a, aSecond.a, aMargin);
michael@0 180 }
michael@0 181
michael@0 182 function getPixels(aDebuggee, aSelector = "canvas") {
michael@0 183 let canvas = aDebuggee.document.querySelector(aSelector);
michael@0 184 let gl = canvas.getContext("webgl");
michael@0 185
michael@0 186 let { width, height } = canvas;
michael@0 187 let buffer = new aDebuggee.Uint8Array(width * height * 4);
michael@0 188 gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buffer);
michael@0 189
michael@0 190 info("Retrieved pixels: " + width + "x" + height);
michael@0 191 return [buffer, width, height];
michael@0 192 }
michael@0 193
michael@0 194 function getPixel(aDebuggee, aPosition, aSelector = "canvas") {
michael@0 195 let canvas = aDebuggee.document.querySelector(aSelector);
michael@0 196 let gl = canvas.getContext("webgl");
michael@0 197
michael@0 198 let { width, height } = canvas;
michael@0 199 let { x, y } = aPosition;
michael@0 200 let buffer = new aDebuggee.Uint8Array(4);
michael@0 201 gl.readPixels(x, height - y - 1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buffer);
michael@0 202
michael@0 203 let pixel = { r: buffer[0], g: buffer[1], b: buffer[2], a: buffer[3] };
michael@0 204
michael@0 205 info("Retrieved pixel: " + pixel.toSource() + " at " + aPosition.toSource());
michael@0 206 return pixel;
michael@0 207 }
michael@0 208
michael@0 209 function ensurePixelIs(aDebuggee, aPosition, aColor, aWaitFlag = false, aSelector = "canvas") {
michael@0 210 let pixel = getPixel(aDebuggee, aPosition, aSelector);
michael@0 211 if (isApproxColor(pixel, aColor)) {
michael@0 212 ok(true, "Expected pixel is shown at: " + aPosition.toSource());
michael@0 213 return promise.resolve(null);
michael@0 214 }
michael@0 215 if (aWaitFlag) {
michael@0 216 return Task.spawn(function() {
michael@0 217 yield waitForFrame(aDebuggee);
michael@0 218 yield ensurePixelIs(aDebuggee, aPosition, aColor, aWaitFlag, aSelector);
michael@0 219 });
michael@0 220 }
michael@0 221 ok(false, "Expected pixel was not already shown at: " + aPosition.toSource());
michael@0 222 return promise.reject(null);
michael@0 223 }
michael@0 224
michael@0 225 function navigateInHistory(aTarget, aDirection, aWaitForTargetEvent = "navigate") {
michael@0 226 executeSoon(() => content.history[aDirection]());
michael@0 227 return once(aTarget, aWaitForTargetEvent);
michael@0 228 }
michael@0 229
michael@0 230 function navigate(aTarget, aUrl, aWaitForTargetEvent = "navigate") {
michael@0 231 executeSoon(() => aTarget.activeTab.navigateTo(aUrl));
michael@0 232 return once(aTarget, aWaitForTargetEvent);
michael@0 233 }
michael@0 234
michael@0 235 function reload(aTarget, aWaitForTargetEvent = "navigate") {
michael@0 236 executeSoon(() => aTarget.activeTab.reload());
michael@0 237 return once(aTarget, aWaitForTargetEvent);
michael@0 238 }
michael@0 239
michael@0 240 function initBackend(aUrl) {
michael@0 241 info("Initializing a shader editor front.");
michael@0 242
michael@0 243 if (!DebuggerServer.initialized) {
michael@0 244 DebuggerServer.init(() => true);
michael@0 245 DebuggerServer.addBrowserActors();
michael@0 246 }
michael@0 247
michael@0 248 return Task.spawn(function*() {
michael@0 249 let tab = yield addTab(aUrl);
michael@0 250 let target = TargetFactory.forTab(tab);
michael@0 251 let debuggee = target.window.wrappedJSObject;
michael@0 252
michael@0 253 yield target.makeRemote();
michael@0 254
michael@0 255 let front = new WebGLFront(target.client, target.form);
michael@0 256 return [target, debuggee, front];
michael@0 257 });
michael@0 258 }
michael@0 259
michael@0 260 function initShaderEditor(aUrl) {
michael@0 261 info("Initializing a shader editor pane.");
michael@0 262
michael@0 263 return Task.spawn(function*() {
michael@0 264 let tab = yield addTab(aUrl);
michael@0 265 let target = TargetFactory.forTab(tab);
michael@0 266 let debuggee = target.window.wrappedJSObject;
michael@0 267
michael@0 268 yield target.makeRemote();
michael@0 269
michael@0 270 Services.prefs.setBoolPref("devtools.shadereditor.enabled", true);
michael@0 271 let toolbox = yield gDevTools.showToolbox(target, "shadereditor");
michael@0 272 let panel = toolbox.getCurrentPanel();
michael@0 273 return [target, debuggee, panel];
michael@0 274 });
michael@0 275 }
michael@0 276
michael@0 277 function teardown(aPanel) {
michael@0 278 info("Destroying the specified shader editor.");
michael@0 279
michael@0 280 return promise.all([
michael@0 281 once(aPanel, "destroyed"),
michael@0 282 removeTab(aPanel.target.tab)
michael@0 283 ]);
michael@0 284 }
michael@0 285
michael@0 286 // Due to `program-linked` events firing synchronously, we cannot
michael@0 287 // just yield/chain them together, as then we miss all actors after the
michael@0 288 // first event since they're fired consecutively. This allows us to capture
michael@0 289 // all actors and returns an array containing them.
michael@0 290 //
michael@0 291 // Takes a `front` object that is an event emitter, the number of
michael@0 292 // programs that should be listened to and waited on, and an optional
michael@0 293 // `onAdd` function that calls with the entire actors array on program link
michael@0 294 function getPrograms(front, count, onAdd) {
michael@0 295 let actors = [];
michael@0 296 let deferred = promise.defer();
michael@0 297 front.on("program-linked", function onLink (actor) {
michael@0 298 if (actors.length !== count) {
michael@0 299 actors.push(actor);
michael@0 300 if (typeof onAdd === 'function') onAdd(actors)
michael@0 301 }
michael@0 302 if (actors.length === count) {
michael@0 303 front.off("program-linked", onLink);
michael@0 304 deferred.resolve(actors);
michael@0 305 }
michael@0 306 });
michael@0 307 return deferred.promise;
michael@0 308 }

mercurial