|
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> |
|
10 |
|
11 <!-- Although we can't test that the vibrator works properly, we can test that |
|
12 navigator.vibrate throws an exception where appropriate. --> |
|
13 |
|
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 } |
|
20 |
|
21 function expectSuccess(param) { |
|
22 result = navigator.vibrate(param); |
|
23 is(result, true, 'vibrate(' + param + ') must succeed.'); |
|
24 } |
|
25 |
|
26 function testFailures() { |
|
27 expectSuccess(null); |
|
28 expectSuccess(undefined); |
|
29 expectFailure(-1); |
|
30 expectSuccess('a'); |
|
31 expectFailure([100, -1]); |
|
32 expectSuccess([100, 'a']); |
|
33 |
|
34 var maxVibrateMs = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_ms'); |
|
35 var maxVibrateListLen = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_list_len'); |
|
36 |
|
37 // Make sure that these preferences are respected. |
|
38 expectFailure(maxVibrateMs + 1); |
|
39 expectFailure([maxVibrateMs + 1]); |
|
40 |
|
41 var arr = []; |
|
42 for (var i = 0; i < maxVibrateListLen + 1; i++) { |
|
43 arr[i] = 0; |
|
44 } |
|
45 expectFailure(arr); |
|
46 } |
|
47 |
|
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]); |
|
58 |
|
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 } |
|
65 |
|
66 var origVibratorEnabled = SpecialPowers.getBoolPref('dom.vibrator.enabled'); |
|
67 |
|
68 // Test with the vibrator pref enabled. |
|
69 try { |
|
70 SpecialPowers.setBoolPref('dom.vibrator.enabled', true); |
|
71 testFailures(); |
|
72 testSuccesses(); |
|
73 |
|
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 } |
|
84 |
|
85 </script> |
|
86 </body> |
|
87 |
|
88 </html> |