Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / guest_view / web_view / web_view_media_access_apitest.cc
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   ~MockWebContentsDelegate() override {}
23
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();
31   }
32
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;
40   }
41
42   void WaitForRequestMediaPermission() {
43     if (requested_)
44       return;
45     request_message_loop_runner_ = new content::MessageLoopRunner;
46     request_message_loop_runner_->Run();
47   }
48
49   void WaitForCheckMediaPermission() {
50     if (checked_)
51       return;
52     check_message_loop_runner_ = new content::MessageLoopRunner;
53     check_message_loop_runner_->Run();
54   }
55
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_;
61
62   DISALLOW_COPY_AND_ASSIGN(MockWebContentsDelegate);
63 };
64
65 }  // namespace
66
67 namespace extensions {
68
69 class WebViewMediaAccessAPITest : public WebViewAPITest {
70  protected:
71   WebViewMediaAccessAPITest() {}
72
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());
81   }
82
83   // content::BrowserTestBase implementation
84   void SetUpOnMainThread() override {
85     WebViewAPITest::SetUpOnMainThread();
86     StartTestServer();
87   }
88
89   void TearDownOnMainThread() override {
90     WebViewAPITest::TearDownOnMainThread();
91     StopTestServer();
92   }
93 };
94
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());
99
100   RunTest("testAllow");
101
102   mock->WaitForRequestMediaPermission();
103 }
104
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());
109
110   RunTest("testAllowAndThenDeny");
111
112   mock->WaitForRequestMediaPermission();
113 }
114
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());
119
120   RunTest("testAllowAsync");
121
122   mock->WaitForRequestMediaPermission();
123 }
124
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());
129
130   RunTest("testAllowTwice");
131
132   mock->WaitForRequestMediaPermission();
133 }
134
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());
139
140   RunTest("testCheck");
141
142   mock->WaitForCheckMediaPermission();
143 }
144
145 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDeny) {
146   LaunchApp("web_view/media_access/deny");
147   RunTest("testDeny");
148 }
149
150 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyThenAllowThrows) {
151   LaunchApp("web_view/media_access/deny");
152   RunTest("testDenyThenAllowThrows");
153 }
154
155 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestDenyWithPreventDefault) {
156   LaunchApp("web_view/media_access/deny");
157   RunTest("testDenyWithPreventDefault");
158 }
159
160 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest, TestNoListenersImplyDeny) {
161   LaunchApp("web_view/media_access/deny");
162   RunTest("testNoListenersImplyDeny");
163 }
164
165 IN_PROC_BROWSER_TEST_F(WebViewMediaAccessAPITest,
166                        TestNoPreventDefaultImpliesDeny) {
167   LaunchApp("web_view/media_access/deny");
168   RunTest("testNoPreventDefaultImpliesDeny");
169 }
170
171 }  // namespace extensions