| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete_match.h" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 | 224 |
| 225 class BetterPopupBlockerBrowserTest : public PopupBlockerBrowserTest { | 225 class BetterPopupBlockerBrowserTest : public PopupBlockerBrowserTest { |
| 226 public: | 226 public: |
| 227 BetterPopupBlockerBrowserTest() {} | 227 BetterPopupBlockerBrowserTest() {} |
| 228 virtual ~BetterPopupBlockerBrowserTest() {} | 228 virtual ~BetterPopupBlockerBrowserTest() {} |
| 229 | 229 |
| 230 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 230 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 231 InProcessBrowserTest::SetUpCommandLine(command_line); | 231 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // Navigates to the test indicated by |test_name| which is expected to try to |
| 235 // open a popup. Verifies that the popup was blocked and then opens the |
| 236 // blocked popup. Once the popup stopped loading, verifies that the title of |
| 237 // the page is "PASS". |
| 238 // |
| 239 // If |expect_new_browser| is true, the popup is expected to open a new |
| 240 // window, or a background tab if it is false. |
| 241 void RunCheckTest(const base::FilePath& test_name, bool expect_new_browser) { |
| 242 GURL url(ui_test_utils::GetTestUrl(base::FilePath(kTestDir), test_name)); |
| 243 |
| 244 CountRenderViewHosts counter; |
| 245 |
| 246 ui_test_utils::NavigateToURL(browser(), url); |
| 247 |
| 248 // If the popup blocker blocked the window.open, there should be only one |
| 249 // tab. |
| 250 EXPECT_EQ(1u, chrome::GetBrowserCount(browser()->profile(), |
| 251 browser()->host_desktop_type())); |
| 252 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 253 WebContents* web_contents = |
| 254 browser()->tab_strip_model()->GetActiveWebContents(); |
| 255 EXPECT_EQ(url, web_contents->GetURL()); |
| 256 |
| 257 // And no new RVH created. |
| 258 EXPECT_EQ(0, counter.GetRenderViewHostCreatedCount()); |
| 259 |
| 260 content::WindowedNotificationObserver observer( |
| 261 chrome::NOTIFICATION_TAB_ADDED, |
| 262 content::NotificationService::AllSources()); |
| 263 ui_test_utils::BrowserAddedObserver browser_observer; |
| 264 |
| 265 // Launch the blocked popup. |
| 266 PopupBlockerTabHelper* popup_blocker_helper = |
| 267 PopupBlockerTabHelper::FromWebContents(web_contents); |
| 268 EXPECT_EQ(1u, popup_blocker_helper->GetBlockedPopupsCount()); |
| 269 std::map<int32, GURL> blocked_requests = |
| 270 popup_blocker_helper->GetBlockedPopupRequests(); |
| 271 std::map<int32, GURL>::const_iterator iter = blocked_requests.begin(); |
| 272 popup_blocker_helper->ShowBlockedPopup(iter->first); |
| 273 |
| 274 observer.Wait(); |
| 275 Browser* new_browser; |
| 276 if (expect_new_browser) { |
| 277 new_browser = browser_observer.WaitForSingleNewBrowser(); |
| 278 web_contents = new_browser->tab_strip_model()->GetActiveWebContents(); |
| 279 } else { |
| 280 new_browser = browser(); |
| 281 EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
| 282 web_contents = browser()->tab_strip_model()->GetWebContentsAt(1); |
| 283 } |
| 284 |
| 285 // Check that the check passed. |
| 286 base::string16 expected_title(base::ASCIIToUTF16("PASS")); |
| 287 content::TitleWatcher title_watcher(web_contents, expected_title); |
| 288 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
| 289 } |
| 290 |
| 234 private: | 291 private: |
| 235 DISALLOW_COPY_AND_ASSIGN(BetterPopupBlockerBrowserTest); | 292 DISALLOW_COPY_AND_ASSIGN(BetterPopupBlockerBrowserTest); |
| 236 }; | 293 }; |
| 237 | 294 |
| 238 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, | 295 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, |
| 239 BlockWebContentsCreation) { | 296 BlockWebContentsCreation) { |
| 240 #if defined(OS_WIN) && defined(USE_ASH) | 297 #if defined(OS_WIN) && defined(USE_ASH) |
| 241 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 298 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
| 242 if (base::win::GetVersion() >= base::win::VERSION_WIN8) | 299 if (base::win::GetVersion() >= base::win::VERSION_WIN8) |
| 243 return; | 300 return; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 Browser* new_browser = browser_observer.WaitForSingleNewBrowser(); | 422 Browser* new_browser = browser_observer.WaitForSingleNewBrowser(); |
| 366 | 423 |
| 367 // Check that the new popup has (roughly) the requested size. | 424 // Check that the new popup has (roughly) the requested size. |
| 368 web_contents = new_browser->tab_strip_model()->GetActiveWebContents(); | 425 web_contents = new_browser->tab_strip_model()->GetActiveWebContents(); |
| 369 gfx::Size window_size = web_contents->GetView()->GetContainerSize(); | 426 gfx::Size window_size = web_contents->GetView()->GetContainerSize(); |
| 370 EXPECT_TRUE(349 <= window_size.width() && window_size.width() <= 351); | 427 EXPECT_TRUE(349 <= window_size.width() && window_size.width() <= 351); |
| 371 EXPECT_TRUE(249 <= window_size.height() && window_size.height() <= 251); | 428 EXPECT_TRUE(249 <= window_size.height() && window_size.height() <= 251); |
| 372 } | 429 } |
| 373 | 430 |
| 374 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, CorrectReferrer) { | 431 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, CorrectReferrer) { |
| 375 GURL url(ui_test_utils::GetTestUrl( | 432 RunCheckTest(base::FilePath(FILE_PATH_LITERAL("popup-referrer.html")), true); |
| 376 base::FilePath(kTestDir), | |
| 377 base::FilePath(FILE_PATH_LITERAL("popup-referrer.html")))); | |
| 378 | |
| 379 CountRenderViewHosts counter; | |
| 380 | |
| 381 ui_test_utils::NavigateToURL(browser(), url); | |
| 382 | |
| 383 // If the popup blocker blocked the blank post, there should be only one tab. | |
| 384 EXPECT_EQ(1u, chrome::GetBrowserCount(browser()->profile(), | |
| 385 browser()->host_desktop_type())); | |
| 386 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
| 387 WebContents* web_contents = | |
| 388 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 389 EXPECT_EQ(url, web_contents->GetURL()); | |
| 390 | |
| 391 // And no new RVH created. | |
| 392 EXPECT_EQ(0, counter.GetRenderViewHostCreatedCount()); | |
| 393 | |
| 394 content::WindowedNotificationObserver observer( | |
| 395 chrome::NOTIFICATION_TAB_ADDED, | |
| 396 content::NotificationService::AllSources()); | |
| 397 ui_test_utils::BrowserAddedObserver browser_observer; | |
| 398 | |
| 399 // Launch the blocked popup. | |
| 400 PopupBlockerTabHelper* popup_blocker_helper = | |
| 401 PopupBlockerTabHelper::FromWebContents(web_contents); | |
| 402 EXPECT_EQ(1u, popup_blocker_helper->GetBlockedPopupsCount()); | |
| 403 std::map<int32, GURL> blocked_requests = | |
| 404 popup_blocker_helper->GetBlockedPopupRequests(); | |
| 405 std::map<int32, GURL>::const_iterator iter = blocked_requests.begin(); | |
| 406 popup_blocker_helper->ShowBlockedPopup(iter->first); | |
| 407 | |
| 408 observer.Wait(); | |
| 409 Browser* new_browser = browser_observer.WaitForSingleNewBrowser(); | |
| 410 | |
| 411 // Check that the referrer was correctly set. | |
| 412 web_contents = new_browser->tab_strip_model()->GetActiveWebContents(); | |
| 413 base::string16 expected_title(base::ASCIIToUTF16("PASS")); | |
| 414 content::TitleWatcher title_watcher(web_contents, expected_title); | |
| 415 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); | |
| 416 } | 433 } |
| 417 | 434 |
| 418 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, WindowFeaturesBarProps) { | 435 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, WindowFeaturesBarProps) { |
| 419 GURL url(ui_test_utils::GetTestUrl( | 436 RunCheckTest(base::FilePath(FILE_PATH_LITERAL("popup-windowfeatures.html")), |
| 420 base::FilePath(kTestDir), | 437 true); |
| 421 base::FilePath(FILE_PATH_LITERAL("popup-windowfeatures.html")))); | 438 } |
| 422 | 439 |
| 423 CountRenderViewHosts counter; | 440 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, SessionStorage) { |
| 441 RunCheckTest(base::FilePath(FILE_PATH_LITERAL("popup-sessionstorage.html")), |
| 442 true); |
| 443 } |
| 424 | 444 |
| 425 ui_test_utils::NavigateToURL(browser(), url); | 445 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, Opener) { |
| 446 RunCheckTest(base::FilePath(FILE_PATH_LITERAL("popup-opener.html")), true); |
| 447 } |
| 426 | 448 |
| 427 // If the popup blocker blocked the blank post, there should be only one tab. | 449 IN_PROC_BROWSER_TEST_F(BetterPopupBlockerBrowserTest, OpenerSuppressed) { |
| 428 EXPECT_EQ(1u, chrome::GetBrowserCount(browser()->profile(), | 450 RunCheckTest( |
| 429 browser()->host_desktop_type())); | 451 base::FilePath(FILE_PATH_LITERAL("popup-openersuppressed.html")), false); |
| 430 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
| 431 WebContents* web_contents = | |
| 432 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 433 EXPECT_EQ(url, web_contents->GetURL()); | |
| 434 | |
| 435 // And no new RVH created. | |
| 436 EXPECT_EQ(0, counter.GetRenderViewHostCreatedCount()); | |
| 437 | |
| 438 content::WindowedNotificationObserver observer( | |
| 439 chrome::NOTIFICATION_TAB_ADDED, | |
| 440 content::NotificationService::AllSources()); | |
| 441 ui_test_utils::BrowserAddedObserver browser_observer; | |
| 442 | |
| 443 // Launch the blocked popup. | |
| 444 PopupBlockerTabHelper* popup_blocker_helper = | |
| 445 PopupBlockerTabHelper::FromWebContents(web_contents); | |
| 446 EXPECT_EQ(1u, popup_blocker_helper->GetBlockedPopupsCount()); | |
| 447 std::map<int32, GURL> blocked_requests = | |
| 448 popup_blocker_helper->GetBlockedPopupRequests(); | |
| 449 std::map<int32, GURL>::const_iterator iter = blocked_requests.begin(); | |
| 450 popup_blocker_helper->ShowBlockedPopup(iter->first); | |
| 451 | |
| 452 observer.Wait(); | |
| 453 Browser* new_browser = browser_observer.WaitForSingleNewBrowser(); | |
| 454 | |
| 455 // Check that the referrer was correctly set. | |
| 456 web_contents = new_browser->tab_strip_model()->GetActiveWebContents(); | |
| 457 base::string16 expected_title(base::ASCIIToUTF16("PASS")); | |
| 458 content::TitleWatcher title_watcher(web_contents, expected_title); | |
| 459 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); | |
| 460 } | 452 } |
| 461 | 453 |
| 462 } // namespace | 454 } // namespace |
| OLD | NEW |