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#if defined(OS_MACOSX)
8#include "base/mac/mac_util.h"
9#endif
10#include "base/strings/stringprintf.h"
11#include "base/win/windows_version.h"
12#include "chrome/browser/extensions/extension_apitest.h"
13#include "chrome/browser/extensions/extension_service.h"
14#include "chrome/browser/extensions/extension_test_message_listener.h"
15#include "chrome/browser/extensions/tab_helper.h"
16#include "chrome/browser/profiles/profile.h"
17#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
18#include "chrome/common/chrome_switches.h"
19#include "chrome/common/chrome_version_info.h"
20#include "chrome/test/base/test_switches.h"
21#include "content/public/browser/render_process_host.h"
22#include "content/public/browser/render_view_host.h"
23#include "content/public/common/content_switches.h"
24#include "extensions/common/feature_switch.h"
25#include "extensions/common/features/base_feature_provider.h"
26#include "extensions/common/features/complex_feature.h"
27#include "extensions/common/features/feature.h"
28#include "extensions/common/features/simple_feature.h"
29#include "extensions/common/switches.h"
30#include "ui/compositor/compositor_switches.h"
31
32namespace {
33
34const char kExtensionId[] = "ddchlicdkolnonkihahngkmmmjnjlkkf";
35
36class TabCaptureApiTest : public ExtensionApiTest {
37 public:
38  void AddExtensionToCommandLineWhitelist() {
39    CommandLine::ForCurrentProcess()->AppendSwitchASCII(
40        extensions::switches::kWhitelistedExtensionID, kExtensionId);
41  }
42};
43
44class TabCaptureApiPixelTest : public TabCaptureApiTest {
45 public:
46  virtual void SetUp() OVERRIDE {
47    EnablePixelOutput();
48    TabCaptureApiTest::SetUp();
49  }
50};
51
52}  // namespace
53
54IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTests) {
55#if defined(OS_WIN) && defined(USE_ASH)
56  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
57  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
58    return;
59#endif
60
61#if defined(OS_WIN)
62  // TODO(justinlin): Disabled for WinXP due to timeout issues.
63  if (base::win::GetVersion() < base::win::VERSION_VISTA) {
64    return;
65  }
66#endif
67
68  AddExtensionToCommandLineWhitelist();
69  ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests.html")) << message_;
70}
71
72IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, ApiTestsAudio) {
73#if defined(OS_WIN)
74  // TODO(justinlin): Disabled for WinXP due to timeout issues.
75  if (base::win::GetVersion() < base::win::VERSION_VISTA) {
76    return;
77  }
78#endif
79
80  AddExtensionToCommandLineWhitelist();
81  ASSERT_TRUE(RunExtensionSubtest("tab_capture", "api_tests_audio.html"))
82      << message_;
83}
84
85// http://crbug.com/177163
86#if !defined(NDEBUG)
87#define MAYBE_EndToEnd DISABLED_EndToEnd
88#else
89#define MAYBE_EndToEnd EndToEnd
90#endif
91IN_PROC_BROWSER_TEST_F(TabCaptureApiPixelTest, MAYBE_EndToEnd) {
92#if defined(OS_WIN)
93  // TODO(justinlin): Disabled for WinXP due to timeout issues.
94  if (base::win::GetVersion() < base::win::VERSION_VISTA) {
95    return;
96  }
97#endif
98  // This test is too slow to succeed with OSMesa on the bots.
99  if (UsingOSMesa())
100    return;
101
102  AddExtensionToCommandLineWhitelist();
103  ASSERT_TRUE(RunExtensionSubtest("tab_capture", "end_to_end.html"))
104      << message_;
105}
106
107// http://crbug.com/177163
108#if defined(OS_WIN) && !defined(NDEBUG)
109#define MAYBE_GetUserMediaTest DISABLED_GetUserMediaTest
110#else
111#define MAYBE_GetUserMediaTest GetUserMediaTest
112#endif
113// Test that we can't get tabCapture streams using GetUserMedia directly.
114IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GetUserMediaTest) {
115  ExtensionTestMessageListener listener("ready", true);
116
117  ASSERT_TRUE(RunExtensionSubtest("tab_capture", "get_user_media_test.html"))
118      << message_;
119
120  EXPECT_TRUE(listener.WaitUntilSatisfied());
121
122  content::OpenURLParams params(GURL("about:blank"), content::Referrer(),
123                                NEW_FOREGROUND_TAB,
124                                content::PAGE_TRANSITION_LINK, false);
125  content::WebContents* web_contents = browser()->OpenURL(params);
126
127  content::RenderViewHost* const rvh = web_contents->GetRenderViewHost();
128  int render_process_id = rvh->GetProcess()->GetID();
129  int routing_id = rvh->GetRoutingID();
130
131  listener.Reply(base::StringPrintf("%i:%i", render_process_id, routing_id));
132
133  ResultCatcher catcher;
134  catcher.RestrictToProfile(browser()->profile());
135  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
136}
137
138// http://crbug.com/177163
139#if defined(OS_WIN) && !defined(NDEBUG)
140#define MAYBE_ActiveTabPermission DISABLED_ActiveTabPermission
141#else
142#define MAYBE_ActiveTabPermission ActiveTabPermission
143#endif
144// Make sure tabCapture.capture only works if the tab has been granted
145// permission via an extension icon click or the extension is whitelisted.
146IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_ActiveTabPermission) {
147  ExtensionTestMessageListener before_open_tab("ready1", true);
148  ExtensionTestMessageListener before_grant_permission("ready2", true);
149  ExtensionTestMessageListener before_open_new_tab("ready3", true);
150  ExtensionTestMessageListener before_whitelist_extension("ready4", true);
151
152  ASSERT_TRUE(RunExtensionSubtest("tab_capture",
153                                  "active_tab_permission_test.html"))
154      << message_;
155
156  // Open a new tab and make sure capture is denied.
157  EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
158  content::OpenURLParams params(GURL("http://google.com"), content::Referrer(),
159                                NEW_FOREGROUND_TAB,
160                                content::PAGE_TRANSITION_LINK, false);
161  content::WebContents* web_contents = browser()->OpenURL(params);
162  before_open_tab.Reply("");
163
164  // Grant permission and make sure capture succeeds.
165  EXPECT_TRUE(before_grant_permission.WaitUntilSatisfied());
166  ExtensionService* extension_service =
167      Profile::FromBrowserContext(web_contents->GetBrowserContext())
168          ->GetExtensionService();
169  const extensions::Extension* extension =
170      extension_service->GetExtensionById(kExtensionId, false);
171  extensions::TabHelper::FromWebContents(web_contents)
172      ->active_tab_permission_granter()->GrantIfRequested(extension);
173  before_grant_permission.Reply("");
174
175  // Open a new tab and make sure capture is denied.
176  EXPECT_TRUE(before_open_new_tab.WaitUntilSatisfied());
177  browser()->OpenURL(params);
178  before_open_new_tab.Reply("");
179
180  // Add extension to whitelist and make sure capture succeeds.
181  EXPECT_TRUE(before_whitelist_extension.WaitUntilSatisfied());
182  AddExtensionToCommandLineWhitelist();
183  before_whitelist_extension.Reply("");
184
185  ResultCatcher catcher;
186  catcher.RestrictToProfile(browser()->profile());
187  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
188}
189
190// http://crbug.com/177163
191#if defined(OS_WIN) && !defined(NDEBUG)
192#define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
193#elif defined(USE_AURA) || defined(OS_MACOSX)
194// These don't always fire fullscreen events when run in tests. Tested manually.
195#define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
196#elif defined(OS_LINUX)
197// Flaky to get out of fullscreen in tests. Tested manually.
198#define MAYBE_FullscreenEvents DISABLED_FullscreenEvents
199#else
200#define MAYBE_FullscreenEvents FullscreenEvents
201#endif
202IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_FullscreenEvents) {
203#if defined(OS_WIN)
204  // TODO(justinlin): Disabled for WinXP due to timeout issues.
205  if (base::win::GetVersion() < base::win::VERSION_VISTA) {
206    return;
207  }
208#endif
209
210  AddExtensionToCommandLineWhitelist();
211
212  content::OpenURLParams params(GURL("chrome://version"),
213                                content::Referrer(),
214                                CURRENT_TAB,
215                                content::PAGE_TRANSITION_LINK, false);
216  content::WebContents* web_contents = browser()->OpenURL(params);
217
218  ExtensionTestMessageListener listeners_setup("ready1", true);
219  ExtensionTestMessageListener fullscreen_entered("ready2", true);
220
221  ASSERT_TRUE(RunExtensionSubtest("tab_capture", "fullscreen_test.html"))
222      << message_;
223  EXPECT_TRUE(listeners_setup.WaitUntilSatisfied());
224
225  // Toggle fullscreen after setting up listeners.
226  browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
227                                                                 true);
228  listeners_setup.Reply("");
229
230  // Toggle again after JS should have the event.
231  EXPECT_TRUE(fullscreen_entered.WaitUntilSatisfied());
232  browser()->fullscreen_controller()->ToggleFullscreenModeForTab(web_contents,
233                                                                 false);
234  fullscreen_entered.Reply("");
235
236  ResultCatcher catcher;
237  catcher.RestrictToProfile(browser()->profile());
238  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
239}
240
241// Times out on Win dbg bots: http://crbug.com/177163
242// #if defined(OS_WIN) && !defined(NDEBUG)
243// Times out on all Win bots: http://crbug.com/294431
244#if defined(OS_WIN)
245#define MAYBE_GrantForChromePages DISABLED_GrantForChromePages
246#else
247#define MAYBE_GrantForChromePages GrantForChromePages
248#endif
249// Make sure tabCapture API can be granted for Chrome:// pages.
250IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_GrantForChromePages) {
251  ExtensionTestMessageListener before_open_tab("ready1", true);
252  ASSERT_TRUE(RunExtensionSubtest("tab_capture",
253                                  "active_tab_chrome_pages.html"))
254      << message_;
255  EXPECT_TRUE(before_open_tab.WaitUntilSatisfied());
256
257  // Open a tab on a chrome:// page and make sure we can capture.
258  content::OpenURLParams params(GURL("chrome://version"), content::Referrer(),
259                                NEW_FOREGROUND_TAB,
260                                content::PAGE_TRANSITION_LINK, false);
261  content::WebContents* web_contents = browser()->OpenURL(params);
262  ExtensionService* extension_service =
263      Profile::FromBrowserContext(web_contents->GetBrowserContext())
264          ->GetExtensionService();
265  extensions::TabHelper::FromWebContents(web_contents)
266      ->active_tab_permission_granter()->GrantIfRequested(
267            extension_service->GetExtensionById(kExtensionId, false));
268  before_open_tab.Reply("");
269
270  ResultCatcher catcher;
271  catcher.RestrictToProfile(browser()->profile());
272  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
273}
274
275#if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_MACOSX)
276// http://crbug.com/326319
277#define MAYBE_CaptureInSplitIncognitoMode DISABLED_CaptureInSplitIncognitoMode
278#else
279#define MAYBE_CaptureInSplitIncognitoMode CaptureInSplitIncognitoMode
280#endif
281// Test that a tab can be captured in split incognito mode.
282IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_CaptureInSplitIncognitoMode) {
283  AddExtensionToCommandLineWhitelist();
284  ASSERT_TRUE(RunExtensionSubtest("tab_capture",
285                                  "incognito.html",
286                                  kFlagEnableIncognito | kFlagUseIncognito))
287      << message_;
288}
289
290#if defined(OS_WIN) && !defined(NDEBUG)
291#define MAYBE_Constraints DISABLED_Constraints
292#else
293#define MAYBE_Constraints Constraints
294#endif
295IN_PROC_BROWSER_TEST_F(TabCaptureApiTest, MAYBE_Constraints) {
296  AddExtensionToCommandLineWhitelist();
297  ASSERT_TRUE(RunExtensionSubtest("tab_capture", "constraints.html"))
298      << message_;
299}
300