Commit ae12db63 authored by grunell's avatar grunell Committed by Commit bot

Add WebView browser test for MediaStreamTrack.getSources and CheckMediaAccessPermisson.

Follow up to https://codereview.chromium.org/562263002/

BUG=406094

Review URL: https://codereview.chromium.org/573183002

Cr-Commit-Position: refs/heads/master@{#297519}
parent 8e22478d
...@@ -167,7 +167,9 @@ void ExecuteScriptWaitForTitle(content::WebContents* web_contents, ...@@ -167,7 +167,9 @@ void ExecuteScriptWaitForTitle(content::WebContents* web_contents,
// the test run successfully on trybots. // the test run successfully on trybots.
class MockWebContentsDelegate : public content::WebContentsDelegate { class MockWebContentsDelegate : public content::WebContentsDelegate {
public: public:
MockWebContentsDelegate() : requested_(false) {} MockWebContentsDelegate()
: requested_(false),
checked_(false) {}
virtual ~MockWebContentsDelegate() {} virtual ~MockWebContentsDelegate() {}
virtual void RequestMediaAccessPermission( virtual void RequestMediaAccessPermission(
...@@ -175,20 +177,39 @@ class MockWebContentsDelegate : public content::WebContentsDelegate { ...@@ -175,20 +177,39 @@ class MockWebContentsDelegate : public content::WebContentsDelegate {
const content::MediaStreamRequest& request, const content::MediaStreamRequest& request,
const content::MediaResponseCallback& callback) OVERRIDE { const content::MediaResponseCallback& callback) OVERRIDE {
requested_ = true; requested_ = true;
if (message_loop_runner_.get()) if (request_message_loop_runner_.get())
message_loop_runner_->Quit(); request_message_loop_runner_->Quit();
} }
void WaitForSetMediaPermission() { virtual bool CheckMediaAccessPermission(
content::WebContents* web_contents,
const GURL& security_origin,
content::MediaStreamType type) OVERRIDE {
checked_ = true;
if (check_message_loop_runner_.get())
check_message_loop_runner_->Quit();
return true;
}
void WaitForRequestMediaPermission() {
if (requested_) if (requested_)
return; return;
message_loop_runner_ = new content::MessageLoopRunner; request_message_loop_runner_ = new content::MessageLoopRunner;
message_loop_runner_->Run(); request_message_loop_runner_->Run();
}
void WaitForCheckMediaPermission() {
if (checked_)
return;
check_message_loop_runner_ = new content::MessageLoopRunner;
check_message_loop_runner_->Run();
} }
private: private:
bool requested_; bool requested_;
scoped_refptr<content::MessageLoopRunner> message_loop_runner_; bool checked_;
scoped_refptr<content::MessageLoopRunner> request_message_loop_runner_;
scoped_refptr<content::MessageLoopRunner> check_message_loop_runner_;
DISALLOW_COPY_AND_ASSIGN(MockWebContentsDelegate); DISALLOW_COPY_AND_ASSIGN(MockWebContentsDelegate);
}; };
...@@ -1765,7 +1786,7 @@ void WebViewTest::MediaAccessAPIAllowTestHelper(const std::string& test_name) { ...@@ -1765,7 +1786,7 @@ void WebViewTest::MediaAccessAPIAllowTestHelper(const std::string& test_name) {
test_name.c_str()))); test_name.c_str())));
ASSERT_TRUE(done_listener.WaitUntilSatisfied()); ASSERT_TRUE(done_listener.WaitUntilSatisfied());
mock->WaitForSetMediaPermission(); mock->WaitForRequestMediaPermission();
} }
IN_PROC_BROWSER_TEST_F(WebViewTest, ContextMenusAPI_Basic) { IN_PROC_BROWSER_TEST_F(WebViewTest, ContextMenusAPI_Basic) {
...@@ -1837,6 +1858,26 @@ IN_PROC_BROWSER_TEST_F(WebViewTest, MediaAccessAPIAllow_TestAllowAsync) { ...@@ -1837,6 +1858,26 @@ IN_PROC_BROWSER_TEST_F(WebViewTest, MediaAccessAPIAllow_TestAllowAsync) {
MediaAccessAPIAllowTestHelper("testAllowAsync"); MediaAccessAPIAllowTestHelper("testAllowAsync");
} }
IN_PROC_BROWSER_TEST_F(WebViewTest, MediaAccessAPIAllow_TestCheck) {
ASSERT_TRUE(StartEmbeddedTestServer()); // For serving guest pages.
LoadAndLaunchPlatformApp("web_view/media_access/check", "Launched");
content::WebContents* embedder_web_contents = GetFirstAppWindowWebContents();
ASSERT_TRUE(embedder_web_contents);
scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
embedder_web_contents->SetDelegate(mock.get());
ExtensionTestMessageListener done_listener("TEST_PASSED", false);
done_listener.set_failure_message("TEST_FAILED");
EXPECT_TRUE(
content::ExecuteScript(
embedder_web_contents,
base::StringPrintf("startCheckTest('')")));
ASSERT_TRUE(done_listener.WaitUntilSatisfied());
mock->WaitForCheckMediaPermission();
}
// Checks that window.screenX/screenY/screenLeft/screenTop works correctly for // Checks that window.screenX/screenY/screenLeft/screenTop works correctly for
// guests. // guests.
IN_PROC_BROWSER_TEST_F(WebViewTest, ScreenCoordinates) { IN_PROC_BROWSER_TEST_F(WebViewTest, ScreenCoordinates) {
......
<!--
* Copyright 2014 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
-->
<html>
<body>
<div id="webview-tag-container"></div>
<script src="embedder.js"></script>
</body>
</html>
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var embedder = {};
embedder.tests = {};
embedder.baseGuestURL = '';
embedder.guestURL = '';
// Sends a message to WebViewTest denoting it is done and test
// has failed.
embedder.failTest = function(msg) {
window.console.log('test failure, reason: ' + msg);
chrome.test.sendMessage('TEST_FAILED');
};
// Sends a message to WebViewTest denoting it is done and test
// has succeeded.
embedder.maybePassTest = function() {
window.console.log('test passed');
chrome.test.sendMessage('TEST_PASSED');
};
/** @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) {
embedder.failTest('No <webview> element created');
return null;
}
return webview;
};
/** @private */
embedder.setUpLoadStop_ = function(webview) {
var onWebViewLoadStop = function(e) {
window.console.log('onWebViewLoadStop');
// Send post message to <webview> when it's ready to receive them.
// This will make the guest start issueing media request. We do not
// worry about the Javascript outcome. MockWebContestsDelegate in
// WebViewTest will take care of that.
webview.contentWindow.postMessage(
JSON.stringify(['get-sources-permission']), '*');
};
webview.addEventListener('loadstop', onWebViewLoadStop);
};
// The test loads a guest which requests media sources, which will in turn check
// for media access permission.
//
// Note that this is a manually run test, not using chrome.test.runTests.
// This is because we want to wait for MockWebContestsDelegate to catch the
// media access check and not actually do a check.
// Entry point for test, called by WebViewTest.
function startCheckTest(testName) {
chrome.test.getConfig(function(config) {
embedder.baseGuestURL = 'http://localhost:' + config.testServer.port;
embedder.guestURL = embedder.baseGuestURL +
'/extensions/platform_apps/web_view/media_access' +
'/media_check_guest.html';
chrome.test.log('Guest url is: ' + embedder.guestURL);
var webview = embedder.setUpGuest_();
if (!webview) {
return;
}
embedder.setUpLoadStop_(webview);
webview.addEventListener('consolemessage', function(e) {
window.console.log(e.message);
});
window.addEventListener('message', function(e) {
var data = JSON.parse(e.data);
if (data[0] == 'got-sources') {
embedder.maybePassTest();
} else {
window.console.log('Unexpected message: ' + e.message);
}
});
});
}
onload = function() {
chrome.test.sendMessage('Launched');
};
{
"name": "Platform App Test: <webview> media",
"description": "Loads a guest which requests media sources, this triggers checking existing permission",
"version": "1",
"permissions": [
"webview"
],
"app": {
"background": {
"scripts": ["test.js"]
}
}
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('embedder.html', {}, function () {});
});
...@@ -45,6 +45,6 @@ ...@@ -45,6 +45,6 @@
</script> </script>
</head> </head>
<body> <body>
<div>This is guest that requests audio media.</div> <div>This is a guest that requests audio media.</div>
</body> </body>
</html> </html>
<!--
* Copyright 2014 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
-->
<html>
<head>
<script type="text/javascript">
// A guest that requests media sources, which in turn checks for media
// access permission.
// Notifies the embedder when done via post message. Note that the
// embedder has to initiate a postMessage first so that guest has a
// reference to the embedder's window.
// The window reference of the embedder to send post message reply.
var embedderWindowChannel = null;
var notifyEmbedder = function(msg) {
embedderWindowChannel.postMessage(msg, '*');
};
var onSourceInfo = function(sources) {
notifyEmbedder(JSON.stringify(['got-sources']));
};
var startTest = function() {
MediaStreamTrack.getSources(onSourceInfo);
};
var onPostMessageReceived = function(e) {
var data = JSON.parse(e.data);
var testName = data[0];
if (testName == 'get-sources-permission') {
embedderWindowChannel = e.source;
// Start the test once we have |embedderWindowChannel|.
startTest();
}
};
window.addEventListener('message', onPostMessageReceived, false);
</script>
</head>
<body>
<div>
This is a guest requests media sources, which will check for media
access permission.
</div>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment