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 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 // Disabled http://crbug.com/367349
84 IN_PROC_BROWSER_TEST_F(TabCaptureApiPixelTest, DISABLED_EndToEnd) {
86 // TODO(justinlin): Disabled for WinXP due to timeout issues.
87 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
91 // This test is too slow to succeed with OSMesa on the bots.
95 AddExtensionToCommandLineWhitelist();
96 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "end_to_end.html"))
100 // http://crbug.com/177163
101 #if defined(OS_WIN) && !defined(NDEBUG)
102 #define MAYBE_GetUserMediaTest DISABLED_GetUserMediaTest
104 #define MAYBE_GetUserMediaTest GetUserMediaTest
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);
110 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "get_user_media_test.html"))
113 EXPECT_TRUE(listener.WaitUntilSatisfied());
115 content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
117 ui::PAGE_TRANSITION_LINK, false);
118 content::WebContents* web_contents = browser()->OpenURL(params);
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()));
126 ResultCatcher catcher;
127 catcher.RestrictToBrowserContext(browser()->profile());
128 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
131 // http://crbug.com/177163
132 #if defined(OS_WIN) && !defined(NDEBUG)
133 #define MAYBE_ActiveTabPermission DISABLED_ActiveTabPermission
135 #define MAYBE_ActiveTabPermission ActiveTabPermission
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);
145 ASSERT_TRUE(RunExtensionSubtest("tab_capture",
146 "active_tab_permission_test.html"))
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(),
153 ui::PAGE_TRANSITION_LINK, false);
154 content::WebContents* web_contents = browser()->OpenURL(params);
155 before_open_tab.Reply("");
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(
162 TabHelper::FromWebContents(web_contents)
163 ->active_tab_permission_granter()->GrantIfRequested(extension);
164 before_grant_permission.Reply("");
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("");
171 // Add extension to whitelist and make sure capture succeeds.
172 EXPECT_TRUE(before_whitelist_extension.WaitUntilSatisfied());
173 AddExtensionToCommandLineWhitelist();
174 before_whitelist_extension.Reply("");
176 ResultCatcher catcher;
177 catcher.RestrictToBrowserContext(browser()->profile());
178 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
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
191 #define MAYBE_FullscreenEvents FullscreenEvents
193 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_FullscreenEvents) {
195 // TODO(justinlin): Disabled for WinXP due to timeout issues.
196 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
201 AddExtensionToCommandLineWhitelist();
203 content::OpenURLParams params(GURL("chrome://version"),
206 ui::PAGE_TRANSITION_LINK, false);
207 content::WebContents* web_contents = browser()->OpenURL(params);
209 ExtensionTestMessageListener listeners_setup("ready1", true);
210 ExtensionTestMessageListener fullscreen_entered("ready2", true);
212 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "fullscreen_test.html"))
214 EXPECT_TRUE(listeners_setup.WaitUntilSatisfied());
216 // Toggle fullscreen after setting up listeners.
217 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
219 listeners_setup.Reply("");
221 // Toggle again after JS should have the event.
222 EXPECT_TRUE(fullscreen_entered.WaitUntilSatisfied());
223 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
225 fullscreen_entered.Reply("");
227 ResultCatcher catcher;
228 catcher.RestrictToBrowserContext(browser()->profile());
229 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
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
238 #define MAYBE_GrantForChromePages GrantForChromePages
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"))
246 EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
248 // Open a tab on a chrome:// page and make sure we can capture.
249 content::OpenURLParams params(GURL("chrome://version"), content::Referrer(),
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(
256 TabHelper::FromWebContents(web_contents)
257 ->active_tab_permission_granter()->GrantIfRequested(extension);
258 before_open_tab.Reply("");
260 ResultCatcher catcher;
261 catcher.RestrictToBrowserContext(browser()->profile());
262 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
265 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_MACOSX)
266 // http://crbug.com/326319
267 #define MAYBE_CaptureInSplitIncognitoMode DISABLED_CaptureInSplitIncognitoMode
269 #define MAYBE_CaptureInSplitIncognitoMode CaptureInSplitIncognitoMode
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",
276 kFlagEnableIncognito | kFlagUseIncognito))
280 #if defined(OS_WIN) && !defined(NDEBUG)
281 #define MAYBE_Constraints DISABLED_Constraints
283 #define MAYBE_Constraints Constraints
285 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_Constraints) {
286 AddExtensionToCommandLineWhitelist();
287 ASSERT_TRUE(RunExtensionSubtest("tab_capture", "constraints.html"))
293 } // namespace extensions