browser_commands_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chrome/app/chrome_dll_resource.h" 6#include "chrome/browser/bookmarks/bookmark_model.h" 7#include "chrome/browser/browser.h" 8#include "chrome/browser/browser_list.h" 9#include "chrome/browser/chrome_thread.h" 10#include "chrome/browser/tab_contents/navigation_controller.h" 11#include "chrome/browser/tab_contents/navigation_entry.h" 12#include "chrome/browser/tab_contents/tab_contents.h" 13#include "chrome/common/url_constants.h" 14#include "chrome/test/browser_with_test_window_test.h" 15#include "chrome/test/testing_profile.h" 16 17typedef BrowserWithTestWindowTest BrowserCommandsTest; 18 19// Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and 20// IDC_SELECT_LAST_TAB. 21TEST_F(BrowserCommandsTest, TabNavigationAccelerators) { 22 GURL about_blank(chrome::kAboutBlankURL); 23 24 // Create three tabs. 25 AddTab(browser(), about_blank); 26 AddTab(browser(), about_blank); 27 AddTab(browser(), about_blank); 28 29 // Select the second tab. 30 browser()->SelectTabContentsAt(1, false); 31 32 // Navigate to the first tab using an accelerator. 33 browser()->ExecuteCommand(IDC_SELECT_TAB_0); 34 ASSERT_EQ(0, browser()->selected_index()); 35 36 // Navigate to the second tab using the next accelerators. 37 browser()->ExecuteCommand(IDC_SELECT_NEXT_TAB); 38 ASSERT_EQ(1, browser()->selected_index()); 39 40 // Navigate back to the first tab using the previous accelerators. 41 browser()->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB); 42 ASSERT_EQ(0, browser()->selected_index()); 43 44 // Navigate to the last tab using the select last accelerator. 45 browser()->ExecuteCommand(IDC_SELECT_LAST_TAB); 46 ASSERT_EQ(2, browser()->selected_index()); 47} 48 49// Tests IDC_DUPLICATE_TAB. 50TEST_F(BrowserCommandsTest, DuplicateTab) { 51 GURL url1("http://foo/1"); 52 GURL url2("http://foo/2"); 53 GURL url3("http://foo/3"); 54 55 // Navigate to the three urls, then go back. 56 AddTab(browser(), url1); 57 NavigateAndCommitActiveTab(url2); 58 NavigateAndCommitActiveTab(url3); 59 60 size_t initial_window_count = BrowserList::size(); 61 62 // Duplicate the tab. 63 browser()->ExecuteCommand(IDC_DUPLICATE_TAB); 64 65 // The duplicated tab should not end up in a new window. 66 size_t window_count = BrowserList::size(); 67 ASSERT_EQ(initial_window_count, window_count); 68 69 // And we should have a newly duplicated tab. 70 ASSERT_EQ(2, browser()->tab_count()); 71 72 // Verify the stack of urls. 73 NavigationController& controller = 74 browser()->GetTabContentsAt(1)->controller(); 75 ASSERT_EQ(3, controller.entry_count()); 76 ASSERT_EQ(2, controller.GetCurrentEntryIndex()); 77 ASSERT_TRUE(url1 == controller.GetEntryAtIndex(0)->url()); 78 ASSERT_TRUE(url2 == controller.GetEntryAtIndex(1)->url()); 79 ASSERT_TRUE(url3 == controller.GetEntryAtIndex(2)->url()); 80} 81 82TEST_F(BrowserCommandsTest, BookmarkCurrentPage) { 83 ChromeThread file_loop(ChromeThread::FILE, MessageLoop::current()); 84 // We use profile() here, since it's a TestingProfile. 85 profile()->CreateBookmarkModel(true); 86 profile()->BlockUntilBookmarkModelLoaded(); 87 88 // Navigate to a url. 89 GURL url1("http://foo/1"); 90 AddTab(browser(), url1); 91 browser()->OpenURL(url1, GURL(), CURRENT_TAB, PageTransition::TYPED); 92 93 // TODO(beng): remove this once we can use TabContentses directly in testing 94 // instead of the TestTabContents which causes this command not to 95 // be enabled when the tab is added (and selected). 96 browser()->command_updater()->UpdateCommandEnabled(IDC_BOOKMARK_PAGE, true); 97 98 // Star it. 99 browser()->ExecuteCommand(IDC_BOOKMARK_PAGE); 100 101 // It should now be bookmarked in the bookmark model. 102 EXPECT_EQ(profile(), browser()->profile()); 103 EXPECT_TRUE(browser()->profile()->GetBookmarkModel()->IsBookmarked(url1)); 104} 105 106// Tests back/forward in new tab (Control + Back/Forward button in the UI). 107TEST_F(BrowserCommandsTest, BackForwardInNewTab) { 108 GURL url1("http://foo/1"); 109 GURL url2("http://foo/2"); 110 111 // Make a tab with the two pages navigated in it. 112 AddTab(browser(), url1); 113 NavigateAndCommitActiveTab(url2); 114 115 // Go back in a new background tab. 116 browser()->GoBack(NEW_BACKGROUND_TAB); 117 EXPECT_EQ(0, browser()->selected_index()); 118 ASSERT_EQ(2, browser()->tab_count()); 119 120 // The original tab should be unchanged. 121 TabContents* zeroth = browser()->GetTabContentsAt(0); 122 EXPECT_EQ(url2, zeroth->GetURL()); 123 EXPECT_TRUE(zeroth->controller().CanGoBack()); 124 EXPECT_FALSE(zeroth->controller().CanGoForward()); 125 126 // The new tab should be like the first one but navigated back. 127 TabContents* first = browser()->GetTabContentsAt(1); 128 EXPECT_EQ(url1, browser()->GetTabContentsAt(1)->GetURL()); 129 EXPECT_FALSE(first->controller().CanGoBack()); 130 EXPECT_TRUE(first->controller().CanGoForward()); 131 132 // Select the second tab and make it go forward in a new background tab. 133 browser()->SelectTabContentsAt(1, true); 134 // TODO(brettw) bug 11055: It should not be necessary to commit the load here, 135 // but because of this bug, it will assert later if we don't. When the bug is 136 // fixed, one of the three commits here related to this bug should be removed 137 // (to test both codepaths). 138 CommitPendingLoad(&first->controller()); 139 EXPECT_EQ(1, browser()->selected_index()); 140 browser()->GoForward(NEW_BACKGROUND_TAB); 141 142 // The previous tab should be unchanged and still in the foreground. 143 EXPECT_EQ(url1, first->GetURL()); 144 EXPECT_FALSE(first->controller().CanGoBack()); 145 EXPECT_TRUE(first->controller().CanGoForward()); 146 EXPECT_EQ(1, browser()->selected_index()); 147 148 // There should be a new tab navigated forward. 149 ASSERT_EQ(3, browser()->tab_count()); 150 TabContents* second = browser()->GetTabContentsAt(2); 151 EXPECT_EQ(url2, second->GetURL()); 152 EXPECT_TRUE(second->controller().CanGoBack()); 153 EXPECT_FALSE(second->controller().CanGoForward()); 154 155 // Now do back in a new foreground tab. Don't bother re-checking every sngle 156 // thing above, just validate that it's opening properly. 157 browser()->SelectTabContentsAt(2, true); 158 // TODO(brettw) bug 11055: see the comment above about why we need this. 159 CommitPendingLoad(&second->controller()); 160 browser()->GoBack(NEW_FOREGROUND_TAB); 161 ASSERT_EQ(3, browser()->selected_index()); 162 ASSERT_EQ(url1, browser()->GetSelectedTabContents()->GetURL()); 163 164 // Same thing again for forward. 165 // TODO(brettw) bug 11055: see the comment above about why we need this. 166 CommitPendingLoad(&browser()->GetSelectedTabContents()->controller()); 167 browser()->GoForward(NEW_FOREGROUND_TAB); 168 ASSERT_EQ(4, browser()->selected_index()); 169 ASSERT_EQ(url2, browser()->GetSelectedTabContents()->GetURL()); 170} 171