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 | 80 |
1 files changed, 66 insertions, 14 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 295aeed752e..3d705c305f3 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 @@ -7,6 +7,7 @@ #include "third_party/blink/renderer/core/dom/element.h" #include "third_party/blink/renderer/core/exported/web_view_impl.h" #include "third_party/blink/renderer/core/frame/local_frame_view.h" +#include "third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.h" #include "third_party/blink/renderer/core/intersection_observer/intersection_observer_delegate.h" #include "third_party/blink/renderer/core/intersection_observer/intersection_observer_init.h" #include "third_party/blink/renderer/core/layout/layout_view.h" @@ -26,6 +27,12 @@ class TestIntersectionObserverDelegate : public IntersectionObserverDelegate { public: TestIntersectionObserverDelegate(Document& document) : document_(document), call_count_(0) {} + // TODO(szager): Add tests for the synchronous delivery code path. There is + // already some indirect coverage by unit tests exercising features that rely + // on it, but we should have some direct coverage in here. + IntersectionObserver::DeliveryBehavior GetDeliveryBehavior() const override { + return IntersectionObserver::kPostTaskToDeliver; + } void Deliver(const HeapVector<Member<IntersectionObserverEntry>>& entries, IntersectionObserver&) override { call_count_++; @@ -38,11 +45,11 @@ class TestIntersectionObserverDelegate : public IntersectionObserverDelegate { FloatRect LastIntersectionRect() const { if (entries_.IsEmpty()) return FloatRect(); - const IntersectionObserverEntry* entry = entries_.back(); - return FloatRect(entry->intersectionRect()->x(), - entry->intersectionRect()->y(), - entry->intersectionRect()->width(), - entry->intersectionRect()->height()); + const IntersectionGeometry& geometry = entries_.back()->GetGeometry(); + return FloatRect(geometry.IntersectionRect().X(), + geometry.IntersectionRect().Y(), + geometry.IntersectionRect().Width(), + geometry.IntersectionRect().Height()); } void Trace(blink::Visitor* visitor) override { @@ -102,15 +109,15 @@ TEST_F(IntersectionObserverTest, NotificationSentWhenRootRemoved) { SimRequest main_resource("https://example.com/", "text/html"); LoadURL("https://example.com/"); main_resource.Complete(R"HTML( -<style> -#target { - width: 100px; - height: 100px; -} -</style> -<div id='root'> - <div id='target'></div> -</div> + <style> + #target { + width: 100px; + height: 100px; + } + </style> + <div id='root'> + <div id='target'></div> + </div> )HTML"); Element* root = GetDocument().getElementById("root"); @@ -333,6 +340,10 @@ TEST_F(IntersectionObserverTest, DisconnectClearsNotifications) { Element* target = GetDocument().getElementById("target"); ASSERT_TRUE(target); observer->observe(target, exception_state); + EXPECT_EQ(GetDocument() + .EnsureIntersectionObserverController() + .GetTrackedTargetCountForTesting(), + 1u); Compositor().BeginFrame(); test::RunPendingTasks(); @@ -344,6 +355,10 @@ TEST_F(IntersectionObserverTest, DisconnectClearsNotifications) { kProgrammaticScroll); Compositor().BeginFrame(); observer->disconnect(); + EXPECT_EQ(GetDocument() + .EnsureIntersectionObserverController() + .GetTrackedTargetCountForTesting(), + 0u); test::RunPendingTasks(); EXPECT_EQ(observer_delegate->CallCount(), 1); } @@ -406,6 +421,43 @@ TEST_F(IntersectionObserverTest, RootIntersectionWithForceZeroLayoutHeight) { EXPECT_TRUE(observer_delegate->LastIntersectionRect().IsEmpty()); } +TEST_F(IntersectionObserverTest, TrackedTargetBookkeeping) { + SimRequest main_resource("https://example.com/", "text/html"); + LoadURL("https://example.com/"); + main_resource.Complete(R"HTML( + <style> + </style> + <div id='target'></div> + )HTML"); + + Element* target = GetDocument().getElementById("target"); + ASSERT_TRUE(target); + IntersectionObserverInit* observer_init = IntersectionObserverInit::Create(); + DummyExceptionStateForTesting exception_state; + TestIntersectionObserverDelegate* observer_delegate = + MakeGarbageCollected<TestIntersectionObserverDelegate>(GetDocument()); + IntersectionObserver* observer1 = IntersectionObserver::Create( + observer_init, *observer_delegate, exception_state); + ASSERT_FALSE(exception_state.HadException()); + observer1->observe(target, exception_state); + ASSERT_FALSE(exception_state.HadException()); + IntersectionObserver* observer2 = IntersectionObserver::Create( + observer_init, *observer_delegate, exception_state); + ASSERT_FALSE(exception_state.HadException()); + observer2->observe(target, exception_state); + ASSERT_FALSE(exception_state.HadException()); + + IntersectionObserverController& controller = + GetDocument().EnsureIntersectionObserverController(); + EXPECT_EQ(controller.GetTrackedTargetCountForTesting(), 1u); + observer1->unobserve(target, exception_state); + ASSERT_FALSE(exception_state.HadException()); + EXPECT_EQ(controller.GetTrackedTargetCountForTesting(), 1u); + observer2->unobserve(target, exception_state); + ASSERT_FALSE(exception_state.HadException()); + EXPECT_EQ(controller.GetTrackedTargetCountForTesting(), 0u); +} + TEST_F(IntersectionObserverV2Test, TrackVisibilityInit) { IntersectionObserverInit* observer_init = IntersectionObserverInit::Create(); DummyExceptionStateForTesting exception_state; |