Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / tab_capture / tab_capture_apitest.cc
1 // Copyright (c) 2012 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/basictypes.h"
6 #include "base/command_line.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/tab_helper.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
12 #include "chrome/common/chrome_version_info.h"
13 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/common/switches.h"
17 #include "extensions/test/extension_test_message_listener.h"
18 #include "extensions/test/result_catcher.h"
19
20 #if defined(OS_WIN)
21 #include "base/win/windows_version.h"
22 #endif
23
24 #if defined(OS_WIN) && defined(USE_ASH)
25 #include "chrome/test/base/test_switches.h"
26 #endif
27
28 namespace extensions {
29
30 namespace {
31
32 const char kExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
33
34 class TabCaptureApiTest : public ExtensionApiTest {
35  public:
36   void AddExtensionToCommandLineWhitelist() {
37     CommandLine::ForCurrentProcess()->AppendSwitchASCII(
38         switches::kWhitelistedExtensionID, kExtensionId);
39   }
40 };
41
42 class TabCaptureApiPixelTest : public TabCaptureApiTest {
43  public:
44   void SetUp() override {
45     EnablePixelOutput();
46     TabCaptureApiTest::SetUp();
47   }
48 };
49
50 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTests) {
51 #if defined(OS_WIN) && defined(USE_ASH)
52   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
53   if (CommandLine::ForCurrentProcess()->HasSwitch(::switches::kAshBrowserTests))
54     return;
55 #endif
56
57 #if defined(OS_WIN)
58   // TODO(justinlin): Disabled for WinXP due to timeout issues.
59   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
60     return;
61   }
62 #endif
63
64   AddExtensionToCommandLineWhitelist();
65   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests.html")) << message_;
66 }
67
68 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTestsAudio) {
69 #if defined(OS_WIN)
70   // TODO(justinlin): Disabled for WinXP due to timeout issues.
71   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
72     return;
73   }
74 #endif
75
76   AddExtensionToCommandLineWhitelist();
77   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests_audio.html"))
78       << message_;
79 }
80
81 // Disabled on ChromeOS for http://crbug.com/406051
82 // Disabled on other platforms for http://crbug.com/177163
83 // Disabled http://crbug.com/367349
84 IN_PROC_BROWSER_TEST_F(TabCaptureApiPixelTest, DISABLED_EndToEnd) {
85 #if defined(OS_WIN)
86   // TODO(justinlin): Disabled for WinXP due to timeout issues.
87   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
88     return;
89   }
90 #endif
91   // This test is too slow to succeed with OSMesa on the bots.
92   if (UsingOSMesa())
93     return;
94
95   AddExtensionToCommandLineWhitelist();
96   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "end_to_end.html"))
97       << message_;
98 }
99
100 // http://crbug.com/177163
101 #if defined(OS_WIN) && !defined(NDEBUG)
102 #define MAYBE_GetUserMediaTest DISABLED_GetUserMediaTest
103 #else
104 #define MAYBE_GetUserMediaTest GetUserMediaTest
105 #endif
106 // Test that we can't get tabCapture streams using GetUserMedia directly.
107 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GetUserMediaTest) {
108   ExtensionTestMessageListener listener("ready", true);
109
110   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "get_user_media_test.html"))
111       << message_;
112
113   EXPECT_TRUE(listener.WaitUntilSatisfied());
114
115   content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
116                                 NEW_FOREGROUND_TAB,
117                                 ui::PAGE_TRANSITION_LINK, false);
118   content::WebContents* web_contents = browser()->OpenURL(params);
119
120   content::RenderFrameHost* const main_frame = web_contents->GetMainFrame();
121   ASSERT_TRUE(main_frame);
122   listener.Reply(base::StringPrintf("web-contents-media-stream://%i:%i",
123                                     main_frame->GetProcess()->GetID(),
124                                     main_frame->GetRoutingID()));
125
126   ResultCatcher catcher;
127   catcher.RestrictToBrowserContext(browser()->profile());
128   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
129 }
130
131 // http://crbug.com/177163
132 #if defined(OS_WIN) && !defined(NDEBUG)
133 #define MAYBE_ActiveTabPermission DISABLED_ActiveTabPermission
134 #else
135 #define MAYBE_ActiveTabPermission ActiveTabPermission
136 #endif
137 // Make sure tabCapture.capture only works if the tab has been granted
138 // permission via an extension icon click or the extension is whitelisted.
139 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_ActiveTabPermission) {
140   ExtensionTestMessageListener before_open_tab("ready1", true);
141   ExtensionTestMessageListener before_grant_permission("ready2", true);
142   ExtensionTestMessageListener before_open_new_tab("ready3", true);
143   ExtensionTestMessageListener before_whitelist_extension("ready4", true);
144
145   ASSERT_TRUE(RunExtensionSubtest("tab_capture",
146                                   "active_tab_permission_test.html"))
147       << message_;
148
149   // Open a new tab and make sure capture is denied.
150   EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
151   content::OpenURLParams params(GURL("http://google.com"), content::Referrer(),
152                                 NEW_FOREGROUND_TAB,
153                                 ui::PAGE_TRANSITION_LINK, false);
154   content::WebContents* web_contents = browser()->OpenURL(params);
155   before_open_tab.Reply("");
156
157   // Grant permission and make sure capture succeeds.
158   EXPECT_TRUE(before_grant_permission.WaitUntilSatisfied());
159   const Extension* extension = ExtensionRegistry::Get(
160       web_contents->GetBrowserContext())->enabled_extensions().GetByID(
161           kExtensionId);
162   TabHelper::FromWebContents(web_contents)
163       ->active_tab_permission_granter()->GrantIfRequested(extension);
164   before_grant_permission.Reply("");
165
166   // Open a new tab and make sure capture is denied.
167   EXPECT_TRUE(before_open_new_tab.WaitUntilSatisfied());
168   browser()->OpenURL(params);
169   before_open_new_tab.Reply("");
170
171   // Add extension to whitelist and make sure capture succeeds.
172   EXPECT_TRUE(before_whitelist_extension.WaitUntilSatisfied());
173   AddExtensionToCommandLineWhitelist();
174   before_whitelist_extension.Reply("");
175
176   ResultCatcher catcher;
177   catcher.RestrictToBrowserContext(browser()->profile());
178   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
179 }
180
181 // http://crbug.com/177163
182 #if defined(OS_WIN) && !defined(NDEBUG)
183 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
184 #elif defined(USE_AURA) || defined(OS_MACOSX)
185 // These don't always fire fullscreen events when run in tests. Tested manually.
186 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
187 #elif defined(OS_LINUX)
188 // Flaky to get out of fullscreen in tests. Tested manually.
189 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
190 #else
191 #define MAYBE_FullscreenEvents FullscreenEvents
192 #endif
193 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_FullscreenEvents) {
194 #if defined(OS_WIN)
195   // TODO(justinlin): Disabled for WinXP due to timeout issues.
196   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
197     return;
198   }
199 #endif
200
201   AddExtensionToCommandLineWhitelist();
202
203   content::OpenURLParams params(GURL("chrome://version"),
204                                 content::Referrer(),
205                                 CURRENT_TAB,
206                                 ui::PAGE_TRANSITION_LINK, false);
207   content::WebContents* web_contents = browser()->OpenURL(params);
208
209   ExtensionTestMessageListener listeners_setup("ready1", true);
210   ExtensionTestMessageListener fullscreen_entered("ready2", true);
211
212   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "fullscreen_test.html"))
213       << message_;
214   EXPECT_TRUE(listeners_setup.WaitUntilSatisfied());
215
216   // Toggle fullscreen after setting up listeners.
217   browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
218                                                                  true);
219   listeners_setup.Reply("");
220
221   // Toggle again after JS should have the event.
222   EXPECT_TRUE(fullscreen_entered.WaitUntilSatisfied());
223   browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
224                                                                  false);
225   fullscreen_entered.Reply("");
226
227   ResultCatcher catcher;
228   catcher.RestrictToBrowserContext(browser()->profile());
229   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
230 }
231
232 // Times out on Win dbg bots: http://crbug.com/177163
233 // #if defined(OS_WIN) && !defined(NDEBUG)
234 // Times out on all Win bots, flaky on MSan bots: http://crbug.com/294431
235 #if defined(OS_WIN) || defined(MEMORY_SANITIZER)
236 #define MAYBE_GrantForChromePages DISABLED_GrantForChromePages
237 #else
238 #define MAYBE_GrantForChromePages GrantForChromePages
239 #endif
240 // Make sure tabCapture API can be granted for Chrome:// pages.
241 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GrantForChromePages) {
242   ExtensionTestMessageListener before_open_tab("ready1", true);
243   ASSERT_TRUE(RunExtensionSubtest("tab_capture",
244                                   "active_tab_chrome_pages.html"))
245       << message_;
246   EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
247
248   // Open a tab on a chrome:// page and make sure we can capture.
249   content::OpenURLParams params(GURL("chrome://version"), content::Referrer(),
250                                 NEW_FOREGROUND_TAB,
251                                 ui::PAGE_TRANSITION_LINK, false);
252   content::WebContents* web_contents = browser()->OpenURL(params);
253   const Extension* extension = ExtensionRegistry::Get(
254       web_contents->GetBrowserContext())->enabled_extensions().GetByID(
255           kExtensionId);
256   TabHelper::FromWebContents(web_contents)
257       ->active_tab_permission_granter()->GrantIfRequested(extension);
258   before_open_tab.Reply("");
259
260   ResultCatcher catcher;
261   catcher.RestrictToBrowserContext(browser()->profile());
262   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
263 }
264
265 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_MACOSX)
266 // http://crbug.com/326319
267 #define MAYBE_CaptureInSplitIncognitoMode DISABLED_CaptureInSplitIncognitoMode
268 #else
269 #define MAYBE_CaptureInSplitIncognitoMode CaptureInSplitIncognitoMode
270 #endif
271 // Test that a tab can be captured in split incognito mode.
272 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_CaptureInSplitIncognitoMode) {
273   AddExtensionToCommandLineWhitelist();
274   ASSERT_TRUE(RunExtensionSubtest("tab_capture",
275                                   "incognito.html",
276                                   kFlagEnableIncognito | kFlagUseIncognito))
277       << message_;
278 }
279
280 #if defined(OS_WIN) && !defined(NDEBUG)
281 #define MAYBE_Constraints DISABLED_Constraints
282 #else
283 #define MAYBE_Constraints Constraints
284 #endif
285 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_Constraints) {
286   AddExtensionToCommandLineWhitelist();
287   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "constraints.html"))
288       << message_;
289 }
290
291 }  // namespace
292
293 }  // namespace extensions