Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / extensions / browser / guest_view / web_view / web_view_media_access_apitest.cc
blob5405aaf6409d4cfd0fbb6452e18e23767a350886
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 #include "base/strings/stringprintf.h"
6 #include "content/public/browser/web_contents_delegate.h"
7 #include "content/public/test/browser_test_utils.h"
8 #include "extensions/browser/guest_view/web_view/web_view_apitest.h"
9 #include "extensions/test/extension_test_message_listener.h"
11 namespace {
13 // This class intercepts media access request from the embedder. The request
14 // should be triggered only if the embedder API (from tests) allows the request
15 // in Javascript.
16 // We do not issue the actual media request; the fact that the request reached
17 // embedder's WebContents is good enough for our tests. This is also to make
18 // the test run successfully on trybots.
19 class MockWebContentsDelegate : public content::WebContentsDelegate {
20 public:
21 MockWebContentsDelegate() : requested_(false), checked_(false) {}
22 ~MockWebContentsDelegate() override {}
24 void RequestMediaAccessPermission(
25 content::WebContents* web_contents,
26 const content::MediaStreamRequest& request,
27 const content::MediaResponseCallback& callback) override {
28 requested_ = true;
29 if (request_message_loop_runner_.get())
30 request_message_loop_runner_->Quit();
33 bool CheckMediaAccessPermission(content::WebContents* web_contents,
34 const GURL& security_origin,
35 content::MediaStreamType type) override {
36 checked_ = true;
37 if (check_message_loop_runner_.get())
38 check_message_loop_runner_->Quit();
39 return true;
42 void WaitForRequestMediaPermission() {
43 if (requested_)
44 return;
45 request_message_loop_runner_ = new content::MessageLoopRunner;
46 request_message_loop_runner_->Run();
49 void WaitForCheckMediaPermission() {
50 if (checked_)
51 return;
52 check_message_loop_runner_ = new content::MessageLoopRunner;
53 check_message_loop_runner_->Run();
56 private:
57 bool requested_;
58 bool checked_;
59 scoped_refptr<content::MessageLoopRunner> request_message_loop_runner_;
60 scoped_refptr<content::MessageLoopRunner> check_message_loop_runner_;
62 DISALLOW_COPY_AND_ASSIGN(MockWebContentsDelegate);
65 } // namespace
67 namespace extensions {
69 class WebViewMediaAccessAPITest : public WebViewAPITest {
70 protected:
71 WebViewMediaAccessAPITest() {}
73 // Runs media_access tests.
74 void RunTest(const std::string& test_name) {
75 ExtensionTestMessageListener test_run_listener("TEST_PASSED", false);
76 test_run_listener.set_failure_message("TEST_FAILED");
77 EXPECT_TRUE(content::ExecuteScript(
78 embedder_web_contents_,
79 base::StringPrintf("runTest('%s');", test_name.c_str())));
80 ASSERT_TRUE(test_run_listener.WaitUntilSatisfied());
83 // content::BrowserTestBase implementation
84 void SetUpOnMainThread() override {
85 WebViewAPITest::SetUpOnMainThread();
86 StartTestServer();
89 void TearDownOnMainThread() override {
90 WebViewAPITest::TearDownOnMainThread();
91 StopTestServer();
95 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllow) {
96 LaunchApp("web_view/media_access/allow");
97 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
98 embedder_web_contents_->SetDelegate(mock.get());
100 RunTest("testAllow");
102 mock->WaitForRequestMediaPermission();
105 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowAndThenDeny) {
106 LaunchApp("web_view/media_access/allow");
107 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
108 embedder_web_contents_->SetDelegate(mock.get());
110 RunTest("testAllowAndThenDeny");
112 mock->WaitForRequestMediaPermission();
115 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowAsync) {
116 LaunchApp("web_view/media_access/allow");
117 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
118 embedder_web_contents_->SetDelegate(mock.get());
120 RunTest("testAllowAsync");
122 mock->WaitForRequestMediaPermission();
125 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowTwice) {
126 LaunchApp("web_view/media_access/allow");
127 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
128 embedder_web_contents_->SetDelegate(mock.get());
130 RunTest("testAllowTwice");
132 mock->WaitForRequestMediaPermission();
135 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestCheck) {
136 LaunchApp("web_view/media_access/check");
137 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate());
138 embedder_web_contents_->SetDelegate(mock.get());
140 RunTest("testCheck");
142 mock->WaitForCheckMediaPermission();
145 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDeny) {
146 LaunchApp("web_view/media_access/deny");
147 RunTest("testDeny");
150 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyThenAllowThrows) {
151 LaunchApp("web_view/media_access/deny");
152 RunTest("testDenyThenAllowThrows");
155 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyWithPreventDefault) {
156 LaunchApp("web_view/media_access/deny");
157 RunTest("testDenyWithPreventDefault");
160 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestNoListenersImplyDeny) {
161 LaunchApp("web_view/media_access/deny");
162 RunTest("testNoListenersImplyDeny");
165 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest,
166 TestNoPreventDefaultImpliesDeny) {
167 LaunchApp("web_view/media_access/deny");
168 RunTest("testNoPreventDefaultImpliesDeny");
171 } // namespace extensions