dom/mobilemessage/tests/marionette/test_invalid_address.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/mobilemessage/tests/marionette/test_invalid_address.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,143 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +MARIONETTE_TIMEOUT = 60000;
     1.8 +
     1.9 +const MMS_MAX_LENGTH_SUBJECT = 40;
    1.10 +
    1.11 +SpecialPowers.addPermission("sms", true, document);
    1.12 +SpecialPowers.setBoolPref("dom.sms.enabled", true);
    1.13 +
    1.14 +let tasks = {
    1.15 +  // List of test fuctions. Each of them should call |tasks.next()| when
    1.16 +  // completed or |tasks.finish()| to jump to the last one.
    1.17 +  _tasks: [],
    1.18 +  _nextTaskIndex: 0,
    1.19 +
    1.20 +  push: function(func) {
    1.21 +    this._tasks.push(func);
    1.22 +  },
    1.23 +
    1.24 +  next: function() {
    1.25 +    let index = this._nextTaskIndex++;
    1.26 +    let task = this._tasks[index];
    1.27 +    try {
    1.28 +      task();
    1.29 +    } catch (ex) {
    1.30 +      ok(false, "test task[" + index + "] throws: " + ex);
    1.31 +      // Run last task as clean up if possible.
    1.32 +      if (index != this._tasks.length - 1) {
    1.33 +        this.finish();
    1.34 +      }
    1.35 +    }
    1.36 +  },
    1.37 +
    1.38 +  finish: function() {
    1.39 +    this._tasks[this._tasks.length - 1]();
    1.40 +  },
    1.41 +
    1.42 +  run: function() {
    1.43 +    this.next();
    1.44 +  }
    1.45 +};
    1.46 +
    1.47 +let manager;
    1.48 +
    1.49 +function getAllMessages(callback, filter, reverse) {
    1.50 +  if (!filter) {
    1.51 +    filter = new MozSmsFilter;
    1.52 +  }
    1.53 +  let messages = [];
    1.54 +  let request = manager.getMessages(filter, reverse || false);
    1.55 +  request.onsuccess = function(event) {
    1.56 +    if (request.result) {
    1.57 +      messages.push(request.result);
    1.58 +      request.continue();
    1.59 +      return;
    1.60 +    }
    1.61 +
    1.62 +    window.setTimeout(callback.bind(null, messages), 0);
    1.63 +  }
    1.64 +}
    1.65 +
    1.66 +function deleteAllMessages() {
    1.67 +  getAllMessages(function deleteAll(messages) {
    1.68 +    let message = messages.shift();
    1.69 +    if (!message) {
    1.70 +      ok(true, "all messages deleted");
    1.71 +      tasks.next();
    1.72 +      return;
    1.73 +    }
    1.74 +
    1.75 +    let request = manager.delete(message.id);
    1.76 +    request.onsuccess = deleteAll.bind(null, messages);
    1.77 +    request.onerror = function(event) {
    1.78 +      ok(false, "failed to delete all messages");
    1.79 +      tasks.finish();
    1.80 +    }
    1.81 +  });
    1.82 +}
    1.83 +
    1.84 +function testInvalidAddressForSMS(aInvalidAddr)  {
    1.85 +  log("manager.send(...) should get 'InvalidAddressError' error " +
    1.86 +      "when attempting to send SMS to: " + aInvalidAddr);
    1.87 +
    1.88 +  let request = manager.send(aInvalidAddr, "Test");
    1.89 +
    1.90 +  request.onerror = function(event) {
    1.91 +    log("Received 'onerror' DOMRequest event.");
    1.92 +    let error = event.target.error;
    1.93 +    ok(error instanceof DOMError, "should be a valid DOMError object");
    1.94 +    ok(error.name === "InvalidAddressError", "should be 'InvalidAddressError'");
    1.95 +    tasks.next();
    1.96 +  };
    1.97 +}
    1.98 +
    1.99 +function testInvalidAddressForMMS(aInvalidAddrs)  {
   1.100 +  log("manager.sendMMS(...) should get 'InvalidAddressError' error " +
   1.101 +      "when attempting to send MMS to: " + aInvalidAddrs);
   1.102 +
   1.103 +  let request = manager.sendMMS({
   1.104 +    subject: "Test",
   1.105 +    receivers: aInvalidAddrs,
   1.106 +    attachments: [],
   1.107 +  });
   1.108 +
   1.109 +  request.onerror = function(event) {
   1.110 +    log("Received 'onerror' DOMRequest event.");
   1.111 +    let error = event.target.error;
   1.112 +    ok(error instanceof DOMError, "should be a valid DOMError object");
   1.113 +    ok(error.name === "InvalidAddressError", "should be 'InvalidAddressError'");
   1.114 +    tasks.next();
   1.115 +  };
   1.116 +}
   1.117 +
   1.118 +tasks.push(function() {
   1.119 +  log("Verifying initial state.");
   1.120 +
   1.121 +  manager = window.navigator.mozMobileMessage;
   1.122 +  ok(manager instanceof MozMobileMessageManager,
   1.123 +     "manager is instance of " + manager.constructor);
   1.124 +
   1.125 +  tasks.next();
   1.126 +});
   1.127 +
   1.128 +// Test sending SMS to invalid addresses.
   1.129 +tasks.push(testInvalidAddressForSMS.bind(this, "&%&"));
   1.130 +tasks.push(testInvalidAddressForSMS.bind(this, ""));
   1.131 +
   1.132 +// Test sending MMS to invalid addresses.
   1.133 +tasks.push(testInvalidAddressForMMS.bind(this, ["&%&"]));
   1.134 +tasks.push(testInvalidAddressForMMS.bind(this, [""]));
   1.135 +tasks.push(testInvalidAddressForMMS.bind(this, ["123", "&%&", "456"]));
   1.136 +
   1.137 +tasks.push(deleteAllMessages);
   1.138 +
   1.139 +// WARNING: All tasks should be pushed before this!!!
   1.140 +tasks.push(function cleanUp() {
   1.141 +  SpecialPowers.removePermission("sms", document);
   1.142 +  SpecialPowers.clearUserPref("dom.sms.enabled");
   1.143 +  finish();
   1.144 +});
   1.145 +
   1.146 +tasks.run();

mercurial