Bug 879308 - null date to mozAlarms.add throws exception. r=nsm sr=mounir
authorMina Almasry <almasry.mina@gmail.com>
Wed, 24 Jul 2013 10:46:59 -0700
changeset 152153 49939eb7372e2a72eb14b1373cdd9281131685c9
parent 152152 19848fff857e5652ef04d78c9ab06e5b2fb8a8a9
child 152154 322a966b388ed4f5bacbf2c432b86c87acb028d1
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-beta@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnsm, mounir
bugs879308
milestone25.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 879308 - null date to mozAlarms.add throws exception. r=nsm sr=mounir
dom/alarm/AlarmsManager.js
dom/alarm/test/test_alarm_add_date.html
--- a/dom/alarm/AlarmsManager.js
+++ b/dom/alarm/AlarmsManager.js
@@ -48,34 +48,38 @@ AlarmsManager.prototype = {
   add: function add(aDate, aRespectTimezone, aData) {
     debug("add()");
 
     if (!this._manifestURL) {
       debug("Cannot add alarms for non-installed apps.");
       throw Components.results.NS_ERROR_FAILURE;
     }
 
+    if (!aDate) {
+      throw Components.results.NS_ERROR_INVALID_ARG;
+    }
+
     let isIgnoreTimezone = true;
     switch (aRespectTimezone) {
       case "honorTimezone":
         isIgnoreTimezone = false;
         break;
 
       case "ignoreTimezone":
         isIgnoreTimezone = true;
         break;
 
       default:
-        throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
+        throw Components.results.NS_ERROR_INVALID_ARG;
         break;
     }
 
     let request = this.createRequest();
     this._cpmm.sendAsyncMessage(
-      "AlarmsManager:Add", 
+      "AlarmsManager:Add",
       { requestId: this.getRequestId(request),
         date: aDate,
         ignoreTimezone: isIgnoreTimezone,
         data: aData,
         pageURL: this._pageURL,
         manifestURL: this._manifestURL }
     );
     return request;
--- a/dom/alarm/test/test_alarm_add_date.html
+++ b/dom/alarm/test/test_alarm_add_date.html
@@ -20,98 +20,101 @@
     tomorrow.setDate(tomorrow.getDate() + 1);
 
     var domRequest;
     try {
       domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", {});
     } catch (e) {
       ok(false,
          "Unexpected exception trying to add alarm for tomorrow.");
+
+      // Proceed to next test.
       return testPastDate();
     }
     domRequest.onsuccess = function(e) {
       navigator.mozAlarms.remove(e.target.result);
       ok(true, "Add alarm for future date.");
 
-      // Awesome, no error so proceed to next test
+      // Awesome, no error so proceed to next test.
       testPastDate();
     };
     domRequest.onerror = function(e) {
       ok(false, "Unable to add alarm for tomorrow`.");
+
+      // Proceed to next test.
       testPastDate();
     };
-
   }
 
-  // Verify passing a Date that's already past fails
+  // Verify passing a Date that's already past doesn't fail (it should fire immediately).
   function testPastDate() {
     var yesterday = new Date();
     yesterday.setDate(yesterday.getDate() - 1);
 
     var domRequest;
     try {
       domRequest = navigator.mozAlarms.add(yesterday, "honorTimezone", {});
     } catch (e) {
       ok(false,
          "Unexpected exception trying to add alarm for yesterday.");
-      return testNull();
+
+      // Move on to the next test.
+      testNullDate();
     }
     domRequest.onsuccess = function(e) {
       navigator.mozAlarms.remove(e.target.result);
 
       ok(true, "Should be able to add alarm for already past date, which should fire immediately.");
-      testNull();
+
+      // Move on to the next test.
+      testNullDate();
     };
     domRequest.onerror = function(e) {
       ok(false, "Unable to add alarm for yesterday.");
 
-      // Errors as it should, on to the next test
-      testNull();
+      // Move on to the next test.
+      testNullDate();
     }
   }
 
   // Verify passing null does indeed fail
-  function testNull() {
-    var domRequest;
+  function testNullDate() {
     try {
-      domRequest = navigator.mozAlarms.add(null, "honorTimezone", {});
+      navigator.mozAlarms.add(null, "honorTimezone", {});
+      ok(false, "Expected an exception to be thrown for alarm with null date.");
     } catch(e) {
-      ok(false, "Unexpected exception thrown while testing null case.");
-
-      // Exception thrown
-      return SimpleTest.finish();
+      ok(true, "Exception thrown for alarm with null date.");
     }
-    domRequest.onsuccess = function(e) {
-      // Null should not be valid
-      ok(false, "Null should not be accepted as input for `date` param.");
-      SimpleTest.finish();
-    };
-    domRequest.onerror = function(e) {
-      // Null should not be valid
-      ok(true, "Passing null for date value causes failure.");
+
+    // Move on to the next test.
+    testInvalidTimeZone()
+  }
 
-      SimpleTest.finish();
-    };
+  function testInvalidTimeZone() {
+    try {
+      navigator.mozAlarms.add(new Date(), "badTimeZoneArg", {});
+      ok(false, "Expected an exception to be thrown while testing bad time zone arg.");
+    } catch(e) {
+      ok(true, "Exception thrown while testing bad time zone arg.");
+    }
+    SimpleTest.finish();
   }
 
   function startTests() {
 
     SpecialPowers.pushPrefEnv({"set": [["dom.mozAlarms.enabled", true]]}, function() {
-
       // Currently applicable only on FxOS
       if (navigator.userAgent.indexOf("Mobile") != -1 &&
-          navigator.appVersion.indexOf("Android") == -1) {
-
+          navigator.appVersion.indexOf("Android") == -1)
+      {
         testFutureDate();
-
       } else {
         ok(true, "mozAlarms on Firefox OS only.");
         SimpleTest.finish();
       }
-
     });
   }
 
   SimpleTest.expectAssertions(0, 9);
   SimpleTest.waitForExplicitFinish();
   if (SpecialPowers.hasPermission("alarms", document)) {
     startTests();
   } else {