Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(533)

Unified Diff: Source/core/frame/FrameViewTest.cpp

Issue 1246173002: Throttle rendering pipeline for invisible iframes (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Add layout tests. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/frame/FrameViewTest.cpp
diff --git a/Source/core/frame/FrameViewTest.cpp b/Source/core/frame/FrameViewTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..aeaf53e16d8f74f11b223e920413459b232b2b74
--- /dev/null
+++ b/Source/core/frame/FrameViewTest.cpp
@@ -0,0 +1,258 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+
+#include "core/frame/FrameView.h"
+
+#include "core/dom/Document.h"
+#include "core/frame/FrameOwner.h"
+#include "core/frame/LocalFrame.h"
+#include "core/loader/EmptyClients.h"
+#include "core/page/Page.h"
+#include "core/testing/DummyPageHolder.h"
+#include "platform/testing/UnitTestHelpers.h"
+#include <gtest/gtest.h>
+
+namespace blink {
+
+class RootFrameLoaderClient;
+
+class MockChromeClient final : public EmptyChromeClient {
esprehn 2015/09/21 21:27:21 Can we just write a Sim test instead?
Sami 2015/09/23 18:31:07 Looks much nicer that way. Done. One minor issue w
+public:
+ MockChromeClient()
+ : m_didScheduleAnimation(false) { }
+ ~MockChromeClient() override { }
+
+ void scheduleAnimation() override
+ {
+ m_didScheduleAnimation = true;
+ }
+
+ bool animationScheduled() const
+ {
+ return m_didScheduleAnimation;
+ }
+
+ void resetAnimationScheduled()
+ {
+ m_didScheduleAnimation = false;
+ }
+private:
+ bool m_didScheduleAnimation;
+};
+
+class StubFrameLoaderClientWithParent final : public EmptyFrameLoaderClient {
+public:
+ static PassOwnPtrWillBeRawPtr<StubFrameLoaderClientWithParent> create(Frame* parent)
+ {
+ return adoptPtrWillBeNoop(new StubFrameLoaderClientWithParent(parent));
+ }
+
+ DEFINE_INLINE_VIRTUAL_TRACE()
+ {
+ visitor->trace(m_parent);
+ EmptyFrameLoaderClient::trace(visitor);
+ }
+
+ Frame* parent() const override { return m_parent.get(); }
+
+private:
+ explicit StubFrameLoaderClientWithParent(Frame* parent)
+ : m_parent(parent)
+ {
+ }
+
+ RawPtrWillBeMember<Frame> m_parent;
+};
+
+class StubFrameOwner : public NoBaseWillBeGarbageCollectedFinalized<StubFrameOwner>, public FrameOwner {
+ WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(StubFrameOwner);
+public:
+ static PassOwnPtrWillBeRawPtr<StubFrameOwner> create()
+ {
+ return adoptPtrWillBeNoop(new StubFrameOwner);
+ }
+
+ DEFINE_INLINE_VIRTUAL_TRACE() { FrameOwner::trace(visitor); }
+
+ bool isLocal() const override { return false; }
+ SandboxFlags sandboxFlags() const override { return SandboxNone; }
+ void dispatchLoad() override { }
+ void renderFallbackContent() override { }
+};
+
+class FrameViewTest : public ::testing::Test {
+public:
+ using SecurityOriginStatus = FrameView::SecurityOriginStatus;
+ using ViewportVisibility = FrameView::ViewportVisibility;
+
+ void SetUp() override;
+ void TearDown() override;
+
+ LocalFrame* childFrame() const
+ {
+ return m_childFrame.get();
+ }
+
+ ViewportVisibility viewportVisibility(FrameView* frameView)
+ {
+ return frameView->m_viewportVisibility;
+ }
+
+ void enableThrottling(FrameView* frameView)
+ {
+ frameView->m_viewportVisibility = ViewportVisibility::Hidden;
+ frameView->m_securityOriginStatusForThrottling = SecurityOriginStatus::IsCrossOrigin;
+ EXPECT_TRUE(frameView->shouldThrottleRenderingPipeline());
+ }
+
+protected:
+ MockChromeClient m_mockChromeClient;
+ Page::PageClients m_pageClients;
+ OwnPtr<DummyPageHolder> m_dummyPageHolder;
+ RefPtrWillBePersistent<Document> m_document;
+ OwnPtrWillBePersistent<StubFrameOwner> m_frameOwner;
+
+ OwnPtrWillBePersistent<StubFrameLoaderClientWithParent> m_childClient;
+ RefPtrWillBePersistent<LocalFrame> m_childFrame;
+};
+
+class RootFrameLoaderClient final : public EmptyFrameLoaderClient {
+public:
+ static PassOwnPtrWillBeRawPtr<RootFrameLoaderClient> create(FrameViewTest* testSuite)
+ {
+ return adoptPtrWillBeNoop(new RootFrameLoaderClient(testSuite));
+ }
+
+ Frame* firstChild() const override { return m_testSuite->childFrame(); }
+
+private:
+ explicit RootFrameLoaderClient(FrameViewTest* testSuite)
+ : m_testSuite(testSuite)
+ {
+ }
+
+ FrameViewTest* m_testSuite;
+};
+
+void FrameViewTest::SetUp()
+{
+ // Create a root frame and one child frame. The FrameLoaderClients will connect the frames in the frame tree.
+ fillWithEmptyClients(m_pageClients);
+ m_pageClients.chromeClient = &m_mockChromeClient;
+ OwnPtrWillBePersistent<RootFrameLoaderClient> frameLoaderClient = RootFrameLoaderClient::create(this);
+ m_dummyPageHolder = DummyPageHolder::create(IntSize(640, 480), &m_pageClients, frameLoaderClient.release());
+ m_document = &m_dummyPageHolder->document();
+ m_frameOwner = StubFrameOwner::create();
+
+ m_childClient = StubFrameLoaderClientWithParent::create(m_document->frame());
+ m_childFrame = LocalFrame::create(m_childClient.get(), m_document->frame()->host(), m_frameOwner.get());
+ m_childFrame->setView(FrameView::create(m_childFrame.get(), IntSize(320, 240)));
+ m_childFrame->init();
+
+ // Also hook up the frames in the FrameView tree.
+ m_document->view()->addChild(m_childFrame->view());
+}
+
+void FrameViewTest::TearDown()
+{
+ m_document->view()->removeChild(m_childFrame->view());
+ m_childFrame->detach(FrameDetachType::Remove);
+ m_childFrame = nullptr;
+}
+
+TEST_F(FrameViewTest, viewportVisibility)
+{
+ // Initially both frames are visible.
+ FrameView* frameView = m_document->view();
+ FrameView* childFrameView = m_childFrame->view();
+ EXPECT_EQ(ViewportVisibility::Visible, viewportVisibility(frameView));
+ EXPECT_EQ(ViewportVisibility::Visible, viewportVisibility(childFrameView));
+
+ // Moving the child fully outside the parent makes it invisible.
+ childFrameView->move(0, frameView->height());
+ frameView->updateAllLifecyclePhases();
+ frameView->updateThrottling();
+ EXPECT_EQ(ViewportVisibility::Visible, viewportVisibility(frameView));
+ EXPECT_EQ(ViewportVisibility::Hidden, viewportVisibility(childFrameView));
+
+ // A partially visible child is considered visible.
+ childFrameView->move(-childFrameView->width() / 2, 0);
+ frameView->updateAllLifecyclePhases();
+ frameView->updateThrottling();
+ EXPECT_EQ(ViewportVisibility::Visible, viewportVisibility(frameView));
+ EXPECT_EQ(ViewportVisibility::Visible, viewportVisibility(childFrameView));
+}
+
+TEST_F(FrameViewTest, viewportVisibilityFullyClipped)
+{
+ // A child which is fully clipped away by its ancestor should become invisible.
+ FrameView* frameView = m_document->view();
+ FrameView* childFrameView = m_childFrame->view();
+ frameView->resize(0, 0);
+ frameView->updateAllLifecyclePhases();
+ frameView->updateThrottling();
+ EXPECT_EQ(ViewportVisibility::Visible, viewportVisibility(frameView));
+ EXPECT_EQ(ViewportVisibility::Hidden, viewportVisibility(childFrameView));
+}
+
+TEST_F(FrameViewTest, hiddenCrossOriginFramesAreThrottled)
+{
+ FrameView* frameView = m_document->view();
+ FrameView* childFrameView = m_childFrame->view();
+ EXPECT_FALSE(frameView->shouldThrottleRenderingPipeline());
+ EXPECT_FALSE(childFrameView->shouldThrottleRenderingPipeline());
+
+ // Just being hidden doesn't make a frame throttled.
+ childFrameView->move(0, frameView->height());
+ frameView->updateAllLifecyclePhases();
+ frameView->updateThrottling();
+ EXPECT_FALSE(frameView->shouldThrottleRenderingPipeline());
+ EXPECT_FALSE(childFrameView->shouldThrottleRenderingPipeline());
+
+ // Also making the frame cross origin enables throttling.
+ m_childFrame->securityContext()->setSecurityOrigin(SecurityOrigin::createUnique());
+ frameView->updateThrottling();
+ EXPECT_FALSE(frameView->shouldThrottleRenderingPipeline());
+ EXPECT_TRUE(childFrameView->shouldThrottleRenderingPipeline());
+}
+
+TEST_F(FrameViewTest, throttledLifecycleUpdate)
+{
+ FrameView* frameView = m_document->view();
+ FrameView* childFrameView = m_childFrame->view();
+ enableThrottling(childFrameView);
+
+ childFrameView->setNeedsLayout();
+ EXPECT_TRUE(childFrameView->needsLayout());
+ frameView->updateAllLifecyclePhases(DocumentLifecycle::ThrottlingMode::Allow);
+ EXPECT_TRUE(childFrameView->needsLayout());
+
+ frameView->updateAllLifecyclePhases(DocumentLifecycle::ThrottlingMode::Disallow);
+ EXPECT_FALSE(childFrameView->needsLayout());
+}
+
+TEST_F(FrameViewTest, unthrottledFrameSchedulesVisualUpdate)
+{
+ FrameView* frameView = m_document->view();
+ FrameView* childFrameView = m_childFrame->view();
+ m_childFrame->securityContext()->setSecurityOrigin(SecurityOrigin::createUnique());
+
+ // First make the child hidden to enable throttling.
+ childFrameView->move(0, frameView->height());
+ frameView->updateAllLifecyclePhases(DocumentLifecycle::ThrottlingMode::Allow);
+ m_mockChromeClient.resetAnimationScheduled();
+ frameView->updateThrottling();
+ EXPECT_FALSE(m_mockChromeClient.animationScheduled());
+
+ // Then bring it back on-screen. This should schedule a visual update.
+ childFrameView->move(0, 0);
+ frameView->updateAllLifecyclePhases(DocumentLifecycle::ThrottlingMode::Allow);
+ m_mockChromeClient.resetAnimationScheduled();
+ frameView->updateThrottling();
+ EXPECT_TRUE(m_mockChromeClient.animationScheduled());
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698