Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
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 file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | /* |
michael@0 | 5 | * The Marionette object, passed to the script context. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | this.Marionette = function Marionette(scope, window, context, logObj, timeout, |
michael@0 | 9 | heartbeatCallback, testName) { |
michael@0 | 10 | this.scope = scope; |
michael@0 | 11 | this.window = window; |
michael@0 | 12 | this.tests = []; |
michael@0 | 13 | this.logObj = logObj; |
michael@0 | 14 | this.context = context; |
michael@0 | 15 | this.timeout = timeout; |
michael@0 | 16 | this.heartbeatCallback = heartbeatCallback; |
michael@0 | 17 | this.testName = testName; |
michael@0 | 18 | this.TEST_UNEXPECTED_FAIL = "TEST-UNEXPECTED-FAIL"; |
michael@0 | 19 | this.TEST_PASS = "TEST-PASS"; |
michael@0 | 20 | this.TEST_KNOWN_FAIL = "TEST-KNOWN-FAIL"; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | Marionette.prototype = { |
michael@0 | 24 | exports: ['ok', 'is', 'isnot', 'log', 'getLogs', 'generate_results', 'waitFor', |
michael@0 | 25 | 'runEmulatorCmd', 'runEmulatorShell', 'TEST_PASS', 'TEST_KNOWN_FAIL', |
michael@0 | 26 | 'TEST_UNEXPECTED_FAIL'], |
michael@0 | 27 | |
michael@0 | 28 | ok: function Marionette__ok(condition, name, passString, failString, diag) { |
michael@0 | 29 | this.heartbeatCallback(); |
michael@0 | 30 | if (typeof(diag) == "undefined") { |
michael@0 | 31 | diag = this.repr(condition) + " was " + !!condition + ", expected true"; |
michael@0 | 32 | } |
michael@0 | 33 | let test = {'result': !!condition, 'name': name, 'diag': diag}; |
michael@0 | 34 | this.logResult(test, |
michael@0 | 35 | typeof(passString) == "undefined" ? this.TEST_PASS : passString, |
michael@0 | 36 | typeof(failString) == "undefined" ? this.TEST_UNEXPECTED_FAIL : failString); |
michael@0 | 37 | this.tests.push(test); |
michael@0 | 38 | }, |
michael@0 | 39 | |
michael@0 | 40 | is: function Marionette__is(a, b, name, passString, failString) { |
michael@0 | 41 | this.heartbeatCallback(); |
michael@0 | 42 | let pass = (a == b); |
michael@0 | 43 | let diag = pass ? this.repr(a) + " should equal " + this.repr(b) |
michael@0 | 44 | : "got " + this.repr(a) + ", expected " + this.repr(b); |
michael@0 | 45 | this.ok(pass, name, passString, failString, diag); |
michael@0 | 46 | }, |
michael@0 | 47 | |
michael@0 | 48 | isnot: function Marionette__isnot (a, b, name, passString, failString) { |
michael@0 | 49 | this.heartbeatCallback(); |
michael@0 | 50 | let pass = (a != b); |
michael@0 | 51 | let diag = pass ? this.repr(a) + " should not equal " + this.repr(b) |
michael@0 | 52 | : "didn't expect " + this.repr(a) + ", but got it"; |
michael@0 | 53 | this.ok(pass, name, passString, failString, diag); |
michael@0 | 54 | }, |
michael@0 | 55 | |
michael@0 | 56 | log: function Marionette__log(msg, level) { |
michael@0 | 57 | this.heartbeatCallback(); |
michael@0 | 58 | dump("MARIONETTE LOG: " + (level ? level : "INFO") + ": " + msg + "\n"); |
michael@0 | 59 | if (this.logObj != null) { |
michael@0 | 60 | this.logObj.log(msg, level); |
michael@0 | 61 | } |
michael@0 | 62 | }, |
michael@0 | 63 | |
michael@0 | 64 | getLogs: function Marionette__getLogs() { |
michael@0 | 65 | this.heartbeatCallback(); |
michael@0 | 66 | if (this.logObj != null) { |
michael@0 | 67 | this.logObj.getLogs(); |
michael@0 | 68 | } |
michael@0 | 69 | }, |
michael@0 | 70 | |
michael@0 | 71 | generate_results: function Marionette__generate_results() { |
michael@0 | 72 | this.heartbeatCallback(); |
michael@0 | 73 | let passed = 0; |
michael@0 | 74 | let failed = 0; |
michael@0 | 75 | let failures = []; |
michael@0 | 76 | for (let i in this.tests) { |
michael@0 | 77 | if(this.tests[i].result) { |
michael@0 | 78 | passed++; |
michael@0 | 79 | } |
michael@0 | 80 | else { |
michael@0 | 81 | failed++; |
michael@0 | 82 | failures.push({'name': this.tests[i].name, |
michael@0 | 83 | 'diag': this.tests[i].diag}); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | // Reset state in case this object is reused for more tests. |
michael@0 | 87 | this.tests = []; |
michael@0 | 88 | return {"passed": passed, "failed": failed, "failures": failures}; |
michael@0 | 89 | }, |
michael@0 | 90 | |
michael@0 | 91 | logToFile: function Marionette__logToFile(file) { |
michael@0 | 92 | this.heartbeatCallback(); |
michael@0 | 93 | //TODO |
michael@0 | 94 | }, |
michael@0 | 95 | |
michael@0 | 96 | logResult: function Marionette__logResult(test, passString, failString) { |
michael@0 | 97 | this.heartbeatCallback(); |
michael@0 | 98 | //TODO: dump to file |
michael@0 | 99 | let resultString = test.result ? passString : failString; |
michael@0 | 100 | let diagnostic = test.name + (test.diag ? " - " + test.diag : ""); |
michael@0 | 101 | let msg = resultString + " | " + this.testName + " | " + diagnostic; |
michael@0 | 102 | dump("MARIONETTE TEST RESULT:" + msg + "\n"); |
michael@0 | 103 | }, |
michael@0 | 104 | |
michael@0 | 105 | repr: function Marionette__repr(o) { |
michael@0 | 106 | if (typeof(o) == "undefined") { |
michael@0 | 107 | return "undefined"; |
michael@0 | 108 | } else if (o === null) { |
michael@0 | 109 | return "null"; |
michael@0 | 110 | } |
michael@0 | 111 | try { |
michael@0 | 112 | if (typeof(o.__repr__) == 'function') { |
michael@0 | 113 | return o.__repr__(); |
michael@0 | 114 | } else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) { |
michael@0 | 115 | return o.repr(); |
michael@0 | 116 | } |
michael@0 | 117 | } catch (e) { |
michael@0 | 118 | } |
michael@0 | 119 | try { |
michael@0 | 120 | if (typeof(o.NAME) == 'string' && ( |
michael@0 | 121 | o.toString == Function.prototype.toString || |
michael@0 | 122 | o.toString == Object.prototype.toString |
michael@0 | 123 | )) { |
michael@0 | 124 | return o.NAME; |
michael@0 | 125 | } |
michael@0 | 126 | } catch (e) { |
michael@0 | 127 | } |
michael@0 | 128 | let ostring; |
michael@0 | 129 | try { |
michael@0 | 130 | ostring = (o + ""); |
michael@0 | 131 | } catch (e) { |
michael@0 | 132 | return "[" + typeof(o) + "]"; |
michael@0 | 133 | } |
michael@0 | 134 | if (typeof(o) == "function") { |
michael@0 | 135 | o = ostring.replace(/^\s+/, ""); |
michael@0 | 136 | let idx = o.indexOf("{"); |
michael@0 | 137 | if (idx != -1) { |
michael@0 | 138 | o = o.substr(0, idx) + "{...}"; |
michael@0 | 139 | } |
michael@0 | 140 | } |
michael@0 | 141 | return ostring; |
michael@0 | 142 | }, |
michael@0 | 143 | |
michael@0 | 144 | waitFor: function test_waitFor(callback, test, timeout) { |
michael@0 | 145 | this.heartbeatCallback(); |
michael@0 | 146 | if (test()) { |
michael@0 | 147 | callback(); |
michael@0 | 148 | return; |
michael@0 | 149 | } |
michael@0 | 150 | var now = Date.now(); |
michael@0 | 151 | var deadline = now + (typeof(timeout) == "undefined" ? this.timeout : timeout); |
michael@0 | 152 | if (deadline <= now) { |
michael@0 | 153 | dump("waitFor timeout: " + test.toString() + "\n"); |
michael@0 | 154 | // the script will timeout here, so no need to raise a separate |
michael@0 | 155 | // timeout exception |
michael@0 | 156 | return; |
michael@0 | 157 | } |
michael@0 | 158 | this.window.setTimeout(this.waitFor.bind(this), 100, callback, test, deadline - now); |
michael@0 | 159 | }, |
michael@0 | 160 | |
michael@0 | 161 | runEmulatorCmd: function runEmulatorCmd(cmd, callback) { |
michael@0 | 162 | this.heartbeatCallback(); |
michael@0 | 163 | this.scope.runEmulatorCmd(cmd, callback); |
michael@0 | 164 | }, |
michael@0 | 165 | |
michael@0 | 166 | runEmulatorShell: function runEmulatorShell(args, callback) { |
michael@0 | 167 | this.heartbeatCallback(); |
michael@0 | 168 | this.scope.runEmulatorShell(args, callback); |
michael@0 | 169 | }, |
michael@0 | 170 | |
michael@0 | 171 | }; |
michael@0 | 172 |