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