-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathtests.js
381 lines (342 loc) · 13.9 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/* global cordova */
exports.defineAutoTests = function () {
describe('Notification (navigator.notification)', function () {
it('should exist', function () {
expect(navigator.notification).toBeDefined();
});
it('should contain a beep function', function () {
expect(typeof navigator.notification.beep).toBeDefined();
expect(typeof navigator.notification.beep).toBe('function');
});
it('should contain an alert function', function () {
expect(typeof navigator.notification.alert).toBeDefined();
expect(typeof navigator.notification.alert).toBe('function');
});
it('should contain a confirm function', function () {
expect(typeof navigator.notification.confirm).toBeDefined();
expect(typeof navigator.notification.confirm).toBe('function');
});
it('should contain a prompt function', function () {
expect(typeof navigator.notification.prompt).toBeDefined();
expect(typeof navigator.notification.prompt).toBe('function');
});
it('should contain a dismissPrevious function', function () {
expect(typeof navigator.notification.dismissPrevious).toBeDefined();
expect(typeof navigator.notification.dismissPrevious).toBe('function');
});
it('should contain a dismissAll function', function () {
expect(typeof navigator.notification.dismissAll).toBeDefined();
expect(typeof navigator.notification.dismissAll).toBe('function');
});
});
};
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
exports.defineManualTests = function (contentEl, createActionButton) {
var logMessage = function (message) {
var log = document.getElementById('info');
var logLine = document.createElement('div');
logLine.innerHTML = message;
log.appendChild(logLine);
};
var clearLog = function () {
var log = document.getElementById('info');
log.innerHTML = '';
};
var beep = function () {
console.log('beep()');
navigator.notification.beep(3);
};
var alertDialog = function (message, title, button) {
console.log('alertDialog()');
navigator.notification.alert(
message,
function () {
console.log('Alert dismissed.');
},
title,
button
);
console.log('After alert');
};
var confirmDialogA = function (message, title, buttons) {
clearLog();
navigator.notification.confirm(
message,
function (r) {
if (r === 0) {
logMessage('Dismissed dialog without making a selection.');
console.log('Dismissed dialog without making a selection.');
} else {
console.log('You selected ' + r);
logMessage('You selected ' + buttons.split(',')[r - 1]);
}
},
title,
buttons
);
};
var confirmDialogB = function (message, title, buttons) {
clearLog();
navigator.notification.confirm(
message,
function (r) {
if (r === 0) {
logMessage('Dismissed dialog without making a selection.');
console.log('Dismissed dialog without making a selection.');
} else {
console.log('You selected ' + r);
logMessage('You selected ' + buttons[r - 1]);
}
},
title,
buttons
);
};
var promptDialog = function (message, title, buttons, defaultText) {
clearLog();
navigator.notification.prompt(
message,
function (r) {
if (r && r.buttonIndex === 0) {
var msg = 'Dismissed dialog';
if (r.input1) {
msg += ' with input: ' + r.input1;
}
logMessage(msg);
console.log(msg);
} else {
console.log('You selected ' + r.buttonIndex + ' and entered: ' + r.input1);
logMessage('You selected ' + buttons[r.buttonIndex - 1] + ' and entered: ' + r.input1);
}
},
title,
buttons,
defaultText
);
};
var dismissPrevious = function (successCallback, errorCallback) {
console.log('dismissPrevious()');
navigator.notification.dismissPrevious(successCallback, errorCallback);
};
var dismissAll = function (successCallback, errorCallback) {
console.log('dismissAll()');
navigator.notification.dismissAll(successCallback, errorCallback);
};
/******************************************************************************/
var isRunningOnAndroid = cordova.platformId === 'android';
var isRunningOniOS = cordova.platformId === 'ios';
var dialogs_tests =
'<div id="beep"></div>' +
'Expected result: Device will beep (unless device is on silent). Nothing will get updated in status box.' +
'<h2>Dialog Tests</h2>' +
'<h3>Dialog boxes will pop up for each of the following tests</h3>' +
'<p/> <div id="alert"></div>' +
'Expected result: Dialog will say "You pressed alert". Press continue to close dialog. Nothing will get updated in status box.' +
'<p/> <div id="confirm_deprecated"></div>' +
'Expected result: Dialog will say "You pressed confirm". Press Yes, No, or Maybe to close dialog. Status box will tell you what option you selected.' +
'<p/> <div id="confirm"></div>' +
'Expected result: Dialog will say "You pressed confirm". Press Yes, No, or Maybe, Not Sure to close dialog. Status box will tell you what option you selected, and should use 1-based indexing.' +
'<p/> <div id="prompt"></div>' +
'Expected result: Dialog will say "You pressed prompt". Enter any message and press Yes, No, or Maybe, Not Sure to close dialog. Status box will tell you what option you selected and message you entered or if empty, it will display "Default Text", and should use 1-based indexing.' +
'<p/> <div id="built_in_alert"></div>' +
'Expected result: Dialog will have title "index.html" and say "You pressed alert" Press OK to close dialog. Nothing will get updated in status box.' +
'<p/> <div id="built_in_confirm"></div>' +
'Expected result: Dialog will have title "index.html" and say "You selected confirm". Press Cancel or OK to close dialog. Nothing will get updated in status box.' +
'<p/> <div id="built_in_prompt"></div>' +
'Expected result: Dialog will have title "index.html" and say "This is a prompt". "Default value" will be in text box. Press Cancel or OK to close dialog. Nothing will get updated in status box.' +
'<p/> <h3>CB-8947 Tests</h3><div id="cb8947"></div>' +
'Expected results: Dialogs will not crash iOS';
if (isRunningOnAndroid || isRunningOniOS) {
dialogs_tests += '<h3>Dismissable dialogs</h3>' +
'<p/> <div id="dismiss_previous"></div>' +
'Expected results: 2 dialogs will open; one will automatically dismiss after 5 seconds' +
'<p/> <div id="dismiss_all"></div>' +
'Expected results: 2 dialogs will open; both will automatically dismiss after 5 seconds';
}
contentEl.innerHTML = '<div id="info"></div>' + dialogs_tests;
createActionButton(
'Beep',
function () {
beep();
},
'beep'
);
createActionButton(
'Alert Dialog',
function () {
alertDialog('You pressed alert.', 'Alert Dialog', 'Continue');
},
'alert'
);
// WP8.1 detection is necessary since it doesn't support confirm dialogs with more than 2 buttons
var isRunningOnWP81 = cordova.platformId === 'windows' && navigator.userAgent.indexOf('Windows Phone') > -1;
createActionButton(
'Confirm Dialog - Deprecated',
function () {
var buttons = isRunningOnWP81 ? 'Yes,No' : 'Yes,No,Maybe';
confirmDialogA('You pressed confirm.', 'Confirm Dialog', buttons);
},
'confirm_deprecated'
);
createActionButton(
'Confirm Dialog',
function () {
var buttons = isRunningOnWP81 ? ['Yes', 'Actually, No'] : ['Yes', 'No', 'Maybe, Not Sure'];
confirmDialogB('You pressed confirm.', 'Confirm Dialog', buttons);
},
'confirm'
);
createActionButton(
'Prompt Dialog',
function () {
promptDialog('You pressed prompt.', 'Prompt Dialog', ['Yes', 'No', 'Maybe, Not Sure'], 'Default Text');
},
'prompt'
);
createActionButton(
'Prompt Dialog - no default',
function () {
promptDialog('You pressed prompt.', 'Prompt Dialog', ['Yes', 'No']);
},
'prompt'
);
createActionButton(
'Built-in Alert Dialog',
function () {
if (typeof alert === 'function') {
alert('You pressed alert');
}
},
'built_in_alert'
);
createActionButton(
'Built-in Confirm Dialog',
function () {
if (typeof confirm === 'function') {
confirm('You selected confirm');
}
},
'built_in_confirm'
);
createActionButton(
'Built-in Prompt Dialog',
function () {
if (typeof prompt === 'function') {
prompt('This is a prompt', 'Default value');
}
},
'built_in_prompt'
);
// CB-8947 - ensure number messages don't crash iOS
createActionButton(
'Alert Dialog with Number',
function () {
var callback = function () {
clearLog();
console.log('Test passed');
};
navigator.notification.alert(17, callback);
},
'cb8947'
);
// CB-8947 - ensure object messages don't crash iOS
createActionButton(
'Alert Dialog with Object',
function () {
var object = { number: 42, message: "Make sure an object doesn't crash iOS", issue: 'CB-8947' };
var callback = function () {
clearLog();
console.log('Test passed');
};
navigator.notification.alert(object, callback);
},
'cb8947'
);
// CB-8947 - ensure object messages don't crash iOS
createActionButton(
'Confirm Dialog with Object',
function () {
var object = { number: 42, message: "Make sure an object doesn't crash iOS", issue: 'CB-8947' };
var callback = function () {
clearLog();
console.log('Test passed');
};
navigator.notification.confirm(object, callback);
},
'cb8947'
);
// CB-8947 - ensure object messages don't crash iOS
createActionButton(
'Prompt Dialog with Object',
function () {
var object = { number: 42, message: "Make sure an object doesn't crash iOS", issue: 'CB-8947' };
var callback = function () {
clearLog();
console.log('Test passed');
};
navigator.notification.prompt(object, callback);
},
'cb8947'
);
// Dismissable dialogs (supported on Android & iOS only)
if (isRunningOnAndroid || isRunningOniOS) {
var open2Dialogs = function () {
var openDialogs = function () {
alertDialog('Alert Dialog 1 pressed', 'Alert Dialog 1', 'Continue');
alertDialog('Alert Dialog 2 pressed', 'Alert Dialog 2', 'Continue');
};
// dismiss any currently open dialogs first
dismissAll(openDialogs, openDialogs);
};
createActionButton(
'Dismiss Previous',
function () {
open2Dialogs();
setTimeout(function () {
dismissPrevious(function () {
console.log('Successfully dismissed previous dialog');
}, function (error) {
console.error('Failed to dismiss previous dialog: ' + error);
});
}, 5000);
},
'dismiss_previous'
);
createActionButton(
'Dismiss All',
function () {
open2Dialogs();
setTimeout(function () {
dismissAll(function () {
console.log('Successfully dismissed all open dialogs');
}, function (error) {
console.error('Failed to dismiss all open dialogs: ' + error);
});
}, 5000);
},
'dismiss_all'
);
}
};