Fix UnlockEndpointIsDelayed again.
[chromium-blink-merge.git] / chrome / browser / browser_commands_unittest.cc
blob7a6077c7215075e76fa9aab797fc5a2fb1ff2b0b
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/common/url_constants.h"
12 #include "chrome/test/base/browser_with_test_window_test.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "components/bookmarks/test/bookmark_test_helpers.h"
16 #include "components/ui/zoom/page_zoom.h"
17 #include "components/ui/zoom/zoom_controller.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/web_contents.h"
22 typedef BrowserWithTestWindowTest BrowserCommandsTest;
24 using bookmarks::BookmarkModel;
25 using content::OpenURLParams;
26 using content::Referrer;
27 using content::WebContents;
28 using ui_zoom::ZoomController;
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(url::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(), ui::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(), ui::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 bookmarks::test::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, ui::PAGE_TRANSITION_TYPED, false));
153 chrome::BookmarkCurrentPageAllowingExtensionOverrides(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 WebContents* zeroth = browser()->tab_strip_model()->GetWebContentsAt(0);
175 WebContents* first = browser()->tab_strip_model()->GetWebContentsAt(1);
177 // The original tab should be unchanged.
178 EXPECT_EQ(url2, zeroth->GetLastCommittedURL());
179 EXPECT_TRUE(zeroth->GetController().CanGoBack());
180 EXPECT_FALSE(zeroth->GetController().CanGoForward());
182 // The new tab should be like the first one but navigated back. Since we
183 // didn't wait for the load to complete, we can't use GetLastCommittedURL.
184 EXPECT_EQ(url1, first->GetVisibleURL());
185 EXPECT_FALSE(first->GetController().CanGoBack());
186 EXPECT_TRUE(first->GetController().CanGoForward());
188 // Select the second tab and make it go forward in a new background tab.
189 browser()->tab_strip_model()->ActivateTabAt(1, true);
190 // TODO(brettw) bug 11055: It should not be necessary to commit the load here,
191 // but because of this bug, it will assert later if we don't. When the bug is
192 // fixed, one of the three commits here related to this bug should be removed
193 // (to test both codepaths).
194 CommitPendingLoad(&first->GetController());
195 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
196 chrome::GoForward(browser(), NEW_BACKGROUND_TAB);
198 // The previous tab should be unchanged and still in the foreground.
199 EXPECT_EQ(url1, first->GetLastCommittedURL());
200 EXPECT_FALSE(first->GetController().CanGoBack());
201 EXPECT_TRUE(first->GetController().CanGoForward());
202 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
204 // There should be a new tab navigated forward.
205 ASSERT_EQ(3, browser()->tab_strip_model()->count());
206 WebContents* second = browser()->tab_strip_model()->GetWebContentsAt(2);
207 // Since we didn't wait for load to complete, we can't use
208 // GetLastCommittedURL.
209 EXPECT_EQ(url2, second->GetVisibleURL());
210 EXPECT_TRUE(second->GetController().CanGoBack());
211 EXPECT_FALSE(second->GetController().CanGoForward());
213 // Now do back in a new foreground tab. Don't bother re-checking every sngle
214 // thing above, just validate that it's opening properly.
215 browser()->tab_strip_model()->ActivateTabAt(2, true);
216 // TODO(brettw) bug 11055: see the comment above about why we need this.
217 CommitPendingLoad(&second->GetController());
218 chrome::GoBack(browser(), NEW_FOREGROUND_TAB);
219 ASSERT_EQ(3, browser()->tab_strip_model()->active_index());
220 ASSERT_EQ(url1,
221 browser()->tab_strip_model()->GetActiveWebContents()->
222 GetVisibleURL());
224 // Same thing again for forward.
225 // TODO(brettw) bug 11055: see the comment above about why we need this.
226 CommitPendingLoad(&
227 browser()->tab_strip_model()->GetActiveWebContents()->GetController());
228 chrome::GoForward(browser(), NEW_FOREGROUND_TAB);
229 ASSERT_EQ(4, browser()->tab_strip_model()->active_index());
230 ASSERT_EQ(url2,
231 browser()->tab_strip_model()->GetActiveWebContents()->
232 GetVisibleURL());
235 TEST_F(BrowserCommandsTest, OnMaxZoomIn) {
236 TabStripModel* tab_strip_model = browser()->tab_strip_model();
238 GURL url("http://www.google.com");
239 AddTab(browser(), url);
240 content::WebContents* contents1 = tab_strip_model->GetWebContentsAt(0);
242 // Continue to zoom in until zoom percent reaches 500.
243 for (int i = 0; i < 9; ++i) {
244 ui_zoom::PageZoom::Zoom(contents1, content::PAGE_ZOOM_IN);
247 // TODO(a.sarkar.arun@gmail.com): Figure out why Zoom-In menu item is not
248 // disabled after Max-zoom is reached. Force disable Zoom-In menu item
249 // from the context menu since it breaks try jobs on bots.
250 if (chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS))
251 chrome::UpdateCommandEnabled(browser(), IDC_ZOOM_PLUS, false);
253 ZoomController* zoom_controller = ZoomController::FromWebContents(contents1);
254 EXPECT_EQ(zoom_controller->GetZoomPercent(), 500.0f);
255 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
256 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
257 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
260 TEST_F(BrowserCommandsTest, OnMaxZoomOut) {
261 TabStripModel* tab_strip_model = browser()->tab_strip_model();
263 GURL url("http://www.google.com");
264 AddTab(browser(), url);
265 content::WebContents* contents1 = tab_strip_model->GetWebContentsAt(0);
267 // Continue to zoom out until zoom percent reaches 25.
268 for (int i = 0; i < 7; ++i) {
269 ui_zoom::PageZoom::Zoom(contents1, content::PAGE_ZOOM_OUT);
272 ZoomController* zoom_controller = ZoomController::FromWebContents(contents1);
273 EXPECT_EQ(zoom_controller->GetZoomPercent(), 25.0f);
274 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
275 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
276 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
279 TEST_F(BrowserCommandsTest, OnZoomReset) {
280 TabStripModel* tab_strip_model = browser()->tab_strip_model();
282 GURL url("http://www.google.com");
283 AddTab(browser(), url);
284 content::WebContents* contents1 = tab_strip_model->GetWebContentsAt(0);
286 // Change the zoom percentage to 100.
287 ui_zoom::PageZoom::Zoom(contents1, content::PAGE_ZOOM_RESET);
289 ZoomController* zoom_controller = ZoomController::FromWebContents(contents1);
290 EXPECT_EQ(zoom_controller->GetZoomPercent(), 100.0f);
291 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
292 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
293 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
296 TEST_F(BrowserCommandsTest, OnZoomLevelChanged) {
297 TabStripModel* tab_strip_model = browser()->tab_strip_model();
299 GURL url("http://www.google.com");
300 AddTab(browser(), url);
301 content::WebContents* contents1 = tab_strip_model->GetWebContentsAt(0);
303 // Changing zoom percentage from default should enable all the zoom
304 // NSMenuItems.
305 ui_zoom::PageZoom::Zoom(contents1, content::PAGE_ZOOM_IN);
307 ZoomController* zoom_controller = ZoomController::FromWebContents(contents1);
308 EXPECT_EQ(zoom_controller->GetZoomPercent(), 110.0f);
309 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
310 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
311 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
314 TEST_F(BrowserCommandsTest, OnZoomChangedForActiveTab) {
315 TabStripModel* tab_strip_model = browser()->tab_strip_model();
317 GURL url("http://www.google.com");
318 GURL url1("http://code.google.com");
320 // Add First tab.
321 AddTab(browser(), url);
322 AddTab(browser(), url1);
323 content::WebContents* contents1 = tab_strip_model->GetWebContentsAt(0);
325 ZoomController* zoom_controller = ZoomController::FromWebContents(contents1);
326 EXPECT_EQ(zoom_controller->GetZoomPercent(), 100.0f);
327 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
328 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
329 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
331 // Add Second tab.
332 content::WebContents* contents2 = tab_strip_model->GetWebContentsAt(1);
334 tab_strip_model->ActivateTabAt(1, true);
335 EXPECT_TRUE(tab_strip_model->IsTabSelected(1));
336 ui_zoom::PageZoom::Zoom(contents2, content::PAGE_ZOOM_OUT);
338 zoom_controller = ZoomController::FromWebContents(contents2);
339 EXPECT_EQ(zoom_controller->GetZoomPercent(), 90.0f);
340 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
341 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
342 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));