1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Touchgui/plugins/org.apache.cordova.vibration/tests/tests.js Thu Jun 04 14:50:33 2015 +0200 1.3 @@ -0,0 +1,313 @@ 1.4 +/* 1.5 + * 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * 1.23 + */ 1.24 + 1.25 +exports.defineAutoTests = function () { 1.26 + 1.27 + describe('Vibration (navigator.notification.vibrate)', function () { 1.28 + it("navigator.notification should exist", function () { 1.29 + expect(navigator.notification).toBeDefined(); 1.30 + }); 1.31 + 1.32 + it("should contain a vibrate function", function () { 1.33 + expect(typeof navigator.notification.vibrate).toBeDefined(); 1.34 + expect(typeof navigator.notification.vibrate).toBe("function"); 1.35 + }); 1.36 + }); 1.37 +}; 1.38 + 1.39 +exports.defineManualTests = function (contentEl, createActionButton) { 1.40 + var logMessage = function (message, color) { 1.41 + var log = document.getElementById('info'); 1.42 + var logLine = document.createElement('div'); 1.43 + if (color) { 1.44 + logLine.style.color = color; 1.45 + } 1.46 + logLine.innerHTML = message; 1.47 + log.appendChild(logLine); 1.48 + } 1.49 + 1.50 + var clearLog = function () { 1.51 + var log = document.getElementById('info'); 1.52 + log.innerHTML = ''; 1.53 + } 1.54 + 1.55 + //------------------------------------------------------------------------- 1.56 + // Vibrations 1.57 + //------------------------------------------------------------------------- 1.58 + 1.59 + //old vibrate call 1.60 + var vibrateOld = function(){ 1.61 + clearLog(); 1.62 + navigator.notification.vibrate(2500); 1.63 + logMessage("navigator.notification.vibrate(2500)", "green"); 1.64 + }; 1.65 + 1.66 + //old vibrate with pattern call 1.67 + var vibrateWithPatternOld = function(){ 1.68 + clearLog(); 1.69 + navigator.notification.vibrateWithPattern([1000, 3000, 2000, 5000]); 1.70 + logMessage("navigator.notification.vibrateWithPattern([1000, 3000, 2000, 5000])", "green"); 1.71 + }; 1.72 + 1.73 + //old cancel vibrate call 1.74 + var cancelOld = function(){ 1.75 + clearLog(); 1.76 + navigator.notification.cancelVibration(); 1.77 + logMessage("navigator.notification.cancelVibration()", "green"); 1.78 + }; 1.79 + 1.80 + //new standard vibrate call that aligns to w3c spec with param long 1.81 + var vibrateWithInt = function() { 1.82 + clearLog(); 1.83 + navigator.vibrate(3000); 1.84 + logMessage("navigator.vibrate(3000)", "green"); 1.85 + }; 1.86 + 1.87 + //new standard vibrate call that aligns to w3c spec with param array 1.88 + var vibrateWithArray = function() { 1.89 + clearLog(); 1.90 + navigator.vibrate([3000]); 1.91 + logMessage("navigator.vibrate([3000])", "green"); 1.92 + }; 1.93 + 1.94 + //vibrate with a pattern using w3c spec 1.95 + var vibrateWithPattern = function() { 1.96 + clearLog(); 1.97 + navigator.vibrate([1000, 2000, 3000, 2000, 5000]); 1.98 + logMessage("navigator.vibrate([1000, 2000, 3000, 2000, 5000])", "green"); 1.99 + }; 1.100 + 1.101 + //cancel existing vibration using w3c spec navigator.vibrate(0) 1.102 + var cancelWithZero = function() { 1.103 + clearLog(); 1.104 + navigator.vibrate(0); 1.105 + logMessage("navigator.vibrate(0)", "green"); 1.106 + }; 1.107 + 1.108 + //cancel existing vibration using w3c spec navigator.vibrate([]) 1.109 + var cancelWithEmpty = function() { 1.110 + clearLog(); 1.111 + navigator.vibrate([]); 1.112 + logMessage("navigator.vibrate([])", "green"); 1.113 + }; 1.114 + 1.115 + //reference to the timeout variable 1.116 + var timeout; 1.117 + 1.118 + //special long vibrate used to test cancel 1.119 + var longVibrate = function() { 1.120 + clearLog(); 1.121 + navigator.vibrate(60000); 1.122 + vibrateOn = true; 1.123 + logMessage("navigator.vibrate(60000)", "green"); 1.124 + timeout = setTimeout(resetVibrateOn, 60000); //if user doesn't cancel vibrate, reset vibrateOn var after 60 seconds 1.125 + }; 1.126 + 1.127 + //special long vibrate with pattern used to test cancel 1.128 + var longVibrateWithPattern = function() { 1.129 + clearLog(); 1.130 + navigator.vibrate([1000, 2000, 3000, 2000, 5000, 2000, 30000]); 1.131 + vibrateOn = true; 1.132 + logMessage("navigator.vibrate([1000, 2000, 3000, 2000, 5000, 2000, 30000])", "green"); 1.133 + timeout = setTimeout(resetVibrateOn, 45000); //if user doesn't cancel vibrate, reset vibrateOn var after 45 seconds 1.134 + }; 1.135 + 1.136 + //initiate two vibrations to test cancel 1.137 + var multipleVibrations = function() { 1.138 + clearLog(); 1.139 + navigator.vibrate(20000); 1.140 + navigator.vibrate(45000); 1.141 + vibrateOn = true; 1.142 + logMessage("navigator.vibrate(15000)\nnavigator.vibrate(45000)", "green"); 1.143 + timeout = setTimeout(resetVibrateOn, 45000); //if user doesn't cancel vibrate, reset vibrateOn var after 45 seconds 1.144 + } 1.145 + 1.146 + function resetVibrateOn() { 1.147 + vibrateOn = false; 1.148 + } 1.149 + 1.150 + //check whether there is an ongoing vibration 1.151 + var vibrateOn = false; 1.152 + 1.153 + 1.154 + 1.155 + 1.156 + var vibrate_tests = '<h1>Vibrate Tests</h1>' + 1.157 + '<h3>Starred tests only work for Android and Windows. </h3>' + 1.158 + '<h3>iOS ignores the time given for a vibrate </h3>' + 1.159 + '<div id="vibrate_old"></div>' + 1.160 + 'Expected result: Vibrate once for 2.5 seconds.' + 1.161 + '<p/> <div id="vibrateWithPattern_old"></div>' + 1.162 + 'Expected result: Pause for 1s, vibrate for 3s, pause for 2s, vibrate for 5s.' + 1.163 + '<p/> <div id="cancelVibrate_old"></div>' + 1.164 + 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' + 1.165 + '<p/> <div id="cancelVibrateWithPattern_old"></div>' + 1.166 + 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' + 1.167 + '<p/> <div id="vibrate_int"></div>' + 1.168 + 'Expected result: Vibrate once for 3 seconds.' + 1.169 + '<p/> <div id="vibrate_array"></div>' + 1.170 + 'Expected result: Vibrate once for 3 seconds.' + 1.171 + '<p/> <div id="vibrate_with_pattern"></div>' + 1.172 + 'Expected result: Vibrate for 1s, pause for 2s, vibrate for 3s, pause for 2s, vibrate for 5s.' + 1.173 + '<p/> <div id="cancel_zero"></div>' + 1.174 + 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' + 1.175 + '<p/><div id="cancel_array"></div>' + 1.176 + 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' + 1.177 + '<p/> <div id="cancelWithPattern_zero"></div>' + 1.178 + 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' + 1.179 + '<p/> <div id="cancelWithPattern_array"></div>' + 1.180 + 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' + 1.181 + '<p/> <div id="cancelMultipleVibrations"></div>' + 1.182 + '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.'; 1.183 + 1.184 + 1.185 + contentEl.innerHTML = '<div id="info"></div>' + vibrate_tests; 1.186 + 1.187 + //standard vibrate with old call 1.188 + createActionButton('Vibrate (Old)', function () { 1.189 + vibrateOld(); 1.190 + }, 'vibrate_old'); 1.191 + 1.192 + //vibrate with pattern with old call 1.193 + createActionButton('* Vibrate with a pattern (Old)', function () { 1.194 + vibrateWithPatternOld(); 1.195 + }, 'vibrateWithPattern_old'); 1.196 + 1.197 + //cancel vibrate with old call 1.198 + createActionButton('* Cancel vibration (Old)', function() { 1.199 + 1.200 + if (!vibrateOn) 1.201 + { 1.202 + longVibrate(); 1.203 + } 1.204 + else 1.205 + { 1.206 + cancelOld(); 1.207 + resetVibrateOn(); 1.208 + clearTimeout(timeout); //clear the timeout since user has canceled the vibrate 1.209 + } 1.210 + }, 'cancelVibrate_old'); 1.211 + 1.212 + //cancel vibrate with pattern with old call 1.213 + createActionButton('* Cancel vibration with pattern (Old)', function() { 1.214 + 1.215 + if (!vibrateOn) 1.216 + { 1.217 + longVibrateWithPattern(); 1.218 + } 1.219 + else 1.220 + { 1.221 + cancelOld(); 1.222 + resetVibrateOn(); 1.223 + clearTimeout(timeout); //clear the timeout since user has canceled the vibrate 1.224 + } 1.225 + }, 'cancelVibrateWithPattern_old'); 1.226 + 1.227 + //standard vibrate with new call param int 1.228 + createActionButton('Vibrate with int', function() { 1.229 + vibrateWithInt(); 1.230 + }, 'vibrate_int'); 1.231 + 1.232 + //standard vibrate with new call param array 1.233 + createActionButton('Vibrate with array', function() { 1.234 + vibrateWithArray(); 1.235 + }, 'vibrate_array'); 1.236 + 1.237 + //vibrate with a pattern 1.238 + createActionButton('* Vibrate with a pattern', function() { 1.239 + vibrateWithPattern(); 1.240 + }, 'vibrate_with_pattern'); 1.241 + 1.242 + //cancel any existing vibrations with param 0 1.243 + createActionButton('* Cancel vibration with 0', function() { 1.244 + 1.245 + if (!vibrateOn) 1.246 + { 1.247 + longVibrate(); 1.248 + } 1.249 + else 1.250 + { 1.251 + cancelWithZero(); 1.252 + resetVibrateOn(); 1.253 + clearTimeout(timeout); //clear the timeout since user has canceled the vibrate 1.254 + } 1.255 + }, 'cancel_zero'); 1.256 + 1.257 + //cancel any existing vibrations with param [] 1.258 + createActionButton('* Cancel vibration with []', function() { 1.259 + 1.260 + if (!vibrateOn) 1.261 + { 1.262 + longVibrate(); 1.263 + } 1.264 + else 1.265 + { 1.266 + cancelWithEmpty(); 1.267 + resetVibrateOn(); 1.268 + clearTimeout(timeout); //clear the timeout since user has canceled the vibrate 1.269 + } 1.270 + }, 'cancel_array'); 1.271 + 1.272 + //cancel vibration with pattern with param 0 1.273 + createActionButton('* Cancel vibration with pattern with 0', function() { 1.274 + 1.275 + if (!vibrateOn) 1.276 + { 1.277 + longVibrateWithPattern(); 1.278 + } 1.279 + else 1.280 + { 1.281 + cancelWithZero(); 1.282 + resetVibrateOn(); 1.283 + clearTimeout(timeout); //clear the timeout since user has canceled the vibrate 1.284 + } 1.285 + }, 'cancelWithPattern_zero'); 1.286 + 1.287 + //cancel vibration with pattern with param [] 1.288 + createActionButton('* Cancel vibration with pattern with []', function() { 1.289 + 1.290 + if (!vibrateOn) 1.291 + { 1.292 + longVibrateWithPattern(); 1.293 + } 1.294 + else 1.295 + { 1.296 + cancelWithEmpty(); 1.297 + resetVibrateOn(); 1.298 + clearTimeout(timeout); //clear the timeout since user has canceled the vibrate 1.299 + } 1.300 + }, 'cancelWithPattern_array'); 1.301 + 1.302 + //cancel multiple vibrations 1.303 + createActionButton('* Cancel multiple vibrations', function() { 1.304 + 1.305 + if (!vibrateOn) 1.306 + { 1.307 + multipleVibrations(); 1.308 + } 1.309 + else 1.310 + { 1.311 + cancelWithZero(); 1.312 + resetVibrateOn(); 1.313 + clearTimeout(timeout); //clear the timeout since user has canceled the vibrate 1.314 + } 1.315 + }, 'cancelMultipleVibrations'); 1.316 +};