forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebview-spec.js
935 lines (852 loc) · 32.6 KB
/
webview-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
const assert = require('assert')
const path = require('path')
const http = require('http')
const url = require('url')
const {app, session, ipcMain, BrowserWindow} = require('electron').remote
describe('<webview> tag', function () {
this.timeout(20000)
var fixtures = path.join(__dirname, 'fixtures')
var webview = null
let w = null
beforeEach(function () {
webview = new WebView()
})
afterEach(function () {
if (document.body.contains(webview)) {
document.body.removeChild(webview)
}
if (w) {
w.destroy()
w = null
}
})
it('works without script tag in page', function (done) {
w = new BrowserWindow({show: false})
ipcMain.once('pong', function () {
done()
})
w.loadURL('file://' + fixtures + '/pages/webview-no-script.html')
})
it('is disabled when nodeIntegration is disabled', function (done) {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: false,
preload: path.join(fixtures, 'module', 'preload-webview.js')
}
})
ipcMain.once('webview', function (event, type) {
if (type === 'undefined') {
done()
} else {
done('WebView still exists')
}
})
w.loadURL('file://' + fixtures + '/pages/webview-no-script.html')
})
describe('src attribute', function () {
it('specifies the page to load', function (done) {
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'a')
done()
})
webview.src = 'file://' + fixtures + '/pages/a.html'
document.body.appendChild(webview)
})
it('navigates to new page when changed', function (done) {
var listener = function () {
webview.src = 'file://' + fixtures + '/pages/b.html'
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'b')
done()
})
webview.removeEventListener('did-finish-load', listener)
}
webview.addEventListener('did-finish-load', listener)
webview.src = 'file://' + fixtures + '/pages/a.html'
document.body.appendChild(webview)
})
})
describe('nodeintegration attribute', function () {
it('inserts no node symbols when not set', function (done) {
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'undefined undefined undefined undefined')
done()
})
webview.src = 'file://' + fixtures + '/pages/c.html'
document.body.appendChild(webview)
})
it('inserts node symbols when set', function (done) {
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'function object object')
done()
})
webview.setAttribute('nodeintegration', 'on')
webview.src = 'file://' + fixtures + '/pages/d.html'
document.body.appendChild(webview)
})
it('loads node symbols after POST navigation when set', function (done) {
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'function object object')
done()
})
webview.setAttribute('nodeintegration', 'on')
webview.src = 'file://' + fixtures + '/pages/post.html'
document.body.appendChild(webview)
})
it('disables node integration on child windows when it is disabled on the webview', function (done) {
app.once('browser-window-created', function (event, window) {
assert.equal(window.webContents.getWebPreferences().nodeIntegration, false)
done()
})
webview.setAttribute('allowpopups', 'on')
webview.src = url.format({
pathname: `${fixtures}/pages/webview-opener-no-node-integration.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-node.html`
},
slashes: true
})
document.body.appendChild(webview)
})
if (process.platform !== 'win32' || process.execPath.toLowerCase().indexOf('\\out\\d\\') === -1) {
it('loads native modules when navigation happens', function (done) {
var listener = function () {
webview.removeEventListener('did-finish-load', listener)
var listener2 = function (e) {
assert.equal(e.message, 'function')
done()
}
webview.addEventListener('console-message', listener2)
webview.reload()
}
webview.addEventListener('did-finish-load', listener)
webview.setAttribute('nodeintegration', 'on')
webview.src = 'file://' + fixtures + '/pages/native-module.html'
document.body.appendChild(webview)
})
}
})
describe('preload attribute', function () {
it('loads the script before other scripts in window', function (done) {
var listener = function (e) {
assert.equal(e.message, 'function object object')
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.setAttribute('preload', fixtures + '/module/preload.js')
webview.src = 'file://' + fixtures + '/pages/e.html'
document.body.appendChild(webview)
})
it('preload script can still use "process" in required modules when nodeintegration is off', function (done) {
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'object undefined object')
done()
})
webview.setAttribute('preload', fixtures + '/module/preload-node-off.js')
webview.src = 'file://' + fixtures + '/api/blank.html'
document.body.appendChild(webview)
})
it('receives ipc message in preload script', function (done) {
var message = 'boom!'
var listener = function (e) {
assert.equal(e.channel, 'pong')
assert.deepEqual(e.args, [message])
webview.removeEventListener('ipc-message', listener)
done()
}
var listener2 = function () {
webview.send('ping', message)
webview.removeEventListener('did-finish-load', listener2)
}
webview.addEventListener('ipc-message', listener)
webview.addEventListener('did-finish-load', listener2)
webview.setAttribute('preload', fixtures + '/module/preload-ipc.js')
webview.src = 'file://' + fixtures + '/pages/e.html'
document.body.appendChild(webview)
})
it('works without script tag in page', function (done) {
var listener = function (e) {
assert.equal(e.message, 'function object object')
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.setAttribute('preload', fixtures + '/module/preload.js')
webview.src = 'file://' + fixtures + '/pages/base-page.html'
document.body.appendChild(webview)
})
})
describe('httpreferrer attribute', function () {
it('sets the referrer url', function (done) {
var referrer = 'http://github.com/'
var listener = function (e) {
assert.equal(e.message, referrer)
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.setAttribute('httpreferrer', referrer)
webview.src = 'file://' + fixtures + '/pages/referrer.html'
document.body.appendChild(webview)
})
})
describe('useragent attribute', function () {
it('sets the user agent', function (done) {
var referrer = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko'
var listener = function (e) {
assert.equal(e.message, referrer)
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.setAttribute('useragent', referrer)
webview.src = 'file://' + fixtures + '/pages/useragent.html'
document.body.appendChild(webview)
})
})
describe('disablewebsecurity attribute', function () {
it('does not disable web security when not set', function (done) {
var jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js')
var src = `<script src='file://${jqueryPath}'></script> <script>console.log('ok');</script>`
var encoded = btoa(unescape(encodeURIComponent(src)))
var listener = function (e) {
assert(/Not allowed to load local resource/.test(e.message))
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.src = 'data:text/html;base64,' + encoded
document.body.appendChild(webview)
})
it('disables web security when set', function (done) {
var jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js')
var src = `<script src='file://${jqueryPath}'></script> <script>console.log('ok');</script>`
var encoded = btoa(unescape(encodeURIComponent(src)))
var listener = function (e) {
assert.equal(e.message, 'ok')
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.setAttribute('disablewebsecurity', '')
webview.src = 'data:text/html;base64,' + encoded
document.body.appendChild(webview)
})
it('does not break node integration', function (done) {
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'function object object')
done()
})
webview.setAttribute('nodeintegration', 'on')
webview.setAttribute('disablewebsecurity', '')
webview.src = 'file://' + fixtures + '/pages/d.html'
document.body.appendChild(webview)
})
it('does not break preload script', function (done) {
var listener = function (e) {
assert.equal(e.message, 'function object object')
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.setAttribute('disablewebsecurity', '')
webview.setAttribute('preload', fixtures + '/module/preload.js')
webview.src = 'file://' + fixtures + '/pages/e.html'
document.body.appendChild(webview)
})
})
describe('partition attribute', function () {
it('inserts no node symbols when not set', function (done) {
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'undefined undefined undefined undefined')
done()
})
webview.src = 'file://' + fixtures + '/pages/c.html'
webview.partition = 'test1'
document.body.appendChild(webview)
})
it('inserts node symbols when set', function (done) {
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'function object object')
done()
})
webview.setAttribute('nodeintegration', 'on')
webview.src = 'file://' + fixtures + '/pages/d.html'
webview.partition = 'test2'
document.body.appendChild(webview)
})
it('isolates storage for different id', function (done) {
var listener = function (e) {
assert.equal(e.message, ' 0')
webview.removeEventListener('console-message', listener)
done()
}
window.localStorage.setItem('test', 'one')
webview.addEventListener('console-message', listener)
webview.src = 'file://' + fixtures + '/pages/partition/one.html'
webview.partition = 'test3'
document.body.appendChild(webview)
})
it('uses current session storage when no id is provided', function (done) {
var listener = function (e) {
assert.equal(e.message, 'one 1')
webview.removeEventListener('console-message', listener)
done()
}
window.localStorage.setItem('test', 'one')
webview.addEventListener('console-message', listener)
webview.src = 'file://' + fixtures + '/pages/partition/one.html'
document.body.appendChild(webview)
})
})
describe('allowpopups attribute', function () {
if (process.env.TRAVIS === 'true' && process.platform === 'darwin') {
return
}
it('can not open new window when not set', function (done) {
var listener = function (e) {
assert.equal(e.message, 'null')
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.src = 'file://' + fixtures + '/pages/window-open-hide.html'
document.body.appendChild(webview)
})
it('can open new window when set', function (done) {
var listener = function (e) {
assert.equal(e.message, 'window')
webview.removeEventListener('console-message', listener)
done()
}
webview.addEventListener('console-message', listener)
webview.setAttribute('allowpopups', 'on')
webview.src = 'file://' + fixtures + '/pages/window-open-hide.html'
document.body.appendChild(webview)
})
})
describe('new-window event', function () {
if (process.env.TRAVIS === 'true' && process.platform === 'darwin') {
return
}
it('emits when window.open is called', function (done) {
webview.addEventListener('new-window', function (e) {
assert.equal(e.url, 'http://host/')
assert.equal(e.frameName, 'host')
done()
})
webview.src = 'file://' + fixtures + '/pages/window-open.html'
document.body.appendChild(webview)
})
it('emits when link with target is called', function (done) {
webview.addEventListener('new-window', function (e) {
assert.equal(e.url, 'http://host/')
assert.equal(e.frameName, 'target')
done()
})
webview.src = 'file://' + fixtures + '/pages/target-name.html'
document.body.appendChild(webview)
})
})
describe('ipc-message event', function () {
it('emits when guest sends a ipc message to browser', function (done) {
webview.addEventListener('ipc-message', function (e) {
assert.equal(e.channel, 'channel')
assert.deepEqual(e.args, ['arg1', 'arg2'])
done()
})
webview.src = 'file://' + fixtures + '/pages/ipc-message.html'
webview.setAttribute('nodeintegration', 'on')
document.body.appendChild(webview)
})
})
describe('page-title-set event', function () {
it('emits when title is set', function (done) {
webview.addEventListener('page-title-set', function (e) {
assert.equal(e.title, 'test')
assert(e.explicitSet)
done()
})
webview.src = 'file://' + fixtures + '/pages/a.html'
document.body.appendChild(webview)
})
})
describe('page-favicon-updated event', function () {
it('emits when favicon urls are received', function (done) {
webview.addEventListener('page-favicon-updated', function (e) {
assert.equal(e.favicons.length, 2)
var pageUrl = process.platform === 'win32' ? 'file:///C:/favicon.png' : 'file:///favicon.png'
assert.equal(e.favicons[0], pageUrl)
done()
})
webview.src = 'file://' + fixtures + '/pages/a.html'
document.body.appendChild(webview)
})
})
describe('will-navigate event', function () {
it('emits when a url that leads to oustide of the page is clicked', function (done) {
webview.addEventListener('will-navigate', function (e) {
assert.equal(e.url, 'http://host/')
done()
})
webview.src = 'file://' + fixtures + '/pages/webview-will-navigate.html'
document.body.appendChild(webview)
})
})
describe('did-navigate event', function () {
var p = path.join(fixtures, 'pages', 'webview-will-navigate.html')
p = p.replace(/\\/g, '/')
var pageUrl = url.format({
protocol: 'file',
slashes: true,
pathname: p
})
it('emits when a url that leads to outside of the page is clicked', function (done) {
webview.addEventListener('did-navigate', function (e) {
assert.equal(e.url, pageUrl)
done()
})
webview.src = pageUrl
document.body.appendChild(webview)
})
})
describe('did-navigate-in-page event', function () {
it('emits when an anchor link is clicked', function (done) {
var p = path.join(fixtures, 'pages', 'webview-did-navigate-in-page.html')
p = p.replace(/\\/g, '/')
var pageUrl = url.format({
protocol: 'file',
slashes: true,
pathname: p
})
webview.addEventListener('did-navigate-in-page', function (e) {
assert.equal(e.url, pageUrl + '#test_content')
done()
})
webview.src = pageUrl
document.body.appendChild(webview)
})
it('emits when window.history.replaceState is called', function (done) {
webview.addEventListener('did-navigate-in-page', function (e) {
assert.equal(e.url, 'http://host/')
done()
})
webview.src = 'file://' + fixtures + '/pages/webview-did-navigate-in-page-with-history.html'
document.body.appendChild(webview)
})
it('emits when window.location.hash is changed', function (done) {
var p = path.join(fixtures, 'pages', 'webview-did-navigate-in-page-with-hash.html')
p = p.replace(/\\/g, '/')
var pageUrl = url.format({
protocol: 'file',
slashes: true,
pathname: p
})
webview.addEventListener('did-navigate-in-page', function (e) {
assert.equal(e.url, pageUrl + '#test')
done()
})
webview.src = pageUrl
document.body.appendChild(webview)
})
})
describe('close event', function () {
it('should fire when interior page calls window.close', function (done) {
webview.addEventListener('close', function () {
done()
})
webview.src = 'file://' + fixtures + '/pages/close.html'
document.body.appendChild(webview)
})
})
describe('devtools-opened event', function () {
it('should fire when webview.openDevTools() is called', function (done) {
var listener = function () {
webview.removeEventListener('devtools-opened', listener)
webview.closeDevTools()
done()
}
webview.addEventListener('devtools-opened', listener)
webview.addEventListener('dom-ready', function () {
webview.openDevTools()
})
webview.src = 'file://' + fixtures + '/pages/base-page.html'
document.body.appendChild(webview)
})
})
describe('devtools-closed event', function () {
it('should fire when webview.closeDevTools() is called', function (done) {
var listener2 = function () {
webview.removeEventListener('devtools-closed', listener2)
done()
}
var listener = function () {
webview.removeEventListener('devtools-opened', listener)
webview.closeDevTools()
}
webview.addEventListener('devtools-opened', listener)
webview.addEventListener('devtools-closed', listener2)
webview.addEventListener('dom-ready', function () {
webview.openDevTools()
})
webview.src = 'file://' + fixtures + '/pages/base-page.html'
document.body.appendChild(webview)
})
})
describe('devtools-focused event', function () {
it('should fire when webview.openDevTools() is called', function (done) {
var listener = function () {
webview.removeEventListener('devtools-focused', listener)
webview.closeDevTools()
done()
}
webview.addEventListener('devtools-focused', listener)
webview.addEventListener('dom-ready', function () {
webview.openDevTools()
})
webview.src = 'file://' + fixtures + '/pages/base-page.html'
document.body.appendChild(webview)
})
})
describe('<webview>.reload()', function () {
it('should emit beforeunload handler', function (done) {
var listener = function (e) {
assert.equal(e.channel, 'onbeforeunload')
webview.removeEventListener('ipc-message', listener)
done()
}
var listener2 = function () {
webview.reload()
webview.removeEventListener('did-finish-load', listener2)
}
webview.addEventListener('ipc-message', listener)
webview.addEventListener('did-finish-load', listener2)
webview.setAttribute('nodeintegration', 'on')
webview.src = 'file://' + fixtures + '/pages/beforeunload-false.html'
document.body.appendChild(webview)
})
})
describe('<webview>.clearHistory()', function () {
it('should clear the navigation history', function (done) {
var listener = function (e) {
assert.equal(e.channel, 'history')
assert.equal(e.args[0], 2)
assert(webview.canGoBack())
webview.clearHistory()
assert(!webview.canGoBack())
webview.removeEventListener('ipc-message', listener)
done()
}
webview.addEventListener('ipc-message', listener)
webview.setAttribute('nodeintegration', 'on')
webview.src = 'file://' + fixtures + '/pages/history.html'
document.body.appendChild(webview)
})
})
describe('basic auth', function () {
var auth = require('basic-auth')
it('should authenticate with correct credentials', function (done) {
var message = 'Authenticated'
var server = http.createServer(function (req, res) {
var credentials = auth(req)
if (credentials.name === 'test' && credentials.pass === 'test') {
res.end(message)
} else {
res.end('failed')
}
server.close()
})
server.listen(0, '127.0.0.1', function () {
var port = server.address().port
webview.addEventListener('ipc-message', function (e) {
assert.equal(e.channel, message)
done()
})
webview.src = 'file://' + fixtures + '/pages/basic-auth.html?port=' + port
webview.setAttribute('nodeintegration', 'on')
document.body.appendChild(webview)
})
})
})
describe('dom-ready event', function () {
it('emits when document is loaded', function (done) {
var server = http.createServer(function () {})
server.listen(0, '127.0.0.1', function () {
var port = server.address().port
webview.addEventListener('dom-ready', function () {
done()
})
webview.src = 'file://' + fixtures + '/pages/dom-ready.html?port=' + port
document.body.appendChild(webview)
})
})
it('throws a custom error when an API method is called before the event is emitted', function () {
assert.throws(function () {
webview.stop()
}, 'Cannot call stop because the webContents is unavailable. The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.')
})
})
describe('executeJavaScript', function () {
it('should support user gesture', function (done) {
if (process.env.TRAVIS !== 'true' || process.platform === 'darwin') return done()
var listener = function () {
webview.removeEventListener('enter-html-full-screen', listener)
done()
}
var listener2 = function () {
var jsScript = "document.querySelector('video').webkitRequestFullscreen()"
webview.executeJavaScript(jsScript, true)
webview.removeEventListener('did-finish-load', listener2)
}
webview.addEventListener('enter-html-full-screen', listener)
webview.addEventListener('did-finish-load', listener2)
webview.src = 'file://' + fixtures + '/pages/fullscreen.html'
document.body.appendChild(webview)
})
it('can return the result of the executed script', function (done) {
if (process.env.TRAVIS === 'true' && process.platform === 'darwin') return done()
var listener = function () {
var jsScript = "'4'+2"
webview.executeJavaScript(jsScript, false, function (result) {
assert.equal(result, '42')
done()
})
webview.removeEventListener('did-finish-load', listener)
}
webview.addEventListener('did-finish-load', listener)
webview.src = 'about:blank'
document.body.appendChild(webview)
})
})
describe('sendInputEvent', function () {
it('can send keyboard event', function (done) {
webview.addEventListener('ipc-message', function (e) {
assert.equal(e.channel, 'keyup')
assert.deepEqual(e.args, [67, true, false])
done()
})
webview.addEventListener('dom-ready', function () {
webview.sendInputEvent({
type: 'keyup',
keyCode: 'c',
modifiers: ['shift']
})
})
webview.src = 'file://' + fixtures + '/pages/onkeyup.html'
webview.setAttribute('nodeintegration', 'on')
document.body.appendChild(webview)
})
it('can send mouse event', function (done) {
webview.addEventListener('ipc-message', function (e) {
assert.equal(e.channel, 'mouseup')
assert.deepEqual(e.args, [10, 20, false, true])
done()
})
webview.addEventListener('dom-ready', function () {
webview.sendInputEvent({
type: 'mouseup',
modifiers: ['ctrl'],
x: 10,
y: 20
})
})
webview.src = 'file://' + fixtures + '/pages/onmouseup.html'
webview.setAttribute('nodeintegration', 'on')
document.body.appendChild(webview)
})
})
describe('media-started-playing media-paused events', function () {
it('emits when audio starts and stops playing', function (done) {
var audioPlayed = false
webview.addEventListener('media-started-playing', function () {
audioPlayed = true
})
webview.addEventListener('media-paused', function () {
assert(audioPlayed)
done()
})
webview.src = 'file://' + fixtures + '/pages/audio.html'
document.body.appendChild(webview)
})
})
describe('found-in-page event', function () {
it('emits when a request is made', function (done) {
var requestId = null
var totalMatches = null
var activeMatchOrdinal = []
var listener = function (e) {
assert.equal(e.result.requestId, requestId)
if (e.result.finalUpdate) {
assert.equal(e.result.matches, 3)
totalMatches = e.result.matches
listener2()
} else {
activeMatchOrdinal.push(e.result.activeMatchOrdinal)
if (e.result.activeMatchOrdinal === totalMatches) {
assert.deepEqual(activeMatchOrdinal, [1, 2, 3])
webview.stopFindInPage('clearSelection')
done()
}
}
}
var listener2 = function () {
requestId = webview.findInPage('virtual')
}
webview.addEventListener('found-in-page', listener)
webview.addEventListener('did-finish-load', listener2)
webview.src = 'file://' + fixtures + '/pages/content.html'
document.body.appendChild(webview)
})
})
xdescribe('did-change-theme-color event', function () {
it('emits when theme color changes', function (done) {
webview.addEventListener('did-change-theme-color', function () {
done()
})
webview.src = 'file://' + fixtures + '/pages/theme-color.html'
document.body.appendChild(webview)
})
})
describe('permission-request event', function () {
function setUpRequestHandler (webview, requestedPermission, completed) {
var listener = function (webContents, permission, callback) {
if (webContents.getId() === webview.getId()) {
assert.equal(permission, requestedPermission)
callback(false)
if (completed) completed()
}
}
session.fromPartition(webview.partition).setPermissionRequestHandler(listener)
}
it('emits when using navigator.getUserMedia api', function (done) {
webview.addEventListener('ipc-message', function (e) {
assert(e.channel, 'message')
assert(e.args, ['PermissionDeniedError'])
done()
})
webview.src = 'file://' + fixtures + '/pages/permissions/media.html'
webview.partition = 'permissionTest'
webview.setAttribute('nodeintegration', 'on')
setUpRequestHandler(webview, 'media')
document.body.appendChild(webview)
})
it('emits when using navigator.geolocation api', function (done) {
webview.addEventListener('ipc-message', function (e) {
assert(e.channel, 'message')
assert(e.args, ['ERROR(1): User denied Geolocation'])
done()
})
webview.src = 'file://' + fixtures + '/pages/permissions/geolocation.html'
webview.partition = 'permissionTest'
webview.setAttribute('nodeintegration', 'on')
setUpRequestHandler(webview, 'geolocation')
document.body.appendChild(webview)
})
it('emits when using navigator.requestMIDIAccess api', function (done) {
webview.addEventListener('ipc-message', function (e) {
assert(e.channel, 'message')
assert(e.args, ['SecurityError'])
done()
})
webview.src = 'file://' + fixtures + '/pages/permissions/midi.html'
webview.partition = 'permissionTest'
webview.setAttribute('nodeintegration', 'on')
setUpRequestHandler(webview, 'midiSysex')
document.body.appendChild(webview)
})
it('emits when accessing external protocol', function (done) {
webview.src = 'magnet:test'
webview.partition = 'permissionTest'
setUpRequestHandler(webview, 'openExternal', done)
document.body.appendChild(webview)
})
})
describe('<webview>.getWebContents', function () {
it('can return the webcontents associated', function (done) {
webview.addEventListener('did-finish-load', function () {
const webviewContents = webview.getWebContents()
assert(webviewContents)
assert.equal(webviewContents.getURL(), 'about:blank')
done()
})
webview.src = 'about:blank'
document.body.appendChild(webview)
})
})
describe('did-get-response-details event', function () {
it('emits for the page and its resources', function (done) {
// expected {fileName: resourceType} pairs
var expectedResources = {
'did-get-response-details.html': 'mainFrame',
'logo.png': 'image'
}
var responses = 0
webview.addEventListener('did-get-response-details', function (event) {
responses++
var fileName = event.newURL.slice(event.newURL.lastIndexOf('/') + 1)
var expectedType = expectedResources[fileName]
assert(!!expectedType, `Unexpected response details for ${event.newURL}`)
assert(typeof event.status === 'boolean', 'status should be boolean')
assert.equal(event.httpResponseCode, 200)
assert.equal(event.requestMethod, 'GET')
assert(typeof event.referrer === 'string', 'referrer should be string')
assert(!!event.headers, 'headers should be present')
assert(typeof event.headers === 'object', 'headers should be object')
assert.equal(event.resourceType, expectedType, 'Incorrect resourceType')
if (responses === Object.keys(expectedResources).length) {
done()
}
})
webview.src = 'file://' + path.join(fixtures, 'pages', 'did-get-response-details.html')
document.body.appendChild(webview)
})
})
it('inherits the zoomFactor of the parent window', function (done) {
w = new BrowserWindow({
show: false,
webPreferences: {
zoomFactor: 1.2
}
})
ipcMain.once('pong', function (event, zoomFactor, zoomLevel) {
assert.equal(zoomFactor, 1.2)
assert.equal(zoomLevel, 1)
done()
})
w.loadURL('file://' + fixtures + '/pages/webview-zoom-factor.html')
})
it('inherits the parent window visibility state and receives visibilitychange events', function (done) {
w = new BrowserWindow({
show: false
})
ipcMain.once('pong', function (event, visibilityState, hidden) {
assert.equal(visibilityState, 'hidden')
assert.equal(hidden, true)
w.webContents.send('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', 'visible')
ipcMain.once('pong', function (event, visibilityState, hidden) {
assert.equal(visibilityState, 'visible')
assert.equal(hidden, false)
done()
})
})
w.loadURL('file://' + fixtures + '/pages/webview-visibilitychange.html')
})
it('loads devtools extensions registered on the parent window', function (done) {
this.timeout(10000)
w = new BrowserWindow({
show: false
})
BrowserWindow.removeDevToolsExtension('foo')
var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
BrowserWindow.addDevToolsExtension(extensionPath)
w.loadURL('file://' + fixtures + '/pages/webview-devtools.html')
ipcMain.once('answer', function (event, message) {
assert.equal(message.runtimeId, 'foo')
assert.notEqual(message.tabId, w.webContents.id)
done()
})
})
})