Commit 62e790d7 authored by Ella Ge's avatar Ella Ge Committed by Commit Bot

Image document size apply page zoom on android

Apply PageZoomFactor to image div width for use-zoom-for-dsf
When use-zoom-for-dsf enabled, the visual viewport doesn't have
the device scale factor (DSF) applied, so we need to apply
PageZoomFactor when decide image container size.

This change doesn't need to be under the flag, because when the flag
is disabled, PageZoomFactor is 1.

Bug: 844461
Change-Id: I1e23cafdef387b97f2c446438668a500225c0deb
Reviewed-on: https://chromium-review.googlesource.com/1065145
Commit-Queue: Ella Ge <eirage@chromium.org>
Reviewed-by: default avatarDavid Bokan <bokan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#563144}
parent 5b13731d
...@@ -454,7 +454,8 @@ int ImageDocument::CalculateDivWidth() { ...@@ -454,7 +454,8 @@ int ImageDocument::CalculateDivWidth() {
// of the frame. // of the frame.
// * Images smaller in either dimension are centered along that axis. // * Images smaller in either dimension are centered along that axis.
int viewport_width = int viewport_width =
GetFrame()->GetPage()->GetVisualViewport().Size().Width(); GetFrame()->GetPage()->GetVisualViewport().Size().Width() /
GetFrame()->PageZoomFactor();
// For huge images, minimum-scale=0.1 is still too big on small screens. // For huge images, minimum-scale=0.1 is still too big on small screens.
// Set the <div> width so that the image will shrink to fit the width of the // Set the <div> width so that the image will shrink to fit the width of the
......
...@@ -96,6 +96,9 @@ class CORE_EXPORT ImageDocument final : public HTMLDocument { ...@@ -96,6 +96,9 @@ class CORE_EXPORT ImageDocument final : public HTMLDocument {
enum ShrinkToFitMode { kViewport, kDesktop }; enum ShrinkToFitMode { kViewport, kDesktop };
ShrinkToFitMode shrink_to_fit_mode_; ShrinkToFitMode shrink_to_fit_mode_;
FRIEND_TEST_ALL_PREFIXES(ImageDocumentViewportTest, ZoomForDSFScaleImage);
FRIEND_TEST_ALL_PREFIXES(ImageDocumentViewportTest, DivWidthWithZoomForDSF);
}; };
DEFINE_DOCUMENT_TYPE_CASTS(ImageDocument); DEFINE_DOCUMENT_TYPE_CASTS(ImageDocument);
......
...@@ -310,5 +310,99 @@ TEST_F(ImageDocumentViewportTest, HidingURLBarDoesntChangeImageLocation) { ...@@ -310,5 +310,99 @@ TEST_F(ImageDocumentViewportTest, HidingURLBarDoesntChangeImageLocation) {
EXPECT_EQ(175, rect->y()); EXPECT_EQ(175, rect->y());
} }
TEST_F(ImageDocumentViewportTest, ZoomForDSFScaleImage) {
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
SimRequest request("https://example.com/test.jpg", "image/jpeg");
LoadURL("https://example.com/test.jpg");
Vector<unsigned char> jpeg = JpegImage();
Vector<char> data = Vector<char>();
data.Append(jpeg.data(), jpeg.size());
request.Complete(data);
HTMLImageElement* img = GetDocument().ImageElement();
// no zoom
WebView().Resize(IntSize(100, 100));
WebView().SetZoomFactorForDeviceScaleFactor(1.f);
Compositor().BeginFrame();
EXPECT_EQ(50u, img->width());
EXPECT_EQ(50u, img->height());
EXPECT_EQ(100, GetDocument().CalculateDivWidth());
EXPECT_EQ(1.f, GetVisualViewport().Scale());
EXPECT_EQ(100, GetVisualViewport().Width());
EXPECT_EQ(100, GetVisualViewport().Height());
// zoom-for-dsf = 4. WebView size is in physical pixel(400*400), image and
// visual viewport should be same in CSS pixel, as no dsf applied.
// This simulates running on two phones with different screen densities but
// same (physical) screen size, image document should displayed the same.
WebView().Resize(IntSize(400, 400));
WebView().SetZoomFactorForDeviceScaleFactor(4.f);
Compositor().BeginFrame();
EXPECT_EQ(50u, img->width());
EXPECT_EQ(50u, img->height());
EXPECT_EQ(100, GetDocument().CalculateDivWidth());
EXPECT_EQ(1.f, GetVisualViewport().Scale());
EXPECT_EQ(100, GetVisualViewport().Width());
EXPECT_EQ(100, GetVisualViewport().Height());
}
// Tests that with zoom factor for device scale factor, image with different
// size fit in the viewport correctly.
TEST_F(ImageDocumentViewportTest, DivWidthWithZoomForDSF) {
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
SimRequest request("https://example.com/test.jpg", "image/jpeg");
LoadURL("https://example.com/test.jpg");
Vector<unsigned char> jpeg = JpegImage();
Vector<char> data = Vector<char>();
data.Append(jpeg.data(), jpeg.size());
request.Complete(data);
HTMLImageElement* img = GetDocument().ImageElement();
WebView().SetZoomFactorForDeviceScaleFactor(2.f);
// Image smaller then webview size, visual viewport is not zoomed, and image
// will be centered in the viewport.
WebView().Resize(IntSize(200, 200));
Compositor().BeginFrame();
EXPECT_EQ(50u, img->width());
EXPECT_EQ(50u, img->height());
EXPECT_EQ(100, GetDocument().CalculateDivWidth());
EXPECT_EQ(1.f, GetVisualViewport().Scale());
EXPECT_EQ(100, GetVisualViewport().Width());
EXPECT_EQ(100, GetVisualViewport().Height());
DOMRect* rect = img->getBoundingClientRect();
EXPECT_EQ(25, rect->x());
EXPECT_EQ(25, rect->y());
// Image wider than webview size, image should fill the visual viewport, and
// visual viewport zoom out to 0.5.
WebView().Resize(IntSize(50, 50));
Compositor().BeginFrame();
EXPECT_EQ(50u, img->width());
EXPECT_EQ(50u, img->height());
EXPECT_EQ(50, GetDocument().CalculateDivWidth());
EXPECT_EQ(0.5f, GetVisualViewport().Scale());
EXPECT_EQ(50, GetVisualViewport().Width());
EXPECT_EQ(50, GetVisualViewport().Height());
// When image is more than 10X wider than webview, shrink the image to fit the
// width of the screen.
WebView().Resize(IntSize(4, 20));
Compositor().BeginFrame();
EXPECT_EQ(20u, img->width());
EXPECT_EQ(20u, img->height());
EXPECT_EQ(20, GetDocument().CalculateDivWidth());
EXPECT_EQ(0.1f, GetVisualViewport().Scale());
EXPECT_EQ(20, GetVisualViewport().Width());
EXPECT_EQ(100, GetVisualViewport().Height());
rect = img->getBoundingClientRect();
EXPECT_EQ(0, rect->x());
EXPECT_EQ(40, rect->y());
}
#undef MAYBE #undef MAYBE
} // namespace blink } // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment