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"
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"
21 #include "base/win/windows_version.h"
24 #if defined(OS_WIN) && defined(USE_ASH)
25 #include "chrome/test/base/test_switches.h"
28 namespace extensions {
32 const char kExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
34 class TabCaptureApiTest : public ExtensionApiTest {
36 void AddExtensionToCommandLineWhitelist() {
37 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
38 switches::kWhitelistedExtensionID, kExtensionId);
42 class TabCaptureApiPixelTest : public TabCaptureApiTest {
44 virtual void SetUp() OVERRIDE {
46 TabCaptureApiTest::SetUp();
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))
58 // TODO(justinlin): Disabled for WinXP due to timeout issues.
59 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
64 AddExtensionToCommandLineWhitelist();
65 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests.html")) << message_;
68 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTestsAudio) {
70 // TODO(justinlin): Disabled for WinXP due to timeout issues.
71 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
76 AddExtensionToCommandLineWhitelist();
77 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests_audio.html"))
81 // Disabled on ChromeOS for http://crbug.com/406051
82 // Disabled on other platforms for http://crbug.com/177163
83 #if !defined(NDEBUG) || defined(OS_CHROMEOS)
84 #define MAYBE_EndToEnd DISABLED_EndToEnd
86 #define MAYBE_EndToEnd EndToEnd
88 IN_PROC_BROWSER_TEST_F(TabCaptureApiPixelTest, MAYBE_EndToEnd) {
90 // TODO(justinlin): Disabled for WinXP due to timeout issues.
91 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
95 // This test is too slow to succeed with OSMesa on the bots.
99 AddExtensionToCommandLineWhitelist();
100 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "end_to_end.html"))
104 // http://crbug.com/177163
105 #if defined(OS_WIN) && !defined(NDEBUG)
106 #define MAYBE_GetUserMediaTest DISABLED_GetUserMediaTest
108 #define MAYBE_GetUserMediaTest GetUserMediaTest
110 // Test that we can't get tabCapture streams using GetUserMedia directly.
111 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GetUserMediaTest) {
112 ExtensionTestMessageListener listener("ready", true);
114 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "get_user_media_test.html"))
117 EXPECT_TRUE(listener.WaitUntilSatisfied());
119 content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
121 ui::PAGE_TRANSITION_LINK, false);
122 content::WebContents* web_contents = browser()->OpenURL(params);
124 content::RenderFrameHost* const main_frame = web_contents->GetMainFrame();
125 ASSERT_TRUE(main_frame);
126 listener.Reply(base::StringPrintf("web-contents-media-stream://%i:%i",
127 main_frame->GetProcess()->GetID(),
128 main_frame->GetRoutingID()));
130 ResultCatcher catcher;
131 catcher.RestrictToBrowserContext(browser()->profile());
132 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
135 // http://crbug.com/177163
136 #if defined(OS_WIN) && !defined(NDEBUG)
137 #define MAYBE_ActiveTabPermission DISABLED_ActiveTabPermission
139 #define MAYBE_ActiveTabPermission ActiveTabPermission
141 // Make sure tabCapture.capture only works if the tab has been granted
142 // permission via an extension icon click or the extension is whitelisted.
143 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_ActiveTabPermission) {
144 ExtensionTestMessageListener before_open_tab("ready1", true);
145 ExtensionTestMessageListener before_grant_permission("ready2", true);
146 ExtensionTestMessageListener before_open_new_tab("ready3", true);
147 ExtensionTestMessageListener before_whitelist_extension("ready4", true);
149 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
150 "active_tab_permission_test.html"))
153 // Open a new tab and make sure capture is denied.
154 EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
155 content::OpenURLParams params(GURL("http://google.com"), content::Referrer(),
157 ui::PAGE_TRANSITION_LINK, false);
158 content::WebContents* web_contents = browser()->OpenURL(params);
159 before_open_tab.Reply("");
161 // Grant permission and make sure capture succeeds.
162 EXPECT_TRUE(before_grant_permission.WaitUntilSatisfied());
163 const Extension* extension = ExtensionRegistry::Get(
164 web_contents->GetBrowserContext())->enabled_extensions().GetByID(
166 TabHelper::FromWebContents(web_contents)
167 ->active_tab_permission_granter()->GrantIfRequested(extension);
168 before_grant_permission.Reply("");
170 // Open a new tab and make sure capture is denied.
171 EXPECT_TRUE(before_open_new_tab.WaitUntilSatisfied());
172 browser()->OpenURL(params);
173 before_open_new_tab.Reply("");
175 // Add extension to whitelist and make sure capture succeeds.
176 EXPECT_TRUE(before_whitelist_extension.WaitUntilSatisfied());
177 AddExtensionToCommandLineWhitelist();
178 before_whitelist_extension.Reply("");
180 ResultCatcher catcher;
181 catcher.RestrictToBrowserContext(browser()->profile());
182 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
185 // http://crbug.com/177163
186 #if defined(OS_WIN) && !defined(NDEBUG)
187 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
188 #elif defined(USE_AURA) || defined(OS_MACOSX)
189 // These don't always fire fullscreen events when run in tests. Tested manually.
190 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
191 #elif defined(OS_LINUX)
192 // Flaky to get out of fullscreen in tests. Tested manually.
193 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
195 #define MAYBE_FullscreenEvents FullscreenEvents
197 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_FullscreenEvents) {
199 // TODO(justinlin): Disabled for WinXP due to timeout issues.
200 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
205 AddExtensionToCommandLineWhitelist();
207 content::OpenURLParams params(GURL("chrome://version"),
210 ui::PAGE_TRANSITION_LINK, false);
211 content::WebContents* web_contents = browser()->OpenURL(params);
213 ExtensionTestMessageListener listeners_setup("ready1", true);
214 ExtensionTestMessageListener fullscreen_entered("ready2", true);
216 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "fullscreen_test.html"))
218 EXPECT_TRUE(listeners_setup.WaitUntilSatisfied());
220 // Toggle fullscreen after setting up listeners.
221 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
223 listeners_setup.Reply("");
225 // Toggle again after JS should have the event.
226 EXPECT_TRUE(fullscreen_entered.WaitUntilSatisfied());
227 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
229 fullscreen_entered.Reply("");
231 ResultCatcher catcher;
232 catcher.RestrictToBrowserContext(browser()->profile());
233 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
236 // Times out on Win dbg bots: http://crbug.com/177163
237 // #if defined(OS_WIN) && !defined(NDEBUG)
238 // Times out on all Win bots, flaky on MSan bots: http://crbug.com/294431
239 #if defined(OS_WIN) || defined(MEMORY_SANITIZER)
240 #define MAYBE_GrantForChromePages DISABLED_GrantForChromePages
242 #define MAYBE_GrantForChromePages GrantForChromePages
244 // Make sure tabCapture API can be granted for Chrome:// pages.
245 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GrantForChromePages) {
246 ExtensionTestMessageListener before_open_tab("ready1", true);
247 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
248 "active_tab_chrome_pages.html"))
250 EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
252 // Open a tab on a chrome:// page and make sure we can capture.
253 content::OpenURLParams params(GURL("chrome://version"), content::Referrer(),
255 ui::PAGE_TRANSITION_LINK, false);
256 content::WebContents* web_contents = browser()->OpenURL(params);
257 const Extension* extension = ExtensionRegistry::Get(
258 web_contents->GetBrowserContext())->enabled_extensions().GetByID(
260 TabHelper::FromWebContents(web_contents)
261 ->active_tab_permission_granter()->GrantIfRequested(extension);
262 before_open_tab.Reply("");
264 ResultCatcher catcher;
265 catcher.RestrictToBrowserContext(browser()->profile());
266 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
269 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_MACOSX)
270 // http://crbug.com/326319
271 #define MAYBE_CaptureInSplitIncognitoMode DISABLED_CaptureInSplitIncognitoMode
273 #define MAYBE_CaptureInSplitIncognitoMode CaptureInSplitIncognitoMode
275 // Test that a tab can be captured in split incognito mode.
276 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_CaptureInSplitIncognitoMode) {
277 AddExtensionToCommandLineWhitelist();
278 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
280 kFlagEnableIncognito | kFlagUseIncognito))
284 #if defined(OS_WIN) && !defined(NDEBUG)
285 #define MAYBE_Constraints DISABLED_Constraints
287 #define MAYBE_Constraints Constraints
289 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_Constraints) {
290 AddExtensionToCommandLineWhitelist();
291 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "constraints.html"))
297 } // namespace extensions