UNPKG

10.3 kBJavaScriptView Raw
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/* jshint jasmine: true */
23/* global cordova */
24
25exports.defineAutoTests = function () {
26 describe('Notification (navigator.notification)', function () {
27 it("should exist", function () {
28 expect(navigator.notification).toBeDefined();
29 });
30
31 it("should contain a beep function", function () {
32 expect(typeof navigator.notification.beep).toBeDefined();
33 expect(typeof navigator.notification.beep).toBe("function");
34 });
35
36 it("should contain an alert function", function () {
37 expect(typeof navigator.notification.alert).toBeDefined();
38 expect(typeof navigator.notification.alert).toBe("function");
39 });
40
41 it("should contain a confirm function", function () {
42 expect(typeof navigator.notification.confirm).toBeDefined();
43 expect(typeof navigator.notification.confirm).toBe("function");
44 });
45
46 it("should contain a prompt function", function () {
47 expect(typeof navigator.notification.prompt).toBeDefined();
48 expect(typeof navigator.notification.prompt).toBe("function");
49 });
50 });
51};
52
53/******************************************************************************/
54/******************************************************************************/
55/******************************************************************************/
56
57exports.defineManualTests = function (contentEl, createActionButton) {
58 var logMessage = function (message) {
59 var log = document.getElementById('info');
60 var logLine = document.createElement('div');
61 logLine.innerHTML = message;
62 log.appendChild(logLine);
63 };
64
65 var clearLog = function () {
66 var log = document.getElementById('info');
67 log.innerHTML = '';
68 };
69
70 var beep = function () {
71 console.log("beep()");
72 navigator.notification.beep(3);
73 };
74
75 var alertDialog = function (message, title, button) {
76 console.log("alertDialog()");
77 navigator.notification.alert(message,
78 function () {
79 console.log("Alert dismissed.");
80 },
81 title, button);
82 console.log("After alert");
83 };
84
85 var confirmDialogA = function (message, title, buttons) {
86 clearLog();
87 navigator.notification.confirm(message,
88 function (r) {
89 if (r === 0) {
90 logMessage("Dismissed dialog without making a selection.");
91 console.log("Dismissed dialog without making a selection.");
92 } else {
93 console.log("You selected " + r);
94 logMessage("You selected " + (buttons.split(","))[r - 1]);
95 }
96 },
97 title,
98 buttons);
99 };
100
101 var confirmDialogB = function (message, title, buttons) {
102 clearLog();
103 navigator.notification.confirm(message,
104 function (r) {
105 if (r === 0) {
106 logMessage("Dismissed dialog without making a selection.");
107 console.log("Dismissed dialog without making a selection.");
108 } else {
109 console.log("You selected " + r);
110 logMessage("You selected " + buttons[r - 1]);
111 }
112 },
113 title,
114 buttons);
115 };
116
117 var promptDialog = function (message, title, buttons,defaultText) {
118 clearLog();
119 navigator.notification.prompt(message,
120 function (r) {
121 if (r && r.buttonIndex === 0) {
122 var msg = "Dismissed dialog";
123 if (r.input1) {
124 msg += " with input: " + r.input1;
125 }
126 logMessage(msg);
127 console.log(msg);
128 } else {
129 console.log("You selected " + r.buttonIndex + " and entered: " + r.input1);
130 logMessage("You selected " + buttons[r.buttonIndex - 1] + " and entered: " + r.input1);
131 }
132 },
133 title,
134 buttons,defaultText);
135 };
136
137 /******************************************************************************/
138
139 var dialogs_tests = '<div id="beep"></div>' +
140 'Expected result: Device will beep (unless device is on silent). Nothing will get updated in status box.' +
141 '<h2>Dialog Tests</h2>' +
142 '<h3>Dialog boxes will pop up for each of the following tests</h3>' +
143 '<p/> <div id="alert"></div>' +
144 'Expected result: Dialog will say "You pressed alert". Press continue to close dialog. Nothing will get updated in status box.' +
145 '<p/> <div id="confirm_deprecated"></div>' +
146 '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.' +
147 '<p/> <div id="confirm"></div>' +
148 '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.' +
149 '<p/> <div id="prompt"></div>' +
150 '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.' +
151 '<p/> <div id="built_in_alert"></div>' +
152 '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.' +
153 '<p/> <div id="built_in_confirm"></div>' +
154 '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.' +
155 '<p/> <div id="built_in_prompt"></div>' +
156 '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.' +
157 '<p/> <h3>CB-8947 Tests</h3><div id="cb8947"></div>' +
158 'Expected results: Dialogs will not crash iOS';
159
160 contentEl.innerHTML = '<div id="info"></div>' +
161 dialogs_tests;
162
163 createActionButton('Beep', function () {
164 beep();
165 }, 'beep');
166
167 createActionButton('Alert Dialog', function () {
168 alertDialog('You pressed alert.', 'Alert Dialog', 'Continue');
169 }, 'alert');
170
171 // WP8.1 detection is necessary since it doesn't support confirm dialogs with more than 2 buttons
172 var isRunningOnWP81 = cordova.platformId == "windows" && navigator.userAgent.indexOf('Windows Phone') > -1;
173
174 createActionButton('Confirm Dialog - Deprecated', function () {
175 var buttons = isRunningOnWP81 ? 'Yes,No' : 'Yes,No,Maybe';
176 confirmDialogA('You pressed confirm.', 'Confirm Dialog', buttons);
177 }, 'confirm_deprecated');
178
179 createActionButton('Confirm Dialog', function () {
180 var buttons = isRunningOnWP81 ? ['Yes', 'Actually, No'] : ['Yes', 'No', 'Maybe, Not Sure'];
181 confirmDialogB('You pressed confirm.', 'Confirm Dialog', buttons);
182 }, 'confirm');
183
184 createActionButton('Prompt Dialog', function () {
185 promptDialog('You pressed prompt.', 'Prompt Dialog', ['Yes', 'No', 'Maybe, Not Sure'],'Default Text');
186 }, 'prompt');
187
188 createActionButton('Built-in Alert Dialog', function () {
189 if (typeof alert === 'function') {
190 alert('You pressed alert');
191 }
192 }, 'built_in_alert');
193
194 createActionButton('Built-in Confirm Dialog', function () {
195 if (typeof confirm === 'function') {
196 confirm('You selected confirm');
197 }
198 }, 'built_in_confirm');
199
200 createActionButton('Built-in Prompt Dialog', function () {
201 if (typeof prompt === 'function') {
202 prompt('This is a prompt', 'Default value');
203 }
204 }, 'built_in_prompt');
205
206 // CB-8947 - ensure number messages don't crash iOS
207 createActionButton('Alert Dialog with Number', function () {
208 var callback = function() { clearLog(); console.log("Test passed"); };
209 navigator.notification.alert(17, callback);
210 }, 'cb8947');
211
212 // CB-8947 - ensure object messages don't crash iOS
213 createActionButton('Alert Dialog with Object', function () {
214 var object = { number: 42, message: "Make sure an object doesn't crash iOS", issue: 'CB-8947'};
215 var callback = function() { clearLog(); console.log("Test passed"); };
216 navigator.notification.alert(object, callback);
217 }, 'cb8947');
218
219 // CB-8947 - ensure object messages don't crash iOS
220 createActionButton('Confirm Dialog with Object', function () {
221 var object = { number: 42, message: "Make sure an object doesn't crash iOS", issue: 'CB-8947'};
222 var callback = function() { clearLog(); console.log("Test passed"); };
223 navigator.notification.confirm(object, callback);
224 }, 'cb8947');
225
226 // CB-8947 - ensure object messages don't crash iOS
227 createActionButton('Prompt Dialog with Object', function () {
228 var object = { number: 42, message: "Make sure an object doesn't crash iOS", issue: 'CB-8947'};
229 var callback = function() { clearLog(); console.log("Test passed"); };
230 navigator.notification.prompt(object, callback);
231 }, 'cb8947');
232
233};