2016-02-05 18:27:05 +00:00
const assert = require ( 'assert' ) ;
const path = require ( 'path' ) ;
const http = require ( 'http' ) ;
const url = require ( 'url' ) ;
2016-01-12 02:40:23 +00:00
describe ( '<webview> tag' , function ( ) {
this . timeout ( 10000 ) ;
2016-02-05 18:27:05 +00:00
var fixtures = path . join ( _ _dirname , 'fixtures' ) ;
var webview = null ;
2016-01-12 02:40:23 +00:00
beforeEach ( function ( ) {
2016-02-05 18:27:05 +00:00
webview = new WebView ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
afterEach ( function ( ) {
if ( document . body . contains ( webview ) ) {
2016-02-05 18:27:05 +00:00
document . body . removeChild ( webview ) ;
2016-01-12 02:40:23 +00:00
}
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'src attribute' , function ( ) {
it ( 'specifies the page to load' , function ( done ) {
webview . addEventListener ( 'console-message' , function ( e ) {
assert . equal ( e . message , 'a' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/a.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'navigates to new page when changed' , function ( done ) {
2016-01-19 22:49:40 +00:00
var listener = function ( ) {
2016-01-12 02:40:23 +00:00
webview . src = "file://" + fixtures + "/pages/b.html" ;
webview . addEventListener ( 'console-message' , function ( e ) {
assert . equal ( e . message , 'b' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
webview . removeEventListener ( 'did-finish-load' , listener ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'did-finish-load' , listener ) ;
webview . src = "file://" + fixtures + "/pages/a.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
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' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/c.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
it ( 'inserts node symbols when set' , function ( done ) {
webview . addEventListener ( 'console-message' , function ( e ) {
assert . equal ( e . message , 'function object object' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
webview . src = "file://" + fixtures + "/pages/d.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
it ( 'loads node symbols after POST navigation when set' , function ( done ) {
webview . addEventListener ( 'console-message' , function ( e ) {
assert . equal ( e . message , 'function object object' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
webview . src = "file://" + fixtures + "/pages/post.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
if ( process . platform !== 'win32' || process . execPath . toLowerCase ( ) . indexOf ( '\\out\\d\\' ) === - 1 ) {
2016-02-05 18:27:05 +00:00
it ( 'loads native modules when navigation happens' , function ( done ) {
2016-01-19 22:49:40 +00:00
var listener = function ( ) {
2016-01-12 02:40:23 +00:00
webview . removeEventListener ( 'did-finish-load' , listener ) ;
2016-01-19 22:49:40 +00:00
var listener2 = function ( e ) {
2016-01-12 02:40:23 +00:00
assert . equal ( e . message , 'function' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'console-message' , listener2 ) ;
2016-02-05 18:27:05 +00:00
webview . reload ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'did-finish-load' , listener ) ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
webview . src = "file://" + fixtures + "/pages/native-module.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
}
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'preload attribute' , function ( ) {
it ( 'loads the script before other scripts in window' , function ( done ) {
var listener ;
listener = function ( e ) {
assert . equal ( e . message , 'function object object' ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . setAttribute ( 'preload' , fixtures + "/module/preload.js" ) ;
webview . src = "file://" + fixtures + "/pages/e.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
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' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . setAttribute ( 'preload' , fixtures + "/module/preload-node-off.js" ) ;
webview . src = "file://" + fixtures + "/api/blank.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'receives ipc message in preload script' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener , listener2 , message ;
message = 'boom!' ;
listener = function ( e ) {
assert . equal ( e . channel , 'pong' ) ;
assert . deepEqual ( e . args , [ message ] ) ;
webview . removeEventListener ( 'ipc-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
2016-01-19 22:49:40 +00:00
listener2 = function ( ) {
2016-01-12 02:40:23 +00:00
webview . send ( 'ping' , message ) ;
2016-02-05 18:27:05 +00:00
webview . removeEventListener ( 'did-finish-load' , listener2 ) ;
2016-01-12 02:40:23 +00:00
} ;
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" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'httpreferrer attribute' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'sets the referrer url' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener , referrer ;
referrer = 'http://github.com/' ;
listener = function ( e ) {
assert . equal ( e . message , referrer ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . setAttribute ( 'httpreferrer' , referrer ) ;
webview . src = "file://" + fixtures + "/pages/referrer.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'useragent attribute' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'sets the user agent' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener , referrer ;
referrer = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko' ;
listener = function ( e ) {
assert . equal ( e . message , referrer ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . setAttribute ( 'useragent' , referrer ) ;
webview . src = "file://" + fixtures + "/pages/useragent.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'disablewebsecurity attribute' , function ( ) {
it ( 'does not disable web security when not set' , function ( done ) {
var encoded , listener , src ;
src = "<script src='file://" + _ _dirname + "/static/jquery-2.0.3.min.js'></script> <script>console.log('ok');</script>" ;
encoded = btoa ( unescape ( encodeURIComponent ( src ) ) ) ;
listener = function ( e ) {
assert ( /Not allowed to load local resource/ . test ( e . message ) ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . src = "data:text/html;base64," + encoded ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'disables web security when set' , function ( done ) {
2016-01-12 02:40:23 +00:00
var encoded , listener , src ;
src = "<script src='file://" + _ _dirname + "/static/jquery-2.0.3.min.js'></script> <script>console.log('ok');</script>" ;
encoded = btoa ( unescape ( encodeURIComponent ( src ) ) ) ;
listener = function ( e ) {
assert . equal ( e . message , 'ok' ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . setAttribute ( 'disablewebsecurity' , '' ) ;
webview . src = "data:text/html;base64," + encoded ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
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' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/c.html" ;
webview . partition = 'test1' ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
it ( 'inserts node symbols when set' , function ( done ) {
webview . addEventListener ( 'console-message' , function ( e ) {
assert . equal ( e . message , 'function object object' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
webview . src = "file://" + fixtures + "/pages/d.html" ;
webview . partition = 'test2' ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
it ( 'isolates storage for different id' , function ( done ) {
var listener ;
listener = function ( e ) {
assert . equal ( e . message , " 0" ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
window . localStorage . setItem ( 'test' , 'one' ) ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . src = "file://" + fixtures + "/pages/partition/one.html" ;
webview . partition = 'test3' ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'uses current session storage when no id is provided' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener ;
listener = function ( e ) {
assert . equal ( e . message , "one 1" ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
window . localStorage . setItem ( 'test' , 'one' ) ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . src = "file://" + fixtures + "/pages/partition/one.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'allowpopups attribute' , function ( ) {
it ( 'can not open new window when not set' , function ( done ) {
var listener ;
listener = function ( e ) {
assert . equal ( e . message , 'null' ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . src = "file://" + fixtures + "/pages/window-open-hide.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'can open new window when set' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener ;
listener = function ( e ) {
assert . equal ( e . message , 'window' ) ;
webview . removeEventListener ( 'console-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'console-message' , listener ) ;
webview . setAttribute ( 'allowpopups' , 'on' ) ;
webview . src = "file://" + fixtures + "/pages/window-open-hide.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'new-window event' , function ( ) {
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' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/window-open.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'emits when link with target is called' , function ( done ) {
2016-01-12 02:40:23 +00:00
webview . addEventListener ( 'new-window' , function ( e ) {
assert . equal ( e . url , 'http://host/' ) ;
assert . equal ( e . frameName , 'target' ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/target-name.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'ipc-message event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'emits when guest sends a ipc message to browser' , function ( done ) {
2016-01-12 02:40:23 +00:00
webview . addEventListener ( 'ipc-message' , function ( e ) {
assert . equal ( e . channel , 'channel' ) ;
assert . deepEqual ( e . args , [ 'arg1' , 'arg2' ] ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/ipc-message.html" ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'page-title-set event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'emits when title is set' , function ( done ) {
2016-01-12 02:40:23 +00:00
webview . addEventListener ( 'page-title-set' , function ( e ) {
assert . equal ( e . title , 'test' ) ;
assert ( e . explicitSet ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/a.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'page-favicon-updated event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'emits when favicon urls are received' , function ( done ) {
2016-01-12 02:40:23 +00:00
webview . addEventListener ( 'page-favicon-updated' , function ( e ) {
var pageUrl ;
assert . equal ( e . favicons . length , 2 ) ;
pageUrl = process . platform === 'win32' ? 'file:///C:/favicon.png' : 'file:///favicon.png' ;
assert . equal ( e . favicons [ 0 ] , pageUrl ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/a.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'will-navigate event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'emits when a url that leads to oustide of the page is clicked' , function ( done ) {
2016-01-12 02:40:23 +00:00
webview . addEventListener ( 'will-navigate' , function ( e ) {
assert . equal ( e . url , "http://host/" ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/webview-will-navigate.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'did-navigate event' , function ( ) {
var p , pageUrl ;
p = path . join ( fixtures , 'pages' , 'webview-will-navigate.html' ) ;
p = p . replace ( /\\/g , '/' ) ;
pageUrl = url . format ( {
protocol : 'file' ,
slashes : true ,
pathname : p
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'emits when a url that leads to outside of the page is clicked' , function ( done ) {
2016-01-12 02:40:23 +00:00
webview . addEventListener ( 'did-navigate' , function ( e ) {
assert . equal ( e . url , pageUrl ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = pageUrl ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
describe ( 'did-navigate-in-page event' , function ( ) {
it ( 'emits when an anchor link is clicked' , function ( done ) {
var p , pageUrl ;
p = path . join ( fixtures , 'pages' , 'webview-did-navigate-in-page.html' ) ;
p = p . replace ( /\\/g , '/' ) ;
pageUrl = url . format ( {
protocol : 'file' ,
slashes : true ,
pathname : p
} ) ;
webview . addEventListener ( 'did-navigate-in-page' , function ( e ) {
assert . equal ( e . url , pageUrl + "#test_content" ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = pageUrl ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
it ( 'emits when window.history.replaceState is called' , function ( done ) {
webview . addEventListener ( 'did-navigate-in-page' , function ( e ) {
assert . equal ( e . url , "http://host/" ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/webview-did-navigate-in-page-with-history.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'emits when window.location.hash is changed' , function ( done ) {
2016-01-12 02:40:23 +00:00
var p , pageUrl ;
p = path . join ( fixtures , 'pages' , 'webview-did-navigate-in-page-with-hash.html' ) ;
p = p . replace ( /\\/g , '/' ) ;
pageUrl = url . format ( {
protocol : 'file' ,
slashes : true ,
pathname : p
} ) ;
webview . addEventListener ( 'did-navigate-in-page' , function ( e ) {
assert . equal ( e . url , pageUrl + "#test" ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = pageUrl ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'close event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'should fire when interior page calls window.close' , function ( done ) {
2016-01-12 02:40:23 +00:00
webview . addEventListener ( 'close' , function ( ) {
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/close.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'devtools-opened event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'should fire when webview.openDevTools() is called' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener ;
listener = function ( ) {
webview . removeEventListener ( 'devtools-opened' , listener ) ;
webview . closeDevTools ( ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'devtools-opened' , listener ) ;
webview . addEventListener ( 'dom-ready' , function ( ) {
2016-02-05 18:27:05 +00:00
webview . openDevTools ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/base-page.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'devtools-closed event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'should fire when webview.closeDevTools() is called' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener , listener2 ;
listener2 = function ( ) {
webview . removeEventListener ( 'devtools-closed' , listener2 ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
listener = function ( ) {
webview . removeEventListener ( 'devtools-opened' , listener ) ;
2016-02-05 18:27:05 +00:00
webview . closeDevTools ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'devtools-opened' , listener ) ;
webview . addEventListener ( 'devtools-closed' , listener2 ) ;
webview . addEventListener ( 'dom-ready' , function ( ) {
2016-02-05 18:27:05 +00:00
webview . openDevTools ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/base-page.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'devtools-focused event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'should fire when webview.openDevTools() is called' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener ;
listener = function ( ) {
webview . removeEventListener ( 'devtools-focused' , listener ) ;
webview . closeDevTools ( ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'devtools-focused' , listener ) ;
webview . addEventListener ( 'dom-ready' , function ( ) {
2016-02-05 18:27:05 +00:00
webview . openDevTools ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/base-page.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( '<webview>.reload()' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'should emit beforeunload handler' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener , listener2 ;
listener = function ( e ) {
assert . equal ( e . channel , 'onbeforeunload' ) ;
webview . removeEventListener ( 'ipc-message' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
2016-01-19 22:49:40 +00:00
listener2 = function ( ) {
2016-01-12 02:40:23 +00:00
webview . reload ( ) ;
2016-02-05 18:27:05 +00:00
webview . removeEventListener ( 'did-finish-load' , listener2 ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'ipc-message' , listener ) ;
webview . addEventListener ( 'did-finish-load' , listener2 ) ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
webview . src = "file://" + fixtures + "/pages/beforeunload-false.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( '<webview>.clearHistory()' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'should clear the navigation history' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener ;
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 ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'ipc-message' , listener ) ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
webview . src = "file://" + fixtures + "/pages/history.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'basic auth' , function ( ) {
var auth ;
auth = require ( 'basic-auth' ) ;
2016-02-05 18:27:05 +00:00
it ( 'should authenticate with correct credentials' , function ( done ) {
2016-01-12 02:40:23 +00:00
var message , server ;
message = 'Authenticated' ;
server = http . createServer ( function ( req , res ) {
var credentials ;
credentials = auth ( req ) ;
if ( credentials . name === 'test' && credentials . pass === 'test' ) {
res . end ( message ) ;
} else {
res . end ( 'failed' ) ;
}
2016-02-05 18:27:05 +00:00
server . close ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
server . listen ( 0 , '127.0.0.1' , function ( ) {
2016-01-12 02:40:23 +00:00
var port ;
port = server . address ( ) . port ;
webview . addEventListener ( 'ipc-message' , function ( e ) {
assert . equal ( e . channel , message ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/basic-auth.html?port=" + port ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'dom-ready event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'emits when document is loaded' , function ( done ) {
2016-01-12 02:40:23 +00:00
var server ;
2016-01-19 22:49:40 +00:00
server = http . createServer ( function ( ) { } ) ;
2016-02-05 18:27:05 +00:00
server . listen ( 0 , '127.0.0.1' , function ( ) {
2016-01-12 02:40:23 +00:00
var port ;
port = server . address ( ) . port ;
webview . addEventListener ( 'dom-ready' , function ( ) {
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/dom-ready.html?port=" + port ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:48:07 +00:00
it ( 'throws a custom error when an API method is called before the event is emitted' , function ( ) {
2016-02-09 21:53:10 +00:00
assert . throws ( function ( ) {
2016-02-09 21:55:49 +00:00
webview . stop ( ) ;
2016-02-11 21:32:34 +00:00
} , '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.' ) ;
2016-02-05 18:48:07 +00:00
} ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'executeJavaScript' , function ( ) {
if ( process . env . TRAVIS !== 'true' ) {
return ;
}
2016-02-05 18:27:05 +00:00
it ( 'should support user gesture' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener , listener2 ;
2016-01-19 22:49:40 +00:00
listener = function ( ) {
2016-01-12 02:40:23 +00:00
webview . removeEventListener ( 'enter-html-full-screen' , listener ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ;
2016-01-19 22:49:40 +00:00
listener2 = function ( ) {
2016-01-12 02:40:23 +00:00
var jsScript ;
jsScript = 'document.getElementsByTagName("video")[0].webkitRequestFullScreen()' ;
webview . executeJavaScript ( jsScript , true ) ;
2016-02-05 18:27:05 +00:00
webview . removeEventListener ( 'did-finish-load' , listener2 ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'enter-html-full-screen' , listener ) ;
webview . addEventListener ( 'did-finish-load' , listener2 ) ;
webview . src = "file://" + fixtures + "/pages/fullscreen.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
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 ] ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . addEventListener ( 'dom-ready' , function ( ) {
2016-02-05 18:27:05 +00:00
webview . sendInputEvent ( {
2016-01-12 02:40:23 +00:00
type : 'keyup' ,
keyCode : 'c' ,
modifiers : [ 'shift' ]
} ) ;
} ) ;
webview . src = "file://" + fixtures + "/pages/onkeyup.html" ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'can send mouse event' , function ( done ) {
2016-01-12 02:40:23 +00:00
webview . addEventListener ( 'ipc-message' , function ( e ) {
assert . equal ( e . channel , 'mouseup' ) ;
assert . deepEqual ( e . args , [ 10 , 20 , false , true ] ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . addEventListener ( 'dom-ready' , function ( ) {
2016-02-05 18:27:05 +00:00
webview . sendInputEvent ( {
2016-01-12 02:40:23 +00:00
type : 'mouseup' ,
modifiers : [ 'ctrl' ] ,
x : 10 ,
y : 20
} ) ;
} ) ;
webview . src = "file://" + fixtures + "/pages/onmouseup.html" ;
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'media-started-playing media-paused events' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'emits when audio starts and stops playing' , function ( done ) {
2016-01-12 02:40:23 +00:00
var audioPlayed ;
audioPlayed = false ;
webview . addEventListener ( 'media-started-playing' , function ( ) {
2016-02-05 18:27:05 +00:00
audioPlayed = true ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . addEventListener ( 'media-paused' , function ( ) {
assert ( audioPlayed ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/audio.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-12 02:40:23 +00:00
describe ( 'found-in-page event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'emits when a request is made' , function ( done ) {
2016-01-12 02:40:23 +00:00
var listener , listener2 , requestId ;
requestId = null ;
listener = function ( e ) {
assert . equal ( e . result . requestId , requestId ) ;
if ( e . result . finalUpdate ) {
assert . equal ( e . result . matches , 3 ) ;
webview . stopFindInPage ( "clearSelection" ) ;
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
}
} ;
2016-01-19 22:49:40 +00:00
listener2 = function ( ) {
2016-02-05 18:27:05 +00:00
requestId = webview . findInPage ( "virtual" ) ;
2016-01-12 02:40:23 +00:00
} ;
webview . addEventListener ( 'found-in-page' , listener ) ;
webview . addEventListener ( 'did-finish-load' , listener2 ) ;
webview . src = "file://" + fixtures + "/pages/content.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-25 16:37:15 +00:00
xdescribe ( 'did-change-theme-color event' , function ( ) {
2016-02-05 18:27:05 +00:00
it ( 'emits when theme color changes' , function ( done ) {
2016-01-19 22:49:40 +00:00
webview . addEventListener ( 'did-change-theme-color' , function ( ) {
2016-02-05 18:27:05 +00:00
done ( ) ;
2016-01-12 02:40:23 +00:00
} ) ;
webview . src = "file://" + fixtures + "/pages/theme-color.html" ;
2016-02-05 18:27:05 +00:00
document . body . appendChild ( webview ) ;
2016-01-12 02:40:23 +00:00
} ) ;
} ) ;
2016-02-05 18:27:05 +00:00
2016-01-25 16:37:15 +00:00
describe ( 'permission-request event' , function ( ) {
2016-01-31 21:35:34 +00:00
function setUpRequestHandler ( webview , requested _permission ) {
const session = require ( 'electron' ) . remote . session ;
var listener = function ( webContents , permission , callback ) {
if ( webContents . getId ( ) === webview . getId ( ) ) {
assert . equal ( permission , requested _permission ) ;
2016-02-01 10:03:38 +00:00
callback ( false ) ;
2016-01-31 21:35:34 +00:00
}
} ;
session . fromPartition ( webview . partition ) . setPermissionRequestHandler ( listener ) ;
}
2016-02-05 18:27:05 +00:00
it ( 'emits when using navigator.getUserMedia api' , function ( done ) {
2016-01-25 16:37:15 +00:00
webview . addEventListener ( 'ipc-message' , function ( e ) {
assert ( e . channel , 'message' ) ;
assert ( e . args , [ 'PermissionDeniedError' ] ) ;
done ( ) ;
} ) ;
2016-01-30 13:31:10 +00:00
webview . src = "file://" + fixtures + "/pages/permissions/media.html" ;
2016-01-31 21:35:34 +00:00
webview . partition = "permissionTest" ;
2016-01-30 13:31:10 +00:00
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
2016-01-31 21:35:34 +00:00
setUpRequestHandler ( webview , "media" ) ;
2016-01-30 13:31:10 +00:00
document . body . appendChild ( webview ) ;
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'emits when using navigator.geolocation api' , function ( done ) {
2016-01-30 13:31:10 +00:00
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" ;
2016-01-31 21:35:34 +00:00
webview . partition = "permissionTest" ;
2016-01-30 13:31:10 +00:00
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
2016-01-31 21:35:34 +00:00
setUpRequestHandler ( webview , "geolocation" ) ;
2016-01-30 13:31:10 +00:00
document . body . appendChild ( webview ) ;
} ) ;
2016-02-05 18:27:05 +00:00
it ( 'emits when using navigator.requestMIDIAccess api' , function ( done ) {
2016-01-30 13:31:10 +00:00
webview . addEventListener ( 'ipc-message' , function ( e ) {
assert ( e . channel , 'message' ) ;
assert ( e . args , [ 'SecurityError' ] ) ;
done ( ) ;
} ) ;
webview . src = "file://" + fixtures + "/pages/permissions/midi.html" ;
2016-01-31 21:35:34 +00:00
webview . partition = "permissionTest" ;
2016-01-25 16:37:15 +00:00
webview . setAttribute ( 'nodeintegration' , 'on' ) ;
2016-01-31 21:35:34 +00:00
setUpRequestHandler ( webview , "midiSysex" ) ;
2016-01-25 16:37:15 +00:00
document . body . appendChild ( webview ) ;
} ) ;
} ) ;
2016-01-12 02:40:23 +00:00
} ) ;