dom/alarm/test/test_alarm_add_date.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/alarm/test/test_alarm_add_date.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <meta charset="utf-8">
     1.8 +  <title>Test date Paramter for Alarm API</title>
     1.9 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.10 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.11 +</head>
    1.12 +<body>
    1.13 +<p id="display"></p>
    1.14 +<div id="content" style="display: none"></div>
    1.15 +<pre id="test">
    1.16 +  <script type="application/javascript">
    1.17 +
    1.18 +  "use strict";
    1.19 +
    1.20 +  // Verify passing a Date in the future doesn't fail
    1.21 +  function testFutureDate() {
    1.22 +    var tomorrow = new Date();
    1.23 +    tomorrow.setDate(tomorrow.getDate() + 1);
    1.24 +
    1.25 +    var domRequest;
    1.26 +    try {
    1.27 +      domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", {});
    1.28 +    } catch (e) {
    1.29 +      ok(false,
    1.30 +         "Unexpected exception trying to add alarm for tomorrow.");
    1.31 +
    1.32 +      // Proceed to next test.
    1.33 +      return testPastDate();
    1.34 +    }
    1.35 +    domRequest.onsuccess = function(e) {
    1.36 +      navigator.mozAlarms.remove(e.target.result);
    1.37 +      ok(true, "Add alarm for future date.");
    1.38 +
    1.39 +      // Awesome, no error so proceed to next test.
    1.40 +      testPastDate();
    1.41 +    };
    1.42 +    domRequest.onerror = function(e) {
    1.43 +      ok(false, "Unable to add alarm for tomorrow`.");
    1.44 +
    1.45 +      // Proceed to next test.
    1.46 +      testPastDate();
    1.47 +    };
    1.48 +  }
    1.49 +
    1.50 +  // Verify passing a Date that's already past doesn't fail (it should fire immediately).
    1.51 +  function testPastDate() {
    1.52 +    var yesterday = new Date();
    1.53 +    yesterday.setDate(yesterday.getDate() - 1);
    1.54 +
    1.55 +    var domRequest;
    1.56 +    try {
    1.57 +      domRequest = navigator.mozAlarms.add(yesterday, "honorTimezone", {});
    1.58 +    } catch (e) {
    1.59 +      ok(false,
    1.60 +         "Unexpected exception trying to add alarm for yesterday.");
    1.61 +
    1.62 +      // Move on to the next test.
    1.63 +      testNullDate();
    1.64 +    }
    1.65 +    domRequest.onsuccess = function(e) {
    1.66 +      navigator.mozAlarms.remove(e.target.result);
    1.67 +
    1.68 +      ok(true, "Should be able to add alarm for already past date, which should fire immediately.");
    1.69 +
    1.70 +      // Move on to the next test.
    1.71 +      testNullDate();
    1.72 +    };
    1.73 +    domRequest.onerror = function(e) {
    1.74 +      ok(false, "Unable to add alarm for yesterday.");
    1.75 +
    1.76 +      // Move on to the next test.
    1.77 +      testNullDate();
    1.78 +    }
    1.79 +  }
    1.80 +
    1.81 +  // Verify passing null does indeed fail
    1.82 +  function testNullDate() {
    1.83 +    try {
    1.84 +      navigator.mozAlarms.add(null, "honorTimezone", {});
    1.85 +      ok(false, "Expected an exception to be thrown for alarm with null date.");
    1.86 +    } catch(e) {
    1.87 +      ok(true, "Exception thrown for alarm with null date.");
    1.88 +    }
    1.89 +
    1.90 +    // Move on to the next test.
    1.91 +    testInvalidTimeZone()
    1.92 +  }
    1.93 +
    1.94 +  function testInvalidTimeZone() {
    1.95 +    try {
    1.96 +      navigator.mozAlarms.add(new Date(), "badTimeZoneArg", {});
    1.97 +      ok(false, "Expected an exception to be thrown while testing bad time zone arg.");
    1.98 +    } catch(e) {
    1.99 +      ok(true, "Exception thrown while testing bad time zone arg.");
   1.100 +    }
   1.101 +    SimpleTest.finish();
   1.102 +  }
   1.103 +
   1.104 +  function startTests() {
   1.105 +
   1.106 +    SpecialPowers.pushPrefEnv({"set": [["dom.mozAlarms.enabled", true]]}, function() {
   1.107 +      // Currently applicable only on FxOS
   1.108 +      if (navigator.userAgent.indexOf("Mobile") != -1 &&
   1.109 +          navigator.appVersion.indexOf("Android") == -1)
   1.110 +      {
   1.111 +        testFutureDate();
   1.112 +      } else {
   1.113 +        ok(true, "mozAlarms on Firefox OS only.");
   1.114 +        SimpleTest.finish();
   1.115 +      }
   1.116 +    });
   1.117 +  }
   1.118 +
   1.119 +  SimpleTest.expectAssertions(0, 9);
   1.120 +  SimpleTest.waitForExplicitFinish();
   1.121 +  if (SpecialPowers.hasPermission("alarms", document)) {
   1.122 +    startTests();
   1.123 +  } else {
   1.124 +    // Add the permissions and reload so they propogate
   1.125 +    SpecialPowers.addPermission("alarms", true, document);
   1.126 +    window.location.reload();
   1.127 +  }
   1.128 +
   1.129 +  </script>
   1.130 +</pre>
   1.131 +</body>
   1.132 +</html>

mercurial