blob: d04ae231817bd27ff1fd296e361557160606b007 [file] [log] [blame]
[email protected]1ea49d52011-04-12 17:44:441// Copyright (c) 2011 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]1bda97552011-03-01 20:11:5211#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]c38831a12011-10-28 12:44:4914#include "content/test/test_browser_thread.h"
[email protected]be3877f2009-01-14 15:51:1015
16typedef BrowserWithTestWindowTest BrowserCommandsTest;
17
[email protected]e5d549d2011-12-28 01:29:2018using content::OpenURLParams;
19using content::Referrer;
20
[email protected]be3877f2009-01-14 15:51:1021// Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and
22// IDC_SELECT_LAST_TAB.
[email protected]6df642082009-08-13 22:31:3723TEST_F(BrowserCommandsTest, TabNavigationAccelerators) {
[email protected]9423d9412009-04-14 22:13:5524 GURL about_blank(chrome::kAboutBlankURL);
25
[email protected]be3877f2009-01-14 15:51:1026 // Create three tabs.
[email protected]9423d9412009-04-14 22:13:5527 AddTab(browser(), about_blank);
28 AddTab(browser(), about_blank);
29 AddTab(browser(), about_blank);
[email protected]be3877f2009-01-14 15:51:1030
31 // Select the second tab.
[email protected]eaca0ad12011-04-18 15:53:4132 browser()->ActivateTabAt(1, false);
[email protected]be3877f2009-01-14 15:51:1033
34 // Navigate to the first tab using an accelerator.
35 browser()->ExecuteCommand(IDC_SELECT_TAB_0);
[email protected]1ea49d52011-04-12 17:44:4436 ASSERT_EQ(0, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1037
38 // Navigate to the second tab using the next accelerators.
39 browser()->ExecuteCommand(IDC_SELECT_NEXT_TAB);
[email protected]1ea49d52011-04-12 17:44:4440 ASSERT_EQ(1, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1041
42 // Navigate back to the first tab using the previous accelerators.
43 browser()->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
[email protected]1ea49d52011-04-12 17:44:4444 ASSERT_EQ(0, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1045
46 // Navigate to the last tab using the select last accelerator.
47 browser()->ExecuteCommand(IDC_SELECT_LAST_TAB);
[email protected]1ea49d52011-04-12 17:44:4448 ASSERT_EQ(2, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1049}
50
51// Tests IDC_DUPLICATE_TAB.
52TEST_F(BrowserCommandsTest, DuplicateTab) {
[email protected]9423d9412009-04-14 22:13:5553 GURL url1("http://foo/1");
54 GURL url2("http://foo/2");
55 GURL url3("http://foo/3");
[email protected]be3877f2009-01-14 15:51:1056
57 // Navigate to the three urls, then go back.
[email protected]9423d9412009-04-14 22:13:5558 AddTab(browser(), url1);
[email protected]0683cf62009-04-10 19:31:4059 NavigateAndCommitActiveTab(url2);
60 NavigateAndCommitActiveTab(url3);
[email protected]be3877f2009-01-14 15:51:1061
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]a0a8b1d2009-03-11 14:57:2268 size_t window_count = BrowserList::size();
[email protected]be3877f2009-01-14 15:51:1069 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]ce3fa3c2009-04-20 19:55:5775 NavigationController& controller =
[email protected]f5fa20e2011-12-21 22:35:5676 browser()->GetTabContentsAt(1)->GetController();
[email protected]ce3fa3c2009-04-20 19:55:5777 ASSERT_EQ(3, controller.entry_count());
78 ASSERT_EQ(2, controller.GetCurrentEntryIndex());
[email protected]36fc0392011-12-25 03:59:5179 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]be3877f2009-01-14 15:51:1082}
[email protected]b7ca4e62009-01-23 20:37:2983
84TEST_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]9423d9412009-04-14 22:13:5590 GURL url1("http://foo/1");
91 AddTab(browser(), url1);
[email protected]e5d549d2011-12-28 01:29:2092 browser()->OpenURL(OpenURLParams(
93 url1, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
[email protected]b7ca4e62009-01-23 20:37:2994
[email protected]57c6a652009-05-04 07:58:3495 // TODO(beng): remove this once we can use TabContentses directly in testing
[email protected]b7ca4e62009-01-23 20:37:2996 // instead of the TestTabContents which causes this command not to
97 // be enabled when the tab is added (and selected).
[email protected]a206b442009-10-08 23:20:2098 browser()->command_updater()->UpdateCommandEnabled(IDC_BOOKMARK_PAGE, true);
[email protected]b7ca4e62009-01-23 20:37:2999
100 // Star it.
[email protected]a206b442009-10-08 23:20:20101 browser()->ExecuteCommand(IDC_BOOKMARK_PAGE);
[email protected]b7ca4e62009-01-23 20:37:29102
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]e21e8c92009-04-29 02:42:09107
108// Tests back/forward in new tab (Control + Back/Forward button in the UI).
109TEST_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]1ea49d52011-04-12 17:44:44119 EXPECT_EQ(0, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09120 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]f5fa20e2011-12-21 22:35:56125 EXPECT_TRUE(zeroth->GetController().CanGoBack());
126 EXPECT_FALSE(zeroth->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09127
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]f5fa20e2011-12-21 22:35:56131 EXPECT_FALSE(first->GetController().CanGoBack());
132 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09133
134 // Select the second tab and make it go forward in a new background tab.
[email protected]eaca0ad12011-04-18 15:53:41135 browser()->ActivateTabAt(1, true);
[email protected]e21e8c92009-04-29 02:42:09136 // 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]f5fa20e2011-12-21 22:35:56140 CommitPendingLoad(&first->GetController());
[email protected]1ea49d52011-04-12 17:44:44141 EXPECT_EQ(1, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09142 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]f5fa20e2011-12-21 22:35:56146 EXPECT_FALSE(first->GetController().CanGoBack());
147 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]1ea49d52011-04-12 17:44:44148 EXPECT_EQ(1, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09149
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]f5fa20e2011-12-21 22:35:56154 EXPECT_TRUE(second->GetController().CanGoBack());
155 EXPECT_FALSE(second->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09156
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]eaca0ad12011-04-18 15:53:41159 browser()->ActivateTabAt(2, true);
[email protected]e21e8c92009-04-29 02:42:09160 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]f5fa20e2011-12-21 22:35:56161 CommitPendingLoad(&second->GetController());
[email protected]e21e8c92009-04-29 02:42:09162 browser()->GoBack(NEW_FOREGROUND_TAB);
[email protected]1ea49d52011-04-12 17:44:44163 ASSERT_EQ(3, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09164 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]f5fa20e2011-12-21 22:35:56168 CommitPendingLoad(&browser()->GetSelectedTabContents()->GetController());
[email protected]e21e8c92009-04-29 02:42:09169 browser()->GoForward(NEW_FOREGROUND_TAB);
[email protected]1ea49d52011-04-12 17:44:44170 ASSERT_EQ(4, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09171 ASSERT_EQ(url2, browser()->GetSelectedTabContents()->GetURL());
172}
[email protected]0080c4f2011-10-03 21:56:37173
174// Tests IDC_SEARCH (the Search key on Chrome OS devices).
175#if defined(OS_CHROMEOS)
176TEST_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