// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var embedder = {};
embedder.test = {};
embedder.baseGuestURL = '';
embedder.guestURL = '';
window.runTest = function(testName) {
if (!embedder.test.testList[testName]) {
console.log('Incorrect testName: ' + testName);
embedder.test.fail();
return;
}
// Run the test.
embedder.test.testList[testName]();
};
// window.* exported functions end.
embedder.test.succeed = function() {
chrome.test.sendMessage('TEST_PASSED');
};
embedder.test.fail = function() {
chrome.test.sendMessage('TEST_FAILED');
};
embedder.test.assertEq = function(a, b) {
if (a != b) {
console.log('assertion failed: ' + a + ' != ' + b);
embedder.test.fail();
}
};
embedder.test.assertTrue = function(condition) {
if (!condition) {
console.log('assertion failed: true != ' + condition);
embedder.test.fail();
}
};
embedder.test.assertFalse = function(condition) {
if (condition) {
console.log('assertion failed: false != ' + condition);
embedder.test.fail();
}
};
embedder.setUp = function(config) {
embedder.baseGuestURL = 'http://localhost:' + config.testServer.port;
embedder.guestURL = embedder.baseGuestURL +
'/extensions/platform_apps/web_view/geolocation' +
'/geolocation_access_guest.html';
chrome.test.log('Guest url is: ' + embedder.guestURL);
};
/** @private */
embedder.setUpGuest_ = function() {
document.querySelector('#webview-tag-container').innerHTML =
'<webview style="width: 100px; height: 100px;"' +
' src="' + embedder.guestURL + '"' +
'></webview>';
var webview = document.querySelector('webview');
if (!webview) {
console.log('No <webview> element created');
embedder.test.fail();
}
return webview;
};
/** @private */
embedder.setUpLoadStop_ = function(webview, testName) {
var onWebViewLoadStop = function(e) {
// Send post message to <webview> when it's ready to receive them.
webview.contentWindow.postMessage(
JSON.stringify(['check-geolocation-permission', '' + testName]), '*');
};
webview.addEventListener('loadstop', onWebViewLoadStop);
};
/** @private */
embedder.registerAndWaitForPostMessage_ = function(webview, expectedData) {
var testName = expectedData[0];
var onPostMessageReceived = function(e) {
var data = JSON.parse(e.data);
if (data[0] == '' + testName) {
embedder.test.assertEq(expectedData[1], data[1]);
embedder.test.succeed();
}
};
window.addEventListener('message', onPostMessageReceived);
};
/** @private */
embedder.assertCorrectEvent_ = function(e) {
embedder.test.assertEq('geolocation', e.permission);
embedder.test.assertTrue(!!e.url);
embedder.test.assertTrue(e.url.indexOf(embedder.baseGuestURL) == 0);
};
// Tests begin.
// Embedder does not have geolocation permission, so geolocation access is
// always denied for these tests.
// Calling deny() results in deny.
function testDenyDenies() {
var webview = embedder.setUpGuest_();
var onPermissionRequest = function(e) {
chrome.test.log('Embedder notified on permissionRequest');
embedder.assertCorrectEvent_(e);
e.request.deny();
};
webview.addEventListener('permissionrequest', onPermissionRequest);
embedder.setUpLoadStop_(webview, 'test1');
embedder.registerAndWaitForPostMessage_(webview, ['test1', 'access-denied']);
}
// Calling allow() results in deny too.
function testAllowDenies() {
var webview = embedder.setUpGuest_();
var onPermissionRequest = function(e) {
chrome.test.log('Embedder notified on permissionRequest');
embedder.assertCorrectEvent_(e);
e.request.allow();
};
webview.addEventListener('permissionrequest', onPermissionRequest);
embedder.setUpLoadStop_(webview, 'test2');
embedder.registerAndWaitForPostMessage_(webview, ['test2', 'access-denied']);
}
embedder.test.testList = {
'testDenyDenies': testDenyDenies,
'testAllowDenies': testAllowDenies
};
onload = function() {
chrome.test.getConfig(function(config) {
embedder.setUp(config);
chrome.test.sendMessage('Launched');
});
};