tests.js 10 KB

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