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

changeset 0
e8ccd40d0ef6
equal deleted inserted replaced
-1:000000000000 0:82ac8a627d94
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 */
21
22 exports.defineAutoTests = function () {
23
24 describe('Vibration (navigator.notification.vibrate)', function () {
25 it("navigator.notification should exist", function () {
26 expect(navigator.notification).toBeDefined();
27 });
28
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 };
35
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 }
46
47 var clearLog = function () {
48 var log = document.getElementById('info');
49 log.innerHTML = '';
50 }
51
52 //-------------------------------------------------------------------------
53 // Vibrations
54 //-------------------------------------------------------------------------
55
56 //old vibrate call
57 var vibrateOld = function(){
58 clearLog();
59 navigator.notification.vibrate(2500);
60 logMessage("navigator.notification.vibrate(2500)", "green");
61 };
62
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 };
69
70 //old cancel vibrate call
71 var cancelOld = function(){
72 clearLog();
73 navigator.notification.cancelVibration();
74 logMessage("navigator.notification.cancelVibration()", "green");
75 };
76
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 };
83
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 };
90
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 };
97
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 };
104
105 //cancel existing vibration using w3c spec navigator.vibrate([])
106 var cancelWithEmpty = function() {
107 clearLog();
108 navigator.vibrate([]);
109 logMessage("navigator.vibrate([])", "green");
110 };
111
112 //reference to the timeout variable
113 var timeout;
114
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 };
123
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 };
132
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 }
142
143 function resetVibrateOn() {
144 vibrateOn = false;
145 }
146
147 //check whether there is an ongoing vibration
148 var vibrateOn = false;
149
150
151
152
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.';
180
181
182 contentEl.innerHTML = '<div id="info"></div>' + vibrate_tests;
183
184 //standard vibrate with old call
185 createActionButton('Vibrate (Old)', function () {
186 vibrateOld();
187 }, 'vibrate_old');
188
189 //vibrate with pattern with old call
190 createActionButton('* Vibrate with a pattern (Old)', function () {
191 vibrateWithPatternOld();
192 }, 'vibrateWithPattern_old');
193
194 //cancel vibrate with old call
195 createActionButton('* Cancel vibration (Old)', function() {
196
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');
208
209 //cancel vibrate with pattern with old call
210 createActionButton('* Cancel vibration with pattern (Old)', function() {
211
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');
223
224 //standard vibrate with new call param int
225 createActionButton('Vibrate with int', function() {
226 vibrateWithInt();
227 }, 'vibrate_int');
228
229 //standard vibrate with new call param array
230 createActionButton('Vibrate with array', function() {
231 vibrateWithArray();
232 }, 'vibrate_array');
233
234 //vibrate with a pattern
235 createActionButton('* Vibrate with a pattern', function() {
236 vibrateWithPattern();
237 }, 'vibrate_with_pattern');
238
239 //cancel any existing vibrations with param 0
240 createActionButton('* Cancel vibration with 0', function() {
241
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');
253
254 //cancel any existing vibrations with param []
255 createActionButton('* Cancel vibration with []', function() {
256
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');
268
269 //cancel vibration with pattern with param 0
270 createActionButton('* Cancel vibration with pattern with 0', function() {
271
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');
283
284 //cancel vibration with pattern with param []
285 createActionButton('* Cancel vibration with pattern with []', function() {
286
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');
298
299 //cancel multiple vibrations
300 createActionButton('* Cancel multiple vibrations', function() {
301
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