Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | let TargetFactory = gDevTools.TargetFactory; |
michael@0 | 6 | |
michael@0 | 7 | let tempScope = {}; |
michael@0 | 8 | Components.utils.import("resource://gre/modules/devtools/Console.jsm", tempScope); |
michael@0 | 9 | let console = tempScope.console; |
michael@0 | 10 | Components.utils.import("resource://gre/modules/Promise.jsm", tempScope); |
michael@0 | 11 | let promise = tempScope.Promise; |
michael@0 | 12 | |
michael@0 | 13 | let {devtools} = Components.utils.import("resource://gre/modules/devtools/Loader.jsm", {}); |
michael@0 | 14 | let TargetFactory = devtools.TargetFactory; |
michael@0 | 15 | |
michael@0 | 16 | gDevTools.testing = true; |
michael@0 | 17 | SimpleTest.registerCleanupFunction(() => { |
michael@0 | 18 | gDevTools.testing = false; |
michael@0 | 19 | }); |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * Open a new tab at a URL and call a callback on load |
michael@0 | 23 | */ |
michael@0 | 24 | function addTab(aURL, aCallback) |
michael@0 | 25 | { |
michael@0 | 26 | waitForExplicitFinish(); |
michael@0 | 27 | |
michael@0 | 28 | gBrowser.selectedTab = gBrowser.addTab(); |
michael@0 | 29 | if (aURL != null) { |
michael@0 | 30 | content.location = aURL; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | let deferred = promise.defer(); |
michael@0 | 34 | |
michael@0 | 35 | let tab = gBrowser.selectedTab; |
michael@0 | 36 | let target = TargetFactory.forTab(gBrowser.selectedTab); |
michael@0 | 37 | let browser = gBrowser.getBrowserForTab(tab); |
michael@0 | 38 | |
michael@0 | 39 | function onTabLoad() { |
michael@0 | 40 | browser.removeEventListener("load", onTabLoad, true); |
michael@0 | 41 | |
michael@0 | 42 | if (aCallback != null) { |
michael@0 | 43 | aCallback(browser, tab, browser.contentDocument); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | deferred.resolve({ browser: browser, tab: tab, target: target }); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | browser.addEventListener("load", onTabLoad, true); |
michael@0 | 50 | return deferred.promise; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | registerCleanupFunction(function tearDown() { |
michael@0 | 54 | while (gBrowser.tabs.length > 1) { |
michael@0 | 55 | gBrowser.removeCurrentTab(); |
michael@0 | 56 | } |
michael@0 | 57 | }); |
michael@0 | 58 | |
michael@0 | 59 | function synthesizeKeyFromKeyTag(aKeyId, document) { |
michael@0 | 60 | let key = document.getElementById(aKeyId); |
michael@0 | 61 | isnot(key, null, "Successfully retrieved the <key> node"); |
michael@0 | 62 | |
michael@0 | 63 | let modifiersAttr = key.getAttribute("modifiers"); |
michael@0 | 64 | |
michael@0 | 65 | let name = null; |
michael@0 | 66 | |
michael@0 | 67 | if (key.getAttribute("keycode")) |
michael@0 | 68 | name = key.getAttribute("keycode"); |
michael@0 | 69 | else if (key.getAttribute("key")) |
michael@0 | 70 | name = key.getAttribute("key"); |
michael@0 | 71 | |
michael@0 | 72 | isnot(name, null, "Successfully retrieved keycode/key"); |
michael@0 | 73 | |
michael@0 | 74 | let modifiers = { |
michael@0 | 75 | shiftKey: modifiersAttr.match("shift"), |
michael@0 | 76 | ctrlKey: modifiersAttr.match("ctrl"), |
michael@0 | 77 | altKey: modifiersAttr.match("alt"), |
michael@0 | 78 | metaKey: modifiersAttr.match("meta"), |
michael@0 | 79 | accelKey: modifiersAttr.match("accel") |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | EventUtils.synthesizeKey(name, modifiers); |
michael@0 | 83 | } |