JD2022-TU1/main/extern/Hue/libhuestream/test/TestEngine.cpp

275 lines
10 KiB
C++

/******************************************************************************/
/* */
/* $Workfile:: main.c $ */
/* */
/* $Revision:: 28095 $ */
/* */
/* $Archive:: $ */
/* */
/* $Modtime:: $ */
/* */
/* $Date:: 2015-09-08 08:41:52 +0200 (Tue, 08 Sep 2015) $ */
/* */
/* $Author:: wies.hubbers@philips.com $ */
/* */
/* Owner:: <Enter the name of the original owner> */
/* */
/* Description:: 'Main', responsible for startup of the application */
/* */
/* Department:: B.G. Lighting Electronics */
/* */
/* Philips Company Confidential. */
/* Copyright (C) 2010 Koninklijke Philips Electronics N.V. */
/* All rights reserved. */
/* */
/******************************************************************************/
/******************************************************************************/
/* INCLUDE FILES */
/******************************************************************************/
#include "gtest/gtest.h"
#include <memory>
#include "huestream/effect/Engine.h"
#include "MockEffect.h"
#include "MockTimeManager.h"
using ::testing::Return;
using ::testing::_;
using ::testing::Sequence;
using ::testing::Property;
using ::testing::AllOf;
using ::testing::Pointee;
using namespace huestream;
/******************************************************************************/
/* PUBLIC FUNCTION IMPLEMENTATIONS */
/******************************************************************************/
class TestEngine : public testing::Test {
protected:
std::shared_ptr<Engine> _engine;
std::shared_ptr<Group> _group;
std::vector<std::shared_ptr<MockEffect>> _effects;
std::vector<Sequence> _sequences;
const static int EffectLayer0 = 0;
const static int EffectLayer1 = 1;
const static int EffectLayer2 = 2;
virtual void SetUp()
{
_group = std::make_shared<Group>();
_group->AddLight(1, 0.1, 0.1);
_group->AddLight(3, 0.5, 0.6);
_engine = std::make_shared<Engine>();
_engine->SetGroup(_group);
}
virtual void TearDown()
{
for (auto effect : _effects)
{
effect.reset();
}
_effects.clear();
_engine.reset();
_group.reset();
_sequences.clear();
}
virtual void AddEffectWithPriority(unsigned int priority)
{
auto effect = std::shared_ptr<MockEffect>(new MockEffect("testEffect"));
effect->SetPriority(priority);
_engine->SetEffect(effect);
effect->Enable();
EXPECT_CALL(*effect, Render());
EXPECT_CALL(*effect, IsFinished()).WillRepeatedly(Return(false));
_sequences.push_back(Sequence());
_effects.push_back(effect);
}
virtual void AddEffectWithPriorityNotRendered(unsigned int priority)
{
auto effect = std::shared_ptr<MockEffect>(new MockEffect("testEffect"));
effect->SetPriority(priority);
_sequences.push_back(Sequence());
_effects.push_back(effect);
_engine->SetEffect(effect);
}
virtual void SetExpectEffectGet(int effectIndex, Location pos, Color color)
{
auto effect = _effects[effectIndex];
EXPECT_CALL(*effect, GetColor(Pointee(Property(&Light::GetPosition, AllOf(Property(&Location::GetX, pos.GetX()),
Property(&Location::GetY, pos.GetY()))))))
.InSequence(_sequences[effectIndex])
.WillOnce(Return(color));
}
virtual void SetExpectionForEffectIsFinished(int effectIndex, bool isFinished)
{
auto effect = _effects[effectIndex];
EXPECT_CALL(*effect, IsFinished()).WillRepeatedly(Return(isFinished));
}
virtual void SetExpectEffectNoGet(int effectIndex)
{
auto effect = _effects[effectIndex];
EXPECT_CALL(*effect, GetColor(_)).Times(0);
}
virtual void AssertColors(Colors colors)
{
auto lights = _group->GetLights();
ASSERT_EQ(colors.size(), lights->size());
auto i = 0;
for (auto light : *lights)
{
Color s = light->GetColor();
AssertColor(colors[i++], s);
}
}
virtual void AssertColor(const Color& expectedColor, const Color& actualColor)
{
EXPECT_EQ(expectedColor.GetR(), actualColor.GetR());
EXPECT_EQ(expectedColor.GetG(), actualColor.GetG());
EXPECT_EQ(expectedColor.GetB(), actualColor.GetB());
}
};
TEST_F(TestEngine, AnEffectIsAppliedToLights)
{
auto color1 = Color(1, 0, 0);
auto color2 = Color(0, 1, 0);
auto foregroundLayer = EffectLayer0;
AddEffectWithPriority(0);
SetExpectEffectGet(foregroundLayer, Location(0.1, 0.1), color1);
SetExpectEffectGet(foregroundLayer, Location(0.5, 0.6), color2);
_engine->Render();
AssertColors(Colors::Create(2, color1, color2));
}
TEST_F(TestEngine, ColorMixing_SolidBackgroundAndForegroundColors)
{
auto color1 = Color(123, 0.456,0.654);
auto color2 = Color(122, 0.457,0.653);
auto backgroundLayer = EffectLayer0;
auto foregroundLayer = EffectLayer1;
AddEffectWithPriority(0);
AddEffectWithPriority(1);
SetExpectEffectGet(backgroundLayer, Location(0.1, 0.1), Color());
SetExpectEffectGet(backgroundLayer, Location(0.5, 0.6), Color());
SetExpectEffectGet(foregroundLayer, Location(0.1, 0.1), color1);
SetExpectEffectGet(foregroundLayer, Location(0.5, 0.6), color2);
_engine->Render();
AssertColors(Colors::Create(2, color1, color2));
}
TEST_F(TestEngine, ColorMixing_TransparentColorForegroundSolidBackground)
{
auto color1 = Color(123, 0.456,0.654);
auto color2 = Color(122, 0.457,0.653);
auto transparentColor = Color(1, 0.1, 0.2, 0);
auto backgroundLayer = EffectLayer0;
auto foregroundLayer = EffectLayer1;
AddEffectWithPriority(0);
AddEffectWithPriority(1);
SetExpectEffectGet(backgroundLayer, Location(0.1, 0.1), color1);
SetExpectEffectGet(backgroundLayer, Location(0.5, 0.6), color2);
SetExpectEffectGet(foregroundLayer, Location(0.1, 0.1), transparentColor);
SetExpectEffectGet(foregroundLayer, Location(0.5, 0.6), transparentColor);
_engine->Render();
AssertColors(Colors::Create(2, color1, color2));
}
TEST_F(TestEngine, ColorMixing_SemiTransparentColorForegroundSolidBackground)
{
auto color1 = Color(0.2, 0.2, 0.2);
auto color2 = Color(0, 0, 0);
auto semiTransparentColor1 = Color(1, 1, 1, 0.5);
auto semiTransparentColor2 = Color(1, 1, 1, 0.2);
auto resultColor1 = Color(0.6, 0.6, 0.6);
auto resultColor2 = Color(0.2, 0.2, 0.2);
auto backgroundLayer = EffectLayer0;
auto foregroundLayer = EffectLayer1;
AddEffectWithPriority(0);
AddEffectWithPriority(1);
SetExpectEffectGet(backgroundLayer, Location(0.1, 0.1), color1);
SetExpectEffectGet(backgroundLayer, Location(0.5, 0.6), color2);
SetExpectEffectGet(foregroundLayer, Location(0.1, 0.1), semiTransparentColor1);
SetExpectEffectGet(foregroundLayer, Location(0.5, 0.6), semiTransparentColor2);
_engine->Render();
AssertColors(Colors::Create(2, resultColor1, resultColor2));
}
TEST_F(TestEngine, ColorMixing_SemiTransparentColorForegroundSemiTransparentColorMiddleSolidBackground)
{
auto solidColorBackground = Color(0.2, 0.2, 0.2, 1);
auto semiTransparentColorMiddle = Color(1, 1, 1, 0.5);
auto semiTransparentColorForeground = Color(1, 1, 1, 0.5);
auto resultColor = Color(0.8, 0.8, 0.8);
auto backgroundLayer = EffectLayer0;
auto middleLayer = EffectLayer1;
auto foregroundLayer = EffectLayer2;
AddEffectWithPriority(0);
AddEffectWithPriority(1);
AddEffectWithPriority(2);
SetExpectEffectGet(backgroundLayer, Location(0.1, 0.1), solidColorBackground);
SetExpectEffectGet(middleLayer, Location(0.1, 0.1), semiTransparentColorMiddle);
SetExpectEffectGet(foregroundLayer, Location(0.1, 0.1), semiTransparentColorForeground);
SetExpectEffectGet(backgroundLayer, Location(0.5, 0.6), solidColorBackground);
SetExpectEffectGet(middleLayer, Location(0.5, 0.6), semiTransparentColorMiddle);
SetExpectEffectGet(foregroundLayer, Location(0.5, 0.6), semiTransparentColorForeground);
_engine->Render();
AssertColors(Colors::Create(2, resultColor, resultColor));
}
TEST_F(TestEngine, EffectIsNotCalledWhenNotEnabled)
{
auto foregroundLayer = EffectLayer0;
AddEffectWithPriorityNotRendered(0);
SetExpectEffectNoGet(foregroundLayer);
_engine->Render();
AssertColors(Colors::Create(2, Color(0,0,0), Color(0,0,0)));
}
TEST_F(TestEngine, RemoveEffectWhenStateOfEffectIsFinished)
{
auto foregroundLayer = EffectLayer0;
AddEffectWithPriorityNotRendered(0);
SetExpectEffectNoGet(foregroundLayer);
_effects[0]->Finish();
_engine->Render();
AssertColors(Colors::Create(2, Color(0,0,0), Color(0,0,0)));
}
/******************************************************************************/
/* END OF FILE */
/******************************************************************************/