| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/ui/browser_navigator_browsertest.h" |
| 6 |
| 5 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 6 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 7 #include "chrome/browser/tab_contents/tab_contents.h" | 9 #include "chrome/browser/tab_contents/tab_contents.h" |
| 8 #include "chrome/browser/tab_contents/tab_contents_view.h" | 10 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 9 #include "chrome/browser/tabs/tab_strip_model.h" | 11 #include "chrome/browser/tabs/tab_strip_model.h" |
| 10 #include "chrome/browser/ui/browser.h" | |
| 11 #include "chrome/browser/ui/browser_list.h" | 12 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/browser/ui/browser_navigator.h" | 13 #include "chrome/browser/ui/browser_navigator.h" |
| 13 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 15 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/test/in_process_browser_test.h" | |
| 17 #include "chrome/test/ui_test_utils.h" | 17 #include "chrome/test/ui_test_utils.h" |
| 18 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 19 | 19 |
| 20 GURL BrowserNavigatorTest::GetGoogleURL() const { |
| 21 return GURL("http://www.google.com/"); |
| 22 } |
| 23 |
| 24 browser::NavigateParams BrowserNavigatorTest::MakeNavigateParams() const { |
| 25 return MakeNavigateParams(browser()); |
| 26 } |
| 27 |
| 28 browser::NavigateParams BrowserNavigatorTest::MakeNavigateParams( |
| 29 Browser* browser) const { |
| 30 browser::NavigateParams params(browser, GetGoogleURL(), |
| 31 PageTransition::LINK); |
| 32 params.show_window = true; |
| 33 return params; |
| 34 } |
| 35 |
| 36 Browser* BrowserNavigatorTest::CreateEmptyBrowserForType(Browser::Type type, |
| 37 Profile* profile) { |
| 38 Browser* browser = Browser::CreateForType(type, profile); |
| 39 browser->AddBlankTab(true); |
| 40 return browser; |
| 41 } |
| 42 |
| 43 TabContentsWrapper* BrowserNavigatorTest::CreateTabContents() { |
| 44 return Browser::TabContentsFactory( |
| 45 browser()->profile(), |
| 46 NULL, |
| 47 MSG_ROUTING_NONE, |
| 48 browser()->GetSelectedTabContents(), |
| 49 NULL); |
| 50 } |
| 51 |
| 52 void BrowserNavigatorTest::RunSuppressTest(WindowOpenDisposition disposition) { |
| 53 GURL old_url = browser()->GetSelectedTabContents()->GetURL(); |
| 54 browser::NavigateParams p(MakeNavigateParams()); |
| 55 p.disposition = disposition; |
| 56 browser::Navigate(&p); |
| 57 |
| 58 // Nothing should have happened as a result of Navigate(); |
| 59 EXPECT_EQ(1, browser()->tab_count()); |
| 60 EXPECT_EQ(1u, BrowserList::size()); |
| 61 EXPECT_EQ(old_url, browser()->GetSelectedTabContents()->GetURL()); |
| 62 } |
| 63 |
| 64 void BrowserNavigatorTest::Observe(NotificationType type, |
| 65 const NotificationSource& source, |
| 66 const NotificationDetails& details) { |
| 67 switch (type.value) { |
| 68 case NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB: { |
| 69 ++this->created_tab_contents_count_; |
| 70 break; |
| 71 } |
| 72 default: |
| 73 break; |
| 74 } |
| 75 } |
| 76 |
| 20 namespace { | 77 namespace { |
| 21 | 78 |
| 22 class BrowserNavigatorTest : public InProcessBrowserTest, | |
| 23 public NotificationObserver { | |
| 24 protected: | |
| 25 GURL GetGoogleURL() const { | |
| 26 return GURL("http://www.google.com/"); | |
| 27 } | |
| 28 | |
| 29 browser::NavigateParams MakeNavigateParams() const { | |
| 30 return MakeNavigateParams(browser()); | |
| 31 } | |
| 32 browser::NavigateParams MakeNavigateParams(Browser* browser) const { | |
| 33 browser::NavigateParams params(browser, GetGoogleURL(), | |
| 34 PageTransition::LINK); | |
| 35 params.show_window = true; | |
| 36 return params; | |
| 37 } | |
| 38 | |
| 39 Browser* CreateEmptyBrowserForType(Browser::Type type, Profile* profile) { | |
| 40 Browser* browser = Browser::CreateForType(type, profile); | |
| 41 browser->AddBlankTab(true); | |
| 42 return browser; | |
| 43 } | |
| 44 | |
| 45 TabContentsWrapper* CreateTabContents() { | |
| 46 return Browser::TabContentsFactory( | |
| 47 browser()->profile(), | |
| 48 NULL, | |
| 49 MSG_ROUTING_NONE, | |
| 50 browser()->GetSelectedTabContents(), | |
| 51 NULL); | |
| 52 } | |
| 53 | |
| 54 void RunSuppressTest(WindowOpenDisposition disposition) { | |
| 55 GURL old_url = browser()->GetSelectedTabContents()->GetURL(); | |
| 56 browser::NavigateParams p(MakeNavigateParams()); | |
| 57 p.disposition = disposition; | |
| 58 browser::Navigate(&p); | |
| 59 | |
| 60 // Nothing should have happened as a result of Navigate(); | |
| 61 EXPECT_EQ(1, browser()->tab_count()); | |
| 62 EXPECT_EQ(1u, BrowserList::size()); | |
| 63 EXPECT_EQ(old_url, browser()->GetSelectedTabContents()->GetURL()); | |
| 64 } | |
| 65 | |
| 66 void Observe(NotificationType type, const NotificationSource& source, | |
| 67 const NotificationDetails& details) { | |
| 68 switch (type.value) { | |
| 69 case NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB: { | |
| 70 ++this->created_tab_contents_count_; | |
| 71 break; | |
| 72 } | |
| 73 default: | |
| 74 break; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 size_t created_tab_contents_count_; | |
| 79 }; | |
| 80 | |
| 81 // This test verifies that when a navigation occurs within a tab, the tab count | 79 // This test verifies that when a navigation occurs within a tab, the tab count |
| 82 // of the Browser remains the same and the current tab bears the loaded URL. | 80 // of the Browser remains the same and the current tab bears the loaded URL. |
| 83 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_CurrentTab) { | 81 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_CurrentTab) { |
| 84 browser::NavigateParams p(MakeNavigateParams()); | 82 browser::NavigateParams p(MakeNavigateParams()); |
| 85 browser::Navigate(&p); | 83 browser::Navigate(&p); |
| 86 ui_test_utils::WaitForNavigationInCurrentTab(browser()); | 84 ui_test_utils::WaitForNavigationInCurrentTab(browser()); |
| 87 EXPECT_EQ(GetGoogleURL(), browser()->GetSelectedTabContents()->GetURL()); | 85 EXPECT_EQ(GetGoogleURL(), browser()->GetSelectedTabContents()->GetURL()); |
| 88 // We should have one window with one tab. | 86 // We should have one window with one tab. |
| 89 EXPECT_EQ(1u, BrowserList::size()); | 87 EXPECT_EQ(1u, BrowserList::size()); |
| 90 EXPECT_EQ(1, browser()->tab_count()); | 88 EXPECT_EQ(1, browser()->tab_count()); |
| 91 } | 89 } |
| 92 | 90 |
| 93 // This test verifies that a singleton tab is refocused if one is already open | 91 // This test verifies that a singleton tab is refocused if one is already opened |
| 94 // in another or an existing window, or added if it is not. | 92 // in another or an existing window, or added if it is not. |
| 95 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_SingletonTabExisting) { | 93 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, Disposition_SingletonTabExisting) { |
| 96 GURL url("http://www.google.com/"); | 94 GURL url("http://www.google.com/"); |
| 97 GURL singleton_url1("http://maps.google.com/"); | 95 GURL singleton_url1("http://maps.google.com/"); |
| 98 | 96 |
| 99 // Register for a notification if an additional tab_contents was instantiated. | 97 // Register for a notification if an additional tab_contents was instantiated. |
| 100 // Opening a Singleton tab that is already open should not be opening a new | 98 // Opening a Singleton tab that is already opened should not be opening a new |
| 101 // tab nor be creating a new TabContents object | 99 // tab nor be creating a new TabContents object |
| 102 NotificationRegistrar registrar; | 100 NotificationRegistrar registrar; |
| 103 | 101 |
| 104 // As the registrar object goes out of scope, this will get unregistered | 102 // As the registrar object goes out of scope, this will get unregistered |
| 105 registrar.Add(this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, | 103 registrar.Add(this, NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB, |
| 106 NotificationService::AllSources()); | 104 NotificationService::AllSources()); |
| 107 | 105 |
| 108 browser()->AddSelectedTabWithURL(singleton_url1, PageTransition::LINK); | 106 browser()->AddSelectedTabWithURL(singleton_url1, PageTransition::LINK); |
| 109 browser()->AddSelectedTabWithURL(url, PageTransition::LINK); | 107 browser()->AddSelectedTabWithURL(url, PageTransition::LINK); |
| 110 | 108 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 128 // No tab contents should have been created | 126 // No tab contents should have been created |
| 129 EXPECT_EQ(previous_tab_contents_count, | 127 EXPECT_EQ(previous_tab_contents_count, |
| 130 created_tab_contents_count_); | 128 created_tab_contents_count_); |
| 131 } | 129 } |
| 132 | 130 |
| 133 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, | 131 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 134 Disposition_SingletonTabNoneExisting) { | 132 Disposition_SingletonTabNoneExisting) { |
| 135 GURL url("http://www.google.com/"); | 133 GURL url("http://www.google.com/"); |
| 136 GURL singleton_url1("http://maps.google.com/"); | 134 GURL singleton_url1("http://maps.google.com/"); |
| 137 | 135 |
| 138 // We should have one browser with 3 tabs, the 3rd selected. | 136 // We should have one browser with 1 tab. |
| 139 EXPECT_EQ(1u, BrowserList::size()); | 137 EXPECT_EQ(1u, BrowserList::size()); |
| 140 EXPECT_EQ(0, browser()->selected_index()); | 138 EXPECT_EQ(0, browser()->selected_index()); |
| 141 | 139 |
| 142 // Navigate to singleton_url1. | 140 // Navigate to singleton_url1. |
| 143 browser::NavigateParams p(MakeNavigateParams()); | 141 browser::NavigateParams p(MakeNavigateParams()); |
| 144 p.disposition = SINGLETON_TAB; | 142 p.disposition = SINGLETON_TAB; |
| 145 p.url = singleton_url1; | 143 p.url = singleton_url1; |
| 146 browser::Navigate(&p); | 144 browser::Navigate(&p); |
| 147 | 145 |
| 148 // We should now have 2 tabs, the 2nd one selected. | 146 // We should now have 2 tabs, the 2nd one selected. |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 browser::Navigate(&p); | 672 browser::Navigate(&p); |
| 675 | 673 |
| 676 // The second tab should still be selected, but navigated to the new path. | 674 // The second tab should still be selected, but navigated to the new path. |
| 677 EXPECT_EQ(browser(), p.browser); | 675 EXPECT_EQ(browser(), p.browser); |
| 678 EXPECT_EQ(2, browser()->tab_count()); | 676 EXPECT_EQ(2, browser()->tab_count()); |
| 679 EXPECT_EQ(1, browser()->selected_index()); | 677 EXPECT_EQ(1, browser()->selected_index()); |
| 680 EXPECT_EQ(singleton_url_target, | 678 EXPECT_EQ(singleton_url_target, |
| 681 browser()->GetSelectedTabContents()->GetURL()); | 679 browser()->GetSelectedTabContents()->GetURL()); |
| 682 } | 680 } |
| 683 | 681 |
| 682 // This test verifies that the settings page isn't opened in the incognito |
| 683 // window. |
| 684 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 685 Disposition_Settings_UseNonIncognitoWindow) { |
| 686 Browser* incognito_browser = CreateIncognitoBrowser(); |
| 687 |
| 688 EXPECT_EQ(2u, BrowserList::size()); |
| 689 EXPECT_EQ(1, browser()->tab_count()); |
| 690 EXPECT_EQ(1, incognito_browser->tab_count()); |
| 691 |
| 692 // Navigate to the settings page. |
| 693 browser::NavigateParams p(MakeNavigateParams(incognito_browser)); |
| 694 p.disposition = SINGLETON_TAB; |
| 695 p.url = GURL("chrome://settings"); |
| 696 p.show_window = true; |
| 697 p.ignore_path = true; |
| 698 browser::Navigate(&p); |
| 699 |
| 700 // The settings page should be opened in browser() window. |
| 701 EXPECT_NE(incognito_browser, p.browser); |
| 702 EXPECT_EQ(browser(), p.browser); |
| 703 EXPECT_EQ(2, browser()->tab_count()); |
| 704 EXPECT_EQ(GURL("chrome://settings"), |
| 705 browser()->GetSelectedTabContents()->GetURL()); |
| 706 } |
| 707 |
| 708 // This test verifies that the bookmarks page isn't opened in the incognito |
| 709 // window. |
| 710 IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, |
| 711 Disposition_Bookmarks_UseNonIncognitoWindow) { |
| 712 Browser* incognito_browser = CreateIncognitoBrowser(); |
| 713 |
| 714 EXPECT_EQ(2u, BrowserList::size()); |
| 715 EXPECT_EQ(1, browser()->tab_count()); |
| 716 EXPECT_EQ(1, incognito_browser->tab_count()); |
| 717 |
| 718 // Navigate to the settings page. |
| 719 browser::NavigateParams p(MakeNavigateParams(incognito_browser)); |
| 720 p.disposition = SINGLETON_TAB; |
| 721 p.url = GURL("chrome://bookmarks"); |
| 722 p.show_window = true; |
| 723 p.ignore_path = true; |
| 724 browser::Navigate(&p); |
| 725 |
| 726 // The bookmarks page should be opened in browser() window. |
| 727 EXPECT_NE(incognito_browser, p.browser); |
| 728 EXPECT_EQ(browser(), p.browser); |
| 729 EXPECT_EQ(2, browser()->tab_count()); |
| 730 EXPECT_EQ(GURL("chrome://bookmarks"), |
| 731 browser()->GetSelectedTabContents()->GetURL()); |
| 732 } |
| 733 |
| 684 } // namespace | 734 } // namespace |
| OLD | NEW |