[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 1a3aba8 | 2010-11-08 23:52:54 | [diff] [blame] | 5 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 6 | #include "chrome/browser/bookmarks/bookmark_model.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 7 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 8 | #include "chrome/common/url_constants.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 9 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 10 | #include "chrome/test/base/testing_profile.h" |
[email protected] | 1bda9755 | 2011-03-01 20:11:52 | [diff] [blame] | 11 | #include "content/browser/tab_contents/navigation_controller.h" |
| 12 | #include "content/browser/tab_contents/navigation_entry.h" |
| 13 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 14 | #include "content/test/test_browser_thread.h" |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 15 | |
| 16 | typedef BrowserWithTestWindowTest BrowserCommandsTest; |
| 17 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame^] | 18 | using content::OpenURLParams; |
| 19 | using content::Referrer; |
| 20 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 21 | // Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and |
| 22 | // IDC_SELECT_LAST_TAB. |
[email protected] | 6df64208 | 2009-08-13 22:31:37 | [diff] [blame] | 23 | TEST_F(BrowserCommandsTest, TabNavigationAccelerators) { |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 24 | GURL about_blank(chrome::kAboutBlankURL); |
| 25 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 26 | // Create three tabs. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 27 | AddTab(browser(), about_blank); |
| 28 | AddTab(browser(), about_blank); |
| 29 | AddTab(browser(), about_blank); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 30 | |
| 31 | // Select the second tab. |
[email protected] | eaca0ad1 | 2011-04-18 15:53:41 | [diff] [blame] | 32 | browser()->ActivateTabAt(1, false); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 33 | |
| 34 | // Navigate to the first tab using an accelerator. |
| 35 | browser()->ExecuteCommand(IDC_SELECT_TAB_0); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 36 | ASSERT_EQ(0, browser()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 37 | |
| 38 | // Navigate to the second tab using the next accelerators. |
| 39 | browser()->ExecuteCommand(IDC_SELECT_NEXT_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 40 | ASSERT_EQ(1, browser()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 41 | |
| 42 | // Navigate back to the first tab using the previous accelerators. |
| 43 | browser()->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 44 | ASSERT_EQ(0, browser()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 45 | |
| 46 | // Navigate to the last tab using the select last accelerator. |
| 47 | browser()->ExecuteCommand(IDC_SELECT_LAST_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 48 | ASSERT_EQ(2, browser()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Tests IDC_DUPLICATE_TAB. |
| 52 | TEST_F(BrowserCommandsTest, DuplicateTab) { |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 53 | GURL url1("http://foo/1"); |
| 54 | GURL url2("http://foo/2"); |
| 55 | GURL url3("http://foo/3"); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 56 | |
| 57 | // Navigate to the three urls, then go back. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 58 | AddTab(browser(), url1); |
[email protected] | 0683cf6 | 2009-04-10 19:31:40 | [diff] [blame] | 59 | NavigateAndCommitActiveTab(url2); |
| 60 | NavigateAndCommitActiveTab(url3); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 61 | |
| 62 | size_t initial_window_count = BrowserList::size(); |
| 63 | |
| 64 | // Duplicate the tab. |
| 65 | browser()->ExecuteCommand(IDC_DUPLICATE_TAB); |
| 66 | |
| 67 | // The duplicated tab should not end up in a new window. |
[email protected] | a0a8b1d | 2009-03-11 14:57:22 | [diff] [blame] | 68 | size_t window_count = BrowserList::size(); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 69 | ASSERT_EQ(initial_window_count, window_count); |
| 70 | |
| 71 | // And we should have a newly duplicated tab. |
| 72 | ASSERT_EQ(2, browser()->tab_count()); |
| 73 | |
| 74 | // Verify the stack of urls. |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 75 | NavigationController& controller = |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 76 | browser()->GetTabContentsAt(1)->GetController(); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 77 | ASSERT_EQ(3, controller.entry_count()); |
| 78 | ASSERT_EQ(2, controller.GetCurrentEntryIndex()); |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 79 | ASSERT_TRUE(url1 == controller.GetEntryAtIndex(0)->GetURL()); |
| 80 | ASSERT_TRUE(url2 == controller.GetEntryAtIndex(1)->GetURL()); |
| 81 | ASSERT_TRUE(url3 == controller.GetEntryAtIndex(2)->GetURL()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 82 | } |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 83 | |
| 84 | TEST_F(BrowserCommandsTest, BookmarkCurrentPage) { |
| 85 | // We use profile() here, since it's a TestingProfile. |
| 86 | profile()->CreateBookmarkModel(true); |
| 87 | profile()->BlockUntilBookmarkModelLoaded(); |
| 88 | |
| 89 | // Navigate to a url. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 90 | GURL url1("http://foo/1"); |
| 91 | AddTab(browser(), url1); |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame^] | 92 | browser()->OpenURL(OpenURLParams( |
| 93 | url1, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 94 | |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 95 | // TODO(beng): remove this once we can use TabContentses directly in testing |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 96 | // instead of the TestTabContents which causes this command not to |
| 97 | // be enabled when the tab is added (and selected). |
[email protected] | a206b44 | 2009-10-08 23:20:20 | [diff] [blame] | 98 | browser()->command_updater()->UpdateCommandEnabled(IDC_BOOKMARK_PAGE, true); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 99 | |
| 100 | // Star it. |
[email protected] | a206b44 | 2009-10-08 23:20:20 | [diff] [blame] | 101 | browser()->ExecuteCommand(IDC_BOOKMARK_PAGE); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 102 | |
| 103 | // It should now be bookmarked in the bookmark model. |
| 104 | EXPECT_EQ(profile(), browser()->profile()); |
| 105 | EXPECT_TRUE(browser()->profile()->GetBookmarkModel()->IsBookmarked(url1)); |
| 106 | } |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 107 | |
| 108 | // Tests back/forward in new tab (Control + Back/Forward button in the UI). |
| 109 | TEST_F(BrowserCommandsTest, BackForwardInNewTab) { |
| 110 | GURL url1("http://foo/1"); |
| 111 | GURL url2("http://foo/2"); |
| 112 | |
| 113 | // Make a tab with the two pages navigated in it. |
| 114 | AddTab(browser(), url1); |
| 115 | NavigateAndCommitActiveTab(url2); |
| 116 | |
| 117 | // Go back in a new background tab. |
| 118 | browser()->GoBack(NEW_BACKGROUND_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 119 | EXPECT_EQ(0, browser()->active_index()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 120 | ASSERT_EQ(2, browser()->tab_count()); |
| 121 | |
| 122 | // The original tab should be unchanged. |
| 123 | TabContents* zeroth = browser()->GetTabContentsAt(0); |
| 124 | EXPECT_EQ(url2, zeroth->GetURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 125 | EXPECT_TRUE(zeroth->GetController().CanGoBack()); |
| 126 | EXPECT_FALSE(zeroth->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 127 | |
| 128 | // The new tab should be like the first one but navigated back. |
| 129 | TabContents* first = browser()->GetTabContentsAt(1); |
| 130 | EXPECT_EQ(url1, browser()->GetTabContentsAt(1)->GetURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 131 | EXPECT_FALSE(first->GetController().CanGoBack()); |
| 132 | EXPECT_TRUE(first->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 133 | |
| 134 | // Select the second tab and make it go forward in a new background tab. |
[email protected] | eaca0ad1 | 2011-04-18 15:53:41 | [diff] [blame] | 135 | browser()->ActivateTabAt(1, true); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 136 | // TODO(brettw) bug 11055: It should not be necessary to commit the load here, |
| 137 | // but because of this bug, it will assert later if we don't. When the bug is |
| 138 | // fixed, one of the three commits here related to this bug should be removed |
| 139 | // (to test both codepaths). |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 140 | CommitPendingLoad(&first->GetController()); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 141 | EXPECT_EQ(1, browser()->active_index()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 142 | browser()->GoForward(NEW_BACKGROUND_TAB); |
| 143 | |
| 144 | // The previous tab should be unchanged and still in the foreground. |
| 145 | EXPECT_EQ(url1, first->GetURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 146 | EXPECT_FALSE(first->GetController().CanGoBack()); |
| 147 | EXPECT_TRUE(first->GetController().CanGoForward()); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 148 | EXPECT_EQ(1, browser()->active_index()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 149 | |
| 150 | // There should be a new tab navigated forward. |
| 151 | ASSERT_EQ(3, browser()->tab_count()); |
| 152 | TabContents* second = browser()->GetTabContentsAt(2); |
| 153 | EXPECT_EQ(url2, second->GetURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 154 | EXPECT_TRUE(second->GetController().CanGoBack()); |
| 155 | EXPECT_FALSE(second->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 156 | |
| 157 | // Now do back in a new foreground tab. Don't bother re-checking every sngle |
| 158 | // thing above, just validate that it's opening properly. |
[email protected] | eaca0ad1 | 2011-04-18 15:53:41 | [diff] [blame] | 159 | browser()->ActivateTabAt(2, true); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 160 | // TODO(brettw) bug 11055: see the comment above about why we need this. |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 161 | CommitPendingLoad(&second->GetController()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 162 | browser()->GoBack(NEW_FOREGROUND_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 163 | ASSERT_EQ(3, browser()->active_index()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 164 | ASSERT_EQ(url1, browser()->GetSelectedTabContents()->GetURL()); |
| 165 | |
| 166 | // Same thing again for forward. |
| 167 | // TODO(brettw) bug 11055: see the comment above about why we need this. |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 168 | CommitPendingLoad(&browser()->GetSelectedTabContents()->GetController()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 169 | browser()->GoForward(NEW_FOREGROUND_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 170 | ASSERT_EQ(4, browser()->active_index()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 171 | ASSERT_EQ(url2, browser()->GetSelectedTabContents()->GetURL()); |
| 172 | } |
[email protected] | 0080c4f | 2011-10-03 21:56:37 | [diff] [blame] | 173 | |
| 174 | // Tests IDC_SEARCH (the Search key on Chrome OS devices). |
| 175 | #if defined(OS_CHROMEOS) |
| 176 | TEST_F(BrowserCommandsTest, Search) { |
| 177 | // Load a non-NTP URL. |
| 178 | GURL non_ntp_url("http://foo/"); |
| 179 | AddTab(browser(), non_ntp_url); |
| 180 | ASSERT_EQ(1, browser()->tab_count()); |
| 181 | EXPECT_EQ(non_ntp_url, browser()->GetSelectedTabContents()->GetURL()); |
| 182 | |
| 183 | // Pressing the Search key should open a new tab containing the NTP. |
| 184 | browser()->Search(); |
| 185 | ASSERT_EQ(2, browser()->tab_count()); |
| 186 | ASSERT_EQ(1, browser()->active_index()); |
| 187 | GURL current_url = browser()->GetSelectedTabContents()->GetURL(); |
| 188 | EXPECT_TRUE(current_url.SchemeIs(chrome::kChromeUIScheme)); |
| 189 | EXPECT_EQ(chrome::kChromeUINewTabHost, current_url.host()); |
| 190 | |
| 191 | // Pressing it a second time while the NTP is open shouldn't change anything. |
| 192 | browser()->Search(); |
| 193 | ASSERT_EQ(2, browser()->tab_count()); |
| 194 | ASSERT_EQ(1, browser()->active_index()); |
| 195 | current_url = browser()->GetSelectedTabContents()->GetURL(); |
| 196 | EXPECT_TRUE(current_url.SchemeIs(chrome::kChromeUIScheme)); |
| 197 | EXPECT_EQ(chrome::kChromeUINewTabHost, current_url.host()); |
| 198 | } |
| 199 | #endif |