diff options
Diffstat (limited to 'devtools/shared/tests/mochitest')
3 files changed, 318 insertions, 0 deletions
diff --git a/devtools/shared/tests/mochitest/chrome.ini b/devtools/shared/tests/mochitest/chrome.ini new file mode 100644 index 000000000..85ece7c48 --- /dev/null +++ b/devtools/shared/tests/mochitest/chrome.ini @@ -0,0 +1,7 @@ +[DEFAULT] +tags = devtools +skip-if = os == 'android' + +[test_eventemitter_basic.html] +[test_devtools_extensions.html] +skip-if = os == 'linux' && debug # Bug 1205739 diff --git a/devtools/shared/tests/mochitest/test_devtools_extensions.html b/devtools/shared/tests/mochitest/test_devtools_extensions.html new file mode 100644 index 000000000..04cc82b22 --- /dev/null +++ b/devtools/shared/tests/mochitest/test_devtools_extensions.html @@ -0,0 +1,117 @@ +<!DOCTYPE html> +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> + +<html> + + <head> + <meta charset="utf8"> + <title></title> + + <script type="application/javascript" + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" + href="chrome://mochikit/content/tests/SimpleTest/test.css"> + + <script type="application/javascript;version=1.8"> + const { classes: Cc, interfaces: Ci, utils: Cu } = Components; + + let { require } = Cu.import("resource://devtools/shared/Loader.jsm", {}); + const contentGlobals = require("devtools/server/content-globals"); + const Services = require("Services"); + const tabs = require('sdk/tabs'); + const { getMostRecentBrowserWindow, getInnerId } = require('sdk/window/utils'); + const { PageMod } = require('sdk/page-mod'); + + var _tests = []; + function addTest(test) { + _tests.push(test); + } + + function runNextTest() { + if (_tests.length == 0) { + SimpleTest.finish() + return; + } + _tests.shift()(); + } + + window.onload = function() { + SimpleTest.waitForExplicitFinish(); + runNextTest(); + } + + addTest(function () { + let TEST_URL = 'data:text/html;charset=utf-8,test'; + + let mod = PageMod({ + include: TEST_URL, + contentScriptWhen: 'ready', + contentScript: 'null;' + }); + + tabs.open({ + url: TEST_URL, + onLoad: function(tab) { + let id = getInnerId(getMostRecentBrowserWindow().gBrowser.selectedBrowser.contentWindow); + + // getting + is(contentGlobals.getContentGlobals({ + 'inner-window-id': id + }).length, 1, 'found a global for inner-id = ' + id); + + Services.obs.addObserver(function observer(subject, topic, data) { + if (id == subject.QueryInterface(Components.interfaces.nsISupportsPRUint64).data) { + Services.obs.removeObserver(observer, 'inner-window-destroyed'); + setTimeout(function() { + // closing the tab window should have removed the global + is(contentGlobals.getContentGlobals({ + 'inner-window-id': id + }).length, 0, 'did not find a global for inner-id = ' + id); + + mod.destroy(); + runNextTest(); + }) + } + }, 'inner-window-destroyed', false); + + tab.close(); + } + }); + }) + + addTest(function testAddRemoveGlobal() { + let global = {}; + let globalDetails = { + global: global, + 'inner-window-id': 5 + }; + + // adding + contentGlobals.addContentGlobal(globalDetails); + + // getting + is(contentGlobals.getContentGlobals({ + 'inner-window-id': 5 + }).length, 1, 'found a global for inner-id = 5'); + is(contentGlobals.getContentGlobals({ + 'inner-window-id': 4 + }).length, 0, 'did not find a global for inner-id = 4'); + + // remove + contentGlobals.removeContentGlobal(globalDetails); + + // getting again + is(contentGlobals.getContentGlobals({ + 'inner-window-id': 5 + }).length, 0, 'did not find a global for inner-id = 5'); + + runNextTest(); + }); + + </script> + </head> + <body></body> +</html> diff --git a/devtools/shared/tests/mochitest/test_eventemitter_basic.html b/devtools/shared/tests/mochitest/test_eventemitter_basic.html new file mode 100644 index 000000000..9826eebca --- /dev/null +++ b/devtools/shared/tests/mochitest/test_eventemitter_basic.html @@ -0,0 +1,194 @@ +<!DOCTYPE html> +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> + +<html> + + <head> + <meta charset="utf8"> + <title></title> + + <script type="application/javascript" + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" + href="chrome://mochikit/content/tests/SimpleTest/test.css"> + </head> + + <body> + + <script type="application/javascript;version=1.8"> + "use strict"; + + const { utils: Cu } = Components; + const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {}); + const promise = require("promise"); + const EventEmitter = require("devtools/shared/event-emitter"); + const { Task } = require("devtools/shared/task"); + + SimpleTest.waitForExplicitFinish(); + + testEmitter(); + testEmitter({}); + + Task.spawn(testPromise) + .then(null, ok.bind(null, false)) + .then(SimpleTest.finish); + + function testEmitter(aObject) { + let emitter; + + if (aObject) { + emitter = aObject; + EventEmitter.decorate(emitter); + } else { + emitter = new EventEmitter(); + } + + ok(emitter, "We have an event emitter"); + + let beenHere1 = false; + let beenHere2 = false; + + emitter.on("next", next); + emitter.emit("next", "abc", "def"); + + function next(eventName, str1, str2) { + is(eventName, "next", "Got event"); + is(str1, "abc", "Argument 1 is correct"); + is(str2, "def", "Argument 2 is correct"); + + ok(!beenHere1, "first time in next callback"); + beenHere1 = true; + + emitter.off("next", next); + + emitter.emit("next"); + + emitter.once("onlyonce", onlyOnce); + + emitter.emit("onlyonce"); + emitter.emit("onlyonce"); + } + + function onlyOnce() { + ok(!beenHere2, "\"once\" listener has been called once"); + beenHere2 = true; + emitter.emit("onlyonce"); + + testThrowingExceptionInListener(); + } + + function testThrowingExceptionInListener() { + function throwListener() { + emitter.off("throw-exception"); + throw { + toString: () => "foo", + stack: "bar", + }; + } + + emitter.on("throw-exception", throwListener); + emitter.emit("throw-exception"); + + killItWhileEmitting(); + } + + function killItWhileEmitting() { + function c1() { + ok(true, "c1 called"); + } + function c2() { + ok(true, "c2 called"); + emitter.off("tick", c3); + } + function c3() { + ok(false, "c3 should not be called"); + } + function c4() { + ok(true, "c4 called"); + } + + emitter.on("tick", c1); + emitter.on("tick", c2); + emitter.on("tick", c3); + emitter.on("tick", c4); + + emitter.emit("tick"); + + offAfterOnce(); + } + + function offAfterOnce() { + let enteredC1 = false; + + function c1() { + enteredC1 = true; + } + + emitter.once("oao", c1); + emitter.off("oao", c1); + + emitter.emit("oao"); + + ok(!enteredC1, "c1 should not be called"); + } + } + + function testPromise() { + let emitter = new EventEmitter(); + let p = emitter.once("thing"); + + // Check that the promise is only resolved once event though we + // emit("thing") more than once + let firstCallbackCalled = false; + let check1 = p.then(arg => { + is(firstCallbackCalled, false, "first callback called only once"); + firstCallbackCalled = true; + is(arg, "happened", "correct arg in promise"); + return "rval from c1"; + }); + + emitter.emit("thing", "happened", "ignored"); + + // Check that the promise is resolved asynchronously + let secondCallbackCalled = false; + let check2 = p.then(arg => { + ok(true, "second callback called"); + is(arg, "happened", "correct arg in promise"); + secondCallbackCalled = true; + is(arg, "happened", "correct arg in promise (a second time)"); + return "rval from c2"; + }); + + // Shouldn't call any of the above listeners + emitter.emit("thing", "trashinate"); + + // Check that we can still separate events with different names + // and that it works with no parameters + let pfoo = emitter.once("foo"); + let pbar = emitter.once("bar"); + + let check3 = pfoo.then(arg => { + ok(arg === undefined, "no arg for foo event"); + return "rval from c3"; + }); + + pbar.then(() => { + ok(false, "pbar should not be called"); + }); + + emitter.emit("foo"); + + is(secondCallbackCalled, false, "second callback not called yet"); + + return promise.all([ check1, check2, check3 ]).then(args => { + is(args[0], "rval from c1", "callback 1 done good"); + is(args[1], "rval from c2", "callback 2 done good"); + is(args[2], "rval from c3", "callback 3 done good"); + }); + } + </script> + </body> +</html> |