File: ImageDocumentTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (208 lines) | stat: -rw-r--r-- 7,476 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/html/ImageDocument.h"

#include "core/dom/Document.h"
#include "core/dom/DocumentParser.h"
#include "core/frame/Settings.h"
#include "core/loader/EmptyClients.h"
#include "core/testing/DummyPageHolder.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace blink {

namespace {

// An image of size 50x50.
Vector<unsigned char> jpegImage() {
  Vector<unsigned char> jpeg;

  static const unsigned char data[] = {
      0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
      0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
      0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00, 0x43, 0x01, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 0x32, 0x00, 0x32, 0x03,
      0x01, 0x22, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xc4, 0x00,
      0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x14, 0x10,
      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc4, 0x00, 0x15, 0x01, 0x01, 0x01,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x00, 0x14, 0x11, 0x01, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
      0x11, 0x00, 0x3f, 0x00, 0x00, 0x94, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xd9};

  jpeg.append(data, sizeof(data));
  return jpeg;
}
}

class WindowToViewportScalingChromeClient : public EmptyChromeClient {
 public:
  WindowToViewportScalingChromeClient()
      : EmptyChromeClient(), m_scaleFactor(1.f) {}

  void setScalingFactor(float s) { m_scaleFactor = s; }
  float windowToViewportScalar(const float s) const override {
    return s * m_scaleFactor;
  }

 private:
  float m_scaleFactor;
};

class ImageDocumentTest : public ::testing::Test {
 protected:
  void TearDown() override { ThreadState::current()->collectAllGarbage(); }

  void createDocumentWithoutLoadingImage(int viewWidth, int viewHeight);
  void createDocument(int viewWidth, int viewHeight);
  void loadImage();

  ImageDocument& document() const;

  int imageWidth() const { return document().imageElement()->width(); }
  int imageHeight() const { return document().imageElement()->height(); }

  void setPageZoom(float);
  void setWindowToViewportScalingFactor(float);

 private:
  Persistent<WindowToViewportScalingChromeClient> m_chromeClient;
  std::unique_ptr<DummyPageHolder> m_dummyPageHolder;
};

void ImageDocumentTest::createDocumentWithoutLoadingImage(int viewWidth,
                                                          int viewHeight) {
  Page::PageClients pageClients;
  fillWithEmptyClients(pageClients);
  m_chromeClient = new WindowToViewportScalingChromeClient();
  pageClients.chromeClient = m_chromeClient;
  m_dummyPageHolder =
      DummyPageHolder::create(IntSize(viewWidth, viewHeight), &pageClients);

  LocalFrame& frame = m_dummyPageHolder->frame();
  frame.document()->shutdown();
  DocumentInit init(KURL(), &frame);
  frame.domWindow()->installNewDocument("image/jpeg", init);
}

void ImageDocumentTest::createDocument(int viewWidth, int viewHeight) {
  createDocumentWithoutLoadingImage(viewWidth, viewHeight);
  loadImage();
}

ImageDocument& ImageDocumentTest::document() const {
  Document* document = m_dummyPageHolder->frame().domWindow()->document();
  ImageDocument* imageDocument = static_cast<ImageDocument*>(document);
  return *imageDocument;
}

void ImageDocumentTest::loadImage() {
  DocumentParser* parser = document().implicitOpen(
      ParserSynchronizationPolicy::ForceSynchronousParsing);
  const Vector<unsigned char>& data = jpegImage();
  parser->appendBytes(reinterpret_cast<const char*>(data.data()), data.size());
  parser->finish();
}

void ImageDocumentTest::setPageZoom(float factor) {
  m_dummyPageHolder->frame().setPageZoomFactor(factor);
}

void ImageDocumentTest::setWindowToViewportScalingFactor(float factor) {
  m_chromeClient->setScalingFactor(factor);
}

TEST_F(ImageDocumentTest, ImageLoad) {
  createDocument(50, 50);
  EXPECT_EQ(50, imageWidth());
  EXPECT_EQ(50, imageHeight());
}

TEST_F(ImageDocumentTest, LargeImageScalesDown) {
  createDocument(25, 30);
  EXPECT_EQ(25, imageWidth());
  EXPECT_EQ(25, imageHeight());

  createDocument(35, 20);
  EXPECT_EQ(20, imageWidth());
  EXPECT_EQ(20, imageHeight());
}

TEST_F(ImageDocumentTest, RestoreImageOnClick) {
  createDocument(30, 40);
  document().imageClicked(4, 4);
  EXPECT_EQ(50, imageWidth());
  EXPECT_EQ(50, imageHeight());
}

TEST_F(ImageDocumentTest, InitialZoomDoesNotAffectScreenFit) {
  createDocumentWithoutLoadingImage(20, 10);
  setPageZoom(2.f);
  loadImage();
  EXPECT_EQ(10, imageWidth());
  EXPECT_EQ(10, imageHeight());
  document().imageClicked(4, 4);
  EXPECT_EQ(50, imageWidth());
  EXPECT_EQ(50, imageHeight());
}

TEST_F(ImageDocumentTest, ZoomingDoesNotChangeRelativeSize) {
  createDocument(75, 75);
  setPageZoom(0.5f);
  document().windowSizeChanged();
  EXPECT_EQ(50, imageWidth());
  EXPECT_EQ(50, imageHeight());
  setPageZoom(2.f);
  document().windowSizeChanged();
  EXPECT_EQ(50, imageWidth());
  EXPECT_EQ(50, imageHeight());
}

TEST_F(ImageDocumentTest, ImageScalesDownWithDsf) {
  createDocumentWithoutLoadingImage(20, 30);
  setWindowToViewportScalingFactor(2.f);
  loadImage();
  EXPECT_EQ(10, imageWidth());
  EXPECT_EQ(10, imageHeight());
}

TEST_F(ImageDocumentTest, ImageNotCenteredWithForceZeroLayoutHeight) {
  createDocumentWithoutLoadingImage(80, 70);
  document().page()->settings().setForceZeroLayoutHeight(true);
  loadImage();
  EXPECT_FALSE(document().shouldShrinkToFit());
  EXPECT_EQ(0, document().imageElement()->offsetLeft());
  EXPECT_EQ(0, document().imageElement()->offsetTop());
  EXPECT_EQ(50, imageWidth());
  EXPECT_EQ(50, imageHeight());
}

TEST_F(ImageDocumentTest, ImageCenteredWithoutForceZeroLayoutHeight) {
  createDocumentWithoutLoadingImage(80, 70);
  document().page()->settings().setForceZeroLayoutHeight(false);
  loadImage();
  EXPECT_TRUE(document().shouldShrinkToFit());
  EXPECT_EQ(15, document().imageElement()->offsetLeft());
  EXPECT_EQ(10, document().imageElement()->offsetTop());
  EXPECT_EQ(50, imageWidth());
  EXPECT_EQ(50, imageHeight());
}

}  // namespace blink