Touchgui/plugins/org.apache.cordova.vibration/tests/tests.js

Thu, 04 Jun 2015 14:50:33 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 04 Jun 2015 14:50:33 +0200
changeset 0
e8ccd40d0ef6
permissions
-rw-r--r--

Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.

michael@0 1 /*
michael@0 2 *
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 *
michael@0 20 */
michael@0 21
michael@0 22 exports.defineAutoTests = function () {
michael@0 23
michael@0 24 describe('Vibration (navigator.notification.vibrate)', function () {
michael@0 25 it("navigator.notification should exist", function () {
michael@0 26 expect(navigator.notification).toBeDefined();
michael@0 27 });
michael@0 28
michael@0 29 it("should contain a vibrate function", function () {
michael@0 30 expect(typeof navigator.notification.vibrate).toBeDefined();
michael@0 31 expect(typeof navigator.notification.vibrate).toBe("function");
michael@0 32 });
michael@0 33 });
michael@0 34 };
michael@0 35
michael@0 36 exports.defineManualTests = function (contentEl, createActionButton) {
michael@0 37 var logMessage = function (message, color) {
michael@0 38 var log = document.getElementById('info');
michael@0 39 var logLine = document.createElement('div');
michael@0 40 if (color) {
michael@0 41 logLine.style.color = color;
michael@0 42 }
michael@0 43 logLine.innerHTML = message;
michael@0 44 log.appendChild(logLine);
michael@0 45 }
michael@0 46
michael@0 47 var clearLog = function () {
michael@0 48 var log = document.getElementById('info');
michael@0 49 log.innerHTML = '';
michael@0 50 }
michael@0 51
michael@0 52 //-------------------------------------------------------------------------
michael@0 53 // Vibrations
michael@0 54 //-------------------------------------------------------------------------
michael@0 55
michael@0 56 //old vibrate call
michael@0 57 var vibrateOld = function(){
michael@0 58 clearLog();
michael@0 59 navigator.notification.vibrate(2500);
michael@0 60 logMessage("navigator.notification.vibrate(2500)", "green");
michael@0 61 };
michael@0 62
michael@0 63 //old vibrate with pattern call
michael@0 64 var vibrateWithPatternOld = function(){
michael@0 65 clearLog();
michael@0 66 navigator.notification.vibrateWithPattern([1000, 3000, 2000, 5000]);
michael@0 67 logMessage("navigator.notification.vibrateWithPattern([1000, 3000, 2000, 5000])", "green");
michael@0 68 };
michael@0 69
michael@0 70 //old cancel vibrate call
michael@0 71 var cancelOld = function(){
michael@0 72 clearLog();
michael@0 73 navigator.notification.cancelVibration();
michael@0 74 logMessage("navigator.notification.cancelVibration()", "green");
michael@0 75 };
michael@0 76
michael@0 77 //new standard vibrate call that aligns to w3c spec with param long
michael@0 78 var vibrateWithInt = function() {
michael@0 79 clearLog();
michael@0 80 navigator.vibrate(3000);
michael@0 81 logMessage("navigator.vibrate(3000)", "green");
michael@0 82 };
michael@0 83
michael@0 84 //new standard vibrate call that aligns to w3c spec with param array
michael@0 85 var vibrateWithArray = function() {
michael@0 86 clearLog();
michael@0 87 navigator.vibrate([3000]);
michael@0 88 logMessage("navigator.vibrate([3000])", "green");
michael@0 89 };
michael@0 90
michael@0 91 //vibrate with a pattern using w3c spec
michael@0 92 var vibrateWithPattern = function() {
michael@0 93 clearLog();
michael@0 94 navigator.vibrate([1000, 2000, 3000, 2000, 5000]);
michael@0 95 logMessage("navigator.vibrate([1000, 2000, 3000, 2000, 5000])", "green");
michael@0 96 };
michael@0 97
michael@0 98 //cancel existing vibration using w3c spec navigator.vibrate(0)
michael@0 99 var cancelWithZero = function() {
michael@0 100 clearLog();
michael@0 101 navigator.vibrate(0);
michael@0 102 logMessage("navigator.vibrate(0)", "green");
michael@0 103 };
michael@0 104
michael@0 105 //cancel existing vibration using w3c spec navigator.vibrate([])
michael@0 106 var cancelWithEmpty = function() {
michael@0 107 clearLog();
michael@0 108 navigator.vibrate([]);
michael@0 109 logMessage("navigator.vibrate([])", "green");
michael@0 110 };
michael@0 111
michael@0 112 //reference to the timeout variable
michael@0 113 var timeout;
michael@0 114
michael@0 115 //special long vibrate used to test cancel
michael@0 116 var longVibrate = function() {
michael@0 117 clearLog();
michael@0 118 navigator.vibrate(60000);
michael@0 119 vibrateOn = true;
michael@0 120 logMessage("navigator.vibrate(60000)", "green");
michael@0 121 timeout = setTimeout(resetVibrateOn, 60000); //if user doesn't cancel vibrate, reset vibrateOn var after 60 seconds
michael@0 122 };
michael@0 123
michael@0 124 //special long vibrate with pattern used to test cancel
michael@0 125 var longVibrateWithPattern = function() {
michael@0 126 clearLog();
michael@0 127 navigator.vibrate([1000, 2000, 3000, 2000, 5000, 2000, 30000]);
michael@0 128 vibrateOn = true;
michael@0 129 logMessage("navigator.vibrate([1000, 2000, 3000, 2000, 5000, 2000, 30000])", "green");
michael@0 130 timeout = setTimeout(resetVibrateOn, 45000); //if user doesn't cancel vibrate, reset vibrateOn var after 45 seconds
michael@0 131 };
michael@0 132
michael@0 133 //initiate two vibrations to test cancel
michael@0 134 var multipleVibrations = function() {
michael@0 135 clearLog();
michael@0 136 navigator.vibrate(20000);
michael@0 137 navigator.vibrate(45000);
michael@0 138 vibrateOn = true;
michael@0 139 logMessage("navigator.vibrate(15000)\nnavigator.vibrate(45000)", "green");
michael@0 140 timeout = setTimeout(resetVibrateOn, 45000); //if user doesn't cancel vibrate, reset vibrateOn var after 45 seconds
michael@0 141 }
michael@0 142
michael@0 143 function resetVibrateOn() {
michael@0 144 vibrateOn = false;
michael@0 145 }
michael@0 146
michael@0 147 //check whether there is an ongoing vibration
michael@0 148 var vibrateOn = false;
michael@0 149
michael@0 150
michael@0 151
michael@0 152
michael@0 153 var vibrate_tests = '<h1>Vibrate Tests</h1>' +
michael@0 154 '<h3>Starred tests only work for Android and Windows. </h3>' +
michael@0 155 '<h3>iOS ignores the time given for a vibrate </h3>' +
michael@0 156 '<div id="vibrate_old"></div>' +
michael@0 157 'Expected result: Vibrate once for 2.5 seconds.' +
michael@0 158 '<p/> <div id="vibrateWithPattern_old"></div>' +
michael@0 159 'Expected result: Pause for 1s, vibrate for 3s, pause for 2s, vibrate for 5s.' +
michael@0 160 '<p/> <div id="cancelVibrate_old"></div>' +
michael@0 161 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' +
michael@0 162 '<p/> <div id="cancelVibrateWithPattern_old"></div>' +
michael@0 163 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' +
michael@0 164 '<p/> <div id="vibrate_int"></div>' +
michael@0 165 'Expected result: Vibrate once for 3 seconds.' +
michael@0 166 '<p/> <div id="vibrate_array"></div>' +
michael@0 167 'Expected result: Vibrate once for 3 seconds.' +
michael@0 168 '<p/> <div id="vibrate_with_pattern"></div>' +
michael@0 169 'Expected result: Vibrate for 1s, pause for 2s, vibrate for 3s, pause for 2s, vibrate for 5s.' +
michael@0 170 '<p/> <div id="cancel_zero"></div>' +
michael@0 171 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' +
michael@0 172 '<p/><div id="cancel_array"></div>' +
michael@0 173 'Expected result: Press once to initiate vibrate for 60 seconds. Press again to cancel vibrate immediately.' +
michael@0 174 '<p/> <div id="cancelWithPattern_zero"></div>' +
michael@0 175 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' +
michael@0 176 '<p/> <div id="cancelWithPattern_array"></div>' +
michael@0 177 'Expected result: Press once to initiate vibrate with pattern for 45s. Press again to cancel vibrate immediately.' +
michael@0 178 '<p/> <div id="cancelMultipleVibrations"></div>' +
michael@0 179 '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 180
michael@0 181
michael@0 182 contentEl.innerHTML = '<div id="info"></div>' + vibrate_tests;
michael@0 183
michael@0 184 //standard vibrate with old call
michael@0 185 createActionButton('Vibrate (Old)', function () {
michael@0 186 vibrateOld();
michael@0 187 }, 'vibrate_old');
michael@0 188
michael@0 189 //vibrate with pattern with old call
michael@0 190 createActionButton('* Vibrate with a pattern (Old)', function () {
michael@0 191 vibrateWithPatternOld();
michael@0 192 }, 'vibrateWithPattern_old');
michael@0 193
michael@0 194 //cancel vibrate with old call
michael@0 195 createActionButton('* Cancel vibration (Old)', function() {
michael@0 196
michael@0 197 if (!vibrateOn)
michael@0 198 {
michael@0 199 longVibrate();
michael@0 200 }
michael@0 201 else
michael@0 202 {
michael@0 203 cancelOld();
michael@0 204 resetVibrateOn();
michael@0 205 clearTimeout(timeout); //clear the timeout since user has canceled the vibrate
michael@0 206 }
michael@0 207 }, 'cancelVibrate_old');
michael@0 208
michael@0 209 //cancel vibrate with pattern with old call
michael@0 210 createActionButton('* Cancel vibration with pattern (Old)', function() {
michael@0 211
michael@0 212 if (!vibrateOn)
michael@0 213 {
michael@0 214 longVibrateWithPattern();
michael@0 215 }
michael@0 216 else
michael@0 217 {
michael@0 218 cancelOld();
michael@0 219 resetVibrateOn();
michael@0 220 clearTimeout(timeout); //clear the timeout since user has canceled the vibrate
michael@0 221 }
michael@0 222 }, 'cancelVibrateWithPattern_old');
michael@0 223
michael@0 224 //standard vibrate with new call param int
michael@0 225 createActionButton('Vibrate with int', function() {
michael@0 226 vibrateWithInt();
michael@0 227 }, 'vibrate_int');
michael@0 228
michael@0 229 //standard vibrate with new call param array
michael@0 230 createActionButton('Vibrate with array', function() {
michael@0 231 vibrateWithArray();
michael@0 232 }, 'vibrate_array');
michael@0 233
michael@0 234 //vibrate with a pattern
michael@0 235 createActionButton('* Vibrate with a pattern', function() {
michael@0 236 vibrateWithPattern();
michael@0 237 }, 'vibrate_with_pattern');
michael@0 238
michael@0 239 //cancel any existing vibrations with param 0
michael@0 240 createActionButton('* Cancel vibration with 0', function() {
michael@0 241
michael@0 242 if (!vibrateOn)
michael@0 243 {
michael@0 244 longVibrate();
michael@0 245 }
michael@0 246 else
michael@0 247 {
michael@0 248 cancelWithZero();
michael@0 249 resetVibrateOn();
michael@0 250 clearTimeout(timeout); //clear the timeout since user has canceled the vibrate
michael@0 251 }
michael@0 252 }, 'cancel_zero');
michael@0 253
michael@0 254 //cancel any existing vibrations with param []
michael@0 255 createActionButton('* Cancel vibration with []', function() {
michael@0 256
michael@0 257 if (!vibrateOn)
michael@0 258 {
michael@0 259 longVibrate();
michael@0 260 }
michael@0 261 else
michael@0 262 {
michael@0 263 cancelWithEmpty();
michael@0 264 resetVibrateOn();
michael@0 265 clearTimeout(timeout); //clear the timeout since user has canceled the vibrate
michael@0 266 }
michael@0 267 }, 'cancel_array');
michael@0 268
michael@0 269 //cancel vibration with pattern with param 0
michael@0 270 createActionButton('* Cancel vibration with pattern with 0', function() {
michael@0 271
michael@0 272 if (!vibrateOn)
michael@0 273 {
michael@0 274 longVibrateWithPattern();
michael@0 275 }
michael@0 276 else
michael@0 277 {
michael@0 278 cancelWithZero();
michael@0 279 resetVibrateOn();
michael@0 280 clearTimeout(timeout); //clear the timeout since user has canceled the vibrate
michael@0 281 }
michael@0 282 }, 'cancelWithPattern_zero');
michael@0 283
michael@0 284 //cancel vibration with pattern with param []
michael@0 285 createActionButton('* Cancel vibration with pattern with []', function() {
michael@0 286
michael@0 287 if (!vibrateOn)
michael@0 288 {
michael@0 289 longVibrateWithPattern();
michael@0 290 }
michael@0 291 else
michael@0 292 {
michael@0 293 cancelWithEmpty();
michael@0 294 resetVibrateOn();
michael@0 295 clearTimeout(timeout); //clear the timeout since user has canceled the vibrate
michael@0 296 }
michael@0 297 }, 'cancelWithPattern_array');
michael@0 298
michael@0 299 //cancel multiple vibrations
michael@0 300 createActionButton('* Cancel multiple vibrations', function() {
michael@0 301
michael@0 302 if (!vibrateOn)
michael@0 303 {
michael@0 304 multipleVibrations();
michael@0 305 }
michael@0 306 else
michael@0 307 {
michael@0 308 cancelWithZero();
michael@0 309 resetVibrateOn();
michael@0 310 clearTimeout(timeout); //clear the timeout since user has canceled the vibrate
michael@0 311 }
michael@0 312 }, 'cancelMultipleVibrations');
michael@0 313 };

mercurial