TreeSynchronizerTest.cpp [plain text]
#include "config.h"
#include "TreeSynchronizer.h"
#include "LayerChromium.h"
#include "cc/CCLayerImpl.h"
#include <gtest/gtest.h>
using namespace WebCore;
namespace {
class MockCCLayerImpl : public CCLayerImpl {
public:
static PassRefPtr<MockCCLayerImpl> create(LayerChromium* owner, int layerId)
{
return adoptRef(new MockCCLayerImpl(owner, layerId));
}
virtual ~MockCCLayerImpl()
{
if (m_ccLayerDestructionList)
m_ccLayerDestructionList->append(id());
}
void setCCLayerDestructionList(Vector<int>* list) { m_ccLayerDestructionList = list; }
private:
MockCCLayerImpl(LayerChromium* owner, int layerId)
: CCLayerImpl(owner, layerId)
, m_ccLayerDestructionList(0)
{
}
Vector<int>* m_ccLayerDestructionList;
};
class MockLayerChromium : public LayerChromium {
public:
static PassRefPtr<MockLayerChromium> create(Vector<int>* ccLayerDestructionList)
{
return adoptRef(new MockLayerChromium(ccLayerDestructionList));
}
virtual ~MockLayerChromium() { }
virtual PassRefPtr<CCLayerImpl> createCCLayerImpl()
{
return MockCCLayerImpl::create(this, m_layerId);
}
virtual void pushPropertiesTo(CCLayerImpl* ccLayer)
{
LayerChromium::pushPropertiesTo(ccLayer);
MockCCLayerImpl* mockCCLayer = static_cast<MockCCLayerImpl*>(ccLayer);
mockCCLayer->setCCLayerDestructionList(m_ccLayerDestructionList);
}
private:
MockLayerChromium(Vector<int>* ccLayerDestructionList)
: LayerChromium(0)
, m_ccLayerDestructionList(ccLayerDestructionList)
{
}
Vector<int>* m_ccLayerDestructionList;
};
void expectTreesAreIdentical(LayerChromium* layer, CCLayerImpl* ccLayer)
{
ASSERT_TRUE(layer);
ASSERT_TRUE(ccLayer);
EXPECT_EQ(layer->id(), ccLayer->id());
ASSERT_EQ(!!layer->maskLayer(), !!ccLayer->maskLayer());
if (layer->maskLayer())
expectTreesAreIdentical(layer->maskLayer(), ccLayer->maskLayer());
ASSERT_EQ(!!layer->replicaLayer(), !!ccLayer->replicaLayer());
if (layer->replicaLayer())
expectTreesAreIdentical(layer->replicaLayer(), ccLayer->replicaLayer());
const Vector<RefPtr<LayerChromium> >& layerChildren = layer->children();
const Vector<RefPtr<CCLayerImpl> >& ccLayerChildren = ccLayer->children();
ASSERT_EQ(layerChildren.size(), ccLayerChildren.size());
for (size_t i = 0; i < layerChildren.size(); ++i)
expectTreesAreIdentical(layerChildren[i].get(), ccLayerChildren[i].get());
}
TEST(TreeSynchronizerTest, syncSimpleTreeFromEmpty)
{
RefPtr<LayerChromium> layerTreeRoot = LayerChromium::create(0);
layerTreeRoot->addChild(LayerChromium::create(0));
layerTreeRoot->addChild(LayerChromium::create(0));
RefPtr<CCLayerImpl> ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), 0);
expectTreesAreIdentical(layerTreeRoot.get(), ccLayerTreeRoot.get());
}
TEST(TreeSynchronizerTest, syncSimpleTreeReusingLayers)
{
Vector<int> ccLayerDestructionList;
RefPtr<LayerChromium> layerTreeRoot = MockLayerChromium::create(&ccLayerDestructionList);
layerTreeRoot->addChild(MockLayerChromium::create(&ccLayerDestructionList));
layerTreeRoot->addChild(MockLayerChromium::create(&ccLayerDestructionList));
RefPtr<CCLayerImpl> ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), 0);
expectTreesAreIdentical(layerTreeRoot.get(), ccLayerTreeRoot.get());
layerTreeRoot->children()[0]->addChild(MockLayerChromium::create(&ccLayerDestructionList));
layerTreeRoot->children()[1]->removeFromParent();
int secondCCLayerId = ccLayerTreeRoot->children()[1]->id();
ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), ccLayerTreeRoot.release());
expectTreesAreIdentical(layerTreeRoot.get(), ccLayerTreeRoot.get());
ASSERT_EQ(1u, ccLayerDestructionList.size());
EXPECT_EQ(secondCCLayerId, ccLayerDestructionList[0]);
}
TEST(TreeSynchronizerTest, syncSimpleTreeAndProperties)
{
RefPtr<LayerChromium> layerTreeRoot = LayerChromium::create(0);
layerTreeRoot->addChild(LayerChromium::create(0));
layerTreeRoot->addChild(LayerChromium::create(0));
FloatPoint rootPosition = FloatPoint(2.3, 7.4);
layerTreeRoot->setPosition(rootPosition);
float firstChildOpacity = 0.25;
layerTreeRoot->children()[0]->setOpacity(firstChildOpacity);
IntSize secondChildBounds = IntSize(25, 53);
layerTreeRoot->children()[1]->setBounds(secondChildBounds);
RefPtr<CCLayerImpl> ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), 0);
expectTreesAreIdentical(layerTreeRoot.get(), ccLayerTreeRoot.get());
FloatPoint rootCCLayerPosition = ccLayerTreeRoot->position();
EXPECT_EQ(rootPosition.x(), rootCCLayerPosition.x());
EXPECT_EQ(rootPosition.y(), rootCCLayerPosition.y());
EXPECT_EQ(firstChildOpacity, ccLayerTreeRoot->children()[0]->opacity());
IntSize secondCCLayerChildBounds = ccLayerTreeRoot->children()[1]->bounds();
EXPECT_EQ(secondChildBounds.width(), secondCCLayerChildBounds.width());
EXPECT_EQ(secondChildBounds.height(), secondCCLayerChildBounds.height());
}
TEST(TreeSynchronizerTest, reuseCCLayersAfterStructuralChange)
{
Vector<int> ccLayerDestructionList;
RefPtr<LayerChromium> layerTreeRoot = MockLayerChromium::create(&ccLayerDestructionList);
layerTreeRoot->addChild(MockLayerChromium::create(&ccLayerDestructionList));
RefPtr<LayerChromium> layerA = layerTreeRoot->children()[0].get();
layerA->addChild(MockLayerChromium::create(&ccLayerDestructionList));
RefPtr<LayerChromium> layerB = layerA->children()[0].get();
layerB->addChild(MockLayerChromium::create(&ccLayerDestructionList));
RefPtr<LayerChromium> layerC = layerB->children()[0].get();
layerB->addChild(MockLayerChromium::create(&ccLayerDestructionList));
RefPtr<LayerChromium> layerD = layerB->children()[1].get();
RefPtr<CCLayerImpl> ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), 0);
expectTreesAreIdentical(layerTreeRoot.get(), ccLayerTreeRoot.get());
layerTreeRoot->removeAllChildren();
layerD->removeAllChildren();
layerTreeRoot->addChild(layerD);
layerA->removeAllChildren();
layerD->addChild(layerA);
layerC->removeAllChildren();
layerD->addChild(layerC);
layerB->removeAllChildren();
layerC->addChild(layerB);
ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), ccLayerTreeRoot.release());
expectTreesAreIdentical(layerTreeRoot.get(), ccLayerTreeRoot.get());
EXPECT_EQ(0u, ccLayerDestructionList.size());
}
TEST(TreeSynchronizerTest, syncSimpleTreeThenDestroy)
{
Vector<int> ccLayerDestructionList;
RefPtr<LayerChromium> oldLayerTreeRoot = MockLayerChromium::create(&ccLayerDestructionList);
oldLayerTreeRoot->addChild(MockLayerChromium::create(&ccLayerDestructionList));
oldLayerTreeRoot->addChild(MockLayerChromium::create(&ccLayerDestructionList));
int oldTreeRootLayerId = oldLayerTreeRoot->id();
int oldTreeFirstChildLayerId = oldLayerTreeRoot->children()[0]->id();
int oldTreeSecondChildLayerId = oldLayerTreeRoot->children()[1]->id();
RefPtr<CCLayerImpl> ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(oldLayerTreeRoot.get(), 0);
expectTreesAreIdentical(oldLayerTreeRoot.get(), ccLayerTreeRoot.get());
oldLayerTreeRoot->removeAllChildren();
RefPtr<LayerChromium> newLayerTreeRoot = LayerChromium::create(0);
ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(newLayerTreeRoot.get(), ccLayerTreeRoot.release());
expectTreesAreIdentical(newLayerTreeRoot.get(), ccLayerTreeRoot.get());
ASSERT_EQ(3u, ccLayerDestructionList.size());
EXPECT_TRUE(ccLayerDestructionList.contains(oldTreeRootLayerId));
EXPECT_TRUE(ccLayerDestructionList.contains(oldTreeFirstChildLayerId));
EXPECT_TRUE(ccLayerDestructionList.contains(oldTreeSecondChildLayerId));
}
TEST(TreeSynchronizerTest, syncMaskReplicaAndReplicaMaskLayers)
{
RefPtr<LayerChromium> layerTreeRoot = LayerChromium::create(0);
layerTreeRoot->addChild(LayerChromium::create(0));
layerTreeRoot->addChild(LayerChromium::create(0));
layerTreeRoot->addChild(LayerChromium::create(0));
RefPtr<LayerChromium> maskLayer = LayerChromium::create(0);
layerTreeRoot->children()[0]->setMaskLayer(maskLayer.get());
RefPtr<LayerChromium> replicaLayer = LayerChromium::create(0);
layerTreeRoot->children()[1]->setReplicaLayer(replicaLayer.get());
RefPtr<LayerChromium> replicaLayerWithMask = LayerChromium::create(0);
RefPtr<LayerChromium> replicaMaskLayer = LayerChromium::create(0);
replicaLayerWithMask->setMaskLayer(replicaMaskLayer.get());
layerTreeRoot->children()[2]->setReplicaLayer(replicaLayerWithMask.get());
RefPtr<CCLayerImpl> ccLayerTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), 0);
expectTreesAreIdentical(layerTreeRoot.get(), ccLayerTreeRoot.get());
}
}