Upstream version 9.38.198.0
[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 #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"
18
19 #if defined(OS_WIN)
20 #include "base/win/windows_version.h"
21 #endif
22
23 #if defined(OS_WIN) && defined(USE_ASH)
24 #include "chrome/test/base/test_switches.h"
25 #endif
26
27 namespace extensions {
28
29 namespace {
30
31 const char kExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
32
33 class TabCaptureApiTest : public ExtensionApiTest {
34  public:
35   void AddExtensionToCommandLineWhitelist() {
36     CommandLine::ForCurrentProcess()->AppendSwitchASCII(
37         switches::kWhitelistedExtensionID, kExtensionId);
38   }
39 };
40
41 class TabCaptureApiPixelTest : public TabCaptureApiTest {
42  public:
43   virtual void SetUp() OVERRIDE {
44     EnablePixelOutput();
45     TabCaptureApiTest::SetUp();
46   }
47 };
48
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))
53     return;
54 #endif
55
56 #if defined(OS_WIN)
57   // TODO(justinlin): Disabled for WinXP due to timeout issues.
58   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
59     return;
60   }
61 #endif
62
63   AddExtensionToCommandLineWhitelist();
64   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests.html")) << message_;
65 }
66
67 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTestsAudio) {
68 #if defined(OS_WIN)
69   // TODO(justinlin): Disabled for WinXP due to timeout issues.
70   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
71     return;
72   }
73 #endif
74
75   AddExtensionToCommandLineWhitelist();
76   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests_audio.html"))
77       << message_;
78 }
79
80 // http://crbug.com/177163
81 #if !defined(NDEBUG)
82 #define MAYBE_EndToEnd DISABLED_EndToEnd
83 #else
84 #define MAYBE_EndToEnd EndToEnd
85 #endif
86 IN_PROC_BROWSER_TEST_F(TabCaptureApiPixelTest, MAYBE_EndToEnd) {
87 #if defined(OS_WIN)
88   // TODO(justinlin): Disabled for WinXP due to timeout issues.
89   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
90     return;
91   }
92 #endif
93   // This test is too slow to succeed with OSMesa on the bots.
94   if (UsingOSMesa())
95     return;
96
97   AddExtensionToCommandLineWhitelist();
98   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "end_to_end.html"))
99       << message_;
100 }
101
102 // http://crbug.com/177163
103 #if defined(OS_WIN) && !defined(NDEBUG)
104 #define MAYBE_GetUserMediaTest DISABLED_GetUserMediaTest
105 #else
106 #define MAYBE_GetUserMediaTest GetUserMediaTest
107 #endif
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);
111
112   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "get_user_media_test.html"))
113       << message_;
114
115   EXPECT_TRUE(listener.WaitUntilSatisfied());
116
117   content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
118                                 NEW_FOREGROUND_TAB,
119                                 content::PAGE_TRANSITION_LINK, false);
120   content::WebContents* web_contents = browser()->OpenURL(params);
121
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()));
127
128   ResultCatcher catcher;
129   catcher.RestrictToProfile(browser()->profile());
130   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
131 }
132
133 // http://crbug.com/177163
134 #if defined(OS_WIN) && !defined(NDEBUG)
135 #define MAYBE_ActiveTabPermission DISABLED_ActiveTabPermission
136 #else
137 #define MAYBE_ActiveTabPermission ActiveTabPermission
138 #endif
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);
146
147   ASSERT_TRUE(RunExtensionSubtest("tab_capture",
148                                   "active_tab_permission_test.html"))
149       << message_;
150
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(),
154                                 NEW_FOREGROUND_TAB,
155                                 content::PAGE_TRANSITION_LINK, false);
156   content::WebContents* web_contents = browser()->OpenURL(params);
157   before_open_tab.Reply("");
158
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(
163           kExtensionId);
164   TabHelper::FromWebContents(web_contents)
165       ->active_tab_permission_granter()->GrantIfRequested(extension);
166   before_grant_permission.Reply("");
167
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("");
172
173   // Add extension to whitelist and make sure capture succeeds.
174   EXPECT_TRUE(before_whitelist_extension.WaitUntilSatisfied());
175   AddExtensionToCommandLineWhitelist();
176   before_whitelist_extension.Reply("");
177
178   ResultCatcher catcher;
179   catcher.RestrictToProfile(browser()->profile());
180   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
181 }
182
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
192 #else
193 #define MAYBE_FullscreenEvents FullscreenEvents
194 #endif
195 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_FullscreenEvents) {
196 #if defined(OS_WIN)
197   // TODO(justinlin): Disabled for WinXP due to timeout issues.
198   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
199     return;
200   }
201 #endif
202
203   AddExtensionToCommandLineWhitelist();
204
205   content::OpenURLParams params(GURL("chrome://version"),
206                                 content::Referrer(),
207                                 CURRENT_TAB,
208                                 content::PAGE_TRANSITION_LINK, false);
209   content::WebContents* web_contents = browser()->OpenURL(params);
210
211   ExtensionTestMessageListener listeners_setup("ready1", true);
212   ExtensionTestMessageListener fullscreen_entered("ready2", true);
213
214   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "fullscreen_test.html"))
215       << message_;
216   EXPECT_TRUE(listeners_setup.WaitUntilSatisfied());
217
218   // Toggle fullscreen after setting up listeners.
219   browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
220                                                                  true);
221   listeners_setup.Reply("");
222
223   // Toggle again after JS should have the event.
224   EXPECT_TRUE(fullscreen_entered.WaitUntilSatisfied());
225   browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
226                                                                  false);
227   fullscreen_entered.Reply("");
228
229   ResultCatcher catcher;
230   catcher.RestrictToProfile(browser()->profile());
231   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
232 }
233
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
237 #if defined(OS_WIN)
238 #define MAYBE_GrantForChromePages DISABLED_GrantForChromePages
239 #else
240 #define MAYBE_GrantForChromePages GrantForChromePages
241 #endif
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"))
247       << message_;
248   EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
249
250   // Open a tab on a chrome:// page and make sure we can capture.
251   content::OpenURLParams params(GURL("chrome://version"), content::Referrer(),
252                                 NEW_FOREGROUND_TAB,
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(
257           kExtensionId);
258   TabHelper::FromWebContents(web_contents)
259       ->active_tab_permission_granter()->GrantIfRequested(extension);
260   before_open_tab.Reply("");
261
262   ResultCatcher catcher;
263   catcher.RestrictToProfile(browser()->profile());
264   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
265 }
266
267 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_MACOSX)
268 // http://crbug.com/326319
269 #define MAYBE_CaptureInSplitIncognitoMode DISABLED_CaptureInSplitIncognitoMode
270 #else
271 #define MAYBE_CaptureInSplitIncognitoMode CaptureInSplitIncognitoMode
272 #endif
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",
277                                   "incognito.html",
278                                   kFlagEnableIncognito | kFlagUseIncognito))
279       << message_;
280 }
281
282 #if defined(OS_WIN) && !defined(NDEBUG)
283 #define MAYBE_Constraints DISABLED_Constraints
284 #else
285 #define MAYBE_Constraints Constraints
286 #endif
287 IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_Constraints) {
288   AddExtensionToCommandLineWhitelist();
289   ASSERT_TRUE(RunExtensionSubtest("tab_capture", "constraints.html"))
290       << message_;
291 }
292
293 }  // namespace
294
295 }  // namespace extensions