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