Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: content/browser/zoom_browsertest.cc

Issue 2608213003: Add browsertest that zoom is preserved on reload (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/iframe_zoom_browsertest.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <vector> 5 #include <vector>
6 6
7 #include "base/strings/string_util.h" 7 #include "base/strings/string_util.h"
8 #include "content/browser/frame_host/frame_tree_node.h" 8 #include "content/browser/frame_host/frame_tree_node.h"
9 #include "content/browser/frame_host/render_frame_host_impl.h" 9 #include "content/browser/frame_host/render_frame_host_impl.h"
10 #include "content/browser/web_contents/web_contents_impl.h" 10 #include "content/browser/web_contents/web_contents_impl.h"
11 #include "content/public/browser/host_zoom_map.h" 11 #include "content/public/browser/host_zoom_map.h"
12 #include "content/public/browser/navigation_entry.h" 12 #include "content/public/browser/navigation_entry.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_types.h"
13 #include "content/public/common/page_zoom.h" 15 #include "content/public/common/page_zoom.h"
14 #include "content/public/test/browser_test_utils.h" 16 #include "content/public/test/browser_test_utils.h"
15 #include "content/public/test/content_browser_test.h" 17 #include "content/public/test/content_browser_test.h"
16 #include "content/public/test/content_browser_test_utils.h" 18 #include "content/public/test/content_browser_test_utils.h"
17 #include "content/public/test/test_navigation_observer.h" 19 #include "content/public/test/test_navigation_observer.h"
18 #include "content/shell/browser/shell.h" 20 #include "content/shell/browser/shell.h"
19 #include "content/test/content_browser_test_utils_internal.h" 21 #include "content/test/content_browser_test_utils_internal.h"
20 #include "net/dns/mock_host_resolver.h" 22 #include "net/dns/mock_host_resolver.h"
21 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
22 #include "url/gurl.h" 24 #include "url/gurl.h"
23 25
24 namespace content { 26 namespace content {
25 27
26 // This file contains tests to make sure that subframes zoom in a manner 28 // This class contains basic tests of zoom functionality.
29 class ZoomBrowserTest : public ContentBrowserTest {
30 public:
31 ZoomBrowserTest() {}
32
33 protected:
34 void SetUpOnMainThread() override {
35 host_resolver()->AddRule("*", "127.0.0.1");
36 SetupCrossSiteRedirector(embedded_test_server());
37 ASSERT_TRUE(embedded_test_server()->Start());
38 }
39
40 WebContentsImpl* web_contents() {
41 return static_cast<WebContentsImpl*>(shell()->web_contents());
42 }
43 };
44
45
46 // This class contains tests to make sure that subframes zoom in a manner
27 // consistent with the top-level frame, even when the subframes are cross-site. 47 // consistent with the top-level frame, even when the subframes are cross-site.
28 // Particular things we want to make sure of: 48 // Particular things we want to make sure of:
29 // 49 //
30 // * Subframes should always have the same zoom level as their main frame, even 50 // * Subframes should always have the same zoom level as their main frame, even
31 // if the subframe's domain has a different zoom level stored in HostZoomMap. 51 // if the subframe's domain has a different zoom level stored in HostZoomMap.
32 // 52 //
33 // * The condition above should continue to hold after a navigation of the 53 // * The condition above should continue to hold after a navigation of the
34 // subframe. 54 // subframe.
35 // 55 //
36 // * Zoom changes applied to the mainframe should propagate to all subframes, 56 // * Zoom changes applied to the mainframe should propagate to all subframes,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 151
132 FrameResizeObserver* toThis() {return this;} 152 FrameResizeObserver* toThis() {return this;}
133 153
134 RenderFrameHost* frame_host; 154 RenderFrameHost* frame_host;
135 std::string msg_label; 155 std::string msg_label;
136 bool zoomed_correctly; 156 bool zoomed_correctly;
137 double expected_inner_width; 157 double expected_inner_width;
138 double tolerance; 158 double tolerance;
139 }; 159 };
140 160
161 // This struct is used to wait until a resize has occurred.
162 struct ResizeObserver {
163 ResizeObserver(RenderFrameHost* host)
164 : frame_host(host) {
165 SetupOnResizeCallback(host);
166 }
167
168 void SetupOnResizeCallback(const ToRenderFrameHost& adapter) {
169 const char kOnResizeCallbackSetup[] =
170 "document.body.onresize = function(){"
171 " window.domAutomationController.setAutomationId(0);"
172 " window.domAutomationController.send('Resized');"
173 "};";
174 EXPECT_TRUE(ExecuteScript(
175 adapter, kOnResizeCallbackSetup));
176 }
177
178 bool IsResizeCallback(const std::string& status_msg) {
179 return status_msg == "Resized";
180 }
181
182 RenderFrameHost* frame_host;
183 };
184
185 void WaitForResize(DOMMessageQueue& msg_queue, ResizeObserver& observer) {
186 std::string status;
187 while (msg_queue.WaitForMessage(&status)) {
188 // Strip the double quotes from the message.
189 status = status.substr(1, status.length() -2);
190 if (observer.IsResizeCallback(status))
191 break;
192 }
193 }
194
141 void WaitAndCheckFrameZoom( 195 void WaitAndCheckFrameZoom(
142 DOMMessageQueue& msg_queue, 196 DOMMessageQueue& msg_queue,
143 std::vector<FrameResizeObserver>& frame_observers) { 197 std::vector<FrameResizeObserver>& frame_observers) {
144 std::string status; 198 std::string status;
145 while (msg_queue.WaitForMessage(&status)) { 199 while (msg_queue.WaitForMessage(&status)) {
146 // Strip the double quotes from the message. 200 // Strip the double quotes from the message.
147 status = status.substr(1, status.length() -2); 201 status = status.substr(1, status.length() -2);
148 202
149 bool all_zoomed_correctly = true; 203 bool all_zoomed_correctly = true;
150 204
151 // Use auto& to operate on a reference, and not a copy. 205 // Use auto& to operate on a reference, and not a copy.
152 for (auto& observer : frame_observers) { 206 for (auto& observer : frame_observers) {
153 observer.Check(status); 207 observer.Check(status);
154 all_zoomed_correctly = all_zoomed_correctly && observer.zoomed_correctly; 208 all_zoomed_correctly = all_zoomed_correctly && observer.zoomed_correctly;
155 } 209 }
156 210
157 if (all_zoomed_correctly) 211 if (all_zoomed_correctly)
158 break; 212 break;
159 } 213 }
160 } 214 }
161 215
162 } // namespace 216 } // namespace
163 217
218 IN_PROC_BROWSER_TEST_F(ZoomBrowserTest, ZoomPreservedOnReload) {
219 std::string top_level_host("a.com");
220
221 GURL main_url(embedded_test_server()->GetURL(
222 top_level_host, "/cross_site_iframe_factory.html?a(b(a))"));
223 EXPECT_TRUE(NavigateToURL(shell(), main_url));
224 NavigationEntry* entry =
225 web_contents()->GetController().GetLastCommittedEntry();
226 ASSERT_TRUE(entry);
227 GURL loaded_url = HostZoomMap::GetURLFromEntry(entry);
228 EXPECT_EQ(top_level_host, loaded_url.host());
229
230 FrameTreeNode* root =
231 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root();
232 double main_frame_window_border = GetMainframeWindowBorder(web_contents());
233
234 HostZoomMap* host_zoom_map = HostZoomMap::GetForWebContents(web_contents());
235 double default_zoom_level = host_zoom_map->GetDefaultZoomLevel();
236 EXPECT_EQ(0.0, default_zoom_level);
237
238 EXPECT_DOUBLE_EQ(
239 1.0, GetMainFrameZoomFactor(web_contents(), main_frame_window_border));
240
241 const double new_zoom_factor = 2.5;
242
243 // Set the new zoom, wait for the page to be resized, and sanity-check that
244 // the zoom was applied.
245 {
246 DOMMessageQueue msg_queue;
247 ResizeObserver observer(root->current_frame_host());
248
249 const double new_zoom_level =
250 default_zoom_level + ZoomFactorToZoomLevel(new_zoom_factor);
251 host_zoom_map->SetZoomLevelForHost(top_level_host, new_zoom_level);
252
253 WaitForResize(msg_queue, observer);
254 }
255
256 // Make this comparison approximate for Nexus5X test;
257 // https://crbug.com/622858.
258 EXPECT_NEAR(
259 new_zoom_factor,
260 GetMainFrameZoomFactor(web_contents(), main_frame_window_border),
261 0.01);
262
263 // Now the actual test: Reload the page and check that the main frame is
264 // still properly zoomed.
265 WindowedNotificationObserver load_stop_observer(
266 NOTIFICATION_LOAD_STOP,
267 NotificationService::AllSources());
268 shell()->Reload();
269 load_stop_observer.Wait();
270
271 EXPECT_NEAR(
wjmaclean 2017/01/03 16:30:30 Do we need to watch for any resize (or similar) ev
blundell 2017/01/03 16:36:57 That's an interesting question. Do you know if a r
272 new_zoom_factor,
273 GetMainFrameZoomFactor(web_contents(), main_frame_window_border),
274 0.01);
275 }
276
164 IN_PROC_BROWSER_TEST_F(IFrameZoomBrowserTest, SubframesZoomProperly) { 277 IN_PROC_BROWSER_TEST_F(IFrameZoomBrowserTest, SubframesZoomProperly) {
165 std::string top_level_host("a.com"); 278 std::string top_level_host("a.com");
166 GURL main_url(embedded_test_server()->GetURL( 279 GURL main_url(embedded_test_server()->GetURL(
167 top_level_host, "/cross_site_iframe_factory.html?a(b(a))")); 280 top_level_host, "/cross_site_iframe_factory.html?a(b(a))"));
168 EXPECT_TRUE(NavigateToURL(shell(), main_url)); 281 EXPECT_TRUE(NavigateToURL(shell(), main_url));
169 NavigationEntry* entry = 282 NavigationEntry* entry =
170 web_contents()->GetController().GetLastCommittedEntry(); 283 web_contents()->GetController().GetLastCommittedEntry();
171 ASSERT_TRUE(entry); 284 ASSERT_TRUE(entry);
172 GURL loaded_url = HostZoomMap::GetURLFromEntry(entry); 285 GURL loaded_url = HostZoomMap::GetURLFromEntry(entry);
173 EXPECT_EQ(top_level_host, loaded_url.host()); 286 EXPECT_EQ(top_level_host, loaded_url.host());
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 EXPECT_EQ(redirect_url, web_contents()->GetLastCommittedURL()); 585 EXPECT_EQ(redirect_url, web_contents()->GetLastCommittedURL());
473 586
474 EXPECT_NEAR( 587 EXPECT_NEAR(
475 kZoomFactorForRedirectedHost, 588 kZoomFactorForRedirectedHost,
476 GetMainFrameZoomFactor(web_contents(), main_frame_window_border), 589 GetMainFrameZoomFactor(web_contents(), main_frame_window_border),
477 0.001); 590 0.001);
478 } 591 }
479 #endif 592 #endif
480 593
481 } // namespace content 594 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/iframe_zoom_browsertest.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698