Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / blocked_content / popup_blocker_browsertest.cc
blob3d0fa1de2a959dc78896bcf75354b7bf3ee53df9
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/tabs/tab_strip_model.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/url_constants.h"
25 #include "chrome/test/base/in_process_browser_test.h"
26 #include "chrome/test/base/test_switches.h"
27 #include "chrome/test/base/ui_test_utils.h"
28 #include "components/app_modal/javascript_app_modal_dialog.h"
29 #include "components/app_modal/native_app_modal_dialog.h"
30 #include "components/content_settings/core/browser/host_content_settings_map.h"
31 #include "components/omnibox/browser/autocomplete_match.h"
32 #include "components/omnibox/browser/autocomplete_result.h"
33 #include "components/omnibox/browser/omnibox_edit_model.h"
34 #include "components/omnibox/browser/omnibox_view.h"
35 #include "content/public/browser/native_web_keyboard_event.h"
36 #include "content/public/browser/navigation_controller.h"
37 #include "content/public/browser/notification_registrar.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/render_frame_host.h"
40 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/browser/web_contents_observer.h"
43 #include "content/public/common/url_constants.h"
44 #include "content/public/test/browser_test_utils.h"
45 #include "content/public/test/test_navigation_observer.h"
46 #include "net/dns/mock_host_resolver.h"
47 #include "net/test/embedded_test_server/embedded_test_server.h"
48 #include "testing/gtest/include/gtest/gtest.h"
49 #include "ui/events/keycodes/dom/dom_code.h"
50 #include "ui/events/keycodes/dom/keycode_converter.h"
52 using content::WebContents;
53 using content::NativeWebKeyboardEvent;
55 namespace {
57 // Counts the number of RenderViewHosts created.
58 class CountRenderViewHosts : public content::NotificationObserver {
59 public:
60 CountRenderViewHosts()
61 : count_(0) {
62 registrar_.Add(this,
63 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
64 content::NotificationService::AllSources());
66 ~CountRenderViewHosts() override {}
68 int GetRenderViewHostCreatedCount() const { return count_; }
70 private:
71 void Observe(int type,
72 const content::NotificationSource& source,
73 const content::NotificationDetails& details) override {
74 count_++;
77 content::NotificationRegistrar registrar_;
79 int count_;
81 DISALLOW_COPY_AND_ASSIGN(CountRenderViewHosts);
84 class CloseObserver : public content::WebContentsObserver {
85 public:
86 explicit CloseObserver(WebContents* contents)
87 : content::WebContentsObserver(contents) {}
89 void Wait() {
90 close_loop_.Run();
93 void WebContentsDestroyed() override { close_loop_.Quit(); }
95 private:
96 base::RunLoop close_loop_;
98 DISALLOW_COPY_AND_ASSIGN(CloseObserver);
101 class BrowserActivationObserver : public chrome::BrowserListObserver {
102 public:
103 explicit BrowserActivationObserver(chrome::HostDesktopType desktop_type)
104 : browser_(chrome::FindLastActiveWithHostDesktopType(desktop_type)),
105 observed_(false) {
106 BrowserList::AddObserver(this);
108 ~BrowserActivationObserver() override { BrowserList::RemoveObserver(this); }
110 void WaitForActivation() {
111 if (observed_)
112 return;
113 message_loop_runner_ = new content::MessageLoopRunner;
114 message_loop_runner_->Run();
117 private:
118 // chrome::BrowserListObserver:
119 void OnBrowserSetLastActive(Browser* browser) override {
120 if (browser == browser_)
121 return;
122 if (browser->host_desktop_type() != browser_->host_desktop_type())
123 return;
124 observed_ = true;
125 if (message_loop_runner_.get() && message_loop_runner_->loop_running())
126 message_loop_runner_->Quit();
129 Browser* browser_;
130 bool observed_;
131 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
133 DISALLOW_COPY_AND_ASSIGN(BrowserActivationObserver);
136 class PopupBlockerBrowserTest : public InProcessBrowserTest {
137 public:
138 PopupBlockerBrowserTest() {}
139 ~PopupBlockerBrowserTest() override {}
141 void SetUpOnMainThread() override {
142 InProcessBrowserTest::SetUpOnMainThread();
144 host_resolver()->AddRule("*", "127.0.0.1");
145 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
148 int GetBlockedContentsCount() {
149 // Do a round trip to the renderer first to flush any in-flight IPCs to
150 // create a to-be-blocked window.
151 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
152 CHECK(content::ExecuteScript(tab, std::string()));
153 PopupBlockerTabHelper* popup_blocker_helper =
154 PopupBlockerTabHelper::FromWebContents(tab);
155 return popup_blocker_helper->GetBlockedPopupsCount();
158 enum WhatToExpect {
159 ExpectPopup,
160 ExpectTab
163 enum ShouldCheckTitle {
164 CheckTitle,
165 DontCheckTitle
168 void NavigateAndCheckPopupShown(const GURL& url,
169 WhatToExpect what_to_expect) {
170 content::WindowedNotificationObserver observer(
171 chrome::NOTIFICATION_TAB_ADDED,
172 content::NotificationService::AllSources());
173 ui_test_utils::NavigateToURL(browser(), url);
174 observer.Wait();
176 if (what_to_expect == ExpectPopup) {
177 ASSERT_EQ(2u,
178 chrome::GetBrowserCount(browser()->profile(),
179 browser()->host_desktop_type()));
180 } else {
181 ASSERT_EQ(1u,
182 chrome::GetBrowserCount(browser()->profile(),
183 browser()->host_desktop_type()));
184 ASSERT_EQ(2, browser()->tab_strip_model()->count());
186 // Check that we always create foreground tabs.
187 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
190 ASSERT_EQ(0, GetBlockedContentsCount());
193 // Navigates to the test indicated by |test_name| using |browser| which is
194 // expected to try to open a popup. Verifies that the popup was blocked and
195 // then opens the blocked popup. Once the popup stopped loading, verifies
196 // that the title of the page is "PASS" if |check_title| is set.
198 // If |what_to_expect| is ExpectPopup, the popup is expected to open a new
199 // window, or a background tab if it is false.
201 // Returns the WebContents of the launched popup.
202 WebContents* RunCheckTest(Browser* browser,
203 const std::string& test_name,
204 WhatToExpect what_to_expect,
205 ShouldCheckTitle check_title) {
206 GURL url(embedded_test_server()->GetURL(test_name));
208 CountRenderViewHosts counter;
210 ui_test_utils::NavigateToURL(browser, url);
212 // Since the popup blocker blocked the window.open, there should be only one
213 // tab.
214 EXPECT_EQ(1u, chrome::GetBrowserCount(browser->profile(),
215 browser->host_desktop_type()));
216 EXPECT_EQ(1, browser->tab_strip_model()->count());
217 WebContents* web_contents =
218 browser->tab_strip_model()->GetActiveWebContents();
219 EXPECT_EQ(url, web_contents->GetURL());
221 // And no new RVH created.
222 EXPECT_EQ(0, counter.GetRenderViewHostCreatedCount());
224 content::WindowedNotificationObserver observer(
225 chrome::NOTIFICATION_TAB_ADDED,
226 content::NotificationService::AllSources());
227 ui_test_utils::BrowserAddedObserver browser_observer;
229 // Launch the blocked popup.
230 PopupBlockerTabHelper* popup_blocker_helper =
231 PopupBlockerTabHelper::FromWebContents(web_contents);
232 if (!popup_blocker_helper->GetBlockedPopupsCount()) {
233 content::WindowedNotificationObserver observer(
234 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
235 content::NotificationService::AllSources());
236 observer.Wait();
238 EXPECT_EQ(1u, popup_blocker_helper->GetBlockedPopupsCount());
239 std::map<int32, GURL> blocked_requests =
240 popup_blocker_helper->GetBlockedPopupRequests();
241 std::map<int32, GURL>::const_iterator iter = blocked_requests.begin();
242 popup_blocker_helper->ShowBlockedPopup(iter->first);
244 observer.Wait();
245 Browser* new_browser;
246 if (what_to_expect == ExpectPopup) {
247 new_browser = browser_observer.WaitForSingleNewBrowser();
248 web_contents = new_browser->tab_strip_model()->GetActiveWebContents();
249 } else {
250 new_browser = browser;
251 EXPECT_EQ(2, browser->tab_strip_model()->count());
252 // Check that we always create foreground tabs.
253 EXPECT_EQ(1, browser->tab_strip_model()->active_index());
254 web_contents = browser->tab_strip_model()->GetWebContentsAt(1);
257 if (check_title == CheckTitle) {
258 // Check that the check passed.
259 base::string16 expected_title(base::ASCIIToUTF16("PASS"));
260 content::TitleWatcher title_watcher(web_contents, expected_title);
261 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
264 return web_contents;
267 private:
268 DISALLOW_COPY_AND_ASSIGN(PopupBlockerBrowserTest);
271 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
272 BlockWebContentsCreation) {
273 #if defined(OS_WIN) && defined(USE_ASH)
274 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
275 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
276 switches::kAshBrowserTests))
277 return;
278 #endif
280 RunCheckTest(
281 browser(),
282 "/popup_blocker/popup-blocked-to-post-blank.html",
283 ExpectTab,
284 DontCheckTitle);
287 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
288 BlockWebContentsCreationIncognito) {
289 #if defined(OS_WIN) && defined(USE_ASH)
290 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
291 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
292 switches::kAshBrowserTests))
293 return;
294 #endif
296 RunCheckTest(
297 CreateIncognitoBrowser(),
298 "/popup_blocker/popup-blocked-to-post-blank.html",
299 ExpectTab,
300 DontCheckTitle);
303 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
304 PopupBlockedFakeClickOnAnchor) {
305 #if defined(OS_WIN) && defined(USE_ASH)
306 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
307 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
308 switches::kAshBrowserTests))
309 return;
310 #endif
312 RunCheckTest(
313 browser(),
314 "/popup_blocker/popup-fake-click-on-anchor.html",
315 ExpectTab,
316 DontCheckTitle);
319 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
320 PopupBlockedFakeClickOnAnchorNoTarget) {
321 #if defined(OS_WIN) && defined(USE_ASH)
322 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
323 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
324 switches::kAshBrowserTests))
325 return;
326 #endif
328 RunCheckTest(
329 browser(),
330 "/popup_blocker/popup-fake-click-on-anchor2.html",
331 ExpectTab,
332 DontCheckTitle);
335 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopups) {
336 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-many.html"));
337 ui_test_utils::NavigateToURL(browser(), url);
338 ASSERT_EQ(2, GetBlockedContentsCount());
341 // Verify that popups are launched on browser back button.
342 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
343 AllowPopupThroughContentSetting) {
344 GURL url(embedded_test_server()->GetURL(
345 "/popup_blocker/popup-blocked-to-post-blank.html"));
346 browser()->profile()->GetHostContentSettingsMap()
347 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
348 ContentSettingsPattern::Wildcard(),
349 CONTENT_SETTINGS_TYPE_POPUPS,
350 std::string(),
351 CONTENT_SETTING_ALLOW);
353 NavigateAndCheckPopupShown(url, ExpectTab);
356 // Verify that content settings are applied based on the top-level frame URL.
357 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
358 AllowPopupThroughContentSettingIFrame) {
359 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-frames.html"));
360 browser()->profile()->GetHostContentSettingsMap()
361 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
362 ContentSettingsPattern::Wildcard(),
363 CONTENT_SETTINGS_TYPE_POPUPS,
364 std::string(),
365 CONTENT_SETTING_ALLOW);
367 // Popup from the iframe should be allowed since the top-level URL is
368 // whitelisted.
369 NavigateAndCheckPopupShown(url, ExpectTab);
371 // Whitelist iframe URL instead.
372 GURL::Replacements replace_host;
373 replace_host.SetHostStr("www.a.com");
374 GURL frame_url(embedded_test_server()
375 ->GetURL("/popup_blocker/popup-frames-iframe.html")
376 .ReplaceComponents(replace_host));
377 browser()->profile()->GetHostContentSettingsMap()->ClearSettingsForOneType(
378 CONTENT_SETTINGS_TYPE_POPUPS);
379 browser()->profile()->GetHostContentSettingsMap()
380 ->SetContentSetting(ContentSettingsPattern::FromURL(frame_url),
381 ContentSettingsPattern::Wildcard(),
382 CONTENT_SETTINGS_TYPE_POPUPS,
383 std::string(),
384 CONTENT_SETTING_ALLOW);
386 // Popup should be blocked.
387 ui_test_utils::NavigateToURL(browser(), url);
388 ASSERT_EQ(1, GetBlockedContentsCount());
391 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
392 PopupsLaunchWhenTabIsClosed) {
393 base::CommandLine::ForCurrentProcess()->AppendSwitch(
394 switches::kDisablePopupBlocking);
395 GURL url(
396 embedded_test_server()->GetURL("/popup_blocker/popup-on-unload.html"));
397 ui_test_utils::NavigateToURL(browser(), url);
399 NavigateAndCheckPopupShown(embedded_test_server()->GetURL("/popup_blocker/"),
400 ExpectPopup);
403 // Verify that when you unblock popup, the popup shows in history and omnibox.
404 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
405 UnblockedPopupShowsInHistoryAndOmnibox) {
406 base::CommandLine::ForCurrentProcess()->AppendSwitch(
407 switches::kDisablePopupBlocking);
408 GURL url(embedded_test_server()->GetURL(
409 "/popup_blocker/popup-blocked-to-post-blank.html"));
410 NavigateAndCheckPopupShown(url, ExpectTab);
412 std::string search_string =
413 "data:text/html,<title>Popup Success!</title>you should not see this "
414 "message if popup blocker is enabled";
416 ui_test_utils::HistoryEnumerator history(browser()->profile());
417 std::vector<GURL>& history_urls = history.urls();
418 ASSERT_EQ(2u, history_urls.size());
419 ASSERT_EQ(GURL(search_string), history_urls[0]);
420 ASSERT_EQ(url, history_urls[1]);
422 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
423 browser()->profile());
424 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
425 LocationBar* location_bar = browser()->window()->GetLocationBar();
426 ui_test_utils::SendToOmniboxAndSubmit(location_bar, search_string);
427 OmniboxEditModel* model = location_bar->GetOmniboxView()->model();
428 EXPECT_EQ(GURL(search_string), model->CurrentMatch(NULL).destination_url);
429 EXPECT_EQ(base::ASCIIToUTF16(search_string),
430 model->CurrentMatch(NULL).contents);
433 // This test fails on linux AURA with this change
434 // https://codereview.chromium.org/23903056
435 // BUG=https://code.google.com/p/chromium/issues/detail?id=295299
436 // TODO(ananta). Debug and fix this test.
437 #if defined(USE_AURA) && defined(OS_LINUX)
438 #define MAYBE_WindowFeatures DISABLED_WindowFeatures
439 #else
440 #define MAYBE_WindowFeatures WindowFeatures
441 #endif
442 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MAYBE_WindowFeatures) {
443 WebContents* popup =
444 RunCheckTest(browser(),
445 "/popup_blocker/popup-window-open.html",
446 ExpectPopup,
447 DontCheckTitle);
449 // Check that the new popup has (roughly) the requested size.
450 gfx::Size window_size = popup->GetContainerBounds().size();
451 EXPECT_TRUE(349 <= window_size.width() && window_size.width() <= 351);
452 EXPECT_TRUE(249 <= window_size.height() && window_size.height() <= 251);
455 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, CorrectReferrer) {
456 RunCheckTest(browser(),
457 "/popup_blocker/popup-referrer.html",
458 ExpectTab,
459 CheckTitle);
462 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, WindowFeaturesBarProps) {
463 RunCheckTest(browser(),
464 "/popup_blocker/popup-windowfeatures.html",
465 ExpectPopup,
466 CheckTitle);
469 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, SessionStorage) {
470 RunCheckTest(browser(),
471 "/popup_blocker/popup-sessionstorage.html",
472 ExpectTab,
473 CheckTitle);
476 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, Opener) {
477 RunCheckTest(browser(),
478 "/popup_blocker/popup-opener.html",
479 ExpectTab,
480 CheckTitle);
483 // Tests that the popup can still close itself after navigating. This tests that
484 // the openedByDOM bit is preserved across blocked popups.
485 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ClosableAfterNavigation) {
486 // Open a popup.
487 WebContents* popup =
488 RunCheckTest(browser(),
489 "/popup_blocker/popup-opener.html",
490 ExpectTab,
491 CheckTitle);
493 // Navigate it elsewhere.
494 content::TestNavigationObserver nav_observer(popup);
495 popup->GetMainFrame()->ExecuteJavaScriptForTests(
496 base::UTF8ToUTF16("location.href = '/empty.html'"));
497 nav_observer.Wait();
499 // Have it close itself.
500 CloseObserver close_observer(popup);
501 popup->GetMainFrame()->ExecuteJavaScriptForTests(
502 base::UTF8ToUTF16("window.close()"));
503 close_observer.Wait();
506 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, OpenerSuppressed) {
507 RunCheckTest(browser(),
508 "/popup_blocker/popup-openersuppressed.html",
509 ExpectTab,
510 CheckTitle);
513 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ShiftClick) {
514 RunCheckTest(
515 browser(),
516 "/popup_blocker/popup-fake-click-on-anchor3.html",
517 ExpectPopup,
518 CheckTitle);
521 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, WebUI) {
522 WebContents* popup =
523 RunCheckTest(browser(),
524 "/popup_blocker/popup-webui.html",
525 ExpectTab,
526 DontCheckTitle);
528 // Check that the new popup displays about:blank.
529 EXPECT_EQ(GURL(url::kAboutBlankURL), popup->GetURL());
532 // Verify that the renderer can't DOS the browser by creating arbitrarily many
533 // popups.
534 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, DenialOfService) {
535 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-dos.html"));
536 ui_test_utils::NavigateToURL(browser(), url);
537 ASSERT_EQ(25, GetBlockedContentsCount());
540 // Verify that an onunload popup does not show up for about:blank.
541 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, Regress427477) {
542 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
543 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
545 GURL url(
546 embedded_test_server()->GetURL("/popup_blocker/popup-on-unload.html"));
547 ui_test_utils::NavigateToURL(browser(), url);
549 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
551 tab->GetController().GoBack();
552 content::WaitForLoadStop(tab);
554 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
555 browser()->host_desktop_type()));
556 ASSERT_EQ(1, browser()->tab_strip_model()->count());
558 // The popup from the unload event handler should not show up for about:blank.
559 ASSERT_EQ(0, GetBlockedContentsCount());
562 // Verify that app modal prompts can't be used to create pop unders.
563 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnder) {
564 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
565 GURL url(
566 embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
567 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
568 ContentSettingsPattern::FromURL(url), ContentSettingsPattern::Wildcard(),
569 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW);
571 NavigateAndCheckPopupShown(url, ExpectPopup);
573 Browser* popup_browser =
574 chrome::FindLastActiveWithHostDesktopType(browser()->host_desktop_type());
575 ASSERT_NE(popup_browser, browser());
577 // Showing an alert will raise the tab over the popup.
578 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
579 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
581 // Verify that after the dialog was closed, the popup is in front again.
582 ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
583 app_modal::JavaScriptAppModalDialog* js_dialog =
584 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
586 BrowserActivationObserver activation_observer(browser()->host_desktop_type());
587 js_dialog->native_dialog()->AcceptAppModalDialog();
589 if (popup_browser != chrome::FindLastActiveWithHostDesktopType(
590 popup_browser->host_desktop_type())) {
591 activation_observer.WaitForActivation();
593 ASSERT_EQ(popup_browser, chrome::FindLastActiveWithHostDesktopType(
594 popup_browser->host_desktop_type()));
597 void BuildSimpleWebKeyEvent(blink::WebInputEvent::Type type,
598 ui::KeyboardCode key_code,
599 int native_key_code,
600 int modifiers,
601 NativeWebKeyboardEvent* event) {
602 event->nativeKeyCode = native_key_code;
603 event->windowsKeyCode = key_code;
604 event->setKeyIdentifierFromWindowsKeyCode();
605 event->type = type;
606 event->modifiers = modifiers;
607 event->isSystemKey = false;
608 event->timeStampSeconds = base::Time::Now().ToDoubleT();
609 event->skip_in_browser = true;
611 if (type == blink::WebInputEvent::Char ||
612 type == blink::WebInputEvent::RawKeyDown) {
613 event->text[0] = key_code;
614 event->unmodifiedText[0] = key_code;
618 void InjectRawKeyEvent(WebContents* web_contents,
619 blink::WebInputEvent::Type type,
620 ui::KeyboardCode key_code,
621 int native_key_code,
622 int modifiers) {
623 NativeWebKeyboardEvent event;
624 BuildSimpleWebKeyEvent(type, key_code, native_key_code, modifiers, &event);
625 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(event);
628 // Tests that Ctrl+Enter/Cmd+Enter keys on a link open the backgournd tab.
629 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, CtrlEnterKey) {
630 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
632 GURL url(embedded_test_server()->GetURL(
633 "/popup_blocker/popup-simulated-click-on-anchor.html"));
634 ui_test_utils::NavigateToURL(browser(), url);
636 content::WindowedNotificationObserver wait_for_new_tab(
637 chrome::NOTIFICATION_TAB_ADDED,
638 content::NotificationService::AllSources());
640 #if defined(OS_MACOSX)
641 int modifiers = blink::WebInputEvent::MetaKey;
642 InjectRawKeyEvent(
643 tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_COMMAND,
644 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::OS_LEFT),
645 modifiers);
646 #else
647 int modifiers = blink::WebInputEvent::ControlKey;
648 InjectRawKeyEvent(
649 tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_CONTROL,
650 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::CONTROL_LEFT),
651 modifiers);
652 #endif
654 InjectRawKeyEvent(tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_RETURN,
655 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
657 InjectRawKeyEvent(tab, blink::WebInputEvent::Char, ui::VKEY_RETURN,
658 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
660 InjectRawKeyEvent(tab, blink::WebInputEvent::KeyUp, ui::VKEY_RETURN,
661 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
663 #if defined(OS_MACOSX)
664 InjectRawKeyEvent(
665 tab, blink::WebInputEvent::KeyUp, ui::VKEY_COMMAND,
666 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::OS_LEFT),
667 modifiers);
668 #else
669 InjectRawKeyEvent(
670 tab, blink::WebInputEvent::KeyUp, ui::VKEY_CONTROL,
671 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::CONTROL_LEFT),
672 modifiers);
673 #endif
674 wait_for_new_tab.Wait();
676 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
677 browser()->host_desktop_type()));
678 ASSERT_EQ(2, browser()->tab_strip_model()->count());
679 // Check that we create the background tab.
680 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
683 // Tests that the tapping gesture with cntl/cmd key on a link open the
684 // backgournd tab.
685 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, TapGestureWithCtrlKey) {
686 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
688 GURL url(embedded_test_server()->GetURL(
689 "/popup_blocker/popup-simulated-click-on-anchor2.html"));
690 ui_test_utils::NavigateToURL(browser(), url);
692 content::WindowedNotificationObserver wait_for_new_tab(
693 chrome::NOTIFICATION_TAB_ADDED,
694 content::NotificationService::AllSources());
696 #if defined(OS_MACOSX)
697 unsigned modifiers = blink::WebInputEvent::MetaKey;
698 #else
699 unsigned modifiers = blink::WebInputEvent::ControlKey;
700 #endif
701 content::SimulateTapWithModifiersAt(tab, modifiers, gfx::Point(350, 250));
703 wait_for_new_tab.Wait();
705 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
706 browser()->host_desktop_type()));
707 ASSERT_EQ(2, browser()->tab_strip_model()->count());
708 // Check that we create the background tab.
709 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
712 } // namespace