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.baseGuestURL = '';
8 embedder.guestURL = '';
9 embedder.guestWithLinkURL = '';
10 embedder.newWindowURL = '';
12 window.runTest = function(testName) {
13 if (!embedder.test.testList[testName]) {
14 console.log('Incorrect testName: ' + testName);
20 embedder.test.testList[testName]();
22 // window.* exported functions end.
25 embedder.setUp_ = function(config) {
26 embedder.baseGuestURL = 'http://localhost:' + config.testServer.port;
27 embedder.guestURL = embedder.baseGuestURL +
28 '/extensions/platform_apps/web_view/newwindow' +
30 embedder.newWindowURL = embedder.baseGuestURL +
31 '/extensions/platform_apps/web_view/newwindow' +
33 chrome.test.log('Guest url is: ' + embedder.guestURL);
34 embedder.guestWithLinkURL = embedder.baseGuestURL +
35 '/extensions/platform_apps/web_view/newwindow' +
36 '/guest_with_link.html';
40 embedder.setUpGuest_ = function(partitionName) {
41 document.querySelector('#webview-tag-container').innerHTML =
42 '<webview style="width: 100px; height: 100px;"></webview>';
43 var webview = document.querySelector('webview');
45 webview.partition = partitionName;
48 embedder.test.fail('No <webview> element created');
54 embedder.setUpNewWindowRequest_ = function(webview, url, frameName, testName) {
55 var onWebViewLoadStop = function(e) {
56 // Send post message to <webview> when it's ready to receive them.
57 var redirect = testName.indexOf("_blank") != -1;
58 webview.contentWindow.postMessage(
60 ['open-window', '' + url, '' + frameName, redirect]), '*');
62 webview.addEventListener('loadstop', onWebViewLoadStop);
63 webview.setAttribute('src', embedder.guestURL);
67 embedder.test.succeed = function() {
68 chrome.test.sendMessage('TEST_PASSED');
71 embedder.test.fail = function() {
72 chrome.test.sendMessage('TEST_FAILED');
75 embedder.test.assertEq = function(a, b) {
77 console.log('assertion failed: ' + a + ' != ' + b);
82 embedder.test.assertTrue = function(condition) {
84 console.log('assertion failed: true != ' + condition);
89 embedder.test.assertFalse = function(condition) {
91 console.log('assertion failed: false != ' + condition);
97 embedder.requestFrameName_ =
98 function(webview, openerWebview, testName, expectedFrameName) {
99 var onWebViewLoadStop = function(e) {
100 // Send post message to <webview> when it's ready to receive them.
101 // Note that loadstop will get called twice if the test is opening
102 // a new window via a redirect: one for navigating to about:blank
103 // and another for navigating to the final destination.
104 // about:blank will not respond to the postMessage so it's OK
105 // to send it again on the second loadstop event.
106 webview.contentWindow.postMessage(
107 JSON.stringify(['get-frame-name', testName]), '*');
109 webview.addEventListener('loadstop', onWebViewLoadStop);
110 var onPostMessageReceived = function(e) {
111 var data = JSON.parse(e.data);
112 if (data[0] == 'get-frame-name') {
114 if (testName != name)
116 var frameName = data[2];
117 embedder.test.assertEq(expectedFrameName, frameName);
118 embedder.test.assertEq(expectedFrameName, webview.name);
119 embedder.test.assertEq(openerWebview.partition, webview.partition);
120 window.removeEventListener('message', onPostMessageReceived);
121 embedder.test.succeed();
124 window.addEventListener('message', onPostMessageReceived);
128 embedder.requestClose_ = function(webview, testName) {
129 var onWebViewLoadStop = function(e) {
130 webview.contentWindow.postMessage(
131 JSON.stringify(['close', testName]), '*');
133 webview.addEventListener('loadstop', onWebViewLoadStop);
134 var onWebViewClose = function(e) {
135 webview.removeEventListener('close', onWebViewClose);
136 embedder.test.succeed();
138 webview.addEventListener('close', onWebViewClose);
142 embedder.requestExecuteScript_ =
143 function(webview, script, expectedResult, testName) {
144 var onWebViewLoadStop = function(e) {
145 webview.executeScript(
148 embedder.test.assertEq(1, results.length);
149 embedder.test.assertEq(expectedResult, results[0]);
150 embedder.test.succeed();
153 webview.addEventListener('loadstop', onWebViewLoadStop);
157 embedder.assertCorrectEvent_ = function(e, guestName) {
158 embedder.test.assertEq('newwindow', e.type);
159 embedder.test.assertTrue(!!e.targetUrl);
160 embedder.test.assertEq(guestName, e.name);
165 var testNewWindowName = function(testName,
170 var webview = embedder.setUpGuest_(partitionName);
172 var onNewWindow = function(e) {
173 chrome.test.log('Embedder notified on newwindow');
174 embedder.assertCorrectEvent_(e, guestName);
176 var newwebview = document.createElement('webview');
177 newwebview.setAttribute('name', webViewName);
178 document.querySelector('#webview-tag-container').appendChild(newwebview);
179 embedder.requestFrameName_(
180 newwebview, webview, testName, expectedFrameName);
182 e.window.attach(newwebview);
184 embedder.test.fail();
187 webview.addEventListener('newwindow', onNewWindow);
189 // Load a new window with the given name.
190 embedder.setUpNewWindowRequest_(webview, 'guest.html', guestName, testName);
193 // Loads a guest which requests a new window.
194 function testNewWindowNameTakesPrecedence() {
195 var webViewName = 'foo';
196 var guestName = 'bar';
197 var partitionName = 'foobar';
198 var expectedName = guestName;
199 testNewWindowName('testNewWindowNameTakesPrecedence',
200 webViewName, guestName, partitionName, expectedName);
203 function testWebViewNameTakesPrecedence() {
204 var webViewName = 'foo';
206 var partitionName = 'persist:foobar';
207 var expectedName = webViewName;
208 testNewWindowName('testWebViewNameTakesPrecedence',
209 webViewName, guestName, partitionName, expectedName);
212 function testNoName() {
213 var webViewName = '';
215 var partitionName = '';
216 var expectedName = '';
217 testNewWindowName('testNoName',
218 webViewName, guestName, partitionName, expectedName);
221 // This test exercises the need for queuing events that occur prior to
222 // attachment. In this test a new window is opened that initially navigates to
223 // about:blank and then subsequently redirects to its final destination. This
224 // test responds to loadstop in the new <webview>. Since "about:blank" does not
225 // have any external resources, it loads immediately prior to attachment, and
226 // the <webview> that is eventually attached never gets a chance to see the
227 // event. GuestView solves this problem by queuing events that occur prior to
228 // attachment and firing them immediately after attachment.
229 function testNewWindowRedirect() {
230 var webViewName = 'foo';
232 var partitionName = 'persist:foobar';
233 var expectedName = webViewName;
234 testNewWindowName('testNewWindowRedirect_blank',
235 webViewName, guestName, partitionName, expectedName);
238 function testNewWindowClose() {
239 var testName = 'testNewWindowClose';
240 var webview = embedder.setUpGuest_('foobar');
242 var onNewWindow = function(e) {
243 chrome.test.log('Embedder notified on newwindow');
244 embedder.assertCorrectEvent_(e, '');
246 var newwebview = document.createElement('webview');
247 document.querySelector('#webview-tag-container').appendChild(newwebview);
248 embedder.requestClose_(newwebview, testName);
250 e.window.attach(newwebview);
252 embedder.test.fail();
255 webview.addEventListener('newwindow', onNewWindow);
257 // Load a new window with the given name.
258 embedder.setUpNewWindowRequest_(webview, 'guest.html', '', testName);
261 function testNewWindowExecuteScript() {
262 var testName = 'testNewWindowExecuteScript';
263 var webview = embedder.setUpGuest_('foobar');
265 var onNewWindow = function(e) {
266 chrome.test.log('Embedder notified on newwindow');
267 embedder.assertCorrectEvent_(e, '');
269 var newwebview = document.createElement('webview');
270 document.querySelector('#webview-tag-container').appendChild(newwebview);
271 embedder.requestExecuteScript_(
273 'document.body.style.backgroundColor = "red";',
277 e.window.attach(newwebview);
279 embedder.test.fail();
282 webview.addEventListener('newwindow', onNewWindow);
284 // Load a new window with the given name.
285 embedder.setUpNewWindowRequest_(webview, 'about:blank', '', testName);
288 function testNewWindowOpenInNewTab() {
289 var webview = embedder.setUpGuest_('foobar');
291 webview.addEventListener('loadstop', function(e) {
293 chrome.test.sendMessage('Loaded');
295 webview.addEventListener('newwindow', function(e) {
296 embedder.test.succeed();
298 webview.src = embedder.guestWithLinkURL;
301 function testNewWindowWebRequest() {
302 var testName = 'testNewWindowWebRequest';
303 var webview = embedder.setUpGuest_('foobar');
305 var onNewWindow = function(e) {
306 chrome.test.log('Embedder notified on newwindow');
307 embedder.assertCorrectEvent_(e, '');
309 var newwebview = new WebView();
310 var calledWebRequestEvent = false;
311 newwebview.request.onBeforeRequest.addListener(function(e) {
312 if (!calledWebRequestEvent) {
313 calledWebRequestEvent = true;
314 embedder.test.succeed();
316 }, {urls: ['<all_urls>']});
317 document.querySelector('#webview-tag-container').appendChild(newwebview);
320 e.window.attach(newwebview);
322 embedder.test.fail();
325 webview.addEventListener('newwindow', onNewWindow);
327 // Load a new window with the given name.
328 embedder.setUpNewWindowRequest_(webview, 'guest.html', '', testName);
331 // This test verifies that a WebRequest event listener's lifetime is not
332 // tied to the context in which it was created but instead at least the
333 // lifetime of the embedder window to which it was attached.
334 function testNewWindowWebRequestCloseWindow() {
335 var current = chrome.app.window.current();
336 var requestCount = 0;
337 var dataUrl = 'data:text/html,<body>foobar</body>';
339 var webview = new WebView();
340 webview.request.onBeforeRequest.addListener(function(e) {
341 console.log('url: ' + e.url);
343 if (requestCount == 1) {
344 // Close the existing window.
345 // TODO(fsamuel): This is currently broken and this test is disabled.
346 // Once we close the first window, the context in which the <webview> was
347 // created is gone and so the <webview> is no longer a custom element.
349 // renavigate the webview.
350 webview.src = embedder.newWindowURL;
351 } else if (requestCount == 2) {
352 embedder.test.succeed();
354 }, {urls: ['<all_urls>']});
355 webview.addEventListener('loadcommit', function(e) {
356 console.log('loadcommit: ' + e.url);
358 webview.src = embedder.guestURL;
360 chrome.app.window.create('newwindow.html', {
363 }, function(newwindow) {
364 newwindow.contentWindow.onload = function(evt) {
365 newwindow.contentWindow.document.body.appendChild(webview);
370 function testNewWindowWebRequestRemoveElement() {
371 var testName = 'testNewWindowWebRequestRemoveElement';
372 var dataUrl = 'data:text/html,<body>foobar</body>';
373 var webview = embedder.setUpGuest_('foobar');
374 var calledWebRequestEvent = false;
375 webview.request.onBeforeRequest.addListener(function(e) {
376 if (e.url == embedder.guestURL && calledWebRequestEvent) {
377 embedder.test.succeed();
379 }, {urls: ['<all_urls>']});
381 var onNewWindow = function(e) {
382 chrome.test.log('Embedder notified on newwindow');
383 embedder.assertCorrectEvent_(e, '');
386 chrome.app.window.create('newwindow.html', {
389 }, function(newwindow) {
390 newwindow.contentWindow.onload = function(evt) {
392 newwindow.contentWindow.document.querySelector('webview');
393 newwebview.request.onBeforeRequest.addListener(function(e) {
394 if (!calledWebRequestEvent) {
395 calledWebRequestEvent = true;
396 newwebview.parentElement.removeChild(newwebview);
397 setTimeout(function() {
398 webview.src = embedder.guestURL;
401 }, {urls: ['<all_urls>']});
403 e.window.attach(newwebview);
405 embedder.test.fail();
410 webview.addEventListener('newwindow', onNewWindow);
412 // Load a new window with the given name.
413 embedder.setUpNewWindowRequest_(webview, 'guest.html', '', testName);
416 embedder.test.testList = {
417 'testNewWindowNameTakesPrecedence': testNewWindowNameTakesPrecedence,
418 'testWebViewNameTakesPrecedence': testWebViewNameTakesPrecedence,
419 'testNoName': testNoName,
420 'testNewWindowRedirect': testNewWindowRedirect,
421 'testNewWindowClose': testNewWindowClose,
422 'testNewWindowExecuteScript': testNewWindowExecuteScript,
423 'testNewWindowOpenInNewTab': testNewWindowOpenInNewTab,
424 'testNewWindowWebRequest': testNewWindowWebRequest,
425 'testNewWindowWebRequestCloseWindow': testNewWindowWebRequestCloseWindow,
426 'testNewWindowWebRequestRemoveElement': testNewWindowWebRequestRemoveElement
429 onload = function() {
430 chrome.test.getConfig(function(config) {
431 embedder.setUp_(config);
432 chrome.test.sendMessage('Launched');