Release v0.0.2
[abstract.git] / xpunit / nsTestFrame.jsm
blob5a2b2a1f87f1a1c081d8691a925c908cd453ef14
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 /* Base object for javascript tests  */
22 function JSTest(testF, checkF)
24   this._test = testF;
25   this._check = checkF;
28 JSTest.prototype =
30   _test : null,
31   _check : null,
33   /* nsISupports */
34   QueryInterface : function (iid)
35   {
36     if (iid.equals(nsCI.nsITest) ||
37         iid.equals(nsCI.nsITimerCallback) ||
38         iid.equals(nsCI.nsIWebProgressListener) ||
39         iid.equals(nsCI.nsIFactory) ||
40         iid.equals(nsCI.nsISupports))
41       return this;
43     throw Components.results.NS_ERROR_NO_INTERFACE;
44   },
46   /* nsITest */
48   test : function (runner)
49   {
50     runner.markTestStart();
51     this._test(runner);
52   },
54   set async() { throw "readonly property"; },
55   get async() { return true; },
57   /* nsITimerCallback */
59   notify : function (timer)
60   {
61     var runner = Components.classes["@aasii.org/cxxunit/testrunner"]
62       .getService(nsCI.nsITestRunner);
63     try {
64       this._check(runner);
65     } finally {
66       runner.markTestEnd(this);
67     }
68   },
70   /* nsIWebProgressListener */
72   onStateChange: function( aWebProgress, aRequest, aStateFlags, aStatus)
73   {
74     var runner = Components.classes["@aasii.org/cxxunit/testrunner"]
75       .getService(nsCI.nsITestRunner);
76     runner.armTimer();
77   },
79   onProgressChange: function() {},
81   onLocationChange: function() {},
83   onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
84   {
85   },
87   onSecurityChange: function() {},
89   /* nsIFactory */
91   createInstance : function (outer, iid)
92   {
93     if (outer != null)
94       throw Components.results.NS_ERROR_NO_AGGREGATION;
96     return this.QueryInterface(iid);
97   },
99   lockFactory : function (lock)
100   {
101     /* no-op */
102   }
105 /* XPCOM module */
106 function JSTestModule()
110 JSTestModule.prototype =
112   get name() { return this._name; },
113   set name() { throw "readonly property"; },
114   _name : null,
116   get CID() { return this._CID; },
117   set CID() { throw "readonly property"; },
118   _CID : null,
120   get contractID() { return this._contractID; },
121   set contractID() { throw "readonly property"; },
122   _contractID : null,
124   _tests : [],
125   _map : {},
126   _child : 0,
128   _add : function (cid, contractID, name, testF, checkF)
129   {
130     var i = this._tests.length;
131     this._tests[i] = { cid : cid,
132                        contractID : contractID,
133                        name : name,
134                        instance : null
135                      };
136     this._map[cid] = i;
137     this._tests[i].instance = new JSTest(testF, checkF);
138   },
140   _repeat : function (index)
141   {
142     var i = this._tests.length;
143     this._tests[i] = this._tests[index];
144   },
146   /* nsISupports */
147   QueryInterface : function mod_QI(iid)
148   {
149     if (iid.equals(nsCI.nsIModule) ||
150         iid.equals(nsCI.nsITest) ||
151         iid.equals(nsCI.nsIUTF8StringEnumerator) ||
152         iid.equals(nsCI.nsIFactory) ||
153         iid.equals(nsCI.nsISupports))
154       return this;
156     throw Components.results.NS_ERROR_NO_INTERFACE;
157   },
159   /* nsITest */
161   test : function (runner)
162   {
163     /* no-op */
164   },
166   set async() { throw "readonly property"; },
167   get async() { return false; },
169   /* nsIStringEnumerator */
171   hasMore : function ()
172   {
173     if (this._child < this._tests.length)
174       return true;
175     else
176       return false;
177   },
179   getNext : function ()
180   {
181     if (this._child >= this._tests.length)
182       throw Components.results.NS_ERROR_NOT_AVAILABLE;
183     return this._tests[this._child++].contractID;
184   },
186   /* nsIFactory */
188   createInstance : function (outer, iid)
189   {
190     if (outer != null)
191       throw Components.results.NS_ERROR_NO_AGGREGATION;
193     return this.QueryInterface(iid);
194   },
196   lockFactory : function (lock)
197   {
198     /* no-op */
199   },
201   /* nsIModule */
202   getClassObject : function mod_gch(compMgr, cid, iid)
203   {
204     if (cid.equals(this.CID)){
205       return this.QueryInterface(iid);
206     }
208     if (cid in this._map) {
209       return this._tests[this._map[cid]].instance.QueryInterface(iid);
210     }
212     throw Components.results.NS_ERROR_NOT_REGISTERED;
213   },
215   registerSelf : function mod_regself(compMgr, fileSpec, location, type)
216   {
217     compMgr.QueryInterface(nsCI.nsIComponentRegistrar);
219     compMgr.registerFactoryLocation(this._CID, this._name, this._contractID,
220         fileSpec, location, type);
222     var i, count = this._tests.length;
223     for (i = 0; i < count ; i++) {
224       var o = this._tests[i];
225       try {
226         compMgr.registerFactoryLocation(o.cid, o.name, o.contractID,
227             fileSpec, location, type);
228       } catch (e) {
229       }
230     }
231   },
233   unregisterSelf : function mod_unreg(compMgr, location, type)
234   {
235     compMgr.QueryInterface(nsCI.nsIComponentRegistrar);
237     compMgr.unregisterFactoryLocation(this.CID, location);
239     var i, count = this._tests.length;
240     for (i = 0; i < count ; i++) {
241       var o = this._tests[i];
242       try {
243         compMgr.unregisterFactoryLocation(o.cid, location);
244       } catch (e) {
245       }
246     }
247   },
249   canUnload : function (compMgr)
250   {
251     return true;
252   }
255 /* Utilities */
256 function sendOnInput(target)
258   var evnt = target.ownerDocument.createEvent("Event");
259   evnt.initEvent("input",true,true);
260   target.dispatchEvent(evnt);
263 function sendOnChange(target)
265   var evnt = target.ownerDocument.createEvent("Event");
266   evnt.initEvent("change",true,true);
267   target.dispatchEvent(evnt);
270 function sendOnClick(target)
272   var wnd = target.ownerDocument.defaultView;
273   var evt = target.ownerDocument.createEvent("MouseEvents");
274   evt.initMouseEvent("click", true, true, wnd,
275       1, 0, 0, 0, 0, false, false, false, false, 0, null);
276   target.dispatchEvent(evt);