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 #1334 from zombie/945748-test-csWhen
Browse files Browse the repository at this point in the history
fix bug 945748 - PageMod.contentScriptWhen tests @r=ZER0
  • Loading branch information
ZER0 committed Feb 9, 2014
2 parents 68dc28c + 2d0749b commit d20dea0
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 1 deletion.
26 changes: 26 additions & 0 deletions test/pagemod-test-helpers.js
Expand Up @@ -62,3 +62,29 @@ exports.testPageMod = function testPageMod(assert, done, testURL, pageModOptions

return pageMods;
}

/**
* helper function that creates a PageMod and calls back the appropriate handler
* based on the value of document.readyState at the time contentScript is attached
*/
exports.handleReadyState = function(url, contentScriptWhen, callbacks) {
const { PageMod } = Loader(module).require('sdk/page-mod');

let pagemod = PageMod({
include: url,
attachTo: ['existing', 'top'],
contentScriptWhen: contentScriptWhen,
contentScript: "self.postMessage(document.readyState)",
onAttach: worker => {
let { tab } = worker;
worker.on('message', readyState => {
pagemod.destroy();
// generate event name from `readyState`, e.g. `"loading"` becomes `onLoading`.
let type = 'on' + readyState[0].toUpperCase() + readyState.substr(1);

if (type in callbacks)
callbacks[type](tab);
})
}
});
}
181 changes: 180 additions & 1 deletion test/test-page-mod.js
Expand Up @@ -4,7 +4,7 @@
"use strict";

const { PageMod } = require("sdk/page-mod");
const testPageMod = require("./pagemod-test-helpers").testPageMod;
const { testPageMod, handleReadyState } = require("./pagemod-test-helpers");
const { Loader } = require('sdk/test/loader');
const tabs = require("sdk/tabs");
const timer = require("sdk/timers");
Expand Down Expand Up @@ -474,6 +474,185 @@ exports.testExistingOnlyFrameMatchesInclude = function(assert, done) {
});
};

exports.testContentScriptWhenDefault = function(assert) {
let pagemod = PageMod({include: '*'});

assert.equal(pagemod.contentScriptWhen, 'end', "Default contentScriptWhen is 'end'");
pagemod.destroy();
}

// test timing for all 3 contentScriptWhen options (start, ready, end)
// for new pages, or tabs opened after PageMod is created
exports.testContentScriptWhenForNewTabs = function(assert, done) {
const url = "data:text/html;charset=utf-8,testContentScriptWhenForNewTabs";

let count = 0;

handleReadyState(url, 'start', {
onLoading: (tab) => {
assert.pass("PageMod is attached while document is loading");
if (++count === 3)
tab.close(done);
},
onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
onComplete: () => assert.fail("onComplete should not be called with 'start'."),
});

handleReadyState(url, 'ready', {
onInteractive: (tab) => {
assert.pass("PageMod is attached while document is interactive");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
});

handleReadyState(url, 'end', {
onComplete: (tab) => {
assert.pass("PageMod is attached when document is complete");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'end'."),
onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
});

tabs.open(url);
}

// test timing for all 3 contentScriptWhen options (start, ready, end)
// for PageMods created right as the tab is created (in tab.onOpen)
exports.testContentScriptWhenOnTabOpen = function(assert, done) {
const url = "data:text/html;charset=utf-8,testContentScriptWhenOnTabOpen";

tabs.open({
url: url,
onOpen: function(tab) {
let count = 0;

handleReadyState(url, 'start', {
onLoading: () => {
assert.pass("PageMod is attached while document is loading");
if (++count === 3)
tab.close(done);
},
onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
onComplete: () => assert.fail("onComplete should not be called with 'start'."),
});

handleReadyState(url, 'ready', {
onInteractive: () => {
assert.pass("PageMod is attached while document is interactive");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
});

handleReadyState(url, 'end', {
onComplete: () => {
assert.pass("PageMod is attached when document is complete");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'end'."),
onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
});

}
});
}

// test timing for all 3 contentScriptWhen options (start, ready, end)
// for PageMods created while the tab is interactive (in tab.onReady)
exports.testContentScriptWhenOnTabReady = function(assert, done) {
const url = "data:text/html;charset=utf-8,testContentScriptWhenOnTabReady";

tabs.open({
url: url,
onReady: function(tab) {
let count = 0;

handleReadyState(url, 'start', {
onInteractive: () => {
assert.pass("PageMod is attached while document is interactive");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'start'."),
onComplete: () => assert.fail("onComplete should not be called with 'start'."),
});

handleReadyState(url, 'ready', {
onInteractive: () => {
assert.pass("PageMod is attached while document is interactive");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
});

handleReadyState(url, 'end', {
onComplete: () => {
assert.pass("PageMod is attached when document is complete");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'end'."),
onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
});

}
});
}

// test timing for all 3 contentScriptWhen options (start, ready, end)
// for PageMods created after a tab has completed loading (in tab.onLoad)
exports.testContentScriptWhenOnTabLoad = function(assert, done) {
const url = "data:text/html;charset=utf-8,testContentScriptWhenOnTabLoad";

tabs.open({
url: url,
onLoad: function(tab) {
let count = 0;

handleReadyState(url, 'start', {
onComplete: () => {
assert.pass("PageMod is attached when document is complete");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'start'."),
onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
});

handleReadyState(url, 'ready', {
onComplete: () => {
assert.pass("PageMod is attached when document is complete");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
onInteractive: () => assert.fail("onInteractive should not be called with 'ready'."),
});

handleReadyState(url, 'end', {
onComplete: () => {
assert.pass("PageMod is attached when document is complete");
if (++count === 3)
tab.close(done);
},
onLoading: () => assert.fail("onLoading should not be called with 'end'."),
onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
});

}
});
}

exports.testTabWorkerOnMessage = function(assert, done) {
let { browserWindows } = require("sdk/windows");
let tabs = require("sdk/tabs");
Expand Down

0 comments on commit d20dea0

Please sign in to comment.