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/extension_test_message_listener.h"
10 #include "chrome/browser/extensions/tab_helper.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
13 #include "chrome/common/chrome_version_info.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/common/switches.h"
20 #include "base/win/windows_version.h"
23 #if defined(OS_WIN) && defined(USE_ASH)
24 #include "chrome/test/base/test_switches.h"
27 namespace extensions {
31 const char kExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
33 class TabCaptureApiTest : public ExtensionApiTest {
35 void AddExtensionToCommandLineWhitelist() {
36 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
37 switches::kWhitelistedExtensionID, kExtensionId);
41 class TabCaptureApiPixelTest : public TabCaptureApiTest {
43 virtual void SetUp() OVERRIDE {
45 TabCaptureApiTest::SetUp();
49 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTests) {
50 #if defined(OS_WIN) && defined(USE_ASH)
51 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
52 if (CommandLine::ForCurrentProcess()->HasSwitch(::switches::kAshBrowserTests))
57 // TODO(justinlin): Disabled for WinXP due to timeout issues.
58 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
63 AddExtensionToCommandLineWhitelist();
64 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests.html")) << message_;
67 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTestsAudio) {
69 // TODO(justinlin): Disabled for WinXP due to timeout issues.
70 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
75 AddExtensionToCommandLineWhitelist();
76 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests_audio.html"))
80 // http://crbug.com/177163
82 #define MAYBE_EndToEnd DISABLED_EndToEnd
84 #define MAYBE_EndToEnd EndToEnd
86 IN_PROC_BROWSER_TEST_F(TabCaptureApiPixelTest, MAYBE_EndToEnd) {
88 // TODO(justinlin): Disabled for WinXP due to timeout issues.
89 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
93 // This test is too slow to succeed with OSMesa on the bots.
97 AddExtensionToCommandLineWhitelist();
98 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "end_to_end.html"))
102 // http://crbug.com/177163
103 #if defined(OS_WIN) && !defined(NDEBUG)
104 #define MAYBE_GetUserMediaTest DISABLED_GetUserMediaTest
106 #define MAYBE_GetUserMediaTest GetUserMediaTest
108 // Test that we can't get tabCapture streams using GetUserMedia directly.
109 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GetUserMediaTest) {
110 ExtensionTestMessageListener listener("ready", true);
112 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "get_user_media_test.html"))
115 EXPECT_TRUE(listener.WaitUntilSatisfied());
117 content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
119 content::PAGE_TRANSITION_LINK, false);
120 content::WebContents* web_contents = browser()->OpenURL(params);
122 content::RenderFrameHost* const main_frame = web_contents->GetMainFrame();
123 ASSERT_TRUE(main_frame);
124 listener.Reply(base::StringPrintf("web-contents-media-stream://%i:%i",
125 main_frame->GetProcess()->GetID(),
126 main_frame->GetRoutingID()));
128 ResultCatcher catcher;
129 catcher.RestrictToProfile(browser()->profile());
130 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
133 // http://crbug.com/177163
134 #if defined(OS_WIN) && !defined(NDEBUG)
135 #define MAYBE_ActiveTabPermission DISABLED_ActiveTabPermission
137 #define MAYBE_ActiveTabPermission ActiveTabPermission
139 // Make sure tabCapture.capture only works if the tab has been granted
140 // permission via an extension icon click or the extension is whitelisted.
141 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_ActiveTabPermission) {
142 ExtensionTestMessageListener before_open_tab("ready1", true);
143 ExtensionTestMessageListener before_grant_permission("ready2", true);
144 ExtensionTestMessageListener before_open_new_tab("ready3", true);
145 ExtensionTestMessageListener before_whitelist_extension("ready4", true);
147 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
148 "active_tab_permission_test.html"))
151 // Open a new tab and make sure capture is denied.
152 EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
153 content::OpenURLParams params(GURL("http://google.com"), content::Referrer(),
155 content::PAGE_TRANSITION_LINK, false);
156 content::WebContents* web_contents = browser()->OpenURL(params);
157 before_open_tab.Reply("");
159 // Grant permission and make sure capture succeeds.
160 EXPECT_TRUE(before_grant_permission.WaitUntilSatisfied());
161 const Extension* extension = ExtensionRegistry::Get(
162 web_contents->GetBrowserContext())->enabled_extensions().GetByID(
164 TabHelper::FromWebContents(web_contents)
165 ->active_tab_permission_granter()->GrantIfRequested(extension);
166 before_grant_permission.Reply("");
168 // Open a new tab and make sure capture is denied.
169 EXPECT_TRUE(before_open_new_tab.WaitUntilSatisfied());
170 browser()->OpenURL(params);
171 before_open_new_tab.Reply("");
173 // Add extension to whitelist and make sure capture succeeds.
174 EXPECT_TRUE(before_whitelist_extension.WaitUntilSatisfied());
175 AddExtensionToCommandLineWhitelist();
176 before_whitelist_extension.Reply("");
178 ResultCatcher catcher;
179 catcher.RestrictToProfile(browser()->profile());
180 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
183 // http://crbug.com/177163
184 #if defined(OS_WIN) && !defined(NDEBUG)
185 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
186 #elif defined(USE_AURA) || defined(OS_MACOSX)
187 // These don't always fire fullscreen events when run in tests. Tested manually.
188 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
189 #elif defined(OS_LINUX)
190 // Flaky to get out of fullscreen in tests. Tested manually.
191 #define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
193 #define MAYBE_FullscreenEvents FullscreenEvents
195 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_FullscreenEvents) {
197 // TODO(justinlin): Disabled for WinXP due to timeout issues.
198 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
203 AddExtensionToCommandLineWhitelist();
205 content::OpenURLParams params(GURL("chrome://version"),
208 content::PAGE_TRANSITION_LINK, false);
209 content::WebContents* web_contents = browser()->OpenURL(params);
211 ExtensionTestMessageListener listeners_setup("ready1", true);
212 ExtensionTestMessageListener fullscreen_entered("ready2", true);
214 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "fullscreen_test.html"))
216 EXPECT_TRUE(listeners_setup.WaitUntilSatisfied());
218 // Toggle fullscreen after setting up listeners.
219 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
221 listeners_setup.Reply("");
223 // Toggle again after JS should have the event.
224 EXPECT_TRUE(fullscreen_entered.WaitUntilSatisfied());
225 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
227 fullscreen_entered.Reply("");
229 ResultCatcher catcher;
230 catcher.RestrictToProfile(browser()->profile());
231 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
234 // Times out on Win dbg bots: http://crbug.com/177163
235 // #if defined(OS_WIN) && !defined(NDEBUG)
236 // Times out on all Win bots: http://crbug.com/294431
238 #define MAYBE_GrantForChromePages DISABLED_GrantForChromePages
240 #define MAYBE_GrantForChromePages GrantForChromePages
242 // Make sure tabCapture API can be granted for Chrome:// pages.
243 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GrantForChromePages) {
244 ExtensionTestMessageListener before_open_tab("ready1", true);
245 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
246 "active_tab_chrome_pages.html"))
248 EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
250 // Open a tab on a chrome:// page and make sure we can capture.
251 content::OpenURLParams params(GURL("chrome://version"), content::Referrer(),
253 content::PAGE_TRANSITION_LINK, false);
254 content::WebContents* web_contents = browser()->OpenURL(params);
255 const Extension* extension = ExtensionRegistry::Get(
256 web_contents->GetBrowserContext())->enabled_extensions().GetByID(
258 TabHelper::FromWebContents(web_contents)
259 ->active_tab_permission_granter()->GrantIfRequested(extension);
260 before_open_tab.Reply("");
262 ResultCatcher catcher;
263 catcher.RestrictToProfile(browser()->profile());
264 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
267 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_MACOSX)
268 // http://crbug.com/326319
269 #define MAYBE_CaptureInSplitIncognitoMode DISABLED_CaptureInSplitIncognitoMode
271 #define MAYBE_CaptureInSplitIncognitoMode CaptureInSplitIncognitoMode
273 // Test that a tab can be captured in split incognito mode.
274 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_CaptureInSplitIncognitoMode) {
275 AddExtensionToCommandLineWhitelist();
276 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
278 kFlagEnableIncognito | kFlagUseIncognito))
282 #if defined(OS_WIN) && !defined(NDEBUG)
283 #define MAYBE_Constraints DISABLED_Constraints
285 #define MAYBE_Constraints Constraints
287 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_Constraints) {
288 AddExtensionToCommandLineWhitelist();
289 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "constraints.html"))
295 } // namespace extensions