blob: d783af82f2c3810eb402d287a381ac89e6f1ef76 [file] [log] [blame]
[email protected]93cc27b2012-03-21 12:44:321// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]be3877f2009-01-14 15:51:102// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1a3aba82010-11-08 23:52:545#include "chrome/app/chrome_command_ids.h"
[email protected]b7ca4e62009-01-23 20:37:296#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]2ad4a902010-11-17 06:05:137#include "chrome/browser/ui/browser_list.h"
[email protected]9423d9412009-04-14 22:13:558#include "chrome/common/url_constants.h"
[email protected]a4ff9eae2011-08-01 19:58:169#include "chrome/test/base/browser_with_test_window_test.h"
10#include "chrome/test/base/testing_profile.h"
[email protected]cdcb1dee2012-01-04 00:46:2011#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:2512#include "content/public/browser/navigation_entry.h"
[email protected]6acde6352012-01-04 16:52:2013#include "content/public/browser/web_contents.h"
[email protected]c38831a12011-10-28 12:44:4914#include "content/test/test_browser_thread.h"
[email protected]be3877f2009-01-14 15:51:1015
[email protected]93cc27b2012-03-21 12:44:3216#if defined(OS_CHROMEOS)
17#include "chrome/browser/chromeos/login/mock_user_manager.h"
18#endif
19
[email protected]be3877f2009-01-14 15:51:1020typedef BrowserWithTestWindowTest BrowserCommandsTest;
21
[email protected]e5d549d2011-12-28 01:29:2022using content::OpenURLParams;
23using content::Referrer;
[email protected]6acde6352012-01-04 16:52:2024using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2025
[email protected]be3877f2009-01-14 15:51:1026// Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and
27// IDC_SELECT_LAST_TAB.
[email protected]6df642082009-08-13 22:31:3728TEST_F(BrowserCommandsTest, TabNavigationAccelerators) {
[email protected]9423d9412009-04-14 22:13:5529 GURL about_blank(chrome::kAboutBlankURL);
30
[email protected]be3877f2009-01-14 15:51:1031 // Create three tabs.
[email protected]9423d9412009-04-14 22:13:5532 AddTab(browser(), about_blank);
33 AddTab(browser(), about_blank);
34 AddTab(browser(), about_blank);
[email protected]be3877f2009-01-14 15:51:1035
36 // Select the second tab.
[email protected]eaca0ad12011-04-18 15:53:4137 browser()->ActivateTabAt(1, false);
[email protected]be3877f2009-01-14 15:51:1038
39 // Navigate to the first tab using an accelerator.
40 browser()->ExecuteCommand(IDC_SELECT_TAB_0);
[email protected]1ea49d52011-04-12 17:44:4441 ASSERT_EQ(0, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1042
43 // Navigate to the second tab using the next accelerators.
44 browser()->ExecuteCommand(IDC_SELECT_NEXT_TAB);
[email protected]1ea49d52011-04-12 17:44:4445 ASSERT_EQ(1, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1046
47 // Navigate back to the first tab using the previous accelerators.
48 browser()->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
[email protected]1ea49d52011-04-12 17:44:4449 ASSERT_EQ(0, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1050
51 // Navigate to the last tab using the select last accelerator.
52 browser()->ExecuteCommand(IDC_SELECT_LAST_TAB);
[email protected]1ea49d52011-04-12 17:44:4453 ASSERT_EQ(2, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1054}
55
56// Tests IDC_DUPLICATE_TAB.
57TEST_F(BrowserCommandsTest, DuplicateTab) {
[email protected]9423d9412009-04-14 22:13:5558 GURL url1("http://foo/1");
59 GURL url2("http://foo/2");
60 GURL url3("http://foo/3");
[email protected]be3877f2009-01-14 15:51:1061
62 // Navigate to the three urls, then go back.
[email protected]9423d9412009-04-14 22:13:5563 AddTab(browser(), url1);
[email protected]0683cf62009-04-10 19:31:4064 NavigateAndCommitActiveTab(url2);
65 NavigateAndCommitActiveTab(url3);
[email protected]be3877f2009-01-14 15:51:1066
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]a0a8b1d2009-03-11 14:57:2273 size_t window_count = BrowserList::size();
[email protected]be3877f2009-01-14 15:51:1074 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]cdcb1dee2012-01-04 00:46:2080 content::NavigationController& controller =
[email protected]6acde6352012-01-04 16:52:2081 browser()->GetWebContentsAt(1)->GetController();
[email protected]a26023822011-12-29 00:23:5582 ASSERT_EQ(3, controller.GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:5783 ASSERT_EQ(2, controller.GetCurrentEntryIndex());
[email protected]36fc0392011-12-25 03:59:5184 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]be3877f2009-01-14 15:51:1087}
[email protected]b7ca4e62009-01-23 20:37:2988
89TEST_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]9423d9412009-04-14 22:13:5595 GURL url1("http://foo/1");
96 AddTab(browser(), url1);
[email protected]e5d549d2011-12-28 01:29:2097 browser()->OpenURL(OpenURLParams(
98 url1, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
[email protected]b7ca4e62009-01-23 20:37:2999
[email protected]57c6a652009-05-04 07:58:34100 // TODO(beng): remove this once we can use TabContentses directly in testing
[email protected]b7ca4e62009-01-23 20:37:29101 // instead of the TestTabContents which causes this command not to
102 // be enabled when the tab is added (and selected).
[email protected]a206b442009-10-08 23:20:20103 browser()->command_updater()->UpdateCommandEnabled(IDC_BOOKMARK_PAGE, true);
[email protected]b7ca4e62009-01-23 20:37:29104
105 // Star it.
[email protected]a206b442009-10-08 23:20:20106 browser()->ExecuteCommand(IDC_BOOKMARK_PAGE);
[email protected]b7ca4e62009-01-23 20:37:29107
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]e21e8c92009-04-29 02:42:09112
113// Tests back/forward in new tab (Control + Back/Forward button in the UI).
114TEST_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]1ea49d52011-04-12 17:44:44124 EXPECT_EQ(0, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09125 ASSERT_EQ(2, browser()->tab_count());
126
127 // The original tab should be unchanged.
[email protected]6acde6352012-01-04 16:52:20128 WebContents* zeroth = browser()->GetWebContentsAt(0);
[email protected]e21e8c92009-04-29 02:42:09129 EXPECT_EQ(url2, zeroth->GetURL());
[email protected]f5fa20e2011-12-21 22:35:56130 EXPECT_TRUE(zeroth->GetController().CanGoBack());
131 EXPECT_FALSE(zeroth->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09132
133 // The new tab should be like the first one but navigated back.
[email protected]6acde6352012-01-04 16:52:20134 WebContents* first = browser()->GetWebContentsAt(1);
135 EXPECT_EQ(url1, browser()->GetWebContentsAt(1)->GetURL());
[email protected]f5fa20e2011-12-21 22:35:56136 EXPECT_FALSE(first->GetController().CanGoBack());
137 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09138
139 // Select the second tab and make it go forward in a new background tab.
[email protected]eaca0ad12011-04-18 15:53:41140 browser()->ActivateTabAt(1, true);
[email protected]e21e8c92009-04-29 02:42:09141 // 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]f5fa20e2011-12-21 22:35:56145 CommitPendingLoad(&first->GetController());
[email protected]1ea49d52011-04-12 17:44:44146 EXPECT_EQ(1, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09147 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]f5fa20e2011-12-21 22:35:56151 EXPECT_FALSE(first->GetController().CanGoBack());
152 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]1ea49d52011-04-12 17:44:44153 EXPECT_EQ(1, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09154
155 // There should be a new tab navigated forward.
156 ASSERT_EQ(3, browser()->tab_count());
[email protected]6acde6352012-01-04 16:52:20157 WebContents* second = browser()->GetWebContentsAt(2);
[email protected]e21e8c92009-04-29 02:42:09158 EXPECT_EQ(url2, second->GetURL());
[email protected]f5fa20e2011-12-21 22:35:56159 EXPECT_TRUE(second->GetController().CanGoBack());
160 EXPECT_FALSE(second->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09161
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]eaca0ad12011-04-18 15:53:41164 browser()->ActivateTabAt(2, true);
[email protected]e21e8c92009-04-29 02:42:09165 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]f5fa20e2011-12-21 22:35:56166 CommitPendingLoad(&second->GetController());
[email protected]e21e8c92009-04-29 02:42:09167 browser()->GoBack(NEW_FOREGROUND_TAB);
[email protected]1ea49d52011-04-12 17:44:44168 ASSERT_EQ(3, browser()->active_index());
[email protected]4ca15302012-01-03 05:53:20169 ASSERT_EQ(url1, browser()->GetSelectedWebContents()->GetURL());
[email protected]e21e8c92009-04-29 02:42:09170
171 // Same thing again for forward.
172 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]4ca15302012-01-03 05:53:20173 CommitPendingLoad(&browser()->GetSelectedWebContents()->GetController());
[email protected]e21e8c92009-04-29 02:42:09174 browser()->GoForward(NEW_FOREGROUND_TAB);
[email protected]1ea49d52011-04-12 17:44:44175 ASSERT_EQ(4, browser()->active_index());
[email protected]4ca15302012-01-03 05:53:20176 ASSERT_EQ(url2, browser()->GetSelectedWebContents()->GetURL());
[email protected]e21e8c92009-04-29 02:42:09177}
[email protected]0080c4f2011-10-03 21:56:37178
179// Tests IDC_SEARCH (the Search key on Chrome OS devices).
180#if defined(OS_CHROMEOS)
[email protected]93cc27b2012-03-21 12:44:32181
182namespace chromeos {
183
[email protected]0080c4f2011-10-03 21:56:37184TEST_F(BrowserCommandsTest, Search) {
[email protected]93cc27b2012-03-21 12:44:32185 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]0080c4f2011-10-03 21:56:37190 // 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]4ca15302012-01-03 05:53:20194 EXPECT_EQ(non_ntp_url, browser()->GetSelectedWebContents()->GetURL());
[email protected]0080c4f2011-10-03 21:56:37195
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]4ca15302012-01-03 05:53:20200 GURL current_url = browser()->GetSelectedWebContents()->GetURL();
[email protected]0080c4f2011-10-03 21:56:37201 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]4ca15302012-01-03 05:53:20208 current_url = browser()->GetSelectedWebContents()->GetURL();
[email protected]0080c4f2011-10-03 21:56:37209 EXPECT_TRUE(current_url.SchemeIs(chrome::kChromeUIScheme));
210 EXPECT_EQ(chrome::kChromeUINewTabHost, current_url.host());
[email protected]93cc27b2012-03-21 12:44:32211
212 UserManager::Set(old_user_manager);
[email protected]0080c4f2011-10-03 21:56:37213}
[email protected]93cc27b2012-03-21 12:44:32214
215} // namespace chromeos
[email protected]0080c4f2011-10-03 21:56:37216#endif