Touchgui/plugins/org.apache.cordova.dialogs/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.

michael@0 1 /*
michael@0 2 *
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 *
michael@0 20 */
michael@0 21
michael@0 22 exports.defineAutoTests = function () {
michael@0 23 describe('Notification (navigator.notification)', function () {
michael@0 24 it("should exist", function () {
michael@0 25 expect(navigator.notification).toBeDefined();
michael@0 26 });
michael@0 27
michael@0 28 it("should contain a beep function", function () {
michael@0 29 expect(typeof navigator.notification.beep).toBeDefined();
michael@0 30 expect(typeof navigator.notification.beep).toBe("function");
michael@0 31 });
michael@0 32
michael@0 33 it("should contain an alert function", function () {
michael@0 34 expect(typeof navigator.notification.alert).toBeDefined();
michael@0 35 expect(typeof navigator.notification.alert).toBe("function");
michael@0 36 });
michael@0 37
michael@0 38 it("should contain a confirm function", function () {
michael@0 39 expect(typeof navigator.notification.confirm).toBeDefined();
michael@0 40 expect(typeof navigator.notification.confirm).toBe("function");
michael@0 41 });
michael@0 42
michael@0 43 it("should contain a prompt function", function () {
michael@0 44 expect(typeof navigator.notification.prompt).toBeDefined();
michael@0 45 expect(typeof navigator.notification.prompt).toBe("function");
michael@0 46 });
michael@0 47 });
michael@0 48 };
michael@0 49
michael@0 50 /******************************************************************************/
michael@0 51 /******************************************************************************/
michael@0 52 /******************************************************************************/
michael@0 53
michael@0 54 exports.defineManualTests = function (contentEl, createActionButton) {
michael@0 55 var logMessage = function (message) {
michael@0 56 var log = document.getElementById('info');
michael@0 57 var logLine = document.createElement('div');
michael@0 58 logLine.innerHTML = message;
michael@0 59 log.appendChild(logLine);
michael@0 60 }
michael@0 61
michael@0 62 var clearLog = function () {
michael@0 63 var log = document.getElementById('info');
michael@0 64 log.innerHTML = '';
michael@0 65 }
michael@0 66
michael@0 67 var beep = function () {
michael@0 68 console.log("beep()");
michael@0 69 navigator.notification.beep(3);
michael@0 70 };
michael@0 71
michael@0 72 var alertDialog = function (message, title, button) {
michael@0 73 console.log("alertDialog()");
michael@0 74 navigator.notification.alert(message,
michael@0 75 function () {
michael@0 76 console.log("Alert dismissed.");
michael@0 77 },
michael@0 78 title, button);
michael@0 79 console.log("After alert");
michael@0 80 };
michael@0 81
michael@0 82 var confirmDialogA = function (message, title, buttons) {
michael@0 83 clearLog();
michael@0 84 navigator.notification.confirm(message,
michael@0 85 function (r) {
michael@0 86 if (r === 0) {
michael@0 87 logMessage("Dismissed dialog without making a selection.");
michael@0 88 console.log("Dismissed dialog without making a selection.");
michael@0 89 } else {
michael@0 90 console.log("You selected " + r);
michael@0 91 logMessage("You selected " + (buttons.split(","))[r - 1]);
michael@0 92 }
michael@0 93 },
michael@0 94 title,
michael@0 95 buttons);
michael@0 96 };
michael@0 97
michael@0 98 var confirmDialogB = function (message, title, buttons) {
michael@0 99 clearLog();
michael@0 100 navigator.notification.confirm(message,
michael@0 101 function (r) {
michael@0 102 if (r === 0) {
michael@0 103 logMessage("Dismissed dialog without making a selection.");
michael@0 104 console.log("Dismissed dialog without making a selection.");
michael@0 105 } else {
michael@0 106 console.log("You selected " + r);
michael@0 107 logMessage("You selected " + buttons[r - 1]);
michael@0 108 }
michael@0 109 },
michael@0 110 title,
michael@0 111 buttons);
michael@0 112 };
michael@0 113
michael@0 114 var promptDialog = function (message, title, buttons) {
michael@0 115 clearLog();
michael@0 116 navigator.notification.prompt(message,
michael@0 117 function (r) {
michael@0 118 if (r && r.buttonIndex === 0) {
michael@0 119 var msg = "Dismissed dialog";
michael@0 120 if (r.input1) {
michael@0 121 msg += " with input: " + r.input1
michael@0 122 }
michael@0 123 logMessage(msg);
michael@0 124 console.log(msg);
michael@0 125 } else {
michael@0 126 console.log("You selected " + r.buttonIndex + " and entered: " + r.input1);
michael@0 127 logMessage("You selected " + buttons[r.buttonIndex - 1] + " and entered: " + r.input1);
michael@0 128 }
michael@0 129 },
michael@0 130 title,
michael@0 131 buttons);
michael@0 132 };
michael@0 133
michael@0 134 /******************************************************************************/
michael@0 135
michael@0 136 var dialogs_tests = '<div id="beep"></div>' +
michael@0 137 'Expected result: Device will beep (unless device is on silent). Nothing will get updated in status box.' +
michael@0 138 '<h2>Dialog Tests</h2>' +
michael@0 139 '<h3>Dialog boxes will pop up for each of the following tests</h3>' +
michael@0 140 '<p/> <div id="alert"></div>' +
michael@0 141 'Expected result: Dialog will say "You pressed alert". Press continue to close dialog. Nothing will get updated in status box.' +
michael@0 142 '<p/> <div id="confirm_deprecated"></div>' +
michael@0 143 '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.' +
michael@0 144 '<p/> <div id="confirm"></div>' +
michael@0 145 '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.' +
michael@0 146 '<p/> <div id="prompt"></div>' +
michael@0 147 '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.' +
michael@0 148 '<p/> <div id="built_in_alert"></div>' +
michael@0 149 '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.' +
michael@0 150 '<p/> <div id="built_in_confirm"></div>' +
michael@0 151 '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.' +
michael@0 152 '<p/> <div id="built_in_prompt"></div>' +
michael@0 153 '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.';
michael@0 154
michael@0 155 contentEl.innerHTML = '<div id="info"></div>' +
michael@0 156 dialogs_tests;
michael@0 157
michael@0 158 createActionButton('Beep', function () {
michael@0 159 beep();
michael@0 160 }, 'beep');
michael@0 161
michael@0 162 createActionButton('Alert Dialog', function () {
michael@0 163 alertDialog('You pressed alert.', 'Alert Dialog', 'Continue');
michael@0 164 }, 'alert');
michael@0 165
michael@0 166 // WP8.1 detection is necessary since it doesn't support confirm dialogs with more than 2 buttons
michael@0 167 var isRunningOnWP81 = cordova.platformId == "windows" && navigator.userAgent.indexOf('Windows Phone') > -1;
michael@0 168
michael@0 169 createActionButton('Confirm Dialog - Deprecated', function () {
michael@0 170 var buttons = isRunningOnWP81 ? 'Yes,No' : 'Yes,No,Maybe';
michael@0 171 confirmDialogA('You pressed confirm.', 'Confirm Dialog', buttons);
michael@0 172 }, 'confirm_deprecated');
michael@0 173
michael@0 174 createActionButton('Confirm Dialog', function () {
michael@0 175 var buttons = isRunningOnWP81 ? ['Yes', 'Actually, No'] : ['Yes', 'No', 'Maybe, Not Sure'];
michael@0 176 confirmDialogB('You pressed confirm.', 'Confirm Dialog', buttons);
michael@0 177 }, 'confirm');
michael@0 178
michael@0 179 createActionButton('Prompt Dialog', function () {
michael@0 180 promptDialog('You pressed prompt.', 'Prompt Dialog', ['Yes', 'No', 'Maybe, Not Sure']);
michael@0 181 }, 'prompt');
michael@0 182
michael@0 183 createActionButton('Built-in Alert Dialog', function () {
michael@0 184 typeof alert === 'function' && alert('You pressed alert');
michael@0 185 }, 'built_in_alert');
michael@0 186
michael@0 187 createActionButton('Built-in Confirm Dialog', function () {
michael@0 188 typeof confirm === 'function' && confirm('You selected confirm');
michael@0 189 }, 'built_in_confirm');
michael@0 190
michael@0 191 createActionButton('Built-in Prompt Dialog', function () {
michael@0 192 typeof prompt === 'function' && prompt('This is a prompt', 'Default value');
michael@0 193 }, 'built_in_prompt');
michael@0 194 };

mercurial