summaryrefslogtreecommitdiff
path: root/StoneIsland/plugins/cordova-plugin-x-socialsharing/tests/test.js
blob: 630899db7641341321937b378bebdb820ee407f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
 * Jasmine Based test suites
 *
 * Several of SocialSharing APIs cannot be automatically tested, because
 * they depend on user interaction in order to call success or fail
 * handlers. For most of them, there is a basic test that assert the presence of
 * the API.
 *
 * There are some cases that test automation can be applied, i.e in "canShareVia",
 * "canShareViaEmail" and "available" methods. For those cases, there is some level
 * of automatic test coverage.
 */
exports.defineAutoTests = function () {
  'use strict';

  describe('socialsharing', function () {
    it('should be defined', function () {
      expect(window.plugins.socialsharing).toBeDefined();
    });

    describe('share', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.share).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.share).toEqual('function');
      });
    });

    describe('shareVia', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.shareVia).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.shareVia).toEqual('function');
      });
    });

    describe('shareViaTwitter', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.shareViaTwitter).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.shareViaTwitter).toEqual('function');
      });
    });

    describe('shareViaFacebook', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.shareViaFacebook).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.shareViaFacebook).toEqual('function');
      });
    });

    describe('shareViaFacebookWithPasteMessageHint', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.shareViaFacebookWithPasteMessageHint).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.shareViaFacebookWithPasteMessageHint).toEqual('function');
      });
    });

    describe('shareViaWhatsApp', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.shareViaWhatsApp).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.shareViaWhatsApp).toEqual('function');
      });
    });

    describe('shareViaSMS', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.shareViaSMS).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.shareViaSMS).toEqual('function');
      });
    });

    describe('shareViaEmail', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.shareViaEmail).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.shareViaEmail).toEqual('function');
      });
    });

    describe('canShareVia', function () {
      it('should be defined', function () {
        expect(window.plugins.socialsharing.canShareVia).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.canShareVia).toEqual('function');
      });

      it('should always call callback or error function', function (done) {
        function onSuccess(data){
          expect(data).not.toEqual(null);
          done();
        }

        function onError(error){
          expect(error).not.toEqual(null);
          done();
        }

        window.plugins.socialsharing.canShareVia('dummytarget','dummymessage', null, null, null, onSuccess, onError);
      });
    });

    describe('canshareViaEmail', function(){
      it('should be defined', function () {
        expect(window.plugins.socialsharing.canShareViaEmail).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.canShareViaEmail).toEqual('function');
      });

      it('should always call callback or error function', function (done) {
        function onSuccess(data){
          expect(data).not.toEqual(null);
          done();
        }

        function onError(error){
          expect(error).not.toEqual(null);
          done();
        }

        window.plugins.socialsharing.canShareViaEmail(onSuccess, onError);
      });
    });

    describe('availabe', function(){
      it('should be defined', function () {
        expect(window.plugins.socialsharing.available).toBeDefined();
      });

      it('should be a function', function () {
        expect(typeof window.plugins.socialsharing.available).toEqual('function');
      });

      it('should return a boolean when called', function(done){
        window.plugins.socialsharing.available(function(isAvailable) {
          expect(typeof isAvailable).toEqual('boolean');
          done();
        });
      });
    });
  });
};

/**
 * Manual tests suites
 *
 * Some actions buttons to execute SocialSharing plugin methods
 */
