Fix build break
[chromium-blink-merge.git] / chrome / browser / browser_commands_unittest.cc
blob8ee05a642d7d8d3c4d2b6a22ed3872db7fc2ddc2
1 // Copyright (c) 2012 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.
5 #include "chrome/app/chrome_command_ids.h"
6 #include "chrome/browser/bookmarks/bookmark_model.h"
7 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
8 #include "chrome/browser/ui/browser_command_controller.h"
9 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/browser_finder.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/browser_with_test_window_test.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/web_contents.h"
20 #if defined(OS_CHROMEOS)
21 #include "chrome/browser/chromeos/login/mock_user_manager.h"
22 #endif
24 typedef BrowserWithTestWindowTest BrowserCommandsTest;
26 using content::OpenURLParams;
27 using content::Referrer;
28 using content::WebContents;
30 // Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and
31 // IDC_SELECT_LAST_TAB.
32 TEST_F(BrowserCommandsTest, TabNavigationAccelerators) {
33 GURL about_blank(chrome::kAboutBlankURL);
35 // Create three tabs.
36 AddTab(browser(), about_blank);
37 AddTab(browser(), about_blank);
38 AddTab(browser(), about_blank);
40 // Select the second tab.
41 browser()->tab_strip_model()->ActivateTabAt(1, false);
43 CommandUpdater* updater = browser()->command_controller()->command_updater();
45 // Navigate to the first tab using an accelerator.
46 updater->ExecuteCommand(IDC_SELECT_TAB_0);
47 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
49 // Navigate to the second tab using the next accelerators.
50 updater->ExecuteCommand(IDC_SELECT_NEXT_TAB);
51 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
53 // Navigate back to the first tab using the previous accelerators.
54 updater->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
55 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
57 // Navigate to the last tab using the select last accelerator.
58 updater->ExecuteCommand(IDC_SELECT_LAST_TAB);
59 ASSERT_EQ(2, browser()->tab_strip_model()->active_index());
62 // Tests IDC_DUPLICATE_TAB.
63 TEST_F(BrowserCommandsTest, DuplicateTab) {
64 GURL url1("http://foo/1");
65 GURL url2("http://foo/2");
66 GURL url3("http://foo/3");
67 GURL url4("http://foo/4");
69 // Navigate to three urls, plus a pending URL that hasn't committed.
70 AddTab(browser(), url1);
71 NavigateAndCommitActiveTab(url2);
72 NavigateAndCommitActiveTab(url3);
73 content::NavigationController& orig_controller =
74 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
75 orig_controller.LoadURL(
76 url4, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
77 EXPECT_EQ(3, orig_controller.GetEntryCount());
78 EXPECT_TRUE(orig_controller.GetPendingEntry());
80 size_t initial_window_count = chrome::GetTotalBrowserCount();
82 // Duplicate the tab.
83 chrome::ExecuteCommand(browser(), IDC_DUPLICATE_TAB);
85 // The duplicated tab should not end up in a new window.
86 size_t window_count = chrome::GetTotalBrowserCount();
87 ASSERT_EQ(initial_window_count, window_count);
89 // And we should have a newly duplicated tab.
90 ASSERT_EQ(2, browser()->tab_strip_model()->count());
92 // Verify the stack of urls.
93 content::NavigationController& controller =
94 browser()->tab_strip_model()->GetWebContentsAt(1)->GetController();
95 EXPECT_EQ(3, controller.GetEntryCount());
96 EXPECT_EQ(2, controller.GetCurrentEntryIndex());
97 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
98 EXPECT_EQ(url2, controller.GetEntryAtIndex(1)->GetURL());
99 EXPECT_EQ(url3, controller.GetEntryAtIndex(2)->GetURL());
100 EXPECT_FALSE(controller.GetPendingEntry());
103 // Tests IDC_VIEW_SOURCE (See http://crbug.com/138140).
104 TEST_F(BrowserCommandsTest, ViewSource) {
105 GURL url1("http://foo/1");
106 GURL url2("http://foo/2");
108 // Navigate to a URL, plus a pending URL that hasn't committed.
109 AddTab(browser(), url1);
110 content::NavigationController& orig_controller =
111 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
112 orig_controller.LoadURL(
113 url2, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
114 EXPECT_EQ(1, orig_controller.GetEntryCount());
115 EXPECT_TRUE(orig_controller.GetPendingEntry());
117 size_t initial_window_count = chrome::GetTotalBrowserCount();
119 // View Source.
120 chrome::ExecuteCommand(browser(), IDC_VIEW_SOURCE);
122 // The view source tab should not end up in a new window.
123 size_t window_count = chrome::GetTotalBrowserCount();
124 ASSERT_EQ(initial_window_count, window_count);
126 // And we should have a newly duplicated tab.
127 ASSERT_EQ(2, browser()->tab_strip_model()->count());
129 // Verify we are viewing the source of the last committed entry.
130 GURL view_source_url("view-source:http://foo/1");
131 content::NavigationController& controller =
132 browser()->tab_strip_model()->GetWebContentsAt(1)->GetController();
133 EXPECT_EQ(1, controller.GetEntryCount());
134 EXPECT_EQ(0, controller.GetCurrentEntryIndex());
135 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
136 EXPECT_EQ(view_source_url, controller.GetEntryAtIndex(0)->GetVirtualURL());
137 EXPECT_FALSE(controller.GetPendingEntry());
140 TEST_F(BrowserCommandsTest, BookmarkCurrentPage) {
141 // We use profile() here, since it's a TestingProfile.
142 profile()->CreateBookmarkModel(true);
144 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
145 ui_test_utils::WaitForBookmarkModelToLoad(model);
147 // Navigate to a url.
148 GURL url1("http://foo/1");
149 AddTab(browser(), url1);
150 browser()->OpenURL(OpenURLParams(
151 url1, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
153 chrome::BookmarkCurrentPage(browser());
155 // It should now be bookmarked in the bookmark model.
156 EXPECT_EQ(profile(), browser()->profile());
157 EXPECT_TRUE(model->IsBookmarked(url1));
160 // Tests back/forward in new tab (Control + Back/Forward button in the UI).
161 TEST_F(BrowserCommandsTest, BackForwardInNewTab) {
162 GURL url1("http://foo/1");
163 GURL url2("http://foo/2");
165 // Make a tab with the two pages navigated in it.
166 AddTab(browser(), url1);
167 NavigateAndCommitActiveTab(url2);
169 // Go back in a new background tab.
170 chrome::GoBack(browser(), NEW_BACKGROUND_TAB);
171 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
172 ASSERT_EQ(2, browser()->tab_strip_model()->count());
174 // The original tab should be unchanged.
175 WebContents* zeroth = browser()->tab_strip_model()->GetWebContentsAt(0);
176 EXPECT_EQ(url2, zeroth->GetURL());
177 EXPECT_TRUE(zeroth->GetController().CanGoBack());
178 EXPECT_FALSE(zeroth->GetController().CanGoForward());
180 // The new tab should be like the first one but navigated back.
181 WebContents* first = browser()->tab_strip_model()->GetWebContentsAt(1);
182 EXPECT_EQ(url1, browser()->tab_strip_model()->GetWebContentsAt(1)->GetURL());
183 EXPECT_FALSE(first->GetController().CanGoBack());
184 EXPECT_TRUE(first->GetController().CanGoForward());
186 // Select the second tab and make it go forward in a new background tab.
187 browser()->tab_strip_model()->ActivateTabAt(1, true);
188 // TODO(brettw) bug 11055: It should not be necessary to commit the load here,
189 // but because of this bug, it will assert later if we don't. When the bug is
190 // fixed, one of the three commits here related to this bug should be removed
191 // (to test both codepaths).
192 CommitPendingLoad(&first->GetController());
193 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
194 chrome::GoForward(browser(), NEW_BACKGROUND_TAB);
196 // The previous tab should be unchanged and still in the foreground.
197 EXPECT_EQ(url1, first->GetURL());
198 EXPECT_FALSE(first->GetController().CanGoBack());
199 EXPECT_TRUE(first->GetController().CanGoForward());
200 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
202 // There should be a new tab navigated forward.
203 ASSERT_EQ(3, browser()->tab_strip_model()->count());
204 WebContents* second = browser()->tab_strip_model()->GetWebContentsAt(2);
205 EXPECT_EQ(url2, second->GetURL());
206 EXPECT_TRUE(second->GetController().CanGoBack());
207 EXPECT_FALSE(second->GetController().CanGoForward());
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.
211 browser()->tab_strip_model()->ActivateTabAt(2, true);
212 // TODO(brettw) bug 11055: see the comment above about why we need this.
213 CommitPendingLoad(&second->GetController());
214 chrome::GoBack(browser(), NEW_FOREGROUND_TAB);
215 ASSERT_EQ(3, browser()->tab_strip_model()->active_index());
216 ASSERT_EQ(url1,
217 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
219 // Same thing again for forward.
220 // TODO(brettw) bug 11055: see the comment above about why we need this.
221 CommitPendingLoad(&
222 browser()->tab_strip_model()->GetActiveWebContents()->GetController());
223 chrome::GoForward(browser(), NEW_FOREGROUND_TAB);
224 ASSERT_EQ(4, browser()->tab_strip_model()->active_index());
225 ASSERT_EQ(url2,
226 browser()->tab_strip_model()->GetActiveWebContents()->GetURL());