CCLayerSorterTest.cpp [plain text]
#include "config.h"
#include "cc/CCLayerSorter.h"
#include "cc/CCLayerImpl.h"
#include "cc/CCMathUtil.h"
#include "cc/CCSingleThreadProxy.h"
#include <gtest/gtest.h>
using namespace WebCore;
namespace {
TEST(CCLayerSorterTest, BasicOverlap)
{
CCLayerSorter::ABCompareResult overlapResult;
const float zThreshold = 0.1f;
float weight = 0;
TransformationMatrix neg4Translate;
neg4Translate.translate3d(0, 0, -4);
CCLayerSorter::LayerShape front(2, 2, neg4Translate);
TransformationMatrix neg5Translate;
neg5Translate.translate3d(0, 0, -5);
CCLayerSorter::LayerShape back(2, 2, neg5Translate);
overlapResult = CCLayerSorter::checkOverlap(&front, &back, zThreshold, weight);
EXPECT_EQ(CCLayerSorter::BBeforeA, overlapResult);
EXPECT_EQ(1, weight);
overlapResult = CCLayerSorter::checkOverlap(&back, &front, zThreshold, weight);
EXPECT_EQ(CCLayerSorter::ABeforeB, overlapResult);
EXPECT_EQ(1, weight);
TransformationMatrix rightTranslate;
rightTranslate.translate3d(10, 0, -5);
CCLayerSorter::LayerShape backRight(2, 2, rightTranslate);
overlapResult = CCLayerSorter::checkOverlap(&front, &backRight, zThreshold, weight);
EXPECT_EQ(CCLayerSorter::None, overlapResult);
overlapResult = CCLayerSorter::checkOverlap(&front, &front, zThreshold, weight);
EXPECT_EQ(0, weight);
}
TEST(CCLayerSorterTest, RightAngleOverlap)
{
CCLayerSorter::ABCompareResult overlapResult;
const float zThreshold = 0.1f;
float weight = 0;
TransformationMatrix perspectiveMatrix;
perspectiveMatrix.applyPerspective(1000);
TransformationMatrix leftFaceMatrix;
leftFaceMatrix.rotate3d(0, 1, 0, -90).translateRight3d(-1, 0, -5);
CCLayerSorter::LayerShape leftFace(2, 2, perspectiveMatrix * leftFaceMatrix);
TransformationMatrix frontFaceMatrix;
frontFaceMatrix.translate3d(0, 0, -4);
CCLayerSorter::LayerShape frontFace(2, 2, perspectiveMatrix * frontFaceMatrix);
overlapResult = CCLayerSorter::checkOverlap(&frontFace, &leftFace, zThreshold, weight);
EXPECT_EQ(CCLayerSorter::BBeforeA, overlapResult);
}
TEST(CCLayerSorterTest, IntersectingLayerOverlap)
{
CCLayerSorter::ABCompareResult overlapResult;
const float zThreshold = 0.1f;
float weight = 0;
TransformationMatrix perspectiveMatrix;
perspectiveMatrix.applyPerspective(1000);
TransformationMatrix frontFaceMatrix;
frontFaceMatrix.translate3d(0, 0, -4);
CCLayerSorter::LayerShape frontFace(2, 2, perspectiveMatrix * frontFaceMatrix);
TransformationMatrix throughMatrix;
throughMatrix.rotate3d(0, 1, 0, 45).translateRight3d(0, 0, -4);
CCLayerSorter::LayerShape rotatedFace(2, 2, perspectiveMatrix * throughMatrix);
overlapResult = CCLayerSorter::checkOverlap(&frontFace, &rotatedFace, zThreshold, weight);
EXPECT_NE(CCLayerSorter::None, overlapResult);
EXPECT_EQ(0, weight);
}
TEST(CCLayerSorterTest, LayersAtAngleOverlap)
{
CCLayerSorter::ABCompareResult overlapResult;
const float zThreshold = 0.1f;
float weight = 0;
TransformationMatrix transformA;
transformA.translate3d(-6, 0, 1);
CCLayerSorter::LayerShape layerA(8, 20, transformA);
TransformationMatrix transformB;
transformB.translate3d(6, 0, -1);
CCLayerSorter::LayerShape layerB(8, 20, transformB);
TransformationMatrix transformC;
transformC.rotate3d(0, 1, 0, 40);
CCLayerSorter::LayerShape layerC(8, 20, transformC);
overlapResult = CCLayerSorter::checkOverlap(&layerA, &layerC, zThreshold, weight);
EXPECT_EQ(CCLayerSorter::ABeforeB, overlapResult);
overlapResult = CCLayerSorter::checkOverlap(&layerC, &layerB, zThreshold, weight);
EXPECT_EQ(CCLayerSorter::ABeforeB, overlapResult);
overlapResult = CCLayerSorter::checkOverlap(&layerA, &layerB, zThreshold, weight);
EXPECT_EQ(CCLayerSorter::None, overlapResult);
}
TEST(CCLayerSorterTest, LayersUnderPathologicalPerspectiveTransform)
{
CCLayerSorter::ABCompareResult overlapResult;
const float zThreshold = 0.1f;
float weight = 0;
TransformationMatrix perspectiveMatrix;
perspectiveMatrix.applyPerspective(1);
TransformationMatrix transformA;
transformA.translate3d(-15, 0, -2);
CCLayerSorter::LayerShape layerA(10, 10, perspectiveMatrix * transformA);
TransformationMatrix transformB;
transformB.translate3d(0, 0, 0.7);
transformB.rotate3d(0, 45, 0);
CCLayerSorter::LayerShape layerB(10, 10, perspectiveMatrix * transformB);
FloatQuad testQuad = FloatQuad(FloatRect(FloatPoint(-0.5, -0.5), FloatSize(1, 1)));
bool clipped = false;
CCMathUtil::mapQuad(perspectiveMatrix * transformB, testQuad, clipped);
ASSERT_TRUE(clipped);
overlapResult = CCLayerSorter::checkOverlap(&layerA, &layerB, zThreshold, weight);
EXPECT_EQ(CCLayerSorter::ABeforeB, overlapResult);
}
TEST(CCLayerSorterTest, verifyExistingOrderingPreservedWhenNoZDiff)
{
DebugScopedSetImplThread thisScopeIsOnImplThread;
OwnPtr<CCLayerImpl> layer1 = CCLayerImpl::create(1);
OwnPtr<CCLayerImpl> layer2 = CCLayerImpl::create(2);
OwnPtr<CCLayerImpl> layer3 = CCLayerImpl::create(3);
OwnPtr<CCLayerImpl> layer4 = CCLayerImpl::create(4);
OwnPtr<CCLayerImpl> layer5 = CCLayerImpl::create(5);
TransformationMatrix BehindMatrix;
BehindMatrix.translate3d(0, 0, 2);
TransformationMatrix FrontMatrix;
FrontMatrix.translate3d(0, 0, 1);
layer1->setBounds(IntSize(10, 10));
layer1->setDrawTransform(BehindMatrix);
layer1->setDrawsContent(true);
layer2->setBounds(IntSize(20, 20));
layer2->setDrawTransform(BehindMatrix);
layer2->setDrawsContent(true);
layer3->setBounds(IntSize(30, 30));
layer3->setDrawTransform(FrontMatrix);
layer3->setDrawsContent(true);
layer4->setBounds(IntSize(40, 40));
layer4->setDrawTransform(FrontMatrix);
layer4->setDrawsContent(true);
layer5->setBounds(IntSize(50, 50));
layer5->setDrawTransform(BehindMatrix);
layer5->setDrawsContent(true);
Vector<CCLayerImpl*> layerList;
layerList.append(layer1.get());
layerList.append(layer2.get());
layerList.append(layer3.get());
layerList.append(layer4.get());
layerList.append(layer5.get());
ASSERT_EQ(static_cast<size_t>(5), layerList.size());
EXPECT_EQ(1, layerList[0]->id());
EXPECT_EQ(2, layerList[1]->id());
EXPECT_EQ(3, layerList[2]->id());
EXPECT_EQ(4, layerList[3]->id());
EXPECT_EQ(5, layerList[4]->id());
CCLayerSorter layerSorter;
layerSorter.sort(layerList.begin(), layerList.end());
ASSERT_EQ(static_cast<size_t>(5), layerList.size());
EXPECT_EQ(3, layerList[0]->id());
EXPECT_EQ(4, layerList[1]->id());
EXPECT_EQ(1, layerList[2]->id());
EXPECT_EQ(2, layerList[3]->id());
EXPECT_EQ(5, layerList[4]->id());
}
}