Bug 1014581 - Vibration API: clamp vibration lengths and array length to match platform restrictions. r=smaug

This commit is contained in:
Mounir Lamouri 2014-05-27 21:54:03 +01:00
Родитель d219051569
Коммит 9094d69df2
2 изменённых файлов: 27 добавлений и 20 удалений

Просмотреть файл

@ -792,19 +792,21 @@ Navigator::Vibrate(const nsTArray<uint32_t>& aPattern)
return false; return false;
} }
if (aPattern.Length() > sMaxVibrateListLen) { nsTArray<uint32_t> pattern(aPattern);
return false;
if (pattern.Length() > sMaxVibrateListLen) {
pattern.SetLength(sMaxVibrateMS);
} }
for (size_t i = 0; i < aPattern.Length(); ++i) { for (size_t i = 0; i < pattern.Length(); ++i) {
if (aPattern[i] > sMaxVibrateMS) { if (pattern[i] > sMaxVibrateMS) {
return false; pattern[i] = sMaxVibrateMS;
} }
} }
// The spec says we check sVibratorEnabled after we've done the sanity // The spec says we check sVibratorEnabled after we've done the sanity
// checking on the pattern. // checking on the pattern.
if (aPattern.IsEmpty() || !sVibratorEnabled) { if (pattern.IsEmpty() || !sVibratorEnabled) {
return true; return true;
} }
@ -822,7 +824,7 @@ Navigator::Vibrate(const nsTArray<uint32_t>& aPattern)
} }
gVibrateWindowListener = new VibrateWindowListener(mWindow, doc); gVibrateWindowListener = new VibrateWindowListener(mWindow, doc);
hal::Vibrate(aPattern, mWindow); hal::Vibrate(pattern, mWindow);
return true; return true;
} }

Просмотреть файл

@ -23,29 +23,36 @@ function expectSuccess(param) {
is(result, true, 'vibrate(' + param + ') must succeed.'); is(result, true, 'vibrate(' + param + ') must succeed.');
} }
function testFailures() { function tests() {
// Some edge cases that the bindings should handle for us.
expectSuccess(null); expectSuccess(null);
expectSuccess(undefined); expectSuccess(undefined);
expectFailure(-1); // -1 will be converted to the highest unsigned long then clamped.
expectSuccess(-1);
expectSuccess('a'); expectSuccess('a');
expectFailure([100, -1]); // -1 will be converted to the highest unsigned long then clamped.
expectSuccess([100, -1]);
expectSuccess([100, 'a']); expectSuccess([100, 'a']);
var maxVibrateMs = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_ms'); var maxVibrateMs = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_ms');
var maxVibrateListLen = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_list_len'); var maxVibrateListLen = SpecialPowers.getIntPref('dom.vibrator.max_vibrate_list_len');
// Make sure that these preferences are respected. // If we pass a vibration pattern with a value higher than max_vibrate_ms or a
expectFailure(maxVibrateMs + 1); // pattern longer than max_vibrate_list_len, the call should succeed but the
expectFailure([maxVibrateMs + 1]); // pattern should be modified to match the restrictions.
// Values will be clamped to dom.vibrator.max_vibrate_ms.
expectSuccess(maxVibrateMs + 1);
expectSuccess([maxVibrateMs + 1]);
var arr = []; var arr = [];
for (var i = 0; i < maxVibrateListLen + 1; i++) { for (var i = 0; i < maxVibrateListLen + 1; i++) {
arr[i] = 0; arr[i] = 0;
} }
expectFailure(arr); // The array will be truncated to have a length equal to dom.vibrator.max_vibrate_list_len.
} expectSuccess(arr);
function testSuccesses() {
expectSuccess(0); expectSuccess(0);
expectSuccess([]); expectSuccess([]);
expectSuccess('1000'); expectSuccess('1000');
@ -68,15 +75,13 @@ var origVibratorEnabled = SpecialPowers.getBoolPref('dom.vibrator.enabled');
// Test with the vibrator pref enabled. // Test with the vibrator pref enabled.
try { try {
SpecialPowers.setBoolPref('dom.vibrator.enabled', true); SpecialPowers.setBoolPref('dom.vibrator.enabled', true);
testFailures(); tests();
testSuccesses();
// Everything should be the same when the vibrator is disabled -- in // Everything should be the same when the vibrator is disabled -- in
// particular, a disabled vibrator shouldn't eat failures we'd otherwise // particular, a disabled vibrator shouldn't eat failures we'd otherwise
// observe. // observe.
SpecialPowers.setBoolPref('dom.vibrator.enabled', false); SpecialPowers.setBoolPref('dom.vibrator.enabled', false);
testFailures(); tests();
testSuccesses();
} }
finally { finally {
SpecialPowers.setBoolPref('dom.vibrator.enabled', origVibratorEnabled); SpecialPowers.setBoolPref('dom.vibrator.enabled', origVibratorEnabled);