1 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=javascript: */
3 * Copyright (C) 2007 Sergey Yanovich <ynvich@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 // addMethod - By John Resig (MIT Licensed)
22 function addMethod(object, name, fn){
23 var old = object[ name ];
25 object[ name ] = function(){
26 if ( fn.length == arguments.length )
27 return fn.apply(this, arguments);
28 else if ( typeof old == 'function' )
29 return old.apply(this, arguments);
35 /* Base object for javascript tests */
40 get name() { return this._name; },
41 set name() { throw "readonly property"; },
44 get CID() { return this._CID; },
45 set CID() { throw "readonly property"; },
48 get contractID() { return this._contractID; },
49 set contractID() { throw "readonly property"; },
54 addJSFailure: function(runner, msg) {
55 runner.addJSFailure("Test cid: " + this.CID + "; Message: " + msg);
59 QueryInterface : function (iid)
61 if (iid.equals(nsCI.nsITest) ||
62 iid.equals(nsCI.nsITimerCallback) ||
63 iid.equals(nsCI.nsIWebProgressListener) ||
64 iid.equals(nsCI.nsIFactory) ||
65 iid.equals(nsCI.nsIModule) ||
66 iid.equals(nsCI.nsISupports))
69 throw Components.results.NS_ERROR_NO_INTERFACE;
72 test : function (runner)
74 runner.markTestStart();
77 set async() { throw "readonly property"; },
78 get async() { return false; },
79 /* nsITimerCallback */
80 notify : function (timer)
82 var runner = Components.classes["@aasii.org/cxxunit/testrunner"].getService(nsCI.nsITestRunner);
86 runner.markTestEnd(this);
89 /* nsIWebProgressListener */
90 onStateChange : function( aWebProgress, aRequest, aStateFlags, aStatus)
92 var timer = Components.classes["@mozilla.org/timer;1"].getService(nsCI.nsITimer);
93 timer.initWithCallback(this, 0, nsCI.nsITimer.TYPE_ONE_SHOT);
96 createInstance : function (outer, iid)
99 throw Components.results.NS_ERROR_NO_AGGREGATION;
100 return this.QueryInterface(iid);
102 lockFactory : function (lock)
107 getClassObject : function mod_gch(compMgr, cid, iid)
109 if (cid.equals(this.CID)){
110 return this.QueryInterface(iid);
112 throw Components.results.NS_ERROR_NOT_REGISTERED;
114 registerSelf : function mod_regself(compMgr, fileSpec, location, type)
116 compMgr.QueryInterface(nsCI.nsIComponentRegistrar);
117 compMgr.registerFactoryLocation(this._CID, this._name, this._contractID, fileSpec, location, type);
119 unregisterSelf : function mod_unreg(compMgr, location, type)
121 compMgr.QueryInterface(nsCI.nsIComponentRegistrar);
122 compMgr.unregisterFactoryLocation(this.CID, location);
124 canUnload : function(compMgr)
132 function JSTestModule()
134 var me = new JSTest();
136 me.base_unregisterSelf = me.unregisterSelf;
137 me.base_QueryInterface = me.QueryInterface;
138 me.base_getClassObject = me.getClassObject;
139 me.base_registerSelf = me.registerSelf;
144 me._parse_counter = 0;
146 me._test = function(runner) {
149 me._check = function(runner) {
152 //you must call _add methods with appropriate arguments count
153 addMethod(me, '_add', function(_jsTest){
154 var i = this._tests.length;
155 this._tests[i] = _jsTest;
156 this._map[_jsTest.CID] = i;
158 addMethod(me, '_add', function(cid, contractID, name, testF, checkF){
159 var test = new JSTest();
162 test._contractID = contractID;
164 test._check = checkF;
167 /*me._add = function(cid, contractID, name, testF, checkF){
168 var test = new JSTest();
171 test._contractID = contractID;
173 test._check = checkF;
174 var i = this._tests.length;
175 this._tests[i] = test;
176 this._map[test.CID] = i;
178 me._repeat = function(idx) {
179 var i = this._tests.length;
180 this._tests[i] = this._tests[idx];
183 me.parseAction = null;
184 me.createTestsFlow = function(actions) {
185 for (this._parse_counter = 0; this._parse_counter < actions.length; this._parse_counter++) {
186 var action = actions[this._parse_counter];
187 if (!this.parseAction) {
188 //runner.addJSFailure("JSTestModule - cann't parse action, parseAction function is not defined");
190 if (!this.parseAction(action)) {
191 throw "cann't parse action";
198 me.QueryInterface = function (iid)
200 if (iid.equals(nsCI.nsIUTF8StringEnumerator)) return this;
201 return this.base_QueryInterface(iid);
203 /* nsIStringEnumerator */
204 me.hasMore = function ()
206 if (this._child < this._tests.length)
211 me.getNext = function ()
213 if (this._child >= this._tests.length)
214 throw Components.results.NS_ERROR_NOT_AVAILABLE;
215 return this._tests[this._child++].contractID;
218 me.getClassObject = function mod_gch(compMgr, cid, iid)
220 if (!cid.equals(this.CID)) {
221 if (cid in this._map) {
222 return this._tests[this._map[cid]].QueryInterface(iid);
224 var i, count = this._tests.length;
226 for (i = 0; i < count ; i++) {
227 var o = this._tests[i];
229 obj = o.getClassObject(compMgr, cid, iid);
237 if (null == obj) throw Components.results.NS_ERROR_NOT_REGISTERED;
241 return this.base_getClassObject(compMgr, cid, iid);
243 me.registerSelf = function mod_regself(compMgr, fileSpec, location, type)
245 this.base_registerSelf(compMgr, fileSpec, location, type);
246 var i, count = this._tests.length;
247 for (i = 0; i < count ; i++) {
248 var o = this._tests[i];
250 o.registerSelf(compMgr, fileSpec, location, type);
255 me.unregisterSelf = function mod_unreg(compMgr, location, type)
257 this.base_unregisterSelf(compMgr, location, type);
258 var i, count = this._tests.length;
259 for (i = 0; i < count ; i++) {
260 var o = this._tests[i];
262 o.unregisterSelf(compMgr, location, type);