summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/paint/image_element_timing_test.cc
blob: bebc1d83a24b5737b225bdc89c4e61d1ea1b57d9 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2019 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 "third_party/blink/renderer/core/paint/image_element_timing.h"

#include "third_party/blink/renderer/core/frame/frame_test_helpers.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/layout/layout_image.h"
#include "third_party/blink/renderer/core/layout/svg/layout_svg_image.h"
#include "third_party/blink/renderer/core/loader/resource/image_resource_content.h"
#include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSurface.h"

namespace blink {

namespace internal {

extern bool IsExplicitlyRegisteredForTiming(const LayoutObject* layout_object);

}

class ImageElementTimingTest : public testing::Test {
 protected:
  void SetUp() override {
    web_view_helper_.Initialize();
    frame_test_helpers::LoadFrame(
        web_view_helper_.GetWebView()->MainFrameImpl(), "about:blank");
    WebURL base_url_ = url_test_helpers::ToKURL("http://www.test.com/");
    // Enable compositing on the page.
    web_view_helper_.GetWebView()
        ->GetPage()
        ->GetSettings()
        .SetAcceleratedCompositingEnabled(true);
    GetDoc()->View()->SetParentVisible(true);
    GetDoc()->View()->SetSelfVisible(true);
  }

  // Sets an image resource for the LayoutImage with the given |id| and return
  // the LayoutImage.
  LayoutImage* SetImageResource(const char* id, int width, int height) {
    ImageResourceContent* content = CreateImageForTest(width, height);
    auto* layout_image = ToLayoutImage(GetLayoutObjectById(id));
    layout_image->ImageResource()->SetImageResource(content);
    return layout_image;
  }

  // Similar to above but for a LayoutSVGImage.
  LayoutSVGImage* SetSVGImageResource(const char* id, int width, int height) {
    ImageResourceContent* content = CreateImageForTest(width, height);
    auto* layout_image = ToLayoutSVGImage(GetLayoutObjectById(id));
    layout_image->ImageResource()->SetImageResource(content);
    return layout_image;
  }

  bool ImagesNotifiedContains(
      const std::pair<const LayoutObject*, const ImageResourceContent*>&
          record_id) {
    return ImageElementTiming::From(*GetDoc()->domWindow())
        .images_notified_.Contains(record_id);
  }

  unsigned ImagesNotifiedSize() {
    return ImageElementTiming::From(*GetDoc()->domWindow())
        .images_notified_.size();
  }

  Document* GetDoc() {
    return web_view_helper_.GetWebView()
        ->MainFrameImpl()
        ->GetFrame()
        ->GetDocument();
  }

  LayoutObject* GetLayoutObjectById(const char* id) {
    return GetDoc()->getElementById(id)->GetLayoutObject();
  }

  void UpdateAllLifecyclePhases() {
    web_view_helper_.GetWebView()
        ->MainFrameImpl()
        ->GetFrame()
        ->View()
        ->UpdateAllLifecyclePhases(
            DocumentLifecycle::LifecycleUpdateReason::kTest);
  }

  frame_test_helpers::WebViewHelper web_view_helper_;
  WebURL base_url_;

 private:
  ImageResourceContent* CreateImageForTest(int width, int height) {
    sk_sp<SkColorSpace> src_rgb_color_space = SkColorSpace::MakeSRGB();
    SkImageInfo raster_image_info =
        SkImageInfo::MakeN32Premul(width, height, src_rgb_color_space);
    sk_sp<SkSurface> surface(SkSurface::MakeRaster(raster_image_info));
    sk_sp<SkImage> image = surface->makeImageSnapshot();
    ImageResourceContent* original_image_resource =
        ImageResourceContent::CreateLoaded(
            StaticBitmapImage::Create(image).get());
    return original_image_resource;
  }
};

TEST_F(ImageElementTimingTest, TestIsExplicitlyRegisteredForTiming) {
  frame_test_helpers::LoadHTMLString(
      web_view_helper_.GetWebView()->MainFrameImpl(), R"HTML(
    <img id="missing-attribute" style='width: 100px; height: 100px;'/>
    <img id="unset-attribute" elementtiming style='width: 100px; height: 100px;'/>
    <img id="empty-attribute" elementtiming="" style='width: 100px; height: 100px;'/>
    <img id="valid-attribute" elementtiming="valid-id" style='width: 100px; height: 100px;'/>
  )HTML",
      base_url_);

  LayoutObject* without_attribute = GetLayoutObjectById("missing-attribute");
  bool actual = internal::IsExplicitlyRegisteredForTiming(without_attribute);
  EXPECT_FALSE(actual) << "Nodes without an 'elementtiming' attribute should "
                          "not be explicitly registered.";

  LayoutObject* with_undefined_attribute =
      GetLayoutObjectById("unset-attribute");
  actual = internal::IsExplicitlyRegisteredForTiming(with_undefined_attribute);
  EXPECT_FALSE(actual) << "Nodes with undefined 'elementtiming' attribute "
                          "should not be explicitly registered.";

