| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 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" |
| 10 |
| 11 namespace { |
| 12 |
| 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 virtual ~MockWebContentsDelegate() {} |
| 23 |
| 24 virtual 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(); |
| 31 } |
| 32 |
| 33 virtual bool CheckMediaAccessPermission( |
| 34 content::WebContents* web_contents, |
| 35 const GURL& security_origin, |
| 36 content::MediaStreamType type) override { |
| 37 checked_ = true; |
| 38 if (check_message_loop_runner_.get()) |
| 39 check_message_loop_runner_->Quit(); |
| 40 return true; |
| 41 } |
| 42 |
| 43 void WaitForRequestMediaPermission() { |
| 44 if (requested_) |
| 45 return; |
| 46 request_message_loop_runner_ = new content::MessageLoopRunner; |
| 47 request_message_loop_runner_->Run(); |
| 48 } |
| 49 |
| 50 void WaitForCheckMediaPermission() { |
| 51 if (checked_) |
| 52 return; |
| 53 check_message_loop_runner_ = new content::MessageLoopRunner; |
| 54 check_message_loop_runner_->Run(); |
| 55 } |
| 56 |
| 57 private: |
| 58 bool requested_; |
| 59 bool checked_; |
| 60 scoped_refptr<content::MessageLoopRunner> request_message_loop_runner_; |
| 61 scoped_refptr<content::MessageLoopRunner> check_message_loop_runner_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(MockWebContentsDelegate); |
| 64 }; |
| 65 |
| 66 } // namespace |
| 67 |
| 68 namespace extensions { |
| 69 |
| 70 class WebViewMediaAccessAPITest : public WebViewAPITest { |
| 71 protected: |
| 72 WebViewMediaAccessAPITest() {} |
| 73 |
| 74 // Runs media_access tests. |
| 75 void RunTest(const std::string& test_name) { |
| 76 ExtensionTestMessageListener test_run_listener("TEST_PASSED", false); |
| 77 test_run_listener.set_failure_message("TEST_FAILED"); |
| 78 EXPECT_TRUE(content::ExecuteScript( |
| 79 embedder_web_contents_, |
| 80 base::StringPrintf("runTest('%s');", test_name.c_str()))); |
| 81 ASSERT_TRUE(test_run_listener.WaitUntilSatisfied()); |
| 82 } |
| 83 |
| 84 // content::BrowserTestBase implementation |
| 85 virtual void SetUpOnMainThread() override { |
| 86 WebViewAPITest::SetUpOnMainThread(); |
| 87 StartTestServer(); |
| 88 } |
| 89 |
| 90 virtual void TearDownOnMainThread() override { |
| 91 WebViewAPITest::TearDownOnMainThread(); |
| 92 StopTestServer(); |
| 93 } |
| 94 }; |
| 95 |
| 96 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllow) { |
| 97 LaunchApp("web_view/media_access/allow"); |
| 98 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 99 embedder_web_contents_->SetDelegate(mock.get()); |
| 100 |
| 101 RunTest("testAllow"); |
| 102 |
| 103 mock->WaitForRequestMediaPermission(); |
| 104 } |
| 105 |
| 106 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowAndThenDeny) { |
| 107 LaunchApp("web_view/media_access/allow"); |
| 108 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 109 embedder_web_contents_->SetDelegate(mock.get()); |
| 110 |
| 111 RunTest("testAllowAndThenDeny"); |
| 112 |
| 113 mock->WaitForRequestMediaPermission(); |
| 114 } |
| 115 |
| 116 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowAsync) { |
| 117 LaunchApp("web_view/media_access/allow"); |
| 118 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 119 embedder_web_contents_->SetDelegate(mock.get()); |
| 120 |
| 121 RunTest("testAllowAsync"); |
| 122 |
| 123 mock->WaitForRequestMediaPermission(); |
| 124 } |
| 125 |
| 126 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowTwice) { |
| 127 LaunchApp("web_view/media_access/allow"); |
| 128 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 129 embedder_web_contents_->SetDelegate(mock.get()); |
| 130 |
| 131 RunTest("testAllowTwice"); |
| 132 |
| 133 mock->WaitForRequestMediaPermission(); |
| 134 } |
| 135 |
| 136 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestCheck) { |
| 137 LaunchApp("web_view/media_access/check"); |
| 138 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 139 embedder_web_contents_->SetDelegate(mock.get()); |
| 140 |
| 141 RunTest("testCheck"); |
| 142 |
| 143 mock->WaitForCheckMediaPermission(); |
| 144 } |
| 145 |
| 146 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDeny) { |
| 147 LaunchApp("web_view/media_access/deny"); |
| 148 RunTest("testDeny"); |
| 149 } |
| 150 |
| 151 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyThenAllowThrows) { |
| 152 LaunchApp("web_view/media_access/deny"); |
| 153 RunTest("testDenyThenAllowThrows"); |
| 154 } |
| 155 |
| 156 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyWithPreventDefault) { |
| 157 LaunchApp("web_view/media_access/deny"); |
| 158 RunTest("testDenyWithPreventDefault"); |
| 159 } |
| 160 |
| 161 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestNoListenersImplyDeny) { |
| 162 LaunchApp("web_view/media_access/deny"); |
| 163 RunTest("testNoListenersImplyDeny"); |
| 164 } |
| 165 |
| 166 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, |
| 167 TestNoPreventDefaultImpliesDeny) { |
| 168 LaunchApp("web_view/media_access/deny"); |
| 169 RunTest("testNoPreventDefaultImpliesDeny"); |
| 170 } |
| 171 |
| 172 } // namespace extensions |
| OLD | NEW |