diff options
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/content/unittest/run.html | 18 | ||||
-rw-r--r-- | chrome/content/unittest/testcases.js | 51 |
2 files changed, 61 insertions, 8 deletions
diff --git a/chrome/content/unittest/run.html b/chrome/content/unittest/run.html index 5658137..1557610 100644 --- a/chrome/content/unittest/run.html +++ b/chrome/content/unittest/run.html @@ -27,7 +27,7 @@ row.appendChild(cell);
table.appendChild(row);
cell.innerHTML = name;
- document.getElementsByTagName('body')[0].appendChild(table);
+ document.getElementById('tests').appendChild(table);
for (var i = 0; i < testcase.tests.length; i++) {
var testdata = testcase.tests[i];
row = document.createElement('tr');
@@ -46,17 +46,21 @@ }
function setup() {
- var tables = document.getElementsByTagName('table');
- for (var i = 0; i < tables.length; i++) {
- tables[i].parentNode.removeChild(tables[i]);
- }
+ //quick and dirty cleanup
+ document.getElementById('tests').innerHTML = '';
subscriptLoader.loadSubScript('chrome://redirector/content/code/redirect.js');
subscriptLoader.loadSubScript('chrome://redirector/content/unittest/testcases.js');
redirector.reload();
+ var sorted = [];
for (var name in tests) {
- setupTest(name, tests[name]);
+ sorted.push(name);
}
+
+ sorted.sort();
+ for each(var name in sorted) {
+ setupTest(name, tests[name]);
+ }
}
function runTests() {
@@ -89,5 +93,7 @@ <h1>Redirector Unit Tests</h1>
<button onclick="runTests();">Run tests</button>
<button onclick="setup();">Reload tests</button>
+ <div id="tests">
+ </div>
</body>
</html>
\ No newline at end of file diff --git a/chrome/content/unittest/testcases.js b/chrome/content/unittest/testcases.js index ac9539f..ece1a67 100644 --- a/chrome/content/unittest/testcases.js +++ b/chrome/content/unittest/testcases.js @@ -1,4 +1,6 @@ //// $Id$
+var nsIContentPolicy = Components.interfaces.nsIContentPolicy;
+
var tests = {
"Wildcard matches" : {
run : function(data,log) {
@@ -39,7 +41,7 @@ var tests = { },
"Regex matches" : {
- run : function(data,log) {
+ run : function(data) {
var pattern = data[0],
url = data[1],
expected = data[2];
@@ -74,6 +76,51 @@ var tests = { ['(.*)://(.*)oo(.*)bar(.*)', 'http://foo.is/bar/baz', 'http,f,.is/,/baz'],
['(.*)://(.*?)(.*)oo(.*)bar(.*)', 'http://foo.is/bar/baz', 'http,,f,.is/,/baz'],
]
- }
+ },
+ "nsIContentPolicy implementation" : {
+ run : function(data) {
+ var runTest = function() {
+ var args = {
+ contentType : nsIContentPolicy.TYPE_DOCUMENT,
+ contentLocation : "http://foo.is",
+ requestOrigin : null,
+ aContext : { loadURI : function(){}},
+ mimeTypeGuess : null,
+ extra : null
+ };
+ for (var key in data[1]) {
+ args[key] = data[1][key];
+ }
+
+ var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
+ args.contentLocation = ioService.newURI(args.contentLocation, null, null);
+ var result = redirector.shouldLoad(args.contentType, args.contentLocation, args.requestOrigin, args.aContext, args.mimeTypeGuess, args.extra);
+ return { passed: result == nsIContentPolicy.ACCEPT, message : "Expected nsIContentPolicy.ACCEPT, actual was " + result };
+ }
+
+ if (typeof data[2] == "function") {
+ return data[2](runTest);
+ } else {
+ return runTest();
+ }
+ },
+
+ describe : function(data) { return data[0]; },
+ tests : [
+ ["Accepts if not TYPE_DOCUMENT", { contentType : nsIContentPolicy.TYPE_STYLESHEET}],
+ ["Accepts if not http or https", { contentLocation : "resource://foo/bar"}],
+ ["Accepts if no aContext", { aContext : null}],
+ ["Accepts if aContext has no loadURI function", { aContext : { foo : function(){}}}],
+ ["Accepts if Redirector is not enabled", {}, function(doFunc) {
+ try {
+ redirector.enabled = false;
+ return doFunc();
+ } catch(e) {
+ redirector.enabled = true;
+ throw e;
+ }
+ }]
+ ]
+ }
};
|