blob: 36793781bf8bd0d4f4094c97121c69dcd7682733 [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
avie4d7b6f2015-12-26 00:59:185#include <stddef.h>
6
[email protected]1a3aba82010-11-08 23:52:547#include "chrome/app/chrome_command_ids.h"
[email protected]fa50dea02012-08-01 23:20:378#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]5d98294912012-06-27 22:57:409#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:1010#include "chrome/browser/ui/browser_commands.h"
[email protected]0665ebe2013-02-13 09:53:1911#include "chrome/browser/ui/browser_finder.h"
[email protected]57892182012-12-03 19:15:3912#include "chrome/browser/ui/tabs/tab_strip_model.h"
a.sarkar.arundaadc712015-02-26 05:39:0813#include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
[email protected]9423d9412009-04-14 22:13:5514#include "chrome/common/url_constants.h"
[email protected]a4ff9eae2011-08-01 19:58:1615#include "chrome/test/base/browser_with_test_window_test.h"
16#include "chrome/test/base/testing_profile.h"
[email protected]a90c8ca2014-05-20 17:16:0417#include "components/bookmarks/browser/bookmark_model.h"
18#include "components/bookmarks/test/bookmark_test_helpers.h"
wjmaclean91ddc472015-01-15 19:58:2719#include "components/ui/zoom/page_zoom.h"
wjmaclean7f63c6b2014-12-09 14:59:5520#include "components/ui/zoom/zoom_controller.h"
[email protected]cdcb1dee2012-01-04 00:46:2021#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:2522#include "content/public/browser/navigation_entry.h"
[email protected]6acde6352012-01-04 16:52:2023#include "content/public/browser/web_contents.h"
creise435e682016-01-20 00:10:1824#include "content/public/test/test_renderer_host.h"
a.sarkar.arundaadc712015-02-26 05:39:0825#include "testing/gtest/include/gtest/gtest.h"
[email protected]be3877f2009-01-14 15:51:1026
27typedef BrowserWithTestWindowTest BrowserCommandsTest;
28
tfarinaa0ec34e2015-01-12 18:46:4829using bookmarks::BookmarkModel;
[email protected]e5d549d2011-12-28 01:29:2030using content::OpenURLParams;
31using content::Referrer;
[email protected]6acde6352012-01-04 16:52:2032using content::WebContents;
wjmaclean7f63c6b2014-12-09 14:59:5533using ui_zoom::ZoomController;
[email protected]e5d549d2011-12-28 01:29:2034
[email protected]be3877f2009-01-14 15:51:1035// Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and
36// IDC_SELECT_LAST_TAB.
[email protected]6df642082009-08-13 22:31:3737TEST_F(BrowserCommandsTest, TabNavigationAccelerators) {
[email protected]8e09c7af2014-06-10 11:46:1738 GURL about_blank(url::kAboutBlankURL);
[email protected]9423d9412009-04-14 22:13:5539
[email protected]be3877f2009-01-14 15:51:1040 // Create three tabs.
[email protected]9423d9412009-04-14 22:13:5541 AddTab(browser(), about_blank);
42 AddTab(browser(), about_blank);
43 AddTab(browser(), about_blank);
[email protected]be3877f2009-01-14 15:51:1044
45 // Select the second tab.
[email protected]57892182012-12-03 19:15:3946 browser()->tab_strip_model()->ActivateTabAt(1, false);
[email protected]be3877f2009-01-14 15:51:1047
[email protected]5d98294912012-06-27 22:57:4048 CommandUpdater* updater = browser()->command_controller()->command_updater();
49
[email protected]be3877f2009-01-14 15:51:1050 // Navigate to the first tab using an accelerator.
[email protected]5d98294912012-06-27 22:57:4051 updater->ExecuteCommand(IDC_SELECT_TAB_0);
[email protected]57892182012-12-03 19:15:3952 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]be3877f2009-01-14 15:51:1053
54 // Navigate to the second tab using the next accelerators.
[email protected]5d98294912012-06-27 22:57:4055 updater->ExecuteCommand(IDC_SELECT_NEXT_TAB);
[email protected]57892182012-12-03 19:15:3956 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]be3877f2009-01-14 15:51:1057
58 // Navigate back to the first tab using the previous accelerators.
[email protected]5d98294912012-06-27 22:57:4059 updater->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
[email protected]57892182012-12-03 19:15:3960 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]be3877f2009-01-14 15:51:1061
62 // Navigate to the last tab using the select last accelerator.
[email protected]5d98294912012-06-27 22:57:4063 updater->ExecuteCommand(IDC_SELECT_LAST_TAB);
[email protected]57892182012-12-03 19:15:3964 ASSERT_EQ(2, browser()->tab_strip_model()->active_index());
[email protected]be3877f2009-01-14 15:51:1065}
66
67// Tests IDC_DUPLICATE_TAB.
68TEST_F(BrowserCommandsTest, DuplicateTab) {
[email protected]9423d9412009-04-14 22:13:5569 GURL url1("http://foo/1");
70 GURL url2("http://foo/2");
71 GURL url3("http://foo/3");
[email protected]8fcc7bf2012-08-14 18:14:5072 GURL url4("http://foo/4");
[email protected]be3877f2009-01-14 15:51:1073
[email protected]8fcc7bf2012-08-14 18:14:5074 // Navigate to three urls, plus a pending URL that hasn't committed.
[email protected]9423d9412009-04-14 22:13:5575 AddTab(browser(), url1);
[email protected]0683cf62009-04-10 19:31:4076 NavigateAndCommitActiveTab(url2);
77 NavigateAndCommitActiveTab(url3);
[email protected]8fcc7bf2012-08-14 18:14:5078 content::NavigationController& orig_controller =
[email protected]57892182012-12-03 19:15:3979 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
[email protected]8fcc7bf2012-08-14 18:14:5080 orig_controller.LoadURL(
Sylvain Defresnec6ccc77d2014-09-19 10:19:3581 url4, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
[email protected]8fcc7bf2012-08-14 18:14:5082 EXPECT_EQ(3, orig_controller.GetEntryCount());
83 EXPECT_TRUE(orig_controller.GetPendingEntry());
[email protected]be3877f2009-01-14 15:51:1084
[email protected]0665ebe2013-02-13 09:53:1985 size_t initial_window_count = chrome::GetTotalBrowserCount();
[email protected]be3877f2009-01-14 15:51:1086
87 // Duplicate the tab.
[email protected]5d98294912012-06-27 22:57:4088 chrome::ExecuteCommand(browser(), IDC_DUPLICATE_TAB);
[email protected]be3877f2009-01-14 15:51:1089
90 // The duplicated tab should not end up in a new window.
[email protected]0665ebe2013-02-13 09:53:1991 size_t window_count = chrome::GetTotalBrowserCount();
[email protected]be3877f2009-01-14 15:51:1092 ASSERT_EQ(initial_window_count, window_count);
93
94 // And we should have a newly duplicated tab.
[email protected]57892182012-12-03 19:15:3995 ASSERT_EQ(2, browser()->tab_strip_model()->count());
[email protected]be3877f2009-01-14 15:51:1096
97 // Verify the stack of urls.
[email protected]cdcb1dee2012-01-04 00:46:2098 content::NavigationController& controller =
[email protected]57892182012-12-03 19:15:3999 browser()->tab_strip_model()->GetWebContentsAt(1)->GetController();
[email protected]8fcc7bf2012-08-14 18:14:50100 EXPECT_EQ(3, controller.GetEntryCount());
101 EXPECT_EQ(2, controller.GetCurrentEntryIndex());
102 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
103 EXPECT_EQ(url2, controller.GetEntryAtIndex(1)->GetURL());
104 EXPECT_EQ(url3, controller.GetEntryAtIndex(2)->GetURL());
105 EXPECT_FALSE(controller.GetPendingEntry());
106}
107
108// Tests IDC_VIEW_SOURCE (See http://crbug.com/138140).
109TEST_F(BrowserCommandsTest, ViewSource) {
110 GURL url1("http://foo/1");
creise435e682016-01-20 00:10:18111 GURL url1_subframe("http://foo/subframe");
[email protected]8fcc7bf2012-08-14 18:14:50112 GURL url2("http://foo/2");
113
creise435e682016-01-20 00:10:18114 // Navigate to a URL and simulate a subframe committing.
[email protected]8fcc7bf2012-08-14 18:14:50115 AddTab(browser(), url1);
creise435e682016-01-20 00:10:18116 content::RenderFrameHostTester* rfh_tester =
117 content::RenderFrameHostTester::For(
118 browser()->tab_strip_model()->GetWebContentsAt(0)->GetMainFrame());
119 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe");
120 content::RenderFrameHostTester* subframe_tester =
121 content::RenderFrameHostTester::For(subframe);
122 subframe_tester->SimulateNavigationStart(GURL(url1_subframe));
123 subframe_tester->SimulateNavigationCommit(GURL(url1_subframe));
124 subframe_tester->SimulateNavigationStop();
125
126 // Now start a pending navigation that hasn't committed.
[email protected]8fcc7bf2012-08-14 18:14:50127 content::NavigationController& orig_controller =
[email protected]57892182012-12-03 19:15:39128 browser()->tab_strip_model()->GetWebContentsAt(0)->GetController();
[email protected]8fcc7bf2012-08-14 18:14:50129 orig_controller.LoadURL(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35130 url2, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
[email protected]8fcc7bf2012-08-14 18:14:50131 EXPECT_EQ(1, orig_controller.GetEntryCount());
132 EXPECT_TRUE(orig_controller.GetPendingEntry());
133
[email protected]0665ebe2013-02-13 09:53:19134 size_t initial_window_count = chrome::GetTotalBrowserCount();
[email protected]8fcc7bf2012-08-14 18:14:50135
136 // View Source.
137 chrome::ExecuteCommand(browser(), IDC_VIEW_SOURCE);
138
139 // The view source tab should not end up in a new window.
[email protected]0665ebe2013-02-13 09:53:19140 size_t window_count = chrome::GetTotalBrowserCount();
[email protected]8fcc7bf2012-08-14 18:14:50141 ASSERT_EQ(initial_window_count, window_count);
142
143 // And we should have a newly duplicated tab.
[email protected]57892182012-12-03 19:15:39144 ASSERT_EQ(2, browser()->tab_strip_model()->count());
[email protected]8fcc7bf2012-08-14 18:14:50145
146 // Verify we are viewing the source of the last committed entry.
147 GURL view_source_url("view-source:http://foo/1");
148 content::NavigationController& controller =
[email protected]57892182012-12-03 19:15:39149 browser()->tab_strip_model()->GetWebContentsAt(1)->GetController();
[email protected]8fcc7bf2012-08-14 18:14:50150 EXPECT_EQ(1, controller.GetEntryCount());
151 EXPECT_EQ(0, controller.GetCurrentEntryIndex());
152 EXPECT_EQ(url1, controller.GetEntryAtIndex(0)->GetURL());
153 EXPECT_EQ(view_source_url, controller.GetEntryAtIndex(0)->GetVirtualURL());
154 EXPECT_FALSE(controller.GetPendingEntry());
[email protected]be3877f2009-01-14 15:51:10155}
[email protected]b7ca4e62009-01-23 20:37:29156
157TEST_F(BrowserCommandsTest, BookmarkCurrentPage) {
158 // We use profile() here, since it's a TestingProfile.
159 profile()->CreateBookmarkModel(true);
[email protected]1f9c74192013-03-25 23:04:35160
161 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
tfarina8f4aae22014-10-23 17:46:11162 bookmarks::test::WaitForBookmarkModelToLoad(model);
[email protected]b7ca4e62009-01-23 20:37:29163
164 // Navigate to a url.
[email protected]9423d9412009-04-14 22:13:55165 GURL url1("http://foo/1");
166 AddTab(browser(), url1);
[email protected]e5d549d2011-12-28 01:29:20167 browser()->OpenURL(OpenURLParams(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35168 url1, Referrer(), CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
[email protected]b7ca4e62009-01-23 20:37:29169
deepak.m154a7f392014-12-15 04:41:43170 chrome::BookmarkCurrentPageAllowingExtensionOverrides(browser());
[email protected]b7ca4e62009-01-23 20:37:29171
172 // It should now be bookmarked in the bookmark model.
173 EXPECT_EQ(profile(), browser()->profile());
[email protected]1f9c74192013-03-25 23:04:35174 EXPECT_TRUE(model->IsBookmarked(url1));
[email protected]b7ca4e62009-01-23 20:37:29175}
[email protected]e21e8c92009-04-29 02:42:09176
177// Tests back/forward in new tab (Control + Back/Forward button in the UI).
178TEST_F(BrowserCommandsTest, BackForwardInNewTab) {
179 GURL url1("http://foo/1");
180 GURL url2("http://foo/2");
181
182 // Make a tab with the two pages navigated in it.
183 AddTab(browser(), url1);
184 NavigateAndCommitActiveTab(url2);
185
186 // Go back in a new background tab.
[email protected]a37d4b02012-06-25 21:56:10187 chrome::GoBack(browser(), NEW_BACKGROUND_TAB);
[email protected]57892182012-12-03 19:15:39188 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
189 ASSERT_EQ(2, browser()->tab_strip_model()->count());
[email protected]e21e8c92009-04-29 02:42:09190
[email protected]57892182012-12-03 19:15:39191 WebContents* zeroth = browser()->tab_strip_model()->GetWebContentsAt(0);
[email protected]e022aca2013-07-18 19:10:18192 WebContents* first = browser()->tab_strip_model()->GetWebContentsAt(1);
193
194 // The original tab should be unchanged.
195 EXPECT_EQ(url2, zeroth->GetLastCommittedURL());
[email protected]f5fa20e2011-12-21 22:35:56196 EXPECT_TRUE(zeroth->GetController().CanGoBack());
197 EXPECT_FALSE(zeroth->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09198
[email protected]e022aca2013-07-18 19:10:18199 // The new tab should be like the first one but navigated back. Since we
200 // didn't wait for the load to complete, we can't use GetLastCommittedURL.
[email protected]a093ce02013-07-22 20:53:14201 EXPECT_EQ(url1, first->GetVisibleURL());
[email protected]f5fa20e2011-12-21 22:35:56202 EXPECT_FALSE(first->GetController().CanGoBack());
203 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09204
205 // Select the second tab and make it go forward in a new background tab.
[email protected]57892182012-12-03 19:15:39206 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]e21e8c92009-04-29 02:42:09207 // TODO(brettw) bug 11055: It should not be necessary to commit the load here,
208 // but because of this bug, it will assert later if we don't. When the bug is
209 // fixed, one of the three commits here related to this bug should be removed
210 // (to test both codepaths).
[email protected]f5fa20e2011-12-21 22:35:56211 CommitPendingLoad(&first->GetController());
[email protected]57892182012-12-03 19:15:39212 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]a37d4b02012-06-25 21:56:10213 chrome::GoForward(browser(), NEW_BACKGROUND_TAB);
[email protected]e21e8c92009-04-29 02:42:09214
215 // The previous tab should be unchanged and still in the foreground.
[email protected]e022aca2013-07-18 19:10:18216 EXPECT_EQ(url1, first->GetLastCommittedURL());
[email protected]f5fa20e2011-12-21 22:35:56217 EXPECT_FALSE(first->GetController().CanGoBack());
218 EXPECT_TRUE(first->GetController().CanGoForward());
[email protected]57892182012-12-03 19:15:39219 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]e21e8c92009-04-29 02:42:09220
221 // There should be a new tab navigated forward.
[email protected]57892182012-12-03 19:15:39222 ASSERT_EQ(3, browser()->tab_strip_model()->count());
223 WebContents* second = browser()->tab_strip_model()->GetWebContentsAt(2);
[email protected]e022aca2013-07-18 19:10:18224 // Since we didn't wait for load to complete, we can't use
225 // GetLastCommittedURL.
[email protected]a093ce02013-07-22 20:53:14226 EXPECT_EQ(url2, second->GetVisibleURL());
[email protected]f5fa20e2011-12-21 22:35:56227 EXPECT_TRUE(second->GetController().CanGoBack());
228 EXPECT_FALSE(second->GetController().CanGoForward());
[email protected]e21e8c92009-04-29 02:42:09229
230 // Now do back in a new foreground tab. Don't bother re-checking every sngle
231 // thing above, just validate that it's opening properly.
[email protected]57892182012-12-03 19:15:39232 browser()->tab_strip_model()->ActivateTabAt(2, true);
[email protected]e21e8c92009-04-29 02:42:09233 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]f5fa20e2011-12-21 22:35:56234 CommitPendingLoad(&second->GetController());
[email protected]a37d4b02012-06-25 21:56:10235 chrome::GoBack(browser(), NEW_FOREGROUND_TAB);
[email protected]57892182012-12-03 19:15:39236 ASSERT_EQ(3, browser()->tab_strip_model()->active_index());
237 ASSERT_EQ(url1,
[email protected]e022aca2013-07-18 19:10:18238 browser()->tab_strip_model()->GetActiveWebContents()->
[email protected]a093ce02013-07-22 20:53:14239 GetVisibleURL());
[email protected]e21e8c92009-04-29 02:42:09240
241 // Same thing again for forward.
242 // TODO(brettw) bug 11055: see the comment above about why we need this.
[email protected]57892182012-12-03 19:15:39243 CommitPendingLoad(&
244 browser()->tab_strip_model()->GetActiveWebContents()->GetController());
[email protected]a37d4b02012-06-25 21:56:10245 chrome::GoForward(browser(), NEW_FOREGROUND_TAB);
[email protected]57892182012-12-03 19:15:39246 ASSERT_EQ(4, browser()->tab_strip_model()->active_index());
247 ASSERT_EQ(url2,
[email protected]e022aca2013-07-18 19:10:18248 browser()->tab_strip_model()->GetActiveWebContents()->
[email protected]a093ce02013-07-22 20:53:14249 GetVisibleURL());
[email protected]e21e8c92009-04-29 02:42:09250}
[email protected]d93dbd12014-08-04 23:42:53251
252TEST_F(BrowserCommandsTest, OnMaxZoomIn) {
253 TabStripModel* tab_strip_model = browser()->tab_strip_model();
254
255 GURL url("http://www.google.com");
256 AddTab(browser(), url);
a.sarkar.arun93c53be2015-02-27 17:10:10257 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
[email protected]d93dbd12014-08-04 23:42:53258
259 // Continue to zoom in until zoom percent reaches 500.
260 for (int i = 0; i < 9; ++i) {
a.sarkar.arun93c53be2015-02-27 17:10:10261 ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_IN);
[email protected]d93dbd12014-08-04 23:42:53262 }
263
264 // TODO([email protected]): Figure out why Zoom-In menu item is not
265 // disabled after Max-zoom is reached. Force disable Zoom-In menu item
266 // from the context menu since it breaks try jobs on bots.
267 if (chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS))
268 chrome::UpdateCommandEnabled(browser(), IDC_ZOOM_PLUS, false);
269
a.sarkar.arun93c53be2015-02-27 17:10:10270 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
a.sarkar.arundaadc712015-02-26 05:39:08271 EXPECT_FLOAT_EQ(500.0f, zoom_controller->GetZoomPercent());
[email protected]d93dbd12014-08-04 23:42:53272 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
273 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
274 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
275}
276
277TEST_F(BrowserCommandsTest, OnMaxZoomOut) {
278 TabStripModel* tab_strip_model = browser()->tab_strip_model();
279
280 GURL url("http://www.google.com");
281 AddTab(browser(), url);
a.sarkar.arun93c53be2015-02-27 17:10:10282 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
[email protected]d93dbd12014-08-04 23:42:53283
284 // Continue to zoom out until zoom percent reaches 25.
285 for (int i = 0; i < 7; ++i) {
a.sarkar.arun93c53be2015-02-27 17:10:10286 ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_OUT);
[email protected]d93dbd12014-08-04 23:42:53287 }
288
a.sarkar.arun93c53be2015-02-27 17:10:10289 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
a.sarkar.arundaadc712015-02-26 05:39:08290 EXPECT_FLOAT_EQ(25.0f, zoom_controller->GetZoomPercent());
[email protected]d93dbd12014-08-04 23:42:53291 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
292 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
293 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
294}
295
296TEST_F(BrowserCommandsTest, OnZoomReset) {
297 TabStripModel* tab_strip_model = browser()->tab_strip_model();
298
299 GURL url("http://www.google.com");
300 AddTab(browser(), url);
a.sarkar.arun93c53be2015-02-27 17:10:10301 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
[email protected]d93dbd12014-08-04 23:42:53302
303 // Change the zoom percentage to 100.
a.sarkar.arun93c53be2015-02-27 17:10:10304 ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_RESET);
[email protected]d93dbd12014-08-04 23:42:53305
a.sarkar.arun93c53be2015-02-27 17:10:10306 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
a.sarkar.arundaadc712015-02-26 05:39:08307 EXPECT_FLOAT_EQ(100.0f, zoom_controller->GetZoomPercent());
[email protected]d93dbd12014-08-04 23:42:53308 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
309 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
310 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
ccameronb7c1d6c2015-03-09 17:08:24311
312 // Changing the page scale factor will re-enable IDC_ZOOM_NORMAL
313 zoom_controller->SetPageScaleFactorIsOneForTesting(false);
314 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
[email protected]d93dbd12014-08-04 23:42:53315}
316
317TEST_F(BrowserCommandsTest, OnZoomLevelChanged) {
318 TabStripModel* tab_strip_model = browser()->tab_strip_model();
319
320 GURL url("http://www.google.com");
321 AddTab(browser(), url);
a.sarkar.arun93c53be2015-02-27 17:10:10322 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
[email protected]d93dbd12014-08-04 23:42:53323
324 // Changing zoom percentage from default should enable all the zoom
325 // NSMenuItems.
a.sarkar.arun93c53be2015-02-27 17:10:10326 ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_IN);
[email protected]d93dbd12014-08-04 23:42:53327
a.sarkar.arun93c53be2015-02-27 17:10:10328 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
a.sarkar.arundaadc712015-02-26 05:39:08329 EXPECT_FLOAT_EQ(110.0f, zoom_controller->GetZoomPercent());
[email protected]d93dbd12014-08-04 23:42:53330 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
331 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
332 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
333}
334
335TEST_F(BrowserCommandsTest, OnZoomChangedForActiveTab) {
336 TabStripModel* tab_strip_model = browser()->tab_strip_model();
337
338 GURL url("http://www.google.com");
339 GURL url1("http://code.google.com");
340
341 // Add First tab.
342 AddTab(browser(), url);
343 AddTab(browser(), url1);
a.sarkar.arun93c53be2015-02-27 17:10:10344 WebContents* first_tab = tab_strip_model->GetWebContentsAt(0);
[email protected]d93dbd12014-08-04 23:42:53345
a.sarkar.arun93c53be2015-02-27 17:10:10346 ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab);
a.sarkar.arundaadc712015-02-26 05:39:08347 EXPECT_FLOAT_EQ(100.0f, zoom_controller->GetZoomPercent());
[email protected]d93dbd12014-08-04 23:42:53348 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
349 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
350 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
351
352 // Add Second tab.
a.sarkar.arun93c53be2015-02-27 17:10:10353 WebContents* second_tab = tab_strip_model->GetWebContentsAt(1);
[email protected]d93dbd12014-08-04 23:42:53354
355 tab_strip_model->ActivateTabAt(1, true);
356 EXPECT_TRUE(tab_strip_model->IsTabSelected(1));
a.sarkar.arun93c53be2015-02-27 17:10:10357 ui_zoom::PageZoom::Zoom(second_tab, content::PAGE_ZOOM_OUT);
[email protected]d93dbd12014-08-04 23:42:53358
a.sarkar.arun93c53be2015-02-27 17:10:10359 zoom_controller = ZoomController::FromWebContents(second_tab);
a.sarkar.arundaadc712015-02-26 05:39:08360 EXPECT_FLOAT_EQ(90.0f, zoom_controller->GetZoomPercent());
361 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
362 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
363 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
364}
365
366TEST_F(BrowserCommandsTest, OnDefaultZoomLevelChanged) {
367 TabStripModel* tab_strip_model = browser()->tab_strip_model();
368 GURL url("http://code.google.com");
369 AddTab(browser(), url);
a.sarkar.arun93c53be2015-02-27 17:10:10370 WebContents* tab = tab_strip_model->GetWebContentsAt(0);
371 ZoomController* zoom_controller = ZoomController::FromWebContents(tab);
a.sarkar.arundaadc712015-02-26 05:39:08372
373 // Set the default zoom level to 125.
374 profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(
375 content::ZoomFactorToZoomLevel(1.25));
376 EXPECT_FLOAT_EQ(125.0f, zoom_controller->GetZoomPercent());
377
378 // Actual Size from context menu should be disabled now.
379 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
380 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
381 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
382
383 // Change the zoom level.
a.sarkar.arun93c53be2015-02-27 17:10:10384 ui_zoom::PageZoom::Zoom(tab, content::PAGE_ZOOM_IN);
a.sarkar.arundaadc712015-02-26 05:39:08385
386 EXPECT_FLOAT_EQ(150.0f, zoom_controller->GetZoomPercent());
387
388 // Tab no longer at default zoom hence actual size should be enabled.
[email protected]d93dbd12014-08-04 23:42:53389 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_PLUS));
390 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_NORMAL));
391 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ZOOM_MINUS));
392}