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