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 "third_party/blink/renderer/core/css/cssom/css_style_image_value.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/blink/renderer/platform/graphics/image.h"
9
10 namespace blink {
11
12 namespace {
13
14 class FakeCSSStyleImageValue : public CSSStyleImageValue {
15 public:
FakeCSSStyleImageValue(bool cache_pending,IntSize size)16 FakeCSSStyleImageValue(bool cache_pending, IntSize size)
17 : cache_pending_(cache_pending), size_(size) {}
18
19 // CSSStyleImageValue
IntrinsicSize() const20 base::Optional<IntSize> IntrinsicSize() const final {
21 if (cache_pending_)
22 return base::nullopt;
23 return size_;
24 }
25
26 // CanvasImageSource
GetSourceImageForCanvas(SourceImageStatus *,const FloatSize &)27 scoped_refptr<Image> GetSourceImageForCanvas(SourceImageStatus*,
28 const FloatSize&) final {
29 return nullptr;
30 }
Status() const31 ResourceStatus Status() const final {
32 if (cache_pending_)
33 return ResourceStatus::kNotStarted;
34 return ResourceStatus::kCached;
35 }
IsAccelerated() const36 bool IsAccelerated() const final { return false; }
37
38 // CSSStyleValue
ToCSSValue() const39 const CSSValue* ToCSSValue() const final { return nullptr; }
GetType() const40 StyleValueType GetType() const final { return kUnknownType; }
41
42 private:
43 bool cache_pending_;
44 IntSize size_;
45 };
46
47 } // namespace
48
TEST(CSSStyleImageValueTest,PendingCache)49 TEST(CSSStyleImageValueTest, PendingCache) {
50 FakeCSSStyleImageValue style_image_value(true, IntSize(100, 100));
51 bool is_null = false;
52 EXPECT_EQ(style_image_value.intrinsicWidth(is_null), 0);
53 EXPECT_EQ(style_image_value.intrinsicHeight(is_null), 0);
54 EXPECT_EQ(style_image_value.intrinsicRatio(is_null), 0);
55 EXPECT_TRUE(is_null);
56 }
57
TEST(CSSStyleImageValueTest,ValidLoadedImage)58 TEST(CSSStyleImageValueTest, ValidLoadedImage) {
59 FakeCSSStyleImageValue style_image_value(false, IntSize(480, 120));
60 bool is_null = false;
61 EXPECT_EQ(style_image_value.intrinsicWidth(is_null), 480);
62 EXPECT_EQ(style_image_value.intrinsicHeight(is_null), 120);
63 EXPECT_EQ(style_image_value.intrinsicRatio(is_null), 4);
64 EXPECT_FALSE(is_null);
65 }
66
67 } // namespace blink
68