wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 1 | // 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 Anforowicz | a3482425 | 2017-10-02 20:31:34 | [diff] [blame] | 7 | #include "content/public/browser/render_frame_host.h" |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 8 | #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" |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 12 | #include "content/public/test/content_browser_test_utils.h" |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 13 | #include "content/shell/browser/shell.h" |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 14 | #include "net/dns/mock_host_resolver.h" |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 15 | #include "url/gurl.h" |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | class HostZoomMapImplBrowserTest : public ContentBrowserTest { |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 20 | protected: |
| 21 | void SetUpOnMainThread() override { |
| 22 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 23 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 24 | } |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | void RunTestForURL(const GURL& url, |
| 28 | Shell* shell, |
| 29 | double host_zoom_level, |
| 30 | double temp_zoom_level) { |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 31 | WebContents* web_contents = shell->web_contents(); |
| 32 | |
| 33 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 34 | HostZoomMap::GetForWebContents(web_contents)); |
| 35 | |
davidsac | 996be11 | 2016-11-30 08:56:38 | [diff] [blame] | 36 | int view_id = web_contents->GetRenderViewHost()->GetRoutingID(); |
Lukasz Anforowicz | a3482425 | 2017-10-02 20:31:34 | [diff] [blame] | 37 | int process_id = web_contents->GetRenderViewHost()->GetProcess()->GetID(); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 38 | |
| 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 Anforowicz | a3482425 | 2017-10-02 20:31:34 | [diff] [blame] | 43 | url, process_id, view_id)); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 44 | |
| 45 | // Make sure that GetZoomLevelForView() works for temporary zoom levels. |
Lukasz Anforowicz | a3482425 | 2017-10-02 20:31:34 | [diff] [blame] | 46 | host_zoom_map->SetTemporaryZoomLevel(process_id, view_id, temp_zoom_level); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 47 | EXPECT_DOUBLE_EQ(temp_zoom_level, host_zoom_map->GetZoomLevelForView( |
Lukasz Anforowicz | a3482425 | 2017-10-02 20:31:34 | [diff] [blame] | 48 | url, process_id, view_id)); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 49 | // Clear the temporary zoom level in case subsequent test calls use the same |
| 50 | // web_contents. |
Lukasz Anforowicz | a3482425 | 2017-10-02 20:31:34 | [diff] [blame] | 51 | host_zoom_map->ClearTemporaryZoomLevel(process_id, view_id); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 52 | } |
| 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. |
| 57 | IN_PROC_BROWSER_TEST_F(HostZoomMapImplBrowserTest, GetZoomForView_Host) { |
Alexander Semashko | 043cf1d4 | 2017-09-10 22:43:08 | [diff] [blame] | 58 | GURL url(embedded_test_server()->GetURL("abc.com", "/title1.html")); |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 59 | |
| 60 | // We must navigate so the WebContents has a committed entry. |
| 61 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 62 | |
| 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. |
| 78 | IN_PROC_BROWSER_TEST_F(HostZoomMapImplBrowserTest, |
| 79 | GetZoomForView_HostAndScheme) { |
Alexander Semashko | 043cf1d4 | 2017-09-10 22:43:08 | [diff] [blame] | 80 | GURL url(embedded_test_server()->GetURL("abc.com", "/title1.html")); |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 81 | |
| 82 | // We must navigate so the WebContents has a committed entry. |
| 83 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 84 | |
| 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 |