diff options
author | Einar Egilsson | 2011-09-04 22:53:16 +0200 |
---|---|---|
committer | Einar Egilsson | 2011-09-04 22:53:16 +0200 |
commit | 4b612baf4f29178214edec130856d83a40bf473d (patch) | |
tree | 20e36e3fcf7a24e829640c2fd605622f14af0edc /chrome/unittest | |
parent | 8fb65cf8aeaaf66636698310c0168929fc1a2ce0 (diff) |
Total restructuring of files and deletion of old files
Diffstat (limited to 'chrome/unittest')
-rw-r--r-- | chrome/unittest/run.html | 99 | ||||
-rw-r--r-- | chrome/unittest/testcases.js | 129 |
2 files changed, 228 insertions, 0 deletions
diff --git a/chrome/unittest/run.html b/chrome/unittest/run.html new file mode 100644 index 0000000..cc178aa --- /dev/null +++ b/chrome/unittest/run.html @@ -0,0 +1,99 @@ +<!-- $Id$ --> +<html> + <head> + <title>Redirector Unit Tests</title> + <style type="text/css"> + body { font-family: Verdana, Arial; color:black; background-color:white; font-size:0.8em; width:800px; margin:auto; text-align:center;} + a { color:blue; } + h1 { text-align:center; margin:10px 0px; } + table { margin:10px auto; border:solid 1px black; width:700px; border-collapse:collapse;} + td { border:solid 1px black; padding:3px; } + td.result { width:20px; height:20px; padding:0;} + td.result div { width:70%; height:70%; margin:auto; } + button { margin:20px auto; } + </style> + <script type="text/javascript"> + + //Global variables + var subscriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader); + var redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.rdIRedirector); + + function setupTest(name, testcase) { + var table = document.createElement('table'); + var row = document.createElement('tr'); + var cell = document.createElement('th'); + var testdata; + cell.setAttribute('colspan', 2); + row.appendChild(cell); + table.appendChild(row); + cell.innerHTML = name; + document.getElementById('tests').appendChild(table); + for (var i = 0; i < testcase.tests.length; i++) { + var testdata = testcase.tests[i]; + row = document.createElement('tr'); + cell = document.createElement('td'); + cell.setAttribute('class', 'result'); + var dot = document.createElement('div'); + dot.setAttribute('id', name + '_' + i); + cell.appendChild(dot); + + row.appendChild(cell); + cell = document.createElement('td'); + cell.innerHTML = testcase.describe(testdata); + row.appendChild(cell); + table.appendChild(row); + } + } + + function setup() { + //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) { + sorted.push(name); + } + + sorted.sort(); + for each(var name in sorted) { + setupTest(name, tests[name]); + } + } + + function runTests() { + for (var testcaseName in tests) { + var testcase = tests[testcaseName]; + for (var i = 0; i < testcase.tests.length; i++) { + try { + var dot = document.getElementById(testcaseName + '_' + i); + var result = testcase.run(testcase.tests[i]); + if (result && result.passed) { + dot.style.backgroundColor = '#17f816'; + } else { + dot.style.backgroundColor = '#ff0000'; + if (result && result.message) { + dot.parentNode.nextSibling.innerHTML += '<br/><span style="color:red;">' + result.message + '</span>'; + } + } + } catch(e) { + dot.style.backgroundColor = '#ff0000'; + dot.parentNode.nextSibling.innerHTML += '<br/><span style="color:red;">' + e + '</span>'; + ; + } + } + } + } + + </script> + </head> + <body onload="setup();"> + <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/unittest/testcases.js b/chrome/unittest/testcases.js new file mode 100644 index 0000000..0c0c10c --- /dev/null +++ b/chrome/unittest/testcases.js @@ -0,0 +1,129 @@ +//// $Id$ +var nsIContentPolicy = Components.interfaces.nsIContentPolicy; + +var tests = { + "Wildcard matches" : { + run : function(data,log) { + var pattern = data[0], + url = data[1], + expected = data[2]; + var parts = expected.split(','); + var redirectUrl = ''; + if (!(parts.length == 1 && parts[0] == '')) { + for (var i in parts) { + redirectUrl += '$' + (parseFloat(i)+1) + ','; + } + redirectUrl = redirectUrl.substr(0, redirectUrl.length-1); + } + var redirect = new Redirect(null, pattern, redirectUrl, Redirect.WILDCARD); + var result = redirect.getMatch(url); + return { passed: result.isMatch && (result.redirectTo == expected), message : "Expected '" + expected + "', actual was '" + result.redirectTo + "'"}; + }, + + describe : function(data) { return data[0] + ' == ' + data[1] + ', matches=' + data[2]; }, + tests : [ + ['http://foo*', 'http://foobar.is', 'bar.is'], + ['http://foo*', 'http://foo', ''], + ['*://foo.is', 'http://foo.is', 'http'], + ['*http://foo.is', 'http://foo.is', ''], + ['http*foo*', 'http://foobar.is', '://,bar.is'], + ['http*foo*', 'http://foo', '://,'], + ['*://f*.is', 'http://foo.is', 'http,oo'], + ['*http://f*.is', 'http://foo.is', ',oo'], + ['*foo*', 'http://foo', 'http://,'], + ['*foo*', 'foobar.is', ',bar.is'], + ['*foo*', 'http://foobar.is', 'http://,bar.is'], + ['http://foo.is', 'http://foo.is', ''], + ['*', 'http://foo.is', 'http://foo.is'], + ['*://*oo*bar*', 'http://foo.is/bar/baz', 'http,f,.is/,/baz'], + ['*://**oo*bar*', 'http://foo.is/bar/baz', 'http,,f,.is/,/baz'], + ] + }, + + "Regex matches" : { + run : function(data) { + var pattern = data[0], + url = data[1], + expected = data[2]; + var parts = expected.split(','); + var redirectUrl = ''; + if (!(parts.length == 1 && parts[0] == '')) { + for (var i in parts) { + redirectUrl += '$' + (parseFloat(i)+1) + ','; + } + redirectUrl = redirectUrl.substr(0, redirectUrl.length-1); + } + var redirect = new Redirect(null, pattern, redirectUrl, Redirect.REGEX, null, null); + var result = redirect.getMatch(url); + return { passed: result.isMatch && result.redirectTo == expected, message : "Expected '" + expected + "', actual was '" + result.redirectTo + "'"}; + }, + + describe : function(data) { return data[0] + ' == ' + data[1] + ', matches=' + data[2]; }, + tests : [ + ['http://foo(.*)', 'http://foobar.is', 'bar.is'], + ['http://foo(.*)', 'http://foo', ''], + ['(.*)://foo.is', 'http://foo.is', 'http'], + ['(.*)http://foo\\.is', 'http://foo.is', ''], + ['http(.*)foo(.*)', 'http://foobar.is', '://,bar.is'], + ['http(.*)foo(.*)', 'http://foo', '://,'], + ['(.*)://f(.*)\\.is', 'http://foo.is', 'http,oo'], + ['(.*)http://f(.*)\\.is', 'http://foo.is', ',oo'], + ['(.*)foo(.*)', 'http://foo', 'http://,'], + ['(.*)foo(.*)', 'foobar.is', ',bar.is'], + ['(.*)foo(.*)', 'http://foobar.is', 'http://,bar.is'], + ['http://foo\.is', 'http://foo.is', ''], + ['(.*)', 'http://foo.is', 'http://foo.is'], + ['(.*)://(.*)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 contentPolicy = redirector.QueryInterface(nsIContentPolicy); + var result = contentPolicy.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(); + redirector.enabled = true; + + } catch(e) { + redirector.enabled = true; + throw e; + } + }] + ] + } +}; |