diff --git a/hueplusplus/include/linHttpHandler.h b/hueplusplus/include/linHttpHandler.h old mode 100644 new mode 100755 index 1a82342..76f4104 --- a/hueplusplus/include/linHttpHandler.h +++ b/hueplusplus/include/linHttpHandler.h @@ -37,7 +37,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return String containing the response of the host - std::string send(const std::string &msg, const std::string &adr, int port=80) const; + virtual std::string send(const std::string &msg, const std::string &adr, int port=80) const; //! \brief Function that sends a multicast request with the specified message. //! @@ -46,7 +46,7 @@ public: //! \param port Optional integer that specifies the port to which the request is sent. Default is 1900 //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5 //! \return Vector containing strings of each answer received - std::vector sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const; + virtual std::vector sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const; }; #endif diff --git a/hueplusplus/test/CMakeLists.txt b/hueplusplus/test/CMakeLists.txt index e18e811..b0816d2 100755 --- a/hueplusplus/test/CMakeLists.txt +++ b/hueplusplus/test/CMakeLists.txt @@ -34,9 +34,15 @@ endif() # define all test sources set(TEST_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/test_ExtendedColorHueStrategy.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_ExtendedColorTemperatureStrategy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_Hue.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_HueLight.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_IHttpHandler.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_Main.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_SimpleBrightnessStrategy.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_SimpleColorHueStrategy.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_SimpleColorTemperatureStrategy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_UPnP.cpp ) diff --git a/hueplusplus/test/mocks/mock_HttpHandler.h b/hueplusplus/test/mocks/mock_HttpHandler.h index bb0a682..d6e379c 100755 --- a/hueplusplus/test/mocks/mock_HttpHandler.h +++ b/hueplusplus/test/mocks/mock_HttpHandler.h @@ -32,7 +32,6 @@ class MockHttpHandler : public IHttpHandler { public: - MOCK_CONST_METHOD3( send, std::string(const std::string &msg, const std::string &adr, int port) ); MOCK_CONST_METHOD3( sendGetHTTPBody, std::string(const std::string &msg, const std::string &adr, int port) ); diff --git a/hueplusplus/test/mocks/mock_linHttpHandler.h b/hueplusplus/test/mocks/mock_linHttpHandler.h new file mode 100755 index 0000000..1fe2746 --- /dev/null +++ b/hueplusplus/test/mocks/mock_linHttpHandler.h @@ -0,0 +1,40 @@ +/** + \file mock_linHttpHandler.h + Copyright Notice\n + Copyright (C) 2017 Jan Rogall - developer\n + Copyright (C) 2017 Moritz Wirger - developer\n + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +**/ + +#ifndef _MOCK_LIN_HTTPHANDLER_H +#define _MOCK_LIN_HTTPHANDLER_H + +#include +#include + +#include + +#include "../hueplusplus/include/linHttpHandler.h" +#include "../hueplusplus/include/json/json.h" + +//! Mock Class +class MockLinHttpHandler : public linHttpHandler +{ +public: + MOCK_CONST_METHOD3( send, std::string(const std::string &msg, const std::string &adr, int port) ); + + MOCK_CONST_METHOD4( sendMulticast, std::vector(const std::string &msg, const std::string &adr, int port, int timeout) ); +}; + +#endif diff --git a/hueplusplus/test/test_ExtendedColorHueStrategy.cpp b/hueplusplus/test/test_ExtendedColorHueStrategy.cpp new file mode 100755 index 0000000..188bdc0 --- /dev/null +++ b/hueplusplus/test/test_ExtendedColorHueStrategy.cpp @@ -0,0 +1,293 @@ +#include +#include + +#include "../include/ExtendedColorHueStrategy.h" +#include "../include/json/json.h" +#include "mocks/mock_HttpHandler.h" +#include "mocks/mock_HueLight.h" +#include "testhelper.h" + +#include +#include + +TEST(ExtendedColorHueStrategy, alertHueSaturation) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["colormode"] = "invalid"; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(30000, 128, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "hs"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["hue"] = 200; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "xy"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["xy"][0] = 0.1; + test_light.getState()["state"]["xy"][1] = 0.1; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "ct"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["ct"] = 200; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, setColorTemperature(_, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) ); +} + +TEST(ExtendedColorHueStrategy, alertXY) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["colormode"] = "invalid"; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "hs"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["xy"][0] = 0.1; + test_light.getState()["state"]["xy"][1] = 0.1; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["hue"] = 200; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "xy"; + test_light.getState()["state"]["on"] = true; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "ct"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["ct"] = 200; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, setColorTemperature(_, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) ); +} + +TEST(ExtendedColorHueStrategy, alertRGB) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["colormode"] = "invalid"; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "hs"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["hue"] = 200; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "xy"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["xy"][0] = 0.1; + test_light.getState()["state"]["xy"][1] = 0.1; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "ct"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["ct"] = 200; + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorTemperature(_, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) ); +} diff --git a/hueplusplus/test/test_ExtendedColorTemperatureStrategy.cpp b/hueplusplus/test/test_ExtendedColorTemperatureStrategy.cpp new file mode 100755 index 0000000..89b8b10 --- /dev/null +++ b/hueplusplus/test/test_ExtendedColorTemperatureStrategy.cpp @@ -0,0 +1,147 @@ +#include +#include + +#include "../include/ExtendedColorTemperatureStrategy.h" +#include "../include/json/json.h" +#include "mocks/mock_HttpHandler.h" +#include "mocks/mock_HueLight.h" +#include "testhelper.h" + +#include +#include + +TEST(ExtendedColorTemperatureStrategy, setColorTemperature) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + Json::Value prep_ret; + prep_ret = Json::Value(Json::arrayValue); + prep_ret[0] = Json::Value(Json::objectValue); + prep_ret[0]["success"] = Json::Value(Json::objectValue); + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6; + prep_ret[1] = Json::Value(Json::objectValue); + prep_ret[1]["success"] = Json::Value(Json::objectValue); + prep_ret[1]["success"]["/lights/1/state/on"] = true; + prep_ret[2] = Json::Value(Json::objectValue); + prep_ret[2]["success"] = Json::Value(Json::objectValue); + prep_ret[2]["success"]["/lights/1/state/ct"] = 155; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + + test_light.getState()["state"]["colormode"] = "ct"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["ct"] = 200; + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().setColorTemperature(200, 4, test_light) ); + + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().setColorTemperature(155, 6, test_light) ); + + prep_ret[2]["success"]["/lights/1/state/ct"] = 153; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().setColorTemperature(0, 6, test_light) ); + + prep_ret[2]["success"]["/lights/1/state/ct"] = 500; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().setColorTemperature(600, 6, test_light) ); +} + +TEST(ExtendedColorTemperatureStrategy, alertTemperature) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["colormode"] = "invalid"; + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, setColorTemperature(_, _)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "hs"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["ct"] = 200; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["hue"] = 200; + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, setColorTemperature(_, _)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "xy"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["xy"][0] = 0.1; + test_light.getState()["state"]["xy"][1] = 0.1; + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, setColorTemperature(_, _)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "ct"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["ct"] = 200; + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); + + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) ); +} diff --git a/hueplusplus/test/test_HueLight.cpp b/hueplusplus/test/test_HueLight.cpp old mode 100644 new mode 100755 index db446a8..afe3853 --- a/hueplusplus/test/test_HueLight.cpp +++ b/hueplusplus/test/test_HueLight.cpp @@ -838,3 +838,18 @@ TEST_F(HueLightTest, setColorLoop) EXPECT_EQ(false, test_light_2.setColorLoop(false)); EXPECT_EQ(false, test_light_3.setColorLoop(true)); } + +TEST_F(HueLightTest, refreshState) +{ + using namespace ::testing; + test_bridge.getLight(1); + test_bridge.getLight(2); + test_bridge.getLight(3); + + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(2) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + + const HueLight ctest_light_1 = test_bridge.getLight(1); + HueLight test_light_1 = test_bridge.getLight(1); +} diff --git a/hueplusplus/test/test_IHttpHandler.cpp b/hueplusplus/test/test_IHttpHandler.cpp new file mode 100755 index 0000000..bf2826d --- /dev/null +++ b/hueplusplus/test/test_IHttpHandler.cpp @@ -0,0 +1,197 @@ +#include +#include + +#include "../include/json/json.h" +#include "mocks/mock_linHttpHandler.h" +#include "testhelper.h" + +#include +#include + +TEST(IHttpHandler, sendGetHTTPBody) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + EXPECT_CALL(handler, send("testmsg", "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillRepeatedly(Return("\r\n\r\ntestreply")); + + EXPECT_THROW(handler.sendGetHTTPBody("testmsg", "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ("testreply", handler.sendGetHTTPBody("testmsg", "192.168.2.1", 90)); +} + +TEST(IHttpHandler, sendHTTPRequest) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + EXPECT_CALL(handler, send("GET UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillRepeatedly(Return("\r\n\r\ntestreply")); + + EXPECT_THROW(handler.sendHTTPRequest("GET", "UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ("testreply", handler.sendHTTPRequest("GET", "UrI", "text/html", "body", "192.168.2.1", 90)); +} + +TEST(IHttpHandler, GETString) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + EXPECT_CALL(handler, send("GET UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillRepeatedly(Return("\r\n\r\ntestreply")); + + EXPECT_THROW(handler.GETString("UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ("testreply", handler.GETString("UrI", "text/html", "body", "192.168.2.1", 90)); +} + +TEST(IHttpHandler, POSTString) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + EXPECT_CALL(handler, send("POST UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillRepeatedly(Return("\r\n\r\ntestreply")); + + EXPECT_THROW(handler.POSTString("UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ("testreply", handler.POSTString("UrI", "text/html", "body", "192.168.2.1", 90)); +} + +TEST(IHttpHandler, PUTString) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + EXPECT_CALL(handler, send("PUT UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillRepeatedly(Return("\r\n\r\ntestreply")); + + EXPECT_THROW(handler.PUTString("UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ("testreply", handler.PUTString("UrI", "text/html", "body", "192.168.2.1", 90)); +} + +TEST(IHttpHandler, DELETEString) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + EXPECT_CALL(handler, send("DELETE UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillRepeatedly(Return("\r\n\r\ntestreply")); + + EXPECT_THROW(handler.DELETEString("UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ("testreply", handler.DELETEString("UrI", "text/html", "body", "192.168.2.1", 90)); +} + +TEST(IHttpHandler, GETJson) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + Json::Value testval; + testval["test"] = 100; + std::string expected_call = "GET UrI HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: "; + expected_call.append(std::to_string(testval.toStyledString().size())); + expected_call.append("\r\n\r\n"); + expected_call.append(testval.toStyledString()); + expected_call.append("\r\n\r\n"); + + EXPECT_CALL(handler, send(expected_call, "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillOnce(Return("\r\n\r\n")) + .WillRepeatedly(Return("\r\n\r\n{\"test\" : \"whatever\"}")); + Json::Value expected; + expected["test"] = "whatever"; + + EXPECT_THROW(handler.GETJson("UrI", testval, "192.168.2.1", 90), std::runtime_error); + EXPECT_THROW(handler.GETJson("UrI", testval, "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ(expected, handler.GETJson("UrI", testval, "192.168.2.1", 90)); +} + +TEST(IHttpHandler, POSTJson) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + Json::Value testval; + testval["test"] = 100; + std::string expected_call = "POST UrI HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: "; + expected_call.append(std::to_string(testval.toStyledString().size())); + expected_call.append("\r\n\r\n"); + expected_call.append(testval.toStyledString()); + expected_call.append("\r\n\r\n"); + + EXPECT_CALL(handler, send(expected_call, "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillOnce(Return("\r\n\r\n")) + .WillRepeatedly(Return("\r\n\r\n{\"test\" : \"whatever\"}")); + Json::Value expected; + expected["test"] = "whatever"; + + EXPECT_THROW(handler.POSTJson("UrI", testval, "192.168.2.1", 90), std::runtime_error); + EXPECT_THROW(handler.POSTJson("UrI", testval, "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ(expected, handler.POSTJson("UrI", testval, "192.168.2.1", 90)); +} + +TEST(IHttpHandler, PUTJson) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + Json::Value testval; + testval["test"] = 100; + std::string expected_call = "PUT UrI HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: "; + expected_call.append(std::to_string(testval.toStyledString().size())); + expected_call.append("\r\n\r\n"); + expected_call.append(testval.toStyledString()); + expected_call.append("\r\n\r\n"); + + EXPECT_CALL(handler, send(expected_call, "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillOnce(Return("\r\n\r\n")) + .WillRepeatedly(Return("\r\n\r\n{\"test\" : \"whatever\"}")); + Json::Value expected; + expected["test"] = "whatever"; + + EXPECT_THROW(handler.PUTJson("UrI", testval, "192.168.2.1", 90), std::runtime_error); + EXPECT_THROW(handler.PUTJson("UrI", testval, "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ(expected, handler.PUTJson("UrI", testval, "192.168.2.1", 90)); +} + +TEST(IHttpHandler, DELETEJson) +{ + using namespace ::testing; + MockLinHttpHandler handler; + + Json::Value testval; + testval["test"] = 100; + std::string expected_call = "DELETE UrI HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: "; + expected_call.append(std::to_string(testval.toStyledString().size())); + expected_call.append("\r\n\r\n"); + expected_call.append(testval.toStyledString()); + expected_call.append("\r\n\r\n"); + + EXPECT_CALL(handler, send(expected_call, "192.168.2.1", 90)) + .Times(AtLeast(2)) + .WillOnce(Return("")) + .WillOnce(Return("\r\n\r\n")) + .WillRepeatedly(Return("\r\n\r\n{\"test\" : \"whatever\"}")); + Json::Value expected; + expected["test"] = "whatever"; + + EXPECT_THROW(handler.DELETEJson("UrI", testval, "192.168.2.1", 90), std::runtime_error); + EXPECT_THROW(handler.DELETEJson("UrI", testval, "192.168.2.1", 90), std::runtime_error); + EXPECT_EQ(expected, handler.DELETEJson("UrI", testval, "192.168.2.1", 90)); +} diff --git a/hueplusplus/test/test_SimpleBrightnessStrategy.cpp b/hueplusplus/test/test_SimpleBrightnessStrategy.cpp new file mode 100755 index 0000000..867466b --- /dev/null +++ b/hueplusplus/test/test_SimpleBrightnessStrategy.cpp @@ -0,0 +1,78 @@ +#include +#include + +#include "../include/SimpleBrightnessStrategy.h" +#include "../include/json/json.h" +#include "mocks/mock_HttpHandler.h" +#include "mocks/mock_HueLight.h" +#include "testhelper.h" + +#include +#include +#include + +TEST(SimpleBrightnessStrategy, setBrightness) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + Json::Value prep_ret; + prep_ret = Json::Value(Json::arrayValue); + prep_ret[0] = Json::Value(Json::objectValue); + prep_ret[0]["success"] = Json::Value(Json::objectValue); + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6; + prep_ret[1] = Json::Value(Json::objectValue); + prep_ret[1]["success"] = Json::Value(Json::objectValue); + prep_ret[1]["success"]["/lights/1/state/on"] = true; + prep_ret[2] = Json::Value(Json::objectValue); + prep_ret[2]["success"] = Json::Value(Json::objectValue); + prep_ret[2]["success"]["/lights/1/state/bri"] = 50; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + + test_light.getState()["state"]["on"] = true; + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(0, 4, test_light) ); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(0, 4, test_light) ); + + test_light.getState()["state"]["bri"] = 0; + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(50, 6, test_light) ); + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["bri"] = 50; + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(50, 6, test_light) ); + + prep_ret[2]["success"]["/lights/1/state/bri"] = 254; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(255, 6, test_light) ); +} + +TEST(SimpleBrightnessStrategy, getBrightness) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["bri"] = 200; + EXPECT_EQ( 200, SimpleBrightnessStrategy().getBrightness(test_light) ); + test_light.getState()["state"]["bri"] = 0; + EXPECT_EQ( 0, SimpleBrightnessStrategy().getBrightness(static_cast(test_light)) ); +} diff --git a/hueplusplus/test/test_SimpleColorHueStrategy.cpp b/hueplusplus/test/test_SimpleColorHueStrategy.cpp new file mode 100755 index 0000000..41acd38 --- /dev/null +++ b/hueplusplus/test/test_SimpleColorHueStrategy.cpp @@ -0,0 +1,455 @@ +#include +#include + +#include "../include/SimpleColorHueStrategy.h" +#include "../include/json/json.h" +#include "mocks/mock_HttpHandler.h" +#include "mocks/mock_HueLight.h" +#include "testhelper.h" + +#include +#include + +TEST(SimpleColorHueStrategy, setColorHue) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + Json::Value prep_ret; + prep_ret = Json::Value(Json::arrayValue); + prep_ret[0] = Json::Value(Json::objectValue); + prep_ret[0]["success"] = Json::Value(Json::objectValue); + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6; + prep_ret[1] = Json::Value(Json::objectValue); + prep_ret[1]["success"] = Json::Value(Json::objectValue); + prep_ret[1]["success"]["/lights/1/state/on"] = true; + prep_ret[2] = Json::Value(Json::objectValue); + prep_ret[2]["success"] = Json::Value(Json::objectValue); + prep_ret[2]["success"]["/lights/1/state/hue"] = 30500; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["hue"] = 200; + test_light.getState()["state"]["colormode"] = "hs"; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorHue(200, 4, test_light) ); + + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorHue(30500, 6, test_light) ); +} + +TEST(SimpleColorHueStrategy, setColorSaturation) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + Json::Value prep_ret; + prep_ret = Json::Value(Json::arrayValue); + prep_ret[0] = Json::Value(Json::objectValue); + prep_ret[0]["success"] = Json::Value(Json::objectValue); + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6; + prep_ret[1] = Json::Value(Json::objectValue); + prep_ret[1]["success"] = Json::Value(Json::objectValue); + prep_ret[1]["success"]["/lights/1/state/on"] = true; + prep_ret[2] = Json::Value(Json::objectValue); + prep_ret[2]["success"] = Json::Value(Json::objectValue); + prep_ret[2]["success"]["/lights/1/state/sat"] = 254; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["colormode"] = "hs"; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorSaturation(100, 4, test_light) ); + + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorSaturation(255, 6, test_light) ); +} + +TEST(SimpleColorHueStrategy, setColorHueSaturation) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + Json::Value prep_ret; + prep_ret = Json::Value(Json::arrayValue); + prep_ret[0] = Json::Value(Json::objectValue); + prep_ret[0]["success"] = Json::Value(Json::objectValue); + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6; + prep_ret[1] = Json::Value(Json::objectValue); + prep_ret[1]["success"] = Json::Value(Json::objectValue); + prep_ret[1]["success"]["/lights/1/state/on"] = true; + prep_ret[2] = Json::Value(Json::objectValue); + prep_ret[2]["success"] = Json::Value(Json::objectValue); + prep_ret[2]["success"]["/lights/1/state/hue"] = 30500; + prep_ret[3] = Json::Value(Json::objectValue); + prep_ret[3]["success"] = Json::Value(Json::objectValue); + prep_ret[3]["success"]["/lights/1/state/sat"] = 254; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["hue"] = 200; + test_light.getState()["state"]["colormode"] = "hs"; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorHueSaturation(200, 100, 4, test_light) ); + + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorHueSaturation(30500, 255, 6, test_light) ); +} + +TEST(SimpleColorHueStrategy, setColorXY) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + Json::Value prep_ret; + prep_ret = Json::Value(Json::arrayValue); + prep_ret[0] = Json::Value(Json::objectValue); + prep_ret[0]["success"] = Json::Value(Json::objectValue); + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6; + prep_ret[1] = Json::Value(Json::objectValue); + prep_ret[1]["success"] = Json::Value(Json::objectValue); + prep_ret[1]["success"]["/lights/1/state/on"] = true; + prep_ret[2] = Json::Value(Json::objectValue); + prep_ret[2]["success"] = Json::Value(Json::objectValue); + prep_ret[2]["success"]["/lights/1/state/xy"][0] = 0.2355; + prep_ret[2]["success"]["/lights/1/state/xy"][1] = 0.1234; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["xy"][0] = 0.1; + test_light.getState()["state"]["xy"][1] = 0.1; + test_light.getState()["state"]["colormode"] = "xy"; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorXY(0.1, 0.1, 4, test_light) ); + + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorXY(0.2355, 0.1234, 6, test_light) ); +} + +TEST(SimpleColorHueStrategy, setColorRGB) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, setColorXY(_, _, 4)) + .Times(2) + .WillRepeatedly(Return(true)); + + EXPECT_EQ( true, SimpleColorHueStrategy().setColorRGB(128, 128, 128, 4, test_light) ); + + EXPECT_EQ( true, SimpleColorHueStrategy().setColorRGB(255, 255, 255, 4, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(4)) + .Times(1) + .WillOnce(Return(true)); + EXPECT_EQ( true, SimpleColorHueStrategy().setColorRGB(0, 0, 0, 4, test_light) ); +} + +TEST(SimpleColorHueStrategy, setColorLoop) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + Json::Value prep_ret; + prep_ret = Json::Value(Json::arrayValue); + prep_ret[0] = Json::Value(Json::objectValue); + prep_ret[0]["success"] = Json::Value(Json::objectValue); + prep_ret[0]["success"]["/lights/1/state/on"] = true; + prep_ret[1] = Json::Value(Json::objectValue); + prep_ret[1]["success"] = Json::Value(Json::objectValue); + prep_ret[1]["success"]["/lights/1/state/effect"] = "colorloop"; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["effect"] = "colorloop"; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorLoop(true, test_light) ); + + test_light.getState()["state"]["on"] = false; + test_light.getState()["state"]["effect"] = "none"; + EXPECT_EQ( true, SimpleColorHueStrategy().setColorLoop(true, test_light) ); +} + +TEST(SimpleColorHueStrategy, alertHueSaturation) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["colormode"] = "invalid"; + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(30000, 128, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "hs"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["hue"] = 200; + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_EQ( true, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "xy"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["xy"][0] = 0.1; + test_light.getState()["state"]["xy"][1] = 0.1; + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) ); +} + +TEST(SimpleColorHueStrategy, alertXY) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["colormode"] = "invalid"; + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "hs"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["xy"][0] = 0.1; + test_light.getState()["state"]["xy"][1] = 0.1; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["hue"] = 200; + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "xy"; + test_light.getState()["state"]["on"] = true; + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_EQ( true, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) ); +} + +TEST(SimpleColorHueStrategy, alertRGB) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["colormode"] = "invalid"; + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "hs"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["sat"] = 100; + test_light.getState()["state"]["hue"] = 200; + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "xy"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["xy"][0] = 0.1; + test_light.getState()["state"]["xy"][1] = 0.1; + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, setColorXY(_, _, 1)) + .Times(AtLeast(2)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( true, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) ); +} + +TEST(SimpleColorHueStrategy, getColorHueSaturation) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["hue"] = 5000; + test_light.getState()["state"]["sat"] = 128; + EXPECT_EQ( std::make_pair(static_cast(5000), static_cast(128)), SimpleColorHueStrategy().getColorHueSaturation(test_light) ); + test_light.getState()["state"]["hue"] = 50000; + test_light.getState()["state"]["sat"] = 158; + EXPECT_EQ( std::make_pair(static_cast(50000), static_cast(158)), SimpleColorHueStrategy().getColorHueSaturation(static_cast(test_light)) ); +} + +TEST(SimpleColorHueStrategy, getColorXY) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["xy"][0] = 0.1234; + test_light.getState()["state"]["xy"][1] = 0.1234; + EXPECT_EQ( std::make_pair(static_cast(0.1234), static_cast(0.1234)), SimpleColorHueStrategy().getColorXY(test_light) ); + test_light.getState()["state"]["xy"][0] = 0.12; + test_light.getState()["state"]["xy"][1] = 0.6458; + EXPECT_EQ( std::make_pair(static_cast(0.12), static_cast(0.6458)), SimpleColorHueStrategy().getColorXY(static_cast(test_light)) ); +} diff --git a/hueplusplus/test/test_SimpleColorTemperatureStrategy.cpp b/hueplusplus/test/test_SimpleColorTemperatureStrategy.cpp new file mode 100755 index 0000000..5effca0 --- /dev/null +++ b/hueplusplus/test/test_SimpleColorTemperatureStrategy.cpp @@ -0,0 +1,115 @@ +#include +#include + +#include "../include/SimpleColorTemperatureStrategy.h" +#include "../include/json/json.h" +#include "mocks/mock_HttpHandler.h" +#include "mocks/mock_HueLight.h" +#include "testhelper.h" + +#include +#include +#include + +TEST(SimpleColorTemperatureStrategy, setColorTemperature) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + Json::Value prep_ret; + prep_ret = Json::Value(Json::arrayValue); + prep_ret[0] = Json::Value(Json::objectValue); + prep_ret[0]["success"] = Json::Value(Json::objectValue); + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6; + prep_ret[1] = Json::Value(Json::objectValue); + prep_ret[1]["success"] = Json::Value(Json::objectValue); + prep_ret[1]["success"]["/lights/1/state/on"] = true; + prep_ret[2] = Json::Value(Json::objectValue); + prep_ret[2]["success"] = Json::Value(Json::objectValue); + prep_ret[2]["success"]["/lights/1/state/ct"] = 155; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["ct"] = 200; + EXPECT_EQ( true, SimpleColorTemperatureStrategy().setColorTemperature(200, 4, test_light) ); + + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorTemperatureStrategy().setColorTemperature(155, 6, test_light) ); + + prep_ret[2]["success"]["/lights/1/state/ct"] = 153; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + EXPECT_EQ( true, SimpleColorTemperatureStrategy().setColorTemperature(0, 6, test_light) ); + + prep_ret[2]["success"]["/lights/1/state/ct"] = 500; + EXPECT_CALL(test_light, SendPutRequest(_, "/state")) + .Times(1) + .WillOnce(Return(prep_ret)); + EXPECT_EQ( true, SimpleColorTemperatureStrategy().setColorTemperature(600, 6, test_light) ); +} + +TEST(SimpleColorTemperatureStrategy, alertTemperature) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["colormode"] = "invalid"; + EXPECT_EQ( false, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, setColorTemperature(_, _)) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["colormode"] = "ct"; + test_light.getState()["state"]["on"] = true; + test_light.getState()["state"]["ct"] = 200; + EXPECT_EQ( false, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, alert()) + .Times(AtLeast(2)) + .WillOnce(Return(false)) + .WillRepeatedly(Return(true)); + EXPECT_EQ( false, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_EQ( true, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) ); + + EXPECT_CALL(test_light, OffNoRefresh(_)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(true)); + test_light.getState()["state"]["on"] = false; + EXPECT_EQ( true, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) ); +} + +TEST(SimpleColorTemperatureStrategy, getColorTemperature) +{ + using namespace ::testing; + std::shared_ptr handler(std::make_shared()); + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(AtLeast(1)) + .WillRepeatedly(Return(Json::Value(Json::objectValue))); + MockHueLight test_light(handler); + EXPECT_CALL(test_light, refreshState()) + .Times(AtLeast(1)) + .WillRepeatedly(Return()); + + test_light.getState()["state"]["ct"] = 200; + EXPECT_EQ( 200, SimpleColorTemperatureStrategy().getColorTemperature(test_light) ); + test_light.getState()["state"]["ct"] = 500; + EXPECT_EQ( 500, SimpleColorTemperatureStrategy().getColorTemperature(static_cast(test_light)) ); +} diff --git a/hueplusplus/test/testhelper.h b/hueplusplus/test/testhelper.h old mode 100644 new mode 100755 index 51e676f..51e676f --- a/hueplusplus/test/testhelper.h +++ b/hueplusplus/test/testhelper.h