My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Project Information
Members
Links

Description

Let you test your Firefox/Thunderbird extension from inside the application and with command line with Rhino.

How does it work

Very simple, you could run it under Rhino JavaScript Shell or under Mozilla application from inside your extension code.

Show me example

file testExample.js

function make() {
    return {
	log:"Log",
	files: [1, 2, "3"]
    };
}

test.example = function() {
    assert.that(make(), is.eqJson({
	log:"Log",
	files:[1, 2, 3]}));
};

produce following log

testExample.js: example: Assert #1: < expected: 
{
    'files' : [
        1,
        2,
        3
    ],
    'log' : 'Log'
}
:>, < got: 
{
    'files' : [
        1,
        2,
        '3'
    ],
    'log' : 'Log'
}
:>, < difference between (-)expected and (+)got : 
 
 {
     'files' : [
         1,
-        3
+        '3'
     ],
     'log' : 'Log'
 }
 

:>
testExample.js: Warning: Reason: Polluted global namespace with 'make'

What functions it provides

log(text)
ignore(name) //ignore global variable with name "name"
importFile(filename) //to import another JS file
various assert methods

Installation

First you have to create a folder just under your extension root (where content folder is) named test-framework and copy code there. Alternatively you could use svn:externals to do that.

Then somewhere in the extension code you create a folder for test cases. Files with test cases should start with "test".

Now create some test or copy that example from above and you are almost ready to go. To start tests from command line you should run

java -cp test-framework/js.jar org.mozilla.javascript.tools.shell.Main -version 170 -debug test-framework/main.js --test-directory content/tests/

--test-directory command line option obviously should point to directory with tests

Start it from application is a little bit harder. Just copy the code from below and change the id of extension and path to tests

var MY_ID = "{282C3C7A-15A8-4037-A30D-BBEB23FFC76B}";
var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "test-framework/main.js");
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
fstream.init(file, -1, 0, 0);
sstream.init(fstream);

var data = "";
var str = sstream.read(4096);
while (str.length > 0) {data += str; str = sstream.read(4096);}

sstream.close();
fstream.close();

eval(data);
run_tests(MY_ID, "content/tests/", document);

Make sure that code doesn't go into your packaged extension of is turned off by some preference. It won't work and might be security issue.

Limitation

It will work only where JavaScript 1.7 supported, that is Gecko 1.8.1 or above. It will work only if your extension is installed to development, that is, not packaged. That's when you want it to run anyway.

It doesn't work / I know a better way

Send me a patch. Make a note if you'd like to become co-owner.

Powered by Google Project Hosting