browser/devtools/framework/test/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/framework/test/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +let TargetFactory = gDevTools.TargetFactory;
     1.9 +
    1.10 +let tempScope = {};
    1.11 +Components.utils.import("resource://gre/modules/devtools/Console.jsm", tempScope);
    1.12 +let console = tempScope.console;
    1.13 +Components.utils.import("resource://gre/modules/Promise.jsm", tempScope);
    1.14 +let promise = tempScope.Promise;
    1.15 +
    1.16 +let {devtools} = Components.utils.import("resource://gre/modules/devtools/Loader.jsm", {});
    1.17 +let TargetFactory = devtools.TargetFactory;
    1.18 +
    1.19 +gDevTools.testing = true;
    1.20 +SimpleTest.registerCleanupFunction(() => {
    1.21 +  gDevTools.testing = false;
    1.22 +});
    1.23 +
    1.24 +/**
    1.25 + * Open a new tab at a URL and call a callback on load
    1.26 + */
    1.27 +function addTab(aURL, aCallback)
    1.28 +{
    1.29 +  waitForExplicitFinish();
    1.30 +
    1.31 +  gBrowser.selectedTab = gBrowser.addTab();
    1.32 +  if (aURL != null) {
    1.33 +    content.location = aURL;
    1.34 +  }
    1.35 +
    1.36 +  let deferred = promise.defer();
    1.37 +
    1.38 +  let tab = gBrowser.selectedTab;
    1.39 +  let target = TargetFactory.forTab(gBrowser.selectedTab);
    1.40 +  let browser = gBrowser.getBrowserForTab(tab);
    1.41 +
    1.42 +  function onTabLoad() {
    1.43 +    browser.removeEventListener("load", onTabLoad, true);
    1.44 +
    1.45 +    if (aCallback != null) {
    1.46 +      aCallback(browser, tab, browser.contentDocument);
    1.47 +    }
    1.48 +
    1.49 +    deferred.resolve({ browser: browser, tab: tab, target: target });
    1.50 +  }
    1.51 +
    1.52 +  browser.addEventListener("load", onTabLoad, true);
    1.53 +  return deferred.promise;
    1.54 +}
    1.55 +
    1.56 +registerCleanupFunction(function tearDown() {
    1.57 +  while (gBrowser.tabs.length > 1) {
    1.58 +    gBrowser.removeCurrentTab();
    1.59 +  }
    1.60 +});
    1.61 +
    1.62 +function synthesizeKeyFromKeyTag(aKeyId, document) {
    1.63 +  let key = document.getElementById(aKeyId);
    1.64 +  isnot(key, null, "Successfully retrieved the <key> node");
    1.65 +
    1.66 +  let modifiersAttr = key.getAttribute("modifiers");
    1.67 +
    1.68 +  let name = null;
    1.69 +
    1.70 +  if (key.getAttribute("keycode"))
    1.71 +    name = key.getAttribute("keycode");
    1.72 +  else if (key.getAttribute("key"))
    1.73 +    name = key.getAttribute("key");
    1.74 +
    1.75 +  isnot(name, null, "Successfully retrieved keycode/key");
    1.76 +
    1.77 +  let modifiers = {
    1.78 +    shiftKey: modifiersAttr.match("shift"),
    1.79 +    ctrlKey: modifiersAttr.match("ctrl"),
    1.80 +    altKey: modifiersAttr.match("alt"),
    1.81 +    metaKey: modifiersAttr.match("meta"),
    1.82 +    accelKey: modifiersAttr.match("accel")
    1.83 +  }
    1.84 +
    1.85 +  EventUtils.synthesizeKey(name, modifiers);
    1.86 +}

mercurial