| 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 "extensions/browser/guest_view/web_view/web_view_media_access_apitest.h
" |
| 6 |
| 7 #include "base/strings/stringprintf.h" |
| 8 #include "content/public/browser/web_contents_delegate.h" |
| 9 #include "content/public/test/browser_test_utils.h" |
| 10 #include "extensions/test/extension_test_message_listener.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // This class intercepts media access request from the embedder. The request |
| 15 // should be triggered only if the embedder API (from tests) allows the request |
| 16 // in Javascript. |
| 17 // We do not issue the actual media request; the fact that the request reached |
| 18 // embedder's WebContents is good enough for our tests. This is also to make |
| 19 // the test run successfully on trybots. |
| 20 class MockWebContentsDelegate : public content::WebContentsDelegate { |
| 21 public: |
| 22 MockWebContentsDelegate() : requested_(false), checked_(false) {} |
| 23 virtual ~MockWebContentsDelegate() {} |
| 24 |
| 25 virtual void RequestMediaAccessPermission( |
| 26 content::WebContents* web_contents, |
| 27 const content::MediaStreamRequest& request, |
| 28 const content::MediaResponseCallback& callback) OVERRIDE { |
| 29 requested_ = true; |
| 30 if (request_message_loop_runner_.get()) |
| 31 request_message_loop_runner_->Quit(); |
| 32 } |
| 33 |
| 34 virtual bool CheckMediaAccessPermission( |
| 35 content::WebContents* web_contents, |
| 36 const GURL& security_origin, |
| 37 content::MediaStreamType type) OVERRIDE { |
| 38 checked_ = true; |
| 39 if (check_message_loop_runner_.get()) |
| 40 check_message_loop_runner_->Quit(); |
| 41 return true; |
| 42 } |
| 43 |
| 44 void WaitForRequestMediaPermission() { |
| 45 if (requested_) |
| 46 return; |
| 47 request_message_loop_runner_ = new content::MessageLoopRunner; |
| 48 request_message_loop_runner_->Run(); |
| 49 } |
| 50 |
| 51 void WaitForCheckMediaPermission() { |
| 52 if (checked_) |
| 53 return; |
| 54 check_message_loop_runner_ = new content::MessageLoopRunner; |
| 55 check_message_loop_runner_->Run(); |
| 56 } |
| 57 |
| 58 private: |
| 59 bool requested_; |
| 60 bool checked_; |
| 61 scoped_refptr<content::MessageLoopRunner> request_message_loop_runner_; |
| 62 scoped_refptr<content::MessageLoopRunner> check_message_loop_runner_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(MockWebContentsDelegate); |
| 65 }; |
| 66 |
| 67 } // namespace |
| 68 |
| 69 namespace extensions { |
| 70 |
| 71 WebViewMediaAccessAPITest::WebViewMediaAccessAPITest() { |
| 72 } |
| 73 |
| 74 void WebViewMediaAccessAPITest::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()); |
| 81 } |
| 82 |
| 83 void WebViewMediaAccessAPITest::SetUpOnMainThread() { |
| 84 WebViewAPITest::SetUpOnMainThread(); |
| 85 StartTestServer(); |
| 86 } |
| 87 |
| 88 void WebViewMediaAccessAPITest::TearDownOnMainThread() { |
| 89 WebViewAPITest::TearDownOnMainThread(); |
| 90 StopTestServer(); |
| 91 } |
| 92 |
| 93 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllow) { |
| 94 LaunchApp("web_view/media_access/allow"); |
| 95 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 96 embedder_web_contents_->SetDelegate(mock.get()); |
| 97 |
| 98 RunTest("testAllow"); |
| 99 |
| 100 mock->WaitForRequestMediaPermission(); |
| 101 } |
| 102 |
| 103 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowAndThenDeny) { |
| 104 LaunchApp("web_view/media_access/allow"); |
| 105 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 106 embedder_web_contents_->SetDelegate(mock.get()); |
| 107 |
| 108 RunTest("testAllowAndThenDeny"); |
| 109 |
| 110 mock->WaitForRequestMediaPermission(); |
| 111 } |
| 112 |
| 113 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowAsync) { |
| 114 LaunchApp("web_view/media_access/allow"); |
| 115 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 116 embedder_web_contents_->SetDelegate(mock.get()); |
| 117 |
| 118 RunTest("testAllowAsync"); |
| 119 |
| 120 mock->WaitForRequestMediaPermission(); |
| 121 } |
| 122 |
| 123 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestAllowTwice) { |
| 124 LaunchApp("web_view/media_access/allow"); |
| 125 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 126 embedder_web_contents_->SetDelegate(mock.get()); |
| 127 |
| 128 RunTest("testAllowTwice"); |
| 129 |
| 130 mock->WaitForRequestMediaPermission(); |
| 131 } |
| 132 |
| 133 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestCheck) { |
| 134 LaunchApp("web_view/media_access/check"); |
| 135 scoped_ptr<MockWebContentsDelegate> mock(new MockWebContentsDelegate()); |
| 136 embedder_web_contents_->SetDelegate(mock.get()); |
| 137 |
| 138 RunTest("testCheck"); |
| 139 |
| 140 mock->WaitForCheckMediaPermission(); |
| 141 } |
| 142 |
| 143 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDeny) { |
| 144 LaunchApp("web_view/media_access/deny"); |
| 145 RunTest("testDeny"); |
| 146 } |
| 147 |
| 148 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyThenAllowThrows) { |
| 149 LaunchApp("web_view/media_access/deny"); |
| 150 RunTest("testDenyThenAllowThrows"); |
| 151 } |
| 152 |
| 153 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyWithPreventDefault) { |
| 154 LaunchApp("web_view/media_access/deny"); |
| 155 RunTest("testDenyWithPreventDefault"); |
| 156 } |
| 157 |
| 158 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestNoListenersImplyDeny) { |
| 159 LaunchApp("web_view/media_access/deny"); |
| 160 RunTest("testNoListenersImplyDeny"); |
| 161 } |
| 162 |
| 163 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, |
| 164 TestNoPreventDefaultImpliesDeny) { |
| 165 LaunchApp("web_view/media_access/deny"); |
| 166 RunTest("testNoPreventDefaultImpliesDeny"); |
| 167 } |
| 168 |
| 169 } // namespace extensions |
| OLD | NEW |