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