testing/marionette/marionette-simpletest.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/marionette/marionette-simpletest.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +/*
     1.8 + * The Marionette object, passed to the script context.
     1.9 + */
    1.10 +
    1.11 +this.Marionette = function Marionette(scope, window, context, logObj, timeout,
    1.12 +                                      heartbeatCallback, testName) {
    1.13 +  this.scope = scope;
    1.14 +  this.window = window;
    1.15 +  this.tests = [];
    1.16 +  this.logObj = logObj;
    1.17 +  this.context = context;
    1.18 +  this.timeout = timeout;
    1.19 +  this.heartbeatCallback = heartbeatCallback;
    1.20 +  this.testName = testName;
    1.21 +  this.TEST_UNEXPECTED_FAIL = "TEST-UNEXPECTED-FAIL";
    1.22 +  this.TEST_PASS = "TEST-PASS";
    1.23 +  this.TEST_KNOWN_FAIL = "TEST-KNOWN-FAIL";
    1.24 +}
    1.25 +
    1.26 +Marionette.prototype = {
    1.27 +  exports: ['ok', 'is', 'isnot', 'log', 'getLogs', 'generate_results', 'waitFor',
    1.28 +            'runEmulatorCmd', 'runEmulatorShell', 'TEST_PASS', 'TEST_KNOWN_FAIL',
    1.29 +            'TEST_UNEXPECTED_FAIL'],
    1.30 +
    1.31 +  ok: function Marionette__ok(condition, name, passString, failString, diag) {
    1.32 +    this.heartbeatCallback();
    1.33 +    if (typeof(diag) == "undefined") {
    1.34 +      diag = this.repr(condition) + " was " + !!condition + ", expected true";
    1.35 +    }
    1.36 +    let test = {'result': !!condition, 'name': name, 'diag': diag};
    1.37 +    this.logResult(test,
    1.38 +                   typeof(passString) == "undefined" ? this.TEST_PASS : passString,
    1.39 +                   typeof(failString) == "undefined" ? this.TEST_UNEXPECTED_FAIL : failString);
    1.40 +    this.tests.push(test);
    1.41 +  },
    1.42 +
    1.43 +  is: function Marionette__is(a, b, name, passString, failString) {
    1.44 +    this.heartbeatCallback();
    1.45 +    let pass = (a == b);
    1.46 +    let diag = pass ? this.repr(a) + " should equal " + this.repr(b)
    1.47 +                    : "got " + this.repr(a) + ", expected " + this.repr(b);
    1.48 +    this.ok(pass, name, passString, failString, diag);
    1.49 +  },
    1.50 +
    1.51 +  isnot: function Marionette__isnot (a, b, name, passString, failString) {
    1.52 +    this.heartbeatCallback();
    1.53 +    let pass = (a != b);
    1.54 +    let diag = pass ? this.repr(a) + " should not equal " + this.repr(b)
    1.55 +                    : "didn't expect " + this.repr(a) + ", but got it";
    1.56 +    this.ok(pass, name, passString, failString, diag);
    1.57 +  },
    1.58 +
    1.59 +  log: function Marionette__log(msg, level) {
    1.60 +    this.heartbeatCallback();
    1.61 +    dump("MARIONETTE LOG: " + (level ? level : "INFO") + ": " + msg + "\n");
    1.62 +    if (this.logObj != null) {
    1.63 +      this.logObj.log(msg, level);
    1.64 +    }
    1.65 +  },
    1.66 +
    1.67 +  getLogs: function Marionette__getLogs() {
    1.68 +    this.heartbeatCallback();
    1.69 +    if (this.logObj != null) {
    1.70 +      this.logObj.getLogs();
    1.71 +    }
    1.72 +  },
    1.73 +
    1.74 +  generate_results: function Marionette__generate_results() {
    1.75 +    this.heartbeatCallback();
    1.76 +    let passed = 0;
    1.77 +    let failed = 0;
    1.78 +    let failures = [];
    1.79 +    for (let i in this.tests) {
    1.80 +      if(this.tests[i].result) {
    1.81 +        passed++;
    1.82 +      }
    1.83 +      else {
    1.84 +        failed++;
    1.85 +        failures.push({'name': this.tests[i].name,
    1.86 +                       'diag': this.tests[i].diag});
    1.87 +      }
    1.88 +    }
    1.89 +    // Reset state in case this object is reused for more tests.
    1.90 +    this.tests = [];
    1.91 +    return {"passed": passed, "failed": failed, "failures": failures};
    1.92 +  },
    1.93 +
    1.94 +  logToFile: function Marionette__logToFile(file) {
    1.95 +    this.heartbeatCallback();
    1.96 +    //TODO
    1.97 +  },
    1.98 +
    1.99 +  logResult: function Marionette__logResult(test, passString, failString) {
   1.100 +    this.heartbeatCallback();
   1.101 +    //TODO: dump to file
   1.102 +    let resultString = test.result ? passString : failString;
   1.103 +    let diagnostic = test.name + (test.diag ? " - " + test.diag : "");
   1.104 +    let msg = resultString + " | " + this.testName + " | " + diagnostic;
   1.105 +    dump("MARIONETTE TEST RESULT:" + msg + "\n");
   1.106 +  },
   1.107 +
   1.108 +  repr: function Marionette__repr(o) {
   1.109 +      if (typeof(o) == "undefined") {
   1.110 +          return "undefined";
   1.111 +      } else if (o === null) {
   1.112 +          return "null";
   1.113 +      }
   1.114 +      try {
   1.115 +          if (typeof(o.__repr__) == 'function') {
   1.116 +              return o.__repr__();
   1.117 +          } else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) {
   1.118 +              return o.repr();
   1.119 +          }
   1.120 +     } catch (e) {
   1.121 +     }
   1.122 +     try {
   1.123 +          if (typeof(o.NAME) == 'string' && (
   1.124 +                  o.toString == Function.prototype.toString ||
   1.125 +                  o.toString == Object.prototype.toString
   1.126 +              )) {
   1.127 +              return o.NAME;
   1.128 +          }
   1.129 +      } catch (e) {
   1.130 +      }
   1.131 +      let ostring;
   1.132 +      try {
   1.133 +          ostring = (o + "");
   1.134 +      } catch (e) {
   1.135 +          return "[" + typeof(o) + "]";
   1.136 +      }
   1.137 +      if (typeof(o) == "function") {
   1.138 +          o = ostring.replace(/^\s+/, "");
   1.139 +          let idx = o.indexOf("{");
   1.140 +          if (idx != -1) {
   1.141 +              o = o.substr(0, idx) + "{...}";
   1.142 +          }
   1.143 +      }
   1.144 +      return ostring;
   1.145 +  },
   1.146 +
   1.147 +  waitFor: function test_waitFor(callback, test, timeout) {
   1.148 +      this.heartbeatCallback();
   1.149 +      if (test()) {
   1.150 +          callback();
   1.151 +          return;
   1.152 +      }
   1.153 +      var now = Date.now();
   1.154 +      var deadline = now + (typeof(timeout) == "undefined" ? this.timeout : timeout);
   1.155 +      if (deadline <= now) {
   1.156 +        dump("waitFor timeout: " + test.toString() + "\n");
   1.157 +        // the script will timeout here, so no need to raise a separate
   1.158 +        // timeout exception
   1.159 +        return;
   1.160 +      }
   1.161 +      this.window.setTimeout(this.waitFor.bind(this), 100, callback, test, deadline - now);
   1.162 +  },
   1.163 +
   1.164 +  runEmulatorCmd: function runEmulatorCmd(cmd, callback) {
   1.165 +    this.heartbeatCallback();
   1.166 +    this.scope.runEmulatorCmd(cmd, callback);
   1.167 +  },
   1.168 +
   1.169 +  runEmulatorShell: function runEmulatorShell(args, callback) {
   1.170 +    this.heartbeatCallback();
   1.171 +    this.scope.runEmulatorShell(args, callback);
   1.172 +  },
   1.173 +
   1.174 +};
   1.175 +

mercurial