blob: 22d83d5772e283d8b042f6bf81c08f3186356d09 [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]fa50dea02012-08-01 23:20:377#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]5d98294912012-06-27 22:57:408#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:109#include "chrome/browser/ui/browser_commands.h"
[email protected]0665ebe2013-02-13 09:53:1910#include "chrome/browser/ui/browser_finder.h"
[email protected]57892182012-12-03 19:15:3911#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]9423d9412009-04-14 22:13:5512#include "chrome/common/url_constants.h"
[email protected]a4ff9eae2011-08-01 19:58:1613#include "chrome/test/base/browser_with_test_window_test.h"
14#include "chrome/test/base/testing_profile.h"
[email protected]1f9c74192013-03-25 23:04:3515#include "chrome/test/base/ui_test_utils.h"
[email protected]cdcb1dee2012-01-04 00:46:2016#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:2517#include "content/public/browser/navigation_entry.h"
[email protected]6acde6352012-01-04 16:52:2018#include "content/public/browser/web_contents.h"
[email protected]be3877f2009-01-14 15:51:1019
[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]081dc522013-05-15 04:59:2029 GURL about_blank(content::kAboutBlankURL);
[email protected]9423d9412009-04-14 22:13:5530
[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]57892182012-12-03 19:15:3937 browser()->tab_strip_model()->ActivateTabAt(1, false);
[email protected]be3877f2009-01-14 15:51:1038
[email protected]5d98294912012-06-27 22:57:4039 CommandUpdater* updater = browser()->command_controller()->command_updater();
40
[email protected]be3877f2009-01-14 15:51:1041 // Navigate to the first tab using an accelerator.
[email protected]5d98294912012-06-27 22:57:4042 updater->ExecuteCommand(IDC_SELECT_TAB_0);
[email protected]57892182012-12-03 19:15:3943 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]be3877f2009-01-14 15:51:1044
45 // Navigate to the second tab using the next accelerators.
[email protected]5d98294912012-06-27 22:57:4046 updater->ExecuteCommand(IDC_SELECT_NEXT_TAB);
[email protected]57892182012-12-03 19:15:3947 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]be3877f2009-01-14 15:51:1048
49 // Navigate back to the first tab using the previous accelerators.
[email protected]5d98294912012-06-27 22:57:4050 updater->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
[email protected]57892182012-12-03 19:15:3951 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]be3877f2009-01-14 15:51:1052
53 // Navigate to the last tab using the select last accelerator.
[email protected]5d98294912012-06-27 22:57:4054 updater->ExecuteCommand(IDC_SELECT_LAST_TAB);
[email protected]57892182012-12-03 19:15:3955 ASSERT_EQ(2, browser()->tab_strip_model()->active_index());
[email protected]be3877f2009-01-14 15:51:1056}
57
58// Tests IDC_DUPLICATE_TAB.
59TEST_F(BrowserCommandsTest, DuplicateTab) {
[email protected]9423d9412009-04-14 22:13:5560 GURL url1("http://foo/1");
61 GURL url2("http://foo/2");
62 GURL url3("http://foo/3");
[email protected]8fcc7bf2012-08-14 18:14:5063 GURL url4("http://foo/4");
[email protected]be3877f2009-01-14 15:51:1064
[email protected]8fcc7bf2012-08-14 18:14:5065 // Navigate to three urls, plus a pending URL that hasn't committed.
[email protected]9423d9412009-04-14 22:13:5566 AddTab(browser(), url1);
[email protected]0683cf62009-04-10 19:31:4067 NavigateAndCommitActiveTab(url2);
68 NavigateAndCommitActiveTab(url3);
[email protected]8fcc7bf2012-08-14 18:14:5069 content::NavigationController& orig_controller =
[email protected]57892182012-12-03 19:15:3970 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
[email protected]8fcc7bf2012-08-14 18:14:5071 orig_controller.LoadURL(
72 url4, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
73 EXPECT_EQ(3, orig_controller.GetEntryCount());
74 EXPECT_TRUE(orig_controller.GetPendingEntry());
[email protected]be3877f2009-01-14 15:51:1075
[email protected]0665ebe2013-02-13 09:53:1976 size_t initial_window_count = chrome::GetTotalBrowserCount();
[email protected]be3877f2009-01-14 15:51:1077
78 // Duplicate the tab.
[email protected]5d98294912012-06-27 22:57:4079 chrome::ExecuteCommand(browser(), IDC_DUPLICATE_TAB);
[email protected]be3877f2009-01-14 15:51:1080
81 // The duplicated tab should not end up in a new window.
[email protected]0665ebe2013-02-13 09:53:1982 size_t window_count = chrome::GetTotalBrowserCount();
[email protected]be3877f2009-01-14 15:51:1083 ASSERT_EQ(initial_window_count, window_count);
84
85 // And we should have a newly duplicated tab.
[email protected]57892182012-12-03 19:15:3986 ASSERT_EQ(2, browser()->tab_strip_model()->count());
[email protected]be3877f2009-01-14 15:51:1087
88 // Verify the stack of urls.
[email protected]cdcb1dee2012-01-04 00:46:2089 content::NavigationController& controller =
[email protected]57892182012-12-03 19:15:3990 browser()->tab_strip_model()->GetWebContentsAt(1)->GetController();
[email protected]8fcc7bf2012-08-14 18:14:5091 EXPECT_EQ(3, controller.GetEntryCount());
92 EXPECT_EQ(2, controller.GetCurrentEntryIndex());
93 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
94 EXPECT_EQ(url2, controller.GetEntryAtIndex(1)->GetURL());
95 EXPECT_EQ(url3, controller.GetEntryAtIndex(2)->GetURL());
96 EXPECT_FALSE(controller.GetPendingEntry());
97}
98
99// Tests IDC_VIEW_SOURCE (See http://crbug.com/138140).
100TEST_F(BrowserCommandsTest, ViewSource) {
101 GURL url1("http://foo/1");
102 GURL url2("http://foo/2");
103
104 // Navigate to a URL, plus a pending URL that hasn't committed.
105 AddTab(browser(), url1);
106 content::NavigationController& orig_controller =
[email protected]57892182012-12-03 19:15:39107 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
[email protected]8fcc7bf2012-08-14 18:14:50108 orig_controller.LoadURL(
109 url2, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
110 EXPECT_EQ(1, orig_controller.GetEntryCount());
111 EXPECT_TRUE(orig_controller.GetPendingEntry());
112
[email protected]0665ebe2013-02-13 09:53:19113 size_t initial_window_count = chrome::GetTotalBrowserCount();
[email protected]8fcc7bf2012-08-14 18:14:50114
115 // View Source.
116 chrome::ExecuteCommand(browser(), IDC_VIEW_SOURCE);
117
118 // The view source tab should not end up in a new window.
[email protected]0665ebe2013-02-13 09:53:19119 size_t window_count = chrome::GetTotalBrowserCount();
[email protected]8fcc7bf2012-08-14 18:14:50120 ASSERT_EQ(initial_window_count, window_count);
121
122 // And we should have a newly duplicated tab.
[email protected]57892182012-12-03 19:15:39123 ASSERT_EQ(2, browser()->tab_strip_model()->count());
[email protected]8fcc7bf2012-08-14 18:14:50124
125 // Verify we are viewing the source of the last committed entry.
126 GURL view_source_url("view-source:http://foo/1");
127 content::NavigationController& controller =
[email protected]57892182012-12-03 19:15:39128 browser()->tab_strip_model()->GetWebContentsAt(1)->GetController();
[email protected]8fcc7bf2012-08-14 18:14:50129 EXPECT_EQ(1, controller.GetEntryCount());
130 EXPECT_EQ(0, controller.GetCurrentEntryIndex());
131 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
132 EXPECT_EQ(view_source_url, controller.GetEntryAtIndex(0)->GetVirtualURL());
133 EXPECT_FALSE(controller.GetPendingEntry());
[email protected]be3877f2009-01-14 15:51:10134}
[email protected]b7ca4e62009-01-23 20:37:29135
136TEST_F(BrowserCommandsTest, BookmarkCurrentPage) {
137 // We use profile() here, since it's a TestingProfile.
138 profile()->CreateBookmarkModel(true);
[email protected]1f9c74192013-03-25 23:04:35139
140 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
141 ui_test_utils::WaitForBookmarkModelToLoad(model);
[email protected]b7ca4e62009-01-23 20:37:29142
143 // Navigate to a url.
[email protected]9423d9412009-04-14 22:13:55144 GURL url1("http://foo/1");
145 AddTab(browser(), url1);
[email protected]e5d549d2011-12-28 01:29:20146 browser()->OpenURL(OpenURLParams(
147 url1, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
[email protected]b7ca4e62009-01-23 20:37:29148
[email protected]5d98294912012-06-27 22:57:40149 chrome::BookmarkCurrentPage(browser());
[email protected]b7ca4e62009-01-23 20:37:29150
151 // It should now be bookmarked in the bookmark model.
152 EXPECT_EQ(profile(), browser()->profile());
[email protected]1f9c74192013-03-25 23:04:35153 EXPECT_TRUE(model->IsBookmarked(url1));
[email protected]b7ca4e62009-01-23 20:37:29154}
[email protected]e21e8c92009-04-29 02:42:09155
156// Tests back/forward in new tab (Control + Back/Forward button in the UI).
157TEST_F(BrowserCommandsTest, BackForwardInNewTab) {
158 GURL url1("http://foo/1");
159 GURL url2("http://foo/2");
160
161 // Make a tab with the two pages navigated in it.
162 AddTab(browser(), url1);
163 NavigateAndCommitActiveTab(url2);
164
165 // Go back in a new background tab.
[email protected]a37d4b02012-06-25 21:56:10166 chrome::GoBack(browser(), NEW_BACKGROUND_TAB);
[email protected]57892182012-12-03 19:15:39167 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
168 ASSERT_EQ(2, browser()->tab_strip_model()->count());
[email protected]e21e8c92009-04-29 02:42:09169
[email protected]57892182012-12-03 19:15:39170 WebContents* zeroth = browser()->tab_strip_model()->GetWebContentsAt(0);
[email protected]e022aca2013-07-18 19:10:18171 WebContents* first = browser()->tab_strip_model()->GetWebContentsAt(1);
172
173 // The original tab should be unchanged.
174 EXPECT_EQ(url2, zeroth->GetLastCommittedURL());
[email protected]f5fa20e2011-12-21 22:35:56175 EXPECT_TRUE(zeroth->GetController().CanGoBack());
176 EXPECT_FALSE(zeroth->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09177
[email protected]e022aca2013-07-18 19:10:18178 // The new tab should be like the first one but navigated back. Since we
179 // didn't wait for the load to complete, we can't use GetLastCommittedURL.
180 EXPECT_EQ(url1, first->GetActiveURL());
[email protected]f5fa20e2011-12-21 22:35:56181 EXPECT_FALSE(first->GetController().CanGoBack());
182 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09183
184 // Select the second tab and make it go forward in a new background tab.
[email protected]57892182012-12-03 19:15:39185 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]e21e8c92009-04-29 02:42:09186 // TODO(brettw) bug 11055: It should not be necessary to commit the load here,
187 // but because of this bug, it will assert later if we don't. When the bug is
188 // fixed, one of the three commits here related to this bug should be removed
189 // (to test both codepaths).
[email protected]f5fa20e2011-12-21 22:35:56190 CommitPendingLoad(&first->GetController());
[email protected]57892182012-12-03 19:15:39191 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]a37d4b02012-06-25 21:56:10192 chrome::GoForward(browser(), NEW_BACKGROUND_TAB);
[email protected]e21e8c92009-04-29 02:42:09193
194 // The previous tab should be unchanged and still in the foreground.
[email protected]e022aca2013-07-18 19:10:18195 EXPECT_EQ(url1, first->GetLastCommittedURL());
[email protected]f5fa20e2011-12-21 22:35:56196 EXPECT_FALSE(first->GetController().CanGoBack());
197 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]57892182012-12-03 19:15:39198 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]e21e8c92009-04-29 02:42:09199
200 // There should be a new tab navigated forward.
[email protected]57892182012-12-03 19:15:39201 ASSERT_EQ(3, browser()->tab_strip_model()->count());
202 WebContents* second = browser()->tab_strip_model()->GetWebContentsAt(2);
[email protected]e022aca2013-07-18 19:10:18203 // Since we didn't wait for load to complete, we can't use
204 // GetLastCommittedURL.
205 EXPECT_EQ(url2, second->GetActiveURL());
[email protected]f5fa20e2011-12-21 22:35:56206 EXPECT_TRUE(second->GetController().CanGoBack());
207 EXPECT_FALSE(second->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09208
209 // Now do back in a new foreground tab. Don't bother re-checking every sngle
210 // thing above, just validate that it's opening properly.
[email protected]57892182012-12-03 19:15:39211 browser()->tab_strip_model()->ActivateTabAt(2, true);
[email protected]e21e8c92009-04-29 02:42:09212 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]f5fa20e2011-12-21 22:35:56213 CommitPendingLoad(&second->GetController());
[email protected]a37d4b02012-06-25 21:56:10214 chrome::GoBack(browser(), NEW_FOREGROUND_TAB);
[email protected]57892182012-12-03 19:15:39215 ASSERT_EQ(3, browser()->tab_strip_model()->active_index());
216 ASSERT_EQ(url1,
[email protected]e022aca2013-07-18 19:10:18217 browser()->tab_strip_model()->GetActiveWebContents()->
218 GetActiveURL());
[email protected]e21e8c92009-04-29 02:42:09219
220 // Same thing again for forward.
221 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]57892182012-12-03 19:15:39222 CommitPendingLoad(&
223 browser()->tab_strip_model()->GetActiveWebContents()->GetController());
[email protected]a37d4b02012-06-25 21:56:10224 chrome::GoForward(browser(), NEW_FOREGROUND_TAB);
[email protected]57892182012-12-03 19:15:39225 ASSERT_EQ(4, browser()->tab_strip_model()->active_index());
226 ASSERT_EQ(url2,
[email protected]e022aca2013-07-18 19:10:18227 browser()->tab_strip_model()->GetActiveWebContents()->
228 GetActiveURL());
[email protected]e21e8c92009-04-29 02:42:09229}