2012-03-22 08:19:57 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
/*
|
|
|
|
* The Marionette object, passed to the script context.
|
|
|
|
*/
|
|
|
|
|
2013-05-14 13:54:07 -07:00
|
|
|
this.Marionette = function Marionette(scope, window, context, logObj, timeout, testName) {
|
2012-05-18 13:30:43 -07:00
|
|
|
this.scope = scope;
|
2012-03-22 08:19:57 -07:00
|
|
|
this.window = window;
|
|
|
|
this.tests = [];
|
|
|
|
this.logObj = logObj;
|
|
|
|
this.context = context;
|
2012-10-31 23:51:30 -07:00
|
|
|
this.timeout = timeout;
|
2012-10-31 15:36:57 -07:00
|
|
|
this.testName = testName;
|
2012-09-14 14:34:38 -07:00
|
|
|
this.TEST_UNEXPECTED_FAIL = "TEST-UNEXPECTED-FAIL";
|
|
|
|
this.TEST_PASS = "TEST-PASS";
|
|
|
|
this.TEST_KNOWN_FAIL = "TEST-KNOWN-FAIL";
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Marionette.prototype = {
|
2012-05-18 13:30:43 -07:00
|
|
|
exports: ['ok', 'is', 'isnot', 'log', 'getLogs', 'generate_results', 'waitFor',
|
2013-04-29 09:38:54 -07:00
|
|
|
'runEmulatorCmd', 'TEST_PASS', 'TEST_KNOWN_FAIL',
|
|
|
|
'TEST_UNEXPECTED_FAIL'],
|
2012-05-09 12:05:39 -07:00
|
|
|
|
2012-09-14 14:34:38 -07:00
|
|
|
ok: function Marionette__ok(condition, name, passString, failString, diag) {
|
|
|
|
if (typeof(diag) == "undefined") {
|
2013-01-03 10:44:17 -08:00
|
|
|
diag = this.repr(condition) + " was " + !!condition + ", expected true";
|
2012-09-14 14:34:38 -07:00
|
|
|
}
|
2012-03-22 08:19:57 -07:00
|
|
|
let test = {'result': !!condition, 'name': name, 'diag': diag};
|
2012-09-14 14:34:38 -07:00
|
|
|
this.logResult(test,
|
|
|
|
typeof(passString) == "undefined" ? this.TEST_PASS : passString,
|
|
|
|
typeof(failString) == "undefined" ? this.TEST_UNEXPECTED_FAIL : failString);
|
2012-03-22 08:19:57 -07:00
|
|
|
this.tests.push(test);
|
|
|
|
},
|
|
|
|
|
2012-09-14 14:34:38 -07:00
|
|
|
is: function Marionette__is(a, b, name, passString, failString) {
|
2012-03-22 08:19:57 -07:00
|
|
|
let pass = (a == b);
|
|
|
|
let diag = pass ? this.repr(a) + " should equal " + this.repr(b)
|
|
|
|
: "got " + this.repr(a) + ", expected " + this.repr(b);
|
2012-09-14 14:34:38 -07:00
|
|
|
this.ok(pass, name, passString, failString, diag);
|
2012-03-22 08:19:57 -07:00
|
|
|
},
|
|
|
|
|
2012-09-14 14:34:38 -07:00
|
|
|
isnot: function Marionette__isnot (a, b, name, passString, failString) {
|
2012-03-22 08:19:57 -07:00
|
|
|
let pass = (a != b);
|
|
|
|
let diag = pass ? this.repr(a) + " should not equal " + this.repr(b)
|
|
|
|
: "didn't expect " + this.repr(a) + ", but got it";
|
2012-09-14 14:34:38 -07:00
|
|
|
this.ok(pass, name, passString, failString, diag);
|
2012-03-22 08:19:57 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
log: function Marionette__log(msg, level) {
|
2012-03-29 12:34:00 -07:00
|
|
|
dump("MARIONETTE LOG: " + (level ? level : "INFO") + ": " + msg + "\n");
|
2012-03-22 08:19:57 -07:00
|
|
|
if (this.logObj != null) {
|
|
|
|
this.logObj.log(msg, level);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getLogs: function Marionette__getLogs() {
|
|
|
|
if (this.logObj != null) {
|
|
|
|
this.logObj.getLogs();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
generate_results: function Marionette__generate_results() {
|
|
|
|
let passed = 0;
|
|
|
|
let failed = 0;
|
|
|
|
let failures = [];
|
|
|
|
for (let i in this.tests) {
|
|
|
|
if(this.tests[i].result) {
|
|
|
|
passed++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
failed++;
|
|
|
|
failures.push({'name': this.tests[i].name,
|
|
|
|
'diag': this.tests[i].diag});
|
|
|
|
}
|
|
|
|
}
|
2012-05-09 12:05:39 -07:00
|
|
|
// Reset state in case this object is reused for more tests.
|
|
|
|
this.tests = [];
|
2012-03-22 08:19:57 -07:00
|
|
|
return {"passed": passed, "failed": failed, "failures": failures};
|
|
|
|
},
|
|
|
|
|
|
|
|
logToFile: function Marionette__logToFile(file) {
|
|
|
|
//TODO
|
|
|
|
},
|
|
|
|
|
|
|
|
logResult: function Marionette__logResult(test, passString, failString) {
|
|
|
|
//TODO: dump to file
|
|
|
|
let resultString = test.result ? passString : failString;
|
|
|
|
let diagnostic = test.name + (test.diag ? " - " + test.diag : "");
|
2012-10-31 15:36:57 -07:00
|
|
|
let msg = resultString + " | " + this.testName + " | " + diagnostic;
|
2012-03-22 08:19:57 -07:00
|
|
|
dump("MARIONETTE TEST RESULT:" + msg + "\n");
|
|
|
|
},
|
|
|
|
|
|
|
|
repr: function Marionette__repr(o) {
|
|
|
|
if (typeof(o) == "undefined") {
|
|
|
|
return "undefined";
|
|
|
|
} else if (o === null) {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (typeof(o.__repr__) == 'function') {
|
|
|
|
return o.__repr__();
|
|
|
|
} else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) {
|
|
|
|
return o.repr();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (typeof(o.NAME) == 'string' && (
|
|
|
|
o.toString == Function.prototype.toString ||
|
|
|
|
o.toString == Object.prototype.toString
|
|
|
|
)) {
|
|
|
|
return o.NAME;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
let ostring;
|
|
|
|
try {
|
|
|
|
ostring = (o + "");
|
|
|
|
} catch (e) {
|
|
|
|
return "[" + typeof(o) + "]";
|
|
|
|
}
|
|
|
|
if (typeof(o) == "function") {
|
|
|
|
o = ostring.replace(/^\s+/, "");
|
|
|
|
let idx = o.indexOf("{");
|
|
|
|
if (idx != -1) {
|
|
|
|
o = o.substr(0, idx) + "{...}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ostring;
|
|
|
|
},
|
|
|
|
|
|
|
|
waitFor: function test_waitFor(callback, test, timeout) {
|
|
|
|
if (test()) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2012-10-31 23:51:30 -07:00
|
|
|
var now = Date.now();
|
|
|
|
var deadline = now + (typeof(timeout) == "undefined" ? this.timeout : timeout);
|
|
|
|
if (deadline <= now) {
|
2012-05-10 16:07:07 -07:00
|
|
|
dump("waitFor timeout: " + test.toString() + "\n");
|
|
|
|
// the script will timeout here, so no need to raise a separate
|
|
|
|
// timeout exception
|
|
|
|
return;
|
2012-03-22 08:19:57 -07:00
|
|
|
}
|
2012-10-31 23:51:30 -07:00
|
|
|
this.window.setTimeout(this.waitFor.bind(this), 100, callback, test, deadline - now);
|
2012-03-22 08:19:57 -07:00
|
|
|
},
|
2012-05-18 13:30:43 -07:00
|
|
|
|
|
|
|
runEmulatorCmd: function runEmulatorCmd(cmd, callback) {
|
|
|
|
this.scope.runEmulatorCmd(cmd, callback);
|
|
|
|
},
|
|
|
|
|
2012-03-22 08:19:57 -07:00
|
|
|
};
|
|
|
|
|