dom/tests/mochitest/general/test_vibrator.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4   <title>Test for Vibrator</title>
     5   <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
     6   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     7   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     8 </head>
     9 <body>
    11 <!-- Although we can't test that the vibrator works properly, we can test that
    12      navigator.vibrate throws an exception where appropriate. -->
    14 <script class="testbody" type="text/javascript;version=1.7">
    15 var result;
    16 function expectFailure(param) {
    17   result = navigator.vibrate(param);
    18   is(result, false, 'vibrate(' + param + ') should have failed.');
    19 }
    21 function expectSuccess(param) {
    22   result = navigator.vibrate(param);
    23   is(result, true, 'vibrate(' + param + ') must succeed.');
    24 }
    26 function testFailures() {
    27   expectSuccess(null);
    28   expectSuccess(undefined);
    29   expectFailure(-1);
    30   expectSuccess('a');
    31   expectFailure([100, -1]);
    32   expectSuccess([100, 'a']);
    34   var maxVibrateMs = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_ms');
    35   var maxVibrateListLen = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_list_len');
    37   // Make sure that these preferences are respected.
    38   expectFailure(maxVibrateMs + 1);
    39   expectFailure([maxVibrateMs + 1]);
    41   var arr = [];
    42   for (var i = 0; i < maxVibrateListLen + 1; i++) {
    43     arr[i] = 0;
    44   }
    45   expectFailure(arr);
    46 }
    48 function testSuccesses() {
    49   expectSuccess(0);
    50   expectSuccess([]);
    51   expectSuccess('1000');
    52   expectSuccess(1000);
    53   expectSuccess(1000.1);
    54   expectSuccess([0, 0, 0]);
    55   expectSuccess(['1000', 1000]);
    56   expectSuccess([1000, 1000]);
    57   expectSuccess([1000, 1000.1]);
    59   // The following loop shouldn't cause us to crash.  See bug 701716.
    60   for (var i = 0; i < 10000; i++) {
    61     navigator.vibrate([100, 100]);
    62   }
    63   ok(true, "Didn't crash after issuing a lot of vibrate() calls.");
    64 }
    66 var origVibratorEnabled = SpecialPowers.getBoolPref('dom.vibrator.enabled');
    68 // Test with the vibrator pref enabled.
    69 try {
    70   SpecialPowers.setBoolPref('dom.vibrator.enabled', true);
    71   testFailures();
    72   testSuccesses();
    74   // Everything should be the same when the vibrator is disabled -- in
    75   // particular, a disabled vibrator shouldn't eat failures we'd otherwise
    76   // observe.
    77   SpecialPowers.setBoolPref('dom.vibrator.enabled', false);
    78   testFailures();
    79   testSuccesses();
    80 }
    81 finally {
    82   SpecialPowers.setBoolPref('dom.vibrator.enabled', origVibratorEnabled);
    83 }
    85 </script>
    86 </body>
    88 </html>

mercurial