blob: 4a6735c1085d718f0c728c9662be7a64f998f04a [file] [log] [blame]
[email protected]be3877f2009-01-14 15:51:101// 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
[email protected]b782578e2009-02-27 23:52:345#include "chrome/app/chrome_dll_resource.h"
[email protected]b7ca4e62009-01-23 20:37:296#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]be3877f2009-01-14 15:51:107#include "chrome/browser/browser.h"
8#include "chrome/browser/browser_list.h"
[email protected]f3ec7742009-01-15 00:59:169#include "chrome/browser/tab_contents/navigation_controller.h"
10#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]9423d9412009-04-14 22:13:5511#include "chrome/common/url_constants.h"
[email protected]be3877f2009-01-14 15:51:1012#include "chrome/test/browser_with_test_window_test.h"
[email protected]b7ca4e62009-01-23 20:37:2913#include "chrome/test/testing_profile.h"
[email protected]be3877f2009-01-14 15:51:1014
15typedef BrowserWithTestWindowTest BrowserCommandsTest;
16
17// Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and
18// IDC_SELECT_LAST_TAB.
19TEST_F(BrowserCommandsTest, TabNavigationAccelerators) {
[email protected]9423d9412009-04-14 22:13:5520 GURL about_blank(chrome::kAboutBlankURL);
21
[email protected]be3877f2009-01-14 15:51:1022 // Create three tabs.
[email protected]9423d9412009-04-14 22:13:5523 AddTab(browser(), about_blank);
24 AddTab(browser(), about_blank);
25 AddTab(browser(), about_blank);
[email protected]be3877f2009-01-14 15:51:1026
27 // Select the second tab.
28 browser()->SelectTabContentsAt(1, false);
29
30 // Navigate to the first tab using an accelerator.
31 browser()->ExecuteCommand(IDC_SELECT_TAB_0);
32 ASSERT_EQ(0, browser()->selected_index());
33
34 // Navigate to the second tab using the next accelerators.
35 browser()->ExecuteCommand(IDC_SELECT_NEXT_TAB);
36 ASSERT_EQ(1, browser()->selected_index());
37
38 // Navigate back to the first tab using the previous accelerators.
39 browser()->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
40 ASSERT_EQ(0, browser()->selected_index());
41
42 // Navigate to the last tab using the select last accelerator.
43 browser()->ExecuteCommand(IDC_SELECT_LAST_TAB);
44 ASSERT_EQ(2, browser()->selected_index());
45}
46
47// Tests IDC_DUPLICATE_TAB.
48TEST_F(BrowserCommandsTest, DuplicateTab) {
[email protected]9423d9412009-04-14 22:13:5549 GURL url1("http://foo/1");
50 GURL url2("http://foo/2");
51 GURL url3("http://foo/3");
[email protected]be3877f2009-01-14 15:51:1052
53 // Navigate to the three urls, then go back.
[email protected]9423d9412009-04-14 22:13:5554 AddTab(browser(), url1);
[email protected]0683cf62009-04-10 19:31:4055 NavigateAndCommitActiveTab(url2);
56 NavigateAndCommitActiveTab(url3);
[email protected]be3877f2009-01-14 15:51:1057
58 size_t initial_window_count = BrowserList::size();
59
60 // Duplicate the tab.
61 browser()->ExecuteCommand(IDC_DUPLICATE_TAB);
62
63 // The duplicated tab should not end up in a new window.
[email protected]a0a8b1d2009-03-11 14:57:2264 size_t window_count = BrowserList::size();
[email protected]be3877f2009-01-14 15:51:1065 ASSERT_EQ(initial_window_count, window_count);
66
67 // And we should have a newly duplicated tab.
68 ASSERT_EQ(2, browser()->tab_count());
69
70 // Verify the stack of urls.
[email protected]ce3fa3c2009-04-20 19:55:5771 NavigationController& controller =
[email protected]be3877f2009-01-14 15:51:1072 browser()->GetTabContentsAt(1)->controller();
[email protected]ce3fa3c2009-04-20 19:55:5773 ASSERT_EQ(3, controller.entry_count());
74 ASSERT_EQ(2, controller.GetCurrentEntryIndex());
75 ASSERT_TRUE(url1 == controller.GetEntryAtIndex(0)->url());
76 ASSERT_TRUE(url2 == controller.GetEntryAtIndex(1)->url());
77 ASSERT_TRUE(url3 == controller.GetEntryAtIndex(2)->url());
[email protected]be3877f2009-01-14 15:51:1078}
[email protected]b7ca4e62009-01-23 20:37:2979
80TEST_F(BrowserCommandsTest, BookmarkCurrentPage) {
81 // We use profile() here, since it's a TestingProfile.
82 profile()->CreateBookmarkModel(true);
83 profile()->BlockUntilBookmarkModelLoaded();
84
85 // Navigate to a url.
[email protected]9423d9412009-04-14 22:13:5586 GURL url1("http://foo/1");
87 AddTab(browser(), url1);
[email protected]b7ca4e62009-01-23 20:37:2988 browser()->OpenURL(url1, GURL(), CURRENT_TAB, PageTransition::TYPED);
89
90 // TODO(beng): remove this once we can use WebContentses directly in testing
91 // instead of the TestTabContents which causes this command not to
92 // be enabled when the tab is added (and selected).
93 browser()->command_updater()->UpdateCommandEnabled(IDC_STAR, true);
94
95 // Star it.
96 browser()->ExecuteCommand(IDC_STAR);
97
98 // It should now be bookmarked in the bookmark model.
99 EXPECT_EQ(profile(), browser()->profile());
100 EXPECT_TRUE(browser()->profile()->GetBookmarkModel()->IsBookmarked(url1));
101}
[email protected]e21e8c92009-04-29 02:42:09102
103// Tests back/forward in new tab (Control + Back/Forward button in the UI).
104TEST_F(BrowserCommandsTest, BackForwardInNewTab) {
105 GURL url1("http://foo/1");
106 GURL url2("http://foo/2");
107
108 // Make a tab with the two pages navigated in it.
109 AddTab(browser(), url1);
110 NavigateAndCommitActiveTab(url2);
111
112 // Go back in a new background tab.
113 browser()->GoBack(NEW_BACKGROUND_TAB);
114 EXPECT_EQ(0, browser()->selected_index());
115 ASSERT_EQ(2, browser()->tab_count());
116
117 // The original tab should be unchanged.
118 TabContents* zeroth = browser()->GetTabContentsAt(0);
119 EXPECT_EQ(url2, zeroth->GetURL());
120 EXPECT_TRUE(zeroth->controller().CanGoBack());
121 EXPECT_FALSE(zeroth->controller().CanGoForward());
122
123 // The new tab should be like the first one but navigated back.
124 TabContents* first = browser()->GetTabContentsAt(1);
125 EXPECT_EQ(url1, browser()->GetTabContentsAt(1)->GetURL());
126 EXPECT_FALSE(first->controller().CanGoBack());
127 EXPECT_TRUE(first->controller().CanGoForward());
128
129 // Select the second tab and make it go forward in a new background tab.
130 browser()->SelectTabContentsAt(1, true);
131 // TODO(brettw) bug 11055: It should not be necessary to commit the load here,
132 // but because of this bug, it will assert later if we don't. When the bug is
133 // fixed, one of the three commits here related to this bug should be removed
134 // (to test both codepaths).
135 CommitPendingLoad(&first->controller());
136 EXPECT_EQ(1, browser()->selected_index());
137 browser()->GoForward(NEW_BACKGROUND_TAB);
138
139 // The previous tab should be unchanged and still in the foreground.
140 EXPECT_EQ(url1, first->GetURL());
141 EXPECT_FALSE(first->controller().CanGoBack());
142 EXPECT_TRUE(first->controller().CanGoForward());
143 EXPECT_EQ(1, browser()->selected_index());
144
145 // There should be a new tab navigated forward.
146 ASSERT_EQ(3, browser()->tab_count());
147 TabContents* second = browser()->GetTabContentsAt(2);
148 EXPECT_EQ(url2, second->GetURL());
149 EXPECT_TRUE(second->controller().CanGoBack());
150 EXPECT_FALSE(second->controller().CanGoForward());
151
152 // Now do back in a new foreground tab. Don't bother re-checking every sngle
153 // thing above, just validate that it's opening properly.
154 browser()->SelectTabContentsAt(2, true);
155 // TODO(brettw) bug 11055: see the comment above about why we need this.
156 CommitPendingLoad(&second->controller());
157 browser()->GoBack(NEW_FOREGROUND_TAB);
158 ASSERT_EQ(3, browser()->selected_index());
159 ASSERT_EQ(url1, browser()->GetSelectedTabContents()->GetURL());
160
161 // Same thing again for forward.
162 // TODO(brettw) bug 11055: see the comment above about why we need this.
163 CommitPendingLoad(&browser()->GetSelectedTabContents()->controller());
164 browser()->GoForward(NEW_FOREGROUND_TAB);
165 ASSERT_EQ(4, browser()->selected_index());
166 ASSERT_EQ(url2, browser()->GetSelectedTabContents()->GetURL());
167}