  LayoutObject* with_empty_attribute = GetLayoutObjectById("empty-attribute");
  actual = internal::IsExplicitlyRegisteredForTiming(with_empty_attribute);
  EXPECT_FALSE(actual) << "Nodes with an empty 'elementtiming' attribute "
                          "should not be explicitly registered.";

  LayoutObject* with_explicit_element_timing =
      GetLayoutObjectById("valid-attribute");
  actual =
      internal::IsExplicitlyRegisteredForTiming(with_explicit_element_timing);
  EXPECT_TRUE(actual) << "Nodes with a non-empty 'elementtiming' attribute "
                         "should be explicitly registered.";
}

TEST_F(ImageElementTimingTest, IgnoresUnmarkedElement) {
  // Tests that, if the 'elementtiming' attribute is missing, the element isn't
  // considered by ImageElementTiming.
  frame_test_helpers::LoadHTMLString(
      web_view_helper_.GetWebView()->MainFrameImpl(), R"HTML(
    <img id="target" style='width: 100px; height: 100px;'/>
  )HTML",
      base_url_);
  LayoutImage* layout_image = SetImageResource("target", 5, 5);
  ASSERT_TRUE(layout_image);
  UpdateAllLifecyclePhases();
  EXPECT_FALSE(ImagesNotifiedContains(
      std::make_pair(layout_image, layout_image->CachedImage())));
}

TEST_F(ImageElementTimingTest, ImageInsideSVG) {
  frame_test_helpers::LoadHTMLString(
      web_view_helper_.GetWebView()->MainFrameImpl(), R"HTML(
    <svg mask="url(#mask)">
      <mask id="mask">
        <foreignObject width="100" height="100">
          <img elementtiming="image-inside-svg" id="target" style='width: 100px; height: 100px;'/>
        </foreignObject>
      </mask>
      <rect width="100" height="100" fill="green"/>
    </svg>
  )HTML",
      base_url_);
  LayoutImage* layout_image = SetImageResource("target", 5, 5);
  ASSERT_TRUE(layout_image);
  UpdateAllLifecyclePhases();

  // |layout_image| should have had its paint notified to ImageElementTiming.
  EXPECT_TRUE(ImagesNotifiedContains(
      std::make_pair(layout_image, layout_image->CachedImage())));
}

TEST_F(ImageElementTimingTest, ImageRemoved) {
  frame_test_helpers::LoadHTMLString(
      web_view_helper_.GetWebView()->MainFrameImpl(), R"HTML(
    <img elementtiming="will-be-removed" id="target" style='width: 100px; height: 100px;'/>
  )HTML",
      base_url_);
  LayoutImage* layout_image = SetImageResource("target", 5, 5);
  ASSERT_TRUE(layout_image);
  UpdateAllLifecyclePhases();
  EXPECT_TRUE(ImagesNotifiedContains(
      std::make_pair(layout_image, layout_image->CachedImage())));

  GetDoc()->getElementById("target")->remove();
  // |layout_image| should no longer be part of |images_notified| since it will
  // be destroyed.
  EXPECT_EQ(ImagesNotifiedSize(), 0u);
}

TEST_F(ImageElementTimingTest, SVGImageRemoved) {
  frame_test_helpers::LoadHTMLString(
      web_view_helper_.GetWebView()->MainFrameImpl(), R"HTML(
    <svg>
      <image elementtiming="svg-will-be-removed" id="target" style='width: 100px; height: 100px;'/>
    </svg>
  )HTML",
      base_url_);
  LayoutSVGImage* layout_image = SetSVGImageResource("target", 5, 5);
  ASSERT_TRUE(layout_image);
  UpdateAllLifecyclePhases();
  EXPECT_TRUE(ImagesNotifiedContains(std::make_pair(
      layout_image, layout_image->ImageResource()->CachedImage())));

  GetDoc()->getElementById("target")->remove();
  // |layout_image| should no longer be part of |images_notified| since it will
  // be destroyed.
  EXPECT_EQ(ImagesNotifiedSize(), 0u);
}

TEST_F(ImageElementTimingTest, BackgroundImageRemoved) {
  frame_test_helpers::LoadHTMLString(
      web_view_helper_.GetWebView()->MainFrameImpl(), R"HTML(
    <style>
      #target {
        width: 100px;
        height: 100px;
        background: url(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==);
      }
    </style>
    <div elementtiming="time-my-background-image" id="target"></div>
  )HTML",
      base_url_);
  LayoutObject* object = GetLayoutObjectById("target");
  ImageResourceContent* content =
      object->Style()->BackgroundLayers().GetImage()->CachedImage();
  UpdateAllLifecyclePhases();
  EXPECT_EQ(ImagesNotifiedSize(), 1u);
  EXPECT_TRUE(ImagesNotifiedContains(std::make_pair(object, content)));

  GetDoc()->getElementById("target")->remove();
  EXPECT_EQ(ImagesNotifiedSize(), 0u);
}

}  // namespace blink