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

Side by Side Diff: third_party/WebKit/Source/core/html/ImageDocumentTest.cpp

Issue 2319863006: Change image document zooming logic. (Closed)
Patch Set: enforce Desktop mode Created 4 years, 3 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "core/html/ImageDocument.h"
6
7 #include "core/dom/Document.h"
8 #include "core/loader/EmptyClients.h"
9 #include "core/testing/DummyPageHolder.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace blink {
13
14 namespace {
15
16 // An image of size 50x50.
17 Vector<unsigned char> jpegImage()
18 {
19 Vector<unsigned char> jpeg;
20
21 static const unsigned char data[] = {
22 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x01, 0x00, 0x48,
23 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
24 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
25 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
26 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
27 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00, 0x43, 0x01, 0xff, 0xff,
28 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
29 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
30 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
31 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
32 0x00, 0x11, 0x08, 0x00, 0x32, 0x00, 0x32, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11,
33 0x01, 0xff, 0xc4, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x10, 0x01, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00,
36 0x15, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01,
39 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x03, 0xff, 0xd9
42 };
43
44 jpeg.append(data, sizeof(data));
45 return jpeg;
46 }
47
48 }
49
50 class WindowToViewportScalingChromeClient : public EmptyChromeClient {
51 public:
52 WindowToViewportScalingChromeClient()
53 : EmptyChromeClient()
54 , m_scaleFactor(1.f)
55 {
56 }
57
58 void setScalingFactor(float s) { m_scaleFactor = s; }
59 float windowToViewportScalar(const float s) const override { return m_scaleF actor; }
60
61 private:
62 float m_scaleFactor;
63 };
64
65 class ImageDocumentTest : public ::testing::Test {
66 protected:
67 void TearDown() override
68 {
69 ThreadState::current()->collectAllGarbage();
70 }
71
72 void createDocumentWithoutLoadingImage(int viewWidth, int viewHeight);
73 void createDocument(int viewWidth, int viewHeight);
74 void loadImage();
75
76 ImageDocument& document() const;
77
78 int imageWidth() const { return document().m_imageElement->width(); }
79 int imageHeight() const { return document().m_imageElement->height(); }
80
81 void setPageZoom(float);
82 void setWindowToViewportScalingFactor(float);
83
84 private:
85 WindowToViewportScalingChromeClient* m_chromeClient;
86 std::unique_ptr<DummyPageHolder> m_dummyPageHolder;
87 };
88
89 void ImageDocumentTest::createDocumentWithoutLoadingImage(int viewWidth, int vie wHeight)
90 {
91 Page::PageClients pageClients;
92 fillWithEmptyClients(pageClients);
93 m_chromeClient = new WindowToViewportScalingChromeClient();
94 pageClients.chromeClient = m_chromeClient;
95 m_dummyPageHolder = DummyPageHolder::create(IntSize(viewWidth, viewHeight), &pageClients);
96
97 LocalFrame& frame = m_dummyPageHolder->frame();
98 frame.document()->shutdown();
99 DocumentInit init(KURL(), &frame);
100 frame.localDOMWindow()->installNewDocument("image/jpeg", init);
101 }
102
103 void ImageDocumentTest::createDocument(int viewWidth, int viewHeight)
104 {
105 createDocumentWithoutLoadingImage(viewWidth, viewHeight);
106 loadImage();
107 }
108
109 ImageDocument& ImageDocumentTest::document() const
110 {
111 Document* document = m_dummyPageHolder->frame().domWindow()->document();
112 ImageDocument* imageDocument = static_cast<ImageDocument*>(document);
113 return *imageDocument;
114 }
115
116 void ImageDocumentTest::loadImage()
117 {
118 HTMLImageElement* imageElement = HTMLImageElement::create(document());
119
120 ImageResource* imageResource = ImageResource::create(ResourceRequest(KURL()) );
121 const Vector<unsigned char>& data = jpegImage();
122 imageResource->appendData(reinterpret_cast<const char*>(data.data()), data.s ize());
123 imageResource->finish();
124
125 imageElement->setImageResource(imageResource);
126 document().m_imageElement = imageElement;
127 document().imageUpdated();
128 }
129
130 void ImageDocumentTest::setPageZoom(float factor)
131 {
132 m_dummyPageHolder->frame().setPageZoomFactor(factor);
133 }
134
135 void ImageDocumentTest::setWindowToViewportScalingFactor(float factor)
136 {
137 m_chromeClient->setScalingFactor(factor);
138 }
139
140 TEST_F(ImageDocumentTest, ImageLoad)
141 {
142 createDocument(50, 50);
143 EXPECT_EQ(50, imageWidth());
144 EXPECT_EQ(50, imageHeight());
145 }
146
147 TEST_F(ImageDocumentTest, LargeImageScalesDown)
148 {
149 createDocument(25, 30);
150 EXPECT_EQ(25, imageWidth());
151 EXPECT_EQ(25, imageHeight());
152
153 createDocument(35, 20);
154 EXPECT_EQ(20, imageWidth());
155 EXPECT_EQ(20, imageHeight());
156 }
157
158 TEST_F(ImageDocumentTest, RestoreImageOnClick)
159 {
160 createDocument(30, 40);
161 document().imageClicked(4, 4);
162 EXPECT_EQ(50, imageWidth());
163 EXPECT_EQ(50, imageHeight());
164 }
165
166 TEST_F(ImageDocumentTest, InitialZoom)
167 {
168 createDocumentWithoutLoadingImage(20, 10);
169 setPageZoom(2.f);
170 loadImage();
171 EXPECT_EQ(10, imageWidth());
172 EXPECT_EQ(10, imageHeight());
173 document().imageClicked(4, 4);
174 EXPECT_EQ(50, imageWidth());
175 EXPECT_EQ(50, imageHeight());
176 }
177
178 TEST_F(ImageDocumentTest, ZoomingDoesNotChangeRelativeSize)
179 {
180 createDocument(75, 75);
181 setPageZoom(0.5f);
182 document().windowSizeChanged();
183 EXPECT_EQ(50, imageWidth());
184 EXPECT_EQ(50, imageHeight());
185 setPageZoom(2.f);
186 document().windowSizeChanged();
187 EXPECT_EQ(50, imageWidth());
188 EXPECT_EQ(50, imageHeight());
189 }
190
191 TEST_F(ImageDocumentTest, ImageScalesDownWithDsf)
192 {
193 createDocumentWithoutLoadingImage(20, 30);
194 setWindowToViewportScalingFactor(2.f);
195 loadImage();
196 EXPECT_EQ(10, imageWidth());
197 EXPECT_EQ(10, imageHeight());
198 }
199
200 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698