summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/test_vibrator.html
blob: e7085fc987f5c1a469dab74cda9d9f7c8494edf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Vibrator</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>

<!-- Although we can't test that the vibrator works properly, we can test that
     navigator.vibrate throws an exception where appropriate. -->

<script class="testbody" type="text/javascript">
function testNavigatorVibrate(testCase) {
  result = navigator.vibrate(testCase.value);
  is(result, true, `vibrate(${testCase.value}) must succeed.`);
}

function testNotificationVibrate(testCase) {
  var notification = new Notification('Test notification', {
    body: 'test vibrate',
    vibrate: testCase.value,
  });

  isDeeply(notification.vibrate, testCase.expected, `vibrate = ${testCase.value} should be accepted.`);
}

const MAX_VIBRATE_MS = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_ms');
const MAX_VIBRATE_LIST_LEN = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_list_len');
const TESTCASES = [
  {
    value: null,
    expected: [0],
  },{
    value: undefined,
    expected: [],
  },{
    // -1 will be converted to the highest unsigned long then clamped.
    value: -1,
    expected: [MAX_VIBRATE_MS],
  },{
    value: 'a',
    expected: [0],
  },{
    // -1 will be converted to the highest unsigned long then clamped.
    value: [100, -1],
    expected: [100, MAX_VIBRATE_MS],
  },{
    value: [100, 'a'],
    expected: [100, 0],
  },{
    // If we pass a vibration pattern with a value higher than max_vibrate_ms or a
    // pattern longer than max_vibrate_list_len, the call should succeed but the
    // pattern should be modified to match the restrictions.

    // Values will be clamped to dom.vibrator.max_vibrate_ms.
    value: MAX_VIBRATE_MS + 1,
    expected: [MAX_VIBRATE_MS],
  },{
    value: [MAX_VIBRATE_MS + 1],
    expected: [MAX_VIBRATE_MS],
  },{
    // The array will be truncated to have a length equal to dom.vibrator.max_vibrate_list_len.
    value: new Array(MAX_VIBRATE_LIST_LEN + 1).fill(0),
    expected: new Array(MAX_VIBRATE_LIST_LEN).fill(0),
  },{
    value: 0,
    expected: [0],
  },{
    value: [],
    expected: [],
  },{
    value: '1000',
    expected: [1000],
  },{
    value: 1000,
    expected: [1000],
  },{
    value: 1000.1,
    expected: [1000],
  },{
    value: [0, 0, 0],
    expected: [0, 0, 0],
  },{
    value: ['1000', 1000],
    expected: [1000, 1000],
  },{
    value: [1000, 1000],
    expected: [1000, 1000],
  },{
    value: [1000, 1000.1],
    expected: [1000, 1000],
  }
];

function testWith(tester) {
  for (let testCase of TESTCASES) {
    tester(testCase);
  }
}

add_task(async function test_notification_vibrate_enabled() {
  await SpecialPowers.pushPrefEnv({"set": [['dom.webnotifications.vibrate.enabled', true]]});

  testWith(testNotificationVibrate);
});

add_task(async function test_vibrator_vibrate() {
  await SpecialPowers.pushPermissions([{type: 'vibration', allow: true, context: document}]);
  await SpecialPowers.pushPrefEnv({"set": [['dom.vibrator.enabled', true]]});

  testWith(testNavigatorVibrate);

  await SpecialPowers.pushPrefEnv({"set": [['dom.vibrator.enabled', false]]});

  testWith(testNavigatorVibrate);
});

add_task(async function test_vibrate_many_times() {
  await SpecialPowers.pushPermissions([{type: 'vibration', allow: true, context: document}]);
  await SpecialPowers.pushPrefEnv({"set": [['dom.vibrator.enabled', true]]});

 // The following loop shouldn't cause us to crash.  See bug 701716.
 for (var i = 0; i < 10000; i++) {
   navigator.vibrate([100, 100]);
 }
 ok(true, "Didn't crash after issuing a lot of vibrate() calls.");
});

add_task(async function test_notification_vibrate_silent() {
  await SpecialPowers.pushPrefEnv({"set": [['dom.webnotifications.vibrate.enabled', true],
      ['dom.webnotifications.silent.enabled', true]]});

  try {
    var notification = new Notification('Test notification', {
      body: 'test vibrate',
      vibrate: [100, 100],
      silent: true,
    });
    ok(false, "The above should throw if vibrate is enabled");
  } catch (error) {
    is(error.name, "TypeError", "A silent notification with a vibrate param should throw a TypeError");
  }
});

</script>
</body>
</html>