dom/tests/mochitest/general/test_vibrator.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/general/test_vibrator.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test for Vibrator</title>
     1.8 +  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body>
    1.13 +
    1.14 +<!-- Although we can't test that the vibrator works properly, we can test that
    1.15 +     navigator.vibrate throws an exception where appropriate. -->
    1.16 +
    1.17 +<script class="testbody" type="text/javascript;version=1.7">
    1.18 +var result;
    1.19 +function expectFailure(param) {
    1.20 +  result = navigator.vibrate(param);
    1.21 +  is(result, false, 'vibrate(' + param + ') should have failed.');
    1.22 +}
    1.23 +
    1.24 +function expectSuccess(param) {
    1.25 +  result = navigator.vibrate(param);
    1.26 +  is(result, true, 'vibrate(' + param + ') must succeed.');
    1.27 +}
    1.28 +
    1.29 +function testFailures() {
    1.30 +  expectSuccess(null);
    1.31 +  expectSuccess(undefined);
    1.32 +  expectFailure(-1);
    1.33 +  expectSuccess('a');
    1.34 +  expectFailure([100, -1]);
    1.35 +  expectSuccess([100, 'a']);
    1.36 +
    1.37 +  var maxVibrateMs = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_ms');
    1.38 +  var maxVibrateListLen = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_list_len');
    1.39 +
    1.40 +  // Make sure that these preferences are respected.
    1.41 +  expectFailure(maxVibrateMs + 1);
    1.42 +  expectFailure([maxVibrateMs + 1]);
    1.43 +
    1.44 +  var arr = [];
    1.45 +  for (var i = 0; i < maxVibrateListLen + 1; i++) {
    1.46 +    arr[i] = 0;
    1.47 +  }
    1.48 +  expectFailure(arr);
    1.49 +}
    1.50 +
    1.51 +function testSuccesses() {
    1.52 +  expectSuccess(0);
    1.53 +  expectSuccess([]);
    1.54 +  expectSuccess('1000');
    1.55 +  expectSuccess(1000);
    1.56 +  expectSuccess(1000.1);
    1.57 +  expectSuccess([0, 0, 0]);
    1.58 +  expectSuccess(['1000', 1000]);
    1.59 +  expectSuccess([1000, 1000]);
    1.60 +  expectSuccess([1000, 1000.1]);
    1.61 +
    1.62 +  // The following loop shouldn't cause us to crash.  See bug 701716.
    1.63 +  for (var i = 0; i < 10000; i++) {
    1.64 +    navigator.vibrate([100, 100]);
    1.65 +  }
    1.66 +  ok(true, "Didn't crash after issuing a lot of vibrate() calls.");
    1.67 +}
    1.68 +
    1.69 +var origVibratorEnabled = SpecialPowers.getBoolPref('dom.vibrator.enabled');
    1.70 +
    1.71 +// Test with the vibrator pref enabled.
    1.72 +try {
    1.73 +  SpecialPowers.setBoolPref('dom.vibrator.enabled', true);
    1.74 +  testFailures();
    1.75 +  testSuccesses();
    1.76 +
    1.77 +  // Everything should be the same when the vibrator is disabled -- in
    1.78 +  // particular, a disabled vibrator shouldn't eat failures we'd otherwise
    1.79 +  // observe.
    1.80 +  SpecialPowers.setBoolPref('dom.vibrator.enabled', false);
    1.81 +  testFailures();
    1.82 +  testSuccesses();
    1.83 +}
    1.84 +finally {
    1.85 +  SpecialPowers.setBoolPref('dom.vibrator.enabled', origVibratorEnabled);
    1.86 +}
    1.87 +
    1.88 +</script>
    1.89 +</body>
    1.90 +
    1.91 +</html>

mercurial