blob: 1f9544d9d0458925d6be5378c202269189874b66 [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]5d98294912012-06-27 22:57:407#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:108#include "chrome/browser/ui/browser_commands.h"
[email protected]2ad4a902010-11-17 06:05:139#include "chrome/browser/ui/browser_list.h"
[email protected]9423d9412009-04-14 22:13:5510#include "chrome/common/url_constants.h"
[email protected]a4ff9eae2011-08-01 19:58:1611#include "chrome/test/base/browser_with_test_window_test.h"
12#include "chrome/test/base/testing_profile.h"
[email protected]cdcb1dee2012-01-04 00:46:2013#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:2514#include "content/public/browser/navigation_entry.h"
[email protected]6acde6352012-01-04 16:52:2015#include "content/public/browser/web_contents.h"
[email protected]e97882f2012-06-04 02:23:1716#include "content/public/test/test_browser_thread.h"
[email protected]be3877f2009-01-14 15:51:1017
[email protected]93cc27b2012-03-21 12:44:3218#if defined(OS_CHROMEOS)
19#include "chrome/browser/chromeos/login/mock_user_manager.h"
20#endif
21
[email protected]be3877f2009-01-14 15:51:1022typedef BrowserWithTestWindowTest BrowserCommandsTest;
23
[email protected]e5d549d2011-12-28 01:29:2024using content::OpenURLParams;
25using content::Referrer;
[email protected]6acde6352012-01-04 16:52:2026using content::WebContents;
[email protected]e5d549d2011-12-28 01:29:2027
[email protected]be3877f2009-01-14 15:51:1028// Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and
29// IDC_SELECT_LAST_TAB.
[email protected]6df642082009-08-13 22:31:3730TEST_F(BrowserCommandsTest, TabNavigationAccelerators) {
[email protected]9423d9412009-04-14 22:13:5531 GURL about_blank(chrome::kAboutBlankURL);
32
[email protected]be3877f2009-01-14 15:51:1033 // Create three tabs.
[email protected]9423d9412009-04-14 22:13:5534 AddTab(browser(), about_blank);
35 AddTab(browser(), about_blank);
36 AddTab(browser(), about_blank);
[email protected]be3877f2009-01-14 15:51:1037
38 // Select the second tab.
[email protected]eaca0ad12011-04-18 15:53:4139 browser()->ActivateTabAt(1, false);
[email protected]be3877f2009-01-14 15:51:1040
[email protected]5d98294912012-06-27 22:57:4041 CommandUpdater* updater = browser()->command_controller()->command_updater();
42
[email protected]be3877f2009-01-14 15:51:1043 // Navigate to the first tab using an accelerator.
[email protected]5d98294912012-06-27 22:57:4044 updater->ExecuteCommand(IDC_SELECT_TAB_0);
[email protected]1ea49d52011-04-12 17:44:4445 ASSERT_EQ(0, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1046
47 // Navigate to the second tab using the next accelerators.
[email protected]5d98294912012-06-27 22:57:4048 updater->ExecuteCommand(IDC_SELECT_NEXT_TAB);
[email protected]1ea49d52011-04-12 17:44:4449 ASSERT_EQ(1, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1050
51 // Navigate back to the first tab using the previous accelerators.
[email protected]5d98294912012-06-27 22:57:4052 updater->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
[email protected]1ea49d52011-04-12 17:44:4453 ASSERT_EQ(0, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1054
55 // Navigate to the last tab using the select last accelerator.
[email protected]5d98294912012-06-27 22:57:4056 updater->ExecuteCommand(IDC_SELECT_LAST_TAB);
[email protected]1ea49d52011-04-12 17:44:4457 ASSERT_EQ(2, browser()->active_index());
[email protected]be3877f2009-01-14 15:51:1058}
59
60// Tests IDC_DUPLICATE_TAB.
61TEST_F(BrowserCommandsTest, DuplicateTab) {
[email protected]9423d9412009-04-14 22:13:5562 GURL url1("http://foo/1");
63 GURL url2("http://foo/2");
64 GURL url3("http://foo/3");
[email protected]be3877f2009-01-14 15:51:1065
66 // Navigate to the three urls, then go back.
[email protected]9423d9412009-04-14 22:13:5567 AddTab(browser(), url1);
[email protected]0683cf62009-04-10 19:31:4068 NavigateAndCommitActiveTab(url2);
69 NavigateAndCommitActiveTab(url3);
[email protected]be3877f2009-01-14 15:51:1070
71 size_t initial_window_count = BrowserList::size();
72
73 // Duplicate the tab.
[email protected]5d98294912012-06-27 22:57:4074 chrome::ExecuteCommand(browser(), IDC_DUPLICATE_TAB);
[email protected]be3877f2009-01-14 15:51:1075
76 // The duplicated tab should not end up in a new window.
[email protected]a0a8b1d2009-03-11 14:57:2277 size_t window_count = BrowserList::size();
[email protected]be3877f2009-01-14 15:51:1078 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]cdcb1dee2012-01-04 00:46:2084 content::NavigationController& controller =
[email protected]6acde6352012-01-04 16:52:2085 browser()->GetWebContentsAt(1)->GetController();
[email protected]a26023822011-12-29 00:23:5586 ASSERT_EQ(3, controller.GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:5787 ASSERT_EQ(2, controller.GetCurrentEntryIndex());
[email protected]36fc0392011-12-25 03:59:5188 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]be3877f2009-01-14 15:51:1091}
[email protected]b7ca4e62009-01-23 20:37:2992
93TEST_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]9423d9412009-04-14 22:13:5599 GURL url1("http://foo/1");
100 AddTab(browser(), url1);
[email protected]e5d549d2011-12-28 01:29:20101 browser()->OpenURL(OpenURLParams(
102 url1, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
[email protected]b7ca4e62009-01-23 20:37:29103
[email protected]5d98294912012-06-27 22:57:40104 chrome::BookmarkCurrentPage(browser());
[email protected]b7ca4e62009-01-23 20:37:29105
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]e21e8c92009-04-29 02:42:09110
111// Tests back/forward in new tab (Control + Back/Forward button in the UI).
112TEST_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]a37d4b02012-06-25 21:56:10121 chrome::GoBack(browser(), NEW_BACKGROUND_TAB);
[email protected]1ea49d52011-04-12 17:44:44122 EXPECT_EQ(0, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09123 ASSERT_EQ(2, browser()->tab_count());
124
125 // The original tab should be unchanged.
[email protected]6acde6352012-01-04 16:52:20126 WebContents* zeroth = browser()->GetWebContentsAt(0);
[email protected]e21e8c92009-04-29 02:42:09127 EXPECT_EQ(url2, zeroth->GetURL());
[email protected]f5fa20e2011-12-21 22:35:56128 EXPECT_TRUE(zeroth->GetController().CanGoBack());
129 EXPECT_FALSE(zeroth->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09130
131 // The new tab should be like the first one but navigated back.
[email protected]6acde6352012-01-04 16:52:20132 WebContents* first = browser()->GetWebContentsAt(1);
133 EXPECT_EQ(url1, browser()->GetWebContentsAt(1)->GetURL());
[email protected]f5fa20e2011-12-21 22:35:56134 EXPECT_FALSE(first->GetController().CanGoBack());
135 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09136
137 // Select the second tab and make it go forward in a new background tab.
[email protected]eaca0ad12011-04-18 15:53:41138 browser()->ActivateTabAt(1, true);
[email protected]e21e8c92009-04-29 02:42:09139 // 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]f5fa20e2011-12-21 22:35:56143 CommitPendingLoad(&first->GetController());
[email protected]1ea49d52011-04-12 17:44:44144 EXPECT_EQ(1, browser()->active_index());
[email protected]a37d4b02012-06-25 21:56:10145 chrome::GoForward(browser(), NEW_BACKGROUND_TAB);
[email protected]e21e8c92009-04-29 02:42:09146
147 // The previous tab should be unchanged and still in the foreground.
148 EXPECT_EQ(url1, first->GetURL());
[email protected]f5fa20e2011-12-21 22:35:56149 EXPECT_FALSE(first->GetController().CanGoBack());
150 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]1ea49d52011-04-12 17:44:44151 EXPECT_EQ(1, browser()->active_index());
[email protected]e21e8c92009-04-29 02:42:09152
153 // There should be a new tab navigated forward.
154 ASSERT_EQ(3, browser()->tab_count());
[email protected]6acde6352012-01-04 16:52:20155 WebContents* second = browser()->GetWebContentsAt(2);
[email protected]e21e8c92009-04-29 02:42:09156 EXPECT_EQ(url2, second->GetURL());
[email protected]f5fa20e2011-12-21 22:35:56157 EXPECT_TRUE(second->GetController().CanGoBack());
158 EXPECT_FALSE(second->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09159
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]eaca0ad12011-04-18 15:53:41162 browser()->ActivateTabAt(2, true);
[email protected]e21e8c92009-04-29 02:42:09163 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]f5fa20e2011-12-21 22:35:56164 CommitPendingLoad(&second->GetController());
[email protected]a37d4b02012-06-25 21:56:10165 chrome::GoBack(browser(), NEW_FOREGROUND_TAB);
[email protected]1ea49d52011-04-12 17:44:44166 ASSERT_EQ(3, browser()->active_index());
[email protected]871dc682012-06-11 19:35:33167 ASSERT_EQ(url1, browser()->GetActiveWebContents()->GetURL());
[email protected]e21e8c92009-04-29 02:42:09168
169 // Same thing again for forward.
170 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]871dc682012-06-11 19:35:33171 CommitPendingLoad(&browser()->GetActiveWebContents()->GetController());
[email protected]a37d4b02012-06-25 21:56:10172 chrome::GoForward(browser(), NEW_FOREGROUND_TAB);
[email protected]1ea49d52011-04-12 17:44:44173 ASSERT_EQ(4, browser()->active_index());
[email protected]871dc682012-06-11 19:35:33174 ASSERT_EQ(url2, browser()->GetActiveWebContents()->GetURL());
[email protected]e21e8c92009-04-29 02:42:09175}
[email protected]0080c4f2011-10-03 21:56:37176