Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / blocked_content / popup_blocker_browsertest.cc
blobe4264d4f6c6d6614468d2360e7a436aa29568a03
1 // Copyright 2013 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/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/location_bar/location_bar.h"
21 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
22 #include "chrome/browser/ui/omnibox/omnibox_view.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chrome/test/base/test_switches.h"
28 #include "chrome/test/base/ui_test_utils.h"
29 #include "components/app_modal/javascript_app_modal_dialog.h"
30 #include "components/app_modal/native_app_modal_dialog.h"
31 #include "components/content_settings/core/browser/host_content_settings_map.h"
32 #include "components/omnibox/autocomplete_match.h"
33 #include "components/omnibox/autocomplete_result.h"
34 #include "content/public/browser/native_web_keyboard_event.h"
35 #include "content/public/browser/navigation_controller.h"
36 #include "content/public/browser/notification_registrar.h"
37 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/render_frame_host.h"
39 #include "content/public/browser/render_view_host.h"
40 #include "content/public/browser/web_contents.h"
41 #include "content/public/browser/web_contents_observer.h"
42 #include "content/public/common/url_constants.h"
43 #include "content/public/test/browser_test_utils.h"
44 #include "content/public/test/test_navigation_observer.h"
45 #include "net/dns/mock_host_resolver.h"
46 #include "net/test/embedded_test_server/embedded_test_server.h"
47 #include "testing/gtest/include/gtest/gtest.h"
48 #include "ui/events/keycodes/dom/dom_code.h"
49 #include "ui/events/keycodes/dom/keycode_converter.h"
51 using content::WebContents;
52 using content::NativeWebKeyboardEvent;
54 namespace {
56 // Counts the number of RenderViewHosts created.
57 class CountRenderViewHosts : public content::NotificationObserver {
58 public:
59 CountRenderViewHosts()
60 : count_(0) {
61 registrar_.Add(this,
62 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
63 content::NotificationService::AllSources());
65 ~CountRenderViewHosts() override {}
67 int GetRenderViewHostCreatedCount() const { return count_; }
69 private:
70 void Observe(int type,
71 const content::NotificationSource& source,
72 const content::NotificationDetails& details) override {
73 count_++;
76 content::NotificationRegistrar registrar_;
78 int count_;
80 DISALLOW_COPY_AND_ASSIGN(CountRenderViewHosts);
83 class CloseObserver : public content::WebContentsObserver {
84 public:
85 explicit CloseObserver(WebContents* contents)
86 : content::WebContentsObserver(contents) {}
88 void Wait() {
89 close_loop_.Run();
92 void WebContentsDestroyed() override { close_loop_.Quit(); }
94 private:
95 base::RunLoop close_loop_;
97 DISALLOW_COPY_AND_ASSIGN(CloseObserver);
100 class BrowserActivationObserver : public chrome::BrowserListObserver {
101 public:
102 explicit BrowserActivationObserver(chrome::HostDesktopType desktop_type)
103 : browser_(chrome::FindLastActiveWithHostDesktopType(desktop_type)),
104 observed_(false) {
105 BrowserList::AddObserver(this);
107 ~BrowserActivationObserver() override { BrowserList::RemoveObserver(this); }
109 void WaitForActivation() {
110 if (observed_)
111 return;
112 message_loop_runner_ = new content::MessageLoopRunner;
113 message_loop_runner_->Run();
116 private:
117 // chrome::BrowserListObserver:
118 void OnBrowserSetLastActive(Browser* browser) override {
119 if (browser == browser_)
120 return;
121 if (browser->host_desktop_type() != browser_->host_desktop_type())
122 return;
123 observed_ = true;
124 if (message_loop_runner_.get() && message_loop_runner_->loop_running())
125 message_loop_runner_->Quit();
128 Browser* browser_;
129 bool observed_;
130 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
132 DISALLOW_COPY_AND_ASSIGN(BrowserActivationObserver);
135 class PopupBlockerBrowserTest : public InProcessBrowserTest {
136 public:
137 PopupBlockerBrowserTest() {}
138 ~PopupBlockerBrowserTest() override {}
140 void SetUpOnMainThread() override {
141 InProcessBrowserTest::SetUpOnMainThread();
143 host_resolver()->AddRule("*", "127.0.0.1");
144 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
147 int GetBlockedContentsCount() {
148 // Do a round trip to the renderer first to flush any in-flight IPCs to
149 // create a to-be-blocked window.
150 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
151 CHECK(content::ExecuteScript(tab, std::string()));
152 PopupBlockerTabHelper* popup_blocker_helper =
153 PopupBlockerTabHelper::FromWebContents(tab);
154 return popup_blocker_helper->GetBlockedPopupsCount();
157 enum WhatToExpect {
158 ExpectPopup,
159 ExpectTab
162 enum ShouldCheckTitle {
163 CheckTitle,
164 DontCheckTitle
167 void NavigateAndCheckPopupShown(const GURL& url,
168 WhatToExpect what_to_expect) {
169 content::WindowedNotificationObserver observer(
170 chrome::NOTIFICATION_TAB_ADDED,
171 content::NotificationService::AllSources());
172 ui_test_utils::NavigateToURL(browser(), url);
173 observer.Wait();
175 if (what_to_expect == ExpectPopup) {
176 ASSERT_EQ(2u,
177 chrome::GetBrowserCount(browser()->profile(),
178 browser()->host_desktop_type()));
179 } else {
180 ASSERT_EQ(1u,
181 chrome::GetBrowserCount(browser()->profile(),
182 browser()->host_desktop_type()));
183 ASSERT_EQ(2, browser()->tab_strip_model()->count());
185 // Check that we always create foreground tabs.
186 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
189 ASSERT_EQ(0, GetBlockedContentsCount());
192 // Navigates to the test indicated by |test_name| using |browser| which is
193 // expected to try to open a popup. Verifies that the popup was blocked and
194 // then opens the blocked popup. Once the popup stopped loading, verifies
195 // that the title of the page is "PASS" if |check_title| is set.
197 // If |what_to_expect| is ExpectPopup, the popup is expected to open a new
198 // window, or a background tab if it is false.
200 // Returns the WebContents of the launched popup.
201 WebContents* RunCheckTest(Browser* browser,
202 const std::string& test_name,
203 WhatToExpect what_to_expect,
204 ShouldCheckTitle check_title) {
205 GURL url(embedded_test_server()->GetURL(test_name));
207 CountRenderViewHosts counter;
209 ui_test_utils::NavigateToURL(browser, url);
211 // Since the popup blocker blocked the window.open, there should be only one
212 // tab.
213 EXPECT_EQ(1u, chrome::GetBrowserCount(browser->profile(),
214 browser->host_desktop_type()));
215 EXPECT_EQ(1, browser->tab_strip_model()->count());
216 WebContents* web_contents =
217 browser->tab_strip_model()->GetActiveWebContents();
218 EXPECT_EQ(url, web_contents->GetURL());
220 // And no new RVH created.
221 EXPECT_EQ(0, counter.GetRenderViewHostCreatedCount());
223 content::WindowedNotificationObserver observer(
224 chrome::NOTIFICATION_TAB_ADDED,
225 content::NotificationService::AllSources());
226 ui_test_utils::BrowserAddedObserver browser_observer;
228 // Launch the blocked popup.
229 PopupBlockerTabHelper* popup_blocker_helper =
230 PopupBlockerTabHelper::FromWebContents(web_contents);
231 if (!popup_blocker_helper->GetBlockedPopupsCount()) {
232 content::WindowedNotificationObserver observer(
233 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
234 content::NotificationService::AllSources());
235 observer.Wait();
237 EXPECT_EQ(1u, popup_blocker_helper->GetBlockedPopupsCount());
238 std::map<int32, GURL> blocked_requests =
239 popup_blocker_helper->GetBlockedPopupRequests();
240 std::map<int32, GURL>::const_iterator iter = blocked_requests.begin();
241 popup_blocker_helper->ShowBlockedPopup(iter->first);
243 observer.Wait();
244 Browser* new_browser;
245 if (what_to_expect == ExpectPopup) {
246 new_browser = browser_observer.WaitForSingleNewBrowser();
247 web_contents = new_browser->tab_strip_model()->GetActiveWebContents();
248 } else {
249 new_browser = browser;
250 EXPECT_EQ(2, browser->tab_strip_model()->count());
251 // Check that we always create foreground tabs.
252 EXPECT_EQ(1, browser->tab_strip_model()->active_index());
253 web_contents = browser->tab_strip_model()->GetWebContentsAt(1);
256 if (check_title == CheckTitle) {
257 // Check that the check passed.
258 base::string16 expected_title(base::ASCIIToUTF16("PASS"));
259 content::TitleWatcher title_watcher(web_contents, expected_title);
260 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
263 return web_contents;
266 private:
267 DISALLOW_COPY_AND_ASSIGN(PopupBlockerBrowserTest);
270 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
271 BlockWebContentsCreation) {
272 #if defined(OS_WIN) && defined(USE_ASH)
273 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
274 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
275 switches::kAshBrowserTests))
276 return;
277 #endif
279 RunCheckTest(
280 browser(),
281 "/popup_blocker/popup-blocked-to-post-blank.html",
282 ExpectTab,
283 DontCheckTitle);
286 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
287 BlockWebContentsCreationIncognito) {
288 #if defined(OS_WIN) && defined(USE_ASH)
289 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
290 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
291 switches::kAshBrowserTests))
292 return;
293 #endif
295 RunCheckTest(
296 CreateIncognitoBrowser(),
297 "/popup_blocker/popup-blocked-to-post-blank.html",
298 ExpectTab,
299 DontCheckTitle);
302 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
303 PopupBlockedFakeClickOnAnchor) {
304 #if defined(OS_WIN) && defined(USE_ASH)
305 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
306 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
307 switches::kAshBrowserTests))
308 return;
309 #endif
311 RunCheckTest(
312 browser(),
313 "/popup_blocker/popup-fake-click-on-anchor.html",
314 ExpectTab,
315 DontCheckTitle);
318 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
319 PopupBlockedFakeClickOnAnchorNoTarget) {
320 #if defined(OS_WIN) && defined(USE_ASH)
321 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
322 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
323 switches::kAshBrowserTests))
324 return;
325 #endif
327 RunCheckTest(
328 browser(),
329 "/popup_blocker/popup-fake-click-on-anchor2.html",
330 ExpectTab,
331 DontCheckTitle);
334 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopups) {
335 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-many.html"));
336 ui_test_utils::NavigateToURL(browser(), url);
337 ASSERT_EQ(2, GetBlockedContentsCount());
340 // Verify that popups are launched on browser back button.
341 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
342 AllowPopupThroughContentSetting) {
343 GURL url(embedded_test_server()->GetURL(
344 "/popup_blocker/popup-blocked-to-post-blank.html"));
345 browser()->profile()->GetHostContentSettingsMap()
346 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
347 ContentSettingsPattern::Wildcard(),
348 CONTENT_SETTINGS_TYPE_POPUPS,
349 std::string(),
350 CONTENT_SETTING_ALLOW);
352 NavigateAndCheckPopupShown(url, ExpectTab);
355 // Verify that content settings are applied based on the top-level frame URL.
356 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
357 AllowPopupThroughContentSettingIFrame) {
358 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-frames.html"));
359 browser()->profile()->GetHostContentSettingsMap()
360 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
361 ContentSettingsPattern::Wildcard(),
362 CONTENT_SETTINGS_TYPE_POPUPS,
363 std::string(),
364 CONTENT_SETTING_ALLOW);
366 // Popup from the iframe should be allowed since the top-level URL is
367 // whitelisted.
368 NavigateAndCheckPopupShown(url, ExpectTab);
370 // Whitelist iframe URL instead.
371 GURL::Replacements replace_host;
372 replace_host.SetHostStr("www.a.com");
373 GURL frame_url(embedded_test_server()
374 ->GetURL("/popup_blocker/popup-frames-iframe.html")
375 .ReplaceComponents(replace_host));
376 browser()->profile()->GetHostContentSettingsMap()->ClearSettingsForOneType(
377 CONTENT_SETTINGS_TYPE_POPUPS);
378 browser()->profile()->GetHostContentSettingsMap()
379 ->SetContentSetting(ContentSettingsPattern::FromURL(frame_url),
380 ContentSettingsPattern::Wildcard(),
381 CONTENT_SETTINGS_TYPE_POPUPS,
382 std::string(),
383 CONTENT_SETTING_ALLOW);
385 // Popup should be blocked.
386 ui_test_utils::NavigateToURL(browser(), url);
387 ASSERT_EQ(1, GetBlockedContentsCount());
390 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
391 PopupsLaunchWhenTabIsClosed) {
392 base::CommandLine::ForCurrentProcess()->AppendSwitch(
393 switches::kDisablePopupBlocking);
394 GURL url(
395 embedded_test_server()->GetURL("/popup_blocker/popup-on-unload.html"));
396 ui_test_utils::NavigateToURL(browser(), url);
398 NavigateAndCheckPopupShown(embedded_test_server()->GetURL("/popup_blocker/"),
399 ExpectPopup);
402 // Verify that when you unblock popup, the popup shows in history and omnibox.
403 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
404 UnblockedPopupShowsInHistoryAndOmnibox) {
405 base::CommandLine::ForCurrentProcess()->AppendSwitch(
406 switches::kDisablePopupBlocking);
407 GURL url(embedded_test_server()->GetURL(
408 "/popup_blocker/popup-blocked-to-post-blank.html"));
409 NavigateAndCheckPopupShown(url, ExpectTab);
411 std::string search_string =
412 "data:text/html,<title>Popup Success!</title>you should not see this "
413 "message if popup blocker is enabled";
415 ui_test_utils::HistoryEnumerator history(browser()->profile());
416 std::vector<GURL>& history_urls = history.urls();
417 ASSERT_EQ(2u, history_urls.size());
418 ASSERT_EQ(GURL(search_string), history_urls[0]);
419 ASSERT_EQ(url, history_urls[1]);
421 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
422 browser()->profile());
423 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
424 LocationBar* location_bar = browser()->window()->GetLocationBar();
425 ui_test_utils::SendToOmniboxAndSubmit(location_bar, search_string);
426 OmniboxEditModel* model = location_bar->GetOmniboxView()->model();
427 EXPECT_EQ(GURL(search_string), model->CurrentMatch(NULL).destination_url);
428 EXPECT_EQ(base::ASCIIToUTF16(search_string),
429 model->CurrentMatch(NULL).contents);
432 // This test fails on linux AURA with this change
433 // https://codereview.chromium.org/23903056
434 // BUG=https://code.google.com/p/chromium/issues/detail?id=295299
435 // TODO(ananta). Debug and fix this test.
436 #if defined(USE_AURA) && defined(OS_LINUX)
437 #define MAYBE_WindowFeatures DISABLED_WindowFeatures
438 #else
439 #define MAYBE_WindowFeatures WindowFeatures
440 #endif
441 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MAYBE_WindowFeatures) {
442 WebContents* popup =
443 RunCheckTest(browser(),
444 "/popup_blocker/popup-window-open.html",
445 ExpectPopup,
446 DontCheckTitle);
448 // Check that the new popup has (roughly) the requested size.
449 gfx::Size window_size = popup->GetContainerBounds().size();
450 EXPECT_TRUE(349 <= window_size.width() && window_size.width() <= 351);
451 EXPECT_TRUE(249 <= window_size.height() && window_size.height() <= 251);
454 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, CorrectReferrer) {
455 RunCheckTest(browser(),
456 "/popup_blocker/popup-referrer.html",
457 ExpectTab,
458 CheckTitle);
461 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, WindowFeaturesBarProps) {
462 RunCheckTest(browser(),
463 "/popup_blocker/popup-windowfeatures.html",
464 ExpectPopup,
465 CheckTitle);
468 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, SessionStorage) {
469 RunCheckTest(browser(),
470 "/popup_blocker/popup-sessionstorage.html",
471 ExpectTab,
472 CheckTitle);
475 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, Opener) {
476 RunCheckTest(browser(),
477 "/popup_blocker/popup-opener.html",
478 ExpectTab,
479 CheckTitle);
482 // Tests that the popup can still close itself after navigating. This tests that
483 // the openedByDOM bit is preserved across blocked popups.
484 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ClosableAfterNavigation) {
485 // Open a popup.
486 WebContents* popup =
487 RunCheckTest(browser(),
488 "/popup_blocker/popup-opener.html",
489 ExpectTab,
490 CheckTitle);
492 // Navigate it elsewhere.
493 content::TestNavigationObserver nav_observer(popup);
494 popup->GetMainFrame()->ExecuteJavaScript(
495 base::UTF8ToUTF16("location.href = '/empty.html'"));
496 nav_observer.Wait();
498 // Have it close itself.
499 CloseObserver close_observer(popup);
500 popup->GetMainFrame()->ExecuteJavaScript(
501 base::UTF8ToUTF16("window.close()"));
502 close_observer.Wait();
505 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, OpenerSuppressed) {
506 RunCheckTest(browser(),
507 "/popup_blocker/popup-openersuppressed.html",
508 ExpectTab,
509 CheckTitle);
512 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ShiftClick) {
513 RunCheckTest(
514 browser(),
515 "/popup_blocker/popup-fake-click-on-anchor3.html",
516 ExpectPopup,
517 CheckTitle);
520 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, WebUI) {
521 WebContents* popup =
522 RunCheckTest(browser(),
523 "/popup_blocker/popup-webui.html",
524 ExpectTab,
525 DontCheckTitle);
527 // Check that the new popup displays about:blank.
528 EXPECT_EQ(GURL(url::kAboutBlankURL), popup->GetURL());
531 // Verify that the renderer can't DOS the browser by creating arbitrarily many
532 // popups.
533 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, DenialOfService) {
534 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-dos.html"));
535 ui_test_utils::NavigateToURL(browser(), url);
536 ASSERT_EQ(25, GetBlockedContentsCount());
539 // Verify that an onunload popup does not show up for about:blank.
540 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, Regress427477) {
541 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
542 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
544 GURL url(
545 embedded_test_server()->GetURL("/popup_blocker/popup-on-unload.html"));
546 ui_test_utils::NavigateToURL(browser(), url);
548 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
550 tab->GetController().GoBack();
551 content::WaitForLoadStop(tab);
553 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
554 browser()->host_desktop_type()));
555 ASSERT_EQ(1, browser()->tab_strip_model()->count());
557 // The popup from the unload event handler should not show up for about:blank.
558 ASSERT_EQ(0, GetBlockedContentsCount());
561 // Verify that app modal prompts can't be used to create pop unders.
562 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnder) {
563 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
564 GURL url(
565 embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
566 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
567 ContentSettingsPattern::FromURL(url), ContentSettingsPattern::Wildcard(),
568 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW);
570 NavigateAndCheckPopupShown(url, ExpectPopup);
572 Browser* popup_browser =
573 chrome::FindLastActiveWithHostDesktopType(browser()->host_desktop_type());
574 ASSERT_NE(popup_browser, browser());
576 // Showing an alert will raise the tab over the popup.
577 tab->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16("alert()"));
578 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
580 // Verify that after the dialog was closed, the popup is in front again.
581 ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
582 app_modal::JavaScriptAppModalDialog* js_dialog =
583 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
585 BrowserActivationObserver activation_observer(browser()->host_desktop_type());
586 js_dialog->native_dialog()->AcceptAppModalDialog();
588 if (popup_browser != chrome::FindLastActiveWithHostDesktopType(
589 popup_browser->host_desktop_type())) {
590 activation_observer.WaitForActivation();
592 ASSERT_EQ(popup_browser, chrome::FindLastActiveWithHostDesktopType(
593 popup_browser->host_desktop_type()));
596 void BuildSimpleWebKeyEvent(blink::WebInputEvent::Type type,
597 ui::KeyboardCode key_code,
598 int native_key_code,
599 int modifiers,
600 NativeWebKeyboardEvent* event) {
601 event->nativeKeyCode = native_key_code;
602 event->windowsKeyCode = key_code;
603 event->setKeyIdentifierFromWindowsKeyCode();
604 event->type = type;
605 event->modifiers = modifiers;
606 event->isSystemKey = false;
607 event->timeStampSeconds = base::Time::Now().ToDoubleT();
608 event->skip_in_browser = true;
610 if (type == blink::WebInputEvent::Char ||
611 type == blink::WebInputEvent::RawKeyDown) {
612 event->text[0] = key_code;
613 event->unmodifiedText[0] = key_code;
617 void InjectRawKeyEvent(WebContents* web_contents,
618 blink::WebInputEvent::Type type,
619 ui::KeyboardCode key_code,
620 int native_key_code,
621 int modifiers) {
622 NativeWebKeyboardEvent event;
623 BuildSimpleWebKeyEvent(type, key_code, native_key_code, modifiers, &event);
624 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(event);
627 // Tests that Ctrl+Enter/Cmd+Enter keys on a link open the backgournd tab.
628 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, CtrlEnterKey) {
629 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
631 GURL url(embedded_test_server()->GetURL(
632 "/popup_blocker/popup-simulated-click-on-anchor.html"));
633 ui_test_utils::NavigateToURL(browser(), url);
635 content::WindowedNotificationObserver wait_for_new_tab(
636 chrome::NOTIFICATION_TAB_ADDED,
637 content::NotificationService::AllSources());
639 #if defined(OS_MACOSX)
640 int modifiers = blink::WebInputEvent::MetaKey;
641 InjectRawKeyEvent(
642 tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_COMMAND,
643 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::OS_LEFT),
644 modifiers);
645 #else
646 int modifiers = blink::WebInputEvent::ControlKey;
647 InjectRawKeyEvent(
648 tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_CONTROL,
649 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::CONTROL_LEFT),
650 modifiers);
651 #endif
653 InjectRawKeyEvent(tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_RETURN,
654 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
656 InjectRawKeyEvent(tab, blink::WebInputEvent::Char, ui::VKEY_RETURN,
657 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
659 InjectRawKeyEvent(tab, blink::WebInputEvent::KeyUp, ui::VKEY_RETURN,
660 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
662 #if defined(OS_MACOSX)
663 InjectRawKeyEvent(
664 tab, blink::WebInputEvent::KeyUp, ui::VKEY_COMMAND,
665 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::OS_LEFT),
666 modifiers);
667 #else
668 InjectRawKeyEvent(
669 tab, blink::WebInputEvent::KeyUp, ui::VKEY_CONTROL,
670 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::CONTROL_LEFT),
671 modifiers);
672 #endif
673 wait_for_new_tab.Wait();
675 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
676 browser()->host_desktop_type()));
677 ASSERT_EQ(2, browser()->tab_strip_model()->count());
678 // Check that we create the background tab.
679 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
682 // Tests that the tapping gesture with cntl/cmd key on a link open the
683 // backgournd tab.
684 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, TapGestureWithCtrlKey) {
685 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
687 GURL url(embedded_test_server()->GetURL(
688 "/popup_blocker/popup-simulated-click-on-anchor2.html"));
689 ui_test_utils::NavigateToURL(browser(), url);
691 content::WindowedNotificationObserver wait_for_new_tab(
692 chrome::NOTIFICATION_TAB_ADDED,
693 content::NotificationService::AllSources());
695 #if defined(OS_MACOSX)
696 unsigned modifiers = blink::WebInputEvent::MetaKey;
697 #else
698 unsigned modifiers = blink::WebInputEvent::ControlKey;
699 #endif
700 content::SimulateTapWithModifiersAt(tab, modifiers, gfx::Point(350, 250));
702 wait_for_new_tab.Wait();
704 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
705 browser()->host_desktop_type()));
706 ASSERT_EQ(2, browser()->tab_strip_model()->count());
707 // Check that we create the background tab.
708 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
711 } // namespace