Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / blocked_content / popup_blocker_browsertest.cc
blob0aa4a7480e207b465600cce90de0c51056003bf5
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/host_content_settings_map_factory.h"
12 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/location_bar/location_bar.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/url_constants.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/browser/autocomplete_match.h"
33 #include "components/omnibox/browser/autocomplete_result.h"
34 #include "components/omnibox/browser/omnibox_edit_model.h"
35 #include "components/omnibox/browser/omnibox_view.h"
36 #include "content/public/browser/native_web_keyboard_event.h"
37 #include "content/public/browser/navigation_controller.h"
38 #include "content/public/browser/notification_registrar.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/render_frame_host.h"
41 #include "content/public/browser/render_view_host.h"
42 #include "content/public/browser/web_contents.h"
43 #include "content/public/browser/web_contents_observer.h"
44 #include "content/public/common/url_constants.h"
45 #include "content/public/test/browser_test_utils.h"
46 #include "content/public/test/test_navigation_observer.h"
47 #include "net/dns/mock_host_resolver.h"
48 #include "net/test/embedded_test_server/embedded_test_server.h"
49 #include "testing/gtest/include/gtest/gtest.h"
50 #include "ui/events/keycodes/dom/dom_code.h"
51 #include "ui/events/keycodes/dom/keycode_converter.h"
53 using content::WebContents;
54 using content::NativeWebKeyboardEvent;
56 namespace {
58 // Counts the number of RenderViewHosts created.
59 class CountRenderViewHosts : public content::NotificationObserver {
60 public:
61 CountRenderViewHosts()
62 : count_(0) {
63 registrar_.Add(this,
64 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
65 content::NotificationService::AllSources());
67 ~CountRenderViewHosts() override {}
69 int GetRenderViewHostCreatedCount() const { return count_; }
71 private:
72 void Observe(int type,
73 const content::NotificationSource& source,
74 const content::NotificationDetails& details) override {
75 count_++;
78 content::NotificationRegistrar registrar_;
80 int count_;
82 DISALLOW_COPY_AND_ASSIGN(CountRenderViewHosts);
85 class CloseObserver : public content::WebContentsObserver {
86 public:
87 explicit CloseObserver(WebContents* contents)
88 : content::WebContentsObserver(contents) {}
90 void Wait() {
91 close_loop_.Run();
94 void WebContentsDestroyed() override { close_loop_.Quit(); }
96 private:
97 base::RunLoop close_loop_;
99 DISALLOW_COPY_AND_ASSIGN(CloseObserver);
102 class BrowserActivationObserver : public chrome::BrowserListObserver {
103 public:
104 explicit BrowserActivationObserver(chrome::HostDesktopType desktop_type)
105 : browser_(chrome::FindLastActiveWithHostDesktopType(desktop_type)),
106 observed_(false) {
107 BrowserList::AddObserver(this);
109 ~BrowserActivationObserver() override { BrowserList::RemoveObserver(this); }
111 void WaitForActivation() {
112 if (observed_)
113 return;
114 message_loop_runner_ = new content::MessageLoopRunner;
115 message_loop_runner_->Run();
118 private:
119 // chrome::BrowserListObserver:
120 void OnBrowserSetLastActive(Browser* browser) override {
121 if (browser == browser_)
122 return;
123 if (browser->host_desktop_type() != browser_->host_desktop_type())
124 return;
125 observed_ = true;
126 if (message_loop_runner_.get() && message_loop_runner_->loop_running())
127 message_loop_runner_->Quit();
130 Browser* browser_;
131 bool observed_;
132 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
134 DISALLOW_COPY_AND_ASSIGN(BrowserActivationObserver);
137 class PopupBlockerBrowserTest : public InProcessBrowserTest {
138 public:
139 PopupBlockerBrowserTest() {}
140 ~PopupBlockerBrowserTest() override {}
142 void SetUpOnMainThread() override {
143 InProcessBrowserTest::SetUpOnMainThread();
145 host_resolver()->AddRule("*", "127.0.0.1");
146 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
149 int GetBlockedContentsCount() {
150 // Do a round trip to the renderer first to flush any in-flight IPCs to
151 // create a to-be-blocked window.
152 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
153 CHECK(content::ExecuteScript(tab, std::string()));
154 PopupBlockerTabHelper* popup_blocker_helper =
155 PopupBlockerTabHelper::FromWebContents(tab);
156 return popup_blocker_helper->GetBlockedPopupsCount();
159 enum WhatToExpect {
160 ExpectPopup,
161 ExpectTab
164 enum ShouldCheckTitle {
165 CheckTitle,
166 DontCheckTitle
169 void NavigateAndCheckPopupShown(const GURL& url,
170 WhatToExpect what_to_expect) {
171 content::WindowedNotificationObserver observer(
172 chrome::NOTIFICATION_TAB_ADDED,
173 content::NotificationService::AllSources());
174 ui_test_utils::NavigateToURL(browser(), url);
175 observer.Wait();
177 if (what_to_expect == ExpectPopup) {
178 ASSERT_EQ(2u,
179 chrome::GetBrowserCount(browser()->profile(),
180 browser()->host_desktop_type()));
181 } else {
182 ASSERT_EQ(1u,
183 chrome::GetBrowserCount(browser()->profile(),
184 browser()->host_desktop_type()));
185 ASSERT_EQ(2, browser()->tab_strip_model()->count());
187 // Check that we always create foreground tabs.
188 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
191 ASSERT_EQ(0, GetBlockedContentsCount());
194 // Navigates to the test indicated by |test_name| using |browser| which is
195 // expected to try to open a popup. Verifies that the popup was blocked and
196 // then opens the blocked popup. Once the popup stopped loading, verifies
197 // that the title of the page is "PASS" if |check_title| is set.
199 // If |what_to_expect| is ExpectPopup, the popup is expected to open a new
200 // window, or a background tab if it is false.
202 // Returns the WebContents of the launched popup.
203 WebContents* RunCheckTest(Browser* browser,
204 const std::string& test_name,
205 WhatToExpect what_to_expect,
206 ShouldCheckTitle check_title) {
207 GURL url(embedded_test_server()->GetURL(test_name));
209 CountRenderViewHosts counter;
211 ui_test_utils::NavigateToURL(browser, url);
213 // Since the popup blocker blocked the window.open, there should be only one
214 // tab.
215 EXPECT_EQ(1u, chrome::GetBrowserCount(browser->profile(),
216 browser->host_desktop_type()));
217 EXPECT_EQ(1, browser->tab_strip_model()->count());
218 WebContents* web_contents =
219 browser->tab_strip_model()->GetActiveWebContents();
220 EXPECT_EQ(url, web_contents->GetURL());
222 // And no new RVH created.
223 EXPECT_EQ(0, counter.GetRenderViewHostCreatedCount());
225 content::WindowedNotificationObserver observer(
226 chrome::NOTIFICATION_TAB_ADDED,
227 content::NotificationService::AllSources());
228 ui_test_utils::BrowserAddedObserver browser_observer;
230 // Launch the blocked popup.
231 PopupBlockerTabHelper* popup_blocker_helper =
232 PopupBlockerTabHelper::FromWebContents(web_contents);
233 if (!popup_blocker_helper->GetBlockedPopupsCount()) {
234 content::WindowedNotificationObserver observer(
235 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
236 content::NotificationService::AllSources());
237 observer.Wait();
239 EXPECT_EQ(1u, popup_blocker_helper->GetBlockedPopupsCount());
240 std::map<int32, GURL> blocked_requests =
241 popup_blocker_helper->GetBlockedPopupRequests();
242 std::map<int32, GURL>::const_iterator iter = blocked_requests.begin();
243 popup_blocker_helper->ShowBlockedPopup(iter->first);
245 observer.Wait();
246 Browser* new_browser;
247 if (what_to_expect == ExpectPopup) {
248 new_browser = browser_observer.WaitForSingleNewBrowser();
249 web_contents = new_browser->tab_strip_model()->GetActiveWebContents();
250 } else {
251 new_browser = browser;
252 EXPECT_EQ(2, browser->tab_strip_model()->count());
253 // Check that we always create foreground tabs.
254 EXPECT_EQ(1, browser->tab_strip_model()->active_index());
255 web_contents = browser->tab_strip_model()->GetWebContentsAt(1);
258 if (check_title == CheckTitle) {
259 // Check that the check passed.
260 base::string16 expected_title(base::ASCIIToUTF16("PASS"));
261 content::TitleWatcher title_watcher(web_contents, expected_title);
262 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
265 return web_contents;
268 private:
269 DISALLOW_COPY_AND_ASSIGN(PopupBlockerBrowserTest);
272 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
273 BlockWebContentsCreation) {
274 #if defined(OS_WIN) && defined(USE_ASH)
275 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
276 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
277 switches::kAshBrowserTests))
278 return;
279 #endif
281 RunCheckTest(
282 browser(),
283 "/popup_blocker/popup-blocked-to-post-blank.html",
284 ExpectTab,
285 DontCheckTitle);
288 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
289 BlockWebContentsCreationIncognito) {
290 #if defined(OS_WIN) && defined(USE_ASH)
291 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
292 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
293 switches::kAshBrowserTests))
294 return;
295 #endif
297 RunCheckTest(
298 CreateIncognitoBrowser(),
299 "/popup_blocker/popup-blocked-to-post-blank.html",
300 ExpectTab,
301 DontCheckTitle);
304 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
305 PopupBlockedFakeClickOnAnchor) {
306 #if defined(OS_WIN) && defined(USE_ASH)
307 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
308 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
309 switches::kAshBrowserTests))
310 return;
311 #endif
313 RunCheckTest(
314 browser(),
315 "/popup_blocker/popup-fake-click-on-anchor.html",
316 ExpectTab,
317 DontCheckTitle);
320 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
321 PopupBlockedFakeClickOnAnchorNoTarget) {
322 #if defined(OS_WIN) && defined(USE_ASH)
323 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
324 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
325 switches::kAshBrowserTests))
326 return;
327 #endif
329 RunCheckTest(
330 browser(),
331 "/popup_blocker/popup-fake-click-on-anchor2.html",
332 ExpectTab,
333 DontCheckTitle);
336 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MultiplePopups) {
337 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-many.html"));
338 ui_test_utils::NavigateToURL(browser(), url);
339 ASSERT_EQ(2, GetBlockedContentsCount());
342 // Verify that popups are launched on browser back button.
343 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
344 AllowPopupThroughContentSetting) {
345 GURL url(embedded_test_server()->GetURL(
346 "/popup_blocker/popup-blocked-to-post-blank.html"));
347 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
348 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
349 ContentSettingsPattern::Wildcard(),
350 CONTENT_SETTINGS_TYPE_POPUPS,
351 std::string(),
352 CONTENT_SETTING_ALLOW);
354 NavigateAndCheckPopupShown(url, ExpectTab);
357 // Verify that content settings are applied based on the top-level frame URL.
358 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
359 AllowPopupThroughContentSettingIFrame) {
360 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-frames.html"));
361 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
362 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
363 ContentSettingsPattern::Wildcard(),
364 CONTENT_SETTINGS_TYPE_POPUPS,
365 std::string(),
366 CONTENT_SETTING_ALLOW);
368 // Popup from the iframe should be allowed since the top-level URL is
369 // whitelisted.
370 NavigateAndCheckPopupShown(url, ExpectTab);
372 // Whitelist iframe URL instead.
373 GURL::Replacements replace_host;
374 replace_host.SetHostStr("www.a.com");
375 GURL frame_url(embedded_test_server()
376 ->GetURL("/popup_blocker/popup-frames-iframe.html")
377 .ReplaceComponents(replace_host));
378 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
379 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS);
380 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
381 ->SetContentSetting(ContentSettingsPattern::FromURL(frame_url),
382 ContentSettingsPattern::Wildcard(),
383 CONTENT_SETTINGS_TYPE_POPUPS,
384 std::string(),
385 CONTENT_SETTING_ALLOW);
387 // Popup should be blocked.
388 ui_test_utils::NavigateToURL(browser(), url);
389 ASSERT_EQ(1, GetBlockedContentsCount());
392 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
393 PopupsLaunchWhenTabIsClosed) {
394 base::CommandLine::ForCurrentProcess()->AppendSwitch(
395 switches::kDisablePopupBlocking);
396 GURL url(
397 embedded_test_server()->GetURL("/popup_blocker/popup-on-unload.html"));
398 ui_test_utils::NavigateToURL(browser(), url);
400 NavigateAndCheckPopupShown(embedded_test_server()->GetURL("/popup_blocker/"),
401 ExpectPopup);
404 // Verify that when you unblock popup, the popup shows in history and omnibox.
405 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest,
406 UnblockedPopupShowsInHistoryAndOmnibox) {
407 base::CommandLine::ForCurrentProcess()->AppendSwitch(
408 switches::kDisablePopupBlocking);
409 GURL url(embedded_test_server()->GetURL(
410 "/popup_blocker/popup-blocked-to-post-blank.html"));
411 NavigateAndCheckPopupShown(url, ExpectTab);
413 std::string search_string =
414 "data:text/html,<title>Popup Success!</title>you should not see this "
415 "message if popup blocker is enabled";
417 ui_test_utils::HistoryEnumerator history(browser()->profile());
418 std::vector<GURL>& history_urls = history.urls();
419 ASSERT_EQ(2u, history_urls.size());
420 ASSERT_EQ(GURL(search_string), history_urls[0]);
421 ASSERT_EQ(url, history_urls[1]);
423 TemplateURLService* service = TemplateURLServiceFactory::GetForProfile(
424 browser()->profile());
425 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
426 LocationBar* location_bar = browser()->window()->GetLocationBar();
427 ui_test_utils::SendToOmniboxAndSubmit(location_bar, search_string);
428 OmniboxEditModel* model = location_bar->GetOmniboxView()->model();
429 EXPECT_EQ(GURL(search_string), model->CurrentMatch(NULL).destination_url);
430 EXPECT_EQ(base::ASCIIToUTF16(search_string),
431 model->CurrentMatch(NULL).contents);
434 // This test fails on linux AURA with this change
435 // https://codereview.chromium.org/23903056
436 // BUG=https://code.google.com/p/chromium/issues/detail?id=295299
437 // TODO(ananta). Debug and fix this test.
438 #if defined(USE_AURA) && defined(OS_LINUX)
439 #define MAYBE_WindowFeatures DISABLED_WindowFeatures
440 #else
441 #define MAYBE_WindowFeatures WindowFeatures
442 #endif
443 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, MAYBE_WindowFeatures) {
444 WebContents* popup =
445 RunCheckTest(browser(),
446 "/popup_blocker/popup-window-open.html",
447 ExpectPopup,
448 DontCheckTitle);
450 // Check that the new popup has (roughly) the requested size.
451 gfx::Size window_size = popup->GetContainerBounds().size();
452 EXPECT_TRUE(349 <= window_size.width() && window_size.width() <= 351);
453 EXPECT_TRUE(249 <= window_size.height() && window_size.height() <= 251);
456 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, CorrectReferrer) {
457 RunCheckTest(browser(),
458 "/popup_blocker/popup-referrer.html",
459 ExpectTab,
460 CheckTitle);
463 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, WindowFeaturesBarProps) {
464 RunCheckTest(browser(),
465 "/popup_blocker/popup-windowfeatures.html",
466 ExpectPopup,
467 CheckTitle);
470 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, SessionStorage) {
471 RunCheckTest(browser(),
472 "/popup_blocker/popup-sessionstorage.html",
473 ExpectTab,
474 CheckTitle);
477 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, Opener) {
478 RunCheckTest(browser(),
479 "/popup_blocker/popup-opener.html",
480 ExpectTab,
481 CheckTitle);
484 // Tests that the popup can still close itself after navigating. This tests that
485 // the openedByDOM bit is preserved across blocked popups.
486 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ClosableAfterNavigation) {
487 // Open a popup.
488 WebContents* popup =
489 RunCheckTest(browser(),
490 "/popup_blocker/popup-opener.html",
491 ExpectTab,
492 CheckTitle);
494 // Navigate it elsewhere.
495 content::TestNavigationObserver nav_observer(popup);
496 popup->GetMainFrame()->ExecuteJavaScriptForTests(
497 base::UTF8ToUTF16("location.href = '/empty.html'"));
498 nav_observer.Wait();
500 // Have it close itself.
501 CloseObserver close_observer(popup);
502 popup->GetMainFrame()->ExecuteJavaScriptForTests(
503 base::UTF8ToUTF16("window.close()"));
504 close_observer.Wait();
507 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, OpenerSuppressed) {
508 RunCheckTest(browser(),
509 "/popup_blocker/popup-openersuppressed.html",
510 ExpectTab,
511 CheckTitle);
514 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ShiftClick) {
515 RunCheckTest(
516 browser(),
517 "/popup_blocker/popup-fake-click-on-anchor3.html",
518 ExpectPopup,
519 CheckTitle);
522 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, WebUI) {
523 WebContents* popup =
524 RunCheckTest(browser(),
525 "/popup_blocker/popup-webui.html",
526 ExpectTab,
527 DontCheckTitle);
529 // Check that the new popup displays about:blank.
530 EXPECT_EQ(GURL(url::kAboutBlankURL), popup->GetURL());
533 // Verify that the renderer can't DOS the browser by creating arbitrarily many
534 // popups.
535 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, DenialOfService) {
536 GURL url(embedded_test_server()->GetURL("/popup_blocker/popup-dos.html"));
537 ui_test_utils::NavigateToURL(browser(), url);
538 ASSERT_EQ(25, GetBlockedContentsCount());
541 // Verify that an onunload popup does not show up for about:blank.
542 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, Regress427477) {
543 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
544 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
546 GURL url(
547 embedded_test_server()->GetURL("/popup_blocker/popup-on-unload.html"));
548 ui_test_utils::NavigateToURL(browser(), url);
550 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
552 tab->GetController().GoBack();
553 content::WaitForLoadStop(tab);
555 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
556 browser()->host_desktop_type()));
557 ASSERT_EQ(1, browser()->tab_strip_model()->count());
559 // The popup from the unload event handler should not show up for about:blank.
560 ASSERT_EQ(0, GetBlockedContentsCount());
563 // Verify that app modal prompts can't be used to create pop unders.
564 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, ModalPopUnder) {
565 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
566 GURL url(
567 embedded_test_server()->GetURL("/popup_blocker/popup-window-open.html"));
568 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
569 ->SetContentSetting(ContentSettingsPattern::FromURL(url),
570 ContentSettingsPattern::Wildcard(),
571 CONTENT_SETTINGS_TYPE_POPUPS,
572 std::string(),
573 CONTENT_SETTING_ALLOW);
575 NavigateAndCheckPopupShown(url, ExpectPopup);
577 Browser* popup_browser =
578 chrome::FindLastActiveWithHostDesktopType(browser()->host_desktop_type());
579 ASSERT_NE(popup_browser, browser());
581 // Showing an alert will raise the tab over the popup.
582 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16("alert()"));
583 app_modal::AppModalDialog* dialog = ui_test_utils::WaitForAppModalDialog();
585 // Verify that after the dialog was closed, the popup is in front again.
586 ASSERT_TRUE(dialog->IsJavaScriptModalDialog());
587 app_modal::JavaScriptAppModalDialog* js_dialog =
588 static_cast<app_modal::JavaScriptAppModalDialog*>(dialog);
590 BrowserActivationObserver activation_observer(browser()->host_desktop_type());
591 js_dialog->native_dialog()->AcceptAppModalDialog();
593 if (popup_browser != chrome::FindLastActiveWithHostDesktopType(
594 popup_browser->host_desktop_type())) {
595 activation_observer.WaitForActivation();
597 ASSERT_EQ(popup_browser, chrome::FindLastActiveWithHostDesktopType(
598 popup_browser->host_desktop_type()));
601 void BuildSimpleWebKeyEvent(blink::WebInputEvent::Type type,
602 ui::KeyboardCode key_code,
603 int native_key_code,
604 int modifiers,
605 NativeWebKeyboardEvent* event) {
606 event->nativeKeyCode = native_key_code;
607 event->windowsKeyCode = key_code;
608 event->setKeyIdentifierFromWindowsKeyCode();
609 event->type = type;
610 event->modifiers = modifiers;
611 event->isSystemKey = false;
612 event->timeStampSeconds = base::Time::Now().ToDoubleT();
613 event->skip_in_browser = true;
615 if (type == blink::WebInputEvent::Char ||
616 type == blink::WebInputEvent::RawKeyDown) {
617 event->text[0] = key_code;
618 event->unmodifiedText[0] = key_code;
622 void InjectRawKeyEvent(WebContents* web_contents,
623 blink::WebInputEvent::Type type,
624 ui::KeyboardCode key_code,
625 int native_key_code,
626 int modifiers) {
627 NativeWebKeyboardEvent event;
628 BuildSimpleWebKeyEvent(type, key_code, native_key_code, modifiers, &event);
629 web_contents->GetRenderViewHost()->ForwardKeyboardEvent(event);
632 // Tests that Ctrl+Enter/Cmd+Enter keys on a link open the backgournd tab.
633 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, CtrlEnterKey) {
634 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
636 GURL url(embedded_test_server()->GetURL(
637 "/popup_blocker/popup-simulated-click-on-anchor.html"));
638 ui_test_utils::NavigateToURL(browser(), url);
640 content::WindowedNotificationObserver wait_for_new_tab(
641 chrome::NOTIFICATION_TAB_ADDED,
642 content::NotificationService::AllSources());
644 #if defined(OS_MACOSX)
645 int modifiers = blink::WebInputEvent::MetaKey;
646 InjectRawKeyEvent(
647 tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_COMMAND,
648 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::OS_LEFT),
649 modifiers);
650 #else
651 int modifiers = blink::WebInputEvent::ControlKey;
652 InjectRawKeyEvent(
653 tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_CONTROL,
654 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::CONTROL_LEFT),
655 modifiers);
656 #endif
658 InjectRawKeyEvent(tab, blink::WebInputEvent::RawKeyDown, ui::VKEY_RETURN,
659 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
661 InjectRawKeyEvent(tab, blink::WebInputEvent::Char, ui::VKEY_RETURN,
662 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
664 InjectRawKeyEvent(tab, blink::WebInputEvent::KeyUp, ui::VKEY_RETURN,
665 ui::KeycodeConverter::InvalidNativeKeycode(), modifiers);
667 #if defined(OS_MACOSX)
668 InjectRawKeyEvent(
669 tab, blink::WebInputEvent::KeyUp, ui::VKEY_COMMAND,
670 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::OS_LEFT),
671 modifiers);
672 #else
673 InjectRawKeyEvent(
674 tab, blink::WebInputEvent::KeyUp, ui::VKEY_CONTROL,
675 ui::KeycodeConverter::DomCodeToNativeKeycode(ui::DomCode::CONTROL_LEFT),
676 modifiers);
677 #endif
678 wait_for_new_tab.Wait();
680 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
681 browser()->host_desktop_type()));
682 ASSERT_EQ(2, browser()->tab_strip_model()->count());
683 // Check that we create the background tab.
684 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
687 // Tests that the tapping gesture with cntl/cmd key on a link open the
688 // backgournd tab.
689 IN_PROC_BROWSER_TEST_F(PopupBlockerBrowserTest, TapGestureWithCtrlKey) {
690 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
692 GURL url(embedded_test_server()->GetURL(
693 "/popup_blocker/popup-simulated-click-on-anchor2.html"));
694 ui_test_utils::NavigateToURL(browser(), url);
696 content::WindowedNotificationObserver wait_for_new_tab(
697 chrome::NOTIFICATION_TAB_ADDED,
698 content::NotificationService::AllSources());
700 #if defined(OS_MACOSX)
701 unsigned modifiers = blink::WebInputEvent::MetaKey;
702 #else
703 unsigned modifiers = blink::WebInputEvent::ControlKey;
704 #endif
705 content::SimulateTapWithModifiersAt(tab, modifiers, gfx::Point(350, 250));
707 wait_for_new_tab.Wait();
709 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
710 browser()->host_desktop_type()));
711 ASSERT_EQ(2, browser()->tab_strip_model()->count());
712 // Check that we create the background tab.
713 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
716 } // namespace