1 // Copyright 2015 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 "third_party/blink/renderer/core/html/html_image_element.h"
6 
7 #include <memory>
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/blink/renderer/core/css/css_property_value_set.h"
10 #include "third_party/blink/renderer/core/css/parser/css_parser.h"
11 #include "third_party/blink/renderer/core/dom/document.h"
12 #include "third_party/blink/renderer/core/frame/local_frame_view.h"
13 #include "third_party/blink/renderer/core/testing/page_test_base.h"
14 
15 namespace blink {
16 
17 class HTMLImageElementTest : public PageTestBase {
18  protected:
19   static constexpr int kViewportWidth = 500;
20   static constexpr int kViewportHeight = 600;
21 
SetUp()22   void SetUp() override {
23     PageTestBase::SetUp(IntSize(kViewportWidth, kViewportHeight));
24   }
25 };
26 
27 // Instantiate class constants. Not needed after C++17.
28 constexpr int HTMLImageElementTest::kViewportWidth;
29 constexpr int HTMLImageElementTest::kViewportHeight;
30 
TEST_F(HTMLImageElementTest,width)31 TEST_F(HTMLImageElementTest, width) {
32   auto* image = MakeGarbageCollected<HTMLImageElement>(GetDocument());
33   image->setAttribute(html_names::kWidthAttr, "400");
34   // TODO(yoav): `width` does not impact resourceWidth until we resolve
35   // https://github.com/ResponsiveImagesCG/picture-element/issues/268
36   EXPECT_EQ(500, image->GetResourceWidth().width);
37   image->setAttribute(html_names::kSizesAttr, "100vw");
38   EXPECT_EQ(500, image->GetResourceWidth().width);
39 }
40 
TEST_F(HTMLImageElementTest,sourceSize)41 TEST_F(HTMLImageElementTest, sourceSize) {
42   auto* image = MakeGarbageCollected<HTMLImageElement>(GetDocument());
43   image->setAttribute(html_names::kWidthAttr, "400");
44   EXPECT_EQ(kViewportWidth, image->SourceSize(*image));
45   image->setAttribute(html_names::kSizesAttr, "50vw");
46   EXPECT_EQ(250, image->SourceSize(*image));
47 }
48 
TEST_F(HTMLImageElementTest,attributeLazyLoadDimensionType)49 TEST_F(HTMLImageElementTest, attributeLazyLoadDimensionType) {
50   struct TestCase {
51     const char* attribute_value;
52     HTMLImageElement::LazyLoadDimensionType expected_dimension_type;
53   };
54   const TestCase test_cases[] = {
55       {"", HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
56       {"invalid", HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
57       {"10px", HTMLImageElement::LazyLoadDimensionType::kAbsoluteSmall},
58       {"10", HTMLImageElement::LazyLoadDimensionType::kAbsoluteSmall},
59       {"100px", HTMLImageElement::LazyLoadDimensionType::kAbsoluteNotSmall},
60       {"100", HTMLImageElement::LazyLoadDimensionType::kAbsoluteNotSmall},
61   };
62   for (const auto& test : test_cases) {
63     EXPECT_EQ(test.expected_dimension_type,
64               HTMLImageElement::GetAttributeLazyLoadDimensionType(
65                   test.attribute_value));
66   }
67 }
68 
TEST_F(HTMLImageElementTest,inlineStyleLazyLoadDimensionType)69 TEST_F(HTMLImageElementTest, inlineStyleLazyLoadDimensionType) {
70   struct TestCase {
71     const char* inline_style;
72     HTMLImageElement::LazyLoadDimensionType expected_dimension_type;
73   };
74   const TestCase test_cases[] = {
75       {"", HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
76       {"invalid", HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
77       {"height: 1px", HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
78       {"width: 1px", HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
79       {"height: 1; width: 1",
80        HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
81       {"height: 50%; width: 50%",
82        HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
83       {"height: 1px; width: 1px",
84        HTMLImageElement::LazyLoadDimensionType::kAbsoluteSmall},
85       {"height: 10px; width: 10px",
86        HTMLImageElement::LazyLoadDimensionType::kAbsoluteSmall},
87       {"height: 100px; width: 10px",
88        HTMLImageElement::LazyLoadDimensionType::kAbsoluteNotSmall},
89       {"height: 10px; width: 100px",
90        HTMLImageElement::LazyLoadDimensionType::kAbsoluteNotSmall},
91       {"height: 100px; width: 100px",
92        HTMLImageElement::LazyLoadDimensionType::kAbsoluteNotSmall},
93       {"height: 100; width: 100",
94        HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
95       {"height: 100%; width: 100%",
96        HTMLImageElement::LazyLoadDimensionType::kNotAbsolute},
97   };
98   for (const auto& test : test_cases) {
99     const ImmutableCSSPropertyValueSet* property_set =
100         CSSParser::ParseInlineStyleDeclaration(
101             test.inline_style, kHTMLStandardMode,
102             SecureContextMode::kInsecureContext);
103     EXPECT_EQ(test.expected_dimension_type,
104               HTMLImageElement::GetInlineStyleDimensionsType(property_set));
105   }
106 }
107 
108 }  // namespace blink
109