[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] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame^] | 7 | #include "chrome/browser/ui/browser_command_controller.h" |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 8 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 9 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 10 | #include "chrome/common/url_constants.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 11 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 12 | #include "chrome/test/base/testing_profile.h" |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 13 | #include "content/public/browser/navigation_controller.h" |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 14 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 15 | #include "content/public/browser/web_contents.h" |
[email protected] | e97882f | 2012-06-04 02:23:17 | [diff] [blame] | 16 | #include "content/public/test/test_browser_thread.h" |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 17 | |
[email protected] | 93cc27b | 2012-03-21 12:44:32 | [diff] [blame] | 18 | #if defined(OS_CHROMEOS) |
| 19 | #include "chrome/browser/chromeos/login/mock_user_manager.h" |
| 20 | #endif |
| 21 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 22 | typedef BrowserWithTestWindowTest BrowserCommandsTest; |
| 23 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 24 | using content::OpenURLParams; |
| 25 | using content::Referrer; |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 26 | using content::WebContents; |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 27 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 28 | // Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and |
| 29 | // IDC_SELECT_LAST_TAB. |
[email protected] | 6df64208 | 2009-08-13 22:31:37 | [diff] [blame] | 30 | TEST_F(BrowserCommandsTest, TabNavigationAccelerators) { |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 31 | GURL about_blank(chrome::kAboutBlankURL); |
| 32 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 33 | // Create three tabs. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 34 | AddTab(browser(), about_blank); |
| 35 | AddTab(browser(), about_blank); |
| 36 | AddTab(browser(), about_blank); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 37 | |
| 38 | // Select the second tab. |
[email protected] | eaca0ad1 | 2011-04-18 15:53:41 | [diff] [blame] | 39 | browser()->ActivateTabAt(1, false); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 40 | |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame^] | 41 | CommandUpdater* updater = browser()->command_controller()->command_updater(); |
| 42 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 43 | // Navigate to the first tab using an accelerator. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame^] | 44 | updater->ExecuteCommand(IDC_SELECT_TAB_0); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 45 | ASSERT_EQ(0, browser()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 46 | |
| 47 | // Navigate to the second tab using the next accelerators. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame^] | 48 | updater->ExecuteCommand(IDC_SELECT_NEXT_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 49 | ASSERT_EQ(1, browser()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 50 | |
| 51 | // Navigate back to the first tab using the previous accelerators. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame^] | 52 | updater->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 53 | ASSERT_EQ(0, browser()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 54 | |
| 55 | // Navigate to the last tab using the select last accelerator. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame^] | 56 | updater->ExecuteCommand(IDC_SELECT_LAST_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 57 | ASSERT_EQ(2, browser()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Tests IDC_DUPLICATE_TAB. |
| 61 | TEST_F(BrowserCommandsTest, DuplicateTab) { |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 62 | GURL url1("http://foo/1"); |
| 63 | GURL url2("http://foo/2"); |
| 64 | GURL url3("http://foo/3"); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 65 | |
| 66 | // Navigate to the three urls, then go back. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 67 | AddTab(browser(), url1); |
[email protected] | 0683cf6 | 2009-04-10 19:31:40 | [diff] [blame] | 68 | NavigateAndCommitActiveTab(url2); |
| 69 | NavigateAndCommitActiveTab(url3); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 70 | |
| 71 | size_t initial_window_count = BrowserList::size(); |
| 72 | |
| 73 | // Duplicate the tab. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame^] | 74 | chrome::ExecuteCommand(browser(), IDC_DUPLICATE_TAB); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 75 | |
| 76 | // The duplicated tab should not end up in a new window. |
[email protected] | a0a8b1d | 2009-03-11 14:57:22 | [diff] [blame] | 77 | size_t window_count = BrowserList::size(); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 78 | ASSERT_EQ(initial_window_count, window_count); |
| 79 | |
| 80 | // And we should have a newly duplicated tab. |
| 81 | ASSERT_EQ(2, browser()->tab_count()); |
| 82 | |
| 83 | // Verify the stack of urls. |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 84 | content::NavigationController& controller = |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 85 | browser()->GetWebContentsAt(1)->GetController(); |
[email protected] | a2602382 | 2011-12-29 00:23:55 | [diff] [blame] | 86 | ASSERT_EQ(3, controller.GetEntryCount()); |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 87 | ASSERT_EQ(2, controller.GetCurrentEntryIndex()); |
[email protected] | 36fc039 | 2011-12-25 03:59:51 | [diff] [blame] | 88 | ASSERT_TRUE(url1 == controller.GetEntryAtIndex(0)->GetURL()); |
| 89 | ASSERT_TRUE(url2 == controller.GetEntryAtIndex(1)->GetURL()); |
| 90 | ASSERT_TRUE(url3 == controller.GetEntryAtIndex(2)->GetURL()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 91 | } |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 92 | |
| 93 | TEST_F(BrowserCommandsTest, BookmarkCurrentPage) { |
| 94 | // We use profile() here, since it's a TestingProfile. |
| 95 | profile()->CreateBookmarkModel(true); |
| 96 | profile()->BlockUntilBookmarkModelLoaded(); |
| 97 | |
| 98 | // Navigate to a url. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 99 | GURL url1("http://foo/1"); |
| 100 | AddTab(browser(), url1); |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 101 | browser()->OpenURL(OpenURLParams( |
| 102 | url1, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 103 | |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame^] | 104 | chrome::BookmarkCurrentPage(browser()); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 105 | |
| 106 | // It should now be bookmarked in the bookmark model. |
| 107 | EXPECT_EQ(profile(), browser()->profile()); |
| 108 | EXPECT_TRUE(browser()->profile()->GetBookmarkModel()->IsBookmarked(url1)); |
| 109 | } |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 110 | |
| 111 | // Tests back/forward in new tab (Control + Back/Forward button in the UI). |
| 112 | TEST_F(BrowserCommandsTest, BackForwardInNewTab) { |
| 113 | GURL url1("http://foo/1"); |
| 114 | GURL url2("http://foo/2"); |
| 115 | |
| 116 | // Make a tab with the two pages navigated in it. |
| 117 | AddTab(browser(), url1); |
| 118 | NavigateAndCommitActiveTab(url2); |
| 119 | |
| 120 | // Go back in a new background tab. |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 121 | chrome::GoBack(browser(), NEW_BACKGROUND_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 122 | EXPECT_EQ(0, browser()->active_index()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 123 | ASSERT_EQ(2, browser()->tab_count()); |
| 124 | |
| 125 | // The original tab should be unchanged. |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 126 | WebContents* zeroth = browser()->GetWebContentsAt(0); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 127 | EXPECT_EQ(url2, zeroth->GetURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 128 | EXPECT_TRUE(zeroth->GetController().CanGoBack()); |
| 129 | EXPECT_FALSE(zeroth->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 130 | |
| 131 | // The new tab should be like the first one but navigated back. |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 132 | WebContents* first = browser()->GetWebContentsAt(1); |
| 133 | EXPECT_EQ(url1, browser()->GetWebContentsAt(1)->GetURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 134 | EXPECT_FALSE(first->GetController().CanGoBack()); |
| 135 | EXPECT_TRUE(first->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 136 | |
| 137 | // 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] | 138 | browser()->ActivateTabAt(1, true); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 139 | // TODO(brettw) bug 11055: It should not be necessary to commit the load here, |
| 140 | // but because of this bug, it will assert later if we don't. When the bug is |
| 141 | // fixed, one of the three commits here related to this bug should be removed |
| 142 | // (to test both codepaths). |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 143 | CommitPendingLoad(&first->GetController()); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 144 | EXPECT_EQ(1, browser()->active_index()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 145 | chrome::GoForward(browser(), NEW_BACKGROUND_TAB); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 146 | |
| 147 | // The previous tab should be unchanged and still in the foreground. |
| 148 | EXPECT_EQ(url1, first->GetURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 149 | EXPECT_FALSE(first->GetController().CanGoBack()); |
| 150 | EXPECT_TRUE(first->GetController().CanGoForward()); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 151 | EXPECT_EQ(1, browser()->active_index()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 152 | |
| 153 | // There should be a new tab navigated forward. |
| 154 | ASSERT_EQ(3, browser()->tab_count()); |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 155 | WebContents* second = browser()->GetWebContentsAt(2); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 156 | EXPECT_EQ(url2, second->GetURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 157 | EXPECT_TRUE(second->GetController().CanGoBack()); |
| 158 | EXPECT_FALSE(second->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 159 | |
| 160 | // Now do back in a new foreground tab. Don't bother re-checking every sngle |
| 161 | // thing above, just validate that it's opening properly. |
[email protected] | eaca0ad1 | 2011-04-18 15:53:41 | [diff] [blame] | 162 | browser()->ActivateTabAt(2, true); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 163 | // TODO(brettw) bug 11055: see the comment above about why we need this. |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 164 | CommitPendingLoad(&second->GetController()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 165 | chrome::GoBack(browser(), NEW_FOREGROUND_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 166 | ASSERT_EQ(3, browser()->active_index()); |
[email protected] | 871dc68 | 2012-06-11 19:35:33 | [diff] [blame] | 167 | ASSERT_EQ(url1, browser()->GetActiveWebContents()->GetURL()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 168 | |
| 169 | // Same thing again for forward. |
| 170 | // TODO(brettw) bug 11055: see the comment above about why we need this. |
[email protected] | 871dc68 | 2012-06-11 19:35:33 | [diff] [blame] | 171 | CommitPendingLoad(&browser()->GetActiveWebContents()->GetController()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 172 | chrome::GoForward(browser(), NEW_FOREGROUND_TAB); |
[email protected] | 1ea49d5 | 2011-04-12 17:44:44 | [diff] [blame] | 173 | ASSERT_EQ(4, browser()->active_index()); |
[email protected] | 871dc68 | 2012-06-11 19:35:33 | [diff] [blame] | 174 | ASSERT_EQ(url2, browser()->GetActiveWebContents()->GetURL()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 175 | } |
[email protected] | 0080c4f | 2011-10-03 21:56:37 | [diff] [blame] | 176 | |