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.
5 #include "components/ui/zoom/zoom_controller.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/process/kill.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_commands.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/browser/ui/webui/signin/login_ui_test_utils.h"
14 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
15 #include "chrome/common/url_constants.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/signin/core/common/profile_management_switches.h"
19 #include "content/public/browser/host_zoom_map.h"
20 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/page_type.h"
25 #include "content/public/test/browser_test_utils.h"
26 #include "testing/gmock/include/gmock/gmock.h"
28 using ui_zoom::ZoomController
;
29 using ui_zoom::ZoomObserver
;
31 bool operator==(const ZoomController::ZoomChangedEventData
& lhs
,
32 const ZoomController::ZoomChangedEventData
& rhs
) {
33 return lhs
.web_contents
== rhs
.web_contents
&&
34 lhs
.old_zoom_level
== rhs
.old_zoom_level
&&
35 lhs
.new_zoom_level
== rhs
.new_zoom_level
&&
36 lhs
.zoom_mode
== rhs
.zoom_mode
&&
37 lhs
.can_show_bubble
== rhs
.can_show_bubble
;
40 class ZoomChangedWatcher
: public ZoomObserver
{
43 content::WebContents
* web_contents
,
44 const ZoomController::ZoomChangedEventData
& expected_event_data
)
45 : expected_event_data_(expected_event_data
),
46 message_loop_runner_(new content::MessageLoopRunner
) {
47 ZoomController::FromWebContents(web_contents
)->AddObserver(this);
49 ~ZoomChangedWatcher() override
{}
51 void Wait() { message_loop_runner_
->Run(); }
54 const ZoomController::ZoomChangedEventData
& event_data
) override
{
55 if (event_data
== expected_event_data_
)
56 message_loop_runner_
->Quit();
60 ZoomController::ZoomChangedEventData expected_event_data_
;
61 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
63 DISALLOW_COPY_AND_ASSIGN(ZoomChangedWatcher
);
66 class ZoomControllerBrowserTest
: public InProcessBrowserTest
{
68 ZoomControllerBrowserTest() {}
69 ~ZoomControllerBrowserTest() override
{}
71 void TestResetOnNavigation(ZoomController::ZoomMode zoom_mode
) {
72 DCHECK(zoom_mode
== ZoomController::ZOOM_MODE_ISOLATED
||
73 zoom_mode
== ZoomController::ZOOM_MODE_MANUAL
);
74 content::WebContents
* web_contents
=
75 browser()->tab_strip_model()->GetActiveWebContents();
76 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
77 browser(), GURL("about:blank"), 1);
78 ZoomController
* zoom_controller
=
79 ZoomController::FromWebContents(web_contents
);
80 double zoom_level
= zoom_controller
->GetDefaultZoomLevel();
81 zoom_controller
->SetZoomMode(zoom_mode
);
83 // When the navigation occurs, the zoom_mode will be reset to
84 // ZOOM_MODE_DEFAULT, and this will be reflected in the event that
86 ZoomController::ZoomChangedEventData
zoom_change_data(
87 web_contents
, zoom_level
, zoom_level
, ZoomController::ZOOM_MODE_DEFAULT
,
89 ZoomChangedWatcher
zoom_change_watcher(web_contents
, zoom_change_data
);
91 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUISettingsURL
));
92 zoom_change_watcher
.Wait();
94 }; // ZoomControllerBrowserTest
96 #if defined(OS_ANDROID)
97 #define MAYBE_CrashedTabsDoNotChangeZoom DISABLED_CrashedTabsDoNotChangeZoom
99 #define MAYBE_CrashedTabsDoNotChangeZoom CrashedTabsDoNotChangeZoom
101 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
,
102 MAYBE_CrashedTabsDoNotChangeZoom
) {
103 // At the start of the test we are at a tab displaying about:blank.
104 content::WebContents
* web_contents
=
105 browser()->tab_strip_model()->GetActiveWebContents();
107 ZoomController
* zoom_controller
=
108 ZoomController::FromWebContents(web_contents
);
110 double old_zoom_level
= zoom_controller
->GetZoomLevel();
111 double new_zoom_level
= old_zoom_level
+ 0.5;
113 content::RenderProcessHost
* host
= web_contents
->GetRenderProcessHost();
115 content::RenderProcessHostWatcher
crash_observer(
116 host
, content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
117 host
->Shutdown(0, false);
118 crash_observer
.Wait();
120 EXPECT_FALSE(web_contents
->GetRenderViewHost()->IsRenderViewLive());
122 // The following attempt to change the zoom level for a crashed tab should
124 zoom_controller
->SetZoomLevel(new_zoom_level
);
125 EXPECT_FLOAT_EQ(old_zoom_level
, zoom_controller
->GetZoomLevel());
128 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
, OnPreferenceChanged
) {
129 content::WebContents
* web_contents
=
130 browser()->tab_strip_model()->GetActiveWebContents();
131 double new_default_zoom_level
= 1.0;
132 // Since this page uses the default zoom level, the changes to the default
133 // zoom level will change the zoom level for this web_contents.
134 ZoomController::ZoomChangedEventData
zoom_change_data(
136 new_default_zoom_level
,
137 new_default_zoom_level
,
138 ZoomController::ZOOM_MODE_DEFAULT
,
140 ZoomChangedWatcher
zoom_change_watcher(web_contents
, zoom_change_data
);
141 // TODO(wjmaclean): Convert this to call partition-specific zoom level prefs
142 // when they become available.
143 browser()->profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(
144 new_default_zoom_level
);
145 // Because this test relies on a round-trip IPC to/from the renderer process,
146 // we need to wait for it to propagate.
147 zoom_change_watcher
.Wait();
150 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
, ErrorPagesCanZoom
) {
151 ui_test_utils::NavigateToURL(browser(), GURL("http://kjfhkjsdf.com"));
152 content::WebContents
* web_contents
=
153 browser()->tab_strip_model()->GetActiveWebContents();
155 ZoomController
* zoom_controller
=
156 ZoomController::FromWebContents(web_contents
);
158 content::PAGE_TYPE_ERROR
,
159 web_contents
->GetController().GetLastCommittedEntry()->GetPageType());
161 double old_zoom_level
= zoom_controller
->GetZoomLevel();
162 double new_zoom_level
= old_zoom_level
+ 0.5;
164 // The following attempt to change the zoom level for an error page should
166 zoom_controller
->SetZoomLevel(new_zoom_level
);
167 EXPECT_FLOAT_EQ(new_zoom_level
, zoom_controller
->GetZoomLevel());
170 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
,
171 ErrorPagesCanZoomAfterTabRestore
) {
172 // This url is meant to cause a network error page to be loaded.
173 // Tests can't reach the network, so this test should continue
174 // to work even if the domain listed is someday registered.
175 GURL
url("http://kjfhkjsdf.com");
177 TabStripModel
* tab_strip
= browser()->tab_strip_model();
178 ASSERT_TRUE(tab_strip
);
180 ui_test_utils::NavigateToURLWithDisposition(
181 browser(), url
, NEW_FOREGROUND_TAB
,
182 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
184 content::WebContents
* web_contents
= tab_strip
->GetActiveWebContents();
187 content::PAGE_TYPE_ERROR
,
188 web_contents
->GetController().GetLastCommittedEntry()->GetPageType());
190 content::WebContentsDestroyedWatcher
destroyed_watcher(web_contents
);
191 tab_strip
->CloseWebContentsAt(tab_strip
->active_index(),
192 TabStripModel::CLOSE_CREATE_HISTORICAL_TAB
);
193 destroyed_watcher
.Wait();
195 EXPECT_EQ(1, tab_strip
->count());
197 content::WebContentsAddedObserver new_web_contents_observer
;
198 chrome::RestoreTab(browser());
199 content::WebContents
* web_contents
=
200 new_web_contents_observer
.GetWebContents();
201 content::WaitForLoadStop(web_contents
);
203 EXPECT_EQ(2, tab_strip
->count());
206 content::PAGE_TYPE_ERROR
,
207 web_contents
->GetController().GetLastCommittedEntry()->GetPageType());
209 ZoomController
* zoom_controller
=
210 ZoomController::FromWebContents(web_contents
);
212 double old_zoom_level
= zoom_controller
->GetZoomLevel();
213 double new_zoom_level
= old_zoom_level
+ 0.5;
215 // The following attempt to change the zoom level for an error page should
217 zoom_controller
->SetZoomLevel(new_zoom_level
);
218 EXPECT_FLOAT_EQ(new_zoom_level
, zoom_controller
->GetZoomLevel());
221 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
, Observe
) {
222 content::WebContents
* web_contents
=
223 browser()->tab_strip_model()->GetActiveWebContents();
225 double new_zoom_level
= 1.0;
226 // When the event is initiated from HostZoomMap, the old zoom level is not
228 ZoomController::ZoomChangedEventData
zoom_change_data(
232 ZoomController::ZOOM_MODE_DEFAULT
,
233 false); // The ZoomController did not initiate, so this will be 'false'.
234 ZoomChangedWatcher
zoom_change_watcher(web_contents
, zoom_change_data
);
236 content::HostZoomMap
* host_zoom_map
=
237 content::HostZoomMap::GetDefaultForBrowserContext(
238 web_contents
->GetBrowserContext());
240 host_zoom_map
->SetZoomLevelForHost("about:blank", new_zoom_level
);
241 zoom_change_watcher
.Wait();
244 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
, ObserveDisabledModeEvent
) {
245 content::WebContents
* web_contents
=
246 browser()->tab_strip_model()->GetActiveWebContents();
248 ZoomController
* zoom_controller
=
249 ZoomController::FromWebContents(web_contents
);
251 double default_zoom_level
= zoom_controller
->GetDefaultZoomLevel();
252 double new_zoom_level
= default_zoom_level
+ 1.0;
253 zoom_controller
->SetZoomLevel(new_zoom_level
);
255 ZoomController::ZoomChangedEventData
zoom_change_data(
259 ZoomController::ZOOM_MODE_DISABLED
,
261 ZoomChangedWatcher
zoom_change_watcher(web_contents
, zoom_change_data
);
262 zoom_controller
->SetZoomMode(ZoomController::ZOOM_MODE_DISABLED
);
263 zoom_change_watcher
.Wait();
266 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
, PerTabModeResetSendsEvent
) {
267 TestResetOnNavigation(ZoomController::ZOOM_MODE_ISOLATED
);
270 // Regression test: crbug.com/450909.
271 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
, NavigationResetsManualMode
) {
272 TestResetOnNavigation(ZoomController::ZOOM_MODE_MANUAL
);
275 #if !defined(OS_CHROMEOS)
276 // Regression test: crbug.com/438979.
277 IN_PROC_BROWSER_TEST_F(ZoomControllerBrowserTest
,
278 SettingsZoomAfterSigninWorks
) {
280 std::string(chrome::kChromeUIChromeSigninURL
).append("?source=0"));
281 // We open the signin page in a new tab so that the ZoomController is
282 // created against the HostZoomMap of the special StoragePartition that
283 // backs the signin page. When we subsequently navigate away from the
284 // signin page, the HostZoomMap changes, and we need to test that the
285 // ZoomController correctly detects this.
286 ui_test_utils::NavigateToURLWithDisposition(
287 browser(), signin_url
, NEW_FOREGROUND_TAB
,
288 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
289 login_ui_test_utils::WaitUntilUIReady(browser());
290 content::WebContents
* web_contents
=
291 browser()->tab_strip_model()->GetActiveWebContents();
293 content::PAGE_TYPE_ERROR
,
294 web_contents
->GetController().GetLastCommittedEntry()->GetPageType());
296 EXPECT_EQ(signin_url
, web_contents
->GetLastCommittedURL());
297 ZoomController
* zoom_controller
=
298 ZoomController::FromWebContents(web_contents
);
300 content::HostZoomMap
* host_zoom_map_signin
=
301 content::HostZoomMap::GetForWebContents(web_contents
);
303 GURL
settings_url(chrome::kChromeUISettingsURL
);
304 ui_test_utils::NavigateToURL(browser(), settings_url
);
306 content::PAGE_TYPE_ERROR
,
307 web_contents
->GetController().GetLastCommittedEntry()->GetPageType());
309 // Verify new tab was created.
310 EXPECT_EQ(2, browser()->tab_strip_model()->count());
311 // Verify that the settings page is using the same WebContents.
312 EXPECT_EQ(web_contents
, browser()->tab_strip_model()->GetActiveWebContents());
313 // TODO(wjmaclean): figure out why this next line fails, i.e. why does this
314 // test not properly trigger a navigation to the settings page.
315 EXPECT_EQ(settings_url
, web_contents
->GetLastCommittedURL());
316 EXPECT_EQ(zoom_controller
, ZoomController::FromWebContents(web_contents
));
318 // For the webview based sign-in code, the sign in page uses the default host
320 if (!switches::IsEnableWebviewBasedSignin()) {
321 // We expect the navigation from the chrome sign in page to the settings
322 // page to invoke a storage partition switch, and thus a different
323 // HostZoomMap for the web_contents.
324 content::HostZoomMap
* host_zoom_map_settings
=
325 content::HostZoomMap::GetForWebContents(web_contents
);
326 EXPECT_NE(host_zoom_map_signin
, host_zoom_map_settings
);
329 // If we zoom the new page, it should still generate a ZoomController event.
330 double old_zoom_level
= zoom_controller
->GetZoomLevel();
331 double new_zoom_level
= old_zoom_level
+ 0.5;
333 ZoomController::ZoomChangedEventData
zoom_change_data(
337 ZoomController::ZOOM_MODE_DEFAULT
,
338 true); // We have a non-empty host, so this will be 'true'.
339 ZoomChangedWatcher
zoom_change_watcher(web_contents
, zoom_change_data
);
340 zoom_controller
->SetZoomLevel(new_zoom_level
);
341 zoom_change_watcher
.Wait();
343 #endif // !defined(OS_CHROMEOS)