Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #816 from ochameau/hooked-console
Browse files Browse the repository at this point in the history
Bug 845549 - Expose an helper to ease testing console output in tests r=@erikvold
  • Loading branch information
ochameau committed Feb 28, 2013
2 parents 133c457 + 046db4d commit c1e40cd
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 154 deletions.
45 changes: 44 additions & 1 deletion lib/sdk/test/loader.js
Expand Up @@ -6,8 +6,9 @@

const { Loader, resolveURI, Require,
unload, override, descriptor } = require('../loader/cuddlefish');
const { PlainTextConsole } = require("sdk/console/plain-text");

exports.Loader = function(module, globals, packaging) {
function CustomLoader(module, globals, packaging) {
let options = packaging || require("@loader/options");
options = override(options, {
globals: override(require('../system/globals'), globals || {})
Expand All @@ -26,3 +27,45 @@ exports.Loader = function(module, globals, packaging) {
}
}));
};
exports.Loader = CustomLoader;

// Creates a custom loader instance whose console module is hooked in order
// to avoid printing messages to the console, and instead, expose them in the
// returned `messages` array attribute
exports.LoaderWithHookedConsole = function (module, callback) {
let messages = [];
function hook(msg) {
messages.push({type: this, msg: msg});
if (callback)
callback(this, msg);
}
return {
loader: CustomLoader(module, {
console: {
log: hook.bind("log"),
info: hook.bind("info"),
warn: hook.bind("warn"),
error: hook.bind("error"),
debug: hook.bind("debug"),
exception: hook.bind("exception")
}
}),
messages: messages
};
}

// Same than LoaderWithHookedConsole with lower level, instead we get what is
// actually printed to the command line console
exports.LoaderWithHookedConsole2 = function (module, callback) {
let messages = [];
return {
loader: CustomLoader(module, {
console: new PlainTextConsole(function (msg) {
messages.push(msg);
if (callback)
callback(msg);
})
}),
messages: messages
};
}
63 changes: 34 additions & 29 deletions test/tabs/test-fennec-tabs.js
Expand Up @@ -4,30 +4,20 @@
'use strict';

const { Cc, Ci } = require('chrome');
const { Loader } = require('sdk/test/loader');
const { Loader, LoaderWithHookedConsole } = require('sdk/test/loader');
const timer = require('sdk/timers');
const tabs = require('sdk/tabs');
const windows = require('sdk/windows');

const tabsLen = tabs.length;
const URL = 'data:text/html;charset=utf-8,<html><head><title>#title#</title></head></html>';
const ERR_MSG = 'Error: This method is not yet supported by Fennec';

function LoaderWithHookedConsole() {
let errors = [];
let loader = Loader(module, {
console: Object.create(console, {
error: { value: function(error) {
errors.push(error);
}}
})
});

return {
loader: loader,
errors: errors
}
}
// Fennec error message dispatched on all currently unimplement tab features,
// that match LoaderWithHookedConsole messages object pattern
const ERR_FENNEC_MSG = {
type: "error",
msg: "This method is not yet supported by Fennec"
};

