tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_vibrator.html (4266B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test for Vibrator</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7 </head>
      8 <body>
      9 
     10 <!-- Although we can't test that the vibrator works properly, we can test that
     11     navigator.vibrate throws an exception where appropriate. -->
     12 
     13 <script class="testbody" type="text/javascript">
     14 function testNavigatorVibrate(testCase) {
     15  result = navigator.vibrate(testCase.value);
     16  is(result, true, `vibrate(${testCase.value}) must succeed.`);
     17 }
     18 
     19 function testNotificationVibrate(testCase) {
     20  var notification = new Notification('Test notification', {
     21    body: 'test vibrate',
     22    vibrate: testCase.value,
     23  });
     24 
     25  isDeeply(notification.vibrate, testCase.expected, `vibrate = ${testCase.value} should be accepted.`);
     26 }
     27 
     28 const MAX_VIBRATE_MS = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_ms');
     29 const MAX_VIBRATE_LIST_LEN = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_list_len');
     30 const TESTCASES = [
     31  {
     32    value: null,
     33    expected: [0],
     34  },{
     35    value: undefined,
     36    expected: [],
     37  },{
     38    // -1 will be converted to the highest unsigned long then clamped.
     39    value: -1,
     40    expected: [MAX_VIBRATE_MS],
     41  },{
     42    value: 'a',
     43    expected: [0],
     44  },{
     45    // -1 will be converted to the highest unsigned long then clamped.
     46    value: [100, -1],
     47    expected: [100, MAX_VIBRATE_MS],
     48  },{
     49    value: [100, 'a'],
     50    expected: [100, 0],
     51  },{
     52    // If we pass a vibration pattern with a value higher than max_vibrate_ms or a
     53    // pattern longer than max_vibrate_list_len, the call should succeed but the
     54    // pattern should be modified to match the restrictions.
     55 
     56    // Values will be clamped to dom.vibrator.max_vibrate_ms.
     57    value: MAX_VIBRATE_MS + 1,
     58    expected: [MAX_VIBRATE_MS],
     59  },{
     60    value: [MAX_VIBRATE_MS + 1],
     61    expected: [MAX_VIBRATE_MS],
     62  },{
     63    // The array will be truncated to have a length equal to dom.vibrator.max_vibrate_list_len.
     64    value: new Array(MAX_VIBRATE_LIST_LEN + 1).fill(0),
     65    expected: new Array(MAX_VIBRATE_LIST_LEN).fill(0),
     66  },{
     67    value: 0,
     68    expected: [0],
     69  },{
     70    value: [],
     71    expected: [],
     72  },{
     73    value: '1000',
     74    expected: [1000],
     75  },{
     76    value: 1000,
     77    expected: [1000],
     78  },{
     79    value: 1000.1,
     80    expected: [1000],
     81  },{
     82    value: [0, 0, 0],
     83    expected: [0, 0, 0],
     84  },{
     85    value: ['1000', 1000],
     86    expected: [1000, 1000],
     87  },{
     88    value: [1000, 1000],
     89    expected: [1000, 1000],
     90  },{
     91    value: [1000, 1000.1],
     92    expected: [1000, 1000],
     93  }
     94 ];
     95 
     96 function testWith(tester) {
     97  for (let testCase of TESTCASES) {
     98    tester(testCase);
     99  }
    100 }
    101 
    102 add_task(async function test_notification_vibrate_enabled() {
    103  await SpecialPowers.pushPrefEnv({"set": [['dom.webnotifications.vibrate.enabled', true]]});
    104 
    105  testWith(testNotificationVibrate);
    106 });
    107 
    108 add_task(async function test_vibrator_vibrate() {
    109  await SpecialPowers.pushPermissions([{type: 'vibration', allow: true, context: document}]);
    110  await SpecialPowers.pushPrefEnv({"set": [['dom.vibrator.enabled', true]]});
    111 
    112  testWith(testNavigatorVibrate);
    113 
    114  await SpecialPowers.pushPrefEnv({"set": [['dom.vibrator.enabled', false]]});
    115 
    116  testWith(testNavigatorVibrate);
    117 });
    118 
    119 add_task(async function test_vibrate_many_times() {
    120  await SpecialPowers.pushPermissions([{type: 'vibration', allow: true, context: document}]);
    121  await SpecialPowers.pushPrefEnv({"set": [['dom.vibrator.enabled', true]]});
    122 
    123 // The following loop shouldn't cause us to crash.  See bug 701716.
    124 for (var i = 0; i < 10000; i++) {
    125   navigator.vibrate([100, 100]);
    126 }
    127 ok(true, "Didn't crash after issuing a lot of vibrate() calls.");
    128 });
    129 
    130 add_task(async function test_notification_vibrate_silent() {
    131  await SpecialPowers.pushPrefEnv({"set": [['dom.webnotifications.vibrate.enabled', true],
    132      ['dom.webnotifications.silent.enabled', true]]});
    133 
    134  try {
    135    var notification = new Notification('Test notification', {
    136      body: 'test vibrate',
    137      vibrate: [100, 100],
    138      silent: true,
    139    });
    140    ok(false, "The above should throw if vibrate is enabled");
    141  } catch (error) {
    142    is(error.name, "TypeError", "A silent notification with a vibrate param should throw a TypeError");
    143  }
    144 });
    145 
    146 </script>
    147 </body>
    148 </html>