Commit bb561e4c authored by Stephen McGruer's avatar Stephen McGruer Committed by Commit Bot

[Code Health] Cleanup CSSPaintValue{Test}

A few cleanups for CSSPaintValue and its unittest:

  * Remove unused Create functions
  * Add test for delaying paint until the generator is ready
  * Inline the HTML for the existing tests and make them more unittesty

Bug: None
Change-Id: I9d3e5a7c6abd5fbc5443c9408480567ef0ff3b39
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1622911Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Reviewed-by: default avatarXida Chen <xidachen@chromium.org>
Reviewed-by: default avatarAnders Hartvoll Ruud <andruud@chromium.org>
Commit-Queue: Stephen McGruer <smcgruer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662611}
parent 3714f9f3
...@@ -74,7 +74,6 @@ group("blink_unittests_data") { ...@@ -74,7 +74,6 @@ group("blink_unittests_data") {
"../core/testing/data/", "../core/testing/data/",
"../core/paint/test_data/", "../core/paint/test_data/",
"../core/animation/test_data/", "../core/animation/test_data/",
"../core/css/test_data/",
] ]
} }
......
...@@ -28,6 +28,12 @@ CSSPaintImageGenerator* CSSPaintImageGenerator::Create(const String& name, ...@@ -28,6 +28,12 @@ CSSPaintImageGenerator* CSSPaintImageGenerator::Create(const String& name,
return g_create_function(name, document, observer); return g_create_function(name, document, observer);
} }
// static
CSSPaintImageGenerator::CSSPaintImageGeneratorCreateFunction*
CSSPaintImageGenerator::GetCreateFunctionForTesting() {
return &g_create_function;
}
CSSPaintImageGenerator::~CSSPaintImageGenerator() = default; CSSPaintImageGenerator::~CSSPaintImageGenerator() = default;
} // namespace blink } // namespace blink
...@@ -45,6 +45,7 @@ class CORE_EXPORT CSSPaintImageGenerator ...@@ -45,6 +45,7 @@ class CORE_EXPORT CSSPaintImageGenerator
const Document&, const Document&,
Observer*); Observer*);
static void Init(CSSPaintImageGeneratorCreateFunction); static void Init(CSSPaintImageGeneratorCreateFunction);
static CSSPaintImageGeneratorCreateFunction* GetCreateFunctionForTesting();
// Invokes the CSS Paint API 'paint' callback. May return a nullptr // Invokes the CSS Paint API 'paint' callback. May return a nullptr
// representing an invalid image if an error occurred. // representing an invalid image if an error occurred.
......
...@@ -18,16 +18,6 @@ namespace blink { ...@@ -18,16 +18,6 @@ namespace blink {
class CORE_EXPORT CSSPaintValue : public CSSImageGeneratorValue { class CORE_EXPORT CSSPaintValue : public CSSImageGeneratorValue {
public: public:
static CSSPaintValue* Create(CSSCustomIdentValue* name) {
return MakeGarbageCollected<CSSPaintValue>(name);
}
static CSSPaintValue* Create(
CSSCustomIdentValue* name,
Vector<scoped_refptr<CSSVariableData>>& variable_data) {
return MakeGarbageCollected<CSSPaintValue>(name, variable_data);
}
explicit CSSPaintValue(CSSCustomIdentValue* name); explicit CSSPaintValue(CSSCustomIdentValue* name);
CSSPaintValue(CSSCustomIdentValue* name, CSSPaintValue(CSSCustomIdentValue* name,
Vector<scoped_refptr<CSSVariableData>>&); Vector<scoped_refptr<CSSVariableData>>&);
......
...@@ -5,78 +5,142 @@ ...@@ -5,78 +5,142 @@
#include "third_party/blink/renderer/core/css/css_paint_value.h" #include "third_party/blink/renderer/core/css/css_paint_value.h"
#include <memory> #include <memory>
#include "base/auto_reset.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/css_custom_ident_value.h" #include "third_party/blink/renderer/core/css/css_custom_ident_value.h"
#include "third_party/blink/renderer/core/css/css_syntax_descriptor.h"
#include "third_party/blink/renderer/core/dom/document.h" #include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/frame_test_helpers.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/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/layout/layout_object.h" #include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/style/computed_style.h" #include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/testing/core_unit_test_helper.h" #include "third_party/blink/renderer/core/testing/core_unit_test_helper.h"
#include "third_party/blink/renderer/platform/graphics/paint_generated_image.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h" #include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h" #include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
using testing::_;
using testing::NiceMock;
using testing::Return;
using testing::ReturnRef;
using testing::Values;
namespace blink { namespace blink {
namespace {
using CSSPaintValueTest = RenderingTest;
class CSSPaintValueTest : public RenderingTest { class MockCSSPaintImageGenerator : public CSSPaintImageGenerator {
public: public:
void LoadTestData(const std::string& file_name) { MOCK_METHOD3(Paint,
String testing_path = scoped_refptr<Image>(const ImageResourceObserver&,
test::BlinkRootDir() + "/renderer/core/css/test_data/"; const FloatSize& container_size,
WebURL url = url_test_helpers::RegisterMockedURLLoadFromBase( const CSSStyleValueVector*));
WebString::FromUTF8(base_url_), testing_path, MOCK_CONST_METHOD0(NativeInvalidationProperties, Vector<CSSPropertyID>&());
WebString::FromUTF8(file_name)); MOCK_CONST_METHOD0(CustomInvalidationProperties, Vector<AtomicString>&());
frame_test_helpers::LoadFrame(helper_.GetWebView()->MainFrameImpl(), MOCK_CONST_METHOD0(HasAlpha, bool());
base_url_ + file_name); MOCK_CONST_METHOD0(InputArgumentTypes, Vector<CSSSyntaxDescriptor>&());
ForceFullCompositingUpdate(); MOCK_CONST_METHOD0(IsImageGeneratorReady, bool());
url_test_helpers::RegisterMockedURLUnregister(url); MOCK_CONST_METHOD0(WorkletId, int());
}
void ForceFullCompositingUpdate() {
helper_.GetWebView()->MainFrameWidget()->UpdateAllLifecyclePhases(
WebWidget::LifecycleUpdateReason::kTest);
}
LocalFrame* GetFrame() const { return helper_.LocalMainFrame()->GetFrame(); }
private:
void SetUp() override {
RenderingTest::SetUp();
EnableCompositing();
helper_.Initialize(nullptr, nullptr, nullptr);
base_url_ = "https://www.test.com/";
}
frame_test_helpers::WebViewHelper helper_;
std::string base_url_;
}; };
void CheckTargetObject(Document* document) { // CSSPaintImageGenerator requires that CSSPaintImageGeneratorCreateFunction be
LayoutObject* target_layout_object = // a static method. As such, it cannot access a class member and so instead we
document->getElementById("target")->GetLayoutObject(); // store a pointer to the overriding generator globally.
EXPECT_NE(target_layout_object, nullptr); MockCSSPaintImageGenerator* g_override_generator = nullptr;
EXPECT_NE(target_layout_object->Style()->InsideLink(), CSSPaintImageGenerator* ProvideOverrideGenerator(
EInsideLink::kNotInsideLink); const String&,
const Document&,
auto* css_paint_value = MakeGarbageCollected<CSSPaintValue>( CSSPaintImageGenerator::Observer*) {
MakeGarbageCollected<CSSCustomIdentValue>("linkpainter")); return g_override_generator;
EXPECT_EQ(css_paint_value->GetImage(*target_layout_object, *document, }
target_layout_object->StyleRef(), } // namespace
FloatSize(100.0f, 100.0f)),
nullptr); TEST_F(CSSPaintValueTest, DelayPaintUntilGeneratorReady) {
NiceMock<MockCSSPaintImageGenerator>* mock_generator =
MakeGarbageCollected<NiceMock<MockCSSPaintImageGenerator>>();
base::AutoReset<MockCSSPaintImageGenerator*> scoped_override_generator(
&g_override_generator, mock_generator);
base::AutoReset<CSSPaintImageGenerator::CSSPaintImageGeneratorCreateFunction>
scoped_create_function(
CSSPaintImageGenerator::GetCreateFunctionForTesting(),
ProvideOverrideGenerator);
Vector<CSSSyntaxDescriptor> input_argument_types;
ON_CALL(*mock_generator, InputArgumentTypes())
.WillByDefault(ReturnRef(input_argument_types));
const FloatSize target_size(100, 100);
ON_CALL(*mock_generator, Paint(_, _, _))
.WillByDefault(Return(PaintGeneratedImage::Create(nullptr, target_size)));
SetBodyInnerHTML(R"HTML(
<div id="target"></div>
)HTML");
LayoutObject* target = GetLayoutObjectByElementId("target");
const ComputedStyle& style = *target->Style();
auto* ident = MakeGarbageCollected<CSSCustomIdentValue>("testpainter");
CSSPaintValue* paint_value = MakeGarbageCollected<CSSPaintValue>(ident);
// Initially the generator is not ready, so GetImage should fail.
EXPECT_FALSE(
paint_value->GetImage(*target, GetDocument(), style, target_size));
// Now mark the generator as ready - GetImage should then succeed.
EXPECT_CALL(*mock_generator, IsImageGeneratorReady()).WillOnce(Return(true));
EXPECT_TRUE(
paint_value->GetImage(*target, GetDocument(), style, target_size));
} }
// Regression test for https://crbug.com/835589. // Regression test for https://crbug.com/835589.
TEST_F(CSSPaintValueTest, CSSPaintDoNotPaintForLink) { TEST_F(CSSPaintValueTest, DoNotPaintForLink) {
LoadTestData("csspaint-do-not-paint-for-link.html"); SetBodyInnerHTML(R"HTML(
Document* document = GetFrame()->GetDocument(); <style>
CheckTargetObject(document); a {
background-image: paint(linkpainter);
width: 100px;
height: 100px;
}
</style>
<a href="http://www.example.com" id="target"></a>
)HTML");
LayoutObject* target = GetLayoutObjectByElementId("target");
const ComputedStyle& style = *target->Style();
ASSERT_NE(style.InsideLink(), EInsideLink::kNotInsideLink);
auto* ident = MakeGarbageCollected<CSSCustomIdentValue>("linkpainter");
CSSPaintValue* paint_value = MakeGarbageCollected<CSSPaintValue>(ident);
EXPECT_FALSE(paint_value->GetImage(*target, GetDocument(), style,
FloatSize(100, 100)));
} }
// Regression test for https://crbug.com/835589. // Regression test for https://crbug.com/835589.
TEST_F(CSSPaintValueTest, CSSPaintDoNotPaintWhenParentHasLink) { TEST_F(CSSPaintValueTest, DoNotPaintWhenAncestorHasLink) {
LoadTestData("csspaint-do-not-paint-for-link-descendant.html"); SetBodyInnerHTML(R"HTML(
Document* document = GetFrame()->GetDocument(); <style>
CheckTargetObject(document); a {
width: 200px;
height: 200px;
}
b {
background-image: paint(linkpainter);
width: 100px;
height: 100px;
}
</style>
<a href="http://www.example.com" id="ancestor">
<b id="target"></b>
</a>
)HTML");
LayoutObject* target = GetLayoutObjectByElementId("target");
const ComputedStyle& style = *target->Style();
ASSERT_NE(style.InsideLink(), EInsideLink::kNotInsideLink);
auto* ident = MakeGarbageCollected<CSSCustomIdentValue>("linkpainter");
CSSPaintValue* paint_value = MakeGarbageCollected<CSSPaintValue>(ident);
EXPECT_FALSE(paint_value->GetImage(*target, GetDocument(), style,
FloatSize(100, 100)));
} }
} // namespace blink } // namespace blink
<!DOCTYPE html>
<html>
<head>
<style>
a {
width: 200px;
height: 200px;
}
b {
background-image: paint(linkpainter);
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<a id="ancestor">
<b id="target"></b>
</a>
<script>
function addPaintletFromSource (src) {
var uri = 'data:application/javascript;charset=utf-8;base64,' + btoa(src);
CSS.paintWorklet.addModule(uri).then();
}
addPaintletFromSource(`
class LinkPainter {
paint(ctx, geom) {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, geom.width, geom.height);
}
}
registerPaint('linkpainter', LinkPainter);
`);
ancestor.href = 'https://cs.chromium.org';
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
a {
background-image: paint(linkpainter);
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<a id="target"></a>
<script>
function addPaintletFromSource (src) {
var uri = 'data:application/javascript;charset=utf-8;base64,' + btoa(src);
CSS.paintWorklet.addModule(uri).then();
}
addPaintletFromSource(`
class LinkPainter {
paint(ctx, geom) {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, geom.width, geom.height);
}
}
registerPaint('linkpainter', LinkPainter);
`);
target.href = 'https://cs.chromium.org';
</script>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment