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.
5 #include "base/basictypes.h"
6 #include "base/command_line.h"
8 #include "base/mac/mac_util.h"
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/test/base/test_switches.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/common/content_switches.h"
24 #include "extensions/common/feature_switch.h"
25 #include "extensions/common/features/base_feature_provider.h"
26 #include "extensions/common/features/complex_feature.h"
27 #include "extensions/common/features/feature.h"
28 #include "extensions/common/features/simple_feature.h"
29 #include "extensions/common/switches.h"
30 #include "ui/compositor/compositor_switches.h"
34 const char kExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
36 class TabCaptureApiTest : public ExtensionApiTest {
38 void AddExtensionToCommandLineWhitelist() {
39 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
40 extensions::switches::kWhitelistedExtensionID, kExtensionId);
44 class TabCaptureApiPixelTest : public TabCaptureApiTest {
46 virtual void SetUp() OVERRIDE {
48 TabCaptureApiTest::SetUp();
54 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTests) {
55 #if defined(OS_WIN) && defined(USE_ASH)
56 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
57 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
62 // TODO(justinlin): Disabled for WinXP due to timeout issues.
63 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
68 AddExtensionToCommandLineWhitelist();
69 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests.html")) << message_;
72 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTestsAudio) {
74 // TODO(justinlin): Disabled for WinXP due to timeout issues.
75 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
80 AddExtensionToCommandLineWhitelist();
81 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests_audio.html"))
85 // http://crbug.com/177163
87 #define MAYBE_EndToEnd DISABLED_EndToEnd
89 #define MAYBE_EndToEnd EndToEnd
91 IN_PROC_BROWSER_TEST_F(TabCaptureApiPixelTest, MAYBE_EndToEnd) {
93 // TODO(justinlin): Disabled for WinXP due to timeout issues.
94 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
98 // This test is too slow to succeed with OSMesa on the bots.
102 AddExtensionToCommandLineWhitelist();
103 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "end_to_end.html"))
107 // http://crbug.com/177163
108 #if defined(OS_WIN) && !defined(NDEBUG)
109 #define MAYBE_GetUserMediaTest DISABLED_GetUserMediaTest
111 #define MAYBE_GetUserMediaTest GetUserMediaTest
113 // Test that we can't get tabCapture streams using GetUserMedia directly.
114 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GetUserMediaTest) {
115 ExtensionTestMessageListener listener("ready", true);
117 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "get_user_media_test.html"))
120 EXPECT_TRUE(listener.WaitUntilSatisfied());
122 content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
124 content::PAGE_TRANSITION_LINK, false);
125 content::WebContents* web_contents = browser()->OpenURL(params);
127 content::RenderViewHost* const rvh = web_contents->GetRenderViewHost();
128 int render_process_id = rvh->GetProcess()->GetID();
129 int routing_id = rvh->GetRoutingID();
131 listener.Reply(base::StringPrintf("%i:%i", render_process_id, routing_id));
133 ResultCatcher catcher;
134 catcher.RestrictToProfile(browser()->profile());
135 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
138 // http://crbug.com/177163
139 #if defined(OS_WIN) && !defined(NDEBUG)
140 #define MAYBE_ActiveTabPermission DISABLED_ActiveTabPermission
142 #define MAYBE_ActiveTabPermission ActiveTabPermission
144 // Make sure tabCapture.capture only works if the tab has been granted
145 // permission via an extension icon click or the extension is whitelisted.
146 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_ActiveTabPermission) {
147 ExtensionTestMessageListener before_open_tab("ready1", true);
148 ExtensionTestMessageListener before_grant_permission("ready2", true);
149 ExtensionTestMessageListener before_open_new_tab("ready3", true);
150 ExtensionTestMessageListener before_whitelist_extension("ready4", true);
152 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
153 "active_tab_permission_test.html"))
156 // Open a new tab and make sure capture is denied.
157 EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
158 content::OpenURLParams params(GURL("http://google.com"), content::Referrer(),
160 content::PAGE_TRANSITION_LINK, false);
161 content::WebContents* web_contents = browser()->OpenURL(params);
162 before_open_tab.Reply("");
164 // Grant permission and make sure capture succeeds.
165 EXPECT_TRUE(before_grant_permission.WaitUntilSatisfied());
166 ExtensionService* extension_service =
167 Profile::FromBrowserContext(web_contents->GetBrowserContext())
168 ->GetExtensionService();
169 const extensions::Extension* extension =
170 extension_service->GetExtensionById(kExtensionId, false);
171 extensions::TabHelper::FromWebContents(web_contents)
172 ->active_tab_permission_granter()->GrantIfRequested(extension);
173 before_grant_permission.Reply("");
175 // Open a new tab and make sure capture is denied.
176 EXPECT_TRUE(before_open_new_tab.WaitUntilSatisfied());
177 browser()->OpenURL(params);
178 before_open_new_tab.Reply("");
180 // Add extension to whitelist and make sure capture succeeds.
181 EXPECT_TRUE(before_whitelist_extension.WaitUntilSatisfied());
182 AddExtensionToCommandLineWhitelist();
183 before_whitelist_extension.Reply("");
185 ResultCatcher catcher;
186 catcher.RestrictToProfile(browser()->profile());
187 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
190 // http://crbug.com/177163
191 #if defined(OS_WIN) && !defined(NDEBUG)
192 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
193 #elif defined(USE_AURA) || defined(OS_MACOSX)
194 // These don't always fire fullscreen events when run in tests. Tested manually.
195 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
196 #elif defined(OS_LINUX)
197 // Flaky to get out of fullscreen in tests. Tested manually.
198 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
200 #define MAYBE_FullscreenEvents FullscreenEvents
202 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_FullscreenEvents) {
204 // TODO(justinlin): Disabled for WinXP due to timeout issues.
205 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
210 AddExtensionToCommandLineWhitelist();
212 content::OpenURLParams params(GURL("chrome://version"),
215 content::PAGE_TRANSITION_LINK, false);
216 content::WebContents* web_contents = browser()->OpenURL(params);
218 ExtensionTestMessageListener listeners_setup("ready1", true);
219 ExtensionTestMessageListener fullscreen_entered("ready2", true);
221 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "fullscreen_test.html"))
223 EXPECT_TRUE(listeners_setup.WaitUntilSatisfied());
225 // Toggle fullscreen after setting up listeners.
226 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
228 listeners_setup.Reply("");
230 // Toggle again after JS should have the event.
231 EXPECT_TRUE(fullscreen_entered.WaitUntilSatisfied());
232 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
234 fullscreen_entered.Reply("");
236 ResultCatcher catcher;
237 catcher.RestrictToProfile(browser()->profile());
238 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
241 // Times out on Win dbg bots: http://crbug.com/177163
242 // #if defined(OS_WIN) && !defined(NDEBUG)
243 // Times out on all Win bots: http://crbug.com/294431
245 #define MAYBE_GrantForChromePages DISABLED_GrantForChromePages
247 #define MAYBE_GrantForChromePages GrantForChromePages
249 // Make sure tabCapture API can be granted for Chrome:// pages.
250 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GrantForChromePages) {
251 ExtensionTestMessageListener before_open_tab("ready1", true);
252 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
253 "active_tab_chrome_pages.html"))
255 EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
257 // Open a tab on a chrome:// page and make sure we can capture.
258 content::OpenURLParams params(GURL("chrome://version"), content::Referrer(),
260 content::PAGE_TRANSITION_LINK, false);
261 content::WebContents* web_contents = browser()->OpenURL(params);
262 ExtensionService* extension_service =
263 Profile::FromBrowserContext(web_contents->GetBrowserContext())
264 ->GetExtensionService();
265 extensions::TabHelper::FromWebContents(web_contents)
266 ->active_tab_permission_granter()->GrantIfRequested(
267 extension_service->GetExtensionById(kExtensionId, false));
268 before_open_tab.Reply("");
270 ResultCatcher catcher;
271 catcher.RestrictToProfile(browser()->profile());
272 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
275 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_MACOSX)
276 // http://crbug.com/326319
277 #define MAYBE_CaptureInSplitIncognitoMode DISABLED_CaptureInSplitIncognitoMode
279 #define MAYBE_CaptureInSplitIncognitoMode CaptureInSplitIncognitoMode
281 // Test that a tab can be captured in split incognito mode.
282 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_CaptureInSplitIncognitoMode) {
283 AddExtensionToCommandLineWhitelist();
284 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
286 kFlagEnableIncognito | kFlagUseIncognito))
290 #if defined(OS_WIN) && !defined(NDEBUG)
291 #define MAYBE_Constraints DISABLED_Constraints
293 #define MAYBE_Constraints Constraints
295 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_Constraints) {
296 AddExtensionToCommandLineWhitelist();
297 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "constraints.html"))