// TEST: tab unloader
exports.testAutomaticDestroy = function(test) {
Expand Down Expand Up @@ -109,7 +99,7 @@ exports.testAutomaticDestroy = function(test) {
// TEST: tab properties
exports.testTabProperties = function(test) {
test.waitUntilDone();
let { loader, errors } = LoaderWithHookedConsole();
let { loader, messages } = LoaderWithHookedConsole();
let tabs = loader.require('sdk/tabs');

let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>";
Expand All @@ -121,7 +111,10 @@ exports.testTabProperties = function(test) {
test.assertEqual(tab.url, url, "URL of the new tab matches");
test.assert(tab.favicon, "favicon of the new tab is not empty");
// TODO: remove need for this test by implementing the favicon feature
test.assertEqual(errors.length, 1, "favicon logs an error for now");
// Poors man deepEqual with JSON.stringify...
test.assertEqual(JSON.stringify(messages),
JSON.stringify([ERR_FENNEC_MSG]),
"favicon logs an error for now");
test.assertEqual(tab.style, null, "style of the new tab matches");
test.assertEqual(tab.index, tabsLen, "index of the new tab matches");
test.assertNotEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches");
Expand Down Expand Up @@ -230,7 +223,7 @@ exports.testTabReload = function(test) {
exports.testTabMove = function(test) {
test.waitUntilDone();

let { loader, errors } = LoaderWithHookedConsole();
let { loader, messages } = LoaderWithHookedConsole();
let tabs = loader.require('sdk/tabs');

let url = "data:text/html;charset=utf-8,testTabMove";
Expand All @@ -247,8 +240,9 @@ exports.testTabMove = function(test) {
test.assert(tab.index > tab1.index, "2nd tab has valid index");
tab.index = 0;
test.assertEqual(tab.index, i, "tab index after move matches");
test.assertEqual(errors.length, 1, "setting tab.index logs error");

test.assertEqual(JSON.stringify(messages),
JSON.stringify([ERR_FENNEC_MSG]),
"setting tab.index logs error");
// end test
tab1.close(function() tab.close(function() {
loader.unload();
Expand All @@ -264,7 +258,7 @@ exports.testTabMove = function(test) {
exports.testTabsOpen_alt = function(test) {
test.waitUntilDone();

let { loader, errors } = LoaderWithHookedConsole();
let { loader, messages } = LoaderWithHookedConsole();
let tabs = loader.require('sdk/tabs');
let url = "data:text/html;charset=utf-8,default";

Expand All @@ -274,7 +268,7 @@ exports.testTabsOpen_alt = function(test) {
test.assertEqual(tab.url, url, "URL of the new tab matches");
test.assertEqual(tabs.activeTab, tab, "URL of active tab in the current window matches");
test.assertEqual(tab.isPinned, false, "The new tab is not pinned");
test.assertEqual(errors.length, 1, "isPinned logs error");
test.assertEqual(messages.length, 1, "isPinned logs error");

// end test
tab.close(function() {
Expand All @@ -289,7 +283,7 @@ exports.testTabsOpen_alt = function(test) {
exports.testOpenPinned_alt = function(test) {
test.waitUntilDone();

let { loader, errors } = LoaderWithHookedConsole();
let { loader, messages } = LoaderWithHookedConsole();
let tabs = loader.require('sdk/tabs');
let url = "about:blank";

Expand All @@ -298,7 +292,11 @@ exports.testOpenPinned_alt = function(test) {
isPinned: true,
onOpen: function(tab) {
test.assertEqual(tab.isPinned, false, "The new tab is pinned");
test.assertEqual(errors.length, 2, "isPinned logs error");
// We get two error message: one for tabs.open's isPinned argument
// and another one for tab.isPinned
test.assertEqual(JSON.stringify(messages),
JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]),
"isPinned logs error");

// end test
tab.close(function() {
Expand All @@ -313,7 +311,7 @@ exports.testOpenPinned_alt = function(test) {
exports.testPinUnpin_alt = function(test) {
test.waitUntilDone();

let { loader, errors } = LoaderWithHookedConsole();
let { loader, messages } = LoaderWithHookedConsole();
let tabs = loader.require('sdk/tabs');
let url = "data:text/html;charset=utf-8,default";

Expand All @@ -322,11 +320,18 @@ exports.testPinUnpin_alt = function(test) {
onOpen: function(tab) {
tab.pin();
test.assertEqual(tab.isPinned, false, "The tab was pinned correctly");
test.assertEqual(errors.length, 2, "tab.pin() logs error");
test.assertEqual(JSON.stringify(messages),
JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]),
"tab.pin() logs error");

// Clear console messages for the following test
messages.length = 0;

tab.unpin();
test.assertEqual(tab.isPinned, false, "The tab was unpinned correctly");
test.assertEqual(errors.length, 4, "tab.unpin() logs error");
test.assertEqual(JSON.stringify(messages),
JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]),
"tab.unpin() logs error");

// end test
tab.close(function() {
Expand Down
35 changes: 6 additions & 29 deletions test/test-content-worker.js
Expand Up @@ -6,7 +6,7 @@

const { Cc, Ci } = require("chrome");
const { setTimeout } = require("sdk/timers");
const { Loader, Require, override } = require("sdk/test/loader");
const { LoaderWithHookedConsole } = require("sdk/test/loader");
const { Worker } = require("sdk/content/worker");

const DEFAULT_CONTENT_URL = "data:text/html;charset=utf-8,foo";
Expand Down Expand Up @@ -348,24 +348,12 @@ exports["test:nothing is leaked to content script"] = WorkerTest(
exports["test:ensure console.xxx works in cs"] = WorkerTest(
DEFAULT_CONTENT_URL,
function(assert, browser, done) {

// Create a new module loader in order to be able to create a `console`
// module mockup:
let loader = Loader(module, {
console: {
log: hook.bind("log"),
info: hook.bind("info"),
warn: hook.bind("warn"),
error: hook.bind("error"),
debug: hook.bind("debug"),
exception: hook.bind("exception")
}
});
let { loader } = LoaderWithHookedConsole(module, onMessage);

// Intercept all console method calls
let calls = [];
function hook(msg) {
assert.equal(this, msg,
function onMessage(type, msg) {
assert.equal(type, msg,
"console.xxx(\"xxx\"), i.e. message is equal to the " +
"console method name we are calling");
calls.push(msg);
Expand Down Expand Up @@ -657,22 +645,11 @@ exports["test:check worker API with page history"] = WorkerTest(
exports["test:global postMessage"] = WorkerTest(
DEFAULT_CONTENT_URL,
function(assert, browser, done) {
// Create a new module loader in order to be able to create a `console`
// module mockup:
let loader = Loader(module, {
console: {
log: hook.bind(null, "log"),
info: hook.bind(null, "info"),
warn: hook.bind(null, "warn"),
error: hook.bind(null, "error"),
debug: hook.bind(null, "debug"),
exception: hook.bind(null, "exception")
}
});
let { loader } = LoaderWithHookedConsole(module, onMessage);

// Intercept all console method calls
let seenMessages = 0;
function hook(type, message) {
function onMessage(type, message) {
seenMessages++;
assert.equal(type, "error", "Should be an error");
assert.equal(message, "DEPRECATED: The global `postMessage()` function in " +
Expand Down
48 changes: 18 additions & 30 deletions test/test-deprecate.js
Expand Up @@ -3,37 +3,22 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const deprecate = require("sdk/util/deprecate");
var { Loader } = require("sdk/test/loader");

function LoaderWithHookedConsole() {
let errors = [];
let loader = Loader(module, {
console: Object.create(console, {
error: { value: function(error) {
errors.push(error);
}}
})
});

return {
loader: loader,
deprecate: loader.require("sdk/util/deprecate"),
errors: errors
}
}
const { LoaderWithHookedConsole } = require("sdk/test/loader");

exports["test Deprecate Usage"] = function testDeprecateUsage(assert) {
let { loader, deprecate, errors } = LoaderWithHookedConsole();
let { loader, messages } = LoaderWithHookedConsole(module);
let deprecate = loader.require("sdk/util/deprecate");

function functionIsDeprecated() {
deprecate.deprecateUsage("foo");
}

functionIsDeprecated();

assert.equal(errors.length, 1, "only one error is dispatched");
assert.equal(messages.length, 1, "only one error is dispatched");
assert.equal(messages[0].type, "error", "the console message is an error");

let msg = errors[0];
let msg = messages[0].msg;

assert.ok(msg.indexOf("foo") !== -1,
"message contains the given message");
Expand All @@ -46,7 +31,8 @@ exports["test Deprecate Usage"] = function testDeprecateUsage(assert) {
}

exports["test Deprecate Function"] = function testDeprecateFunction(assert) {
let { loader, deprecate, errors } = LoaderWithHookedConsole();
let { loader, messages } = LoaderWithHookedConsole(module);
let deprecate = loader.require("sdk/util/deprecate");

let self = {};
let arg1 = "foo";
Expand All @@ -63,10 +49,10 @@ exports["test Deprecate Function"] = function testDeprecateFunction(assert) {

deprecateFunction.call(self, arg1, arg2);

assert.equal(errors.length, 1,
"only one error is dispatched");
assert.equal(messages.length, 1, "only one error is dispatched");
assert.equal(messages[0].type, "error", "the console message is an error");

let msg = errors[0];
let msg = messages[0].msg;
assert.ok(msg.indexOf("bar") !== -1, "message contains the given message");
assert.ok(msg.indexOf("testDeprecateFunction") !== -1,
"message contains name of the caller function");
Expand All @@ -77,23 +63,25 @@ exports["test Deprecate Function"] = function testDeprecateFunction(assert) {
}

exports.testDeprecateEvent = function(assert, done) {
let { loader, deprecate, errors } = LoaderWithHookedConsole();
let { loader, messages } = LoaderWithHookedConsole(module);
let deprecate = loader.require("sdk/util/deprecate");

let { on, emit } = loader.require('sdk/event/core');
let testObj = {};
testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']);

testObj.on('fire', function() {
testObj.on('water', function() {
assert.equal(errors.length, 1, "only one error is dispatched");
assert.equal(messages.length, 1, "only one error is dispatched");
loader.unload();
done();
})
assert.equal(errors.length, 1, "only one error is dispatched");
assert.equal(messages.length, 1, "only one error is dispatched");
emit(testObj, 'water');
});
assert.equal(errors.length, 1, "only one error is dispatched");
let msg = errors[0];
assert.equal(messages.length, 1, "only one error is dispatched");
assert.equal(messages[0].type, "error", "the console message is an error");
let msg = messages[0].msg;
assert.ok(msg.indexOf("BAD") !== -1, "message contains the given message");
assert.ok(msg.indexOf("deprecateEvent") !== -1,
"message contains name of the caller function");
Expand Down

0 comments on commit c1e40cd

Please sign in to comment.