Autofill: code cleanup.
[chromium-blink-merge.git] / chrome / browser / browser_commands_unittest.cc
blobb87eb89558bbb87e4dd45fb7663dbb992480eeeb
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_factory.h"
7 #include "chrome/browser/ui/browser_command_controller.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/browser_finder.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.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 "components/bookmarks/browser/bookmark_model.h"
16 #include "components/bookmarks/test/bookmark_test_helpers.h"
17 #include "components/ui/zoom/page_zoom.h"
18 #include "components/ui/zoom/zoom_controller.h"
19 #include "content/public/browser/navigation_controller.h"
20 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/web_contents.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 typedef BrowserWithTestWindowTest BrowserCommandsTest;
26 using bookmarks::BookmarkModel;
27 using content::OpenURLParams;
28 using content::Referrer;
29 using content::WebContents;
30 using ui_zoom::ZoomController;
32 // Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and
33 // IDC_SELECT_LAST_TAB.
34 TEST_F(BrowserCommandsTest, TabNavigationAccelerators) {
35 GURL about_blank(url::kAboutBlankURL);
37 // Create three tabs.
38 AddTab(browser(), about_blank);
39 AddTab(browser(), about_blank);
40 AddTab(browser(), about_blank);
42 // Select the second tab.
43 browser()->tab_strip_model()->ActivateTabAt(1, false);
45 CommandUpdater* updater = browser()->command_controller()->command_updater();
47 // Navigate to the first tab using an accelerator.
48 updater->ExecuteCommand(IDC_SELECT_TAB_0);
49 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
51 // Navigate to the second tab using the next accelerators.
52 updater->ExecuteCommand(IDC_SELECT_NEXT_TAB);
53 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
55 // Navigate back to the first tab using the previous accelerators.
56 updater->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
57 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
59 // Navigate to the last tab using the select last accelerator.
60 updater->ExecuteCommand(IDC_SELECT_LAST_TAB);
61 ASSERT_EQ(2, browser()->tab_strip_model()->active_index());
64 // Tests IDC_DUPLICATE_TAB.
65 TEST_F(BrowserCommandsTest, DuplicateTab) {
66 GURL url1("http://foo/1");
67 GURL url2("http://foo/2");
68 GURL url3("http://foo/3");
69 GURL url4("http://foo/4");
71 // Navigate to three urls, plus a pending URL that hasn't committed.
72 AddTab(browser(), url1);
73 NavigateAndCommitActiveTab(url2);
74 NavigateAndCommitActiveTab(url3);
75 content::NavigationController& orig_controller =
76 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
77 orig_controller.LoadURL(
78 url4, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
79 EXPECT_EQ(3, orig_controller.GetEntryCount());
80 EXPECT_TRUE(orig_controller.GetPendingEntry());
82 size_t initial_window_count = chrome::GetTotalBrowserCount();
84 // Duplicate the tab.
85 chrome::ExecuteCommand(browser(), IDC_DUPLICATE_TAB);
87 // The duplicated tab should not end up in a new window.
88 size_t window_count = chrome::GetTotalBrowserCount();
89 ASSERT_EQ(initial_window_count, window_count);
91 // And we should have a newly duplicated tab.
92 ASSERT_EQ(2, browser()->tab_strip_model()->count());
94 // Verify the stack of urls.
95 content::NavigationController& controller =
96 browser()->tab_strip_model()->GetWebContentsAt(1)->GetController();
97 EXPECT_EQ(3, controller.GetEntryCount());
98 EXPECT_EQ(2, controller.GetCurrentEntryIndex());
99 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
100 EXPECT_EQ(url2, controller.GetEntryAtIndex(1)->GetURL());
101 EXPECT_EQ(url3, controller.GetEntryAtIndex(2)->GetURL());
102 EXPECT_FALSE(controller.GetPendingEntry());
105 // Tests IDC_VIEW_SOURCE (See http://crbug.com/138140).
106 TEST_F(BrowserCommandsTest, ViewSource) {
107 GURL url1("http://foo/1");
108 GURL url2("http://foo/2");
110 // Navigate to a URL, plus a pending URL that hasn't committed.
111 AddTab(browser(), url1);
112 content::NavigationController& orig_controller =
113 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
114 orig_controller.LoadURL(
115 url2, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
116 EXPECT_EQ(1, orig_controller.GetEntryCount());
117 EXPECT_TRUE(orig_controller.GetPendingEntry());
119 size_t initial_window_count = chrome::GetTotalBrowserCount();
121 // View Source.
122 chrome::ExecuteCommand(browser(), IDC_VIEW_SOURCE);
124 // The view source tab should not end up in a new window.
125 size_t window_count = chrome::GetTotalBrowserCount();
126 ASSERT_EQ(initial_window_count, window_count);
128 // And we should have a newly duplicated tab.
129 ASSERT_EQ(2, browser()->tab_strip_model()->count());
131 // Verify we are viewing the source of the last committed entry.
132 GURL view_source_url("view-source:http://foo/1");
133 content::NavigationController& controller =
134 browser()->tab_strip_model()->GetWebContentsAt(1)->GetController();
135 EXPECT_EQ(1, controller.GetEntryCount());
136 EXPECT_EQ(0, controller.GetCurrentEntryIndex());
137 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
138 EXPECT_EQ(view_source_url, controller.GetEntryAtIndex(0)->GetVirtualURL());
139 EXPECT_FALSE(controller.GetPendingEntry());
142 TEST_F(BrowserCommandsTest, BookmarkCurrentPage) {
143 // We use profile() here, since it's a TestingProfile.
144 profile()->CreateBookmarkModel(true);
146 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
147 bookmarks::test::WaitForBookmarkModelToLoad(model);
149 // Navigate to a url.
150 GURL url1("http://foo/1");
151 AddTab(browser(), url1);
152 browser()->OpenURL(OpenURLParams(
153 url1, Referrer(), CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
155 chrome::BookmarkCurrentPageAllowingExtensionOverrides(browser());
157 // It should now be bookmarked in the bookmark model.
158 EXPECT_EQ(profile(), browser()->profile());
159 EXPECT_TRUE(model->IsBookmarked(url1));
162 // Tests back/forward in new tab (Control + Back/Forward button in the UI).
163 TEST_F(BrowserCommandsTest, BackForwardInNewTab) {
164 GURL url1("http://foo/1");
165 GURL url2("http://foo/2");
167 // Make a tab with the two pages navigated in it.
168 AddTab(browser(), url1);
169 NavigateAndCommitActiveTab(url2);
171 // Go back in a new background tab.
172 chrome::GoBack(browser(), NEW_BACKGROUND_TAB);
173 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
174 ASSERT_EQ(2, browser()->tab_strip_model()->count());
176 WebContents* zeroth = browser()->tab_strip_model()->GetWebContentsAt(0);
177 WebContents* first = browser()->tab_strip_model()->GetWebContentsAt(1);
179 // The original tab should be unchanged.
180 EXPECT_EQ(url2, zeroth->GetLastCommittedURL());
181 EXPECT_TRUE(zeroth->GetController().CanGoBack());
182 EXPECT_FALSE(zeroth->GetController().CanGoForward());
184 // The new tab should be like the first one but navigated back. Since we
185 // didn't wait for the load to complete, we can't use GetLastCommittedURL.
186 EXPECT_EQ(url1, first->GetVisibleURL());
187 EXPECT_FALSE(first->GetController().CanGoBack());
188 EXPECT_TRUE(first->GetController().CanGoForward());
190 // Select the second tab and make it go forward in a new background tab.
191 browser()->tab_strip_model()->ActivateTabAt(1, true);
192 // TODO(brettw) bug 11055: It should not be necessary to commit the load here,
193 // but because of this bug, it will assert later if we don't. When the bug is
194 // fixed, one of the three commits here related to this bug should be removed
195 // (to test both codepaths).
196 CommitPendingLoad(&first->GetController());
197 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
198 chrome::GoForward(browser(), NEW_BACKGROUND_TAB);
200 // The previous tab should be unchanged and still in the foreground.
201 EXPECT_EQ(url1, first->GetLastCommittedURL());
202 EXPECT_FALSE(first->GetController().CanGoBack());
203 EXPECT_TRUE(first->GetController().CanGoForward());
204 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
206 // There should be a new tab navigated forward.
207 ASSERT_EQ(3, browser()->tab_strip_model()->count());
208 WebContents* second = browser()->tab_strip_model()->GetWebContentsAt(2);
209 // Since we didn't wait for load to complete, we can't use
210 // GetLastCommittedURL.
211 EXPECT_EQ(url2, second->GetVisibleURL());
212 EXPECT_TRUE(second->GetController().CanGoBack());
213 EXPECT_FALSE(second->GetController().CanGoForward());
215 // Now do back in a new foreground tab. Don't bother re-checking every sngle
216 // thing above, just validate that it's opening properly.
217 browser()->tab_strip_model()->ActivateTabAt(2, true);
218 // TODO(brettw) bug 11055: see the comment above about why we need this.
219 CommitPendingLoad(&second->GetController());
220 chrome::GoBack(browser(), NEW_FOREGROUND_TAB);
221 ASSERT_EQ(3, browser()->tab_strip_model()->active_index());
222 ASSERT_EQ(url1,
223 browser()->tab_strip_model()->GetActiveWebContents()->
224 GetVisibleURL());
226 // Same thing again for forward.
227 // TODO(brettw) bug 11055: see the comment above about why we need this.
228 CommitPendingLoad(&
229 browser()->tab_strip_model()->GetActiveWebContents()->GetController());
230 chrome::GoForward(browser(), NEW_FOREGROUND_TAB);
231 ASSERT_EQ(4, browser()->tab_strip_model()->active_index());
232 ASSERT_EQ(url2,
233 browser()->tab_strip_model()->GetActiveWebContents()->
234 GetVisibleURL());
237 TEST_F(BrowserCommandsTest, OnMaxZoomIn) {
238 TabStripModel* tab_strip_model = browser()->tab_strip_model();
240 GURL url("http://www.google.com");
241 AddTab(browser(), url);
242 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
244 // Continue to zoom in until zoom percent reaches 500.
245 for (int i = 0; i < 9; ++i) {
246 ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_IN);
249 // TODO(a.sarkar.arun@gmail.com): Figure out why Zoom-In menu item is not
250 // disabled after Max-zoom is reached. Force disable Zoom-In menu item
251 // from the context menu since it breaks try jobs on bots.
252 if (chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS))
253 chrome::UpdateCommandEnabled(browser(), IDC_ZOOM_PLUS, false);
255 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
256 EXPECT_FLOAT_EQ(500.0f, zoom_controller->GetZoomPercent());
257 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
258 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
259 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
262 TEST_F(BrowserCommandsTest, OnMaxZoomOut) {
263 TabStripModel* tab_strip_model = browser()->tab_strip_model();
265 GURL url("http://www.google.com");
266 AddTab(browser(), url);
267 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
269 // Continue to zoom out until zoom percent reaches 25.
270 for (int i = 0; i < 7; ++i) {
271 ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_OUT);
274 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
275 EXPECT_FLOAT_EQ(25.0f, zoom_controller->GetZoomPercent());
276 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
277 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
278 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
281 TEST_F(BrowserCommandsTest, OnZoomReset) {
282 TabStripModel* tab_strip_model = browser()->tab_strip_model();
284 GURL url("http://www.google.com");
285 AddTab(browser(), url);
286 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
288 // Change the zoom percentage to 100.
289 ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_RESET);
291 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
292 EXPECT_FLOAT_EQ(100.0f, zoom_controller->GetZoomPercent());
293 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
294 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
295 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
297 // Changing the page scale factor will re-enable IDC_ZOOM_NORMAL
298 zoom_controller->SetPageScaleFactorIsOneForTesting(false);
299 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
302 TEST_F(BrowserCommandsTest, OnZoomLevelChanged) {
303 TabStripModel* tab_strip_model = browser()->tab_strip_model();
305 GURL url("http://www.google.com");
306 AddTab(browser(), url);
307 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
309 // Changing zoom percentage from default should enable all the zoom
310 // NSMenuItems.
311 ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_IN);
313 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
314 EXPECT_FLOAT_EQ(110.0f, zoom_controller->GetZoomPercent());
315 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
316 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
317 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
320 TEST_F(BrowserCommandsTest, OnZoomChangedForActiveTab) {
321 TabStripModel* tab_strip_model = browser()->tab_strip_model();
323 GURL url("http://www.google.com");
324 GURL url1("http://code.google.com");
326 // Add First tab.
327 AddTab(browser(), url);
328 AddTab(browser(), url1);
329 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
331 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
332 EXPECT_FLOAT_EQ(100.0f, zoom_controller->GetZoomPercent());
333 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
334 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
335 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
337 // Add Second tab.
338 WebContents* second_tab = tab_strip_model->GetWebContentsAt(1);
340 tab_strip_model->ActivateTabAt(1, true);
341 EXPECT_TRUE(tab_strip_model->IsTabSelected(1));
342 ui_zoom::PageZoom::Zoom(second_tab, content::PAGE_ZOOM_OUT);
344 zoom_controller = ZoomController::FromWebContents(second_tab);
345 EXPECT_FLOAT_EQ(90.0f, zoom_controller->GetZoomPercent());
346 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
347 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
348 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
351 TEST_F(BrowserCommandsTest, OnDefaultZoomLevelChanged) {
352 TabStripModel* tab_strip_model = browser()->tab_strip_model();
353 GURL url("http://code.google.com");
354 AddTab(browser(), url);
355 WebContents* tab = tab_strip_model->GetWebContentsAt(0);
356 ZoomController* zoom_controller = ZoomController::FromWebContents(tab);
358 // Set the default zoom level to 125.
359 profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(
360 content::ZoomFactorToZoomLevel(1.25));
361 EXPECT_FLOAT_EQ(125.0f, zoom_controller->GetZoomPercent());
363 // Actual Size from context menu should be disabled now.
364 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
365 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
366 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
368 // Change the zoom level.
369 ui_zoom::PageZoom::Zoom(tab, content::PAGE_ZOOM_IN);
371 EXPECT_FLOAT_EQ(150.0f, zoom_controller->GetZoomPercent());
373 // Tab no longer at default zoom hence actual size should be enabled.
374 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
375 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
376 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));