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.

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

mercurial