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