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 // These variables will be filled in chrome.test.getConfig() below.
8 embedder.baseGuestURL = '';
9 embedder.guestURL = '';
11 embedder.passTest = function() {
12 chrome.test.sendMessage('TEST_PASSED');
15 embedder.failTest = function(opt_msg) {
17 window.console.warn(opt_msg);
19 chrome.test.sendMessage('TEST_FAILED');
22 embedder.assertTrue = function(condition, msg) {
24 window.console.log('embedder assertion failure: ' + msg);
30 embedder.setUpGuest_ = function() {
31 document.querySelector('#webview-tag-container').innerHTML =
32 '<webview style="width: 100px; height: 100px;"' +
33 ' src="' + embedder.guestURL + '"' +
35 var webview = document.querySelector('webview');
36 embedder.assertTrue(webview, 'No <webview> element created');
41 embedder.setUpLoadStop_ = function(webview, testName) {
42 var onWebViewLoadStop = function(e) {
43 // Send post message to <webview> when it's ready to receive them.
44 webview.contentWindow.postMessage(
45 JSON.stringify(['check-media-permission', '' + testName]), '*');
47 webview.addEventListener('loadstop', onWebViewLoadStop);
51 embedder.registerAndWaitForPostMessage_ = function(webview, testName) {
52 var onPostMessageReceived = function(e) {
53 var data = JSON.parse(e.data);
54 if (data[1] == '' + testName) {
55 embedder.assertTrue('access-denied' == data[0],
56 'expected access-denied, found' + data[0]);
60 window.addEventListener('message', onPostMessageReceived);
64 embedder.assertCorrectMediaEvent_ = function(e) {
65 embedder.assertTrue('media' == e.permission, 'permission: ' + e.permission);
66 embedder.assertTrue(!!e.url, 'e.url must be defined');
67 embedder.assertTrue(e.url.indexOf(embedder.baseGuestURL) == 0,
68 'wrong e.url, ' + e.url);
71 // Each test loads a guest which requests media access.
72 // All tests listed in this file expect the guest's request to be denied.
74 // Once the guest is denied media access, the guest notifies the embedder about
75 // the fact via post message.
76 // The embedder has to initiate a post message so that the guest can get a
77 // reference to embedder to send the reply back.
79 embedder.tests.testDeny = function testDeny() {
80 var webview = embedder.setUpGuest_();
82 var onPermissionRequest = function(e) {
83 chrome.test.log('Embedder notified on permissionRequest');
84 embedder.assertCorrectMediaEvent_(e);
87 // Calling allow/deny after this should raise exception.
88 ['allow', 'deny'].forEach(function(funcName) {
89 var exceptionCaught = false;
91 e.request[funcName]();
93 exceptionCaught = true;
95 if (!exceptionCaught) {
96 chrome.failTest('Expected exception on multiple e.allow()');
100 webview.addEventListener('permissionrequest', onPermissionRequest);
102 embedder.setUpLoadStop_(webview, 'test1');
103 embedder.registerAndWaitForPostMessage_(webview, 'test1');
106 embedder.tests.testDenyThenAllowThrows = function testDenyThenAllowThrows() {
107 var webview = embedder.setUpGuest_();
109 var exceptionCount = 0;
111 var denyCalled = false;
112 var callDenyAndCheck = function(e) {
124 if (callCount == 1 && exceptionCount == 1) {
129 var onPermissionRequest1 = function(e) {
130 embedder.assertCorrectMediaEvent_(e);
133 var onPermissionRequest2 = function(e) {
134 embedder.assertCorrectMediaEvent_(e);
137 webview.addEventListener('permissionrequest', onPermissionRequest1);
138 webview.addEventListener('permissionrequest', onPermissionRequest2);
140 embedder.setUpLoadStop_(webview, 'test2');
141 embedder.registerAndWaitForPostMessage_(webview, 'test2');
144 embedder.tests.testDenyWithPreventDefault =
145 function testDenyWithPreventDefault() {
146 var webview = embedder.setUpGuest_();
148 var onPermissionRequest = function(e) {
149 embedder.assertCorrectMediaEvent_(e);
151 // Deny asynchronously.
152 window.setTimeout(function() { e.request.deny(); });
154 webview.addEventListener('permissionrequest', onPermissionRequest);
156 embedder.setUpLoadStop_(webview, 'test3');
157 embedder.registerAndWaitForPostMessage_(webview, 'test3');
160 embedder.tests.testNoListenersImplyDeny = function testNoListenersImplyDeny() {
161 var webview = embedder.setUpGuest_();
162 embedder.setUpLoadStop_(webview, 'test4');
163 embedder.registerAndWaitForPostMessage_(webview, 'test4');
166 embedder.tests.testNoPreventDefaultImpliesDeny =
167 function testNoPreventDefaultImpliesDeny() {
168 var webview = embedder.setUpGuest_();
170 var onPermissionRequest = function(e) {
171 embedder.assertCorrectMediaEvent_(e);
172 window.setTimeout(function() {
173 // Allowing asynchronously. Since we didn't call preventDefault(), the
174 // request will be denied before we get here.
177 } catch (exception) {
182 webview.addEventListener('permissionrequest', onPermissionRequest);
184 embedder.setUpLoadStop_(webview, 'test5');
185 embedder.registerAndWaitForPostMessage_(webview, 'test5');
188 embedder.testNamesToTestMap = {
189 'testDeny': embedder.tests.testDeny,
190 'testDenyThenAllowThrows': embedder.tests.testDenyThenAllowThrows,
191 'testDenyWithPreventDefault': embedder.tests.testDenyWithPreventDefault,
192 'testNoListenersImplyDeny': embedder.tests.testNoListenersImplyDeny,
193 'testNoPreventDefaultImpliesDeny':
194 embedder.tests.testNoPreventDefaultImpliesDeny
197 function runTest(testName) {
198 var testFunction = embedder.testNamesToTestMap[testName];
200 embedder.failTest('No such test: ' + testName);
207 onload = function() {
208 chrome.test.getConfig(function(config) {
209 embedder.baseGuestURL = 'http://localhost:' + config.testServer.port;
210 embedder.guestURL = embedder.baseGuestURL + '/media_access_guest.html';
211 chrome.test.log('Guest url is: ' + embedder.guestURL);
212 chrome.test.sendMessage('LAUNCHED');