Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / test / data / web_view / media_access / check / embedder.js
blob18549786cb26808758efcadf8835b195fc5d9067
1 // Copyright 2014 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.
5 var embedder = {};
6 embedder.tests = {};
7 // These variables will be filled in chrome.test.getConfig() below.
8 embedder.baseGuestURL = '';
9 embedder.guestURL = '';
11 // Sends a message to WebViewTest denoting it is done and test
12 // has failed.
13 embedder.failTest = function(msg) {
14 window.console.log('test failure, reason: ' + msg);
15 chrome.test.sendMessage('TEST_FAILED');
18 // Sends a message to WebViewTest denoting it is done and test
19 // has succeeded.
20 embedder.maybePassTest = function() {
21 chrome.test.sendMessage('TEST_PASSED');
24 /** @private */
25 embedder.setUpGuest_ = function() {
26 document.querySelector('#webview-tag-container').innerHTML =
27 '<webview style="width: 100px; height: 100px;"' +
28 ' src="' + embedder.guestURL + '"' +
29 '></webview>';
30 var webview = document.querySelector('webview');
31 if (!webview) {
32 embedder.failTest('No <webview> element created');
33 return null;
35 return webview;
38 /** @private */
39 embedder.setUpLoadStop_ = function(webview) {
40 var onWebViewLoadStop = function(e) {
41 window.console.log('onWebViewLoadStop');
43 // Send post message to <webview> when it's ready to receive them.
44 // This will make the guest start issueing media request. We do not
45 // worry about the Javascript outcome. MockWebContestsDelegate in
46 // WebViewTest will take care of that.
47 webview.contentWindow.postMessage(
48 JSON.stringify(['get-sources-permission']), '*');
50 webview.addEventListener('loadstop', onWebViewLoadStop);
53 // The test loads a guest which requests media sources, which will in turn check
54 // for media access permission.
56 // Note that this is a manually run test, not using chrome.test.runTests.
57 // This is because we want to wait for MockWebContestsDelegate to catch the
58 // media access check and not actually do a check.
60 // Entry point for test, called by WebViewTest.
61 function runTest(testName) {
62 chrome.test.getConfig(function(config) {
63 embedder.baseGuestURL = 'http://localhost:' + config.testServer.port;
64 embedder.guestURL = embedder.baseGuestURL + '/media_check_guest.html';
65 chrome.test.log('Guest url is: ' + embedder.guestURL);
67 var webview = embedder.setUpGuest_();
68 if (!webview) {
69 return;
72 embedder.setUpLoadStop_(webview);
74 webview.addEventListener('consolemessage', function(e) {
75 window.console.log(e.message);
76 });
78 window.addEventListener('message', function(e) {
79 var data = JSON.parse(e.data);
80 if (data[0] == 'got-sources') {
81 embedder.maybePassTest();
82 } else {
83 window.console.log('Unexpected message: ' + e.message);
85 });
86 });
89 onload = function() {
90 chrome.test.sendMessage('LAUNCHED');