diff options
Diffstat (limited to 'chromium/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc')
-rw-r--r-- | chromium/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc | 121 |
1 files changed, 114 insertions, 7 deletions
diff --git a/chromium/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc b/chromium/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc index 0928877b684..7388410bd99 100644 --- a/chromium/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc +++ b/chromium/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc @@ -64,7 +64,7 @@ class TestIntersectionObserverDelegate : public IntersectionObserverDelegate { return geometry.IntersectionRect(); } - void Trace(Visitor* visitor) override { + void Trace(Visitor* visitor) const override { IntersectionObserverDelegate::Trace(visitor); visitor->Trace(document_); visitor->Trace(entries_); @@ -80,11 +80,9 @@ class TestIntersectionObserverDelegate : public IntersectionObserverDelegate { class IntersectionObserverTest : public SimTest {}; -class IntersectionObserverV2Test : public IntersectionObserverTest, - public ScopedIntersectionObserverV2ForTest { +class IntersectionObserverV2Test : public IntersectionObserverTest { public: - IntersectionObserverV2Test() - : IntersectionObserverTest(), ScopedIntersectionObserverV2ForTest(true) { + IntersectionObserverV2Test() { IntersectionObserver::SetThrottleDelayEnabledForTesting(false); } @@ -237,7 +235,8 @@ TEST_F(IntersectionObserverTest, ReportsFractionOfTargetOrRoot) { MakeGarbageCollected<IntersectionObserver>( *target_observer_delegate, nullptr, Vector<Length>(), Vector<float>{kExpectedFractionOfTarget / 2}, - IntersectionObserver::kFractionOfTarget, 0, false, false); + IntersectionObserver::kFractionOfTarget, 0, false, false, + IntersectionObserver::kApplyMarginToRoot); DummyExceptionStateForTesting exception_state; target_observer->observe(target, exception_state); ASSERT_FALSE(exception_state.HadException()); @@ -248,7 +247,8 @@ TEST_F(IntersectionObserverTest, ReportsFractionOfTargetOrRoot) { MakeGarbageCollected<IntersectionObserver>( *root_observer_delegate, nullptr, Vector<Length>(), Vector<float>{kExpectedFractionOfRoot / 2}, - IntersectionObserver::kFractionOfRoot, 0, false, false); + IntersectionObserver::kFractionOfRoot, 0, false, false, + IntersectionObserver::kApplyMarginToRoot); root_observer->observe(target, exception_state); ASSERT_FALSE(exception_state.HadException()); @@ -950,4 +950,111 @@ TEST_F(IntersectionObserverV2Test, BasicTransform) { EXPECT_FALSE(observer_delegate->LastEntry()->isVisible()); } +TEST_F(IntersectionObserverTest, ApplyMarginToTarget) { + WebView().MainFrameWidget()->Resize(WebSize(200, 200)); + SimRequest main_resource("https://example.com/", "text/html"); + LoadURL("https://example.com/"); + main_resource.Complete(R"HTML( + <style> + #scroller { height: 100px; overflow: scroll; } + #target { width: 50px; height: 50px; } + .spacer { height: 105px; } + </style> + <div id=scroller> + <div class=spacer></div> + <div id=target></div> + </div> + )HTML"); + + Element* target = GetDocument().getElementById("target"); + ASSERT_TRUE(target); + + TestIntersectionObserverDelegate* root_margin_delegate = + MakeGarbageCollected<TestIntersectionObserverDelegate>(GetDocument()); + IntersectionObserver* root_margin_observer = + MakeGarbageCollected<IntersectionObserver>( + *root_margin_delegate, nullptr, Vector<Length>{Length::Fixed(10)}, + Vector<float>{std::numeric_limits<float>::min()}, + IntersectionObserver::kFractionOfTarget, 0, false, false, + IntersectionObserver::kApplyMarginToRoot); + + DummyExceptionStateForTesting exception_state; + root_margin_observer->observe(target, exception_state); + ASSERT_FALSE(exception_state.HadException()); + + TestIntersectionObserverDelegate* target_margin_delegate = + MakeGarbageCollected<TestIntersectionObserverDelegate>(GetDocument()); + // Same parameters as above except that margin is applied to target. + IntersectionObserver* target_margin_observer = + MakeGarbageCollected<IntersectionObserver>( + *target_margin_delegate, nullptr, Vector<Length>{Length::Fixed(10)}, + Vector<float>{std::numeric_limits<float>::min()}, + IntersectionObserver::kFractionOfTarget, 0, false, false, + IntersectionObserver::kApplyMarginToTarget); + + target_margin_observer->observe(target, exception_state); + ASSERT_FALSE(exception_state.HadException()); + + Compositor().BeginFrame(); + test::RunPendingTasks(); + ASSERT_FALSE(Compositor().NeedsBeginFrame()); + + EXPECT_EQ(root_margin_delegate->CallCount(), 1); + EXPECT_EQ(root_margin_delegate->EntryCount(), 1); + // Since the inner scroller clips content, the root margin has no effect and + // target is not intersecting. + EXPECT_FALSE(root_margin_delegate->LastEntry()->isIntersecting()); + + EXPECT_EQ(target_margin_delegate->CallCount(), 1); + EXPECT_EQ(target_margin_delegate->EntryCount(), 1); + // Since the margin is applied to the target, the inner scroller clips an + // expanded rect, which ends up being visible in the root. Hence, it is + // intersecting. + EXPECT_TRUE(target_margin_delegate->LastEntry()->isIntersecting()); +} + +TEST_F(IntersectionObserverTest, TargetMarginPercentResolvesAgainstRoot) { + WebView().MainFrameWidget()->Resize(WebSize(200, 500)); + SimRequest main_resource("https://example.com/", "text/html"); + LoadURL("https://example.com/"); + main_resource.Complete(R"HTML( + <style> + #scroller { height: 100px; overflow: scroll; } + #target { width: 50px; height: 50px; } + .spacer { height: 145px; } + </style> + <div id=scroller> + <div class=spacer></div> + <div id=target></div> + </div> + )HTML"); + + Element* target = GetDocument().getElementById("target"); + ASSERT_TRUE(target); + + TestIntersectionObserverDelegate* target_margin_delegate = + MakeGarbageCollected<TestIntersectionObserverDelegate>(GetDocument()); + // 10% margin on a target would be 5px if it resolved against target, which is + // not enough to intersect. It would be 10px if it resolved against the + // scroller, which is also not enough. However, it would be 50px if it + // resolved against root, which would make it intersecting. + IntersectionObserver* target_margin_observer = + MakeGarbageCollected<IntersectionObserver>( + *target_margin_delegate, nullptr, Vector<Length>{Length::Percent(10)}, + Vector<float>{std::numeric_limits<float>::min()}, + IntersectionObserver::kFractionOfTarget, 0, false, false, + IntersectionObserver::kApplyMarginToTarget); + + DummyExceptionStateForTesting exception_state; + target_margin_observer->observe(target, exception_state); + ASSERT_FALSE(exception_state.HadException()); + + Compositor().BeginFrame(); + test::RunPendingTasks(); + ASSERT_FALSE(Compositor().NeedsBeginFrame()); + + EXPECT_EQ(target_margin_delegate->CallCount(), 1); + EXPECT_EQ(target_margin_delegate->EntryCount(), 1); + EXPECT_TRUE(target_margin_delegate->LastEntry()->isIntersecting()); +} } // namespace blink |