Bug 848617 - New mochitests for Alarm API. r=gene
authorLuqman Aden <laden@mozilla.com>
Mon, 18 Mar 2013 15:47:30 -0700
changeset 125700 284c4adfc17c03c44053a4ee9382e90f15ada206
parent 125699 2c4666fa1ad90fb89eb2ee68e958c3ebc4594aee
child 125701 bb9dce2a497794f4081200f30a04b6c920e8e3bb
push id25063
push userryanvm@gmail.com
push dateThu, 21 Mar 2013 01:59:11 +0000
treeherdermozilla-inbound@bb9dce2a4977 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgene
bugs848617
milestone22.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 848617 - New mochitests for Alarm API. r=gene
dom/alarm/test/Makefile.in
dom/alarm/test/test_alarm_add_data.html
dom/alarm/test/test_alarm_add_date.html
dom/alarm/test/test_alarm_add_respectTimezone.html
dom/alarm/test/test_alarm_remove.html
--- a/dom/alarm/test/Makefile.in
+++ b/dom/alarm/test/Makefile.in
@@ -7,13 +7,17 @@ topsrcdir        = @top_srcdir@
 srcdir           = @srcdir@
 VPATH            = @srcdir@
 
 relativesrcdir   = @relativesrcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MOCHITEST_FILES = \
+  test_alarm_add_data.html \
+  test_alarm_add_date.html \
+  test_alarm_add_respectTimezone.html \
+  test_alarm_non_permitted_app.html \
   test_alarm_permitted_app.html \
