You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
12 KiB

  1. /**
  2. * Jasmine Based test suites
  3. *
  4. * Several of SocialSharing APIs cannot be automatically tested, because
  5. * they depend on user interaction in order to call success or fail
  6. * handlers. For most of them, there is a basic test that assert the presence of
  7. * the API.
  8. *
  9. * There are some cases that test automation can be applied, i.e in "canShareVia",
  10. * "canShareViaEmail" and "available" methods. For those cases, there is some level
  11. * of automatic test coverage.
  12. */
  13. exports.defineAutoTests = function () {
  14. 'use strict';
  15. describe('socialsharing', function () {
  16. it('should be defined', function () {
  17. expect(window.plugins.socialsharing).toBeDefined();
  18. });
  19. describe('share', function () {
  20. it('should be defined', function () {
  21. expect(window.plugins.socialsharing.share).toBeDefined();
  22. });
  23. it('should be a function', function () {
  24. expect(typeof window.plugins.socialsharing.share).toEqual('function');
  25. });
  26. });
  27. describe('shareVia', function () {
  28. it('should be defined', function () {
  29. expect(window.plugins.socialsharing.shareVia).toBeDefined();
  30. });
  31. it('should be a function', function () {
  32. expect(typeof window.plugins.socialsharing.shareVia).toEqual('function');
  33. });
  34. });
  35. describe('shareViaTwitter', function () {
  36. it('should be defined', function () {
  37. expect(window.plugins.socialsharing.shareViaTwitter).toBeDefined();
  38. });
  39. it('should be a function', function () {
  40. expect(typeof window.plugins.socialsharing.shareViaTwitter).toEqual('function');
  41. });
  42. });
  43. describe('shareViaFacebook', function () {
  44. it('should be defined', function () {
  45. expect(window.plugins.socialsharing.shareViaFacebook).toBeDefined();
  46. });
  47. it('should be a function', function () {
  48. expect(typeof window.plugins.socialsharing.shareViaFacebook).toEqual('function');
  49. });
  50. });
  51. describe('shareViaFacebookWithPasteMessageHint', function () {
  52. it('should be defined', function () {
  53. expect(window.plugins.socialsharing.shareViaFacebookWithPasteMessageHint).toBeDefined();
  54. });
  55. it('should be a function', function () {
  56. expect(typeof window.plugins.socialsharing.shareViaFacebookWithPasteMessageHint).toEqual('function');
  57. });
  58. });
  59. describe('shareViaWhatsApp', function () {
  60. it('should be defined', function () {
  61. expect(window.plugins.socialsharing.shareViaWhatsApp).toBeDefined();
  62. });
  63. it('should be a function', function () {
  64. expect(typeof window.plugins.socialsharing.shareViaWhatsApp).toEqual('function');
  65. });
  66. });
  67. describe('shareViaSMS', function () {
  68. it('should be defined', function () {
  69. expect(window.plugins.socialsharing.shareViaSMS).toBeDefined();
  70. });
  71. it('should be a function', function () {
  72. expect(typeof window.plugins.socialsharing.shareViaSMS).toEqual('function');
  73. });
  74. });
  75. describe('shareViaEmail', function () {
  76. it('should be defined', function () {
  77. expect(window.plugins.socialsharing.shareViaEmail).toBeDefined();
  78. });
  79. it('should be a function', function () {
  80. expect(typeof window.plugins.socialsharing.shareViaEmail).toEqual('function');
  81. });
  82. });
  83. describe('canShareVia', function () {
  84. it('should be defined', function () {
  85. expect(window.plugins.socialsharing.canShareVia).toBeDefined();
  86. });
  87. it('should be a function', function () {
  88. expect(typeof window.plugins.socialsharing.canShareVia).toEqual('function');
  89. });
  90. it('should always call callback or error function', function (done) {
  91. function onSuccess(data){
  92. expect(data).not.toEqual(null);
  93. done();
  94. }
  95. function onError(error){
  96. expect(error).not.toEqual(null);
  97. done();
  98. }
  99. window.plugins.socialsharing.canShareVia('dummytarget','dummymessage', null, null, null, onSuccess, onError);
  100. });
  101. });
  102. describe('canshareViaEmail', function(){
  103. it('should be defined', function () {
  104. expect(window.plugins.socialsharing.canShareViaEmail).toBeDefined();
  105. });
  106. it('should be a function', function () {
  107. expect(typeof window.plugins.socialsharing.canShareViaEmail).toEqual('function');
  108. });
  109. it('should always call callback or error function', function (done) {
  110. function onSuccess(data){
  111. expect(data).not.toEqual(null);
  112. done();
  113. }
  114. function onError(error){
  115. expect(error).not.toEqual(null);
  116. done();
  117. }
  118. window.plugins.socialsharing.canShareViaEmail(onSuccess, onError);
  119. });
  120. });
  121. describe('availabe', function(){
  122. it('should be defined', function () {
  123. expect(window.plugins.socialsharing.available).toBeDefined();
  124. });
  125. it('should be a function', function () {
  126. expect(typeof window.plugins.socialsharing.available).toEqual('function');
  127. });
  128. it('should return a boolean when called', function(done){
  129. window.plugins.socialsharing.available(function(isAvailable) {
  130. expect(typeof isAvailable).toEqual('boolean');
  131. done();
  132. });
  133. });
  134. });
  135. });
  136. };
  137. /**
  138. * Manual tests suites
  139. *
  140. * Some actions buttons to execute SocialSharing plugin methods
  141. */
  142. exports.defineManualTests = function (contentEl, createActionButton) {
  143. 'use strict';
  144. /** helper function to log messages in the log div element */
  145. function logMessage(message, color) {
  146. var log = document.getElementById('info'),
  147. logLine = document.createElement('div');
  148. if (color) {
  149. logLine.style.color = color;
  150. }
  151. logLine.innerHTML = message;
  152. log.appendChild(logLine);
  153. }
  154. /** helper function to clear the log div element */
  155. function clearLog() {
  156. var log = document.getElementById('info');
  157. log.innerHTML = '';
  158. }
  159. /** helper function to declare a not implemented test */
  160. function testNotImplemented(testName) {
  161. return function () {
  162. console.error(testName, 'test not implemented');
  163. };
  164. }
  165. /** init method called on deviceready event */
  166. function init() {}
  167. /** object to hold properties and configs */
  168. var TestSuite = {};
  169. TestSuite.getCanShareViaTarget = function(){
  170. return document.getElementById('inputCanShareVia').value;
  171. };
  172. TestSuite.$markup = '' +
  173. '<fieldset>' +
  174. '<legend>Available Tests</legend>' +
  175. '<h3>Available</h3>' +
  176. '<div id="buttonIsAvailable"></div>' +
  177. 'Expected result: Should log if the plugin is available or not' +
  178. '</fieldset>' +
  179. '<fieldset>' +
  180. '<legend>Share Tests</legend>' +
  181. '<h3>Share Message</h3>' +
  182. '<div id="buttonShareMessage"></div>' +
  183. 'Expected result: Should display share widget, and the message to share should contain "Message body"' +
  184. '<h3>Share Message with Subject</h3>' +
  185. '<div id="buttonShareMessageWithSubject"></div>' +
  186. 'Expected result: Should display share widget, and the message to share should contain "Message body", and the subject should be "Message subject"' +
  187. '<h3>Share Link</h3>' +
  188. '<div id="buttonShareLink"></div>' +
  189. 'Expected result: Should display share widget, and the message to share should contain "http://www.x-services.nl"' +
  190. '<h3>Share Message with Link</h3>' +
  191. '<div id="buttonShareMessageAndLink"></div>' +
  192. 'Expected result: Should display share widget, and the message to share should contain "Message body http://www.x-services.nl"' +
  193. '<h3>Share Image</h3>' +
  194. '<div id="buttonShareImage"></div>' +
  195. 'Expected result: Should display share widget, and the message to share should contain an image' +
  196. '<h3>Share Image in base 64</h3>' +
  197. '<div id="buttonShareImageBase64"></div>' +
  198. 'Expected result: Should display share widget, and the message to share should contain an image. The image is encoded in base 64' +
  199. '<h3>Share Image with Message</h3>' +
  200. '<div id="buttonShareMessageImage"></div>' +
  201. 'Expected result: Should display share widget, and the message to share should contain "Message body" and an image' +
  202. '<h3>Share Image with Message and Link</h3>' +
  203. '<div id="buttonShareMessageImageLink"></div>' +
  204. 'Expected result: Should display share widget, and the message to share should contain "Message body http://www.x-services.nl" and an image' +
  205. '<h3>Share Image with Message, Subject and Link</h3>' +
  206. '<div id="buttonShareMessageSubjectImageLink"></div>' +
  207. 'Expected result: Should display share widget, and the message to share should contain "Message body http://www.x-services.nl", "Message subject" as subject, and an image' +
  208. '</fieldset>' +
  209. '<fieldset>' +
  210. '<legend>Can Share Tests</legend>' +
  211. 'Target: <input id="inputCanShareVia" type="text"/><br>' +
  212. '<h3>Can Share via</h3>' +
  213. '<div id="buttonCanShareVia"></div>' +
  214. 'Expected result: should log OK if can share, or should log a list of available share targets' +
  215. '<h3>Can Share via Email</h3>' +
  216. '<div id="buttonCanShareViaEmail"></div>' +
  217. 'Expected result: should log OK if can share' +
  218. '</fieldset>' +
  219. '';
  220. contentEl.innerHTML = '<div id="info"></div>' + TestSuite.$markup;
  221. createActionButton('availabe', function () {
  222. clearLog();
  223. window.plugins.socialsharing.available(function(isAvailable) {
  224. var message = 'is this plugin available? ';
  225. message += isAvailable? 'Yes' : 'No';
  226. logMessage(message, isAvailable? 'green' : 'red');
  227. });
  228. }, 'buttonIsAvailable');
  229. createActionButton('share message', function () {
  230. window.plugins.socialsharing.share('Message body');
  231. }, 'buttonShareMessage');
  232. createActionButton('share message and subject', function () {
  233. window.plugins.socialsharing.share('Message body', 'Message subject');
  234. }, 'buttonShareMessageWithSubject');
  235. createActionButton('share link', function () {
  236. window.plugins.socialsharing.share(null, null, null, 'http://www.x-services.nl');
  237. }, 'buttonShareLink');
  238. createActionButton('share message and link', function () {
  239. window.plugins.socialsharing.share('Message body', null, null, 'http://www.x-services.nl');
  240. }, 'buttonShareMessageAndLink');
  241. createActionButton('share image', function () {
  242. window.plugins.socialsharing.share(null, null, 'https://www.google.nl/images/srpr/logo4w.png', null);
  243. }, 'buttonShareImage');
  244. createActionButton('share image base 64', function () {
  245. window.plugins.socialsharing.share(null, 'Android filename', 'data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7', null);
  246. }, 'buttonShareImageBase64');
  247. createActionButton('share message with image', function () {
  248. window.plugins.socialsharing.share('Message body', null, 'https://www.google.nl/images/srpr/logo4w.png', null);
  249. }, 'buttonShareMessageImage');
  250. createActionButton('share message, image, and link', function () {
  251. window.plugins.socialsharing.share('Message body', null, 'https://www.google.nl/images/srpr/logo4w.png', 'http://www.x-services.nl');
  252. }, 'buttonShareMessageImageLink');
  253. createActionButton('share message, subject, image, and link', function () {
  254. window.plugins.socialsharing.share('Message body', 'Message subject', 'https://www.google.nl/images/srpr/logo4w.png', 'http://www.x-services.nl');
  255. }, 'buttonShareMessageSubjectImageLink');
  256. createActionButton('can share via', function () {
  257. var target = TestSuite.getCanShareViaTarget();
  258. if(!target){
  259. console.error('must have a canShareVia target');
  260. }
  261. else {
  262. clearLog();
  263. window.plugins.socialsharing.canShareVia(target, 'msg', null, null, null, function(e){
  264. console.log('canShareVia success, see log for more information');
  265. logMessage('canShareVia: ' + e,'green');
  266. }, function(e){
  267. console.error('canShareVia fail, see log for more information');
  268. var message = "Share targets<br>";
  269. message += "<ul>";
  270. e.forEach(function(target){
  271. message += "<li>" + target + "</li>";
  272. });
  273. message += "</ul>";
  274. logMessage(message,'red');
  275. });
  276. }
  277. }, 'buttonCanShareVia');
  278. createActionButton('can share via email', function () {
  279. clearLog();
  280. window.plugins.socialsharing.canShareViaEmail(function(e){
  281. console.log('canShareViaEmail success, see log for more information');
  282. logMessage('canShareViaEmail: ' + e,'green');
  283. }, function(e){
  284. console.error('canShareViaEmail fail, see log for more information');
  285. logMessage('canShareViaEmail: ' + e,'red');
  286. });
  287. }, 'buttonCanShareViaEmail');
  288. document.addEventListener('deviceready', init, false);
  289. };