667
edits
Changes
→Instructions
== Instructions ==
Mozilla [http://developer.mozilla.org/en/docs/Mozilla_automated_testing currently uses ] three unit test methods:
# The first is '''xpcshell''' for C++ unit tests that run on the command line. These are the best method to use whenever possible. Using them effectively requires composing code as stateless functions that return values whenever possible--always a good practice.
For purposes of testing our FirstXpcom component, we will use xpcshell tests. This will allow us to easily write tests to insure the functionality of our class's methods.
$ cd mozilla/extensions/firstxpcom
$ mkdir test
* Now copy the xpcshell test-harness code into this directory:
$ cd mozilla/extensions/firstxpcom/test
$ cp -r ../../../tools/test-harness/xpcshell-simple/example/* .
* Now create some Unit Tests for your component so you can easily test things as you go (see This will result in the following for more details -- httpfile/directories being copied to '''test'''://developer.mozilla.org/en/docs/Writing_xpcshell-based_unit_tests)
# Note: DEPTH should be set to the relative path to mozilla/ DEPTH = ../../.. topsrcdir = @top_srcdir@ srcdir = @srcdir@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk # Note: set the test module's name to test_<yourmodule> MODULE = test_firstxpcom # This is a list of directories containing tests to run, separated by spaces. # Most likely, tho, you won't use more than one directory here. XPCSHELL_TESTS = unit include $(topsrcdir)/config/rules.mk # Note: Invoke any additional (non-xpcshell) test programs here. check::
* Your component's Makefile needs to include $(DEPTH)this new test directory. Modify '''mozilla/extensions/configfirstxpcom/autoconfMakefile.mkin''' (change shown in bold):
DEPTH = ../.. topsrcdir = @top_srcdir@ srcdir = @srcdir@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk MODULE = weblockfirstxpcom DIRS = public \ src \ $(NULL) '''# Adding xpcshell unit tests''' '''ifdef ENABLE_TESTS''' '''DIRS += test''' '''endif''' XPI_NAME = firstxpcom # A Unique ID for your extension INSTALL_EXTENSION_ID = firstxpcom@senecac.on.ca # Will create a .xpi in /mozilla/$(MOZ_OBJDIR)/dist/xpi-stage/ XPI_PKGNAME = firstxpcom # install.rdf will tell Firefox how to install our extension and # this says, "copy install.rdf into our extension dir and xpi" DIST_FILES = install.rdf include $(topsrcdir)/config/rules.mk
function run_test() {
// Create an instance of FirstXpcom to test
try {
const cid = "@senecac.on.ca/firstxpcom;1";
var obj = Components.classes[cid].createInstance();
obj = obj.QueryInterface(Components.interfaces.IFirstXpcom);
} catch (err) {
do_throw(err);
return;
}
// Test Add
var result = obj.add(1, 2);
do_check_eq(3, result);
// Test Name get and set
var name = "New String";
obj.name = name;
do_check_eq(name, obj.name);
return true;
}
$ cd mozilla/''objdir''/extensions/firstxpcom
$ make check
== Resources ==