-  test_alarm_non_permitted_app.html \
+  test_alarm_remove.html \
   $(NULL)
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/dom/alarm/test/test_alarm_add_data.html
@@ -0,0 +1,233 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test data Paramter for Alarm API</title>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none"></div>
+<pre id="test">
+  <script type="application/javascript">
+
+  "use strict";
+
+  // Verify passing {} for the data paramter
+  function testEmptyObject() {
+    var tomorrow = new Date();
+    tomorrow.setDate(tomorrow.getDate() + 1);
+
+    var data = {};
+
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", data);
+    } catch (e) {
+      ok(false,
+         "Unexpected exception trying to add alarm for tomorrow for empty object test.");
+
+      return testEmptyList();
+    }
+    domRequest.onsuccess = function(e) {
+      var alarmId = e.target.result;
+
+      // Confirm the alarm added has the data we requested
+      var allReq;
+      try {
+        allReq = navigator.mozAlarms.getAll();
+      } catch (e) {
+        ok(false,
+          "Unexpected exception trying to get all alarms for empty object test.");
+
+        return testEmptyList();
+      }
+      allReq.onsuccess = function(ev) {
+        navigator.mozAlarms.remove(alarmId);
+
+        var found = false;
+        ev.target.result.forEach(function(alarm, i, arr) {
+          if (alarm.id == alarmId) {
+            // Found the one we added
+            ok(Object.keys(alarm.data).length === 0,
+               "Empty object passed for data parameter for new alarm.");
+
+            found = true;
+          }
+        });
+
+        if (!found) {
+          ok(false, "Couldn't find alarm that was added for empty object test.");
+        }
+
+        testEmptyList();
+      }
+      allReq.onerror = function(e) {
+        ok(false, "Unable to get all alarms for empty object test.");
+
+        testEmptyList();
+      }
+    };
+    domRequest.onerror = function(e) {
+      ok(false, "Unable to add alarm for tomorrow for empty object test.");
+
+      testEmptyList();
+    };
+
+  }
+
+  // Verify passing [] for the data paramter
+  function testEmptyList() {
+    var tomorrow = new Date();
+    tomorrow.setDate(tomorrow.getDate() + 1);
+
+    var data = [];
+
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", data);
+    } catch (e) {
+      ok(false,
+         "Unexpected exception trying to add alarm for tomorrow for empty list test.");
+
+      return testNull();
+    }
+    domRequest.onsuccess = function(e) {
+      var alarmId = e.target.result;
+
+      // Confirm the alarm added has the data we requested
+      var allReq;
+      try {
+        allReq = navigator.mozAlarms.getAll();
+      } catch (e) {
+        ok(false,
+          "Unexpected exception trying to get all alarms for empty list test.");
+
+        return testNull();
+      }
+      allReq.onsuccess = function(ev) {
+        navigator.mozAlarms.remove(alarmId);
+
+        var found = false;
+        ev.target.result.forEach(function(alarm, i, arr) {
+          if (alarm.id == alarmId) {
+            // Found the one we added
+            ok(alarm.data.length === 0,
+               "Empty list passed for data parameter for new alarm.");
+
+            found = true;
+          }
+        });
+
+        if (!found) {
+          ok(false, "Couldn't find alarm that was added for empty list test.");
+        }
+
+        testNull();
+      }
+      allReq.onerror = function(e) {
+        ok(false, "Unable to get all alarms for empty list test.");
+
+        testNull();
+      }
+    };
+    domRequest.onerror = function(e) {
+      ok(false, "Unable to add alarm for tomorrow for empty list test.");
+
+      testNull();
+    };
+
+  }
+
+  // Verify passing null for the data paramter
+  function testNull() {
+    var tomorrow = new Date();
+    tomorrow.setDate(tomorrow.getDate() + 1);
+
+    var data = null;
+
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", data);
+    } catch (e) {
+      ok(false,
+         "Unexpected exception trying to add alarm for tomorrow for null test.");
+      return SimpleTest.finish();
+    }
+    domRequest.onsuccess = function(e) {
+      var alarmId = e.target.result;
+
+      // Confirm the alarm added has the data we requested
+      var allReq;
+      try {
+        allReq = navigator.mozAlarms.getAll();
+      } catch (e) {
+        ok(false,
+          "Unexpected exception trying to get all alarms for null test.");
+        return SimpleTest.finish();
+      }
+      allReq.onsuccess = function(ev) {
+        navigator.mozAlarms.remove(alarmId);
+
+        var found = false;
+        ev.target.result.forEach(function(alarm, i, arr) {
+          if (alarm.id == alarmId) {
+            // Found the one we added
+            ok(alarm.data === null,
+               "Null passed for data parameter for new alarm.");
+
+            found = true;
+          }
+        });
+
+        if (!found) {
+          ok(false, "Couldn't find alarm that was added for null test.");
+        }
+        SimpleTest.finish();
+      }
+      allReq.onerror = function(e) {
+        ok(false, "Unable to get all alarms for null test.");
+        SimpleTest.finish();
+      }
+    };
+    domRequest.onerror = function(e) {
+      ok(false, "Unable to add alarm for tomorrow for null test.");
+      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) {
+
+        testEmptyObject();
+
+      } else {
+        ok(true, "mozAlarms on Firefox OS only.");
+        SimpleTest.finish();
+      }
+
+    });
+
+  }
+
+  SimpleTest.expectAssertions(0, 9);
+  SimpleTest.waitForExplicitFinish();
+  if (SpecialPowers.hasPermission("alarms", document)) {
+    startTests();
+  } else {
+    // Add the permission and reload the page so it propogates
+    SpecialPowers.addPermission("alarms", true, document);
+    window.location.reload();
+  }
+
+  </script>
+</pre>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/alarm/test/test_alarm_add_date.html
@@ -0,0 +1,126 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test date Paramter for Alarm API</title>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none"></div>
+<pre id="test">
+  <script type="application/javascript">
+
+  "use strict";
+
+  // Verify passing a Date in the future doesn't fail
+  function testFutureDate() {
+    var tomorrow = new Date();
+    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.");
+      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
+      testPastDate();
+    };
+    domRequest.onerror = function(e) {
+      ok(false, "Unable to add alarm for tomorrow`.");
+      testPastDate();
+    };
+
+  }
+
+  // Verify passing a Date that's already past fails
+  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();
+    }
+    domRequest.onsuccess = function(e) {
+      navigator.mozAlarms.remove(e.target.result);
+
+      ok(false, "Should not be able to add alarm for already past date.");
+      testNull();
+    };
+    domRequest.onerror = function(e) {
+      ok(true, "Can't use past date when adding new alarm.");
+
+      // Errors as it should, on to the next test
+      testNull();
+    }
+  }
+
+  // Verify passing null does indeed fail
+  function testNull() {
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.add(null, "honorTimezone", {});
+    } catch(e) {
+      ok(false, "Unexpected exception thrown while testing null case.");
+
+      // Exception thrown
+      return SimpleTest.finish();
+    }
+    domRequest.onsuccess = function(e) {
+      // Null should not be valid
+      ok(false, "Null should not be accepted as input for `respectTimezone` param.");
+      SimpleTest.finish();
+    };
+    domRequest.onerror = function(e) {
+      // Null should not be valid
+      ok(true, "Passing null for date value causes failure.");
+
+      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) {
+
+        testFutureDate();
+
+      } else {
+        ok(true, "mozAlarms on Firefox OS only.");
+        SimpleTest.finish();
+      }
+
+    });
+  }
+
+  SimpleTest.expectAssertions(0, 9);
+  SimpleTest.waitForExplicitFinish();
+  if (SpecialPowers.hasPermission("alarms", document)) {
+    startTests();
+  } else {
+    // Add the permissions and reload so they propogate
+    SpecialPowers.addPermission("alarms", true, document);
+    window.location.reload();
+  }
+
+  </script>
+</pre>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/alarm/test/test_alarm_add_respectTimezone.html
@@ -0,0 +1,155 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test respectTimezone Parameter for Alarm API</title>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none"></div>
+<pre id="test">
+  <script type="application/javascript">
+
+  "use strict";
+
+  // Verify passing `honorTimezone` doesn't fail
+  function testHonorTimezone(tomorrow) {
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", {});
+    } catch (e) {
+      ok(false,
+         "Unexpected exception trying to add alarm for tomorrow with `honorTimezone`.");
+      return testIgnoreTimezone(tomorrow);
+    }
+    domRequest.onsuccess = function(e) {
+      navigator.mozAlarms.remove(e.target.result);
+
+      ok(true, "Passing `honorTimezone` for repectTimezone argument.");
+      testIgnoreTimezone(tomorrow);
+    };
+    domRequest.onerror = function(e) {
+      ok(false, "Unable to add alarm for tomorrow with `honorTimezone`.");
+      testIgnoreTimezone(tomorrow);
+    };
+
+  }
+
+  // Verify passing `ignoreTimezone` doesn't fail
+  function testIgnoreTimezone(tomorrow) {
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.add(tomorrow, "ignoreTimezone", {});
+    } catch (e) {
+      ok(false,
+         "Unexpected exception trying to add alarm for tomorrow with `ignoreTimezone`.");
+      return testBadInput(tomorrow);
+    }
+    domRequest.onsuccess = function(e) {
+      navigator.mozAlarms.remove(e.target.result);
+
+      ok(true, "Passing `ignoreTimezone` for respectTimezone argument.");
+      testBadInput(tomorrow);
+    };
+    domRequest.onerror = function(e) {
+      ok(false, "Unable to add alarm for tomorrow with `ignoreTimezone`.");
+      testBadInput(tomorrow);
+    }
+  }
+
+  // Verify passing a string that's not `honorTimezone` or `ignoreTimezone`
+  // does fail
+  function testBadInput(tomorrow) {
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.add(tomorrow, "badinput", {});
+    } catch (e) {
+      ok(true, "Bad input for repectTimezone does indeed fail.");
+
+      // Errors, as it should. On to the next test.
+      return testNull(tomorrow);
+    }
+    domRequest.onsuccess = function(e) {
+      // Welp, this shouldn't happen
+      ok(false, "Malformed input accepted for `respectTimezone` param.");
+      testNull(tomorrow);
+    };
+  }
+
+  // Verify passing null does indeed fail
+  function testNull(tomorrow) {
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.add(tomorrow, null, {});
+    } catch(e) {
+      ok(true, "Passing null for respectTimezone does indeed fail.");
+
+      // Exception thrown, on to the next test
+      return testMisspelt(tomorrow);
+    }
+    domRequest.onsuccess = function(e) {
+      // Null should not be valid
+      ok(false, "Null should not be accepted as input for `respectTimezone` param.");
+      testMisspelt(tomorrow);
+    };
+  }
+
+  // Verify that misspelling does indeed fail
+  function testMisspelt(tomorrow) {
+    var domRequest;
+    try {
+      // Missing the e in `ignoreTimezone`
+      domRequest = navigator.mozAlarms.add(tomorrow, "ignoreTimzone", {});
+    } catch (e) {
+      ok(true, "Misspelling `ignoreTimezone` does indeed fail.");
+
+      // Exception thrown, all is right in the world.
+      // All done with tests now.
+      return SimpleTest.finish();
+    }
+    domRequest.onsuccess = function(e) {
+      // The misspelled word should not be valid
+      ok(false, "Misspelt parameter should fail.");
+      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) {
+
+        // Arbitrary date to use for tests
+        var tomorrow = new Date();
+        tomorrow.setDate(tomorrow.getDate() + 1);
+
+        // Kick off the tests
+        testHonorTimezone(tomorrow);
+
+      } else {
+        ok(true, "mozAlarms on Firefox OS only.");
+        SimpleTest.finish();
+      }
+
+    });
+  }
+
+  SimpleTest.expectAssertions(0, 9);
+  SimpleTest.waitForExplicitFinish();
+  if (SpecialPowers.hasPermission("alarms", document)) {
+    startTests();
+  } else {
+    // Add the permission and reload the page so it propogates
+    SpecialPowers.addPermission("alarms", true, document);
+    window.location.reload();
+  }
+
+  </script>
+</pre>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/alarm/test/test_alarm_remove.html
@@ -0,0 +1,103 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Test Adding and Removing Alarms with Alarm API</title>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none"></div>
+<pre id="test">
+  <script type="application/javascript">
+
+  "use strict";
+
+  function checkNumberOfAlarms(n, cbk) {
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.getAll();
+    } catch (e) {
+      ok(false,
+         "Unexpected exception trying to get all alarms.");
+    }
+    domRequest.onsuccess = function(e) {
+      ok(e.target.result.length === n, "Correct number of alarms set.");
+      cbk();
+    }
+  }
+
+  // Add alarm and then remove it
+  function testAddRemove() {
+    var tomorrow = new Date();
+    tomorrow.setDate(tomorrow.getDate() + 1);
+
+    var domRequest;
+    try {
+      domRequest = navigator.mozAlarms.getAll();
+    } catch (e) {
+      ok(false,
+         "Unexpected exception trying to get all alarms.");
+
+      return SimpleTest.finish();
+    }
+    domRequest.onsuccess = function(e) {
+      var initialAlarmsN = e.target.result.length;
+
+      var dr;
+      try {
+        dr = navigator.mozAlarms.add(tomorrow, "honorTimezone", null);
+      } catch (e) {
+        ok(false,
+           "Unexpected exception trying add alarm.");
+
+        return SimpleTest.finish();
+      }
+      dr.onsuccess = function(ev) {
+        var alarmId = ev.target.result;
+
+        checkNumberOfAlarms(initialAlarmsN + 1, function() {
+          navigator.mozAlarms.remove(alarmId);
+
+          checkNumberOfAlarms(initialAlarmsN, function() {
+            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) {
+
+        testAddRemove();
+
+      } else {
+        ok(true, "mozAlarms on Firefox OS only.");
+        SimpleTest.finish();
+      }
+
+    });
+  }
+
+  SimpleTest.expectAssertions(0, 9);
+  SimpleTest.waitForExplicitFinish();
+  if (SpecialPowers.hasPermission("alarms", document)) {
+    startTests();
+  } else {
+    // Add the permission and reload so it's propogated
+    SpecialPowers.addPermission("alarms", true, document);
+    window.location.reload();
+  }
+  </script>
+</pre>
+</body>
+</html>