transport: pass correct channel
[abstract.git] / xpunit / nsTestFrame.jsm
blob18a9441ebd62105c229e67f925da212f50b7471d
1 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=javascript: */
2 /*
3  * Copyright (C) 2007 Sergey Yanovich <ynvich@gmail.com>
4  *
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.
9  *
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.
14  *
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.
19  */
21 // addMethod - By John Resig (MIT Licensed)
22 function addMethod(object, name, fn){
23   var old = object[ name ];
24   if ( old )
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);
30     };
31   else
32     object[ name ] = fn;
35 /* Base object for javascript tests  */
36 function JSTest()
39 JSTest.prototype = {
40     get name() { return this._name; },
41     set name() { throw "readonly property"; },
42     _name : null,
44     get CID() { return this._CID; },
45     set CID() { throw "readonly property"; },
46     _CID : null,
48     get contractID() { return this._contractID; },
49     set contractID() { throw "readonly property"; },
50     _contractID : null,
51     _test : null,
52     _check : null,
53     
54     addJSFailure: function(runner, msg) {
55         runner.addJSFailure("Test cid: " + this.CID + "; Message: " + msg);
56     },
57     
58     /* nsISupports */
59     QueryInterface : function (iid)
60     {
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))
67             return this;
69         throw Components.results.NS_ERROR_NO_INTERFACE;
70     },
71     /* nsITest */
72     test : function (runner)
73     {
74         runner.markTestStart();
75         this._test(runner);
76     },
77     set async() { throw "readonly property"; },
78     get async() { return false; },
79     /* nsITimerCallback */
80     notify : function (timer)
81     {
82         var runner = Components.classes["@aasii.org/cxxunit/testrunner"].getService(nsCI.nsITestRunner);
83         try {
84             this._check(runner);
85         } finally {
86             runner.markTestEnd(this);
87         }
88     },
89     /* nsIWebProgressListener */
90     onStateChange : function( aWebProgress, aRequest, aStateFlags, aStatus)
91     {
92         var timer = Components.classes["@mozilla.org/timer;1"].getService(nsCI.nsITimer);
93         timer.initWithCallback(this, 0, nsCI.nsITimer.TYPE_ONE_SHOT);
94     },
95     /* nsIFactory */
96     createInstance : function (outer, iid)
97     {
98         if (outer != null)
99             throw Components.results.NS_ERROR_NO_AGGREGATION;
100         return this.QueryInterface(iid);
101     },
102     lockFactory : function (lock)
103     {
104         /* no-op */
105     },
106     /* nsIModule */
107     getClassObject : function mod_gch(compMgr, cid, iid)
108     {
109         if (cid.equals(this.CID)){
110             return this.QueryInterface(iid);
111         }
112         throw Components.results.NS_ERROR_NOT_REGISTERED;
113     },
114     registerSelf : function mod_regself(compMgr, fileSpec, location, type)
115     {
116         compMgr.QueryInterface(nsCI.nsIComponentRegistrar);
117         compMgr.registerFactoryLocation(this._CID, this._name, this._contractID, fileSpec, location, type);
118     },
119     unregisterSelf : function mod_unreg(compMgr, location, type)
120     {
121         compMgr.QueryInterface(nsCI.nsIComponentRegistrar);
122         compMgr.unregisterFactoryLocation(this.CID, location);
123     },
124     canUnload : function(compMgr)
125     {
126         return true;
127     }
131 /* XPCOM module */
132 function JSTestModule()
134     var me = new JSTest();
135     //get old functions
136     me.base_unregisterSelf = me.unregisterSelf;
137     me.base_QueryInterface = me.QueryInterface;
138     me.base_getClassObject = me.getClassObject;
139     me.base_registerSelf = me.registerSelf;
140     //variables
141     me._tests = [];
142     me._map = {};
143     me._child = 0;
144     me._parse_counter = 0;
145     //functions
146     me._test = function(runner) {
147         /* no - op */
148     }
149     me._check = function(runner) {
150         /* no - op */
151     }
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;
157     });
158     addMethod(me, '_add', function(cid, contractID, name, testF, checkF){
159         var test = new JSTest();
160         test._CID = cid;
161         test._name = name;
162         test._contractID = contractID;
163         test._test = testF;
164         test._check = checkF;
165         this._add(test);
166     });
167     /*me._add = function(cid, contractID, name, testF, checkF){
168         var test = new JSTest();
169         test._CID = cid;
170         test._name = name;
171         test._contractID = contractID;
172         test._test = testF;
173         test._check = checkF;
174         var i = this._tests.length;
175         this._tests[i] = test;
176         this._map[test.CID] = i;
177     }*/
178     me._repeat = function(idx) {
179         var i = this._tests.length;
180         this._tests[i] = this._tests[idx];
181     }
182     //test flow
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");
189             } else {
190                 if (!this.parseAction(action)) {
191                     throw "cann't parse action";
192                 }
193             }
194         }
195     }
196     //end test flow
197     /* nsISupports */
198     me.QueryInterface = function (iid)
199     {
200         if (iid.equals(nsCI.nsIUTF8StringEnumerator)) return this;
201         return this.base_QueryInterface(iid);
202     }
203     /* nsIStringEnumerator */
204     me.hasMore = function ()
205     {
206         if (this._child < this._tests.length)
207             return true;
208         else
209             return false;
210     }
211     me.getNext = function ()
212     {
213         if (this._child >= this._tests.length)
214             throw Components.results.NS_ERROR_NOT_AVAILABLE;
215         return this._tests[this._child++].contractID;
216     }
217     /* nsIModule */
218     me.getClassObject = function mod_gch(compMgr, cid, iid)
219     {
220         if (!cid.equals(this.CID)) {
221             if (cid in this._map) {
222                 return this._tests[this._map[cid]].QueryInterface(iid);
223             } else {
224                 var i, count = this._tests.length;
225                 var obj = null;
226                 for (i = 0; i < count ; i++) {
227                     var o = this._tests[i];
228                     try {
229                         obj = o.getClassObject(compMgr, cid, iid);
230                         if (null != obj) {
231                             return obj;
232                         }
233                     } catch (e) {
234                         obj = null;
235                     }
236                 }
237                 if (null == obj) throw Components.results.NS_ERROR_NOT_REGISTERED;
238                 return obj;
239             }
240         }
241         return this.base_getClassObject(compMgr, cid, iid);
242     }
243     me.registerSelf = function mod_regself(compMgr, fileSpec, location, type)
244     {
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];
249             try {
250                 o.registerSelf(compMgr, fileSpec, location, type);
251             } catch (e) {
252             }
253         }
254     }
255     me.unregisterSelf = function mod_unreg(compMgr, location, type)
256     {
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];
261             try {
262                 o.unregisterSelf(compMgr, location, type);
263             } catch (e) {
264             }
265         }
266     }
267     return me;