michael@0: /* michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * michael@0: */ michael@0: michael@0: exports.defineAutoTests = function () { michael@0: michael@0: describe('Vibration (navigator.notification.vibrate)', function () { michael@0: it("navigator.notification should exist", function () { michael@0: expect(navigator.notification).toBeDefined(); michael@0: }); michael@0: michael@0: it("should contain a vibrate function", function () { michael@0: expect(typeof navigator.notification.vibrate).toBeDefined(); michael@0: expect(typeof navigator.notification.vibrate).toBe("function"); michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports.defineManualTests = function (contentEl, createActionButton) { michael@0: var logMessage = function (message, color) { michael@0: var log = document.getElementById('info'); michael@0: var logLine = document.createElement('div'); michael@0: if (color) { michael@0: logLine.style.color = color; michael@0: } michael@0: logLine.innerHTML = message; michael@0: log.appendChild(logLine); michael@0: } michael@0: michael@0: var clearLog = function () { michael@0: var log = document.getElementById('info'); michael@0: log.innerHTML = ''; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Vibrations michael@0: //------------------------------------------------------------------------- michael@0: michael@0: //old vibrate call michael@0: var vibrateOld = function(){ michael@0: clearLog(); michael@0: navigator.notification.vibrate(2500); michael@0: logMessage("navigator.notification.vibrate(2500)", "green"); michael@0: }; michael@0: michael@0: //old vibrate with pattern call michael@0: var vibrateWithPatternOld = function(){ michael@0: clearLog(); michael@0: navigator.notification.vibrateWithPattern([1000, 3000, 2000, 5000]); michael@0: logMessage("navigator.notification.vibrateWithPattern([1000, 3000, 2000, 5000])", "green"); michael@0: }; michael@0: michael@0: //old cancel vibrate call michael@0: var cancelOld = function(){ michael@0: clearLog(); michael@0: navigator.notification.cancelVibration(); michael@0: logMessage("navigator.notification.cancelVibration()", "green"); michael@0: }; michael@0: michael@0: //new standard vibrate call that aligns to w3c spec with param long michael@0: var vibrateWithInt = function() { michael@0: clearLog(); michael@0: navigator.vibrate(3000); michael@0: logMessage("navigator.vibrate(3000)", "green"); michael@0: }; michael@0: michael@0: //new standard vibrate call that aligns to w3c spec with param array michael@0: var vibrateWithArray = function() { michael@0: clearLog(); michael@0: navigator.vibrate([3000]); michael@0: logMessage("navigator.vibrate([3000])", "green"); michael@0: }; michael@0: michael@0: //vibrate with a pattern using w3c spec michael@0: var vibrateWithPattern = function() { michael@0: clearLog(); michael@0: navigator.vibrate([1000, 2000, 3000, 2000, 5000]); michael@0: logMessage("navigator.vibrate([1000, 2000, 3000, 2000, 5000])", "green"); michael@0: }; michael@0: michael@0: //cancel existing vibration using w3c spec navigator.vibrate(0) michael@0: var cancelWithZero = function() { michael@0: clearLog(); michael@0: navigator.vibrate(0); michael@0: logMessage("navigator.vibrate(0)", "green"); michael@0: }; michael@0: michael@0: //cancel existing vibration using w3c spec navigator.vibrate([]) michael@0: var cancelWithEmpty = function() { michael@0: clearLog(); michael@0: navigator.vibrate([]); michael@0: logMessage("navigator.vibrate([])", "green"); michael@0: }; michael@0: michael@0: //reference to the timeout variable michael@0: var timeout; michael@0: michael@0: //special long vibrate used to test cancel michael@0: var longVibrate = function() { michael@0: clearLog(); michael@0: navigator.vibrate(60000); michael@0: vibrateOn = true; michael@0: logMessage("navigator.vibrate(60000)", "green"); michael@0: timeout = setTimeout(resetVibrateOn, 60000); //if user doesn't cancel vibrate, reset vibrateOn var after 60 seconds michael@0: }; michael@0: michael@0: //special long vibrate with pattern used to test cancel michael@0: var longVibrateWithPattern = function() { michael@0: clearLog(); michael@0: navigator.vibrate([1000, 2000, 3000, 2000, 5000, 2000, 30000]); michael@0: vibrateOn = true; michael@0: logMessage("navigator.vibrate([1000, 2000, 3000, 2000, 5000, 2000, 30000])", "green"); michael@0: timeout = setTimeout(resetVibrateOn, 45000); //if user doesn't cancel vibrate, reset vibrateOn var after 45 seconds michael@0: }; michael@0: michael@0: //initiate two vibrations to test cancel michael@0: var multipleVibrations = function() { michael@0: clearLog(); michael@0: navigator.vibrate(20000); michael@0: navigator.vibrate(45000); michael@0: vibrateOn = true; michael@0: logMessage("navigator.vibrate(15000)\nnavigator.vibrate(45000)", "green"); michael@0: timeout = setTimeout(resetVibrateOn, 45000); //if user doesn't cancel vibrate, reset vibrateOn var after 45 seconds michael@0: } michael@0: michael@0: function resetVibrateOn() { michael@0: vibrateOn = false; michael@0: } michael@0: michael@0: //check whether there is an ongoing vibration michael@0: var vibrateOn = false; michael@0: michael@0: michael@0: michael@0: michael@0: var vibrate_tests = '

Vibrate Tests

' + michael@0: '

Starred tests only work for Android and Windows.

' + michael@0: '

iOS ignores the time given for a vibrate

' + michael@0: '
' + michael@0: 'Expected result: Vibrate once for 2.5 seconds.' + michael@0: '

' + michael@0: 'Expected result: Pause for 1s, vibrate for 3s, pause for 2s, vibrate for 5s.' + michael@0: '

' + michael@0: 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' + michael@0: '

' + michael@0: 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' + michael@0: '

' + michael@0: 'Expected result: Vibrate once for 3 seconds.' + michael@0: '

' + michael@0: 'Expected result: Vibrate once for 3 seconds.' + michael@0: '

' + michael@0: 'Expected result: Vibrate for 1s, pause for 2s, vibrate for 3s, pause for 2s, vibrate for 5s.' + michael@0: '

' + michael@0: 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' + michael@0: '

' + michael@0: 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' + michael@0: '

' + michael@0: 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' + michael@0: '

' + michael@0: 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' + michael@0: '

' + michael@0: 'Expected result: Press once to initiate two vibrations simultaneously (one for 20s the other for 45s so total of 45s). Press again to cancel both vibrations immediately.'; michael@0: michael@0: michael@0: contentEl.innerHTML = '
' + vibrate_tests; michael@0: michael@0: //standard vibrate with old call michael@0: createActionButton('Vibrate (Old)', function () { michael@0: vibrateOld(); michael@0: }, 'vibrate_old'); michael@0: michael@0: //vibrate with pattern with old call michael@0: createActionButton('* Vibrate with a pattern (Old)', function () { michael@0: vibrateWithPatternOld(); michael@0: }, 'vibrateWithPattern_old'); michael@0: michael@0: //cancel vibrate with old call michael@0: createActionButton('* Cancel vibration (Old)', function() { michael@0: michael@0: if (!vibrateOn) michael@0: { michael@0: longVibrate(); michael@0: } michael@0: else michael@0: { michael@0: cancelOld(); michael@0: resetVibrateOn(); michael@0: clearTimeout(timeout); //clear the timeout since user has canceled the vibrate michael@0: } michael@0: }, 'cancelVibrate_old'); michael@0: michael@0: //cancel vibrate with pattern with old call michael@0: createActionButton('* Cancel vibration with pattern (Old)', function() { michael@0: michael@0: if (!vibrateOn) michael@0: { michael@0: longVibrateWithPattern(); michael@0: } michael@0: else michael@0: { michael@0: cancelOld(); michael@0: resetVibrateOn(); michael@0: clearTimeout(timeout); //clear the timeout since user has canceled the vibrate michael@0: } michael@0: }, 'cancelVibrateWithPattern_old'); michael@0: michael@0: //standard vibrate with new call param int michael@0: createActionButton('Vibrate with int', function() { michael@0: vibrateWithInt(); michael@0: }, 'vibrate_int'); michael@0: michael@0: //standard vibrate with new call param array michael@0: createActionButton('Vibrate with array', function() { michael@0: vibrateWithArray(); michael@0: }, 'vibrate_array'); michael@0: michael@0: //vibrate with a pattern michael@0: createActionButton('* Vibrate with a pattern', function() { michael@0: vibrateWithPattern(); michael@0: }, 'vibrate_with_pattern'); michael@0: michael@0: //cancel any existing vibrations with param 0 michael@0: createActionButton('* Cancel vibration with 0', function() { michael@0: michael@0: if (!vibrateOn) michael@0: { michael@0: longVibrate(); michael@0: } michael@0: else michael@0: { michael@0: cancelWithZero(); michael@0: resetVibrateOn(); michael@0: clearTimeout(timeout); //clear the timeout since user has canceled the vibrate michael@0: } michael@0: }, 'cancel_zero'); michael@0: michael@0: //cancel any existing vibrations with param [] michael@0: createActionButton('* Cancel vibration with []', function() { michael@0: michael@0: if (!vibrateOn) michael@0: { michael@0: longVibrate(); michael@0: } michael@0: else michael@0: { michael@0: cancelWithEmpty(); michael@0: resetVibrateOn(); michael@0: clearTimeout(timeout); //clear the timeout since user has canceled the vibrate michael@0: } michael@0: }, 'cancel_array'); michael@0: michael@0: //cancel vibration with pattern with param 0 michael@0: createActionButton('* Cancel vibration with pattern with 0', function() { michael@0: michael@0: if (!vibrateOn) michael@0: { michael@0: longVibrateWithPattern(); michael@0: } michael@0: else michael@0: { michael@0: cancelWithZero(); michael@0: resetVibrateOn(); michael@0: clearTimeout(timeout); //clear the timeout since user has canceled the vibrate michael@0: } michael@0: }, 'cancelWithPattern_zero'); michael@0: michael@0: //cancel vibration with pattern with param [] michael@0: createActionButton('* Cancel vibration with pattern with []', function() { michael@0: michael@0: if (!vibrateOn) michael@0: { michael@0: longVibrateWithPattern(); michael@0: } michael@0: else michael@0: { michael@0: cancelWithEmpty(); michael@0: resetVibrateOn(); michael@0: clearTimeout(timeout); //clear the timeout since user has canceled the vibrate michael@0: } michael@0: }, 'cancelWithPattern_array'); michael@0: michael@0: //cancel multiple vibrations michael@0: createActionButton('* Cancel multiple vibrations', function() { michael@0: michael@0: if (!vibrateOn) michael@0: { michael@0: multipleVibrations(); michael@0: } michael@0: else michael@0: { michael@0: cancelWithZero(); michael@0: resetVibrateOn(); michael@0: clearTimeout(timeout); //clear the timeout since user has canceled the vibrate michael@0: } michael@0: }, 'cancelMultipleVibrations'); michael@0: };