[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 1 | // Copyright 2014 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. |
| 4 | |
juncai | a64f769c2 | 2016-06-04 00:55:32 | [diff] [blame] | 5 | #include "components/zoom/zoom_controller.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 6 | |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 7 | #include "base/macros.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 8 | #include "base/process/kill.h" |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 9 | #include "build/build_config.h" |
wjmaclean | 32a991c | 2014-09-02 14:13:47 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser.h" |
wjmaclean | 7431bb2 | 2015-02-19 14:53:43 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_commands.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 13 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
rogerta | e77304c | 2015-01-07 02:53:55 | [diff] [blame] | 14 | #include "chrome/browser/ui/webui/signin/login_ui_test_utils.h" |
wjmaclean | e530aa74 | 2014-10-14 21:43:30 | [diff] [blame] | 15 | #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 16 | #include "chrome/common/url_constants.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 17 | #include "chrome/test/base/in_process_browser_test.h" |
| 18 | #include "chrome/test/base/ui_test_utils.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 19 | #include "components/prefs/pref_service.h" |
David Roger | a6c8812 | 2017-10-25 13:02:46 | [diff] [blame] | 20 | #include "components/signin/core/browser/profile_management_switches.h" |
Kevin McNee | f6cf6ac | 2017-11-02 19:07:27 | [diff] [blame^] | 21 | #include "components/zoom/test/zoom_test_utils.h" |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 22 | #include "content/public/browser/host_zoom_map.h" |
wjmaclean | e6a5d22 | 2014-09-25 01:41:41 | [diff] [blame] | 23 | #include "content/public/browser/navigation_entry.h" |
Lukasz Anforowicz | a192ffb | 2017-10-02 20:36:38 | [diff] [blame] | 24 | #include "content/public/browser/render_frame_host.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 25 | #include "content/public/browser/render_process_host.h" |
wjmaclean | 5eeba91 | 2014-09-02 20:23:47 | [diff] [blame] | 26 | #include "content/public/browser/render_view_host.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 27 | #include "content/public/browser/web_contents.h" |
wjmaclean | e6a5d22 | 2014-09-25 01:41:41 | [diff] [blame] | 28 | #include "content/public/common/page_type.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 29 | #include "content/public/test/browser_test_utils.h" |
wjmaclean | 32a991c | 2014-09-02 14:13:47 | [diff] [blame] | 30 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 31 | |
Kevin McNee | f6cf6ac | 2017-11-02 19:07:27 | [diff] [blame^] | 32 | using zoom::ZoomChangedWatcher; |
juncai | a64f769c2 | 2016-06-04 00:55:32 | [diff] [blame] | 33 | using zoom::ZoomController; |
| 34 | using zoom::ZoomObserver; |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 35 | |
wjmaclean | 96fc2e9 | 2015-01-28 20:42:11 | [diff] [blame] | 36 | class ZoomControllerBrowserTest : public InProcessBrowserTest { |
| 37 | public: |
| 38 | ZoomControllerBrowserTest() {} |
| 39 | ~ZoomControllerBrowserTest() override {} |
| 40 | |
| 41 | void TestResetOnNavigation(ZoomController::ZoomMode zoom_mode) { |
| 42 | DCHECK(zoom_mode == ZoomController::ZOOM_MODE_ISOLATED || |
| 43 | zoom_mode == ZoomController::ZOOM_MODE_MANUAL); |
| 44 | content::WebContents* web_contents = |
| 45 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 46 | ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 47 | browser(), GURL("about:blank"), 1); |
| 48 | ZoomController* zoom_controller = |
| 49 | ZoomController::FromWebContents(web_contents); |
| 50 | double zoom_level = zoom_controller->GetDefaultZoomLevel(); |
| 51 | zoom_controller->SetZoomMode(zoom_mode); |
| 52 | |
| 53 | // When the navigation occurs, the zoom_mode will be reset to |
| 54 | // ZOOM_MODE_DEFAULT, and this will be reflected in the event that |
| 55 | // is generated. |
| 56 | ZoomController::ZoomChangedEventData zoom_change_data( |
| 57 | web_contents, zoom_level, zoom_level, ZoomController::ZOOM_MODE_DEFAULT, |
| 58 | false); |
| 59 | ZoomChangedWatcher zoom_change_watcher(web_contents, zoom_change_data); |
| 60 | |
| 61 | ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUISettingsURL)); |
| 62 | zoom_change_watcher.Wait(); |
| 63 | } |
| 64 | }; // ZoomControllerBrowserTest |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 65 | |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 66 | #if defined(OS_ANDROID) |
| 67 | #define MAYBE_CrashedTabsDoNotChangeZoom DISABLED_CrashedTabsDoNotChangeZoom |
| 68 | #else |
| 69 | #define MAYBE_CrashedTabsDoNotChangeZoom CrashedTabsDoNotChangeZoom |
| 70 | #endif |
| 71 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, |
| 72 | MAYBE_CrashedTabsDoNotChangeZoom) { |
| 73 | // At the start of the test we are at a tab displaying about:blank. |
| 74 | content::WebContents* web_contents = |
| 75 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 76 | |
| 77 | ZoomController* zoom_controller = |
| 78 | ZoomController::FromWebContents(web_contents); |
| 79 | |
| 80 | double old_zoom_level = zoom_controller->GetZoomLevel(); |
| 81 | double new_zoom_level = old_zoom_level + 0.5; |
| 82 | |
Lukasz Anforowicz | a192ffb | 2017-10-02 20:36:38 | [diff] [blame] | 83 | content::RenderProcessHost* host = web_contents->GetMainFrame()->GetProcess(); |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 84 | { |
| 85 | content::RenderProcessHostWatcher crash_observer( |
| 86 | host, content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
jaekyun | 37e572a3 | 2014-12-04 23:33:35 | [diff] [blame] | 87 | host->Shutdown(0, false); |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 88 | crash_observer.Wait(); |
| 89 | } |
wjmaclean | 5eeba91 | 2014-09-02 20:23:47 | [diff] [blame] | 90 | EXPECT_FALSE(web_contents->GetRenderViewHost()->IsRenderViewLive()); |
[email protected] | c17d9e6 | 2014-08-18 19:36:52 | [diff] [blame] | 91 | |
| 92 | // The following attempt to change the zoom level for a crashed tab should |
| 93 | // fail. |
| 94 | zoom_controller->SetZoomLevel(new_zoom_level); |
| 95 | EXPECT_FLOAT_EQ(old_zoom_level, zoom_controller->GetZoomLevel()); |
| 96 | } |
wjmaclean | 32a991c | 2014-09-02 14:13:47 | [diff] [blame] | 97 | |
| 98 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, OnPreferenceChanged) { |
| 99 | content::WebContents* web_contents = |
| 100 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 101 | double new_default_zoom_level = 1.0; |
| 102 | // Since this page uses the default zoom level, the changes to the default |
| 103 | // zoom level will change the zoom level for this web_contents. |
| 104 | ZoomController::ZoomChangedEventData zoom_change_data( |
| 105 | web_contents, |
| 106 | new_default_zoom_level, |
| 107 | new_default_zoom_level, |
| 108 | ZoomController::ZOOM_MODE_DEFAULT, |
wjmaclean | ff3d0f6 | 2015-02-25 19:38:36 | [diff] [blame] | 109 | false); |
wjmaclean | 32a991c | 2014-09-02 14:13:47 | [diff] [blame] | 110 | ZoomChangedWatcher zoom_change_watcher(web_contents, zoom_change_data); |
| 111 | // TODO(wjmaclean): Convert this to call partition-specific zoom level prefs |
| 112 | // when they become available. |
wjmaclean | e530aa74 | 2014-10-14 21:43:30 | [diff] [blame] | 113 | browser()->profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref( |
| 114 | new_default_zoom_level); |
wjmaclean | 32a991c | 2014-09-02 14:13:47 | [diff] [blame] | 115 | // Because this test relies on a round-trip IPC to/from the renderer process, |
| 116 | // we need to wait for it to propagate. |
| 117 | zoom_change_watcher.Wait(); |
| 118 | } |
wjmaclean | e6a5d22 | 2014-09-25 01:41:41 | [diff] [blame] | 119 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 120 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, ErrorPagesCanZoom) { |
wjmaclean | e6a5d22 | 2014-09-25 01:41:41 | [diff] [blame] | 121 | ui_test_utils::NavigateToURL(browser(), GURL("http://kjfhkjsdf.com")); |
| 122 | content::WebContents* web_contents = |
| 123 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 124 | |
| 125 | ZoomController* zoom_controller = |
| 126 | ZoomController::FromWebContents(web_contents); |
| 127 | EXPECT_EQ( |
| 128 | content::PAGE_TYPE_ERROR, |
| 129 | web_contents->GetController().GetLastCommittedEntry()->GetPageType()); |
| 130 | |
| 131 | double old_zoom_level = zoom_controller->GetZoomLevel(); |
| 132 | double new_zoom_level = old_zoom_level + 0.5; |
| 133 | |
| 134 | // The following attempt to change the zoom level for an error page should |
| 135 | // fail. |
| 136 | zoom_controller->SetZoomLevel(new_zoom_level); |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 137 | EXPECT_FLOAT_EQ(new_zoom_level, zoom_controller->GetZoomLevel()); |
wjmaclean | e6a5d22 | 2014-09-25 01:41:41 | [diff] [blame] | 138 | } |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 139 | |
wjmaclean | 7431bb2 | 2015-02-19 14:53:43 | [diff] [blame] | 140 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, |
| 141 | ErrorPagesCanZoomAfterTabRestore) { |
| 142 | // This url is meant to cause a network error page to be loaded. |
| 143 | // Tests can't reach the network, so this test should continue |
| 144 | // to work even if the domain listed is someday registered. |
| 145 | GURL url("http://kjfhkjsdf.com"); |
| 146 | |
| 147 | TabStripModel* tab_strip = browser()->tab_strip_model(); |
| 148 | ASSERT_TRUE(tab_strip); |
| 149 | |
| 150 | ui_test_utils::NavigateToURLWithDisposition( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 151 | browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB, |
wjmaclean | 7431bb2 | 2015-02-19 14:53:43 | [diff] [blame] | 152 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 153 | { |
| 154 | content::WebContents* web_contents = tab_strip->GetActiveWebContents(); |
| 155 | |
| 156 | EXPECT_EQ( |
| 157 | content::PAGE_TYPE_ERROR, |
| 158 | web_contents->GetController().GetLastCommittedEntry()->GetPageType()); |
| 159 | |
| 160 | content::WebContentsDestroyedWatcher destroyed_watcher(web_contents); |
| 161 | tab_strip->CloseWebContentsAt(tab_strip->active_index(), |
| 162 | TabStripModel::CLOSE_CREATE_HISTORICAL_TAB); |
| 163 | destroyed_watcher.Wait(); |
| 164 | } |
| 165 | EXPECT_EQ(1, tab_strip->count()); |
| 166 | |
| 167 | content::WebContentsAddedObserver new_web_contents_observer; |
| 168 | chrome::RestoreTab(browser()); |
| 169 | content::WebContents* web_contents = |
| 170 | new_web_contents_observer.GetWebContents(); |
| 171 | content::WaitForLoadStop(web_contents); |
| 172 | |
| 173 | EXPECT_EQ(2, tab_strip->count()); |
| 174 | |
| 175 | EXPECT_EQ( |
| 176 | content::PAGE_TYPE_ERROR, |
| 177 | web_contents->GetController().GetLastCommittedEntry()->GetPageType()); |
| 178 | |
| 179 | ZoomController* zoom_controller = |
| 180 | ZoomController::FromWebContents(web_contents); |
| 181 | |
| 182 | double old_zoom_level = zoom_controller->GetZoomLevel(); |
| 183 | double new_zoom_level = old_zoom_level + 0.5; |
| 184 | |
| 185 | // The following attempt to change the zoom level for an error page should |
| 186 | // fail. |
| 187 | zoom_controller->SetZoomLevel(new_zoom_level); |
| 188 | EXPECT_FLOAT_EQ(new_zoom_level, zoom_controller->GetZoomLevel()); |
| 189 | } |
| 190 | |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 191 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, Observe) { |
| 192 | content::WebContents* web_contents = |
| 193 | browser()->tab_strip_model()->GetActiveWebContents(); |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 194 | |
| 195 | double new_zoom_level = 1.0; |
| 196 | // When the event is initiated from HostZoomMap, the old zoom level is not |
| 197 | // available. |
| 198 | ZoomController::ZoomChangedEventData zoom_change_data( |
| 199 | web_contents, |
| 200 | new_zoom_level, |
| 201 | new_zoom_level, |
| 202 | ZoomController::ZOOM_MODE_DEFAULT, |
wjmaclean | ff3d0f6 | 2015-02-25 19:38:36 | [diff] [blame] | 203 | false); // The ZoomController did not initiate, so this will be 'false'. |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 204 | ZoomChangedWatcher zoom_change_watcher(web_contents, zoom_change_data); |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 205 | |
| 206 | content::HostZoomMap* host_zoom_map = |
| 207 | content::HostZoomMap::GetDefaultForBrowserContext( |
| 208 | web_contents->GetBrowserContext()); |
| 209 | |
| 210 | host_zoom_map->SetZoomLevelForHost("about:blank", new_zoom_level); |
wjmaclean | 7f63c6b | 2014-12-09 14:59:55 | [diff] [blame] | 211 | zoom_change_watcher.Wait(); |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 212 | } |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 213 | |
wjmaclean | a9373ca | 2015-04-01 22:53:26 | [diff] [blame] | 214 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, ObserveDisabledModeEvent) { |
| 215 | content::WebContents* web_contents = |
| 216 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 217 | |
| 218 | ZoomController* zoom_controller = |
| 219 | ZoomController::FromWebContents(web_contents); |
| 220 | |
| 221 | double default_zoom_level = zoom_controller->GetDefaultZoomLevel(); |
| 222 | double new_zoom_level = default_zoom_level + 1.0; |
| 223 | zoom_controller->SetZoomLevel(new_zoom_level); |
| 224 | |
| 225 | ZoomController::ZoomChangedEventData zoom_change_data( |
| 226 | web_contents, |
| 227 | new_zoom_level, |
| 228 | default_zoom_level, |
| 229 | ZoomController::ZOOM_MODE_DISABLED, |
| 230 | true); |
| 231 | ZoomChangedWatcher zoom_change_watcher(web_contents, zoom_change_data); |
| 232 | zoom_controller->SetZoomMode(ZoomController::ZOOM_MODE_DISABLED); |
| 233 | zoom_change_watcher.Wait(); |
| 234 | } |
| 235 | |
wjmaclean | d96f864 | 2015-01-07 20:10:57 | [diff] [blame] | 236 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, PerTabModeResetSendsEvent) { |
wjmaclean | 96fc2e9 | 2015-01-28 20:42:11 | [diff] [blame] | 237 | TestResetOnNavigation(ZoomController::ZOOM_MODE_ISOLATED); |
| 238 | } |
wjmaclean | d96f864 | 2015-01-07 20:10:57 | [diff] [blame] | 239 | |
wjmaclean | 96fc2e9 | 2015-01-28 20:42:11 | [diff] [blame] | 240 | // Regression test: crbug.com/450909. |
| 241 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, NavigationResetsManualMode) { |
| 242 | TestResetOnNavigation(ZoomController::ZOOM_MODE_MANUAL); |
wjmaclean | d96f864 | 2015-01-07 20:10:57 | [diff] [blame] | 243 | } |
| 244 | |
rogerta | e77304c | 2015-01-07 02:53:55 | [diff] [blame] | 245 | #if !defined(OS_CHROMEOS) |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 246 | // Regression test: crbug.com/438979. |
| 247 | IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest, |
| 248 | SettingsZoomAfterSigninWorks) { |
gogerald | 71bf6c90 | 2015-12-08 00:49:37 | [diff] [blame] | 249 | GURL signin_url(std::string(chrome::kChromeUIChromeSigninURL) |
| 250 | .append("?access_point=0&reason=0")); |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 251 | // We open the signin page in a new tab so that the ZoomController is |
| 252 | // created against the HostZoomMap of the special StoragePartition that |
| 253 | // backs the signin page. When we subsequently navigate away from the |
| 254 | // signin page, the HostZoomMap changes, and we need to test that the |
| 255 | // ZoomController correctly detects this. |
rogerta | e77304c | 2015-01-07 02:53:55 | [diff] [blame] | 256 | ui_test_utils::NavigateToURLWithDisposition( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 257 | browser(), signin_url, WindowOpenDisposition::NEW_FOREGROUND_TAB, |
rogerta | e77304c | 2015-01-07 02:53:55 | [diff] [blame] | 258 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 259 | login_ui_test_utils::WaitUntilUIReady(browser()); |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 260 | content::WebContents* web_contents = |
| 261 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 262 | EXPECT_NE( |
| 263 | content::PAGE_TYPE_ERROR, |
| 264 | web_contents->GetController().GetLastCommittedEntry()->GetPageType()); |
| 265 | |
| 266 | EXPECT_EQ(signin_url, web_contents->GetLastCommittedURL()); |
| 267 | ZoomController* zoom_controller = |
| 268 | ZoomController::FromWebContents(web_contents); |
| 269 | |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 270 | GURL settings_url(chrome::kChromeUISettingsURL); |
| 271 | ui_test_utils::NavigateToURL(browser(), settings_url); |
| 272 | EXPECT_NE( |
| 273 | content::PAGE_TYPE_ERROR, |
| 274 | web_contents->GetController().GetLastCommittedEntry()->GetPageType()); |
| 275 | |
| 276 | // Verify new tab was created. |
| 277 | EXPECT_EQ(2, browser()->tab_strip_model()->count()); |
| 278 | // Verify that the settings page is using the same WebContents. |
| 279 | EXPECT_EQ(web_contents, browser()->tab_strip_model()->GetActiveWebContents()); |
| 280 | // TODO(wjmaclean): figure out why this next line fails, i.e. why does this |
| 281 | // test not properly trigger a navigation to the settings page. |
| 282 | EXPECT_EQ(settings_url, web_contents->GetLastCommittedURL()); |
| 283 | EXPECT_EQ(zoom_controller, ZoomController::FromWebContents(web_contents)); |
| 284 | |
wjmaclean | c7bcb77d | 2014-12-16 15:05:38 | [diff] [blame] | 285 | // If we zoom the new page, it should still generate a ZoomController event. |
| 286 | double old_zoom_level = zoom_controller->GetZoomLevel(); |
| 287 | double new_zoom_level = old_zoom_level + 0.5; |
| 288 | |
| 289 | ZoomController::ZoomChangedEventData zoom_change_data( |
| 290 | web_contents, |
| 291 | old_zoom_level, |
| 292 | new_zoom_level, |
| 293 | ZoomController::ZOOM_MODE_DEFAULT, |
| 294 | true); // We have a non-empty host, so this will be 'true'. |
| 295 | ZoomChangedWatcher zoom_change_watcher(web_contents, zoom_change_data); |
| 296 | zoom_controller->SetZoomLevel(new_zoom_level); |
| 297 | zoom_change_watcher.Wait(); |
| 298 | } |
rogerta | e77304c | 2015-01-07 02:53:55 | [diff] [blame] | 299 | #endif // !defined(OS_CHROMEOS) |