UNPKG

8.69 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) {
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);
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, 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
158 contentEl.innerHTML = '<div id="info"></div>' +
159 dialogs_tests;
160
161 createActionButton('Beep', function () {
162 beep();
163 }, 'beep');
164
165 createActionButton('Alert Dialog', function () {
166 alertDialog('You pressed alert.', 'Alert Dialog', 'Continue');
167 }, 'alert');
168
169 // WP8.1 detection is necessary since it doesn't support confirm dialogs with more than 2 buttons
170 var isRunningOnWP81 = cordova.platformId == "windows" && navigator.userAgent.indexOf('Windows Phone') > -1;
171
172 createActionButton('Confirm Dialog - Deprecated', function () {
173 var buttons = isRunningOnWP81 ? 'Yes,No' : 'Yes,No,Maybe';
174 confirmDialogA('You pressed confirm.', 'Confirm Dialog', buttons);
175 }, 'confirm_deprecated');
176
177 createActionButton('Confirm Dialog', function () {
178 var buttons = isRunningOnWP81 ? ['Yes', 'Actually, No'] : ['Yes', 'No', 'Maybe, Not Sure'];
179 confirmDialogB('You pressed confirm.', 'Confirm Dialog', buttons);
180 }, 'confirm');
181
182 createActionButton('Prompt Dialog', function () {
183 promptDialog('You pressed prompt.', 'Prompt Dialog', ['Yes', 'No', 'Maybe, Not Sure']);
184 }, 'prompt');
185
186 createActionButton('Built-in Alert Dialog', function () {
187 if (typeof alert === 'function') {
188 alert('You pressed alert');
189 }
190 }, 'built_in_alert');
191
192 createActionButton('Built-in Confirm Dialog', function () {
193 if (typeof confirm === 'function') {
194 confirm('You selected confirm');
195 }
196 }, 'built_in_confirm');
197
198 createActionButton('Built-in Prompt Dialog', function () {
199 if (typeof prompt === 'function') {
200 prompt('This is a prompt', 'Default value');
201 }
202 }, 'built_in_prompt');
203};