services/sync/tps/extensions/mozmill/resource/modules/frame.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 var EXPORTED_SYMBOLS = ['Collector','Runner','events', 'runTestFile', 'log',
michael@0 6 'timers', 'persisted', 'shutdownApplication'];
michael@0 7
michael@0 8 const Cc = Components.classes;
michael@0 9 const Ci = Components.interfaces;
michael@0 10 const Cu = Components.utils;
michael@0 11
michael@0 12 const TIMEOUT_SHUTDOWN_HTTPD = 15000;
michael@0 13
michael@0 14 Cu.import("resource://gre/modules/Services.jsm");
michael@0 15
michael@0 16 Cu.import('resource://mozmill/stdlib/httpd.js');
michael@0 17
michael@0 18 var broker = {}; Cu.import('resource://mozmill/driver/msgbroker.js', broker);
michael@0 19 var assertions = {}; Cu.import('resource://mozmill/modules/assertions.js', assertions);
michael@0 20 var errors = {}; Cu.import('resource://mozmill/modules/errors.js', errors);
michael@0 21 var os = {}; Cu.import('resource://mozmill/stdlib/os.js', os);
michael@0 22 var strings = {}; Cu.import('resource://mozmill/stdlib/strings.js', strings);
michael@0 23 var arrays = {}; Cu.import('resource://mozmill/stdlib/arrays.js', arrays);
michael@0 24 var withs = {}; Cu.import('resource://mozmill/stdlib/withs.js', withs);
michael@0 25 var utils = {}; Cu.import('resource://mozmill/stdlib/utils.js', utils);
michael@0 26
michael@0 27 var securableModule = {};
michael@0 28 Cu.import('resource://mozmill/stdlib/securable-module.js', securableModule);
michael@0 29
michael@0 30 var uuidgen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
michael@0 31
michael@0 32 var httpd = null;
michael@0 33 var persisted = {};
michael@0 34
michael@0 35 var assert = new assertions.Assert();
michael@0 36
michael@0 37 var mozmill = undefined;
michael@0 38 var mozelement = undefined;
michael@0 39 var modules = undefined;
michael@0 40
michael@0 41 var timers = [];
michael@0 42
michael@0 43
michael@0 44 /**
michael@0 45 * Shutdown or restart the application
michael@0 46 *
michael@0 47 * @param {boolean} [aFlags=undefined]
michael@0 48 * Additional flags how to handle the shutdown or restart. The attributes
michael@0 49 * eRestarti386 and eRestartx86_64 have not been documented yet.
michael@0 50 * @see https://developer.mozilla.org/nsIAppStartup#Attributes
michael@0 51 */
michael@0 52 function shutdownApplication(aFlags) {
michael@0 53 var flags = Ci.nsIAppStartup.eForceQuit;
michael@0 54
michael@0 55 if (aFlags) {
michael@0 56 flags |= aFlags;
michael@0 57 }
michael@0 58
michael@0 59 // Send a request to shutdown the application. That will allow us and other
michael@0 60 // components to finish up with any shutdown code. Please note that we don't
michael@0 61 // care if other components or add-ons want to prevent this via cancelQuit,
michael@0 62 // we really force the shutdown.
michael@0 63 let cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"].
michael@0 64 createInstance(Components.interfaces.nsISupportsPRBool);
michael@0 65 Services.obs.notifyObservers(cancelQuit, "quit-application-requested", null);
michael@0 66
michael@0 67 // Use a timer to trigger the application restart, which will allow us to
michael@0 68 // send an ACK packet via jsbridge if the method has been called via Python.
michael@0 69 var event = {
michael@0 70 notify: function(timer) {
michael@0 71 Services.startup.quit(flags);
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
michael@0 76 timer.initWithCallback(event, 100, Ci.nsITimer.TYPE_ONE_SHOT);
michael@0 77 }
michael@0 78
michael@0 79 function stateChangeBase(possibilties, restrictions, target, cmeta, v) {
michael@0 80 if (possibilties) {
michael@0 81 if (!arrays.inArray(possibilties, v)) {
michael@0 82 // TODO Error value not in this.poss
michael@0 83 return;
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 if (restrictions) {
michael@0 88 for (var i in restrictions) {
michael@0 89 var r = restrictions[i];
michael@0 90 if (!r(v)) {
michael@0 91 // TODO error value did not pass restriction
michael@0 92 return;
michael@0 93 }
michael@0 94 }
michael@0 95 }
michael@0 96
michael@0 97 // Fire jsbridge notification, logging notification, listener notifications
michael@0 98 events[target] = v;
michael@0 99 events.fireEvent(cmeta, target);
michael@0 100 }
michael@0 101
michael@0 102
michael@0 103 var events = {
michael@0 104 appQuit : false,
michael@0 105 currentModule : null,
michael@0 106 currentState : null,
michael@0 107 currentTest : null,
michael@0 108 shutdownRequested : false,
michael@0 109 userShutdown : null,
michael@0 110 userShutdownTimer : null,
michael@0 111
michael@0 112 listeners : {},
michael@0 113 globalListeners : []
michael@0 114 }
michael@0 115
michael@0 116 events.setState = function (v) {
michael@0 117 return stateChangeBase(['dependencies', 'setupModule', 'teardownModule',
michael@0 118 'test', 'setupTest', 'teardownTest', 'collection'],
michael@0 119 null, 'currentState', 'setState', v);
michael@0 120 }
michael@0 121
michael@0 122 events.toggleUserShutdown = function (obj){
michael@0 123 if (!this.userShutdown) {
michael@0 124 this.userShutdown = obj;
michael@0 125
michael@0 126 var event = {
michael@0 127 notify: function(timer) {
michael@0 128 events.toggleUserShutdown(obj);
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 this.userShutdownTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
michael@0 133 this.userShutdownTimer.initWithCallback(event, obj.timeout, Ci.nsITimer.TYPE_ONE_SHOT);
michael@0 134
michael@0 135 } else {
michael@0 136 this.userShutdownTimer.cancel();
michael@0 137
michael@0 138 // If the application is not going to shutdown, the user shutdown failed and
michael@0 139 // we have to force a shutdown.
michael@0 140 if (!events.appQuit) {
michael@0 141 this.fail({'function':'events.toggleUserShutdown',
michael@0 142 'message':'Shutdown expected but none detected before timeout',
michael@0 143 'userShutdown': obj});
michael@0 144
michael@0 145 var flags = Ci.nsIAppStartup.eAttemptQuit;
michael@0 146 if (events.isRestartShutdown()) {
michael@0 147 flags |= Ci.nsIAppStartup.eRestart;
michael@0 148 }
michael@0 149
michael@0 150 shutdownApplication(flags);
michael@0 151 }
michael@0 152 }
michael@0 153 }
michael@0 154
michael@0 155 events.isUserShutdown = function () {
michael@0 156 return this.userShutdown ? this.userShutdown["user"] : false;
michael@0 157 }
michael@0 158
michael@0 159 events.isRestartShutdown = function () {
michael@0 160 return this.userShutdown.restart;
michael@0 161 }
michael@0 162
michael@0 163 events.startShutdown = function (obj) {
michael@0 164 events.fireEvent('shutdown', obj);
michael@0 165
michael@0 166 if (obj["user"]) {
michael@0 167 events.toggleUserShutdown(obj);
michael@0 168 } else {
michael@0 169 shutdownApplication(obj.flags);
michael@0 170 }
michael@0 171 }
michael@0 172
michael@0 173 events.setTest = function (test) {
michael@0 174 test.__start__ = Date.now();
michael@0 175 test.__passes__ = [];
michael@0 176 test.__fails__ = [];
michael@0 177
michael@0 178 events.currentTest = test;
michael@0 179
michael@0 180 var obj = {'filename': events.currentModule.__file__,
michael@0 181 'name': test.__name__}
michael@0 182 events.fireEvent('setTest', obj);
michael@0 183 }
michael@0 184
michael@0 185 events.endTest = function (test) {
michael@0 186 // use the current test unless specified
michael@0 187 if (test === undefined) {
michael@0 188 test = events.currentTest;
michael@0 189 }
michael@0 190
michael@0 191 // If no test is set it has already been reported. Beside that we don't want
michael@0 192 // to report it a second time.
michael@0 193 if (!test || test.status === 'done')
michael@0 194 return;
michael@0 195
michael@0 196 // report the end of a test
michael@0 197 test.__end__ = Date.now();
michael@0 198 test.status = 'done';
michael@0 199
michael@0 200 var obj = {'filename': events.currentModule.__file__,
michael@0 201 'passed': test.__passes__.length,
michael@0 202 'failed': test.__fails__.length,
michael@0 203 'passes': test.__passes__,
michael@0 204 'fails' : test.__fails__,
michael@0 205 'name' : test.__name__,
michael@0 206 'time_start': test.__start__,
michael@0 207 'time_end': test.__end__}
michael@0 208
michael@0 209 if (test.skipped) {
michael@0 210 obj['skipped'] = true;
michael@0 211 obj.skipped_reason = test.skipped_reason;
michael@0 212 }
michael@0 213
michael@0 214 if (test.meta) {
michael@0 215 obj.meta = test.meta;
michael@0 216 }
michael@0 217
michael@0 218 // Report the test result only if the test is a true test or if it is failing
michael@0 219 if (withs.startsWith(test.__name__, "test") || test.__fails__.length > 0) {
michael@0 220 events.fireEvent('endTest', obj);
michael@0 221 }
michael@0 222 }
michael@0 223
michael@0 224 events.setModule = function (aModule) {
michael@0 225 aModule.__start__ = Date.now();
michael@0 226 aModule.__status__ = 'running';
michael@0 227
michael@0 228 var result = stateChangeBase(null,
michael@0 229 [function (aModule) {return (aModule.__file__ != undefined)}],
michael@0 230 'currentModule', 'setModule', aModule);
michael@0 231
michael@0 232 return result;
michael@0 233 }
michael@0 234
michael@0 235 events.endModule = function (aModule) {
michael@0 236 // It should only reported once, so check if it already has been done
michael@0 237 if (aModule.__status__ === 'done')
michael@0 238 return;
michael@0 239
michael@0 240 aModule.__end__ = Date.now();
michael@0 241 aModule.__status__ = 'done';
michael@0 242
michael@0 243 var obj = {
michael@0 244 'filename': aModule.__file__,
michael@0 245 'time_start': aModule.__start__,
michael@0 246 'time_end': aModule.__end__
michael@0 247 }
michael@0 248
michael@0 249 events.fireEvent('endModule', obj);
michael@0 250 }
michael@0 251
michael@0 252 events.pass = function (obj) {
michael@0 253 // a low level event, such as a keystroke, succeeds
michael@0 254 if (events.currentTest) {
michael@0 255 events.currentTest.__passes__.push(obj);
michael@0 256 }
michael@0 257
michael@0 258 for each (var timer in timers) {
michael@0 259 timer.actions.push(
michael@0 260 {"currentTest": events.currentModule.__file__ + "::" + events.currentTest.__name__,
michael@0 261 "obj": obj,
michael@0 262 "result": "pass"}
michael@0 263 );
michael@0 264 }
michael@0 265
michael@0 266 events.fireEvent('pass', obj);
michael@0 267 }
michael@0 268
michael@0 269 events.fail = function (obj) {
michael@0 270 var error = obj.exception;
michael@0 271
michael@0 272 if (error) {
michael@0 273 // Error objects aren't enumerable https://bugzilla.mozilla.org/show_bug.cgi?id=637207
michael@0 274 obj.exception = {
michael@0 275 name: error.name,
michael@0 276 message: error.message,
michael@0 277 lineNumber: error.lineNumber,
michael@0 278 fileName: error.fileName,
michael@0 279 stack: error.stack
michael@0 280 };
michael@0 281 }
michael@0 282
michael@0 283 // a low level event, such as a keystroke, fails
michael@0 284 if (events.currentTest) {
michael@0 285 events.currentTest.__fails__.push(obj);
michael@0 286 }
michael@0 287
michael@0 288 for each (var time in timers) {
michael@0 289 timer.actions.push(
michael@0 290 {"currentTest": events.currentModule.__file__ + "::" + events.currentTest.__name__,
michael@0 291 "obj": obj,
michael@0 292 "result": "fail"}
michael@0 293 );
michael@0 294 }
michael@0 295
michael@0 296 events.fireEvent('fail', obj);
michael@0 297 }
michael@0 298
michael@0 299 events.skip = function (reason) {
michael@0 300 // this is used to report skips associated with setupModule and nothing else
michael@0 301 events.currentTest.skipped = true;
michael@0 302 events.currentTest.skipped_reason = reason;
michael@0 303
michael@0 304 for (var timer of timers) {
michael@0 305 timer.actions.push(
michael@0 306 {"currentTest": events.currentModule.__file__ + "::" + events.currentTest.__name__,
michael@0 307 "obj": reason,
michael@0 308 "result": "skip"}
michael@0 309 );
michael@0 310 }
michael@0 311
michael@0 312 events.fireEvent('skip', reason);
michael@0 313 }
michael@0 314
michael@0 315 events.fireEvent = function (name, obj) {
michael@0 316 if (events.appQuit) {
michael@0 317 // dump('* Event discarded: ' + name + ' ' + JSON.stringify(obj) + '\n');
michael@0 318 return;
michael@0 319 }
michael@0 320
michael@0 321 if (this.listeners[name]) {
michael@0 322 for (var i in this.listeners[name]) {
michael@0 323 this.listeners[name][i](obj);
michael@0 324 }
michael@0 325 }
michael@0 326
michael@0 327 for each(var listener in this.globalListeners) {
michael@0 328 listener(name, obj);
michael@0 329 }
michael@0 330 }
michael@0 331
michael@0 332 events.addListener = function (name, listener) {
michael@0 333 if (this.listeners[name]) {
michael@0 334 this.listeners[name].push(listener);
michael@0 335 } else if (name == '') {
michael@0 336 this.globalListeners.push(listener)
michael@0 337 } else {
michael@0 338 this.listeners[name] = [listener];
michael@0 339 }
michael@0 340 }
michael@0 341
michael@0 342 events.removeListener = function (listener) {
michael@0 343 for (var listenerIndex in this.listeners) {
michael@0 344 var e = this.listeners[listenerIndex];
michael@0 345
michael@0 346 for (var i in e){
michael@0 347 if (e[i] == listener) {
michael@0 348 this.listeners[listenerIndex] = arrays.remove(e, i);
michael@0 349 }
michael@0 350 }
michael@0 351 }
michael@0 352
michael@0 353 for (var i in this.globalListeners) {
michael@0 354 if (this.globalListeners[i] == listener) {
michael@0 355 this.globalListeners = arrays.remove(this.globalListeners, i);
michael@0 356 }
michael@0 357 }
michael@0 358 }
michael@0 359
michael@0 360 events.persist = function () {
michael@0 361 try {
michael@0 362 events.fireEvent('persist', persisted);
michael@0 363 } catch (e) {
michael@0 364 events.fireEvent('error', "persist serialization failed.")
michael@0 365 }
michael@0 366 }
michael@0 367
michael@0 368 events.firePythonCallback = function (obj) {
michael@0 369 obj['test'] = events.currentModule.__file__;
michael@0 370 events.fireEvent('firePythonCallback', obj);
michael@0 371 }
michael@0 372
michael@0 373 events.screenshot = function (obj) {
michael@0 374 // Find the name of the test function
michael@0 375 for (var attr in events.currentModule) {
michael@0 376 if (events.currentModule[attr] == events.currentTest) {
michael@0 377 var testName = attr;
michael@0 378 break;
michael@0 379 }
michael@0 380 }
michael@0 381
michael@0 382 obj['test_file'] = events.currentModule.__file__;
michael@0 383 obj['test_name'] = testName;
michael@0 384 events.fireEvent('screenshot', obj);
michael@0 385 }
michael@0 386
michael@0 387 var log = function (obj) {
michael@0 388 events.fireEvent('log', obj);
michael@0 389 }
michael@0 390
michael@0 391 // Register the listeners
michael@0 392 broker.addObject({'endTest': events.endTest,
michael@0 393 'fail': events.fail,
michael@0 394 'firePythonCallback': events.firePythonCallback,
michael@0 395 'log': log,
michael@0 396 'pass': events.pass,
michael@0 397 'persist': events.persist,
michael@0 398 'screenshot': events.screenshot,
michael@0 399 'shutdown': events.startShutdown,
michael@0 400 });
michael@0 401
michael@0 402 try {
michael@0 403 Cu.import('resource://jsbridge/modules/Events.jsm');
michael@0 404
michael@0 405 events.addListener('', function (name, obj) {
michael@0 406 Events.fireEvent('mozmill.' + name, obj);
michael@0 407 });
michael@0 408 } catch (e) {
michael@0 409 Services.console.logStringMessage("Event module of JSBridge not available.");
michael@0 410 }
michael@0 411
michael@0 412
michael@0 413 /**
michael@0 414 * Observer for notifications when the application is going to shutdown
michael@0 415 */
michael@0 416 function AppQuitObserver() {
michael@0 417 this.runner = null;
michael@0 418
michael@0 419 Services.obs.addObserver(this, "quit-application-requested", false);
michael@0 420 }
michael@0 421
michael@0 422 AppQuitObserver.prototype = {
michael@0 423 observe: function (aSubject, aTopic, aData) {
michael@0 424 switch (aTopic) {
michael@0 425 case "quit-application-requested":
michael@0 426 Services.obs.removeObserver(this, "quit-application-requested");
michael@0 427
michael@0 428 // If we observe a quit notification make sure to send the
michael@0 429 // results of the current test. In those cases we don't reach
michael@0 430 // the equivalent code in runTestModule()
michael@0 431 events.pass({'message': 'AppQuitObserver: ' + JSON.stringify(aData),
michael@0 432 'userShutdown': events.userShutdown});
michael@0 433
michael@0 434 if (this.runner) {
michael@0 435 this.runner.end();
michael@0 436 }
michael@0 437
michael@0 438 if (httpd) {
michael@0 439 httpd.stop();
michael@0 440 }
michael@0 441
michael@0 442 events.appQuit = true;
michael@0 443
michael@0 444 break;
michael@0 445 }
michael@0 446 }
michael@0 447 }
michael@0 448
michael@0 449 var appQuitObserver = new AppQuitObserver();
michael@0 450
michael@0 451 /**
michael@0 452 * The collector handles HTTPd.js and initilizing the module
michael@0 453 */
michael@0 454 function Collector() {
michael@0 455 this.test_modules_by_filename = {};
michael@0 456 this.testing = [];
michael@0 457 }
michael@0 458
michael@0 459 Collector.prototype.addHttpResource = function (aDirectory, aPath) {
michael@0 460 var fp = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
michael@0 461 fp.initWithPath(os.abspath(aDirectory, this.current_file));
michael@0 462
michael@0 463 return httpd.addHttpResource(fp, aPath);
michael@0 464 }
michael@0 465
michael@0 466 Collector.prototype.initTestModule = function (filename, testname) {
michael@0 467 var test_module = this.loadFile(filename, this);
michael@0 468 var has_restarted = !(testname == null);
michael@0 469 test_module.__tests__ = [];
michael@0 470
michael@0 471 for (var i in test_module) {
michael@0 472 if (typeof(test_module[i]) == "function") {
michael@0 473 test_module[i].__name__ = i;
michael@0 474
michael@0 475 // Only run setupModule if we are a single test OR if we are the first
michael@0 476 // test of a restart chain (don't run it prior to members in a restart
michael@0 477 // chain)
michael@0 478 if (i == "setupModule" && !has_restarted) {
michael@0 479 test_module.__setupModule__ = test_module[i];
michael@0 480 } else if (i == "setupTest") {
michael@0 481 test_module.__setupTest__ = test_module[i];
michael@0 482 } else if (i == "teardownTest") {
michael@0 483 test_module.__teardownTest__ = test_module[i];
michael@0 484 } else if (i == "teardownModule") {
michael@0 485 test_module.__teardownModule__ = test_module[i];
michael@0 486 } else if (withs.startsWith(i, "test")) {
michael@0 487 if (testname && (i != testname)) {
michael@0 488 continue;
michael@0 489 }
michael@0 490
michael@0 491 testname = null;
michael@0 492 test_module.__tests__.push(test_module[i]);
michael@0 493 }
michael@0 494 }
michael@0 495 }
michael@0 496
michael@0 497 test_module.collector = this;
michael@0 498 test_module.status = 'loaded';
michael@0 499
michael@0 500 this.test_modules_by_filename[filename] = test_module;
michael@0 501
michael@0 502 return test_module;
michael@0 503 }
michael@0 504
michael@0 505 Collector.prototype.loadFile = function (path, collector) {
michael@0 506 var moduleLoader = new securableModule.Loader({
michael@0 507 rootPaths: ["resource://mozmill/modules/"],
michael@0 508 defaultPrincipal: "system",
michael@0 509 globals : { Cc: Cc,
michael@0 510 Ci: Ci,
michael@0 511 Cu: Cu,
michael@0 512 Cr: Components.results}
michael@0 513 });
michael@0 514
michael@0 515 // load a test module from a file and add some candy
michael@0 516 var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
michael@0 517 file.initWithPath(path);
michael@0 518 var uri = Services.io.newFileURI(file).spec;
michael@0 519
michael@0 520 this.loadTestResources();
michael@0 521
michael@0 522 var systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
michael@0 523 var module = new Components.utils.Sandbox(systemPrincipal);
michael@0 524 module.assert = new assertions.Assert();
michael@0 525 module.Cc = Cc;
michael@0 526 module.Ci = Ci;
michael@0 527 module.Cr = Components.results;
michael@0 528 module.Cu = Cu;
michael@0 529 module.collector = collector;
michael@0 530 module.driver = moduleLoader.require("driver");
michael@0 531 module.elementslib = mozelement;
michael@0 532 module.errors = errors;
michael@0 533 module.expect = new assertions.Expect();
michael@0 534 module.findElement = mozelement;
michael@0 535 module.log = log;
michael@0 536 module.mozmill = mozmill;
michael@0 537 module.persisted = persisted;
michael@0 538
michael@0 539 module.require = function (mod) {
michael@0 540 var loader = new securableModule.Loader({
michael@0 541 rootPaths: [Services.io.newFileURI(file.parent).spec,
michael@0 542 "resource://mozmill/modules/"],
michael@0 543 defaultPrincipal: "system",
michael@0 544 globals : { mozmill: mozmill,
michael@0 545 elementslib: mozelement, // This a quick hack to maintain backwards compatibility with 1.5.x
michael@0 546 findElement: mozelement,
michael@0 547 persisted: persisted,
michael@0 548 Cc: Cc,
michael@0 549 Ci: Ci,
michael@0 550 Cu: Cu,
michael@0 551 log: log }
michael@0 552 });
michael@0 553
michael@0 554 if (modules != undefined) {
michael@0 555 loader.modules = modules;
michael@0 556 }
michael@0 557
michael@0 558 var retval = loader.require(mod);
michael@0 559 modules = loader.modules;
michael@0 560
michael@0 561 return retval;
michael@0 562 }
michael@0 563
michael@0 564 if (collector != undefined) {
michael@0 565 collector.current_file = file;
michael@0 566 collector.current_path = path;
michael@0 567 }
michael@0 568
michael@0 569 try {
michael@0 570 Services.scriptloader.loadSubScript(uri, module, "UTF-8");
michael@0 571 } catch (e) {
michael@0 572 var obj = {
michael@0 573 'filename': path,
michael@0 574 'passed': 0,
michael@0 575 'failed': 1,
michael@0 576 'passes': [],
michael@0 577 'fails' : [{'exception' : {
michael@0 578 message: e.message,
michael@0 579 filename: e.filename,
michael@0 580 lineNumber: e.lineNumber}}],
michael@0 581 'name' :'<TOP_LEVEL>'
michael@0 582 };
michael@0 583
michael@0 584 events.fail({'exception': e});
michael@0 585 events.fireEvent('endTest', obj);
michael@0 586 }
michael@0 587
michael@0 588 module.__file__ = path;
michael@0 589 module.__uri__ = uri;
michael@0 590
michael@0 591 return module;
michael@0 592 }
michael@0 593
michael@0 594 Collector.prototype.loadTestResources = function () {
michael@0 595 // load resources we want in our tests
michael@0 596 if (mozmill === undefined) {
michael@0 597 mozmill = {};
michael@0 598 Cu.import("resource://mozmill/driver/mozmill.js", mozmill);
michael@0 599 }
michael@0 600 if (mozelement === undefined) {
michael@0 601 mozelement = {};
michael@0 602 Cu.import("resource://mozmill/driver/mozelement.js", mozelement);
michael@0 603 }
michael@0 604 }
michael@0 605
michael@0 606
michael@0 607 /**
michael@0 608 *
michael@0 609 */
michael@0 610 function Httpd(aPort) {
michael@0 611 this.http_port = aPort;
michael@0 612
michael@0 613 while (true) {
michael@0 614 try {
michael@0 615 var srv = new HttpServer();
michael@0 616 srv.registerContentType("sjs", "sjs");
michael@0 617 srv.identity.setPrimary("http", "localhost", this.http_port);
michael@0 618 srv.start(this.http_port);
michael@0 619
michael@0 620 this._httpd = srv;
michael@0 621 break;
michael@0 622 }
michael@0 623 catch (e) {
michael@0 624 // Failure most likely due to port conflict
michael@0 625 this.http_port++;
michael@0 626 }
michael@0 627 }
michael@0 628 }
michael@0 629
michael@0 630 Httpd.prototype.addHttpResource = function (aDir, aPath) {
michael@0 631 var path = aPath ? ("/" + aPath + "/") : "/";
michael@0 632
michael@0 633 try {
michael@0 634 this._httpd.registerDirectory(path, aDir);
michael@0 635 return 'http://localhost:' + this.http_port + path;
michael@0 636 }
michael@0 637 catch (e) {
michael@0 638 throw Error("Failure to register directory: " + aDir.path);
michael@0 639 }
michael@0 640 };
michael@0 641
michael@0 642 Httpd.prototype.stop = function () {
michael@0 643 if (!this._httpd) {
michael@0 644 return;
michael@0 645 }
michael@0 646
michael@0 647 var shutdown = false;
michael@0 648 this._httpd.stop(function () { shutdown = true; });
michael@0 649
michael@0 650 assert.waitFor(function () {
michael@0 651 return shutdown;
michael@0 652 }, "Local HTTP server has been stopped", TIMEOUT_SHUTDOWN_HTTPD);
michael@0 653
michael@0 654 this._httpd = null;
michael@0 655 };
michael@0 656
michael@0 657 function startHTTPd() {
michael@0 658 if (!httpd) {
michael@0 659 // Ensure that we start the HTTP server only once during a session
michael@0 660 httpd = new Httpd(43336);
michael@0 661 }
michael@0 662 }
michael@0 663
michael@0 664
michael@0 665 function Runner() {
michael@0 666 this.collector = new Collector();
michael@0 667 this.ended = false;
michael@0 668
michael@0 669 var m = {}; Cu.import('resource://mozmill/driver/mozmill.js', m);
michael@0 670 this.platform = m.platform;
michael@0 671
michael@0 672 events.fireEvent('startRunner', true);
michael@0 673 }
michael@0 674
michael@0 675 Runner.prototype.end = function () {
michael@0 676 if (!this.ended) {
michael@0 677 this.ended = true;
michael@0 678
michael@0 679 appQuitObserver.runner = null;
michael@0 680
michael@0 681 events.endTest();
michael@0 682 events.endModule(events.currentModule);
michael@0 683 events.fireEvent('endRunner', true);
michael@0 684 events.persist();
michael@0 685 }
michael@0 686 };
michael@0 687
michael@0 688 Runner.prototype.runTestFile = function (filename, name) {
michael@0 689 var module = this.collector.initTestModule(filename, name);
michael@0 690 this.runTestModule(module);
michael@0 691 };
michael@0 692
michael@0 693 Runner.prototype.runTestModule = function (module) {
michael@0 694 appQuitObserver.runner = this;
michael@0 695 events.setModule(module);
michael@0 696
michael@0 697 // If setupModule passes, run all the tests. Otherwise mark them as skipped.
michael@0 698 if (this.execFunction(module.__setupModule__, module)) {
michael@0 699 for (var test of module.__tests__) {
michael@0 700 if (events.shutdownRequested) {
michael@0 701 break;
michael@0 702 }
michael@0 703
michael@0 704 // If setupTest passes, run the test. Otherwise mark it as skipped.
michael@0 705 if (this.execFunction(module.__setupTest__, module)) {
michael@0 706 this.execFunction(test);
michael@0 707 } else {
michael@0 708 this.skipFunction(test, module.__setupTest__.__name__ + " failed");
michael@0 709 }
michael@0 710
michael@0 711 this.execFunction(module.__teardownTest__, module);
michael@0 712 }
michael@0 713
michael@0 714 } else {
michael@0 715 for (var test of module.__tests__) {
michael@0 716 this.skipFunction(test, module.__setupModule__.__name__ + " failed");
michael@0 717 }
michael@0 718 }
michael@0 719
michael@0 720 this.execFunction(module.__teardownModule__, module);
michael@0 721 events.endModule(module);
michael@0 722 };
michael@0 723
michael@0 724 Runner.prototype.execFunction = function (func, arg) {
michael@0 725 if (typeof func !== "function" || events.shutdownRequested) {
michael@0 726 return true;
michael@0 727 }
michael@0 728
michael@0 729 var isTest = withs.startsWith(func.__name__, "test");
michael@0 730
michael@0 731 events.setState(isTest ? "test" : func.__name);
michael@0 732 events.setTest(func);
michael@0 733
michael@0 734 // skip excluded platforms
michael@0 735 if (func.EXCLUDED_PLATFORMS != undefined) {
michael@0 736 if (arrays.inArray(func.EXCLUDED_PLATFORMS, this.platform)) {
michael@0 737 events.skip("Platform exclusion");
michael@0 738 events.endTest(func);
michael@0 739 return false;
michael@0 740 }
michael@0 741 }
michael@0 742
michael@0 743 // skip function if requested
michael@0 744 if (func.__force_skip__ != undefined) {
michael@0 745 events.skip(func.__force_skip__);
michael@0 746 events.endTest(func);
michael@0 747 return false;
michael@0 748 }
michael@0 749
michael@0 750 // execute the test function
michael@0 751 try {
michael@0 752 func(arg);
michael@0 753 } catch (e) {
michael@0 754 if (e instanceof errors.ApplicationQuitError) {
michael@0 755 events.shutdownRequested = true;
michael@0 756 } else {
michael@0 757 events.fail({'exception': e, 'test': func})
michael@0 758 }
michael@0 759 }
michael@0 760
michael@0 761 // If a user shutdown has been requested and the function already returned,
michael@0 762 // we can assume that a shutdown will not happen anymore. We should force a
michael@0 763 // shutdown then, to prevent the next test from being executed.
michael@0 764 if (events.isUserShutdown()) {
michael@0 765 events.shutdownRequested = true;
michael@0 766 events.toggleUserShutdown(events.userShutdown);
michael@0 767 }
michael@0 768
michael@0 769 events.endTest(func);
michael@0 770 return events.currentTest.__fails__.length == 0;
michael@0 771 };
michael@0 772
michael@0 773 function runTestFile(filename, name) {
michael@0 774 var runner = new Runner();
michael@0 775 runner.runTestFile(filename, name);
michael@0 776 runner.end();
michael@0 777
michael@0 778 return true;
michael@0 779 }
michael@0 780
michael@0 781 Runner.prototype.skipFunction = function (func, message) {
michael@0 782 events.setTest(func);
michael@0 783 events.skip(message);
michael@0 784 events.endTest(func);
michael@0 785 };

mercurial