1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/guest_view/web_view/web_view_apitest.h"
#include "extensions/test/extension_test_message_listener.h"
#include "media/base/media_switches.h"
namespace {
// This class intercepts media access request from the embedder. The request
// should be triggered only if the embedder API (from tests) allows the request
// in Javascript.
// We do not issue the actual media request; the fact that the request reached
// embedder's WebContents is good enough for our tests. This is also to make
// the test run successfully on trybots.
class MockWebContentsDelegate : public content::WebContentsDelegate {
public:
MockWebContentsDelegate() : requested_(false), checked_(false) {}
MockWebContentsDelegate(const MockWebContentsDelegate&) = delete;
MockWebContentsDelegate& operator=(const MockWebContentsDelegate&) = delete;
~MockWebContentsDelegate() override {}
void RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback) override {
requested_ = true;
if (request_message_loop_runner_.get())
request_message_loop_runner_->Quit();
}
bool CheckMediaAccessPermission(content::RenderFrameHost* render_frame_host,
const url::Origin& security_origin,
blink::mojom::MediaStreamType type) override {
checked_ = true;
if (check_message_loop_runner_.get())
check_message_loop_runner_->Quit();
return true;
}
void WaitForRequestMediaPermission() {
if (requested_)
return;
request_message_loop_runner_ = new content::MessageLoopRunner;
request_message_loop_runner_->Run();
}
void WaitForCheckMediaPermission() {
if (checked_)
return;
check_message_loop_runner_ = new content::MessageLoopRunner;
check_message_loop_runner_->Run();
}
private:
bool requested_;
bool checked_;
scoped_refptr<content::MessageLoopRunner> request_message_loop_runner_;
scoped_refptr<content::MessageLoopRunner> check_message_loop_runner_;
};
} // namespace
namespace extensions {
class WebViewMediaAccessAPITest : public WebViewAPITest {
protected:
WebViewMediaAccessAPITest() {}
// Runs media_access tests.
void RunTest(const std::string& test_name) {
ExtensionTestMessageListener test_run_listener("TEST_PASSED");
test_run_listener.set_failure_message("TEST_FAILED");
EXPECT_TRUE(content::ExecJs(
embedder_web_contents_.get(),
base::StringPrintf("runTest('%s');", test_name.c_str())));
ASSERT_TRUE(test_run_listener.WaitUntilSatisfied());
}
void SetUp() override {
WebViewAPITest::SetUp();
// Verify fake devices are enabled. This is necessary to make sure there is
// at least one device in the system. Otherwise, this test would fail on
// machines without physical media devices since getUserMedia fails early in
// those cases.
EXPECT_TRUE(base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseFakeDeviceForMediaStream));
}
};
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllow) {
std::string app_location = "web_view/media_access/allow";
StartTestServer(app_location);
LaunchApp(app_location);
std::unique_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
embedder_web_contents_->SetDelegate(mock.get());
RunTest("testAllow");
mock->WaitForRequestMediaPermission();
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowAndThenDeny) {
std::string app_location = "web_view/media_access/allow";
StartTestServer(app_location);
LaunchApp(app_location);
std::unique_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
embedder_web_contents_->SetDelegate(mock.get());
RunTest("testAllowAndThenDeny");
mock->WaitForRequestMediaPermission();
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowAsync) {
std::string app_location = "web_view/media_access/allow";
StartTestServer(app_location);
LaunchApp(app_location);
std::unique_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
embedder_web_contents_->SetDelegate(mock.get());
RunTest("testAllowAsync");
mock->WaitForRequestMediaPermission();
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowTwice) {
std::string app_location = "web_view/media_access/allow";
StartTestServer(app_location);
LaunchApp(app_location);
std::unique_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
embedder_web_contents_->SetDelegate(mock.get());
RunTest("testAllowTwice");
mock->WaitForRequestMediaPermission();
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestCheck) {
std::string app_location = "web_view/media_access/check";
StartTestServer(app_location);
LaunchApp(app_location);
std::unique_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
embedder_web_contents_->SetDelegate(mock.get());
RunTest("testCheck");
mock->WaitForCheckMediaPermission();
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDeny) {
std::string app_location = "web_view/media_access/deny";
StartTestServer(app_location);
LaunchApp(app_location);
RunTest("testDeny");
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyThenAllowThrows) {
std::string app_location = "web_view/media_access/deny";
StartTestServer(app_location);
LaunchApp(app_location);
RunTest("testDenyThenAllowThrows");
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyWithPreventDefault) {
std::string app_location = "web_view/media_access/deny";
StartTestServer(app_location);
LaunchApp(app_location);
RunTest("testDenyWithPreventDefault");
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestNoListenersImplyDeny) {
std::string app_location = "web_view/media_access/deny";
StartTestServer(app_location);
LaunchApp(app_location);
RunTest("testNoListenersImplyDeny");
StopTestServer();
}
IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest,
TestNoPreventDefaultImpliesDeny) {
std::string app_location = "web_view/media_access/deny";
StartTestServer(app_location);
LaunchApp(app_location);
RunTest("testNoPreventDefaultImpliesDeny");
StopTestServer();
}
} // namespace extensions
|