From 4fe3b5000eef4ab31cd95f558827533fb81f1948 Mon Sep 17 00:00:00 2001 From: Bill McCloskey Date: Thu, 27 Aug 2015 16:29:08 -0700 Subject: [PATCH] Bug 1199800 - [webext] Allow extensions to be used from chrome tests (r=gabor) --- browser/components/extensions/moz.build | 2 ++ .../extensions/test/browser/browser.ini | 1 + .../test/browser/browser_extensions_simple.js | 22 +++++++++++++ testing/mochitest/browser-test.js | 8 +++++ .../tests/SimpleTest/ChromePowers.js | 32 +++++++++++++++---- .../specialpowers/content/specialpowersAPI.js | 2 +- 6 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 browser/components/extensions/test/browser/browser.ini create mode 100644 browser/components/extensions/test/browser/browser_extensions_simple.js diff --git a/browser/components/extensions/moz.build b/browser/components/extensions/moz.build index 3bbe6729759c..df2aee900d1b 100644 --- a/browser/components/extensions/moz.build +++ b/browser/components/extensions/moz.build @@ -5,3 +5,5 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. JAR_MANIFESTS += ['jar.mn'] + +BROWSER_CHROME_MANIFESTS += ['test/browser/browser.ini'] diff --git a/browser/components/extensions/test/browser/browser.ini b/browser/components/extensions/test/browser/browser.ini new file mode 100644 index 000000000000..e5a36a7d0783 --- /dev/null +++ b/browser/components/extensions/test/browser/browser.ini @@ -0,0 +1 @@ +[browser_extensions_simple.js] diff --git a/browser/components/extensions/test/browser/browser_extensions_simple.js b/browser/components/extensions/test/browser/browser_extensions_simple.js new file mode 100644 index 000000000000..86df641b7336 --- /dev/null +++ b/browser/components/extensions/test/browser/browser_extensions_simple.js @@ -0,0 +1,22 @@ +add_task(function* test_simple() { + let extension = ExtensionTestUtils.loadExtension("simple"); + info("load complete"); + yield extension.startup(); + info("startup complete"); + yield extension.unload(); + info("extension unloaded successfully"); +}); + +add_task(function* test_background() { + let extension = ExtensionTestUtils.loadExtension("background"); + info("load complete"); + yield extension.startup(); + let x = yield extension.awaitMessage("running"); + is(x, 1, "got correct value from extension"); + info("startup complete"); + extension.sendMessage(10, 20); + yield extension.awaitFinish(); + info("test complete"); + yield extension.unload(); + info("extension unloaded successfully"); +}); diff --git a/testing/mochitest/browser-test.js b/testing/mochitest/browser-test.js index 375a9e78c4c9..dca81f48334b 100644 --- a/testing/mochitest/browser-test.js +++ b/testing/mochitest/browser-test.js @@ -159,6 +159,11 @@ function Tester(aTests, aDumper, aCallback) { this._scriptLoader.loadSubScript("chrome://mochikit/content/chrome-harness.js", simpleTestScope); this.SimpleTest = simpleTestScope.SimpleTest; + var extensionUtilsScope = {}; + extensionUtilsScope.SimpleTest = this.SimpleTest; + this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js", extensionUtilsScope); + this.ExtensionTestUtils = extensionUtilsScope.ExtensionTestUtils; + this.SimpleTest.harnessParameters = gConfig; this.MemoryStats = simpleTestScope.MemoryStats; @@ -211,6 +216,7 @@ Tester.prototype = { SimpleTest: {}, Task: null, ContentTask: null, + ExtensionTestUtils: null, Assert: null, repeat: 0, @@ -686,6 +692,7 @@ Tester.prototype = { this.currentTest.scope.ContentTask = this.ContentTask; this.currentTest.scope.BrowserTestUtils = this.BrowserTestUtils; this.currentTest.scope.TestUtils = this.TestUtils; + this.currentTest.scope.ExtensionTestUtils = this.ExtensionTestUtils; // Pass a custom report function for mochitest style reporting. this.currentTest.scope.Assert = new this.Assert(function(err, message, stack) { let res; @@ -1082,6 +1089,7 @@ testScope.prototype = { ContentTask: null, BrowserTestUtils: null, TestUtils: null, + ExtensionTestUtils: null, Assert: null, /** diff --git a/testing/mochitest/tests/SimpleTest/ChromePowers.js b/testing/mochitest/tests/SimpleTest/ChromePowers.js index 4c03443de2fa..97de578157c0 100644 --- a/testing/mochitest/tests/SimpleTest/ChromePowers.js +++ b/testing/mochitest/tests/SimpleTest/ChromePowers.js @@ -15,6 +15,8 @@ function ChromePowers(window) { } this.spObserver = new SpecialPowersObserverAPI(); + this.spObserver._sendReply = this._sendReply.bind(this); + this.listeners = new Map(); } ChromePowers.prototype = new SpecialPowersAPI(); @@ -25,14 +27,32 @@ ChromePowers.prototype.sanityCheck = function() { return "foo"; }; // This gets filled in in the constructor. ChromePowers.prototype.DOMWindowUtils = undefined; -ChromePowers.prototype._sendSyncMessage = function(type, msg) { - var aMessage = {'name':type, 'json': msg}; - return [this._receiveMessage(aMessage)]; +ChromePowers.prototype._sendReply = function(aOrigMsg, aType, aMsg) { + var msg = {'name':aType, 'json': aMsg, 'data': aMsg}; + if (!this.listeners.has(aType)) { + throw new Error(`No listener for ${aType}`); + } + this.listeners.get(aType)(msg); }; -ChromePowers.prototype._sendAsyncMessage = function(type, msg) { - var aMessage = {'name':type, 'json': msg}; - this._receiveMessage(aMessage); +ChromePowers.prototype._sendSyncMessage = function(aType, aMsg) { + var msg = {'name':aType, 'json': aMsg, 'data': aMsg}; + return [this._receiveMessage(msg)]; +}; + +ChromePowers.prototype._sendAsyncMessage = function(aType, aMsg) { + var msg = {'name':aType, 'json': aMsg, 'data': aMsg}; + this._receiveMessage(msg); +}; + +ChromePowers.prototype._addMessageListener = function(aType, aCallback) { + if (this.listeners.has(aType)) { + throw new Error(`unable to handle multiple listeners for ${aType}`); + } + this.listeners.set(aType, aCallback); +}; +ChromePowers.prototype._removeMessageListener = function(aType, aCallback) { + this.listeners.delete(aType); }; ChromePowers.prototype.registerProcessCrashObservers = function() { diff --git a/testing/specialpowers/content/specialpowersAPI.js b/testing/specialpowers/content/specialpowersAPI.js index 27fd26e26bd7..0618b8d9b0cc 100644 --- a/testing/specialpowers/content/specialpowersAPI.js +++ b/testing/specialpowers/content/specialpowersAPI.js @@ -2055,7 +2055,7 @@ SpecialPowersAPI.prototype = { }); let unloadPromise = new Promise(resolve => { resolveUnload = resolve; }); - handler = handler.wrappedJSObject; + handler = Cu.waiveXrays(handler); let sp = this; let extension = {