exports.defineManualTests = function (contentEl, createActionButton) {
  'use strict';

  /** helper function to log messages in the log div element */
  function logMessage(message, color) {
    var log = document.getElementById('info'),
        logLine = document.createElement('div');

    if (color) {
      logLine.style.color = color;
    }

    logLine.innerHTML = message;
    log.appendChild(logLine);
  }

  /** helper function to clear the log div element */
  function clearLog() {
    var log = document.getElementById('info');
    log.innerHTML = '';
  }

  /** helper function to declare a not implemented test */
  function testNotImplemented(testName) {
    return function () {
      console.error(testName, 'test not implemented');
    };
  }

  /** init method called on deviceready event */
  function init() {}

  /** object to hold properties and configs */
  var TestSuite = {};

  TestSuite.getCanShareViaTarget = function(){
    return document.getElementById('inputCanShareVia').value;
  };

  TestSuite.$markup = '' +
  '<fieldset>' +
  '<legend>Available Tests</legend>' +

  '<h3>Available</h3>' +
  '<div id="buttonIsAvailable"></div>' +
  'Expected result: Should log if the plugin is available or not' +
  '</fieldset>' +

  '<fieldset>' +
  '<legend>Share Tests</legend>' +

  '<h3>Share Message</h3>' +
  '<div id="buttonShareMessage"></div>' +
  'Expected result: Should display share widget, and the message to share should contain "Message body"' +

  '<h3>Share Message with Subject</h3>' +
  '<div id="buttonShareMessageWithSubject"></div>' +
  'Expected result: Should display share widget, and the message to share should contain "Message body", and the subject should be "Message subject"' +

  '<h3>Share Link</h3>' +
  '<div id="buttonShareLink"></div>' +
  'Expected result: Should display share widget, and the message to share should contain "http://www.x-services.nl"' +

  '<h3>Share Message with Link</h3>' +
  '<div id="buttonShareMessageAndLink"></div>' +
  'Expected result: Should display share widget, and the message to share should contain "Message body http://www.x-services.nl"' +

  '<h3>Share Image</h3>' +
  '<div id="buttonShareImage"></div>' +
  'Expected result: Should display share widget, and the message to share should contain an image' +

  '<h3>Share Image in base 64</h3>' +
  '<div id="buttonShareImageBase64"></div>' +
  'Expected result: Should display share widget, and the message to share should contain an image. The image is encoded in base 64' +

  '<h3>Share Image with Message</h3>' +
  '<div id="buttonShareMessageImage"></div>' +
  'Expected result: Should display share widget, and the message to share should contain "Message body" and an image' +

  '<h3>Share Image with Message and Link</h3>' +
  '<div id="buttonShareMessageImageLink"></div>' +
  'Expected result: Should display share widget, and the message to share should contain "Message body http://www.x-services.nl" and an image' +

  '<h3>Share Image with Message, Subject and Link</h3>' +
  '<div id="buttonShareMessageSubjectImageLink"></div>' +
  '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' +
  '</fieldset>' +

  '<fieldset>' +
  '<legend>Can Share Tests</legend>' +

  'Target: <input id="inputCanShareVia" type="text"/><br>' +

  '<h3>Can Share via</h3>' +
  '<div id="buttonCanShareVia"></div>' +
  'Expected result: should log OK if can share, or should log a list of available share targets' +

  '<h3>Can Share via Email</h3>' +
  '<div id="buttonCanShareViaEmail"></div>' +
  'Expected result: should log OK if can share' +
  '</fieldset>' +
  '';

  contentEl.innerHTML = '<div id="info"></div>' + TestSuite.$markup;

  createActionButton('availabe', function () {
    clearLog();
    window.plugins.socialsharing.available(function(isAvailable) {
      var message = 'is this plugin available? ';
      message += isAvailable? 'Yes' : 'No';

      logMessage(message, isAvailable? 'green' : 'red');
    });
  }, 'buttonIsAvailable');

  createActionButton('share message', function () {
    window.plugins.socialsharing.share('Message body');
  }, 'buttonShareMessage');

  createActionButton('share message and subject', function () {
    window.plugins.socialsharing.share('Message body', 'Message subject');
  }, 'buttonShareMessageWithSubject');

  createActionButton('share link', function () {
    window.plugins.socialsharing.share(null, null, null, 'http://www.x-services.nl');
  }, 'buttonShareLink');

  createActionButton('share message and link', function () {
    window.plugins.socialsharing.share('Message body', null, null, 'http://www.x-services.nl');
  }, 'buttonShareMessageAndLink');

  createActionButton('share image', function () {
    window.plugins.socialsharing.share(null, null, 'https://www.google.nl/images/srpr/logo4w.png', null);
  }, 'buttonShareImage');

  createActionButton('share image base 64', function () {
    window.plugins.socialsharing.share(null, 'Android filename', 'data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7', null);
  }, 'buttonShareImageBase64');

  createActionButton('share message with image', function () {
    window.plugins.socialsharing.share('Message body', null, 'https://www.google.nl/images/srpr/logo4w.png', null);
  }, 'buttonShareMessageImage');

  createActionButton('share message, image, and link', function () {
    window.plugins.socialsharing.share('Message body', null, 'https://www.google.nl/images/srpr/logo4w.png', 'http://www.x-services.nl');
  }, 'buttonShareMessageImageLink');

  createActionButton('share message, subject, image, and link', function () {
    window.plugins.socialsharing.share('Message body', 'Message subject', 'https://www.google.nl/images/srpr/logo4w.png', 'http://www.x-services.nl');
  }, 'buttonShareMessageSubjectImageLink');

  createActionButton('can share via', function () {
    var target = TestSuite.getCanShareViaTarget();

    if(!target){
      console.error('must have a canShareVia target');
    }
    else {
      clearLog();
      window.plugins.socialsharing.canShareVia(target, 'msg', null, null, null, function(e){
        console.log('canShareVia success, see log for more information');
        logMessage('canShareVia: ' + e,'green');
      }, function(e){
        console.error('canShareVia fail, see log for more information');
        var message = "Share targets<br>";

        message += "<ul>";

        e.forEach(function(target){
          message += "<li>" + target + "</li>";
        });

        message += "</ul>";
        logMessage(message,'red');
      });
    }
  }, 'buttonCanShareVia');

  createActionButton('can share via email', function () {
    clearLog();
    window.plugins.socialsharing.canShareViaEmail(function(e){
      console.log('canShareViaEmail success, see log for more information');
      logMessage('canShareViaEmail: ' + e,'green');
    }, function(e){
      console.error('canShareViaEmail fail, see log for more information');
      logMessage('canShareViaEmail: ' + e,'red');
    });
  }, 'buttonCanShareViaEmail');

  document.addEventListener('deviceready', init, false);
};