1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 embedder.triggerNavUrl =
8 'data:text/html,<html><body>trigger navigation<body></html>';
10 window.runTest = function(testName) {
11 if (!embedder.test.testList[testName]) {
12 console.log('Incorrect testName: ' + testName);
18 embedder.test.testList[testName]();
20 // window.* exported functions end.
23 embedder.setUpGuest_ = function(partitionName) {
24 document.querySelector('#webview-tag-container').innerHTML =
25 '<webview style="width: 100px; height: 100px;"></webview>';
26 var webview = document.querySelector('webview');
28 webview.partition = partitionName;
31 embedder.test.fail('No <webview> element created');
37 embedder.test.succeed = function() {
38 chrome.test.sendMessage('TEST_PASSED');
41 embedder.test.fail = function() {
42 chrome.test.sendMessage('TEST_FAILED');
45 embedder.test.assertEq = function(a, b) {
47 console.log('assertion failed: ' + a + ' != ' + b);
52 embedder.test.assertTrue = function(condition) {
54 console.log('assertion failed: true != ' + condition);
59 embedder.test.assertFalse = function(condition) {
61 console.log('assertion failed: false != ' + condition);
67 // This test verifies that a webview loses pointer lock when it loses focus.
68 function testPointerLockLostWithFocus() {
69 var webview1 = document.createElement('webview');
70 var webview2 = document.createElement('webview');
72 // Wait until both webviews finish loading.
73 var loadstopCount = 0;
74 var onLoadStop = function(e) {
75 if (++loadstopCount < 2) {
78 console.log('webview1 and webview2 have loaded.');
80 webview1.executeScript(
81 {file: 'inject_pointer_lock.js'},
83 console.log('Injected script into webview1.');
84 // Establish a communication channel with the webview1's guest.
85 var msg = ['connect'];
86 webview1.contentWindow.postMessage(JSON.stringify(msg), '*');
89 webview1.addEventListener('loadstop', onLoadStop);
90 webview2.addEventListener('loadstop', onLoadStop);
92 window.addEventListener('message', function(e) {
93 var data = JSON.parse(e.data);
94 if (data[0] == 'connected') {
95 console.log('Established communication channel with webview1.');
96 // Once a communication channel has been established, we start the test.
97 var msg = ['start-pointerlock'];
98 webview1.contentWindow.postMessage(JSON.stringify(msg), '*');
102 if (data[0] == 'acquired-pointerlock') {
103 console.log('webview1 has successfully acquired pointer lock.');
105 console.log('webview2 has taken focus.');
109 embedder.test.assertEq('lost-pointerlock', data[0]);
110 console.log('webview1 has lost pointer lock.');
111 embedder.test.succeed();
114 webview1.addEventListener('permissionrequest', function(e) {
115 console.log('webview1 has requested pointer lock.');
117 console.log('webview1 has been granted pointer lock.');
120 webview1.setAttribute('src', embedder.triggerNavUrl);
121 webview2.setAttribute('src', embedder.triggerNavUrl);
122 document.body.appendChild(webview1);
123 document.body.appendChild(webview2);
126 embedder.test.testList = {
127 'testPointerLockLostWithFocus': testPointerLockLostWithFocus,
130 onload = function() {
131 chrome.test.getConfig(function(config) {
132 chrome.test.sendMessage('Launched');