blob: 72ca68f63e1cb242e10b6bc743332b46a494e39d [file] [log] [blame]
wjmacleanc62490492015-02-13 22:02:071// Copyright 2015 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
5#include "content/browser/host_zoom_map_impl.h"
6
Lukasz Anforowicza34824252017-10-02 20:31:347#include "content/public/browser/render_frame_host.h"
wjmacleanc62490492015-02-13 22:02:078#include "content/public/browser/render_process_host.h"
9#include "content/public/browser/render_view_host.h"
10#include "content/public/browser/web_contents.h"
11#include "content/public/test/content_browser_test.h"
wjmaclean64951902016-04-29 20:59:1212#include "content/public/test/content_browser_test_utils.h"
wjmacleanc62490492015-02-13 22:02:0713#include "content/shell/browser/shell.h"
wjmaclean64951902016-04-29 20:59:1214#include "net/dns/mock_host_resolver.h"
wjmacleanc62490492015-02-13 22:02:0715#include "url/gurl.h"
16
17namespace content {
18
19class HostZoomMapImplBrowserTest : public ContentBrowserTest {
wjmaclean64951902016-04-29 20:59:1220 protected:
21 void SetUpOnMainThread() override {
22 host_resolver()->AddRule("*", "127.0.0.1");
23 ASSERT_TRUE(embedded_test_server()->Start());
24 }
wjmacleanc62490492015-02-13 22:02:0725};
26
27void RunTestForURL(const GURL& url,
28 Shell* shell,
29 double host_zoom_level,
30 double temp_zoom_level) {
wjmacleanc62490492015-02-13 22:02:0731 WebContents* web_contents = shell->web_contents();
32
33 HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
34 HostZoomMap::GetForWebContents(web_contents));
35
davidsac996be112016-11-30 08:56:3836 int view_id = web_contents->GetRenderViewHost()->GetRoutingID();
Lukasz Anforowicza34824252017-10-02 20:31:3437 int process_id = web_contents->GetRenderViewHost()->GetProcess()->GetID();
wjmacleanc62490492015-02-13 22:02:0738
39 // Assume caller has set the zoom level to |host_zoom_level| using
40 // either a host or host+scheme entry in the HostZoomMap prior to
41 // calling this function.
42 EXPECT_DOUBLE_EQ(host_zoom_level, host_zoom_map->GetZoomLevelForView(
Lukasz Anforowicza34824252017-10-02 20:31:3443 url, process_id, view_id));
wjmacleanc62490492015-02-13 22:02:0744
45 // Make sure that GetZoomLevelForView() works for temporary zoom levels.
Lukasz Anforowicza34824252017-10-02 20:31:3446 host_zoom_map->SetTemporaryZoomLevel(process_id, view_id, temp_zoom_level);
wjmacleanc62490492015-02-13 22:02:0747 EXPECT_DOUBLE_EQ(temp_zoom_level, host_zoom_map->GetZoomLevelForView(
Lukasz Anforowicza34824252017-10-02 20:31:3448 url, process_id, view_id));
wjmacleanc62490492015-02-13 22:02:0749 // Clear the temporary zoom level in case subsequent test calls use the same
50 // web_contents.
Lukasz Anforowicza34824252017-10-02 20:31:3451 host_zoom_map->ClearTemporaryZoomLevel(process_id, view_id);
wjmacleanc62490492015-02-13 22:02:0752}
53
54// Test to make sure that GetZoomForView() works properly for zoom levels
55// stored by host value, and can distinguish temporary zoom levels from
56// these.
57IN_PROC_BROWSER_TEST_F(HostZoomMapImplBrowserTest, GetZoomForView_Host) {
Alexander Semashko043cf1d42017-09-10 22:43:0858 GURL url(embedded_test_server()->GetURL("abc.com", "/title1.html"));
wjmaclean64951902016-04-29 20:59:1259
60 // We must navigate so the WebContents has a committed entry.
61 EXPECT_TRUE(NavigateToURL(shell(), url));
wjmacleanc62490492015-02-13 22:02:0762
63 HostZoomMap* host_zoom_map =
64 HostZoomMap::GetForWebContents(shell()->web_contents());
65
66 double default_zoom_level = host_zoom_map->GetDefaultZoomLevel();
67 double host_zoom_level = default_zoom_level + 1.0;
68 double temp_zoom_level = default_zoom_level + 2.0;
69
70 host_zoom_map->SetZoomLevelForHost(url.host(), host_zoom_level);
71
72 RunTestForURL(url, shell(), host_zoom_level, temp_zoom_level);
73}
74
75// Test to make sure that GetZoomForView() works properly for zoom levels
76// stored by host and scheme values, and can distinguish temporary zoom levels
77// from these.
78IN_PROC_BROWSER_TEST_F(HostZoomMapImplBrowserTest,
79 GetZoomForView_HostAndScheme) {
Alexander Semashko043cf1d42017-09-10 22:43:0880 GURL url(embedded_test_server()->GetURL("abc.com", "/title1.html"));
wjmaclean64951902016-04-29 20:59:1281
82 // We must navigate so the WebContents has a committed entry.
83 EXPECT_TRUE(NavigateToURL(shell(), url));
wjmacleanc62490492015-02-13 22:02:0784
85 HostZoomMap* host_zoom_map =
86 HostZoomMap::GetForWebContents(shell()->web_contents());
87
88 double default_zoom_level = host_zoom_map->GetDefaultZoomLevel();
89 double host_zoom_level = default_zoom_level + 1.0;
90 double temp_zoom_level = default_zoom_level + 2.0;
91
92 host_zoom_map->SetZoomLevelForHostAndScheme(url.scheme(), url.host(),
93 host_zoom_level);
94
95 RunTestForURL(url, shell(), host_zoom_level, temp_zoom_level);
96}
97
98} // namespace content