browser/devtools/tilt/test/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/tilt/test/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,210 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +"use strict";
     1.7 +
     1.8 +let {devtools} = Components.utils.import("resource://gre/modules/devtools/Loader.jsm", {});
     1.9 +let TiltManager = devtools.require("devtools/tilt/tilt").TiltManager;
    1.10 +let TiltGL = devtools.require("devtools/tilt/tilt-gl");
    1.11 +let {EPSILON, TiltMath, vec3, mat3, mat4, quat4} = devtools.require("devtools/tilt/tilt-math");
    1.12 +let TiltUtils = devtools.require("devtools/tilt/tilt-utils");
    1.13 +let {TiltVisualizer} = devtools.require("devtools/tilt/tilt-visualizer");
    1.14 +
    1.15 +let tempScope = {};
    1.16 +Components.utils.import("resource://gre/modules/devtools/LayoutHelpers.jsm", tempScope);
    1.17 +let LayoutHelpers = tempScope.LayoutHelpers;
    1.18 +
    1.19 +
    1.20 +const DEFAULT_HTML = "data:text/html," +
    1.21 +  "<DOCTYPE html>" +
    1.22 +  "<html>" +
    1.23 +    "<head>" +
    1.24 +      "<meta charset='utf-8'/>" +
    1.25 +      "<title>Three Laws</title>" +
    1.26 +    "</head>" +
    1.27 +    "<body>" +
    1.28 +      "<div id='first-law'>" +
    1.29 +        "A robot may not injure a human being or, through inaction, allow a " +
    1.30 +        "human being to come to harm." +
    1.31 +      "</div>" +
    1.32 +      "<div>" +
    1.33 +        "A robot must obey the orders given to it by human beings, except " +
    1.34 +        "where such orders would conflict with the First Law." +
    1.35 +      "</div>" +
    1.36 +      "<div>" +
    1.37 +        "A robot must protect its own existence as long as such protection " +
    1.38 +        "does not conflict with the First or Second Laws." +
    1.39 +      "</div>" +
    1.40 +      "<div id='far-far-away' style='position: absolute; top: 250%;'>" +
    1.41 +        "I like bacon." +
    1.42 +      "</div>" +
    1.43 +    "<body>" +
    1.44 +  "</html>";
    1.45 +
    1.46 +let Tilt = TiltManager.getTiltForBrowser(window);
    1.47 +
    1.48 +const STARTUP = Tilt.NOTIFICATIONS.STARTUP;
    1.49 +const INITIALIZING = Tilt.NOTIFICATIONS.INITIALIZING;
    1.50 +const INITIALIZED = Tilt.NOTIFICATIONS.INITIALIZED;
    1.51 +const DESTROYING = Tilt.NOTIFICATIONS.DESTROYING;
    1.52 +const BEFORE_DESTROYED = Tilt.NOTIFICATIONS.BEFORE_DESTROYED;
    1.53 +const DESTROYED = Tilt.NOTIFICATIONS.DESTROYED;
    1.54 +const SHOWN = Tilt.NOTIFICATIONS.SHOWN;
    1.55 +const HIDDEN = Tilt.NOTIFICATIONS.HIDDEN;
    1.56 +const HIGHLIGHTING = Tilt.NOTIFICATIONS.HIGHLIGHTING;
    1.57 +const UNHIGHLIGHTING = Tilt.NOTIFICATIONS.UNHIGHLIGHTING;
    1.58 +const NODE_REMOVED = Tilt.NOTIFICATIONS.NODE_REMOVED;
    1.59 +
    1.60 +const TILT_ENABLED = Services.prefs.getBoolPref("devtools.tilt.enabled");
    1.61 +
    1.62 +gDevTools.testing = true;
    1.63 +SimpleTest.registerCleanupFunction(() => {
    1.64 +  gDevTools.testing = false;
    1.65 +});
    1.66 +
    1.67 +function isTiltEnabled() {
    1.68 +  info("Apparently, Tilt is" + (TILT_ENABLED ? "" : " not") + " enabled.");
    1.69 +  return TILT_ENABLED;
    1.70 +}
    1.71 +
    1.72 +function isWebGLSupported() {
    1.73 +  let supported = !TiltGL.isWebGLForceEnabled() &&
    1.74 +                   TiltGL.isWebGLSupported() &&
    1.75 +                   TiltGL.create3DContext(createCanvas());
    1.76 +
    1.77 +  info("Apparently, WebGL is" + (supported ? "" : " not") + " supported.");
    1.78 +  return supported;
    1.79 +}
    1.80 +
    1.81 +function isApprox(num1, num2, delta) {
    1.82 +  if (Math.abs(num1 - num2) > (delta || EPSILON)) {
    1.83 +    info("isApprox expected " + num1 + ", got " + num2 + " instead.");
    1.84 +    return false;
    1.85 +  }
    1.86 +  return true;
    1.87 +}
    1.88 +
    1.89 +function isApproxVec(vec1, vec2, delta) {
    1.90 +  vec1 = Array.prototype.slice.call(vec1);
    1.91 +  vec2 = Array.prototype.slice.call(vec2);
    1.92 +
    1.93 +  if (vec1.length !== vec2.length) {
    1.94 +    return false;
    1.95 +  }
    1.96 +  for (let i = 0, len = vec1.length; i < len; i++) {
    1.97 +    if (!isApprox(vec1[i], vec2[i], delta)) {
    1.98 +      info("isApproxVec expected [" + vec1 + "], got [" + vec2 + "] instead.");
    1.99 +      return false;
   1.100 +    }
   1.101 +  }
   1.102 +  return true;
   1.103 +}
   1.104 +
   1.105 +function isEqualVec(vec1, vec2) {
   1.106 +  vec1 = Array.prototype.slice.call(vec1);
   1.107 +  vec2 = Array.prototype.slice.call(vec2);
   1.108 +
   1.109 +  if (vec1.length !== vec2.length) {
   1.110 +    return false;
   1.111 +  }
   1.112 +  for (let i = 0, len = vec1.length; i < len; i++) {
   1.113 +    if (vec1[i] !== vec2[i]) {
   1.114 +      info("isEqualVec expected [" + vec1 + "], got [" + vec2 + "] instead.");
   1.115 +      return false;
   1.116 +    }
   1.117 +  }
   1.118 +  return true;
   1.119 +}
   1.120 +
   1.121 +function createCanvas() {
   1.122 +  return document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
   1.123 +}
   1.124 +
   1.125 +
   1.126 +function createTab(callback, location) {
   1.127 +  info("Creating a tab, with callback " + typeof callback +
   1.128 +                      ", and location " + location + ".");
   1.129 +
   1.130 +  let tab = gBrowser.selectedTab = gBrowser.addTab();
   1.131 +
   1.132 +  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
   1.133 +    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
   1.134 +    callback(tab);
   1.135 +  }, true);
   1.136 +
   1.137 +  gBrowser.selectedBrowser.contentWindow.location = location || DEFAULT_HTML;
   1.138 +  return tab;
   1.139 +}
   1.140 +
   1.141 +
   1.142 +function createTilt(callbacks, close, suddenDeath) {
   1.143 +  info("Creating Tilt, with callbacks {" + Object.keys(callbacks) + "}" +
   1.144 +                   ", autoclose param " + close +
   1.145 +          ", and sudden death handler " + typeof suddenDeath + ".");
   1.146 +
   1.147 +  handleFailure(suddenDeath);
   1.148 +
   1.149 +  Services.prefs.setBoolPref("webgl.verbose", true);
   1.150 +  TiltUtils.Output.suppressAlerts = true;
   1.151 +
   1.152 +  info("Attempting to start Tilt.");
   1.153 +  Services.obs.addObserver(onTiltOpen, INITIALIZING, false);
   1.154 +  Tilt.toggle();
   1.155 +
   1.156 +  function onTiltOpen() {
   1.157 +    info("Tilt was opened.");
   1.158 +    Services.obs.removeObserver(onTiltOpen, INITIALIZING);
   1.159 +
   1.160 +    executeSoon(function() {
   1.161 +      if ("function" === typeof callbacks.onTiltOpen) {
   1.162 +        info("Calling 'onTiltOpen'.");
   1.163 +        callbacks.onTiltOpen(Tilt.visualizers[Tilt.currentWindowId]);
   1.164 +      }
   1.165 +      if (close) {
   1.166 +        executeSoon(function() {
   1.167 +          info("Attempting to close Tilt.");
   1.168 +          Services.obs.addObserver(onTiltClose, DESTROYED, false);
   1.169 +          Tilt.destroy(Tilt.currentWindowId);
   1.170 +        });
   1.171 +      }
   1.172 +    });
   1.173 +  }
   1.174 +
   1.175 +  function onTiltClose() {
   1.176 +    info("Tilt was closed.");
   1.177 +    Services.obs.removeObserver(onTiltClose, DESTROYED);
   1.178 +
   1.179 +    executeSoon(function() {
   1.180 +      if ("function" === typeof callbacks.onTiltClose) {
   1.181 +        info("Calling 'onTiltClose'.");
   1.182 +        callbacks.onTiltClose();
   1.183 +      }
   1.184 +      if ("function" === typeof callbacks.onEnd) {
   1.185 +        info("Calling 'onEnd'.");
   1.186 +        callbacks.onEnd();
   1.187 +      }
   1.188 +    });
   1.189 +  }
   1.190 +
   1.191 +  function handleFailure(suddenDeath) {
   1.192 +    Tilt.failureCallback = function() {
   1.193 +      info("Tilt FAIL.");
   1.194 +      Services.obs.removeObserver(onTiltOpen, INITIALIZING);
   1.195 +
   1.196 +      info("Now relying on sudden death handler " + typeof suddenDeath + ".");
   1.197 +      suddenDeath && suddenDeath();
   1.198 +    }
   1.199 +  }
   1.200 +}
   1.201 +
   1.202 +function getPickablePoint(presenter) {
   1.203 +  let vertices = presenter._meshStacks[0].vertices.components;
   1.204 +
   1.205 +  let topLeft = vec3.create([vertices[0], vertices[1], vertices[2]]);
   1.206 +  let bottomRight = vec3.create([vertices[6], vertices[7], vertices[8]]);
   1.207 +  let center = vec3.lerp(topLeft, bottomRight, 0.5, []);
   1.208 +
   1.209 +  let renderer = presenter._renderer;
   1.210 +  let viewport = [0, 0, renderer.width, renderer.height];
   1.211 +
   1.212 +  return vec3.project(center, viewport, renderer.mvMatrix, renderer.projMatrix);
   1.213 +}

mercurial