[email protected] | 93cc27b | 2012-03-21 12:44:32 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | 1a3aba8 | 2010-11-08 23:52:54 | [diff] [blame] | 7 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | fa50dea0 | 2012-08-01 23:20:37 | [diff] [blame] | 8 | #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 9 | #include "chrome/browser/ui/browser_command_controller.h" |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser_finder.h" |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 12 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 13 | #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 14 | #include "chrome/common/url_constants.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 15 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 16 | #include "chrome/test/base/testing_profile.h" |
[email protected] | a90c8ca | 2014-05-20 17:16:04 | [diff] [blame] | 17 | #include "components/bookmarks/browser/bookmark_model.h" |
| 18 | #include "components/bookmarks/test/bookmark_test_helpers.h" |
wjmaclean | 91ddc47 | 2015-01-15 19:58:27 | [diff] [blame] | 19 | #include "components/ui/zoom/page_zoom.h" |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 20 | #include "components/ui/zoom/zoom_controller.h" |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 21 | #include "content/public/browser/navigation_controller.h" |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 22 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 23 | #include "content/public/browser/web_contents.h" |
creis | e435e68 | 2016-01-20 00:10:18 | [diff] [blame^] | 24 | #include "content/public/test/test_renderer_host.h" |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 25 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 26 | |
| 27 | typedef BrowserWithTestWindowTest BrowserCommandsTest; |
| 28 | |
tfarina | a0ec34e | 2015-01-12 18:46:48 | [diff] [blame] | 29 | using bookmarks::BookmarkModel; |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 30 | using content::OpenURLParams; |
| 31 | using content::Referrer; |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 32 | using content::WebContents; |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 33 | using ui_zoom::ZoomController; |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 34 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 35 | // Tests IDC_SELECT_TAB_0, IDC_SELECT_NEXT_TAB, IDC_SELECT_PREVIOUS_TAB and |
| 36 | // IDC_SELECT_LAST_TAB. |
[email protected] | 6df64208 | 2009-08-13 22:31:37 | [diff] [blame] | 37 | TEST_F(BrowserCommandsTest, TabNavigationAccelerators) { |
[email protected] | 8e09c7af | 2014-06-10 11:46:17 | [diff] [blame] | 38 | GURL about_blank(url::kAboutBlankURL); |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 39 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 40 | // Create three tabs. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 41 | AddTab(browser(), about_blank); |
| 42 | AddTab(browser(), about_blank); |
| 43 | AddTab(browser(), about_blank); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 44 | |
| 45 | // Select the second tab. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 46 | browser()->tab_strip_model()->ActivateTabAt(1, false); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 47 | |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 48 | CommandUpdater* updater = browser()->command_controller()->command_updater(); |
| 49 | |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 50 | // Navigate to the first tab using an accelerator. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 51 | updater->ExecuteCommand(IDC_SELECT_TAB_0); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 52 | ASSERT_EQ(0, browser()->tab_strip_model()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 53 | |
| 54 | // Navigate to the second tab using the next accelerators. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 55 | updater->ExecuteCommand(IDC_SELECT_NEXT_TAB); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 56 | ASSERT_EQ(1, browser()->tab_strip_model()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 57 | |
| 58 | // Navigate back to the first tab using the previous accelerators. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 59 | updater->ExecuteCommand(IDC_SELECT_PREVIOUS_TAB); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 60 | ASSERT_EQ(0, browser()->tab_strip_model()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 61 | |
| 62 | // Navigate to the last tab using the select last accelerator. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 63 | updater->ExecuteCommand(IDC_SELECT_LAST_TAB); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 64 | ASSERT_EQ(2, browser()->tab_strip_model()->active_index()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Tests IDC_DUPLICATE_TAB. |
| 68 | TEST_F(BrowserCommandsTest, DuplicateTab) { |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 69 | GURL url1("http://foo/1"); |
| 70 | GURL url2("http://foo/2"); |
| 71 | GURL url3("http://foo/3"); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 72 | GURL url4("http://foo/4"); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 73 | |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 74 | // Navigate to three urls, plus a pending URL that hasn't committed. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 75 | AddTab(browser(), url1); |
[email protected] | 0683cf6 | 2009-04-10 19:31:40 | [diff] [blame] | 76 | NavigateAndCommitActiveTab(url2); |
| 77 | NavigateAndCommitActiveTab(url3); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 78 | content::NavigationController& orig_controller = |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 79 | browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 80 | orig_controller.LoadURL( |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 81 | url4, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 82 | EXPECT_EQ(3, orig_controller.GetEntryCount()); |
| 83 | EXPECT_TRUE(orig_controller.GetPendingEntry()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 84 | |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 85 | size_t initial_window_count = chrome::GetTotalBrowserCount(); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 86 | |
| 87 | // Duplicate the tab. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 88 | chrome::ExecuteCommand(browser(), IDC_DUPLICATE_TAB); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 89 | |
| 90 | // The duplicated tab should not end up in a new window. |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 91 | size_t window_count = chrome::GetTotalBrowserCount(); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 92 | ASSERT_EQ(initial_window_count, window_count); |
| 93 | |
| 94 | // And we should have a newly duplicated tab. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 95 | ASSERT_EQ(2, browser()->tab_strip_model()->count()); |
[email protected] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 96 | |
| 97 | // Verify the stack of urls. |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 98 | content::NavigationController& controller = |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 99 | browser()->tab_strip_model()->GetWebContentsAt(1)->GetController(); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 100 | 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). |
| 109 | TEST_F(BrowserCommandsTest, ViewSource) { |
| 110 | GURL url1("http://foo/1"); |
creis | e435e68 | 2016-01-20 00:10:18 | [diff] [blame^] | 111 | GURL url1_subframe("http://foo/subframe"); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 112 | GURL url2("http://foo/2"); |
| 113 | |
creis | e435e68 | 2016-01-20 00:10:18 | [diff] [blame^] | 114 | // Navigate to a URL and simulate a subframe committing. |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 115 | AddTab(browser(), url1); |
creis | e435e68 | 2016-01-20 00:10:18 | [diff] [blame^] | 116 | 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] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 127 | content::NavigationController& orig_controller = |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 128 | browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 129 | orig_controller.LoadURL( |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 130 | url2, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 131 | EXPECT_EQ(1, orig_controller.GetEntryCount()); |
| 132 | EXPECT_TRUE(orig_controller.GetPendingEntry()); |
| 133 | |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 134 | size_t initial_window_count = chrome::GetTotalBrowserCount(); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 135 | |
| 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] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 140 | size_t window_count = chrome::GetTotalBrowserCount(); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 141 | ASSERT_EQ(initial_window_count, window_count); |
| 142 | |
| 143 | // And we should have a newly duplicated tab. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 144 | ASSERT_EQ(2, browser()->tab_strip_model()->count()); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 145 | |
| 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] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 149 | browser()->tab_strip_model()->GetWebContentsAt(1)->GetController(); |
[email protected] | 8fcc7bf | 2012-08-14 18:14:50 | [diff] [blame] | 150 | 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] | be3877f | 2009-01-14 15:51:10 | [diff] [blame] | 155 | } |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 156 | |
| 157 | TEST_F(BrowserCommandsTest, BookmarkCurrentPage) { |
| 158 | // We use profile() here, since it's a TestingProfile. |
| 159 | profile()->CreateBookmarkModel(true); |
[email protected] | 1f9c7419 | 2013-03-25 23:04:35 | [diff] [blame] | 160 | |
| 161 | BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
tfarina | 8f4aae2 | 2014-10-23 17:46:11 | [diff] [blame] | 162 | bookmarks::test::WaitForBookmarkModelToLoad(model); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 163 | |
| 164 | // Navigate to a url. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 165 | GURL url1("http://foo/1"); |
| 166 | AddTab(browser(), url1); |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 167 | browser()->OpenURL(OpenURLParams( |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 168 | url1, Referrer(), CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false)); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 169 | |
deepak.m1 | 54a7f39 | 2014-12-15 04:41:43 | [diff] [blame] | 170 | chrome::BookmarkCurrentPageAllowingExtensionOverrides(browser()); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 171 | |
| 172 | // It should now be bookmarked in the bookmark model. |
| 173 | EXPECT_EQ(profile(), browser()->profile()); |
[email protected] | 1f9c7419 | 2013-03-25 23:04:35 | [diff] [blame] | 174 | EXPECT_TRUE(model->IsBookmarked(url1)); |
[email protected] | b7ca4e6 | 2009-01-23 20:37:29 | [diff] [blame] | 175 | } |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 176 | |
| 177 | // Tests back/forward in new tab (Control + Back/Forward button in the UI). |
| 178 | TEST_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] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 187 | chrome::GoBack(browser(), NEW_BACKGROUND_TAB); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 188 | EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
| 189 | ASSERT_EQ(2, browser()->tab_strip_model()->count()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 190 | |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 191 | WebContents* zeroth = browser()->tab_strip_model()->GetWebContentsAt(0); |
[email protected] | e022aca | 2013-07-18 19:10:18 | [diff] [blame] | 192 | WebContents* first = browser()->tab_strip_model()->GetWebContentsAt(1); |
| 193 | |
| 194 | // The original tab should be unchanged. |
| 195 | EXPECT_EQ(url2, zeroth->GetLastCommittedURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 196 | EXPECT_TRUE(zeroth->GetController().CanGoBack()); |
| 197 | EXPECT_FALSE(zeroth->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 198 | |
[email protected] | e022aca | 2013-07-18 19:10:18 | [diff] [blame] | 199 | // 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] | a093ce0 | 2013-07-22 20:53:14 | [diff] [blame] | 201 | EXPECT_EQ(url1, first->GetVisibleURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 202 | EXPECT_FALSE(first->GetController().CanGoBack()); |
| 203 | EXPECT_TRUE(first->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 204 | |
| 205 | // Select the second tab and make it go forward in a new background tab. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 206 | browser()->tab_strip_model()->ActivateTabAt(1, true); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 207 | // 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] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 211 | CommitPendingLoad(&first->GetController()); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 212 | EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 213 | chrome::GoForward(browser(), NEW_BACKGROUND_TAB); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 214 | |
| 215 | // The previous tab should be unchanged and still in the foreground. |
[email protected] | e022aca | 2013-07-18 19:10:18 | [diff] [blame] | 216 | EXPECT_EQ(url1, first->GetLastCommittedURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 217 | EXPECT_FALSE(first->GetController().CanGoBack()); |
| 218 | EXPECT_TRUE(first->GetController().CanGoForward()); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 219 | EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 220 | |
| 221 | // There should be a new tab navigated forward. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 222 | ASSERT_EQ(3, browser()->tab_strip_model()->count()); |
| 223 | WebContents* second = browser()->tab_strip_model()->GetWebContentsAt(2); |
[email protected] | e022aca | 2013-07-18 19:10:18 | [diff] [blame] | 224 | // Since we didn't wait for load to complete, we can't use |
| 225 | // GetLastCommittedURL. |
[email protected] | a093ce0 | 2013-07-22 20:53:14 | [diff] [blame] | 226 | EXPECT_EQ(url2, second->GetVisibleURL()); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 227 | EXPECT_TRUE(second->GetController().CanGoBack()); |
| 228 | EXPECT_FALSE(second->GetController().CanGoForward()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 229 | |
| 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] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 232 | browser()->tab_strip_model()->ActivateTabAt(2, true); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 233 | // TODO(brettw) bug 11055: see the comment above about why we need this. |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 234 | CommitPendingLoad(&second->GetController()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 235 | chrome::GoBack(browser(), NEW_FOREGROUND_TAB); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 236 | ASSERT_EQ(3, browser()->tab_strip_model()->active_index()); |
| 237 | ASSERT_EQ(url1, |
[email protected] | e022aca | 2013-07-18 19:10:18 | [diff] [blame] | 238 | browser()->tab_strip_model()->GetActiveWebContents()-> |
[email protected] | a093ce0 | 2013-07-22 20:53:14 | [diff] [blame] | 239 | GetVisibleURL()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 240 | |
| 241 | // Same thing again for forward. |
| 242 | // TODO(brettw) bug 11055: see the comment above about why we need this. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 243 | CommitPendingLoad(& |
| 244 | browser()->tab_strip_model()->GetActiveWebContents()->GetController()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 245 | chrome::GoForward(browser(), NEW_FOREGROUND_TAB); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 246 | ASSERT_EQ(4, browser()->tab_strip_model()->active_index()); |
| 247 | ASSERT_EQ(url2, |
[email protected] | e022aca | 2013-07-18 19:10:18 | [diff] [blame] | 248 | browser()->tab_strip_model()->GetActiveWebContents()-> |
[email protected] | a093ce0 | 2013-07-22 20:53:14 | [diff] [blame] | 249 | GetVisibleURL()); |
[email protected] | e21e8c9 | 2009-04-29 02:42:09 | [diff] [blame] | 250 | } |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 251 | |
| 252 | TEST_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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 257 | WebContents* first_tab = tab_strip_model->GetWebContentsAt(0); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 258 | |
| 259 | // Continue to zoom in until zoom percent reaches 500. |
| 260 | for (int i = 0; i < 9; ++i) { |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 261 | ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_IN); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 262 | } |
| 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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 270 | ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab); |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 271 | EXPECT_FLOAT_EQ(500.0f, zoom_controller->GetZoomPercent()); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 272 | 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 | |
| 277 | TEST_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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 282 | WebContents* first_tab = tab_strip_model->GetWebContentsAt(0); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 283 | |
| 284 | // Continue to zoom out until zoom percent reaches 25. |
| 285 | for (int i = 0; i < 7; ++i) { |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 286 | ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_OUT); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 287 | } |
| 288 | |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 289 | ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab); |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 290 | EXPECT_FLOAT_EQ(25.0f, zoom_controller->GetZoomPercent()); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 291 | 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 | |
| 296 | TEST_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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 301 | WebContents* first_tab = tab_strip_model->GetWebContentsAt(0); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 302 | |
| 303 | // Change the zoom percentage to 100. |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 304 | ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_RESET); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 305 | |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 306 | ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab); |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 307 | EXPECT_FLOAT_EQ(100.0f, zoom_controller->GetZoomPercent()); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 308 | 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)); |
ccameron | b7c1d6c | 2015-03-09 17:08:24 | [diff] [blame] | 311 | |
| 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] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | TEST_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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 322 | WebContents* first_tab = tab_strip_model->GetWebContentsAt(0); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 323 | |
| 324 | // Changing zoom percentage from default should enable all the zoom |
| 325 | // NSMenuItems. |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 326 | ui_zoom::PageZoom::Zoom(first_tab, content::PAGE_ZOOM_IN); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 327 | |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 328 | ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab); |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 329 | EXPECT_FLOAT_EQ(110.0f, zoom_controller->GetZoomPercent()); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 330 | 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 | |
| 335 | TEST_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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 344 | WebContents* first_tab = tab_strip_model->GetWebContentsAt(0); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 345 | |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 346 | ZoomController* zoom_controller = ZoomController::FromWebContents(first_tab); |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 347 | EXPECT_FLOAT_EQ(100.0f, zoom_controller->GetZoomPercent()); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 348 | 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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 353 | WebContents* second_tab = tab_strip_model->GetWebContentsAt(1); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 354 | |
| 355 | tab_strip_model->ActivateTabAt(1, true); |
| 356 | EXPECT_TRUE(tab_strip_model->IsTabSelected(1)); |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 357 | ui_zoom::PageZoom::Zoom(second_tab, content::PAGE_ZOOM_OUT); |
[email protected] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 358 | |
a.sarkar.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 359 | zoom_controller = ZoomController::FromWebContents(second_tab); |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 360 | 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 | |
| 366 | TEST_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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 370 | WebContents* tab = tab_strip_model->GetWebContentsAt(0); |
| 371 | ZoomController* zoom_controller = ZoomController::FromWebContents(tab); |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 372 | |
| 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.arun | 93c53be | 2015-02-27 17:10:10 | [diff] [blame] | 384 | ui_zoom::PageZoom::Zoom(tab, content::PAGE_ZOOM_IN); |
a.sarkar.arun | daadc71 | 2015-02-26 05:39:08 | [diff] [blame] | 385 | |
| 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] | d93dbd1 | 2014-08-04 23:42:53 | [diff] [blame] | 389 | 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 | } |