webext-schema 6.0.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3039 @@
1
+ /**
2
+ * browser.test.js
3
+ */
4
+
5
+ /* api */
6
+ import { strict as assert } from 'node:assert';
7
+ import { afterEach, beforeEach, describe, it } from 'mocha';
8
+ import sinon from 'sinon';
9
+ import { browser } from './mocha/setup.js';
10
+
11
+ /* test */
12
+ import * as mjs from '../modules/browser.js';
13
+
14
+ describe('browser', () => {
15
+ beforeEach(() => {
16
+ browser._sandbox.reset();
17
+ browser.i18n.getMessage.callsFake((...args) => args.toString());
18
+ browser.permissions.contains.resolves(true);
19
+ global.browser = browser;
20
+ });
21
+ afterEach(() => {
22
+ delete global.browser;
23
+ browser._sandbox.reset();
24
+ });
25
+
26
+ it('should get browser object', () => {
27
+ assert.strictEqual(typeof browser, 'object', 'browser');
28
+ });
29
+
30
+ describe('check if permission is granted', () => {
31
+ const func = mjs.isPermissionGranted;
32
+ beforeEach(() => {
33
+ browser.permissions.contains.callsFake((obj = {}) => {
34
+ const { origins, permissions } = obj;
35
+ let res;
36
+ if (Array.isArray(origins)) {
37
+ const [...origs] = origins;
38
+ // match pattern: '*://*.example.com/*'
39
+ const grantedOrigsReg =
40
+ /^(?:http|ws)s?:\/\/(?:[a-zA-Z\d]+\.)*example\.com\/.*$/;
41
+ for (const orig of origs) {
42
+ res = grantedOrigsReg.test(orig);
43
+ if (res === false) {
44
+ break;
45
+ }
46
+ }
47
+ }
48
+ if ((res || typeof res !== 'boolean') && Array.isArray(permissions)) {
49
+ const [...perms] = permissions;
50
+ const grantedPerms = ['tabs', 'browserSettings'];
51
+ for (const perm of perms) {
52
+ res = grantedPerms.includes(perm);
53
+ if (res === false) {
54
+ break;
55
+ }
56
+ }
57
+ }
58
+ return !!res;
59
+ });
60
+ });
61
+
62
+ it('should get result', async () => {
63
+ const res = await func();
64
+ assert.strictEqual(res, false, 'result');
65
+ });
66
+
67
+ it('should get result', async () => {
68
+ const res = await func('foo');
69
+ assert.strictEqual(res, false, 'result');
70
+ });
71
+
72
+ it('should get result', async () => {
73
+ const res = await func([]);
74
+ assert.strictEqual(res, false, 'result');
75
+ });
76
+
77
+ it('should get result', async () => {
78
+ const res = await func({
79
+ foo: ['bar']
80
+ });
81
+ assert.strictEqual(res, false, 'result');
82
+ });
83
+
84
+ it('should get result', async () => {
85
+ const res = await func({
86
+ origins: ['https://mozilla.org/']
87
+ });
88
+ assert.strictEqual(res, false, 'result');
89
+ });
90
+
91
+ it('should get result', async () => {
92
+ const res = await func({
93
+ origins: ['https://example.com/']
94
+ });
95
+ assert.strictEqual(res, true, 'result');
96
+ });
97
+
98
+ it('should get result', async () => {
99
+ const res = await func({
100
+ origins: ['http://www.example.com/foo']
101
+ });
102
+ assert.strictEqual(res, true, 'result');
103
+ });
104
+
105
+ it('should get result', async () => {
106
+ const res = await func({
107
+ permissions: ['alarms']
108
+ });
109
+ assert.strictEqual(res, false, 'result');
110
+ });
111
+
112
+ it('should get result', async () => {
113
+ const res = await func({
114
+ permissions: ['tabs']
115
+ });
116
+ assert.strictEqual(res, true, 'result');
117
+ });
118
+
119
+ it('should get result', async () => {
120
+ const res = await func({
121
+ permissions: ['tabs', 'bookmarks']
122
+ });
123
+ assert.strictEqual(res, false, 'result');
124
+ });
125
+
126
+ it('should get result', async () => {
127
+ const res = await func({
128
+ permissions: ['tabs', 'browserSettings']
129
+ });
130
+ assert.strictEqual(res, true, 'result');
131
+ });
132
+
133
+ it('should get result', async () => {
134
+ const res = await func({
135
+ permissions: ['tabs', 'browserSettings', 'commands']
136
+ });
137
+ assert.strictEqual(res, false, 'result');
138
+ });
139
+
140
+ it('should get result', async () => {
141
+ const res = await func({
142
+ origins: ['https://example.com/'],
143
+ permissions: ['tabs']
144
+ });
145
+ assert.strictEqual(res, true, 'result');
146
+ });
147
+
148
+ it('should get result', async () => {
149
+ const res = await func({
150
+ origins: ['https://mozilla.org/'],
151
+ permissions: ['tabs']
152
+ });
153
+ assert.strictEqual(res, false, 'result');
154
+ });
155
+
156
+ it('should get result', async () => {
157
+ const res = await func({
158
+ origins: ['https://example.com/'],
159
+ permissions: ['alarms']
160
+ });
161
+ assert.strictEqual(res, false, 'result');
162
+ });
163
+ });
164
+
165
+ describe('create bookmark', () => {
166
+ const func = mjs.createBookmark;
167
+
168
+ it('should not call function if permission is not granted', async () => {
169
+ browser.permissions.contains.resolves(false);
170
+ const i = browser.bookmarks.create.callCount;
171
+ const res = await func({ foo: 'bar' });
172
+ assert.strictEqual(browser.bookmarks.create.callCount, i, 'not called');
173
+ assert.deepEqual(res, null, 'result');
174
+ });
175
+
176
+ it('should get null if no argument given', async () => {
177
+ const res = await func();
178
+ assert.deepEqual(res, null, 'result');
179
+ });
180
+
181
+ it('should get null if argument is not object', async () => {
182
+ const res = await func('foo');
183
+ assert.deepEqual(res, null, 'result');
184
+ });
185
+
186
+ it('should get null if argument is empty object', async () => {
187
+ const res = await func({});
188
+ assert.deepEqual(res, null, 'result');
189
+ });
190
+
191
+ it('should get object', async () => {
192
+ browser.bookmarks.create.withArgs({ foo: 'bar' }).resolves({});
193
+ const res = await func({ foo: 'bar' });
194
+ assert.deepEqual(res, {}, 'result');
195
+ });
196
+ });
197
+
198
+ describe('get bookmark tree node', () => {
199
+ const func = mjs.getBookmarkTreeNode;
200
+
201
+ it('should not call function if permission is not granted', async () => {
202
+ browser.permissions.contains.resolves(false);
203
+ const i = browser.bookmarks.create.callCount;
204
+ const res = await func({ foo: 'bar' });
205
+ assert.strictEqual(browser.bookmarks.create.callCount, i, 'not called');
206
+ assert.deepEqual(res, null, 'result');
207
+ });
208
+
209
+ it('should call function', async () => {
210
+ const i = browser.bookmarks.get.callCount;
211
+ const j = browser.bookmarks.getSubTree.callCount;
212
+ const k = browser.bookmarks.getTree.callCount;
213
+ browser.bookmarks.getTree.resolves([{}]);
214
+ const res = await func();
215
+ assert.strictEqual(browser.bookmarks.get.callCount, i, 'not called get');
216
+ assert.strictEqual(browser.bookmarks.getSubTree.callCount, j,
217
+ 'not called get sub tree');
218
+ assert.strictEqual(browser.bookmarks.getTree.callCount, k + 1,
219
+ 'called get tree');
220
+ assert.deepEqual(res, [{}], 'result');
221
+ });
222
+
223
+ it('should throw if ID not found', async () => {
224
+ browser.bookmarks.getSubTree.withArgs('foo').rejects(new Error('error'));
225
+ await func('foo').catch(e => {
226
+ assert.deepStrictEqual(e, new Error('error'));
227
+ });
228
+ });
229
+
230
+ it('should call function', async () => {
231
+ const i = browser.bookmarks.get.callCount;
232
+ const j = browser.bookmarks.getSubTree.callCount;
233
+ const k = browser.bookmarks.getTree.callCount;
234
+ browser.bookmarks.getSubTree.withArgs('foo').resolves([{}]);
235
+ const res = await func('foo');
236
+ assert.strictEqual(browser.bookmarks.get.callCount, i, 'not called get');
237
+ assert.strictEqual(browser.bookmarks.getSubTree.callCount, j + 1,
238
+ 'called get sub tree');
239
+ assert.strictEqual(browser.bookmarks.getTree.callCount, k,
240
+ 'not called get tree');
241
+ assert.deepEqual(res, [{}], 'result');
242
+ });
243
+
244
+ it('should call function', async () => {
245
+ const i = browser.bookmarks.get.callCount;
246
+ const j = browser.bookmarks.getSubTree.callCount;
247
+ const k = browser.bookmarks.getTree.callCount;
248
+ browser.bookmarks.get.withArgs(['foo', 'bar']).resolves([{}, {}]);
249
+ const res = await func(['foo', 'bar']);
250
+ assert.strictEqual(browser.bookmarks.get.callCount, i + 1, 'called get');
251
+ assert.strictEqual(browser.bookmarks.getSubTree.callCount, j,
252
+ 'not called get sub tree');
253
+ assert.strictEqual(browser.bookmarks.getTree.callCount, k,
254
+ 'not called get tree');
255
+ assert.deepEqual(res, [{}, {}], 'result');
256
+ });
257
+ });
258
+
259
+ describe('get closeTabsByDoubleClick user value', () => {
260
+ const func = mjs.getCloseTabsByDoubleClickValue;
261
+
262
+ it('should not call function if permission is not granted', async () => {
263
+ browser.permissions.contains.resolves(false);
264
+ const i = browser.browserSettings.closeTabsByDoubleClick.get.callCount;
265
+ const res = await func({ foo: 'bar' });
266
+ assert.strictEqual(
267
+ browser.browserSettings.closeTabsByDoubleClick.get.callCount, i,
268
+ 'not called'
269
+ );
270
+ assert.deepEqual(res, null, 'result');
271
+ });
272
+
273
+ it('should get null', async () => {
274
+ const res = await func();
275
+ assert.deepEqual(res, null, 'result');
276
+ });
277
+
278
+ it('should get object', async () => {
279
+ browser.browserSettings.closeTabsByDoubleClick.get.withArgs({})
280
+ .resolves({});
281
+ const res = await func();
282
+ assert.deepEqual(res, {}, 'result');
283
+ });
284
+ });
285
+
286
+ describe('set context menu on mouseup', () => {
287
+ const func = mjs.setContextMenuOnMouseup;
288
+
289
+ it('should get false if permission is not granted', async () => {
290
+ browser.permissions.contains.resolves(false);
291
+ const i = browser.browserSettings.contextMenuShowEvent.get.callCount;
292
+ const res = await func();
293
+ assert.strictEqual(
294
+ browser.browserSettings.contextMenuShowEvent.get.callCount, i,
295
+ 'not called'
296
+ );
297
+ assert.strictEqual(res, false, 'result');
298
+ });
299
+
300
+ it('should get true', async () => {
301
+ const i = browser.browserSettings.contextMenuShowEvent.get.callCount;
302
+ const j = browser.browserSettings.contextMenuShowEvent.set.callCount;
303
+ browser.browserSettings.contextMenuShowEvent.get.resolves({
304
+ value: 'mouseup'
305
+ });
306
+ const res = await func();
307
+ assert.strictEqual(
308
+ browser.browserSettings.contextMenuShowEvent.get.callCount, i + 1,
309
+ 'called'
310
+ );
311
+ assert.strictEqual(
312
+ browser.browserSettings.contextMenuShowEvent.set.callCount, j,
313
+ 'not called'
314
+ );
315
+ assert.strictEqual(res, true, 'result');
316
+ });
317
+
318
+ it('should get true', async () => {
319
+ const i = browser.browserSettings.contextMenuShowEvent.get.callCount;
320
+ const j = browser.browserSettings.contextMenuShowEvent.set.callCount;
321
+ browser.browserSettings.contextMenuShowEvent.get.resolves({
322
+ value: 'mousedown',
323
+ levelOfControl: 'controllable_by_this_extension'
324
+ });
325
+ browser.browserSettings.contextMenuShowEvent.set.resolves(true);
326
+ const res = await func();
327
+ assert.strictEqual(
328
+ browser.browserSettings.contextMenuShowEvent.get.callCount, i + 1,
329
+ 'called'
330
+ );
331
+ assert.strictEqual(
332
+ browser.browserSettings.contextMenuShowEvent.set.callCount, j + 1,
333
+ 'called'
334
+ );
335
+ assert.strictEqual(res, true, 'result');
336
+ });
337
+
338
+ it('should get false', async () => {
339
+ const i = browser.browserSettings.contextMenuShowEvent.get.callCount;
340
+ const j = browser.browserSettings.contextMenuShowEvent.set.callCount;
341
+ browser.browserSettings.contextMenuShowEvent.get.resolves({
342
+ value: 'mousedown',
343
+ levelOfControl: 'controllable_by_this_extension'
344
+ });
345
+ browser.browserSettings.contextMenuShowEvent.set.resolves(false);
346
+ const res = await func();
347
+ assert.strictEqual(
348
+ browser.browserSettings.contextMenuShowEvent.get.callCount, i + 1,
349
+ 'called'
350
+ );
351
+ assert.strictEqual(
352
+ browser.browserSettings.contextMenuShowEvent.set.callCount, j + 1,
353
+ 'called'
354
+ );
355
+ assert.strictEqual(res, false, 'result');
356
+ });
357
+
358
+ it('should get false', async () => {
359
+ const i = browser.browserSettings.contextMenuShowEvent.get.callCount;
360
+ const j = browser.browserSettings.contextMenuShowEvent.set.callCount;
361
+ browser.browserSettings.contextMenuShowEvent.get.resolves({
362
+ value: 'mousedown',
363
+ levelOfControl: 'foo'
364
+ });
365
+ const res = await func();
366
+ assert.strictEqual(
367
+ browser.browserSettings.contextMenuShowEvent.get.callCount, i + 1,
368
+ 'called'
369
+ );
370
+ assert.strictEqual(
371
+ browser.browserSettings.contextMenuShowEvent.set.callCount, j,
372
+ 'not called'
373
+ );
374
+ assert.strictEqual(res, false, 'result');
375
+ });
376
+ });
377
+
378
+ describe('clear context menu on mouseup', () => {
379
+ const func = mjs.clearContextMenuOnMouseup;
380
+
381
+ it('should get false if permission is not granted', async () => {
382
+ browser.permissions.contains.resolves(false);
383
+ const i = browser.browserSettings.contextMenuShowEvent.clear.callCount;
384
+ const res = await func();
385
+ assert.strictEqual(
386
+ browser.browserSettings.contextMenuShowEvent.clear.callCount, i,
387
+ 'not called'
388
+ );
389
+ assert.strictEqual(res, false, 'result');
390
+ });
391
+
392
+ it('should get true', async () => {
393
+ const i = browser.browserSettings.contextMenuShowEvent.clear.callCount;
394
+ browser.browserSettings.contextMenuShowEvent.clear.resolves(true);
395
+ const res = await func();
396
+ assert.strictEqual(
397
+ browser.browserSettings.contextMenuShowEvent.clear.callCount, i + 1,
398
+ 'called'
399
+ );
400
+ assert.strictEqual(res, true, 'result');
401
+ });
402
+
403
+ it('should get false', async () => {
404
+ const i = browser.browserSettings.contextMenuShowEvent.clear.callCount;
405
+ browser.browserSettings.contextMenuShowEvent.clear.resolves(false);
406
+ const res = await func();
407
+ assert.strictEqual(
408
+ browser.browserSettings.contextMenuShowEvent.clear.callCount, i + 1,
409
+ 'called'
410
+ );
411
+ assert.strictEqual(res, false, 'result');
412
+ });
413
+ });
414
+
415
+ describe('get new tab position value', () => {
416
+ const func = mjs.getNewTabPositionValue;
417
+
418
+ it('should get null if permission is not granted', async () => {
419
+ browser.permissions.contains.resolves(false);
420
+ const i = browser.browserSettings.newTabPosition.get.callCount;
421
+ const res = await func();
422
+ assert.strictEqual(
423
+ browser.browserSettings.newTabPosition.get.callCount, i, 'not called'
424
+ );
425
+ assert.deepEqual(res, null, 'result');
426
+ });
427
+
428
+ it('should get value', async () => {
429
+ const i = browser.browserSettings.newTabPosition.get.callCount;
430
+ browser.browserSettings.newTabPosition.get.resolves('foo');
431
+ const res = await func();
432
+ assert.strictEqual(
433
+ browser.browserSettings.newTabPosition.get.callCount, i + 1, 'called'
434
+ );
435
+ assert.strictEqual(res, 'foo', 'result');
436
+ });
437
+ });
438
+
439
+ describe('is command customizable', () => {
440
+ const func = mjs.isCommandCustomizable;
441
+
442
+ it('should get false if permission is not granted', async () => {
443
+ browser.permissions.contains.resolves(false);
444
+ const res = await func();
445
+ assert.strictEqual(res, false, 'result');
446
+ });
447
+
448
+ it('should get true', async () => {
449
+ const res = await func();
450
+ assert.strictEqual(res, true, 'result');
451
+ });
452
+ });
453
+
454
+ describe('update command', () => {
455
+ const func = mjs.updateCommand;
456
+
457
+ it('should throw if no argument given', async () => {
458
+ await func().catch(e => {
459
+ assert.strictEqual(e.message, 'Expected String but got Undefined.');
460
+ });
461
+ });
462
+
463
+ it('should throw if first argument is not string', async () => {
464
+ await func(1).catch(e => {
465
+ assert.strictEqual(e.message, 'Expected String but got Number.');
466
+ });
467
+ });
468
+
469
+ it('should throw if 2nd argument is not string', async () => {
470
+ await func('foo', 1).catch(e => {
471
+ assert.strictEqual(e.message, 'Expected String but got Number.');
472
+ });
473
+ });
474
+
475
+ it('should get null', async () => {
476
+ browser.permissions.contains.resolves(false);
477
+ const res = await func('foo', '');
478
+ assert.strictEqual(browser.commands.reset.calledOnce, false,
479
+ 'not called');
480
+ assert.strictEqual(browser.commands.update.calledOnce, false,
481
+ 'not called');
482
+ assert.deepEqual(res, null, 'result');
483
+ });
484
+
485
+ it('should get null', async () => {
486
+ const res = await func('foo', 'a');
487
+ assert.strictEqual(browser.commands.reset.calledOnce, false,
488
+ 'not called');
489
+ assert.strictEqual(browser.commands.update.calledOnce, false,
490
+ 'not called');
491
+ assert.deepEqual(res, null, 'result');
492
+ });
493
+
494
+ it('should call function', async () => {
495
+ browser.commands.reset.resolves(undefined);
496
+ browser.commands.update.rejects();
497
+ const res = await func('foo', '');
498
+ assert.strictEqual(browser.commands.reset.calledOnce, true, 'called');
499
+ assert.strictEqual(browser.commands.update.calledOnce, false,
500
+ 'not called');
501
+ assert.strictEqual(res, undefined, 'result');
502
+ });
503
+
504
+ it('should call function', async () => {
505
+ browser.commands.reset.rejects();
506
+ browser.commands.update.resolves(undefined);
507
+ const items = [
508
+ 'Alt+1', 'Command+1', 'Ctrl+1', 'MacCtrl+1',
509
+ 'Alt+Shift+1', 'Command+Shift+1', 'Ctrl+Shift+1', 'MacCtrl+Shift+1',
510
+ 'Alt+Command+1', 'Alt+Ctrl+1', 'Alt+MacCtrl+1',
511
+ 'Command+Alt+1', 'Command+MacCtrl+1',
512
+ 'Ctrl+Alt+1', 'Ctrl+MacCtrl+1',
513
+ 'MacCtrl+Alt+1', 'MacCtrl+Command+1', 'MacCtrl+Ctrl+1',
514
+ 'Alt+a', 'Command+a', 'Ctrl+a', 'MacCtrl+a',
515
+ 'Alt+Shift+a', 'Command+Shift+a', 'Ctrl+Shift+a', 'MacCtrl+Shift+a',
516
+ 'Alt+Command+a', 'Alt+Ctrl+a', 'Alt+MacCtrl+a',
517
+ 'Command+Alt+a', 'Command+MacCtrl+a',
518
+ 'Ctrl+Alt+a', 'Ctrl+MacCtrl+a',
519
+ 'MacCtrl+Alt+a', 'MacCtrl+Command+a', 'MacCtrl+Ctrl+a',
520
+ 'Alt+F1', 'Command+F1', 'Ctrl+F1', 'MacCtrl+F1',
521
+ 'Alt+Shift+F1', 'Command+Shift+F1', 'Ctrl+Shift+F1', 'MacCtrl+Shift+F1',
522
+ 'Alt+Command+F1', 'Alt+Ctrl+F1', 'Alt+MacCtrl+F1',
523
+ 'Command+Alt+F1', 'Command+MacCtrl+F1',
524
+ 'Ctrl+Alt+F1', 'Ctrl+MacCtrl+F1',
525
+ 'MacCtrl+Alt+F1', 'MacCtrl+Command+F1', 'MacCtrl+Ctrl+F1',
526
+ 'Alt+PageDown', 'Command+PageDown', 'Ctrl+PageDown', 'MacCtrl+PageDown',
527
+ 'Alt+Shift+PageDown', 'Command+Shift+PageDown', 'Ctrl+Shift+PageDown',
528
+ 'MacCtrl+Shift+PageDown',
529
+ 'Alt+Command+PageDown', 'Alt+Ctrl+PageDown', 'Alt+MacCtrl+PageDown',
530
+ 'Command+Alt+PageDown', 'Command+MacCtrl+PageDown',
531
+ 'Ctrl+Alt+PageDown', 'Ctrl+MacCtrl+PageDown',
532
+ 'MacCtrl+Alt+PageDown', 'MacCtrl+Command+PageDown',
533
+ 'MacCtrl+Ctrl+PageDown',
534
+ 'Alt+Up', 'Command+Up', 'Ctrl+Up', 'MacCtrl+Up',
535
+ 'Alt+Shift+Up', 'Command+Shift+Up', 'Ctrl+Shift+Up', 'MacCtrl+Shift+Up',
536
+ 'Alt+Command+Up', 'Alt+Ctrl+Up', 'Alt+MacCtrl+Up',
537
+ 'Command+Alt+Up', 'Command+MacCtrl+Up',
538
+ 'Ctrl+Alt+Up', 'Ctrl+MacCtrl+Up',
539
+ 'MacCtrl+Alt+Up', 'MacCtrl+Command+Up', 'MacCtrl+Ctrl+Up',
540
+ 'Alt+Left', 'Command+Left', 'Ctrl+Left', 'MacCtrl+Left',
541
+ 'Alt+Shift+Left', 'Command+Shift+Left', 'Ctrl+Shift+Left',
542
+ 'MacCtrl+Shift+Left',
543
+ 'Alt+Command+Left', 'Alt+Ctrl+Left', 'Alt+MacCtrl+Left',
544
+ 'Command+Alt+Left', 'Command+MacCtrl+Left',
545
+ 'Ctrl+Alt+Left', 'Ctrl+MacCtrl+Left',
546
+ 'MacCtrl+Alt+Left', 'MacCtrl+Command+Left', 'MacCtrl+Ctrl+Left',
547
+ 'F1', 'F12',
548
+ 'MediaNextTrack', 'MediaPrevTrack', 'MediaPlayPause', 'MediaStop'
549
+ ];
550
+ for (const item of items) {
551
+ const i = browser.commands.update.callCount;
552
+ // eslint-disable-next-line no-await-in-loop
553
+ const res = await func('foo', item);
554
+ assert.strictEqual(browser.commands.update.callCount, i + 1,
555
+ `called ${item}`);
556
+ assert.strictEqual(browser.commands.reset.calledOnce, false,
557
+ 'not called');
558
+ assert.strictEqual(res, undefined, 'result');
559
+ }
560
+ });
561
+
562
+ it('should call function', async () => {
563
+ browser.commands.reset.rejects();
564
+ browser.commands.update.resolves(undefined);
565
+ const res = await func('foo', ' Ctrl+a ');
566
+ assert.strictEqual(browser.commands.update.calledOnce, true, 'called');
567
+ assert.strictEqual(browser.commands.reset.calledOnce, false,
568
+ 'not called');
569
+ assert.strictEqual(res, undefined, 'result');
570
+ });
571
+ });
572
+
573
+ describe('get all contextual identities', () => {
574
+ const func = mjs.getAllContextualIdentities;
575
+
576
+ it('should not call function if permission is not granted', async () => {
577
+ browser.permissions.contains.resolves(false);
578
+ const i = browser.contextualIdentities.query.callCount;
579
+ const res = await func();
580
+ assert.strictEqual(browser.contextualIdentities.query.callCount, i,
581
+ 'not called');
582
+ assert.deepEqual(res, null, 'result');
583
+ });
584
+
585
+ it('should throw', async () => {
586
+ browser.contextualIdentities.query.rejects(new Error('error'));
587
+ await func().catch(e => {
588
+ assert.deepStrictEqual(e, new Error('error'));
589
+ });
590
+ });
591
+
592
+ it('should get result', async () => {
593
+ browser.contextualIdentities.query.withArgs({}).resolves([
594
+ {
595
+ foo: 'bar'
596
+ },
597
+ {
598
+ baz: 'qux'
599
+ }
600
+ ]);
601
+ const res = await func();
602
+ assert.strictEqual(Array.isArray(res), true, 'array');
603
+ assert.deepEqual(res, [{ foo: 'bar' }, { baz: 'qux' }], 'result');
604
+ });
605
+ });
606
+
607
+ describe('get contextual identities', () => {
608
+ const func = mjs.getContextualId;
609
+
610
+ it('should throw if no argument given', async () => {
611
+ await func().catch(e => {
612
+ assert.deepStrictEqual(e,
613
+ new TypeError('Expected String but got Undefined.'));
614
+ });
615
+ });
616
+
617
+ it('should throw if given argument is not string', async () => {
618
+ await func(1).catch(e => {
619
+ assert.deepStrictEqual(e,
620
+ new TypeError('Expected String but got Number.'));
621
+ });
622
+ });
623
+
624
+ it('should not call function if permission is not granted', async () => {
625
+ browser.permissions.contains.resolves(false);
626
+ const i = browser.contextualIdentities.get.callCount;
627
+ const res = await func('foo');
628
+ assert.strictEqual(browser.contextualIdentities.get.callCount, i,
629
+ 'not called');
630
+ assert.deepEqual(res, null, 'result');
631
+ });
632
+
633
+ it('should throw', async () => {
634
+ browser.contextualIdentities.get.withArgs('foo')
635
+ .rejects(new Error('error'));
636
+ await func('foo').catch(e => {
637
+ assert.deepStrictEqual(e, new Error('error'));
638
+ });
639
+ });
640
+
641
+ it('should get result', async () => {
642
+ browser.contextualIdentities.get.withArgs('foo').resolves({});
643
+ const res = await func('foo');
644
+ assert.deepEqual(res, {}, 'result');
645
+ });
646
+ });
647
+
648
+ describe('get enabled theme', () => {
649
+ const func = mjs.getEnabledTheme;
650
+
651
+ it('should not call function if permission is not granted', async () => {
652
+ browser.permissions.contains.resolves(false);
653
+ const i = browser.management.getAll.callCount;
654
+ const res = await func();
655
+ assert.strictEqual(browser.management.getAll.callCount, i, 'not called');
656
+ assert.deepEqual(res, null, 'result');
657
+ });
658
+
659
+ it('should get null', async () => {
660
+ const res = await func();
661
+ assert.deepEqual(res, null, 'result');
662
+ });
663
+
664
+ it('should throw', async () => {
665
+ browser.management.getAll.rejects(new Error('error'));
666
+ await func().catch(e => {
667
+ assert.deepStrictEqual(e, new Error('error'));
668
+ });
669
+ });
670
+
671
+ it('should get null', async () => {
672
+ browser.management.getAll.resolves([]);
673
+ const res = await func();
674
+ assert.deepEqual(res, null, 'result');
675
+ });
676
+
677
+ it('should get array', async () => {
678
+ browser.management.getAll.resolves([
679
+ {
680
+ enabled: false,
681
+ type: 'theme'
682
+ },
683
+ {
684
+ enabled: true,
685
+ type: 'foo'
686
+ },
687
+ {
688
+ enabled: true,
689
+ type: 'theme'
690
+ },
691
+ {
692
+ enabled: false,
693
+ type: 'bar'
694
+ }
695
+ ]);
696
+ const stub = sinon.stub(console, 'error');
697
+ const res = await func();
698
+ const { called } = stub;
699
+ stub.restore();
700
+ assert.strictEqual(called, false, 'not logged');
701
+ assert.deepEqual(res, [
702
+ {
703
+ enabled: true,
704
+ type: 'theme'
705
+ }
706
+ ], 'result');
707
+ });
708
+ });
709
+
710
+ describe('get extension info', () => {
711
+ const func = mjs.getExtensionInfo;
712
+
713
+ it('should throw if no argument given', async () => {
714
+ await func().catch(e => {
715
+ assert.deepStrictEqual(e,
716
+ new TypeError('Expected String but got Undefined.'));
717
+ });
718
+ });
719
+
720
+ it('should throw if given argument is not string', async () => {
721
+ await func(1).catch(e => {
722
+ assert.deepStrictEqual(e,
723
+ new TypeError('Expected String but got Number.'));
724
+ });
725
+ });
726
+
727
+ it('should not call function if permission is not granted', async () => {
728
+ browser.permissions.contains.resolves(false);
729
+ const i = browser.management.get.callCount;
730
+ const res = await func('foo');
731
+ assert.strictEqual(browser.management.get.callCount, i, 'not called');
732
+ assert.deepEqual(res, null, 'result');
733
+ });
734
+
735
+ it('should reject if given id is not found', async () => {
736
+ browser.management.get.withArgs('foo').rejects(new Error('error'));
737
+ await func('foo').catch(e => {
738
+ assert.deepStrictEqual(e, new Error('error'));
739
+ });
740
+ });
741
+
742
+ it('should get object', async () => {
743
+ browser.management.get.withArgs('foo').resolves({});
744
+ const res = await func('foo');
745
+ assert.deepEqual(res, {}, 'result');
746
+ });
747
+ });
748
+
749
+ describe('get external extensions', () => {
750
+ const func = mjs.getExternalExtensions;
751
+
752
+ it('should not call function if permission is not granted', async () => {
753
+ browser.permissions.contains.resolves(false);
754
+ const i = browser.management.getAll.callCount;
755
+ const res = await func();
756
+ assert.strictEqual(browser.management.getAll.callCount, i, 'not called');
757
+ assert.deepEqual(res, null, 'result');
758
+ });
759
+
760
+ it('should get null', async () => {
761
+ const res = await func();
762
+ assert.deepEqual(res, null, 'result');
763
+ });
764
+
765
+ it('should throw', async () => {
766
+ browser.management.getAll.rejects(new Error('error'));
767
+ await func().catch(e => {
768
+ assert.deepStrictEqual(e, new Error('error'));
769
+ });
770
+ });
771
+
772
+ it('should get null', async () => {
773
+ browser.management.getAll.resolves([]);
774
+ const res = await func();
775
+ assert.deepEqual(res, null, 'result');
776
+ });
777
+
778
+ it('should get array', async () => {
779
+ browser.management.getAll.resolves([
780
+ {
781
+ type: 'extension'
782
+ },
783
+ {
784
+ type: 'foo'
785
+ },
786
+ {
787
+ type: 'extension'
788
+ },
789
+ {
790
+ type: 'bar'
791
+ }
792
+ ]);
793
+ const res = await func();
794
+ assert.deepEqual(res, [
795
+ {
796
+ type: 'extension'
797
+ },
798
+ {
799
+ type: 'extension'
800
+ }
801
+ ], 'result');
802
+ });
803
+ });
804
+
805
+ describe('clear notification', () => {
806
+ const func = mjs.clearNotification;
807
+
808
+ it('should throw if no argument given', async () => {
809
+ await func().catch(e => {
810
+ assert.deepStrictEqual(e,
811
+ new TypeError('Expected String but got Undefined.'));
812
+ });
813
+ });
814
+
815
+ it('should throw if given argument is not string', async () => {
816
+ await func(1).catch(e => {
817
+ assert.deepStrictEqual(e,
818
+ new TypeError('Expected String but got Number.'));
819
+ });
820
+ });
821
+
822
+ it('should not call function if permission is not granted', async () => {
823
+ browser.permissions.contains.resolves(false);
824
+ const i = browser.notifications.clear.callCount;
825
+ const res = await func('foo');
826
+ assert.strictEqual(browser.notifications.clear.callCount, i,
827
+ 'not called');
828
+ assert.deepEqual(res, null, 'result');
829
+ });
830
+
831
+ it('should get result', async () => {
832
+ browser.notifications.clear.withArgs('foo').resolves(true);
833
+ const res = await func('foo');
834
+ assert.strictEqual(res, true, 'result');
835
+ });
836
+ });
837
+
838
+ describe('create notification', () => {
839
+ const func = mjs.createNotification;
840
+
841
+ it('should throw if no argument given', async () => {
842
+ await func().catch(e => {
843
+ assert.deepStrictEqual(e,
844
+ new TypeError('Expected String but got Undefined.'));
845
+ });
846
+ });
847
+
848
+ it('should throw if first argument is not string', async () => {
849
+ await func(1).catch(e => {
850
+ assert.deepStrictEqual(e,
851
+ new TypeError('Expected String but got Number.'));
852
+ });
853
+ });
854
+
855
+ it('should not call function if permission is not granted', async () => {
856
+ browser.permissions.contains.resolves(false);
857
+ const i = browser.notifications.create.callCount;
858
+ const res = await func('foo');
859
+ assert.strictEqual(browser.notifications.create.callCount, i,
860
+ 'not called');
861
+ assert.deepEqual(res, null, 'result');
862
+ });
863
+
864
+ it('should get message', async () => {
865
+ browser.notifications.create.withArgs('foo', {}).resolves('bar');
866
+ const res = await func('foo', {});
867
+ assert.strictEqual(res, 'bar', 'result');
868
+ });
869
+
870
+ it('should get message', async () => {
871
+ browser.notifications.onClosed.hasListener.returns(false);
872
+ browser.notifications.create.withArgs('foo', {}).resolves('bar');
873
+ const i = browser.notifications.onClosed.addListener.callCount;
874
+ const res = await func('foo', {});
875
+ assert.strictEqual(browser.notifications.onClosed.addListener.callCount,
876
+ i + 1, 'called');
877
+ assert.strictEqual(res, 'bar', 'result');
878
+ });
879
+
880
+ it('should get message', async () => {
881
+ browser.notifications.onClosed.hasListener.returns(true);
882
+ browser.notifications.create.withArgs('foo', {}).resolves('bar');
883
+ const i = browser.notifications.onClosed.addListener.callCount;
884
+ const res = await func('foo', {});
885
+ assert.strictEqual(browser.notifications.onClosed.addListener.callCount,
886
+ i, 'not called');
887
+ assert.strictEqual(res, 'bar', 'result');
888
+ });
889
+ });
890
+
891
+ describe('remove permission', () => {
892
+ const func = mjs.removePermission;
893
+
894
+ it('should throw if no argument given', async () => {
895
+ await func().catch(e => {
896
+ assert.deepStrictEqual(e,
897
+ new TypeError('Expected String or Array but got Undefined.'));
898
+ });
899
+ });
900
+
901
+ it('should throw if given argument is not string or array', async () => {
902
+ await func(1).catch(e => {
903
+ assert.deepStrictEqual(e,
904
+ new TypeError('Expected String or Array but got Number.'));
905
+ });
906
+ });
907
+
908
+ it('should get result', async () => {
909
+ browser.permissions.remove.withArgs({ permissions: ['foo'] })
910
+ .resolves(true);
911
+ const res = await func('foo');
912
+ assert.strictEqual(res, true, 'result');
913
+ });
914
+
915
+ it('should get result', async () => {
916
+ browser.permissions.remove.withArgs({ permissions: ['foo'] })
917
+ .resolves(false);
918
+ const res = await func('foo');
919
+ assert.strictEqual(res, false, 'result');
920
+ });
921
+
922
+ it('should get result', async () => {
923
+ browser.permissions.remove.withArgs({ permissions: ['foo'] })
924
+ .resolves(true);
925
+ const res = await func(['foo']);
926
+ assert.strictEqual(res, true, 'result');
927
+ });
928
+ });
929
+
930
+ describe('request permission', () => {
931
+ const func = mjs.requestPermission;
932
+
933
+ it('should throw if no argument given', async () => {
934
+ await func().catch(e => {
935
+ assert.deepStrictEqual(e,
936
+ new TypeError('Expected String or Array but got Undefined.'));
937
+ });
938
+ });
939
+
940
+ it('should throw if given argument is not string or array', async () => {
941
+ await func(1).catch(e => {
942
+ assert.deepStrictEqual(e,
943
+ new TypeError('Expected String or Array but got Number.'));
944
+ });
945
+ });
946
+
947
+ it('should get result', async () => {
948
+ browser.permissions.request.withArgs({ permissions: ['foo'] })
949
+ .resolves(true);
950
+ const res = await func('foo');
951
+ assert.strictEqual(res, true, 'result');
952
+ });
953
+
954
+ it('should get result', async () => {
955
+ browser.permissions.request.withArgs({ permissions: ['foo'] })
956
+ .resolves(false);
957
+ const res = await func('foo');
958
+ assert.strictEqual(res, false, 'result');
959
+ });
960
+
961
+ it('should get result', async () => {
962
+ browser.permissions.request.withArgs({ permissions: ['foo'] })
963
+ .resolves(true);
964
+ const res = await func(['foo']);
965
+ assert.strictEqual(res, true, 'result');
966
+ });
967
+ });
968
+
969
+ describe('get manifest icons', () => {
970
+ const func = mjs.getManifestIcons;
971
+
972
+ it('should get object', () => {
973
+ browser.runtime.getManifest.returns({ icons: { foo: 'bar' } });
974
+ const res = func();
975
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
976
+ });
977
+ });
978
+
979
+ describe('get manifest version', () => {
980
+ const func = mjs.getManifestVersion;
981
+
982
+ it('should get result', () => {
983
+ browser.runtime.getManifest.returns({ manifest_version: 2 });
984
+ const res = func();
985
+ assert.strictEqual(res, 2, 'result');
986
+ });
987
+
988
+ it('should get result', () => {
989
+ browser.runtime.getManifest.returns({ manifest_version: 3 });
990
+ const res = func();
991
+ assert.strictEqual(res, 3, 'result');
992
+ });
993
+ });
994
+
995
+ describe('get OS', () => {
996
+ const func = mjs.getOs;
997
+
998
+ it('should get string', async () => {
999
+ browser.runtime.getPlatformInfo.resolves({ os: 'foo' });
1000
+ const res = await func();
1001
+ assert.strictEqual(res, 'foo', 'result');
1002
+ });
1003
+ });
1004
+
1005
+ describe('make a connection', () => {
1006
+ const func = mjs.makeConnection;
1007
+
1008
+ it('should get object', async () => {
1009
+ browser.tabs.connect.withArgs(1).resolves({ bar: 'baz' });
1010
+ const res = await func(1);
1011
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1012
+ });
1013
+
1014
+ it('should get object', async () => {
1015
+ browser.tabs.connect.withArgs(1, { foo: 'bar' }).resolves({ bar: 'baz' });
1016
+ const res = await func(1, { foo: 'bar' });
1017
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1018
+ });
1019
+
1020
+ it('should get object', async () => {
1021
+ browser.tabs.connect.withArgs(1, { foo: 'bar' }).resolves({ bar: 'baz' });
1022
+ const res = await func(1, { foo: 'bar' });
1023
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1024
+ });
1025
+
1026
+ it('should get object', async () => {
1027
+ browser.runtime.connect.withArgs('foo').resolves({ bar: 'baz' });
1028
+ const res = await func('foo');
1029
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1030
+ });
1031
+
1032
+ it('should get object', async () => {
1033
+ browser.runtime.connect.withArgs('foo', { foo: 'bar' })
1034
+ .resolves({ bar: 'baz' });
1035
+ const res = await func('foo', { foo: 'bar' });
1036
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1037
+ });
1038
+
1039
+ it('should get object', async () => {
1040
+ browser.runtime.connectNative.withArgs('foo').resolves({ bar: 'baz' });
1041
+ const res = await func('foo', true);
1042
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1043
+ });
1044
+
1045
+ it('should get object', async () => {
1046
+ browser.runtime.connect.withArgs({ foo: 'bar' }).resolves({ bar: 'baz' });
1047
+ const res = await func({ foo: 'bar' });
1048
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1049
+ });
1050
+
1051
+ it('should get object', async () => {
1052
+ browser.runtime.connect.withArgs({ foo: 'bar' }).resolves({ bar: 'baz' });
1053
+ const res = await func(null, { foo: 'bar' });
1054
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1055
+ });
1056
+
1057
+ it('should get object', async () => {
1058
+ browser.runtime.connect.withArgs().resolves({ bar: 'baz' });
1059
+ const res = await func();
1060
+ assert.deepEqual(res, { bar: 'baz' }, 'result');
1061
+ });
1062
+ });
1063
+
1064
+ describe('send message', () => {
1065
+ const func = mjs.sendMessage;
1066
+
1067
+ it('should not call function', async () => {
1068
+ const i = browser.tabs.sendMessage.callCount;
1069
+ const j = browser.runtime.sendMessage.callCount;
1070
+ const res = await func();
1071
+ assert.strictEqual(browser.tabs.sendMessage.callCount, i, 'not called');
1072
+ assert.strictEqual(browser.runtime.sendMessage.callCount, j,
1073
+ 'not called');
1074
+ assert.deepEqual(res, null, 'result');
1075
+ });
1076
+
1077
+ it('should call function', async () => {
1078
+ browser.runtime.sendMessage.withArgs('foo', null).resolves({});
1079
+ const i = browser.runtime.sendMessage.callCount;
1080
+ const res = await func(null, 'foo');
1081
+ assert.strictEqual(browser.runtime.sendMessage.callCount, i + 1,
1082
+ 'called');
1083
+ assert.deepEqual(res, {}, 'result');
1084
+ });
1085
+
1086
+ it('should call function', async () => {
1087
+ browser.runtime.sendMessage.withArgs('foo', { bar: 'baz' })
1088
+ .resolves({});
1089
+ const i = browser.runtime.sendMessage.callCount;
1090
+ const res = await func(null, 'foo', { bar: 'baz' });
1091
+ assert.strictEqual(browser.runtime.sendMessage.callCount, i + 1,
1092
+ 'called');
1093
+ assert.deepEqual(res, {}, 'result');
1094
+ });
1095
+
1096
+ it('should call function', async () => {
1097
+ browser.runtime.sendMessage.withArgs('foo', 'bar', null).resolves({});
1098
+ const i = browser.runtime.sendMessage.callCount;
1099
+ const res = await func('foo', 'bar');
1100
+ assert.strictEqual(browser.runtime.sendMessage.callCount, i + 1,
1101
+ 'called');
1102
+ assert.deepEqual(res, {}, 'result');
1103
+ });
1104
+
1105
+ it('should call function', async () => {
1106
+ browser.tabs.sendMessage.withArgs(1, 'foo', null).resolves({});
1107
+ const i = browser.tabs.sendMessage.callCount;
1108
+ const res = await func(1, 'foo');
1109
+ assert.strictEqual(browser.tabs.sendMessage.callCount, i + 1, 'called');
1110
+ assert.deepEqual(res, {}, 'result');
1111
+ });
1112
+
1113
+ it('should not call function', async () => {
1114
+ const i = browser.tabs.sendMessage.callCount;
1115
+ const j = browser.runtime.sendMessage.callCount;
1116
+ const res = await func(browser.tabs.TAB_ID_NONE, 'foo');
1117
+ assert.strictEqual(browser.tabs.sendMessage.callCount, i, 'not called');
1118
+ assert.strictEqual(browser.runtime.sendMessage.callCount, j,
1119
+ 'not called');
1120
+ assert.deepEqual(res, null, 'result');
1121
+ });
1122
+ });
1123
+
1124
+ describe('is scripting available', () => {
1125
+ const func = mjs.isScriptingAvailable;
1126
+
1127
+ it('should get false if permission is not granted', async () => {
1128
+ browser.permissions.contains.resolves(false);
1129
+ const res = await func();
1130
+ assert.strictEqual(res, false, 'result');
1131
+ });
1132
+
1133
+ it('should get true', async () => {
1134
+ const res = await func();
1135
+ assert.strictEqual(res, true, 'result');
1136
+ });
1137
+ });
1138
+
1139
+ describe('execute script to tab', () => {
1140
+ const func = mjs.executeScriptToTab;
1141
+
1142
+ it('should not call function if permission is not granted', async () => {
1143
+ browser.permissions.contains.resolves(false);
1144
+ browser.scripting.executeScript.resolves([{}]);
1145
+ const i = browser.scripting.executeScript.callCount;
1146
+ const res = await func();
1147
+ assert.strictEqual(browser.scripting.executeScript.callCount, i,
1148
+ 'not called');
1149
+ assert.deepEqual(res, null, 'result');
1150
+ });
1151
+
1152
+ it('should not call function if no tabId in target', async () => {
1153
+ browser.scripting.executeScript.resolves([{}]);
1154
+ const i = browser.scripting.executeScript.callCount;
1155
+ const res = await func({
1156
+ target: {}
1157
+ });
1158
+ assert.strictEqual(browser.scripting.executeScript.callCount, i,
1159
+ 'not called');
1160
+ assert.deepEqual(res, null, 'result');
1161
+ });
1162
+
1163
+ it('should not call function if files / func is not included', async () => {
1164
+ browser.scripting.executeScript.resolves([{}]);
1165
+ const i = browser.scripting.executeScript.callCount;
1166
+ const res = await func({
1167
+ target: {
1168
+ tabId: 1
1169
+ }
1170
+ });
1171
+ assert.strictEqual(browser.scripting.executeScript.callCount, i,
1172
+ 'not called');
1173
+ assert.deepEqual(res, null, 'result');
1174
+ });
1175
+
1176
+ it('should not call function if files is an empty array', async () => {
1177
+ browser.scripting.executeScript.resolves([{}]);
1178
+ const i = browser.scripting.executeScript.callCount;
1179
+ const res = await func({
1180
+ files: [],
1181
+ target: {
1182
+ tabId: 1
1183
+ }
1184
+ });
1185
+ assert.strictEqual(browser.scripting.executeScript.callCount, i,
1186
+ 'not called');
1187
+ assert.deepEqual(res, null, 'result');
1188
+ });
1189
+
1190
+ it('should throw', async () => {
1191
+ browser.scripting.executeScript.rejects(new Error('error'));
1192
+ const stubErr = sinon.stub(console, 'error');
1193
+ await func({
1194
+ files: ['foo/bar'],
1195
+ target: {
1196
+ tabId: 1
1197
+ }
1198
+ }).catch(e => {
1199
+ assert.deepStrictEqual(e, new Error('error'));
1200
+ });
1201
+ stubErr.restore();
1202
+ });
1203
+
1204
+ it('should call function', async () => {
1205
+ browser.scripting.executeScript.resolves([{}]);
1206
+ const i = browser.scripting.executeScript.callCount;
1207
+ const res = await func({
1208
+ files: ['foo/bar'],
1209
+ target: {
1210
+ tabId: 1
1211
+ }
1212
+ });
1213
+ assert.strictEqual(browser.scripting.executeScript.callCount, i + 1,
1214
+ 'called');
1215
+ assert.deepEqual(res, [{}], 'result');
1216
+ });
1217
+
1218
+ it('should not call function if func is not a function', async () => {
1219
+ browser.scripting.executeScript.resolves([{}]);
1220
+ const i = browser.scripting.executeScript.callCount;
1221
+ const res = await func({
1222
+ func: 'foo',
1223
+ target: {
1224
+ tabId: 1
1225
+ }
1226
+ });
1227
+ assert.strictEqual(browser.scripting.executeScript.callCount, i,
1228
+ 'not called');
1229
+ assert.deepEqual(res, null, 'result');
1230
+ });
1231
+
1232
+ it('should call function', async () => {
1233
+ browser.scripting.executeScript.resolves([{}]);
1234
+ const i = browser.scripting.executeScript.callCount;
1235
+ const res = await func({
1236
+ func: () => {},
1237
+ target: {
1238
+ tabId: 1
1239
+ }
1240
+ });
1241
+ assert.strictEqual(browser.scripting.executeScript.callCount, i + 1,
1242
+ 'called');
1243
+ assert.deepEqual(res, [{}], 'result');
1244
+ });
1245
+
1246
+ it('should call function', async () => {
1247
+ browser.scripting.executeScript.resolves([{}]);
1248
+ const i = browser.scripting.executeScript.callCount;
1249
+ const res = await func({
1250
+ args: ['foo'],
1251
+ func: arg => arg,
1252
+ target: {
1253
+ tabId: 1
1254
+ }
1255
+ });
1256
+ assert.strictEqual(browser.scripting.executeScript.callCount, i + 1,
1257
+ 'called');
1258
+ assert.deepEqual(res, [{}], 'result');
1259
+ });
1260
+ });
1261
+
1262
+ describe('search in new tab', () => {
1263
+ const func = mjs.searchInNewTab;
1264
+
1265
+ it('should throw if no argument given', async () => {
1266
+ await func().catch(e => {
1267
+ assert.strictEqual(e.message,
1268
+ 'Expected String but got Undefined.');
1269
+ });
1270
+ });
1271
+
1272
+ it('should throw if 1st argument is not string', async () => {
1273
+ await func(1).catch(e => {
1274
+ assert.strictEqual(e.message,
1275
+ 'Expected String but got Number.');
1276
+ });
1277
+ });
1278
+
1279
+ it('should not call function if permission is not granted', async () => {
1280
+ browser.permissions.contains.resolves(false);
1281
+ const i = browser.search.query.callCount;
1282
+ const res = await func('foo');
1283
+ assert.strictEqual(browser.search.query.callCount, i, 'not called');
1284
+ assert.deepEqual(res, null, 'result');
1285
+ });
1286
+
1287
+ it('should call function', async () => {
1288
+ browser.tabs.create.resolves({
1289
+ id: 1
1290
+ });
1291
+ browser.tabs.get.withArgs(1).resolves({
1292
+ id: 1
1293
+ });
1294
+ const i = browser.search.query.withArgs({
1295
+ text: 'foo',
1296
+ tabId: 1
1297
+ }).callCount;
1298
+ const res = await func('foo');
1299
+ assert.strictEqual(browser.search.query.withArgs({
1300
+ text: 'foo',
1301
+ tabId: 1
1302
+ }).callCount, i + 1, 'called');
1303
+ assert.deepEqual(res, {
1304
+ id: 1
1305
+ }, 'result');
1306
+ });
1307
+
1308
+ it('should call function', async () => {
1309
+ browser.tabs.create.withArgs({
1310
+ index: 2
1311
+ }).resolves({
1312
+ id: 1,
1313
+ index: 2
1314
+ });
1315
+ browser.tabs.get.withArgs(1).resolves({
1316
+ id: 1,
1317
+ index: 2
1318
+ });
1319
+ const i = browser.search.query.withArgs({
1320
+ text: 'foo',
1321
+ tabId: 1
1322
+ }).callCount;
1323
+ const res = await func('foo', {
1324
+ index: 2
1325
+ });
1326
+ assert.strictEqual(browser.search.query.withArgs({
1327
+ text: 'foo',
1328
+ tabId: 1
1329
+ }).callCount, i + 1, 'called');
1330
+ assert.deepEqual(res, {
1331
+ id: 1,
1332
+ index: 2
1333
+ }, 'result');
1334
+ });
1335
+ });
1336
+
1337
+ describe('search with a search engine', () => {
1338
+ const func = mjs.searchWithSearchEngine;
1339
+
1340
+ it('should throw if no argument given', async () => {
1341
+ await func().catch(e => {
1342
+ assert.deepStrictEqual(e,
1343
+ new TypeError('Expected String but got Undefined.'));
1344
+ });
1345
+ });
1346
+
1347
+ it('should throw if 1st argument is not string', async () => {
1348
+ await func(1).catch(e => {
1349
+ assert.deepStrictEqual(e,
1350
+ new TypeError('Expected String but got Number.'));
1351
+ });
1352
+ });
1353
+
1354
+ it('should not call function if permission is not granted', async () => {
1355
+ browser.permissions.contains.resolves(false);
1356
+ const i = browser.search.search.callCount;
1357
+ await func('foo');
1358
+ assert.strictEqual(browser.search.search.callCount, i, 'not called');
1359
+ });
1360
+
1361
+ it('should call function', async () => {
1362
+ const i = browser.search.search.withArgs({
1363
+ query: 'foo'
1364
+ }).callCount;
1365
+ await func('foo');
1366
+ assert.strictEqual(browser.search.search.withArgs({
1367
+ query: 'foo'
1368
+ }).callCount, i + 1, 'called');
1369
+ });
1370
+
1371
+ it('should call function', async () => {
1372
+ const i = browser.search.search.withArgs({
1373
+ engine: 'bar',
1374
+ query: 'foo',
1375
+ tabId: 1
1376
+ }).callCount;
1377
+ await func('foo', {
1378
+ engine: 'bar',
1379
+ tabId: 1
1380
+ });
1381
+ assert.strictEqual(browser.search.search.withArgs({
1382
+ engine: 'bar',
1383
+ query: 'foo',
1384
+ tabId: 1
1385
+ }).callCount, i + 1, 'called');
1386
+ });
1387
+ });
1388
+
1389
+ describe('get recently closed tab', () => {
1390
+ const func = mjs.getRecentlyClosedTab;
1391
+
1392
+ it('should not call function if permission is not granted', async () => {
1393
+ browser.permissions.contains.resolves(false);
1394
+ const i = browser.sessions.getRecentlyClosed.callCount;
1395
+ const res = await func();
1396
+ assert.strictEqual(browser.sessions.getRecentlyClosed.callCount, i,
1397
+ 'not called');
1398
+ assert.deepEqual(res, null, 'result');
1399
+ });
1400
+
1401
+ it('should get null', async () => {
1402
+ browser.sessions.getRecentlyClosed.resolves([]);
1403
+ const res = await func();
1404
+ assert.deepEqual(res, null, 'result');
1405
+ });
1406
+
1407
+ it('should get null', async () => {
1408
+ browser.sessions.getRecentlyClosed.resolves([]);
1409
+ const res = await func(1);
1410
+ assert.deepEqual(res, null, 'result');
1411
+ });
1412
+
1413
+ it('should get null', async () => {
1414
+ browser.sessions.getRecentlyClosed.resolves([{}]);
1415
+ const res = await func();
1416
+ assert.deepEqual(res, null, 'result');
1417
+ });
1418
+
1419
+ it('should get null', async () => {
1420
+ browser.sessions.getRecentlyClosed.resolves([{ tab: { windowId: 2 } }]);
1421
+ const res = await func(1);
1422
+ assert.deepEqual(res, null, 'result');
1423
+ });
1424
+
1425
+ it('should get object', async () => {
1426
+ const tab = { windowId: 1 };
1427
+ browser.sessions.getRecentlyClosed.resolves([{ tab }]);
1428
+ const res = await func(1);
1429
+ assert.deepEqual(res, tab, 'result');
1430
+ });
1431
+
1432
+ it('should get object', async () => {
1433
+ const tab = { windowId: 1 };
1434
+ browser.sessions.getRecentlyClosed.resolves([
1435
+ { tab: { windowId: 2 } },
1436
+ { tab }
1437
+ ]);
1438
+ const res = await func(1);
1439
+ assert.deepEqual(res, tab, 'result');
1440
+ });
1441
+ });
1442
+
1443
+ describe('get session window value', () => {
1444
+ const func = mjs.getSessionWindowValue;
1445
+
1446
+ it('should throw if no argument given', async () => {
1447
+ await func().catch(e => {
1448
+ assert.deepStrictEqual(e,
1449
+ new TypeError('Expected String but got Undefined.'));
1450
+ });
1451
+ });
1452
+
1453
+ it('should throw if given argument is not string', async () => {
1454
+ await func(1).catch(e => {
1455
+ assert.deepStrictEqual(e,
1456
+ new TypeError('Expected String but got Number.'));
1457
+ });
1458
+ });
1459
+
1460
+ it('should not call function if permission is not granted', async () => {
1461
+ browser.permissions.contains.resolves(false);
1462
+ const i = browser.sessions.getWindowValue.callCount;
1463
+ const res = await func('foo', 1);
1464
+ assert.strictEqual(browser.sessions.getWindowValue.callCount, i,
1465
+ 'not called');
1466
+ assert.deepEqual(res, null, 'result');
1467
+ });
1468
+
1469
+ it('should get object', async () => {
1470
+ browser.sessions.getWindowValue.withArgs(1, 'foo').resolves('bar');
1471
+ const res = await func('foo', 1);
1472
+ assert.strictEqual(res, 'bar', 'result');
1473
+ });
1474
+
1475
+ it('should get object', async () => {
1476
+ browser.sessions.getWindowValue
1477
+ .withArgs(browser.windows.WINDOW_ID_CURRENT, 'foo').resolves('bar');
1478
+ const res = await func('foo');
1479
+ assert.strictEqual(res, 'bar', 'result');
1480
+ });
1481
+ });
1482
+
1483
+ describe('restore session', () => {
1484
+ const func = mjs.restoreSession;
1485
+
1486
+ it('should throw if no argument given', async () => {
1487
+ await func().catch(e => {
1488
+ assert.deepStrictEqual(e,
1489
+ new TypeError('Expected String but got Undefined.'));
1490
+ });
1491
+ });
1492
+
1493
+ it('should throw if given argument is not string', async () => {
1494
+ await func(1).catch(e => {
1495
+ assert.deepStrictEqual(e,
1496
+ new TypeError('Expected String but got Number.'));
1497
+ });
1498
+ });
1499
+
1500
+ it('should not call function if permission is not granted', async () => {
1501
+ browser.permissions.contains.resolves(false);
1502
+ const i = browser.sessions.getWindowValue.callCount;
1503
+ const res = await func('foo');
1504
+ assert.strictEqual(browser.sessions.getWindowValue.callCount, i,
1505
+ 'not called');
1506
+ assert.deepEqual(res, null, 'result');
1507
+ });
1508
+
1509
+ it('should get object', async () => {
1510
+ browser.sessions.restore.withArgs('foo').resolves({});
1511
+ const res = await func('foo');
1512
+ assert.deepEqual(res, {}, 'result');
1513
+ });
1514
+ });
1515
+
1516
+ describe('set session window value', () => {
1517
+ const func = mjs.setSessionWindowValue;
1518
+
1519
+ it('should throw if no argument given', async () => {
1520
+ await func().catch(e => {
1521
+ assert.deepStrictEqual(e,
1522
+ new TypeError('Expected String but got Undefined.'));
1523
+ });
1524
+ });
1525
+
1526
+ it('should throw if given argument is not string', async () => {
1527
+ await func(1).catch(e => {
1528
+ assert.deepStrictEqual(e,
1529
+ new TypeError('Expected String but got Number.'));
1530
+ });
1531
+ });
1532
+
1533
+ it('should not call function if permission is not granted', async () => {
1534
+ browser.permissions.contains.resolves(false);
1535
+ const i = browser.sessions.setWindowValue.callCount;
1536
+ await func('foo');
1537
+ assert.strictEqual(browser.sessions.setWindowValue.callCount, i,
1538
+ 'not called');
1539
+ });
1540
+
1541
+ it('should call function', async () => {
1542
+ const i = browser.sessions.setWindowValue.callCount;
1543
+ await func('foo', 'bar', 1);
1544
+ assert.strictEqual(browser.sessions.setWindowValue.callCount, i + 1,
1545
+ 'called');
1546
+ });
1547
+
1548
+ it('should call function', async () => {
1549
+ const i = browser.sessions.setWindowValue.callCount;
1550
+ await func('foo', 'bar');
1551
+ assert.strictEqual(browser.sessions.setWindowValue.callCount, i + 1,
1552
+ 'called');
1553
+ });
1554
+ });
1555
+
1556
+ describe('clear storage', () => {
1557
+ const func = mjs.clearStorage;
1558
+
1559
+ it('should not call function if permission is not granted', async () => {
1560
+ browser.permissions.contains.resolves(false);
1561
+ const i = browser.storage.local.clear.callCount;
1562
+ const j = browser.storage.managed.clear.callCount;
1563
+ // const k = browser.storage.session.clear.callCount;
1564
+ const l = browser.storage.sync.clear.callCount;
1565
+ await func();
1566
+ assert.strictEqual(browser.storage.local.clear.callCount, i,
1567
+ 'not called');
1568
+ assert.strictEqual(browser.storage.managed.clear.callCount, j,
1569
+ 'not called');
1570
+ /*
1571
+ assert.strictEqual(browser.storage.session.clear.callCount, k,
1572
+ 'not called');
1573
+ */
1574
+ assert.strictEqual(browser.storage.sync.clear.callCount, l, 'not called');
1575
+ });
1576
+
1577
+ it('should not call function', async () => {
1578
+ const i = browser.storage.local.clear.callCount;
1579
+ const j = browser.storage.managed.clear.callCount;
1580
+ // const k = browser.storage.session.clear.callCount;
1581
+ const l = browser.storage.sync.clear.callCount;
1582
+ await func('foo');
1583
+ assert.strictEqual(browser.storage.local.clear.callCount, i,
1584
+ 'not called');
1585
+ assert.strictEqual(browser.storage.managed.clear.callCount, j,
1586
+ 'not called');
1587
+ /*
1588
+ assert.strictEqual(browser.storage.session.clear.callCount, k,
1589
+ 'not called');
1590
+ */
1591
+ assert.strictEqual(browser.storage.sync.clear.callCount, l, 'not called');
1592
+ });
1593
+
1594
+ it('should call function', async () => {
1595
+ const i = browser.storage.local.clear.callCount;
1596
+ await func();
1597
+ assert.strictEqual(browser.storage.local.clear.callCount, i + 1,
1598
+ 'called');
1599
+ });
1600
+
1601
+ it('should call function', async () => {
1602
+ const i = browser.storage.local.clear.callCount;
1603
+ await func('local');
1604
+ assert.strictEqual(browser.storage.local.clear.callCount, i + 1,
1605
+ 'called');
1606
+ });
1607
+
1608
+ it('should not call function', async () => {
1609
+ const i = browser.storage.managed.clear.callCount;
1610
+ await func('managed');
1611
+ assert.strictEqual(browser.storage.managed.clear.callCount, i,
1612
+ 'not called');
1613
+ });
1614
+
1615
+ /*
1616
+ it('should call function', async () => {
1617
+ const i = browser.storage.session.clear.callCount;
1618
+ await func('session');
1619
+ assert.strictEqual(browser.storage.session.clear.callCount, i + 1,
1620
+ 'called');
1621
+ });
1622
+ */
1623
+
1624
+ it('should call function', async () => {
1625
+ const i = browser.storage.sync.clear.callCount;
1626
+ await func('sync');
1627
+ assert.strictEqual(browser.storage.sync.clear.callCount, i + 1, 'called');
1628
+ });
1629
+ });
1630
+
1631
+ describe('get all storage', () => {
1632
+ const func = mjs.getAllStorage;
1633
+
1634
+ it('should not call function if permission is not granted', async () => {
1635
+ browser.permissions.contains.resolves(false);
1636
+ const i = browser.storage.local.get.callCount;
1637
+ const j = browser.storage.managed.get.callCount;
1638
+ // const k = browser.storage.session.get.callCount;
1639
+ const l = browser.storage.sync.get.callCount;
1640
+ const res = await func();
1641
+ assert.strictEqual(browser.storage.local.get.callCount, i,
1642
+ 'not called');
1643
+ assert.strictEqual(browser.storage.managed.get.callCount, j,
1644
+ 'not called');
1645
+ /*
1646
+ assert.strictEqual(browser.storage.session.get.callCount, k,
1647
+ 'not called');
1648
+ */
1649
+ assert.strictEqual(browser.storage.sync.get.callCount, l, 'not called');
1650
+ assert.deepEqual(res, null, 'result');
1651
+ });
1652
+
1653
+ it('should not call function', async () => {
1654
+ const i = browser.storage.local.get.callCount;
1655
+ const j = browser.storage.managed.get.callCount;
1656
+ // const k = browser.storage.session.get.callCount;
1657
+ const l = browser.storage.sync.get.callCount;
1658
+ const res = await func('foo');
1659
+ assert.strictEqual(browser.storage.local.get.callCount, i, 'not called');
1660
+ assert.strictEqual(browser.storage.managed.get.callCount, j,
1661
+ 'not called');
1662
+ /*
1663
+ assert.strictEqual(browser.storage.session.get.callCount, k,
1664
+ 'not called');
1665
+ */
1666
+ assert.strictEqual(browser.storage.sync.get.callCount, l, 'not called');
1667
+ assert.deepEqual(res, null, 'result');
1668
+ });
1669
+
1670
+ it('should call function', async () => {
1671
+ browser.storage.local.get.resolves({ foo: 'bar' });
1672
+ const i = browser.storage.local.get.callCount;
1673
+ const res = await func();
1674
+ assert.strictEqual(browser.storage.local.get.callCount, i + 1, 'called');
1675
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1676
+ });
1677
+
1678
+ it('should call function', async () => {
1679
+ browser.storage.local.get.resolves({ foo: 'bar' });
1680
+ const i = browser.storage.local.get.callCount;
1681
+ const res = await func('local');
1682
+ assert.strictEqual(browser.storage.local.get.callCount, i + 1, 'called');
1683
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1684
+ });
1685
+
1686
+ it('should call function', async () => {
1687
+ browser.storage.managed.get.resolves({ foo: 'bar' });
1688
+ const i = browser.storage.managed.get.callCount;
1689
+ const res = await func('managed');
1690
+ assert.strictEqual(browser.storage.managed.get.callCount, i + 1,
1691
+ 'called');
1692
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1693
+ });
1694
+
1695
+ /*
1696
+ it('should call function', async () => {
1697
+ browser.storage.session.get.resolves({ foo: 'bar' });
1698
+ const i = browser.storage.session.get.callCount;
1699
+ const res = await func('session');
1700
+ assert.strictEqual(browser.storage.session.get.callCount, i + 1,
1701
+ 'called');
1702
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1703
+ });
1704
+ */
1705
+
1706
+ it('should call function', async () => {
1707
+ browser.storage.sync.get.resolves({ foo: 'bar' });
1708
+ const i = browser.storage.sync.get.callCount;
1709
+ const res = await func('sync');
1710
+ assert.strictEqual(browser.storage.sync.get.callCount, i + 1, 'called');
1711
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1712
+ });
1713
+ });
1714
+
1715
+ describe('get storage', () => {
1716
+ const func = mjs.getStorage;
1717
+
1718
+ it('should not call function if permission is not granted', async () => {
1719
+ browser.permissions.contains.resolves(false);
1720
+ const i = browser.storage.local.get.callCount;
1721
+ const j = browser.storage.managed.get.callCount;
1722
+ // const k = browser.storage.session.get.callCount;
1723
+ const l = browser.storage.sync.get.callCount;
1724
+ const res = await func('foo');
1725
+ assert.strictEqual(browser.storage.local.get.callCount, i, 'not called');
1726
+ assert.strictEqual(browser.storage.managed.get.callCount, j,
1727
+ 'not called');
1728
+ /*
1729
+ assert.strictEqual(browser.storage.session.get.callCount, k,
1730
+ 'not called');
1731
+ */
1732
+ assert.strictEqual(browser.storage.sync.get.callCount, l, 'not called');
1733
+ assert.deepEqual(res, null, 'result');
1734
+ });
1735
+
1736
+ it('should not call function', async () => {
1737
+ const i = browser.storage.local.get.callCount;
1738
+ const j = browser.storage.managed.get.callCount;
1739
+ // const k = browser.storage.session.get.callCount;
1740
+ const l = browser.storage.sync.get.callCount;
1741
+ const res = await func('foo', 'bar');
1742
+ assert.strictEqual(browser.storage.local.get.callCount, i, 'not called');
1743
+ assert.strictEqual(browser.storage.managed.get.callCount, j,
1744
+ 'not called');
1745
+ /*
1746
+ assert.strictEqual(browser.storage.session.get.callCount, k,
1747
+ 'not called');
1748
+ */
1749
+ assert.strictEqual(browser.storage.sync.get.callCount, l, 'not called');
1750
+ assert.deepEqual(res, null, 'result');
1751
+ });
1752
+
1753
+ it('should call function', async () => {
1754
+ browser.storage.local.get.withArgs('foo').resolves({ foo: 'bar' });
1755
+ const i = browser.storage.local.get.callCount;
1756
+ const res = await func('foo');
1757
+ assert.strictEqual(browser.storage.local.get.callCount, i + 1, 'called');
1758
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1759
+ });
1760
+
1761
+ it('should call function', async () => {
1762
+ browser.storage.local.get.withArgs('foo').resolves({ foo: 'bar' });
1763
+ const i = browser.storage.local.get.callCount;
1764
+ const res = await func('foo', 'local');
1765
+ assert.strictEqual(browser.storage.local.get.callCount, i + 1, 'called');
1766
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1767
+ });
1768
+
1769
+ it('should call function', async () => {
1770
+ browser.storage.managed.get.withArgs('foo').resolves({ foo: 'bar' });
1771
+ const i = browser.storage.managed.get.callCount;
1772
+ const res = await func('foo', 'managed');
1773
+ assert.strictEqual(browser.storage.managed.get.callCount, i + 1,
1774
+ 'called');
1775
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1776
+ });
1777
+
1778
+ /*
1779
+ it('should call function', async () => {
1780
+ browser.storage.session.get.withArgs('foo').resolves({ foo: 'bar' });
1781
+ const i = browser.storage.session.get.callCount;
1782
+ const res = await func('foo', 'session');
1783
+ assert.strictEqual(browser.storage.session.get.callCount, i + 1,
1784
+ 'called');
1785
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1786
+ });
1787
+ */
1788
+
1789
+ it('should call function', async () => {
1790
+ browser.storage.sync.get.withArgs('foo').resolves({ foo: 'bar' });
1791
+ const i = browser.storage.sync.get.callCount;
1792
+ const res = await func('foo', 'sync');
1793
+ assert.strictEqual(browser.storage.sync.get.callCount, i + 1, 'called');
1794
+ assert.deepEqual(res, { foo: 'bar' }, 'result');
1795
+ });
1796
+ });
1797
+
1798
+ describe('remove storage', () => {
1799
+ const func = mjs.removeStorage;
1800
+
1801
+ it('should not call function if permission is not granted', async () => {
1802
+ browser.permissions.contains.resolves(false);
1803
+ const i = browser.storage.local.remove.callCount;
1804
+ const j = browser.storage.managed.remove.callCount;
1805
+ // const k = browser.storage.session.remove.callCount;
1806
+ const l = browser.storage.sync.remove.callCount;
1807
+ await func('foo');
1808
+ assert.strictEqual(browser.storage.local.remove.callCount, i,
1809
+ 'not called');
1810
+ assert.strictEqual(browser.storage.managed.remove.callCount, j,
1811
+ 'not called');
1812
+ /*
1813
+ assert.strictEqual(browser.storage.session.remove.callCount, k,
1814
+ 'not called');
1815
+ */
1816
+ assert.strictEqual(browser.storage.sync.remove.callCount, l,
1817
+ 'not called');
1818
+ });
1819
+
1820
+ it('should not call function', async () => {
1821
+ const i = browser.storage.local.remove.callCount;
1822
+ const j = browser.storage.managed.remove.callCount;
1823
+ // const k = browser.storage.session.remove.callCount;
1824
+ const l = browser.storage.sync.remove.callCount;
1825
+ await func('foo', 'bar');
1826
+ assert.strictEqual(browser.storage.local.remove.callCount, i,
1827
+ 'not called');
1828
+ assert.strictEqual(browser.storage.managed.remove.callCount, j,
1829
+ 'not called');
1830
+ /*
1831
+ assert.strictEqual(browser.storage.session.remove.callCount, k,
1832
+ 'not called');
1833
+ */
1834
+ assert.strictEqual(browser.storage.sync.remove.callCount, l,
1835
+ 'not called');
1836
+ });
1837
+
1838
+ it('should call function', async () => {
1839
+ const i = browser.storage.local.remove.callCount;
1840
+ await func('foo');
1841
+ assert.strictEqual(browser.storage.local.remove.callCount, i + 1,
1842
+ 'called');
1843
+ });
1844
+
1845
+ it('should call function', async () => {
1846
+ const i = browser.storage.local.remove.callCount;
1847
+ await func('foo', 'local');
1848
+ assert.strictEqual(browser.storage.local.remove.callCount, i + 1,
1849
+ 'called');
1850
+ });
1851
+
1852
+ it('should not call function', async () => {
1853
+ const i = browser.storage.managed.remove.callCount;
1854
+ await func('foo', 'managed');
1855
+ assert.strictEqual(browser.storage.managed.remove.callCount, i,
1856
+ 'not called');
1857
+ });
1858
+
1859
+ /*
1860
+ it('should call function', async () => {
1861
+ const i = browser.storage.session.remove.callCount;
1862
+ await func('foo', 'session');
1863
+ assert.strictEqual(browser.storage.session.remove.callCount, i + 1,
1864
+ 'called');
1865
+ });
1866
+ */
1867
+
1868
+ it('should call function', async () => {
1869
+ const i = browser.storage.sync.remove.callCount;
1870
+ await func('foo', 'sync');
1871
+ assert.strictEqual(browser.storage.sync.remove.callCount, i + 1,
1872
+ 'called');
1873
+ });
1874
+ });
1875
+
1876
+ describe('set storage', () => {
1877
+ const func = mjs.setStorage;
1878
+
1879
+ it('should not call function if permission is not granted', async () => {
1880
+ browser.permissions.contains.resolves(false);
1881
+ const i = browser.storage.local.set.callCount;
1882
+ const j = browser.storage.managed.set.callCount;
1883
+ // const k = browser.storage.session.set.callCount;
1884
+ const l = browser.storage.sync.set.callCount;
1885
+ await func('foo');
1886
+ assert.strictEqual(browser.storage.local.set.callCount, i,
1887
+ 'not called');
1888
+ assert.strictEqual(browser.storage.managed.set.callCount, j,
1889
+ 'not called');
1890
+ /*
1891
+ assert.strictEqual(browser.storage.session.set.callCount, k,
1892
+ 'not called');
1893
+ */
1894
+ assert.strictEqual(browser.storage.sync.set.callCount, l, 'not called');
1895
+ });
1896
+
1897
+ it('should not call function if first argument is falsy', async () => {
1898
+ const i = browser.storage.local.set.callCount;
1899
+ await func();
1900
+ assert.strictEqual(browser.storage.local.set.callCount, i, 'not called');
1901
+ });
1902
+
1903
+ it('should call function', async () => {
1904
+ const i = browser.storage.local.set.callCount;
1905
+ await func('foo');
1906
+ assert.strictEqual(browser.storage.local.set.callCount, i + 1, 'called');
1907
+ });
1908
+
1909
+ it('should call function', async () => {
1910
+ const i = browser.storage.local.set.callCount;
1911
+ await func('foo', 'local');
1912
+ assert.strictEqual(browser.storage.local.set.callCount, i + 1, 'called');
1913
+ });
1914
+
1915
+ it('should not call function', async () => {
1916
+ const i = browser.storage.managed.set.callCount;
1917
+ await func('foo', 'managed');
1918
+ assert.strictEqual(browser.storage.managed.set.callCount, i,
1919
+ 'not called');
1920
+ });
1921
+
1922
+ /*
1923
+ it('should call function', async () => {
1924
+ const i = browser.storage.session.set.callCount;
1925
+ await func('foo', 'session');
1926
+ assert.strictEqual(browser.storage.session.set.callCount, i + 1,
1927
+ 'called');
1928
+ });
1929
+ */
1930
+
1931
+ it('should call function', async () => {
1932
+ const i = browser.storage.sync.set.callCount;
1933
+ await func('foo', 'sync');
1934
+ assert.strictEqual(browser.storage.sync.set.callCount, i + 1, 'called');
1935
+ });
1936
+ });
1937
+
1938
+ describe('create tab', () => {
1939
+ const func = mjs.createTab;
1940
+
1941
+ it('should get object', async () => {
1942
+ browser.tabs.create.withArgs(null).resolves({});
1943
+ const res = await func();
1944
+ assert.deepEqual(res, {}, 'result');
1945
+ });
1946
+
1947
+ it('should get object', async () => {
1948
+ browser.tabs.create.withArgs(null).resolves({});
1949
+ const res = await func({});
1950
+ assert.deepEqual(res, {}, 'result');
1951
+ });
1952
+
1953
+ it('should get object', async () => {
1954
+ const opt = {
1955
+ foo: 'bar'
1956
+ };
1957
+ browser.tabs.create.withArgs(opt).resolves({});
1958
+ const res = await func(opt);
1959
+ assert.deepEqual(res, {}, 'result');
1960
+ });
1961
+ });
1962
+
1963
+ describe('duplicate tab', () => {
1964
+ const func = mjs.duplicateTab;
1965
+
1966
+ it('should throw', async () => {
1967
+ await func().catch(e => {
1968
+ assert.deepStrictEqual(e,
1969
+ new TypeError('Expected Number but got Undefined.'));
1970
+ });
1971
+ });
1972
+
1973
+ it('should get object', async () => {
1974
+ browser.tabs.duplicate.withArgs(1, null).resolves({});
1975
+ const res = await func(1);
1976
+ assert.deepEqual(res, {}, 'result');
1977
+ });
1978
+
1979
+ it('should get object', async () => {
1980
+ const opt = {
1981
+ active: true
1982
+ };
1983
+ browser.tabs.duplicate.withArgs(1, opt).resolves({});
1984
+ const res = await func(1, opt);
1985
+ assert.deepEqual(res, {}, 'result');
1986
+ });
1987
+ });
1988
+
1989
+ describe('query tabs', async () => {
1990
+ const func = mjs.queryTabs;
1991
+
1992
+ it('should call function', async () => {
1993
+ const i = browser.tabs.query.withArgs({}).callCount;
1994
+ browser.tabs.query.resolves([{}]);
1995
+ browser.tabs.query.withArgs({}).resolves([{}, {}]);
1996
+ const res = await func({});
1997
+ assert.strictEqual(browser.tabs.query.withArgs({}).callCount, i + 1,
1998
+ 'called');
1999
+ assert.deepEqual(res, [{}, {}], 'result');
2000
+ });
2001
+ });
2002
+
2003
+ describe('execute content script to existing tab', () => {
2004
+ const func = mjs.execScriptToTab;
2005
+
2006
+ it('should get null if no argument given', async () => {
2007
+ const res = await func();
2008
+ assert.deepEqual(res, null, 'result');
2009
+ });
2010
+
2011
+ it('should call function', async () => {
2012
+ const stubErr = sinon.stub(console, 'error');
2013
+ const file = '/foo/bar';
2014
+ const i = browser.tabs.executeScript.withArgs(1, {
2015
+ file
2016
+ }).callCount;
2017
+ browser.tabs.executeScript.withArgs(1, {
2018
+ file
2019
+ }).resolves([{}]);
2020
+ const res = await func(1, { file });
2021
+ const { calledOnce: errCalled } = stubErr;
2022
+ stubErr.restore();
2023
+ assert.strictEqual(browser.tabs.executeScript.withArgs(1, {
2024
+ file
2025
+ }).callCount, i + 1, 'called');
2026
+ assert.strictEqual(errCalled, false, 'error not called');
2027
+ assert.deepEqual(res, [{}], 'result');
2028
+ });
2029
+
2030
+ it('should log error', async () => {
2031
+ const stubErr = sinon.stub(console, 'error');
2032
+ const file = '/foo/bar';
2033
+ const i = browser.tabs.executeScript.withArgs(1, {
2034
+ file
2035
+ }).callCount;
2036
+ browser.tabs.executeScript.withArgs(1, {
2037
+ file
2038
+ }).rejects(new Error('error'));
2039
+ const res = await func(1, { file });
2040
+ const { calledOnce: errCalled } = stubErr;
2041
+ stubErr.restore();
2042
+ assert.strictEqual(browser.tabs.executeScript.withArgs(1, {
2043
+ file
2044
+ }).callCount, i + 1, 'called');
2045
+ assert.strictEqual(errCalled, true, 'error called');
2046
+ assert.strictEqual(res, false, 'result');
2047
+ });
2048
+
2049
+ it('should get result', async () => {
2050
+ const stubErr = sinon.stub(console, 'error');
2051
+ const file = '/foo/bar';
2052
+ const i = browser.tabs.executeScript.withArgs({
2053
+ file
2054
+ }).callCount;
2055
+ browser.tabs.executeScript.withArgs({
2056
+ file
2057
+ }).resolves([{}]);
2058
+ const res = await func({ file });
2059
+ const { calledOnce: errCalled } = stubErr;
2060
+ stubErr.restore();
2061
+ assert.strictEqual(browser.tabs.executeScript.withArgs({
2062
+ file
2063
+ }).callCount, i + 1, 'called');
2064
+ assert.strictEqual(errCalled, false, 'error not called');
2065
+ assert.deepEqual(res, [{}], 'result');
2066
+ });
2067
+
2068
+ it('should log error', async () => {
2069
+ const stubErr = sinon.stub(console, 'error');
2070
+ const file = '/foo/bar';
2071
+ const i = browser.tabs.executeScript.withArgs({
2072
+ file
2073
+ }).callCount;
2074
+ browser.tabs.executeScript.withArgs({
2075
+ file
2076
+ }).rejects(new Error('error'));
2077
+ const res = await func({ file });
2078
+ const { calledOnce: errCalled } = stubErr;
2079
+ stubErr.restore();
2080
+ assert.strictEqual(browser.tabs.executeScript.withArgs({
2081
+ file
2082
+ }).callCount, i + 1, 'called');
2083
+ assert.strictEqual(errCalled, true, 'error called');
2084
+ assert.strictEqual(res, false, 'result');
2085
+ });
2086
+
2087
+ it('should get result', async () => {
2088
+ const stubErr = sinon.stub(console, 'error');
2089
+ const file = '/foo/bar';
2090
+ const i = browser.tabs.executeScript.withArgs({
2091
+ file
2092
+ }).callCount;
2093
+ browser.tabs.executeScript.withArgs({
2094
+ file
2095
+ }).resolves([{}]);
2096
+ const res = await func(null, { file });
2097
+ const { calledOnce: errCalled } = stubErr;
2098
+ stubErr.restore();
2099
+ assert.strictEqual(browser.tabs.executeScript.withArgs({
2100
+ file
2101
+ }).callCount, i + 1, 'called');
2102
+ assert.strictEqual(errCalled, false, 'error not called');
2103
+ assert.deepEqual(res, [{}], 'result');
2104
+ });
2105
+
2106
+ it('should log error', async () => {
2107
+ const stubErr = sinon.stub(console, 'error');
2108
+ const file = '/foo/bar';
2109
+ const i = browser.tabs.executeScript.withArgs({
2110
+ file
2111
+ }).callCount;
2112
+ browser.tabs.executeScript.withArgs({
2113
+ file
2114
+ }).rejects(new Error('error'));
2115
+ const res = await func(null, { file });
2116
+ const { calledOnce: errCalled } = stubErr;
2117
+ stubErr.restore();
2118
+ assert.strictEqual(browser.tabs.executeScript.withArgs({
2119
+ file
2120
+ }).callCount, i + 1, 'called');
2121
+ assert.strictEqual(errCalled, true, 'error called');
2122
+ assert.strictEqual(res, false, 'result');
2123
+ });
2124
+ });
2125
+
2126
+ describe('execute content script to existing tabs', () => {
2127
+ const func = mjs.execScriptToTabs;
2128
+
2129
+ it('should call function', async () => {
2130
+ const stubErr = sinon.stub(console, 'error');
2131
+ const i = browser.tabs.executeScript.withArgs(1, {}).callCount;
2132
+ const j = browser.tabs.executeScript.withArgs(2, {}).callCount;
2133
+ browser.tabs.query.resolves([
2134
+ {
2135
+ id: 1,
2136
+ url: 'https://example.com'
2137
+ },
2138
+ {
2139
+ id: 2,
2140
+ url: 'https://example.net'
2141
+ },
2142
+ {
2143
+ id: 3,
2144
+ url: 'about:blank'
2145
+ }
2146
+ ]);
2147
+ browser.tabs.executeScript.withArgs(1, {}).resolves([{}]);
2148
+ browser.tabs.executeScript.withArgs(2, {}).rejects(new Error('error'));
2149
+ const res = await func();
2150
+ const { calledOnce: errCalled } = stubErr;
2151
+ stubErr.restore();
2152
+ assert.strictEqual(browser.tabs.executeScript.withArgs(1, {}).callCount,
2153
+ i + 1, 'called');
2154
+ assert.strictEqual(browser.tabs.executeScript.withArgs(2, {}).callCount,
2155
+ j + 1, 'called');
2156
+ assert.strictEqual(errCalled, true, 'error called');
2157
+ assert.deepEqual(res, [[{}], false], 'result');
2158
+ });
2159
+
2160
+ it('should call function', async () => {
2161
+ const stubErr = sinon.stub(console, 'error');
2162
+ const file = '/foo/bar';
2163
+ const i = browser.tabs.executeScript.withArgs(1, {
2164
+ file,
2165
+ allFrames: true
2166
+ }).callCount;
2167
+ const j = browser.tabs.executeScript.withArgs(2, {
2168
+ file,
2169
+ allFrames: true
2170
+ }).callCount;
2171
+ browser.tabs.query.resolves([
2172
+ {
2173
+ id: 1,
2174
+ url: 'https://example.com'
2175
+ },
2176
+ {
2177
+ id: 2,
2178
+ url: 'https://example.net'
2179
+ },
2180
+ {
2181
+ id: 3,
2182
+ url: 'about:blank'
2183
+ }
2184
+ ]);
2185
+ browser.tabs.executeScript.withArgs(1, {
2186
+ file,
2187
+ allFrames: true
2188
+ }).resolves([{}, {}]);
2189
+ browser.tabs.executeScript.withArgs(2, {
2190
+ file,
2191
+ allFrames: true
2192
+ }).rejects(new Error('error'));
2193
+ const res = await func({
2194
+ file,
2195
+ allFrames: true
2196
+ });
2197
+ const { calledOnce: errCalled } = stubErr;
2198
+ stubErr.restore();
2199
+ assert.strictEqual(browser.tabs.executeScript.withArgs(1, {
2200
+ file,
2201
+ allFrames: true
2202
+ }).callCount, i + 1, 'called');
2203
+ assert.strictEqual(browser.tabs.executeScript.withArgs(2, {
2204
+ file,
2205
+ allFrames: true
2206
+ }).callCount, j + 1, 'called');
2207
+ assert.strictEqual(errCalled, true, 'error called');
2208
+ assert.deepEqual(res, [[{}, {}], false], 'result');
2209
+ });
2210
+ });
2211
+
2212
+ describe('execute scripts to tab in order', () => {
2213
+ const func = mjs.execScriptsToTabInOrder;
2214
+
2215
+ it('should get null if no arguments given', async () => {
2216
+ const res = await func();
2217
+ assert.deepEqual(res, null, 'result');
2218
+ });
2219
+
2220
+ it('should get null if no opts given', async () => {
2221
+ const i = browser.tabs.executeScript.callCount;
2222
+ const res = await func(1);
2223
+ assert.strictEqual(browser.tabs.executeScript.callCount, i, 'not called');
2224
+ assert.deepEqual(res, null, 'result');
2225
+ });
2226
+
2227
+ it('should get null if opts is empty array', async () => {
2228
+ const i = browser.tabs.executeScript.callCount;
2229
+ const res = await func(1, []);
2230
+ assert.strictEqual(browser.tabs.executeScript.callCount, i, 'not called');
2231
+ assert.deepEqual(res, null, 'result');
2232
+ });
2233
+
2234
+ it('should get null if opts is empty array', async () => {
2235
+ const i = browser.tabs.executeScript.callCount;
2236
+ const res = await func([]);
2237
+ assert.strictEqual(browser.tabs.executeScript.callCount, i, 'not called');
2238
+ assert.deepEqual(res, null, 'result');
2239
+ });
2240
+
2241
+ it('should get null if opts is empty array', async () => {
2242
+ const i = browser.tabs.executeScript.callCount;
2243
+ const res = await func(null, []);
2244
+ assert.strictEqual(browser.tabs.executeScript.callCount, i, 'not called');
2245
+ assert.deepEqual(res, null, 'result');
2246
+ });
2247
+
2248
+ it('should get result', async () => {
2249
+ const stubErr = sinon.stub(console, 'error');
2250
+ const opt = {
2251
+ file: '/foo/bar'
2252
+ };
2253
+ const opt2 = {
2254
+ file: '/baz/qux'
2255
+ };
2256
+ const i = browser.tabs.executeScript.withArgs(1, opt).callCount;
2257
+ const j = browser.tabs.executeScript.withArgs(1, opt2).callCount;
2258
+ browser.tabs.executeScript.withArgs(1, opt).rejects(new Error('error'));
2259
+ browser.tabs.executeScript.withArgs(1, opt2).resolves([{}]);
2260
+ const opts = [opt, opt2];
2261
+ const res = await func(1, opts);
2262
+ const { calledOnce: errCalled } = stubErr;
2263
+ stubErr.restore();
2264
+ assert.strictEqual(browser.tabs.executeScript.withArgs(1, opt).callCount,
2265
+ i + 1, 'called');
2266
+ assert.strictEqual(browser.tabs.executeScript.withArgs(1, opt2).callCount,
2267
+ j + 1, 'called');
2268
+ assert.strictEqual(errCalled, true, 'error called');
2269
+ assert.deepEqual(res, [{}], 'result');
2270
+ });
2271
+
2272
+ it('should get result', async () => {
2273
+ const stubErr = sinon.stub(console, 'error');
2274
+ const opt = {
2275
+ file: '/foo/bar'
2276
+ };
2277
+ const opt2 = {
2278
+ file: '/baz/qux'
2279
+ };
2280
+ const i = browser.tabs.executeScript.withArgs(opt).callCount;
2281
+ const j = browser.tabs.executeScript.withArgs(opt2).callCount;
2282
+ browser.tabs.executeScript.withArgs(opt).rejects(new Error('error'));
2283
+ browser.tabs.executeScript.withArgs(opt2).resolves([{}]);
2284
+ const opts = [opt, opt2];
2285
+ const res = await func(opts);
2286
+ const { calledOnce: errCalled } = stubErr;
2287
+ stubErr.restore();
2288
+ assert.strictEqual(browser.tabs.executeScript.withArgs(opt).callCount,
2289
+ i + 1, 'called');
2290
+ assert.strictEqual(browser.tabs.executeScript.withArgs(opt2).callCount,
2291
+ j + 1, 'called');
2292
+ assert.strictEqual(errCalled, true, 'error called');
2293
+ assert.deepEqual(res, [{}], 'result');
2294
+ });
2295
+
2296
+ it('should get result', async () => {
2297
+ const stubErr = sinon.stub(console, 'error');
2298
+ const opt = {
2299
+ file: '/foo/bar'
2300
+ };
2301
+ const opt2 = {
2302
+ file: '/baz/qux'
2303
+ };
2304
+ const i = browser.tabs.executeScript.withArgs(opt).callCount;
2305
+ const j = browser.tabs.executeScript.withArgs(opt2).callCount;
2306
+ browser.tabs.executeScript.withArgs(opt).rejects(new Error('error'));
2307
+ browser.tabs.executeScript.withArgs(opt2).resolves([{}]);
2308
+ const opts = [opt, opt2];
2309
+ const res = await func(null, opts);
2310
+ const { calledOnce: errCalled } = stubErr;
2311
+ stubErr.restore();
2312
+ assert.strictEqual(browser.tabs.executeScript.withArgs(opt).callCount,
2313
+ i + 1, 'called');
2314
+ assert.strictEqual(browser.tabs.executeScript.withArgs(opt2).callCount,
2315
+ j + 1, 'called');
2316
+ assert.strictEqual(errCalled, true, 'error called');
2317
+ assert.deepEqual(res, [{}], 'result');
2318
+ });
2319
+ });
2320
+
2321
+ describe('get active tab', () => {
2322
+ const func = mjs.getActiveTab;
2323
+
2324
+ it('should get number', async () => {
2325
+ browser.tabs.query.resolves([{}]);
2326
+ const res = await func(1);
2327
+ assert.deepEqual(res, {}, 'result');
2328
+ });
2329
+
2330
+ it('should get number', async () => {
2331
+ browser.tabs.query.resolves([{}]);
2332
+ const res = await func();
2333
+ assert.deepEqual(res, {}, 'result');
2334
+ });
2335
+ });
2336
+
2337
+ describe('get active tab ID', () => {
2338
+ const func = mjs.getActiveTabId;
2339
+
2340
+ it('should get null', async () => {
2341
+ browser.tabs.query.resolves([]);
2342
+ const res = await func(1);
2343
+ assert.deepEqual(res, null, 'result');
2344
+ });
2345
+
2346
+ it('should get number', async () => {
2347
+ browser.tabs.query.resolves([
2348
+ {
2349
+ id: 1
2350
+ }
2351
+ ]);
2352
+ const res = await func(1);
2353
+ assert.strictEqual(res, 1, 'result');
2354
+ });
2355
+
2356
+ it('should get number', async () => {
2357
+ browser.tabs.query.resolves([
2358
+ {
2359
+ id: 1
2360
+ }
2361
+ ]);
2362
+ const res = await func();
2363
+ assert.strictEqual(res, 1, 'result');
2364
+ });
2365
+
2366
+ it('should get number', async () => {
2367
+ browser.tabs.query.resolves([
2368
+ {
2369
+ id: browser.tabs.TAB_ID_NONE
2370
+ }
2371
+ ]);
2372
+ const res = await func();
2373
+ assert.strictEqual(res, browser.tabs.TAB_ID_NONE, 'result');
2374
+ });
2375
+ });
2376
+
2377
+ describe('get all tabs in window', () => {
2378
+ const func = mjs.getAllTabsInWindow;
2379
+
2380
+ it('should get array', async () => {
2381
+ browser.tabs.query.resolves([{}]);
2382
+ const res = await func(1);
2383
+ assert.deepEqual(res, [{}], 'result');
2384
+ });
2385
+
2386
+ it('should get array', async () => {
2387
+ browser.tabs.query.resolves([{}]);
2388
+ const res = await func();
2389
+ assert.deepEqual(res, [{}], 'result');
2390
+ });
2391
+ });
2392
+
2393
+ describe('get highlighted tab', () => {
2394
+ const func = mjs.getHighlightedTab;
2395
+
2396
+ it('should get array', async () => {
2397
+ browser.tabs.query.resolves([{}]);
2398
+ const res = await func(1);
2399
+ assert.deepEqual(res, [{}], 'result');
2400
+ });
2401
+
2402
+ it('should get array', async () => {
2403
+ browser.tabs.query.resolves([{}]);
2404
+ const res = await func();
2405
+ assert.deepEqual(res, [{}], 'result');
2406
+ });
2407
+ });
2408
+
2409
+ describe('get tab', () => {
2410
+ const func = mjs.getTab;
2411
+
2412
+ it('should throw if no argument given', async () => {
2413
+ await func().catch(e => {
2414
+ assert.deepStrictEqual(e,
2415
+ new TypeError('Expected Number but got Undefined.'));
2416
+ });
2417
+ });
2418
+
2419
+ it('should throw if argument is not number', async () => {
2420
+ await func('').catch(e => {
2421
+ assert.deepStrictEqual(e,
2422
+ new TypeError('Expected Number but got String.'));
2423
+ });
2424
+ });
2425
+
2426
+ it('should get object', async () => {
2427
+ browser.tabs.get.withArgs(1).resolves({});
2428
+ const res = await func(1);
2429
+ assert.deepEqual(res, {}, 'result');
2430
+ });
2431
+ });
2432
+
2433
+ describe('highlight tab', () => {
2434
+ const func = mjs.highlightTab;
2435
+
2436
+ it('should throw if no argument given', async () => {
2437
+ await func().catch(e => {
2438
+ assert.deepStrictEqual(e,
2439
+ new TypeError('Expected Number or Array but got Undefined.'));
2440
+ });
2441
+ });
2442
+
2443
+ it('should throw if argument is not number', async () => {
2444
+ await func('').catch(e => {
2445
+ assert.deepStrictEqual(e,
2446
+ new TypeError('Expected Number or Array but got String.'));
2447
+ });
2448
+ });
2449
+
2450
+ it('should get object', async () => {
2451
+ browser.tabs.highlight.resolves({});
2452
+ const res = await func(1);
2453
+ assert.deepEqual(res, {}, 'result');
2454
+ });
2455
+
2456
+ it('should get object', async () => {
2457
+ browser.tabs.highlight.resolves({});
2458
+ const res = await func(1, 2);
2459
+ assert.deepEqual(res, {}, 'result');
2460
+ });
2461
+ });
2462
+
2463
+ describe('move tab', () => {
2464
+ const func = mjs.moveTab;
2465
+
2466
+ it('should throw if no argument given', async () => {
2467
+ await func().catch(e => {
2468
+ assert.deepStrictEqual(e,
2469
+ new TypeError('Expected Number or Array but got Undefined.'));
2470
+ });
2471
+ });
2472
+
2473
+ it('should throw if argument is not number', async () => {
2474
+ await func('').catch(e => {
2475
+ assert.deepStrictEqual(e,
2476
+ new TypeError('Expected Number or Array but got String.'));
2477
+ });
2478
+ });
2479
+
2480
+ it('should get null', async () => {
2481
+ const res = await func(1);
2482
+ assert.deepEqual(res, null, 'res');
2483
+ });
2484
+
2485
+ it('should get array', async () => {
2486
+ browser.tabs.move.withArgs(1).resolves({});
2487
+ const res = await func(1);
2488
+ assert.deepEqual(res, [{}], 'res');
2489
+ });
2490
+
2491
+ it('should get array', async () => {
2492
+ browser.tabs.move.withArgs(1, { foo: 'bar' }).resolves({});
2493
+ const res = await func(1, { foo: 'bar' });
2494
+ assert.deepEqual(res, [{}], 'res');
2495
+ });
2496
+
2497
+ it('should get array', async () => {
2498
+ browser.tabs.move.withArgs([1, 2]).resolves([{}, {}]);
2499
+ const res = await func([1, 2]);
2500
+ assert.deepEqual(res, [{}, {}], 'res');
2501
+ });
2502
+
2503
+ it('should get array', async () => {
2504
+ browser.tabs.move.withArgs([1, 2]).resolves([]);
2505
+ const res = await func([1, 2]);
2506
+ assert.deepEqual(res, [], 'res');
2507
+ });
2508
+ });
2509
+
2510
+ describe('reload tab', () => {
2511
+ const func = mjs.reloadTab;
2512
+
2513
+ it('should throw if no argument given', async () => {
2514
+ await func().catch(e => {
2515
+ assert.deepStrictEqual(e,
2516
+ new TypeError('Expected Number but got Undefined.'));
2517
+ });
2518
+ });
2519
+
2520
+ it('should throw if argument is not number', async () => {
2521
+ await func('').catch(e => {
2522
+ assert.deepStrictEqual(e,
2523
+ new TypeError('Expected Number but got String.'));
2524
+ });
2525
+ });
2526
+
2527
+ it('should call function', async () => {
2528
+ const i = browser.tabs.reload.withArgs(1, null).callCount;
2529
+ await func(1);
2530
+ assert.strictEqual(browser.tabs.reload.withArgs(1, null).callCount, i + 1,
2531
+ 'res');
2532
+ });
2533
+
2534
+ it('should call function', async () => {
2535
+ const i = browser.tabs.reload.withArgs(1, { foo: 'bar' }).callCount;
2536
+ await func(1, { foo: 'bar' });
2537
+ assert.strictEqual(
2538
+ browser.tabs.reload.withArgs(1, { foo: 'bar' }).callCount,
2539
+ i + 1, 'res'
2540
+ );
2541
+ });
2542
+ });
2543
+
2544
+ describe('remove tab', () => {
2545
+ const func = mjs.removeTab;
2546
+
2547
+ it('should throw if no argument given', async () => {
2548
+ await func().catch(e => {
2549
+ assert.deepStrictEqual(e,
2550
+ new TypeError('Expected Array but got Undefined.'));
2551
+ });
2552
+ });
2553
+
2554
+ it('should throw if argument is not number or array', async () => {
2555
+ await func('').catch(e => {
2556
+ assert.deepStrictEqual(e,
2557
+ new TypeError('Expected Array but got String.'));
2558
+ });
2559
+ });
2560
+
2561
+ it('should call function', async () => {
2562
+ const i = browser.tabs.remove.callCount;
2563
+ await func(1);
2564
+ assert.strictEqual(browser.tabs.remove.callCount, i + 1, 'res');
2565
+ });
2566
+
2567
+ it('should call function', async () => {
2568
+ const i = browser.tabs.remove.callCount;
2569
+ await func([1]);
2570
+ assert.strictEqual(browser.tabs.remove.callCount, i + 1, 'res');
2571
+ });
2572
+ });
2573
+
2574
+ describe('update tab', () => {
2575
+ const func = mjs.updateTab;
2576
+
2577
+ it('should throw if no argument given', async () => {
2578
+ await func().catch(e => {
2579
+ assert.deepStrictEqual(e,
2580
+ new TypeError('Expected Number but got Undefined.'));
2581
+ });
2582
+ });
2583
+
2584
+ it('should throw if argument is not number', async () => {
2585
+ await func('').catch(e => {
2586
+ assert.deepStrictEqual(e,
2587
+ new TypeError('Expected Number but got String.'));
2588
+ });
2589
+ });
2590
+
2591
+ it('should throw if tab does not exist', async () => {
2592
+ browser.tabs.update.withArgs(1).throws(new Error('error'));
2593
+ await func(1).catch(e => {
2594
+ assert.deepStrictEqual(e, new Error('error'));
2595
+ });
2596
+ });
2597
+
2598
+ it('should get object', async () => {
2599
+ browser.tabs.update.withArgs(1).resolves({});
2600
+ const i = browser.tabs.update.withArgs(1).callCount;
2601
+ const res = await func(1);
2602
+ assert.strictEqual(browser.tabs.update.withArgs(1).callCount, i + 1,
2603
+ 'called');
2604
+ assert.strictEqual(typeof res, 'object', 'res');
2605
+ });
2606
+
2607
+ it('should get object', async () => {
2608
+ browser.tabs.update.withArgs(1, { foo: 'bar' }).resolves({});
2609
+ const i = browser.tabs.update.withArgs(1, { foo: 'bar' }).callCount;
2610
+ const res = await func(1, { foo: 'bar' });
2611
+ assert.strictEqual(
2612
+ browser.tabs.update.withArgs(1, { foo: 'bar' }).callCount,
2613
+ i + 1,
2614
+ 'called'
2615
+ );
2616
+ assert.strictEqual(typeof res, 'object', 'res');
2617
+ });
2618
+ });
2619
+
2620
+ describe('warmup tab', () => {
2621
+ const func = mjs.warmupTab;
2622
+
2623
+ it('should throw if no argument given', async () => {
2624
+ await func().catch(e => {
2625
+ assert.deepStrictEqual(e,
2626
+ new TypeError('Expected Number but got Undefined.'));
2627
+ });
2628
+ });
2629
+
2630
+ it('should throw if argument is not number', async () => {
2631
+ await func('').catch(e => {
2632
+ assert.deepStrictEqual(e,
2633
+ new TypeError('Expected Number but got String.'));
2634
+ });
2635
+ });
2636
+
2637
+ it('should call function', async () => {
2638
+ if (typeof browser.tabs.warmup === 'function') {
2639
+ const i = browser.tabs.warmup.callCount;
2640
+ await func(1);
2641
+ assert.strictEqual(browser.tabs.warmup.callCount, i + 1, 'called');
2642
+ }
2643
+ });
2644
+ });
2645
+
2646
+ describe('capture visible tab', () => {
2647
+ const func = mjs.captureVisibleTab;
2648
+
2649
+ it('should not call function if permission is not granted', async () => {
2650
+ browser.runtime.getBrowserInfo.resolves({
2651
+ vender: 'Mozilla',
2652
+ version: '125.0.1'
2653
+ });
2654
+ browser.permissions.contains.withArgs({
2655
+ origins: ['<all_urls>']
2656
+ }).resolves(false);
2657
+ const i = browser.tabs.captureVisibleTab.callCount;
2658
+ const res = await func();
2659
+ assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i,
2660
+ 'not called');
2661
+ assert.deepEqual(res, null, 'result');
2662
+ });
2663
+
2664
+ it('should call function', async () => {
2665
+ browser.runtime.getBrowserInfo.resolves({
2666
+ vender: 'Mozilla',
2667
+ version: '125.0.1'
2668
+ });
2669
+ browser.permissions.contains.withArgs({
2670
+ origins: ['<all_urls>']
2671
+ }).resolves(true);
2672
+ const i = browser.tabs.captureVisibleTab.callCount;
2673
+ const windowId = browser.windows.WINDOW_ID_CURRENT;
2674
+ const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
2675
+ browser.tabs.captureVisibleTab.withArgs(windowId, {
2676
+ format: 'png'
2677
+ }).resolves(url);
2678
+ const res = await func();
2679
+ assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i + 1,
2680
+ 'called');
2681
+ assert.strictEqual(res, url, 'result');
2682
+ });
2683
+
2684
+ it('should not call function if permission is not granted', async () => {
2685
+ browser.runtime.getBrowserInfo.resolves({
2686
+ vender: 'Mozilla',
2687
+ version: '126.0.a1'
2688
+ });
2689
+ browser.permissions.contains.withArgs({
2690
+ permissions: ['activeTab']
2691
+ }).resolves(false);
2692
+ browser.permissions.contains.withArgs({
2693
+ origins: ['<all_urls>']
2694
+ }).resolves(false);
2695
+ const i = browser.tabs.captureVisibleTab.callCount;
2696
+ const res = await func();
2697
+ assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i,
2698
+ 'not called');
2699
+ assert.deepEqual(res, null, 'result');
2700
+ });
2701
+
2702
+ it('should call function', async () => {
2703
+ browser.runtime.getBrowserInfo.resolves({
2704
+ vender: 'Mozilla',
2705
+ version: '126.0a1'
2706
+ });
2707
+ browser.permissions.contains.withArgs({
2708
+ permissions: ['activeTab']
2709
+ }).resolves(false);
2710
+ browser.permissions.contains.withArgs({
2711
+ origins: ['<all_urls>']
2712
+ }).resolves(true);
2713
+ const i = browser.tabs.captureVisibleTab.callCount;
2714
+ const windowId = browser.windows.WINDOW_ID_CURRENT;
2715
+ const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
2716
+ browser.tabs.captureVisibleTab.withArgs(windowId, {
2717
+ format: 'png'
2718
+ }).resolves(url);
2719
+ const res = await func();
2720
+ assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i + 1,
2721
+ 'called');
2722
+ assert.strictEqual(res, url, 'result');
2723
+ });
2724
+
2725
+ it('should call function', async () => {
2726
+ browser.runtime.getBrowserInfo.resolves({
2727
+ vender: 'Mozilla',
2728
+ version: '126.0a1'
2729
+ });
2730
+ browser.permissions.contains.withArgs({
2731
+ permissions: ['activeTab']
2732
+ }).resolves(true);
2733
+ browser.permissions.contains.withArgs({
2734
+ origins: ['<all_urls>']
2735
+ }).resolves(false);
2736
+ const i = browser.tabs.captureVisibleTab.callCount;
2737
+ const windowId = browser.windows.WINDOW_ID_CURRENT;
2738
+ const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
2739
+ browser.tabs.captureVisibleTab.withArgs(windowId, {
2740
+ format: 'png'
2741
+ }).resolves(url);
2742
+ const res = await func();
2743
+ assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i + 1,
2744
+ 'called');
2745
+ assert.strictEqual(res, url, 'result');
2746
+ });
2747
+
2748
+ it('should not call function if permission is not granted', async () => {
2749
+ browser.runtime.getBrowserInfo.resolves({
2750
+ vender: 'Foo',
2751
+ version: '126.0.1'
2752
+ });
2753
+ browser.permissions.contains.withArgs({
2754
+ permissions: ['activeTab']
2755
+ }).resolves(false);
2756
+ browser.permissions.contains.withArgs({
2757
+ origins: ['<all_urls>']
2758
+ }).resolves(false);
2759
+ const i = browser.tabs.captureVisibleTab.callCount;
2760
+ const res = await func();
2761
+ assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i,
2762
+ 'not called');
2763
+ assert.deepEqual(res, null, 'result');
2764
+ });
2765
+
2766
+ it('should call function', async () => {
2767
+ browser.runtime.getBrowserInfo.resolves({
2768
+ vender: 'Foo',
2769
+ version: '126.0.1'
2770
+ });
2771
+ browser.permissions.contains.withArgs({
2772
+ permissions: ['activeTab']
2773
+ }).resolves(true);
2774
+ browser.permissions.contains.withArgs({
2775
+ origins: ['<all_urls>']
2776
+ }).resolves(false);
2777
+ const i = browser.tabs.captureVisibleTab.callCount;
2778
+ const windowId = browser.windows.WINDOW_ID_CURRENT;
2779
+ const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
2780
+ browser.tabs.captureVisibleTab.withArgs(windowId, {
2781
+ format: 'png'
2782
+ }).resolves(url);
2783
+ const res = await func();
2784
+ assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i + 1,
2785
+ 'called');
2786
+ assert.strictEqual(res, url, 'result');
2787
+ });
2788
+
2789
+ it('should call function', async () => {
2790
+ browser.runtime.getBrowserInfo.resolves({
2791
+ vender: 'Foo',
2792
+ version: '126.0.1'
2793
+ });
2794
+ browser.permissions.contains.withArgs({
2795
+ permissions: ['activeTab']
2796
+ }).resolves(false);
2797
+ browser.permissions.contains.withArgs({
2798
+ origins: ['<all_urls>']
2799
+ }).resolves(true);
2800
+ const i = browser.tabs.captureVisibleTab.callCount;
2801
+ const windowId = browser.windows.WINDOW_ID_CURRENT;
2802
+ const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
2803
+ browser.tabs.captureVisibleTab.withArgs(windowId, {
2804
+ format: 'png'
2805
+ }).resolves(url);
2806
+ const res = await func(windowId, {
2807
+ format: 'png'
2808
+ });
2809
+ assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i + 1,
2810
+ 'called');
2811
+ assert.strictEqual(res, url, 'result');
2812
+ });
2813
+ });
2814
+
2815
+ describe('is tab', () => {
2816
+ const func = mjs.isTab;
2817
+
2818
+ it('should throw if no argument given', async () => {
2819
+ await func().catch(e => {
2820
+ assert.deepStrictEqual(e,
2821
+ new TypeError('Expected Number but got Undefined.'));
2822
+ });
2823
+ });
2824
+
2825
+ it('should throw if argument is not number', async () => {
2826
+ await func('').catch(e => {
2827
+ assert.deepStrictEqual(e,
2828
+ new TypeError('Expected Number but got String.'));
2829
+ });
2830
+ });
2831
+
2832
+ it('should get result', async () => {
2833
+ const res = await func(-1);
2834
+ assert.strictEqual(res, false, 'res');
2835
+ });
2836
+
2837
+ it('should get result', async () => {
2838
+ const e = new Error('error');
2839
+ browser.tabs.get.withArgs(1).rejects(e);
2840
+ const res = await func(1);
2841
+ assert.strictEqual(res, false, 'res');
2842
+ });
2843
+
2844
+ it('should get result', async () => {
2845
+ browser.tabs.get.withArgs(1).resolves({});
2846
+ const res = await func(1);
2847
+ assert.strictEqual(res, true, 'res');
2848
+ });
2849
+ });
2850
+
2851
+ describe('getCurrentTheme', () => {
2852
+ const func = mjs.getCurrentTheme;
2853
+
2854
+ it('should not call function if permission is not granted', async () => {
2855
+ browser.permissions.contains.resolves(false);
2856
+ const i = browser.theme.getCurrent.callCount;
2857
+ await func('foo');
2858
+ assert.strictEqual(browser.theme.getCurrent.callCount, i,
2859
+ 'not called');
2860
+ });
2861
+
2862
+ it('should get function called and get result', async () => {
2863
+ browser.theme.getCurrent.resolves({});
2864
+ const i = browser.theme.getCurrent.callCount;
2865
+ const res = await func();
2866
+ assert.strictEqual(browser.theme.getCurrent.callCount, i + 1, 'called');
2867
+ assert.deepEqual(res, {}, 'result');
2868
+ });
2869
+
2870
+ it('should get function called and get result', async () => {
2871
+ browser.theme.getCurrent.withArgs(1).resolves({});
2872
+ const i = browser.theme.getCurrent.callCount;
2873
+ const res = await func(1);
2874
+ assert.strictEqual(browser.theme.getCurrent.callCount, i + 1, 'called');
2875
+ assert.deepEqual(res, {}, 'result');
2876
+ });
2877
+ });
2878
+
2879
+ describe('create new window', () => {
2880
+ const func = mjs.createNewWindow;
2881
+
2882
+ it('should get function called and get result', async () => {
2883
+ browser.windows.create.withArgs(null).resolves(null);
2884
+ const i = browser.windows.create.callCount;
2885
+ const res = await func();
2886
+ assert.strictEqual(browser.windows.create.callCount, i + 1, 'called');
2887
+ assert.deepEqual(res, null, 'result');
2888
+ });
2889
+
2890
+ it('should get function called and get result', async () => {
2891
+ browser.windows.create.withArgs(null).resolves(null);
2892
+ const i = browser.windows.create.callCount;
2893
+ const res = await func();
2894
+ assert.strictEqual(browser.windows.create.callCount, i + 1, 'called');
2895
+ assert.deepEqual(res, null, 'result');
2896
+ });
2897
+
2898
+ it('should get function called and get result', async () => {
2899
+ const opt = {
2900
+ foo: 'bar'
2901
+ };
2902
+ browser.windows.create.withArgs(opt).resolves(opt);
2903
+ const i = browser.windows.create.callCount;
2904
+ const res = await func(opt);
2905
+ assert.strictEqual(browser.windows.create.callCount, i + 1, 'called');
2906
+ assert.deepEqual(res, opt, 'result');
2907
+ });
2908
+ });
2909
+
2910
+ describe('get all normal windows', () => {
2911
+ const func = mjs.getAllNormalWindows;
2912
+
2913
+ it('should get function called and get result', async () => {
2914
+ browser.windows.getAll.withArgs({
2915
+ populate: false,
2916
+ windowTypes: ['normal']
2917
+ }).resolves([]);
2918
+ const i = browser.windows.getAll.withArgs({
2919
+ populate: false,
2920
+ windowTypes: ['normal']
2921
+ }).callCount;
2922
+ const res = await func();
2923
+ assert.strictEqual(browser.windows.getAll.callCount, i + 1, 'called');
2924
+ assert.strictEqual(Array.isArray(res), true, 'result');
2925
+ });
2926
+
2927
+ it('should get function called and get result', async () => {
2928
+ browser.windows.getAll.withArgs({
2929
+ populate: true,
2930
+ windowTypes: ['normal']
2931
+ }).resolves([]);
2932
+ const i = browser.windows.getAll.withArgs({
2933
+ populate: true,
2934
+ windowTypes: ['normal']
2935
+ }).callCount;
2936
+ const res = await func(true);
2937
+ assert.strictEqual(browser.windows.getAll.callCount, i + 1, 'called');
2938
+ assert.strictEqual(Array.isArray(res), true, 'result');
2939
+ });
2940
+ });
2941
+
2942
+ describe('get current window', () => {
2943
+ const func = mjs.getCurrentWindow;
2944
+
2945
+ it('should get function called and get result', async () => {
2946
+ browser.windows.getCurrent.withArgs(null).resolves(null);
2947
+ const i = browser.windows.getCurrent.callCount;
2948
+ const res = await func();
2949
+ assert.strictEqual(browser.windows.getCurrent.callCount, i + 1, 'called');
2950
+ assert.deepEqual(res, null, 'result');
2951
+ });
2952
+
2953
+ it('should get function called and get result', async () => {
2954
+ browser.windows.getCurrent.withArgs(null).resolves(null);
2955
+ const i = browser.windows.getCurrent.callCount;
2956
+ const res = await func();
2957
+ assert.strictEqual(browser.windows.getCurrent.callCount, i + 1, 'called');
2958
+ assert.deepEqual(res, null, 'result');
2959
+ });
2960
+
2961
+ it('should get function called and get result', async () => {
2962
+ const opt = {
2963
+ foo: 'bar'
2964
+ };
2965
+ browser.windows.getCurrent.withArgs(opt).resolves({});
2966
+ const i = browser.windows.getCurrent.callCount;
2967
+ const res = await func(opt);
2968
+ assert.strictEqual(browser.windows.getCurrent.callCount, i + 1, 'called');
2969
+ assert.deepEqual(res, {}, 'result');
2970
+ });
2971
+ });
2972
+
2973
+ describe('get window', () => {
2974
+ const func = mjs.getWindow;
2975
+
2976
+ it('should throw', async () => {
2977
+ await func().catch(e => {
2978
+ assert.deepStrictEqual(e,
2979
+ new TypeError('Expected Number but got Undefined.'));
2980
+ });
2981
+ });
2982
+
2983
+ it('should get result', async () => {
2984
+ browser.windows.get.withArgs(1, null).resolves({});
2985
+ const i = browser.windows.get.callCount;
2986
+ const res = await func(1);
2987
+ assert.strictEqual(browser.windows.get.callCount, i + 1, 'called');
2988
+ assert.deepEqual(res, {}, 'result');
2989
+ });
2990
+
2991
+ it('should get result', async () => {
2992
+ browser.windows.get.withArgs(1, {
2993
+ populate: true
2994
+ }).resolves({});
2995
+ const i = browser.windows.get.callCount;
2996
+ const res = await func(1, {
2997
+ populate: true
2998
+ });
2999
+ assert.strictEqual(browser.windows.get.callCount, i + 1, 'called');
3000
+ assert.deepEqual(res, {}, 'result');
3001
+ });
3002
+ });
3003
+
3004
+ describe('check whether incognito window exists', () => {
3005
+ const func = mjs.checkIncognitoWindowExists;
3006
+
3007
+ it('should get result', async () => {
3008
+ browser.windows.getAll.resolves([]);
3009
+ const res = await func();
3010
+ assert.strictEqual(res, false, 'result');
3011
+ });
3012
+
3013
+ it('should get result', async () => {
3014
+ browser.windows.getAll.resolves([
3015
+ {
3016
+ incognito: false
3017
+ },
3018
+ {
3019
+ incognito: false
3020
+ }
3021
+ ]);
3022
+ const res = await func();
3023
+ assert.strictEqual(res, false, 'result');
3024
+ });
3025
+
3026
+ it('should get result', async () => {
3027
+ browser.windows.getAll.resolves([
3028
+ {
3029
+ incognito: false
3030
+ },
3031
+ {
3032
+ incognito: true
3033
+ }
3034
+ ]);
3035
+ const res = await func();
3036
+ assert.strictEqual(res, true, 'result');
3037
+ });
3038
+ });
3039
+ });